function ValidateTemplate(lberr, lberr2)
{
	//get all text-boxes
	var inps = document.getElementsByTagName('input');
	if (inps)
	{
		if (checkinput(inps, lberr, lberr2)==false) return false;
	}
	
	
	var inps2 = document.getElementsByTagName('textarea');
	if (inps2)
	{
		if (checkinput(inps2, lberr, lberr2)==false) return false;
	}
	
	var inps3 = document.getElementsByTagName('select');
	if (inps3)
	{
		if (checkinput(inps3, lberr, lberr2)==false) return false;
	}	
	
	return true;
}

function checkinput(inps, lberr, lberr2)
{
	for(var i=0;i<inps.length;i++)
	{
		//dates are always required and should be in the dd/mm/yyyy format
		if (inps[i].className=='dt')
		{
			if (inps[i].value=="")
			{
				document.getElementById(lberr).innerHTML= "Please make sure Dates are valid (MM/DD/YYYY).";
				document.getElementById(lberr).style.color="Red";	
				document.getElementById(lberr2).innerHTML= "Please make sure Dates are valid (MM/DD/YYYY).";	
				document.getElementById(lberr2).style.color="Red";
				inps[i].style.backgroundColor="Yellow";	
				return false;
			}
			if (!isDate(inps[i].value,1))
			{
				document.getElementById(lberr).innerHTML= "Please make sure Dates are valid (MM/DD/YYYY).";	
				document.getElementById(lberr).style.color="Red";
				document.getElementById(lberr2).innerHTML= "Please make sure Dates are valid (MM/DD/YYYY).";	
				document.getElementById(lberr2).style.color="Red";
				inps[i].style.backgroundColor="#d2d1d0";	
				return false;
			}
			
			inps[i].style.backgroundColor="LightYellow";
		}
		//date not required
		else if (inps[i].className=='dtnr')
		{
			if (inps[i].value=="")
			{
				return true;
			}
			if (!isDate(inps[i].value,1))
			{
				document.getElementById(lberr).innerHTML= "Please make sure Dates are valid (MM/DD/YYYY).";	
				document.getElementById(lberr).style.color="Red";
				document.getElementById(lberr2).innerHTML= "Please make sure Dates are valid (MM/DD/YYYY).";	
				document.getElementById(lberr2).style.color="Red";
				inps[i].style.backgroundColor="Yellow";	
				return false;
			}
			
			inps[i].style.backgroundColor="White";
		}
		else if (inps[i].className=='intnr')
		{
			if (inps[i].value=="")
			{
				return true;
			}
			if (!IsInteger(inps[i].value))
			{
				document.getElementById(lberr).innerHTML= "Please make sure you enter a numeric value.";	
				document.getElementById(lberr).style.color="Red";
				document.getElementById(lberr2).innerHTML= "Please make sure you enter a numeric value.";	
				document.getElementById(lberr2).style.color="Red";
				inps[i].style.backgroundColor="Yellow";					
				return false;
			}
			inps[i].style.backgroundColor="White";
		}
		else if (inps[i].className=='tbd_textbox')
		{
			if (inps[i].value=="")
			{
				document.getElementById(lberr).innerHTML= "Please make sure you enter the required fields.";	
				document.getElementById(lberr).style.color="Red";
				document.getElementById(lberr2).innerHTML= "Please make sure you enter the required fields.";	
				document.getElementById(lberr2).style.color="Red";
				inps[i].style.backgroundColor="Yellow";					
				return false;
			}
			else
			{
				inps[i].style.backgroundColor="#d2d1d0";
			}
		}
		else if (inps[i].className=='reqnum')
		{
			if (inps[i].value=="")
			{
				document.getElementById(lberr).innerHTML= "Please make sure you enter the required fields.";	
				document.getElementById(lberr).style.color="Red";
				document.getElementById(lberr2).innerHTML= "Please make sure you enter the required fields.";	
				document.getElementById(lberr2).style.color="Red";
				inps[i].style.backgroundColor="Yellow";					
				return false;
			}
			else
			{
				if (!IsNumeric(inps[i].value))
				{
					document.getElementById(lberr).innerHTML= "Please make sure you enter a numeric value.";	
					document.getElementById(lberr).style.color="Red";
					document.getElementById(lberr2).innerHTML= "Please make sure you enter a numeric value.";	
					document.getElementById(lberr2).style.color="Red";
					inps[i].style.backgroundColor="Yellow";					
					return false;
				}
				else
				{
					inps[i].style.backgroundColor="#d2d1d0";
				}
			}
		}
		else if (inps[i].className=='tbd_textbox_em')
		{
			if (inps[i].value.search(/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/)==-1)
			{
				document.getElementById(lberr).innerHTML= "Please make sure you enter a valid e-mail.";	
				document.getElementById(lberr).style.color="Red";
				document.getElementById(lberr2).innerHTML= "Please make sure you enter a valid e-mail.";	
				document.getElementById(lberr2).style.color="Red";
				inps[i].style.backgroundColor="Yellow";					
				return false;
			}
			else
			{
				inps[i].style.backgroundColor="#d2d1d0";
			}
		}
		else if (inps[i].className=='dd')
		{
			if (inps[i].value.indexOf("Select")==0)
			{
				document.getElementById(lberr).innerHTML= "Please make sure you select an option from the marked dropdown.";	
				document.getElementById(lberr).style.color="Red";
				document.getElementById(lberr2).innerHTML= "Please make sure you select an option from the marked dropdown.";	
				document.getElementById(lberr2).style.color="Red";
				inps[i].style.backgroundColor="Yellow";					
				return false;
			}
			else
			{
				inps[i].style.backgroundColor="#d2d1d0";
			}
		}
		else if (inps[i].className=='reqphone')
		{
			if (inps[i].value=="")
			{
				document.getElementById(lberr).innerHTML= "Please make sure you enter the required fields.";	
				document.getElementById(lberr).style.color="Red";
				document.getElementById(lberr2).innerHTML= "Please make sure you enter the required fields.";	
				document.getElementById(lberr2).style.color="Red";
				inps[i].style.backgroundColor="Yellow";					
				return false;
			}
			else
			{
				if (inps[i].value.search(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/)==-1)
				{
					document.getElementById(lberr).innerHTML= "Please make sure the phone number is in the (###) ###-#### format.";	
					document.getElementById(lberr).style.color="Red";
					document.getElementById(lberr2).innerHTML= "Please make sure the phone number is in the (###) ###-#### format.";	
					document.getElementById(lberr2).style.color="Red";
					inps[i].style.backgroundColor="Yellow";					
					return false;
				}
				else
					inps[i].style.backgroundColor="#d2d1d0";
			}
		}
	}
		
	return true;
}

