function IsAllSpace(strValue) {
    var re = /^\s*$/;
    return re.test(strValue);
}

function IsNumber(strValue) {
    var re = /^\d*$/;
    return re.test(strValue);
}
function IsAllZero(s){
    var i;
    var counter = 0;
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (c == "0") {
          counter++;
        }
    }
    if (counter >= s.length) {
       return true;
    }
    return false;
}
function isEmailValid(emailAddress) {
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
    var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,8}|[0-9]{1,3})(\\]?)$");
    return (!r1.test(trim(emailAddress)) && r2.test(trim(emailAddress)));
}
function trim(inputString) {
    // Removes leading and trailing spaces from the passed string. Also removes
    // consecutive spaces and replaces it with one space. If something besides
    // a string is passed in (null, custom object, etc.) then return the input.
    if (typeof inputString != "string") {
        return inputString;
    }
    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ") { // Check for spaces at the beginning of the string
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }
    ch = retValue.substring(retValue.length - 1, retValue.length);
    while (ch == " ") { // Check for spaces at the end of the string
        retValue = retValue.substring(0, retValue.length - 1);
        ch = retValue.substring(retValue.length - 1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
        retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ") + 1, retValue.length); // Again, there are two spaces in each of the strings
    }
    return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function
// Validate location information from a form
function validateLocationInput() {
    if (IsAllSpace(document.form1.Location.value)) {
        document.form1.Location.focus();
        alert("Please enter Location.")
        return false;
    }

    if (IsAllSpace(document.form1.Address.value)) {
        document.form1.Address.focus();
        alert("Please enter your address.")
        return false;
    }

    if (IsAllSpace(document.form1.City.value))
    {
        document.form1.City.focus();
        alert("Please enter your City.")
        return false;
    }

	if (document.form1.u_state.selectedIndex == 0){
		document.form1.u_state.focus();
		alert("Please select your state");
		return false;
	}

    if (!IsNumber(document.form1.Zip.value) || document.form1.Zip.value.length != 5) {
        document.form1.Zip.focus();
        alert("Please enter valid Zip Code.")
        return false;
    }

    // Phone number check
    if (!IsNumber(document.form1.CTNAreaCode.value) || document.form1.CTNAreaCode.value.length != 3) {
        document.form1.CTNAreaCode.focus();
        alert("Please enter valid Phone Number.")
        return false;
    }

    if (!IsNumber(document.form1.CTNPrefix.value) || document.form1.CTNPrefix.value.length != 3) {
        document.form1.CTNPrefix.focus();
        alert("Please enter valid Phone Number.")
        return false;
    }

	if (!IsNumber(document.form1.CTNNumber.value) || document.form1.CTNNumber.value.length != 4) {
        document.form1.CTNNumber.focus();
        alert("Please enter valid Phone Number.")
        return false;
    }

    if (IsAllZero(document.form1.CTNAreaCode.value) &&
        IsAllZero(document.form1.CTNPrefix.value) &&
        IsAllZero(document.form1.CTNNumber.value)) {
        document.form1.CTNAreaCode.focus();
        alert("Phone Number cannot be zero.")
        return false;
    }

    return true;
}
function validateCustomerInput() {
    if (IsAllSpace(document.form2.First_Name.value)) {
        document.form2.First_Name.focus();
        alert("Please enter Your First Name.")
        return false;
    }
    if (IsAllSpace(document.form2.Last_Name.value)) {
        document.form2.Last_Name.focus();
        alert("Please enter Your Last Name.")
        return false;
    }
    if (isEmailValid(document.form2.email.value) == false) {
		document.form2.email.focus();
        alert("Your email address is not properly formed.  Please change it and try again.");
        return false;
    }
    if (document.form2.password.value.length < 6 || IsAllSpace(document.form2.password.value)) {
        document.form2.password.focus();
        alert("Your Password should be at least 6 characters.");
        return false;
    }//not IsAllSpace
    if (document.form2.password.value.length < 6 && !IsAllSpace(document.form2.password.value)) {
        document.form2.password.focus();
        alert("Your Password should be at least 6 characters.");
        return false;
    }

    if ( ( document.form2.agecheck[0].checked == false ) && ( document.form2.agecheck[1].checked == false ) ){
        alert ( "Are you 13 years old?" );
        return false;
    }
    if (!document.form2.agecheck[0].checked) {
        document.form2.agecheck[0].focus();
        alert("Sorry, you must be at least 13 years old to order from Mack and Manco Piza.");
        return false;
    }
    if ( ( document.form2.newsletter[0].checked == false ) && ( document.form2.newsletter[1].checked == false ) ){
		document.form2.newsletter[0].focus();
        alert ( "Do you want to receive newsletter?" );
        return false;
    }
	
     return true;
}
function PasswordCheck(){
	if (document.form2.password.value != document.form2.Hidden_password.value) {
      	document.form2.password.focus();
        alert("Your Current password does not match.");
        return false;
    }//not IsAllSpace
	
	if (document.form2.New_Password.value.length < 6 || IsAllSpace(document.form2.password.value)) {
      	document.form2.password.focus();
        alert("Your New Password should be at least 6 characters.");
        return false;
    }//not IsAllSpace
    if (document.form2.Confirm_Password.value.length < 6 && !IsAllSpace(document.form2.password.value)) {
        document.form2.password.focus();
        alert("Your Confirm Password should be at least 6 characters.");
        return false;
    }
    if (document.form2.New_Password.value != document.form2.Confirm_Password.value) {
        document.form2.password.focus();
        alert("Passwords do not match.");
        return false;
    }
	return true;
}
function validateAllInput(){
    if (IsAllSpace(document.form1.First_Name.value)) {
        document.form1.First_Name.focus();
        alert("Please enter Your First Name.")
        return false;
    }
    if (IsAllSpace(document.form1.Last_Name.value)) {
        document.form1.Last_Name.focus();
        alert("Please enter Your Last Name.")
        return false;
    }
	    if (IsAllSpace(document.form1.Location.value)) {
        document.form1.Location.focus();
        alert("Please enter Location.")
        return false;
    }

    if (IsAllSpace(document.form1.Address.value)) {
        document.form1.Address.focus();
        alert("Please enter your address.")
        return false;
    }

    if (IsAllSpace(document.form1.City.value))
    {
        document.form1.City.focus();
        alert("Please enter your City.")
        return false;
    }

	if (document.form1.u_state.selectedIndex == 0){
		document.form1.u_state.focus();
		alert("Please select your state");
		return false;
	}

    if (!IsNumber(document.form1.Zip.value) || document.form1.Zip.value.length != 5) {
        document.form1.Zip.focus();
        alert("Please enter valid Zip Code.")
        return false;
    }

    // Phone number check
    if (!IsNumber(document.form1.CTNAreaCode.value) || document.form1.CTNAreaCode.value.length != 3) {
        document.form1.CTNAreaCode.focus();
        alert("Please enter valid Phone Number.")
        return false;
    }

    if (!IsNumber(document.form1.CTNPrefix.value) || document.form1.CTNPrefix.value.length != 3) {
        document.form1.CTNPrefix.focus();
        alert("Please enter valid Phone Number.")
        return false;
    }

	if (!IsNumber(document.form1.CTNNumber.value) || document.form1.CTNNumber.value.length != 4) {
        document.form1.CTNNumber.focus();
        alert("Please enter valid Phone Number.")
        return false;
    }

    if (IsAllZero(document.form1.CTNAreaCode.value) &&
        IsAllZero(document.form1.CTNPrefix.value) &&
        IsAllZero(document.form1.CTNNumber.value)) {
        document.form1.CTNAreaCode.focus();
        alert("Phone Number cannot be zero.")
        return false;
    }
	return true;
}