var geo;

function formCheck(passForm, mode, description)
{
	if (mode == "LOGIN")
	{
		if (document.login.USER_NAME.value == "")
		{
			alert("Please enter your Username.");
	        	document.login.USER_NAME.focus();
			return false;
		}

		if (document.login.USER_PASSWORD.value == "")
		{
			alert("Please enter your Password.");
	        	document.login.USER_PASSWORD.focus();
			return false;
		}
		return true;
	}
	if (mode == "CONTACT")
	{

		if(passForm.Name.value == "")
		{
			alert("Please enter your Name.");
        		passForm.Name.focus();
			return false;
		}
		
		if(passForm.Email.value == "")
		{
			alert("Please enter your Email address.");
  			passForm.Email.focus();
			return false;
		}
		else
		{
			if( checkEmailValue(passForm.Email.value))
			{
			}
			else
			{
      			    passForm.Email.focus();
			    return false;
			}
		}		
							
	}
	if (mode == "REGISTER")
	{

		if(passForm.FirstName.value == "")
		{
			alert("Please enter your First Name.");
        		passForm.FirstName.focus();
			return false;
		}


		if(passForm.LastName.value == "")
		{
			alert("Please enter your Last Name.");
        		passForm.LastName.focus();
			return false;
		}

		if ( ValidateYearBorn(passForm.YearBorn, 13) != true )
		{
			passForm.YearBorn.focus()
			return false;
		}
		

		if(passForm.Address.value == "")
		{
			alert("Please enter your Address.");
        		passForm.Address.focus();
			return false;
		}


		if(passForm.City.value == "")
		{
			alert("Please enter your City.");
        		passForm.City.focus();
			return false;
		}


		if(passForm.State.value == "noneSelected")
		{
			alert("Please select your State.");
        		passForm.State.focus();
			return false;
		}


		if(passForm.Zip.value == "")
		{
			alert("Please enter your Zip/Postal Code.");
        		passForm.Zip.focus();
			return false;
		}
		
		if (passForm.Zip.value.length < 5)
		{
			alert("Please enter your Zip/Postal Code.");
        		passForm.Zip.focus();
			return false;
		}


		if ( ValidatePhone(passForm.AreaCode.value, 3) != true )
		{
			passForm.AreaCode.focus()
			return false;
		}
		if ( ValidatePhone(passForm.LocalExchange.value, 3) != true )
		{
			passForm.LocalExchange.focus()
			return false;
		}
		if ( ValidatePhone(passForm.Phone.value, 4) != true )
		{
			passForm.Phone.focus()
			return false;
		}
		
		
		if(passForm.Email.value == "")
		{
			alert("Please enter your Email address.");
  			passForm.Email.focus();
			return false;
		}
		else
		{
			if( checkEmailValue(passForm.Email.value))
			{
			}
			else
			{
                            passForm.Email.focus();
			    return false;
			}
		}		
	}

	if (mode == "REGISTERGOL")
	{

		if(passForm.FirstName.value == "")
		{
			alert("Please enter your First Name.");
        		passForm.FirstName.focus();
			return false;
		}


		if(passForm.LastName.value == "")
		{
			alert("Please enter your Last Name.");
        		passForm.LastName.focus();
			return false;
		}


		if(passForm.Address.value == "")
		{
			alert("Please enter your Address.");
        		passForm.Address.focus();
			return false;
		}


		if(passForm.City.value == "")
		{
			alert("Please enter your City.");
        		passForm.City.focus();
			return false;
		}


		if(passForm.State.value == "noneSelected")
		{
			alert("Please select your State.");
        		passForm.State.focus();
			return false;
		}


		if(passForm.Zip.value == "")
		{
			alert("Please enter your Zip/Postal Code.");
        		passForm.Zip.focus();
			return false;
		}
		
		if (passForm.Zip.value.length < 5)
		{
			alert("Please enter your Zip/Postal Code.");
        		passForm.Zip.focus();
			return false;
		}


		if ( ValidatePhone(passForm.AreaCode.value, 3) != true )
		{
			passForm.AreaCode.focus()
			return false;
		}
		if ( ValidatePhone(passForm.LocalExchange.value, 3) != true )
		{
			passForm.LocalExchange.focus()
			return false;
		}
		if ( ValidatePhone(passForm.Phone.value, 4) != true )
		{
			passForm.Phone.focus()
			return false;
		}
		
		
		if(passForm.Email.value == "")
		{
			alert("Please enter your Email address.");
  			passForm.Email.focus();
			return false;
		}
		else
		{
			if( checkEmailValue(passForm.Email.value))
			{
			}
			else
			{
                            passForm.Email.focus();
			    return false;
			}
		}	

		if(passForm.Child1Name.value == "")
		{
			alert("Please enter the child's name.");
			passForm.Child1Name.focus();
			return false;
		}	
		else
		{
			if (passForm.Child1Age.value =="noneSelected")
  			{
				alert("Please select the child's age.");
				passForm.Child1Age.focus();
				return false;
			}
		}

		if(passForm.Child2Name.value == "")
		{
		}	
		else
		{
			if (passForm.Child2Age.value =="noneSelected")
  			{
				alert("Please select the child's age.");
				passForm.Child2Age.focus();
				return false;
			}
		}

		if(passForm.Child3Name.value == "")
		{
		}	
		else
		{
			if (passForm.Child3Age.value =="noneSelected")
  			{
				alert("Please select the child's age.");
				passForm.Child3Age.focus();
				return false;
			}
		}
	}
	if (mode == "RESERVEPARTY2")
	{

		if (passForm.PartyTheme.value == "noneSelected")
		{
			alert("Please select a party theme.");
			passForm.PartyTheme.focus();
			return false;
		}

		if (passForm.PartyGoogol.checked == false)
		{
			if (passForm.PartyGreat.checked == false)
			{
				alert("Please select a type of party.");
				passForm.PartyGoogol.focus();
				return false;
			}
		}

		if (passForm.PartyGoogol.checked == true)
		{
			if (passForm.PartyGreat.checked == true)
			{
				alert("Please select only one type of party.");
				passForm.PartyGoogol.focus();
				return false;
			}
		}

		if (passForm.PartyGoogol.checked == true)
		{
			if (passForm.PartyPizza.value == "noneSelected")
			{
				alert("Please select the kind of pizza.");
				passForm.PartyPizza.focus();
				return false;
			}
		}

		if (passForm.PartyGreat.checked == true)
		{
			if (passForm.PartyPizza.value == "noneSelected")
			{
				return true;
			}
			else
			{
				alert("Please note that pizza comes only with a Googol party.");
				passForm.PartyPizza.value = "noneSelected";
				return false;
			}

		}
							
	}
	if (mode == "RESERVEPARTY3")
	{

		if(passForm.PartyChildName.value == "")
		{
			alert("Please enter the Child's Name.");
        		passForm.PartyChildName.focus();
			return false;
		}

		if(passForm.PartyChildBirthday.value == "")
		{
			alert("Please enter the child's birthdate.");
        		passForm.PartyChildBirthday.focus();
			return false;
		}


		if(passForm.PartyFirstName.value == "")
		{
			alert("Please enter your First Name.");
        		passForm.PartyFirstName.focus();
			return false;
		}	
	
		if(passForm.PartyLastName.value == "")
		{
			alert("Please enter your Last Name.");
        		passForm.PartyLastName.focus();
			return false;
		}


		if(passForm.Address.value == "")
		{
			alert("Please enter your Address.");
        		passForm.Address.focus();
			return false;
		}


		if(passForm.City.value == "")
		{
			alert("Please enter your City.");
        		passForm.City.focus();
			return false;
		}


		if(passForm.State.value == "noneSelected")
		{
			alert("Please select your State.");
        		passForm.State.focus();
			return false;
		}


		if(passForm.Zip.value == "")
		{
			alert("Please enter your Zip/Postal Code.");
        		passForm.Zip.focus();
			return false;
		}
		
		if (passForm.Zip.value.length < 5)
		{
			alert("Please enter your Zip/Postal Code.");
        		passForm.Zip.focus();
			return false;
		}


		if ( ValidatePhone(passForm.AreaCode.value, 3) != true )
		{
			passForm.AreaCode.focus()
			return false;
		}
		if ( ValidatePhone(passForm.LocalExchange.value, 3) != true )
		{
			passForm.LocalExchange.focus()
			return false;
		}
		if ( ValidatePhone(passForm.Phone.value, 4) != true )
		{
			passForm.Phone.focus()
			return false;
		}
		
		
		if(passForm.Email.value == "")
		{
			alert("Please enter your Email address.");
  			passForm.Email.focus();
			return false;
		}
		else
		{
			if( checkEmailValue(passForm.Email.value))
			{
			}
			else
			{
                            passForm.Email.focus();
			    return false;
			}
		}		
	}

	if (mode == "VOLUNTEER")
	{

		if(passForm.FirstName.value == "")
		{
			alert("Please enter your First Name.");
        		passForm.FirstName.focus();
			return false;
		}


		if(passForm.LastName.value == "")
		{
			alert("Please enter your Last Name.");
        		passForm.LastName.focus();
			return false;
		}

		if(passForm.Email.value == "")
		{
			alert("Please enter your Email address.");
  			passForm.Email.focus();
			return false;
		}
		else
		{
			if( checkEmailValue(passForm.Email.value))
			{
			}
			else
			{
  			    passForm.Email.focus();
			    return false;
			}
		}	

		if(passForm.ParentName.value == "")
		{
			alert("Please enter your Mom or Dad's Name.");
        		passForm.ParentName.focus();
			return false;
		}	

		if ( ValidatePhone(passForm.AreaCode.value, 3) != true )
		{
			passForm.AreaCode.focus()
			return false;
		}
		if ( ValidatePhone(passForm.LocalExchange.value, 3) != true )
		{
			passForm.LocalExchange.focus()
			return false;
		}
		if ( ValidatePhone(passForm.Phone.value, 4) != true )
		{
			passForm.Phone.focus()
			return false;
		}
				
		if(passForm.Address.value == "")
		{
			alert("Please enter your Address.");
        		passForm.Address.focus();
			return false;
		}


		if(passForm.City.value == "")
		{
			alert("Please enter your City.");
        		passForm.City.focus();
			return false;
		}


		if(passForm.State.value == "noneSelected")
		{
			alert("Please select your State.");
        		passForm.State.focus();
			return false;
		}


		if(passForm.Zip.value == "")
		{
			alert("Please enter your Zip/Postal Code.");
        		passForm.Zip.focus();
			return false;
		}
		
		if (passForm.Zip.value.length < 5)
		{
			alert("Please enter your Zip/Postal Code.");
        		passForm.Zip.focus();
			return false;
		}

		if(passForm.Age.value == "noneSelected")
		{
			alert("Please select your age.");
        		passForm.Age.focus();
			return false;
		}

	}


}  

