function Trim(s) 
{
		while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
		{ s = s.substring(1,s.length); }
		while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
		{ s = s.substring(0,s.length-1); }
		return s;
} 

function validator_isIntNumeric(str){
	return ((str.search(/[^1234567890-]/gi)==-1)?(true):(false));
};

function validator_isEmail(str){
	var z=str.replace(/[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*[@]{1}[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]{2,})+/gi,"");
	if (z!="") return false; else return true;
};


function checkForm(f)
{
	//check length of some fields
	var fields="strLastName,strFirstName,strAddress1,strZip,strCity,strCountry,strPhone,strEmail,intDD1,intMM1,intYYYY1,strAcademicDegree,strAddress1_unit,strZip_unit,strCity_unit,strCountry_unit,strPhone_unit,strReasonForApplication".split(',');
	var err=0;
	for (var i=0; i<fields.length; i++)
	{
		var field=f[fields[i]];
		if ((Trim(field.value)=="") || (field.value==0))
		{
			err=1;
			if (field.options)
				field.options[field.selectedIndex].style.backgroundColor='red';
			field.style.backgroundColor='red';
		}else
		{
			if (field.options)
				field.options[field.selectedIndex].style.backgroundColor='';
			field.style.backgroundColor='';
		}	
	}

	
	var received = document.forms.myForm.ysnHaveReceived;
	var receivedValue=0;
	if (received.length){
		for (var i=0; i<received.length; i++)
			if (received[i].checked)
				receivedValue=received[i].value;
	} else receivedValue=received.value;
	if (parseInt(receivedValue) && Trim(f["memReceivedBeforeInfo"].value)==""){
		err=1;
		f["memReceivedBeforeInfo"].style.backgroundColor='red';
	}
		
		
	if (confirmation==1)
		if ((Trim(f["strConfirmNumber"].value)=="") || (f["strConfirmNumber"].value==0))
		{
			err=1;
			f["strConfirmNumber"].style.backgroundColor='red';
		}	
	if (uploadCV==1)
		if ((Trim(f["fileCV"].value)=="") || (f["fileCV"].value==0))
		{
			err=1;
			f["fileCV"].style.backgroundColor='red';
		}
	
	if (err==1)
	{
		alert("These fields are required!!!");
		return false;
	}
	
	//check email 
	if (!validator_isEmail(f.strEmail.value))
	{
		f.strEmail.style.backgroundColor="red";
		alert("Email Format is Invalid"); 
		return false;
	}
	//check date	
	var d1=f.intDD1;
	var m1=f.intMM1;
	var err=0;
	if (m1.value*1==2 && (d1.value*1==30 || d1.value*1==31))
		err=1;
	if ( (m1.value*1==4 || m1.value*1==6 || m1.value*1==9 || m1.value*1==11) && d1.value*1==31)
		err=1;
	if (err==1)
	{
		f.intDD1.options[f.intDD1.selectedIndex].style.backgroundColor="red";
		f.intMM1.options[f.intMM1.selectedIndex].style.backgroundColor="red";
		f.intYYYY1.options[f.intYYYY1.selectedIndex].style.backgroundColor="red";
		alert("Date Format is Invaid"); 
		return false;
	}
	
	//check unit date
	var d1=f.intDD_unit;
	var m1=f.intMM_unit;
	var y1=f.intYYYY_unit;
	if (d1.value*m1.value*y1.value!=0){
		var err=0;
		if (m1.value*1==2 && (d1.value*1==30 || d1.value*1==31))
			err=1;
		if ( (m1.value*1==4 || m1.value*1==6 || m1.value*1==9 || m1.value*1==11) && d1.value*1==31)
			err=1;
		if (err==1)
		{
			f.intDD_unit.options[f.intDD_unit.selectedIndex].style.backgroundColor="red";
			f.intMM_unit.options[f.intMM_unit.selectedIndex].style.backgroundColor="red";
			f.intYYYY_unit.options[f.intYYYY_unit.selectedIndex].style.backgroundColor="red";
			alert("Date Format is Invalid"); 
			return false;
		}
	}
	
	//check intZip
	/*
	if (!validator_isIntNumeric(f.strZip.value))
	{
		f.strZip.style.backgroundColor="red";
		alert("Enter correct zip code"); 
		return false;
	}
	*/
	/*
	if (!validator_isIntNumeric(f.strZip_unit.value))
	{
		f.intZip_unit.style.backgroundColor="red";
		alert("Enter correct zip code"); 
		return false;
	}
	*/
	
	//check files
	var err=0;
	var file1=f.fileCV;
	for (var i=0; i<2; i++)
	{
		if (file1.value.length>0)
		{
			var file1_tmp=file1.value.split(".");
			if (file1_tmp.length<=1) err=1;
			if (file1_tmp[file1_tmp.length-1].toLowerCase()!='doc' && file1_tmp[file1_tmp.length-1].toLowerCase()!='pdf' && file1_tmp[file1_tmp.length-1].toLowerCase()!='rtf') 
				err=1;	
		}	
		if (err==1)
		{
			file1.style.backgroundColor="red";
			alert("Possible to upload only *.pdf, *.rtf, *.doc files"); 
			return false;
		}
		file1=f.filePublications;
	}	
	document.getElementById("submitButton").disabled=true;
	return true;
}

function returnColor(field)
{
	if (field.options)
		field.options[field.selectedIndex].style.backgroundColor='';
	field.style.backgroundColor='';
}

function changeState()
{
	if (document.getElementById("otherURLs").style.display=='none')
		document.getElementById("otherURLs").style.display='';
	else
		document.getElementById("otherURLs").style.display='none';	
}