// JavaScript Document

function isFormFilled()
{
	var FORM = document.cideApplicationForm;
	
	//......... Program Selected .............
	if(FORM.ApplyProg.selectedIndex<1)
	{
		alert("Please select the program you are interested to apply for");
		FORM.ApplyProg.focus();
		return false;
	}
	//____________________________________
	
	//......... Semester Check .............
	var Err = true;
	var RSi = -1;
	for(var i=0; i<FORM.RegSem.length; i++)
	{
		if(FORM.RegSem[i].checked)
		{
			Err = false;
			RSi = i;
		}
	}
	
	if(Err)
	{
		alert("Please specify the semester you are interested to apply for.");
		FORM.RegSem[0].focus();
		return false;
	}
	else
	{
		if((FORM.ApplyProg.selectedIndex>4) && (RSi<3))
		{ //if any of teh program below the first 4 & any of the first 3 radio buttons clicked
			alert("Please select the appropriate semester.");//Please select the appropriate semester
			FORM.RegSem[4].focus();
			return false;
		}
		else if((FORM.ApplyProg.selectedIndex<5) && (RSi>2))
		{//if any of teh program above the the fifth program & any of the radio buttons more above the first 3 clicked
			alert("Please select the appropriate semester.");
			FORM.RegSem[0].focus();
			return false;
		}
		
	}
	//____________________________________
	
	//......... Title Check .............
	var Err = true;
	for(var i=0; i<FORM.Title.length; i++)
	{
		if(FORM.Title[i].checked)
		{
			Err = false;
		}
	}
	
	if(Err)
	{
		alert("Please specify your title.");
		FORM.Title[0].focus();
		return false;
	}
	//____________________________________
	
	
	//......... First Name .............
	if(!FORM.Fname.value.match(/^[a-zA-Z]{3,}/))
	{
		alert("First name must contain only alphabets and minimum 3 letters.");
		FORM.Fname.focus();
		FORM.Fname.select();
		return false;
	}
	//____________________________________
	
	//......... Last Name .............
	if(!FORM.Lname.value.match(/^[a-zA-Z]{3,}/))
	{
		alert("Last name must contain only alphabets and minimum 3 letters.");
		FORM.Lname.focus();
		FORM.Lname.select();
		return false;
	}
	//____________________________________
	
	//......... Religion .............
	if(!FORM.Religion.value.match(/^[a-zA-Z]{3,}/))
	{
		alert("Please provide your religion.");
		FORM.Religion.focus();
		FORM.Religion.select();
		return false;
	}
	//____________________________________
	
	
	//......... Birthday .............
	if((FORM.Bday.selectedIndex<1) || (FORM.Bmonth.selectedIndex<1) || (FORM.Byear.selectedIndex<1))
	{
		alert("Please enter your date of birth.");
		FORM.Bday.focus();
		return false;
	}
	//____________________________________
	
	//......... Thai Nationality ID .............
	if((FORM.Nationality.options[FORM.Nationality.selectedIndex].value=='thai'))
	{
		if(!FORM.CitizenID.value.match(/^[0-9]{13,13}/))
		{
			alert("The Thai national ID must have 13 numbers.");
			FORM.CitizenID.focus();
			FORM.CitizenID.select();
			return false;
		}
		/*else
		{
			if(!FORM.Nationality.value.match(/^[0-9a-zA-Z]{5,}/))
			{
				alert("The national ID field expecting only numbers and alphabets and must be atleast 5 characters long.");
				FORM.Nationality.focus();
				FORM.Nationality.select();
				return false;
			}
		}*/
	}
	//____________________________________
	
	
	//......... Passport Number .............
	if((FORM.Nationality.options[FORM.Nationality.selectedIndex].value!='thai'))
	{
		if(!FORM.PassportNo.value.match(/^[0-9a-zA-Z]{5,}/))
		{
			alert("Passport number field expecting only numbers and alphabets and must be atleast 5 characters long.");
			FORM.PassportNo.focus();
			FORM.PassportNo.select();
			return false;
		}
	}
	//____________________________________
	
	
	
	//================== Address ======================
	
	//......... Street .............
	if(FORM.cAdsStreet.value.length<5)
	{
		alert("Street address is too short");
		FORM.cAdsStreet.focus();
		FORM.cAdsStreet.select();
		return false;
	}
	//____________________________________
	
	//......... District .............
	if(FORM.cAdsDistrict.value.length<3)
	{
		alert("District name is too short.");
		FORM.cAdsDistrict.focus();
		FORM.cAdsDistrict.select();
		return false;
	}
	//____________________________________
	
	//......... City .............
	if(FORM.cAdsCity.value.length<3)
	{
		alert("City name is too short");
		FORM.cAdsCity.focus();
		FORM.cAdsCity.select();
		return false;
	}
	//____________________________________
	
	
	//......... Post Code .............
	if(!FORM.cAdsPcode.value.match(/^[0-9a-zA-Z]{2,8}/))
	{
		alert("postal code must contain only numbers and alpahbets and must be 4 to 8 characters long.");
		FORM.cAdsPcode.focus();
		FORM.cAdsPcode.select();
		return false;
	}
	//____________________________________
	
	//......... Phone .............
	if(!FORM.cAdsHphone.value.match(/^[0-9-()]{8,16}/))
	{
		alert("Phone number seems to be invalied. Must only contain number.");
		FORM.cAdsHphone.focus();
		FORM.cAdsHphone.select();
		return false;
	}
	//____________________________________
	
	
	//......... Phone .............
	if(!FORM.cAdsEmail.value.match(/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$/))
	{
		alert("Please provide a valied email address.");
		FORM.cAdsEmail.focus();
		FORM.cAdsEmail.select();
		return false;
	}
	//____________________________________
	
	
	//================== Dergree ======================
	
	
	//......... Bachelors degree .............
	if(FORM.bDegName.value.length<3)
	{
		alert("Bachelor's degree name is too short");
		FORM.bDegName.focus();
		FORM.bDegName.select();
		return false;
	}
	//____________________________________
	
	//......... Bachelor's Year .............
	if(FORM.bDegYear.selectedIndex<1)
	{
		alert("Please select the year in which you have completed your bachelor's degree.");
		FORM.bDegYear.focus();
		return false;
	}
	//____________________________________
	
	
	//......... Institution .............
	if(FORM.bDegInst.value.length<5)
	{
		alert("The name of the institute is too short");
		FORM.bDegInst.focus();
		FORM.bDegInst.select();
		return false;
	}
	//____________________________________
	
	//......... District .............
	if(FORM.bDegDistrict.value.length<3)
	{
		alert("The district name is too short.");
		FORM.bDegDistrict.focus();
		FORM.bDegDistrict.select();
		return false;
	}
	//____________________________________
	
	//......... City .............
	if(FORM.bDegCity.value.length<3)
	{
		alert("The name of the city is too short");
		FORM.bDegCity.focus();
		FORM.bDegCity.select();
		return false;
	}
	//____________________________________
	
		//......... Specilization .............
	if(FORM.bDegspec.value.length<3)
	{
		alert("Your specilization is too short.");
		FORM.bDegspec.focus();
		FORM.bDegspec.select();
		return false;
	}
	//____________________________________
	
	//......... GPA .............
	if(!FORM.bDegGPA.value.match(/^[0-9.]{1,4}/))
	{
		alert("Please provide a proper GPA for your bachelor degree.");
		FORM.bDegGPA.focus();
		FORM.bDegGPA.select();
		return false;
	}
	//____________________________________
	
	//****************if PhD Program selected****************
	//*******************************************************
	if(FORM.ApplyProg.selectedIndex==5)
	{
		//......... Masters degree .............
		if(FORM.mDegName.value.length<3)
		{
			alert("Master's degree name is too short");
			FORM.mDegName.focus();
			FORM.mDegName.select();
			return false;
		}
		//____________________________________
		
		//......... Bachelor's Year .............
		if(FORM.mDegYear.selectedIndex<1)
		{
			alert("Please select the year in which you have completed your masters's degree.");
			FORM.mDegYear.focus();
			return false;
		}
		//____________________________________
		
		
		//......... Institution .............
		if(FORM.mDegInst.value.length<5)
		{
			alert("The name of the institute is too short");
			FORM.mDegInst.focus();
			FORM.mDegInst.select();
			return false;
		}
		//____________________________________
		
		
		//......... City .............
		if(FORM.mDegCity.value.length<3)
		{
			alert("The name of the city is too short");
			FORM.mDegCity.focus();
			FORM.mDegCity.select();
			return false;
		}
		//____________________________________
		
			//......... Specilization .............
		if(FORM.mDegspec.value.length<3)
		{
			alert("Your specilization too short.");
			FORM.mDegspec.focus();
			FORM.mDegspec.select();
			return false;
		}
		//____________________________________
		
		//......... GPA .............
		if(!FORM.mDegGPA.value.match(/^[0-9.]{1,4}/))
		{
			alert("Please provide a proper GPA for your bachelor degree.");
			FORM.mDegGPA.focus();
			FORM.mDegGPA.select();
			return false;
		}
		//____________________________________
		
	}
	//*******************************************************
	
	if(FORM.ApplyProg.selectedIndex<4)
	{
		//.............English Test.................
		$TOELF = true;
		if((FORM.toeflScore.value.length<1) && (FORM.toeflDate.value.length>0))
		{
			alert("Please either enter your TOEFL score or clear the TOEFL test date");
			FORM.toeflScore.focus();
			return false;
		}
		else if((FORM.toeflScore.value.length>0) && (FORM.toeflDate.value.length<1))
		{
			alert("Please enter your TOEFL test date");
			FORM.toeflDate.focus();
			return false;
		}
		else if((FORM.toeflScore.value.length<1) && (FORM.toeflDate.value.length<1))
		{
			$TOELF = false;
		}
		
		$IELTS = true;
		if((FORM.ieltsScore.value.length<1) && (FORM.ieltsDate.value.length>0))
		{
			alert("Please enter your IELTS score");
			FORM.ieltsScore.focus();
			return false;
		}
		else if((FORM.ieltsScore.value.length>0) && (FORM.ieltsDate.value.length<1))
		{
			alert("Please enter your IELTS test date");
			FORM.ieltsDate.focus();
			return false;
		}
		else if((FORM.ieltsScore.value.length<1) && (FORM.ieltsDate.value.length<1))
		{
			$IELTS = false;
		}
		
		if((!$TOELF) && (!$IELTS) && (!FORM.EPTYES.checked))
		{
			alert("If you have no TOEFL or IELTS score, you must take the English Proficiency Test conducted by CIDE.");
			FORM.EPTYES.focus();
			return false;
		}
	}
		//alert($TOELF+" "+$IELTS+" "+FORM.EPTYES.checked);
		/*
		if((FORM.toeflScore.value.length<1) && (FORM.toeflDate.value.length<1))
		{
			if((FORM.ieltsScore.value.length<1) && (FORM.ieltsDate.value.length<1))
			{
				if(!FORM.EPTYES.checked)
				{
					alert("If you don't have TOELF or IELTS Exam score you must take the English Proficiency Test conducted by CIDE.");
					FORM.EPTYES.focus();
					return false;
				}
			}
			alert("Your specilization too short.");
			FORM.mDegspec.focus();
			FORM.mDegspec.select();
			return false;
		}*/
		//_______________________________________
	
	
	//......... Photo .............
/*
	if(FORM.Photo.value.length<5)
	{
		alert("Please select your photo. Please provide photos in jpg, gif or png format");
		FORM.Photo.focus();
		return false;
	}
	else
	{
		var ImgFmt = (FORM.Photo.value.substring(FORM.Photo.value.lastIndexOf(".")+1)).toLowerCase();
		//alert(ImgFmt);
		switch(ImgFmt)
		{
			case "jpg":
			case "gif":
			case "png":
			// ok do nothing
			break;
			
			default:
				alert("'"+ImgFmt + "' file format is not acceptable. Please select jpg, gif or png picture format");
				return false;
			break;
		}
	}
*/
	//____________________________________
	
	return true;
}