function ValidNum(NumObj)
{
	var myNum, cleanNum;

	myNum = NumObj.value
	cleanNum = parseInt(myNum);

	if ( myNum == "")
	{
		return true;
	}
       	if(myNum != cleanNum)
	
	{
		alert("You have entered an invalid number.Please re-enter.");
		NumObj.focus();
		return false;
	}
	return true;
}
function ValidateYearBorn(NumObj, age)
{
	var myNum, cleanNum, thisYear;
	thisYear = new Date().getYear();

	myNum = NumObj.value
 
	if (NumObj.value == "undefined")
	{
		alert("Please select the year you were born.")
		return false;
	}	

	if (NumObj.value == "noneSelected")
	{
		alert("Please select the year you were born.")
		return false;
	}	

	if (thisYear - myNum - age < 0)
 	{
		alert("You must be at least 13 years old to register! Please have a parent or guardian register for you.");
		return false;
	}
	return true;
}

function ValidatePhone(field, len)
{
	var myNum;
	
	myNum = field;


	if (myNum == "")
	{
		alert("Please enter a valid phone number.");
		return false;
	}
	
	if (IsNumeric(myNum) == false)
	{
		alert("Please enter a valid phone number.");
		return false;		
	}
	
	if (field.length != len)
	{
		alert("Please enter a valid phone number.");
		return false;		
	}	
	
	return true
}

