//*********************************** Check Valid Email ************************
function emailCheck(str) {
	
	 var at="@"
	 var dot="."
	 var lat=str.indexOf(at)
	 var lstr=str.length
	 var ldot=str.indexOf(dot)
	
	 // Check If the @ sign is present in Email, invalid if not present ...
	 if (str.indexOf(at)==-1){
	   return false
	 }

	// Check If the @ sign is first or last character of Email, invalid if it is first or last ...
	 if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr-1){
	    return false
	 }
 
	 // Check If the Dot '.' is present in the Email and is first or last character
	 // invalid if not present and Invalid if first or last character ...
	 if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot,lstr-1)==lstr-1){
	     return false
	 }

	 // Check If the @ sign is used more than once in Email ...
	 // Invalid if used more than once .
	  if (str.indexOf(at,(lat+1))!=-1){
	      return false
	  }
 
	 // Check If the '.' appears just before or after '@' sign, than that is Invalid ...
	  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	      return false
	  }

	 // Check If the '.' exists after '@' sign (with 1 char gap), Invalid if it does not ...
	  if (str.indexOf(dot,(lat+2))==-1){
	     return false
	  }
  
	  // Check If there is any space in the Email, than that is Invalid ....
	  if (str.indexOf(" ")!=-1){
	     return false
	  }
   
   return true     
}

//*********************************** Trim Spaces ************************
function trimSpaces(str)
{
	if (str != null)
    {
		var i;

		for (i=0; i < str.length; i++)
		{
			if (str.charAt(i)!=" ")
			{
				str=str.substring(i,str.length);
				break;
			}
		}

		for (i = str.length-1; i >= 0; i--)
		{
			if (str.charAt(i)!=" ")
			{
				str = str.substring(0,i+1);
				break;
			}
		}

		if (str.charAt(0)==" ")
		{
			return "";
		}
		else
		{
			return str;
		}
	}
}

//*********************************** Check Null ******************************
function chknull(txtbox, txtname)
{
	if (txtbox.value == '')
	{
		alert(txtname);
		txtbox.focus();
		return false;	
	}
	return true;
}

//********************* Check for Max length reached **************************
function chkmax(txtbox, txtlen, txtname)
{
	if (txtbox.value.length > txtlen)
	{
		alert('Please limit ' + txtname + ' to only ' + txtlen + ' characters.')
		txtbox.focus();
		return false;
	}
	return true;
}

//********************* Authenticate Combo Selection ******************************
function chkcombo(cmbbox,cmbname)
{
	cmbValue = cmbbox.options[cmbbox.selectedIndex].value;	
	if (cmbValue == "-1")
	{
		alert(cmbname);
		cmbbox.focus();
		return false;	
	}
	return true; 
}

//********************* Simple Email Validity ******************************
function chkemailsimple(txtbox, txtname)	{

	if (!chknull(txtbox, txtname)) return false;
	if(txtbox.value.indexOf('@') == -1 || txtbox.value.indexOf('.') == -1)	{
		alert("Please enter valid Email Address.");
		txtbox.focus();
		return false;	
	}
	return true;
}

//*******************Set Deadline Date**********************************************
function chkValidDate(mm,dd,yy)
{
	//mm	= parseInt(document.form1.month.options[document.form1.month.selectedIndex].value);
	//dd	= parseInt(document.form1.day.options[document.form1.day.selectedIndex].value);
	//yy	= parseInt(document.form1.year.options[document.form1.year.selectedIndex].value);

	if(mm == 4 || mm == 6 || mm == 9 || mm == 11)
	{
		max_days = 30;
	}
	else if(mm == 2)
	{		// February			
		if(yy % 4 == 0)
		{	// Leap Year
			max_days = 29;
		}
		else
		{				// Not a Leap Year
			max_days = 28;	
		}			
	}
	else
	{
		max_days = 31;	
	}

	if (dd >max_days )
		return false;
	else
		return true;	
}	

