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;
			}
		}	
		return true;	
							
	}
	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;
			}
		}		
		return true;	
	}	

	if (mode == "CONTRIBUTE")
	{

		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;
			}
		}	
		return true;	

	}

	if (mode == "DONATE")
	{

		if(passForm.x_first_name.value == "")
		{
			alert("Please enter your First Name.");
        		passForm.x_first_name.focus();
			return false;
		}


		if(passForm.x_last_name.value == "")
		{
			alert("Please enter your Last Name.");
        		passForm.x_last_name.focus();
			return false;
		}


		if(passForm.x_address.value == "")
		{
			alert("Please enter your Address.");
        		passForm.x_address.focus();
			return false;
		}


		if(passForm.x_city.value == "")
		{
			alert("Please enter your City.");
        		passForm.x_city.focus();
			return false;
		}


		if(passForm.x_state.value == "noneSelected")
		{
			alert("Please select your State.");
        		passForm.x_state.focus();
			return false;
		}


		if(passForm.x_zip.value == "")
		{
			alert("Please enter your Zip/Postal Code.");
        		passForm.x_zip.focus();
			return false;
		}
		
		if (passForm.x_zip.value.length < 5)
		{
			alert("Please enter your Zip/Postal Code.");
        		passForm.x_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.x_email.value == "")
		{
			alert("Please enter your Email address.");
  			passForm.x_email.focus();
			return false;
		}
		else
		{
			if( checkEmailValue(passForm.x_email.value))
			{
			}
			else
			{
                            passForm.x_email.focus();
			    return false;
			}
		}	
		if(passForm.x_occupation.value == "")
		{
			alert("Please enter your occupation.");
        		passForm.x_occupation.focus();
			return false;
		}
		if(passForm.x_company.value == "")
		{
			alert("Please enter your employer.");
        		passForm.x_company.focus();
			return false;
		}		
		return true;	

	}

}  

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));
}
