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 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] = document.getElementById('_first_name').value;
	arr[1] = document.getElementById('_email').value;
	arr[2] = '';
	arr[3] = document.getElementById('tbFile').value;
	arr[4] = document.getElementById('tbMessage1').value;
	arr[5] = document.getElementById('_other_interest').value;

	//checkboxes
	for (var i=1; i<=6; i++) {
		var str = 'checkbox' + i;
		if (document.getElementById(str).checked==true)
		{
		arr[2] = arr[2]+document.getElementById(str).value+', ';
		
		}
		}
	/*if (''==arr[0])
	{
	   window.alert('Please enter your name'); return;
	}*/

	if ('' == arr[3])
	{
	   window.alert('Please attach your CV'); return;
	}

	if ((''==arr[2])&&(''==arr[5]))
	{
	   window.alert('Please choose the vacancy'); return;
	}


	for (i = 0; i<arr.length-1; i++)
	{ 
	  if ('' ==  arr[i]) { window.alert('Please fill in all fields'); return; }
	}

	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);  
	//add hidden field
  var ua=navigator.userAgent.toLowerCase();
  if(ua.indexOf("msie")!=-1&&ua.indexOf("opera")==-1&&ua.indexOf("webtv")==-1)
	{
            x="vacancies";
	    tn = document.createElement("<input type='hidden' name='"+x+"' value='"+arr[2]+' '+arr[5]+"'>")
            form.appendChild(tn);
	}
  else
	{
	    tn = document.createElement("input");
 	    form.appendChild(tn);
    	    tn.type = "hidden";
  	    tn.name = "vacancies";
  	    tn.value = arr[2]+' '+arr[5];
	}

  form.submit();
//  showApplyForm(2);	
}

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 (typeof(element)=="string") {element=document.getElementById(element);
  element.innerHTML="<BR><BR><BR><BR><div><B>You can attach only *.doc, *.rtf, *.pdf or *.txt files.</B></div>";
  if (doc.documentElement.firstChild.nodeValue!='false')
  {	
	  showApplyForm(2);	
  }
  
  }
}

function getHTML()
  { 
    var _path = document.getElementById('full_url').value;
    var url =_path + 'templates/applynow.html';

    //var myAjax = new Ajax.Updater('placeholder', url, {method: 'post'});
    $('#placeholder').load(url);
  }

function showApplyForm(howHideOrShowForm)
{
	switch (howHideOrShowForm)
	{
	//quick close
	case 0: document.getElementById('applynow_form').style.display = 'none'; break;
	//slow open
	case 1: Effect.SlideDown('applynow_form'); 
	document.getElementById('_page1').style.display = 'block'; 
	document.getElementById('_page2').style.display = 'none';
	clearApplyNowForm();
	break;
	//small window
	case 2:  
	document.getElementById('_page1').style.display = 'none'; 
	document.getElementById('_page2').style.display = 'block';
	break;
	}	
}





function clearApplyNowForm()
{
	alert('test');
	document.getElementById('_first_name').value = '';
	document.getElementById('_email').value='';
	document.getElementById('tbFile').value='';
	document.getElementById('tbMessage1').value='';
	document.getElementById('_other_interest').value='';
	for (var i=0; i < document.apply_now_form.checkbox.length; i++) 
	{
		document.getElementById('apply_now_form').checkbox[i].checked = false;
	}
}