function IsNumeric(sa)
{
	var IsNumber=true;
	var Char;
	var i;
	var ValidChars = "0123456789.";
	var PtCnt = 0;
	for (i = 0; i < sa.length && IsNumber == true; i++)   
	{
		Char = sa.charAt(i);
		if(Char == ".") PtCnt ++;
		if(PtCnt > 1) return false; 
		if (ValidChars.indexOf(Char) == -1) IsNumber = false;
	}
	return IsNumber;
}

function IsInteger(sa)
{
	var IsNumber=true;
	var Char;
	var i;
	var ValidChars = "-0123456789";
	for (i = 0; i < sa.length && IsNumber == true; i++)   
	{
		Char = sa.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) IsNumber = false;
	}

	return IsNumber;
}

function isDate(s,f) 
{var a1=s.split("/"); 
var a2=s.split("-"); 
var e=true; 
if ((a1.length!=3) && (a2.length!=3)) 
{ 
e=false; 
} 
else 
{if (a1.length==3) 
var na=a1; 
if (a2.length==3) 
var na=a2; 
if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2])) 
{ if (f==1) 
{var d=na[1],m=na[0]; 
} 
else 
{var d=na[0],m=na[1]; 
} 
var y=na[2]; 
if (((e) && (y<1000)||y.length>4)) 
e=false 
if (e) 
{ 
v=new Date(m+"/"+d+"/"+y); 
if (v.getMonth()!=m-1) 
e=false; 
} 
} 
else 
{ 
e=false; 
} 
} 
return e 
}

function isPositiveInteger(theString) 
{ 
var theData = new String(theString) 

if (!isDigit(theData.charAt(0))) 
if (!(theData.charAt(0)== '+')) 
return false 

for (var i = 1; i < theData.length; i++) 
if (!isDigit(theData.charAt(i))) 
return false 
return true 
}

function isDigit(theDigit) 
{ 
var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j; 

for (j = 0; j < digitArray.length; j++) 
{if (theDigit == digitArray[j]) 
return true 
} 
return false 
}

function SameAsStudent1()
{
	document.Form1.tbParentAddress1.value = document.Form1.tbStudentAddress.value
	document.Form1.tbParentCity1.value = document.Form1.tbStudentCity.value
	document.Form1.ddP1State.selectedIndex = document.Form1.ddState.selectedIndex
	document.Form1.tbParentZIP1.value = document.Form1.tbStudentZIP.value
	document.Form1.tbParentPhone1.value = document.Form1.tbStudentPhone.value
	document.Form1.tbParentMobile1.value = document.Form1.tbStudentMobile.value
	document.Form1.ddParentCountry1.selectedIndex = document.Form1.ddStudentCountry.selectedIndex
}