function checkEmailValue(emailValue)
{     //runs the validate script and returns error box or nonerror

	if (emailValue == "")
	{
		return true;
	}

	startPos = 0;

	commaPos = emailValue.indexOf(",",startPos+1);	//position of colomn
	while (commaPos != -1)
	{
		endPos = commaPos;
		emailElement = emailValue.substring(startPos,endPos);
		if(!validEmail(emailElement))
		{
			alert("You have entered an invalid email address. Please re-enter it.");
			return false;
		}
		startPos = endPos + 1;		
		commaPos = emailValue.indexOf(",",startPos);	//position of colomn
	}
	endPos = emailValue.length;
	emailElement = emailValue.substring(startPos,endPos);

	if(!validEmail(emailElement))
	{
		alert("You have entered an invalid email address. Please re-enter it.");
		return false;
	}
	return true;		
}				

function validEmail(email)
{
	invalidchars = " /:;"

// remove leading spaces
	while(email.substring(0,1)==" ")
	{
		email = email.substring(1,email.length);
	}

// remove trailing spaces
	while(email.substring(email.length-1,email.length)==" ")
	{

		email = email.substring(0,email.length-1);
	}

	if(email == "")
	{			//checks to see if blank field
		return false
	}
			
	for(i=0;i<invalidchars.length;i++)
	{ //checks for invalid chars
			
		badchars = invalidchars.charAt(i)
			
		if(email.indexOf(badchars,0)!= -1)
		{
			return false
		}	
	}
	
	atPos = email.indexOf("@",1)  //holds position of "@"
			
	if(atPos == -1) 
	{	//checks to see if "@" present
		return false
	}
							
	if(email.indexOf("@",atPos+1) != -1) 
	{ //checks for second "@"
		return false
	}
							
	periodPos = email.indexOf(".",atPos) //holds position of "."
			
	if(periodPos == -1)
	{ //checks for presence of "."
		return false
	}
	if(periodPos+3 > email.length)
	{//makes sure at least two chars after the period
		return false
	}
	return true 
}

function IsNumeric(sText)
{	
	var ValidChars = "01234567890.";
	var IsNumber = true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++)
	{
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}
	


/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