//........ Online_Order2.asp / Online_registrationForm2.asp Form Submission Validation .................................
function frmSubmit()
{	
	var isValid = true;
	var varMulti=false;
	var ArrCount=0;
	dlCode = parseInt(document.frmOrder.dlcode.value);		
	if (dlCode > 5)
	{
		varMulti=true;
		ArrCount = document.frmOrder.txtOther.length-1;
		for(i=0;i<=document.frmOrder.txtOther.length-1;i++)
		{
			if (isValid)
			{
				ii=i+1
				//if (document.frmOrder.txtOther[i].value=="")
				//{
				major_dlcode = parseInt(document.frmOrder.major_dlcode[i].value);
				if(major_dlcode!=5)
				{
					var txt = document.frmOrder.tempDegree[i].value;
					if ((document.frmOrder.txtOther[i].value != "" )&&(document.frmOrder.cmbMajor[i].options[document.frmOrder.cmbMajor[i].selectedIndex].value != "-1") )
					{
						alert('You may choose or enter one major at a time only for '+ txt  );
						document.frmOrder.txtOther[i].focus();
						return false;
					}
					if (document.frmOrder.txtOther[i].value=="")
						if (!chkcombo(document.frmOrder.cmbMajor[i],"Please choose or enter a major for the " + txt +' package')) 
						{
							isValid = false;
							break;
						}
				}
				else
				{
					document.frmOrder.cmbMajor[i].value = 294;
				}
				//}
			}
		}
	}
	else
	{
		ArrCount = 0;
		var txt = document.frmOrder.tempDegree.value;
		major_dlcode = parseInt(document.frmOrder.major_dlcode.value);
		if(major_dlcode!=5)
		{
			//if (document.frmOrder.txtOther.value=="")
			//{
			if ((document.frmOrder.txtOther.value != "" ) && (document.frmOrder.cmbMajor.options[document.frmOrder.cmbMajor.selectedIndex].value != "-1") )
			{
				alert('You may choose or enter one major at a time only for '+ txt +' package' );
				document.frmOrder.txtOther.focus();
				return false;
			}
			if (document.frmOrder.txtOther.value=="")
				if ((!chkcombo(document.frmOrder.cmbMajor, "Please choose or enter a major for the " + txt )) && (isValid)){
					isValid = false;
		}
			//}
		}
		else
		{
			document.frmOrder.cmbMajor.value = 294;
		}
	}	
	if  ((document.frmOrder.txtResume.value=="")  && (isValid))
	{
		if  ((!chknull(document.frmOrder.txtExperience, "Please enter your Experience")) ||(!chkmax(document.frmOrder.txtExperience, "5000", "Experience")))
		{
			isValid = false;
			//return false;
		}
	}		

	if (isValid)
	{
		if (parseInt(ArrCount) >  0)
		{
			for(i=0;i<=ArrCount;i++)
			{
				if (isValid)
				{
				//ii=i+1;
					if ((document.frmOrder.cmbPDateMonth[i].options[document.frmOrder.cmbPDateMonth[i].selectedIndex].value !="-1")||(document.frmOrder.cmbPDateDay[i].options[document.frmOrder.cmbPDateDay[i].selectedIndex].value !="-1")||(document.frmOrder.cmbPDate[i].options[document.frmOrder.cmbPDate[i].selectedIndex].value !="-1"))
					{
						txt = document.frmOrder.tempDegree[i].value;
						if (!chkcombo(document.frmOrder.cmbPDateMonth[i],"Please select Complete Preferred Date for your " + txt + " package"))
						{
							isValid = false;
							break;
						}
						if  ((isValid) && (!chkcombo(document.frmOrder.cmbPDateDay[i],"Please select Complete Preferred Date for your " + txt + " package")) )
						{
							isValid = false;
							break;
						}
						if ((isValid)&&(!chkcombo(document.frmOrder.cmbPDate[i],"Please select Complete Preferred Date for your " + txt + " package")) )
						{
							isValid = false;
							break;
						}

//						var mm =document.frmOrder.cmbPDateMonth[i].options[document.frmOrder.cmbPDateMonth[i].selectedIndex].value;
//						var dd =document.frmOrder.cmbPDateDay[i].options[document.frmOrder.cmbPDateDay[i].selectedIndex].value;
//						var yy =document.frmOrder.cmbPDate[i].options[document.frmOrder.cmbPDate[i].selectedIndex].value;

//						if (!chkValidDate(mm,dd,yy))
//						{
//							alert('Please select a valid Day of the Month for your ' + txt + ' package');
//							document.frmOrder.cmbPDateDay[i].focus();
//							isValid = false;
//							break;
//						}
					}
				}
			}
		}
		else
		{
			if ((document.frmOrder.cmbPDateMonth.options[document.frmOrder.cmbPDateMonth.selectedIndex].value !="-1") ||(document.frmOrder.cmbPDateDay.options[document.frmOrder.cmbPDateDay.selectedIndex].value !="-1")||(document.frmOrder.cmbPDate.options[document.frmOrder.cmbPDate.selectedIndex].value !="-1"))
			{
				var txt = document.frmOrder.tempDegree.value;
				if (!chkcombo(document.frmOrder.cmbPDateMonth,"Please select Complete Preferred Date for your " + txt + " package"))
				{
					isValid = false;
				}
				if ((isValid) && (!chkcombo(document.frmOrder.cmbPDateDay,"Please select Complete Preferred Date for your " + txt + " package")) )
				{
					isValid = false;
				}
				if ((isValid) && (!chkcombo(document.frmOrder.cmbPDate,"Please select Complete Preferred Date for your " + txt + " package")))
				{
					isValid = false;
				}

//				var mm =document.frmOrder.cmbPDateMonth.options[document.frmOrder.cmbPDateMonth.selectedIndex].value;
//				var dd =document.frmOrder.cmbPDateDay.options[document.frmOrder.cmbPDateDay.selectedIndex].value;
//				var yy =document.frmOrder.cmbPDate.options[document.frmOrder.cmbPDate.selectedIndex].value;
//				if (!chkValidDate(mm,dd,yy))
//				{
//					alert('Please select a valid Day of the Month for your ' + txt + ' package');
//					document.frmOrder.cmbPDateDay.focus();
//					isValid = false;
//				}
			}
		}
	}
	
	if (isValid)
	{
		var mmm =document.frmOrder.cmbMonth.options[document.frmOrder.cmbMonth.selectedIndex].value;
		var ddd =document.frmOrder.cmbDay.options[document.frmOrder.cmbDay.selectedIndex].value;
		var yyy =document.frmOrder.cmbYear.options[document.frmOrder.cmbYear.selectedIndex].value;
		
		if (!chknull(document.frmOrder.txtContactName, "Please enter your Contact Name")){
			return false;
		}else if (!chkcombo(document.frmOrder.cmbMonth,"Please enter your Date of Birth" )){
			return false;
		}else if(!chkcombo(document.frmOrder.cmbDay,"Please enter your Date of Birth" )){
			return false;
		}else if(!chkcombo(document.frmOrder.cmbYear,"Please enter your Date of Birth" )){
			return false;
		} 
		//else if (!chkValidDate(mmm,ddd,yyy)){
		//	alert('Please enter valid date of Birth' );
		//	document.frmOrder.cmbDay.focus();
		//	return false;
		//}
		else if(!chkemailsimple(document.frmOrder.txtEmail,"Please enter your E-mail Address" )){
			return false;
		}else if(!emailCheck(document.frmOrder.txtEmail.value)) {
			alert("Please enter valid E-mail Address");
			document.frmOrder.txtEmail.focus();
			return false;
		}
		/*else if(!chkemailsimple(document.frmOrder.txtVEmail,"Please verify your E-mail Address" )){
			return false;
		}
		else if(!emailCheck(document.frmOrder.txtVEmail.value)) {
			alert("Please enter valid E-mail Address");
			document.frmOrder.txtVEmail.focus();
			return false;
		}
		else if(document.frmOrder.txtEmail.value != document.frmOrder.txtVEmail.value ){
			alert('The two E-mail addresses you provided do not match');
			document.frmOrder.txtVEmail.focus();
			return false;
		}*/
		else if(document.frmOrder.txtAEmail.value.length > 0 && 
			  ( !emailCheck(document.frmOrder.txtAEmail.value) ) ) {
			alert("Please enter valid Alternate E-mail Address");
			document.frmOrder.txtAEmail.focus();
			return false;
		}else if(!chknull(document.frmOrder.txtAddress,"Please enter your Street Address" )){
			return false;
		}else if(!chknull(document.frmOrder.txtCity,"Please enter your City Name" )){
			return false;
		}else if(!chknull(document.frmOrder.txtStateProvince,"Please enter your State or Province" )){
			return false;
		}else if(!chknull(document.frmOrder.txtZip,"Please enter your Zip / Postal Code" )){
			return false;
		}else if(!chkcombo(document.frmOrder.cmbCountry,"Please select your Country Name" )){
			return false;
		}else if(!chknull(document.frmOrder.txtPhone,"Please enter your Phone Number" )){
			return false;
		}
	}
	else
	{
		return false;
	}

	return true;
}