var dpt;//determines department to be sended
var suf;//determines suffix of varibles

function createIFrame() {
  var id = 'f' + Math.floor(Math.random() * 99999);
  var div = document.createElement('div');
  div.innerHTML = '<iframe style="display:none" src="about:blank" id="'+id+'" name="'+id+'" onload="sendComplete(\''+id+'\')"></iframe>';
  document.body.appendChild(div);
  return document.getElementById(id);
}

function setDepartment(d, s)
{
    dpt = d;
    suf = s;
}
function sendapplyForm(form, url, func, arg) {
  if (!document.createElement) return; // not supported
  if (typeof(form)=="string") form=document.getElementById(form);
        var i;	
	var arr = [];
	arr[0] = $('#js_name'+suf).val();
	arr[1] = $('#js_email'+suf).val();
	arr[2] = '';
	arr[3] = $('#js_file'+suf).val();
	arr[4] = $('#js_subject'+suf).val();
	arr[5] = $('#js_message'+suf).val();

    //name check
   if ('' == arr[0])
	{
	   window.alert('Please enter your name'); return;
	}
    //message check
	if ('' == arr[5])
	{
	   window.alert('Please enter message'); return;
	}
   //email check
	var valid_email = /^.+\@.+\..+$/i.test(arr[1]);
   if(!valid_email)
	{
          window.alert('Please enter correct email');return;
   }

  var frame=createIFrame();
  frame.onSendComplete = function() 
{ 
func(arg, getIFrameXML(frame)); 
};

  form.setAttribute('target', frame.id);
  form.setAttribute('action', url);  

  form.submit();
}

function sendComplete(id) {
  var iframe=document.getElementById(id);
  if (iframe.onSendComplete && typeof(iframe.onSendComplete) == 'function') iframe.onSendComplete();
}

function getIFrameXML(iframe) {
  var doc=iframe.contentDocument;
  if (!doc && iframe.contentWindow) doc=iframe.contentWindow.document;
  if (!doc) doc=window.frames[iframe.id].document;
  if (!doc) return null;
  if (doc.location=="about:blank") return null;
  if (doc.XMLDocument) doc=doc.XMLDocument;
  return doc;
}

var cnt=0;

function uploadComplete(element, doc) 
{
  if (!doc) return;
  if (doc.documentElement.firstChild.nodeValue!='false')
  {	
	 itsOk();
  }
  if (doc.documentElement.firstChild.nodeValue=='false') alert('message not sended');
}
function itsOk()
{
   alert('Message send');
   document.getElementById(dpt + '_form').reset();
   return;
}