// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()-. ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

// Check for properly formatted zip code
function validateZIP(field) {
	var valid = "0123456789-";
	var hyphencount = 0;

	if (field.length!=5 && field.length!=10) {
		alert("Please enter your 5 digit or 5 digit+4 zip code.");
		return false;
	}
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") {
		alert("Invalid characters in your zip code.  Please try again.");
		return false;
	}
	if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
		alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
		return false;
   }
}
return true;
}


function Validator(theForm){

	// build regular expression to filter invalid characters
	validSearch=/[^0-9a-zA-Z\s]/;
	validQuestion=/[^0-9a-zA-Z\.|\s]/;
	// Was FirstName entered
	if (theForm.FirstName.value == "") {
		alert("Please enter your First Name.");
		theForm.FirstName.focus();
		theForm.FirstName.select();
		return (false);
	}
	else if (validSearch.test(theForm.FirstName.value)){
		alert("The value entered contains one or more invalid characters.\nPlease try again.");
		theForm.FirstName.focus();
		theForm.FirstName.select();
		return (false); 
	}
	
	
	// Was LastName entered
	if (theForm.LastName.value != "") {
		if (validSearch.test(theForm.LastName.value)){
		alert("The value entered contains one or more invalid characters.\nPlease try again.");
		theForm.LastName.focus();
		theForm.LastName.select();
		return (false); 
		}
	}
	
	/*
	// Was Address, City, State, Zip entered
	if (theForm.Address1.value != "") {
		if (validSearch.test(theForm.Address1.value)){
		alert("The value entered contains one or more invalid characters.\nPlease try again.");
		theForm.Address1.focus();
		theForm.Address1.select();
		return (false);
		}
	}	
	// City
	if (theForm.City.value != "") {
		if (validSearch.test(theForm.City.value)){
		alert("The value entered contains one or more invalid characters.\nPlease try again.");
		theForm.City.focus();
		theForm.City.select();
		return (false); 
		}
	}	
	// State
	if (theForm.State.value != "") {
		if (validSearch.test(theForm.State.value)){
		alert("The value entered contains one or more invalid characters.\nPlease try again.");
		theForm.State.focus();
		theForm.State.select();
		return (false); 
		}
	}
	// Zip
	if (theForm.Zip.value != "") {
		if (validateZIP(theForm.Zip.Value) == false){
		alert("The value entered contains one or more invalid characters.\nPlease try again.");
		theForm.Zip.focus();
		theForm.Zip.select();
		return (false); 
		}
	}		
	
	*/ 
	
	// Was Phone entered
	/*
	if ((theForm.Phone.value == "") || (theForm.Phone.value == null)) {
		alert("Please enter your phone number.");
		theForm.Phone.focus();
		return (false);
	}
	*/
	
	if (theForm.Phone.value != "") {
		if (checkInternationalPhone(theForm.Phone.value)==false){
			alert("Please enter a valid phone number")
			//theForm.Phone.value=""
			theForm.Phone.select()
			theForm.Phone.focus()
			return (false);
		}
	}
	// Was help question entered
	if (theForm.helpQuestion.value == "") {
		alert("Please tell us what we can help you with.");
		theForm.helpQuestion.focus();
		return (false);
	}
	else if (validQuestion.test(theForm.helpQuestion.value)){
		alert("The value entered contains one or more invalid characters.\nPlease try again.");
		theForm.helpQuestion.focus();
		return (false); 
	}

//	 Was email entered
	if (theForm.Email.value != "") {
		var strFieldContents = theForm.Email.value;
		var bolValid = true;
		var intCountAtSign;
		var intCountPeriod;
		var intFieldLength;
		var i;

		intFieldLength = strFieldContents.length;	

		if (intFieldLength < 5) {
			bolValid = false;
		} else {
			intCountAtSign = 0;
			intCountPeriod = 0;
			for (i = 0;  i < intFieldLength;  i++) {
				if (bolValid == true) {
					if (strFieldContents.charAt(i) == "@") {
						if (intCountAtSign > 0) {
							bolValid = false;
						} else { 
							intCountAtSign = intCountAtSign + 1;
  					 }
					}
					if (strFieldContents.charAt(i) == ".") {
						intCountPeriod = intCountPeriod + 1;
				}
					if (strFieldContents.charAt(i) == " ") {
						bolValid = false;
					}
				}
			}

		}
		if (bolValid == false || intCountPeriod == 0 || intCountAtSign == 0) {
			alert("Please enter a valid email address.");
			theForm.Email.select();
			theForm.Email.focus();
			return (false);
		}
}

	return (true);
}