function SameAsStudent2()
{
	document.Form1.tbParentAddress2.value = document.Form1.tbStudentAddress.value
	document.Form1.tbParentCity2.value = document.Form1.tbStudentCity.value
	document.Form1.ddP2State.selectedIndex = document.Form1.ddState.selectedIndex
	document.Form1.tbParentZIP2.value = document.Form1.tbStudentZIP.value
	document.Form1.tbParentPhone2.value = document.Form1.tbStudentPhone.value
	document.Form1.tbParentMobile2.value = document.Form1.tbStudentMobile.value
	document.Form1.ddParentCountry2.selectedIndex = document.Form1.ddStudentCountry.selectedIndex
}

function vApplication()
{
	var ErrorMsg = "In order to submit an application, you need to: \n";
	if(
	 document.Form1.ddStudentType.selectedIndex == 0 ||
	 document.Form1.ddProgramType.selectedIndex == 0 ||
	 document.Form1.ddHowDidYouHear.selectedIndex == 0 ||
	 document.Form1.ddAffiliated.selectedIndex == 0 ||
	 document.Form1.tbStudentFirstName.value == "" || 
	 document.Form1.tbStudentLastName.value == "" ||
	 isFutureDate(document.Form1.tbDOB.value, 1) != 1 ||
	 document.Form1.ddGender.selectedIndex == 0 ||
	 document.Form1.tbStudentAddress.value == "" ||
	 document.Form1.tbStudentCity.value == "" ||
	 document.Form1.ddState.selectedIndex == 0 ||
	 document.Form1.tbStudentZIP.value == "" ||
	 document.Form1.ddStudentCountry.selectedIndex == 0 ||
	 !IsValidZIP(document.Form1.ddStudentCountry.value, document.Form1.tbStudentZIP.value) ||
	 document.Form1.tbStudentPhone.value == "" ||
	 !isValidEmail(document.Form1.tbStudentEmail.value) ||
	 document.Form1.tbPriviousSchoolName1.value == "" ||
	 document.Form1.tbPriviousSchoolState1.value == "" ||
	 document.Form1.ddPriviousSchoolCountry1.selectedIndex == 0 ||
	 document.Form1.tbPriviousSchoolPhone1.value == "" ||
	 document.Form1.tbParentFirstName1.value == "" || 
	 document.Form1.tbParentLastName1.value == "" ||
	 document.Form1.tbParentAddress1.value == "" ||
	 document.Form1.tbParentCity1.value == "" ||
	 document.Form1.ddP1State.selectedIndex == 0 ||
	 document.Form1.tbParentZIP1.value == "" ||
	 document.Form1.ddParentCountry1.selectedIndex == 0 ||
	 document.Form1.tbParentPhone1.value == "" ||
	 document.Form1.tbParentMobile1.value == "" ||
	 !isValidEmail(document.Form1.tbParentEmail1.value) ||
	 document.Form1.tbEssay.value == "" ||
	 document.Form1.tbSignatureName.value == "" ||
	 isFutureDate(document.Form1.tbSignatureDate.value, 1) != 1 )
	{
	     if(document.Form1.ddStudentType.selectedIndex == 0 )  ErrorMsg   += "\n\t Select a student type.";
	     if(document.Form1.ddProgramType.selectedIndex == 0 )  ErrorMsg   += "\n\t Select a program type.";
	     if(document.Form1.ddHowDidYouHear.selectedIndex == 0 )  ErrorMsg   += "\n\t Select how you heard about us.";
	     if(document.Form1.ddAffiliated.selectedIndex == 0 )  ErrorMsg   += "\n\t Select an affiliation.";
	     if(document.Form1.tbStudentFirstName.value == "") ErrorMsg   += "\n\t Provide your student's first name."; 
	     if(document.Form1.tbStudentLastName.value == "") ErrorMsg   += "\n\t Provide your student's last name.";
	     if(isFutureDate(document.Form1.tbDOB.value, 1) != 1) ErrorMsg   += "\n\t Provide your student's date of birth in the correct format."; 
	     if(document.Form1.ddGender.selectedIndex == 0 )  ErrorMsg   += "\n\t Select the student gender.";
	     if(document.Form1.tbStudentAddress.value == "") ErrorMsg   += "\n\t Provide your student's address.";
	     if(document.Form1.tbStudentCity.value == "") ErrorMsg   += "\n\t Provide your student's city of residence.";
	     if(document.Form1.ddState.selectedIndex == 0) ErrorMsg   += "\n\t Provide your student's state of residence.";
	     if(document.Form1.tbStudentZIP.value == "") ErrorMsg   += "\n\t Provide your student's ZIP of residence.";
	     else if(!IsValidZIP(document.Form1.ddStudentCountry.value, document.Form1.tbStudentZIP.value)) ErrorMsg += "\n\t Provide a 5 digits for a USA ZIP code.";
	     if(document.Form1.ddStudentCountry.selectedIndex == 0 )  ErrorMsg   += "\n\t Select the student country.";
	     if(document.Form1.tbStudentPhone.value == "") ErrorMsg   += "\n\t Provide your student's telephone.";
	     if(!isValidEmail(document.Form1.tbStudentEmail.value)) ErrorMsg   += "\n\t Provide your student's email with a valid format (aaa@bbb.com).";
	     if(document.Form1.tbPriviousSchoolName1.value == "") ErrorMsg   += "\n\t Provide the student's previous school name.";
	     if(document.Form1.tbPriviousSchoolState1.value == "") ErrorMsg   += "\n\t Provide the student's previous school State.";
	     if(document.Form1.ddPriviousSchoolCountry1.selectedIndex == 0 )  ErrorMsg   += "\n\t Select the previous school country.";
	     if(document.Form1.tbPriviousSchoolPhone1.value == "") ErrorMsg   += "\n\t Provide the student's previous school phone.";
	     if(document.Form1.tbParentFirstName1.value == "") ErrorMsg   += "\n\t Provide the student parent's first name."; 
	     if(document.Form1.tbParentLastName1.value == "") ErrorMsg   += "\n\t Provide the student parent's last name.";
	     if(document.Form1.tbParentAddress1.value == "") ErrorMsg   += "\n\t Provide the student parent's address.";
	     if(document.Form1.tbParentCity1.value == "") ErrorMsg   += "\n\t Provide the student parent's city.";
	     if(document.Form1.ddP1State.selectedIndex == 0) ErrorMsg   += "\n\t Provide the student parent's state.";
	     if(document.Form1.tbParentZIP1.value == "") ErrorMsg   += "\n\t Provide the student parent's ZIP code.";
	     if(document.Form1.ddParentCountry1.selectedIndex == 0 )  ErrorMsg   += "\n\t Select the parent's country.";
	     if(document.Form1.tbParentPhone1.value == "") ErrorMsg   += "\n\t Provide the student parent's telephone.";
	     if(document.Form1.tbParentMobile1.value == "") ErrorMsg   += "\n\t Provide the student parent's mobile phone.";
	     if(!isValidEmail(document.Form1.tbParentEmail1.value)) ErrorMsg   += "\n\t Provide the student parent's email with a valid format (aaa@bbb.com).";
	     if(document.Form1.tbEssay.value == "") ErrorMsg   += "\n\t Provide an essay.";
	     if(document.Form1.tbSignatureName.value == "") ErrorMsg   += "\n\t Provide the student parent's signature.";
	     if(isFutureDate(document.Form1.tbSignatureDate.value, 1) != 1) ErrorMsg   += "\n\t Provide the signature date in the correct format.";
	
			alert(ErrorMsg);
         
		return false;
	}
	return true;
}

