/*this script is used for ALL request information pages in the SHC website.  There are
2 different functions here because of the 2 different types of request pages.
1.  The requestion-information.html page.
2.  The short form which 
*/	
	
//form validator
//params:  theForm = sending form:  requestType = extended for dropdown form, short for page forms, standard for all other forms.
//NOTE:  case sensitive.  make sure the field names are in all lower case - no exceptions.
function ValidateFields(theForm, requestType)
{

	
	var temp = new Array();	
	var allValid = true;  
	var checkStr = "";
	var checkOK ;
//FRIEND FIELDS FIRST		
	if(requestType == 'refer')  //if the code reached this point, then all of the fields passed validation. AND if it isn't referral then we don't check friends fields.
	{
	  temp[0] = theForm.friendfirstname.value;
	  temp[1] = theForm.friendlastname.value;
	  for( b = 0; b < 2; b++)
	  {  
		 //go through each character to determine if this is an alpha or a hypen.  Numbers are not allowed.		
				  checkStr = temp[b];	
				  //first and last names are required.
				  if(checkStr.length == 0)
				  {
					alert("The First and Last Name fields are required.");
					b == 0 ? theForm.friendfirstname.focus():theForm.friendlastname.focus();
					return(false);			
				  }
				  for (i = 0;  i < checkStr.length;  i++)
				  {
						//the individual character to be checked.
						ch = checkStr.charAt(i);
						if(!isNaN(ch) || ch == "-") //if this is numeric and not a hyphen - invalid
						{
							alert("Numbers are not allowed in the name fields.");
							b == 0 ? theForm.friendfirstname.focus():theForm.friendlastname.focus();
							return (false);
						}
				  }
	   }

		
		//zip code
	  	  if (theForm.friendzip.value == "")
		  {
		    alert("Please enter your zip code.");
		    theForm.friendzip.focus();
		    return (false);
		  }
		  checkStr = theForm.friendzip.value;
			//replace all characters that aren't numeric.
			for (i = 0;  i < checkStr.length;  i++)
			  {
					//the individual character to be checked.
					ch = checkStr.charAt(i);
					if(!isNaN(ch)) //strip out all non numeric characters.
						checkStr.substring(i,1).replace(ch,'');						
			  }
		  //after stripping out all of the characters, the phone number has to be at least 10 numbers.
		  if(checkStr.length < 5)
		  {
			alert("The zip code must be at least 5 numbers in length.");
			theForm.friendzip.focus();
			//don't go any further.
			return (false);			  
		  }	 
		

	  temp[0] = theForm.friendphonenumber.value;
	  temp[1] = theForm.friendcellphone.value;
	  for( b = 0; b < 2; b++)
	  {  
		 //go through each character to determine if this is an alpha or a hypen.  Numbers are not allowed.		
				  checkStr = temp[b];	
			if(b == 1 && checkStr.length == 0)
				break;
			//replace all characters that aren't numeric.
			for (i = 0;  i < checkStr.length;  i++)
			  {
					//the individual character to be checked.
					ch = checkStr.charAt(i);
					if(isNaN(ch)) //strip out all non numeric characters.
						checkStr.substring(i,1).replace(ch,'');						
			  }
			  //after stripping out all of the characters, the phone number has to be at least 10 numbers.
			  if(checkStr.length < 10)
			  {
				alert("Phone numbers and area code must contain at least 10 numbers.");		
				if(b == 0)
					theForm.friendphonenumber.focus();
				if(b == 1)
					theForm.friendcellphone.focus();

				return (false);			  
			  }						
		}
	 	
		//email field is required
		if (theForm.friendemail.value == "")
		 {
		    alert("Please enter a valid email.");
		    theForm.friendemail.focus();
		    return (false);
		  }
	  
		  var value = theForm.friendemail.value;
		  apos=value.indexOf("@");
		  dotpos=value.lastIndexOf(".");

		  if (apos<1||dotpos-apos<2)
		  {
		    alert("Please enter valid email addreess");
		    return (false);
		  }
		 
			
	}//end checking fields for friend information
//PERSON REFERRING
	  temp[0] = theForm.firstname.value;
	  temp[1] = theForm.lastname.value;
	  for( b = 0; b < 2; b++)
	  {  
		 //go through each character to determine if this is an alpha or a hypen.  Numbers are not allowed.		
				  checkStr = temp[b];	
				  //first and last names are required.
				  if(checkStr.length == 0)
				  {
					alert("The First and Last Name fields are required.");
					b == 0 ? theForm.firstname.focus():theForm.lastname.focus();
					return(false);			
				  }
				  for (i = 0;  i < checkStr.length;  i++)
				  {
						//the individual character to be checked.
						ch = checkStr.charAt(i);
						if(!isNaN(ch) || ch == "-") //if this is numeric and not a hyphen - invalid
						{
							alert("Numbers are not allowed in the name fields.");
							b == 0 ? theForm.firstname.focus():theForm.lastname.focus();
							return (false);
						}
				  }
	   }

		
		//zip code
	  	  if (theForm.zip.value == "")
		  {
		    alert("Please enter your zip code.");
		    theForm.zip.focus();
		    return (false);
		  }
		  checkStr = theForm.zip.value;
			//replace all characters that aren't numeric.
			for (i = 0;  i < checkStr.length;  i++)
			  {
					//the individual character to be checked.
					ch = checkStr.charAt(i);
					if(!isNaN(ch)) //strip out all non numeric characters.
						checkStr.substring(i,1).replace(ch,'');						
			  }
		  //after stripping out all of the characters, the phone number has to be at least 10 numbers.
		  if(checkStr.length < 5)
		  {
			alert("The zip code must be at least 5 numbers in length.");
			theForm.zip.focus();
			//don't go any further.
			return (false);			  
		  }	 
		

	  temp[0] = theForm.phonenumber.value;
	  temp[1] = theForm.cellphone.value;
	  for( b = 0; b < 2; b++)
	  {  
		 //go through each character to determine if this is an alpha or a hypen.  Numbers are not allowed.		
				  checkStr = temp[b];	
			if(b == 1 && checkStr.length == 0)
				break;
			//replace all characters that aren't numeric.
			for (i = 0;  i < checkStr.length;  i++)
			  {
					//the individual character to be checked.
					ch = checkStr.charAt(i);
					if(isNaN(ch)) //strip out all non numeric characters.
						checkStr.substring(i,1).replace(ch,'');						
			  }
			  //after stripping out all of the characters, the phone number has to be at least 10 numbers.
			  if(checkStr.length < 10)
			  {
				alert("Phone numbers and area code must contain at least 10 numbers.");		
				if(b == 0)
					theForm.phonenumber.focus();
				if(b == 1)
					theForm.cellphone.focus();

				return (false);			  
			  }						
		}
	 	
		//email field is required
		if (theForm.email.value == "")
		 {
		    alert("Please enter a valid email.");
		    theForm.email.focus();
		    return (false);
		  }
	  
		  var value = theForm.email.value;
		  apos=value.indexOf("@");
		  dotpos=value.lastIndexOf(".");

		  if (apos<1||dotpos-apos<2)
		  {
		    alert("Please enter valid email addreess");
		    return (false);
		  }
		  		 			 
		  if (theForm.program.value == "")
		  {
				alert("Please select a specific program option.");
				theForm.program.focus();
				return (false);
		  }
		
		return(true);	
}
	

function setProgramName(theObject) { 
	document.getElementById("programfullname").value = theObject.getElementsByTagName("option")[theObject.selectedIndex].innerHTML;
} 

	
	