function isValidEmail(str) 
{
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function IsValidZIP(Country, str) 
{// 10/29/2009 Created to make sure the ZIP of USA student have 5 digits
	if(Country != "USA") return true;
	if (str.search(/^\d{5}$/)==-1) return false;
	return true;
}

//validates maximum length in multiline
function validatelen(ctrl)
{
	if (ctrl="ESSA")
	{
		text = document.Form1.tbEssay.value;
		if (text.length > 2048) document.Form1.tbEssay.value = text.substring(0,2048);
	}
	
	if (ctrl="COMM")
	{
		text = document.Form1.tbComments.value;
		if (text.length > 50) document.Form1.tbComments.value = text.substring(0,50);
	}
}

function isFutureDate(s,f) 
{
    var a1=s.split("/"); 
    var a2=s.split("-"); 
    var e=true;
    var r=0; 
    if ((a1.length==3) || (a2.length==3)) 
    {
        if (a1.length==3) 
            var na=a1; 
        if (a2.length==3) 
            var na=a2; 
        if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2])) 
        {
            if (f==1) 
            {
                var d=na[1],m=na[0]; 
            } 
            else 
            {
                var d=na[0],m=na[1]; 
            } 
            var y=na[2];
            if (y>1000 && y.length==4) 
            { 
                v=new Date(m+"/"+d+"/"+y); 
                if (v.getMonth()==m-1 && v.getDate()==d) 
                {
                    var today = new Date();
                    if(v <= today) r = 1;
                    else r = 2;    
                }                
            } 
        } 
    } 
    return r 
}