var valid = 0;
var alertStr = "";

function validate(validation,valCount,form,firstName,firstNameField,lastName,lastNameField,passwordConfirm,passwordEnterField,passwordConfirmField,emailConfirm,emailEnterField,emailConfirmField,zip,zipField,address,addressField,city,cityField,phone,phoneField,email,emailField) {
	
  valid = 0;
  alertStr = "";

  if (validation == 0) {
    return true;
  }
  
  else {
    if (firstName != "") {
			//alert("valid: " + valid + "valCount: " + valCount)
      firstNameVal(form,firstNameField);
    }

    if (lastName != "") {
			//alert("valid: " + valid + "valCount: " + valCount)
      lastNameVal(form,lastNameField);
    }

    if (passwordConfirm != "") {
			//alert("valid: " + valid + "valCount: " + valCount)
      passwordConfirmVal(form,passwordEnterField,passwordConfirmField);
    }
  
    if (emailConfirm != "") {
			//alert("valid: " + valid + "valCount: " + valCount)
      emailConfirmVal(form,emailEnterField,emailConfirmField);
    }

    if (zip != "") {
			//alert("valid: " + valid + "valCount: " + valCount)
      zipVal(form,zipField);
    }

    if (address != "") {
			//alert("valid: " + valid + "valCount: " + valCount)
      addressVal(form,addressField);
    }
    

    if (email != "") {
			//alert("valid: " + valid + "valCount: " + valCount)
      emailVal(form,emailField);
    }

    if (valid != valCount) {
			//alert(" valid: " + valid + "valCount: " + valCount)			
      alert ("Please fill out the information requested below:\n" + alertStr);
      return false;
    }
  
    if (emailConfirm != "" && valid == valCount) {
      document.forms[form].elements[emailEnterField].value = document.forms[form].elements[emailEnterField].value.toLowerCase()
      document.forms[form].elements[emailConfirmField].value = document.forms[form].elements[emailConfirmField].value.toLowerCase()
      if (alertStr != "") {				
        alert (alertStr)
      }
      return true;
    }
    
   //==>zip, email format,missing fields
    if (email != "" && valid == valCount) {
      document.forms[form].elements[emailField].value = document.forms[form].elements[emailField].value.toLowerCase()
      if (alertStr != "") {				
        alert (alertStr)
        return false;
      }
      return true;
    }
  
    if (valid == valCount) {
      if (alertStr != "") {				
        alert (alertStr)
      }
      return true;
    }
  }
  
}

function removeChr(str, chr){
	
		var cstr = new String(str);
		var restr="";
		
		for (i=0; i <= cstr.length ; i++){
			if (cstr.charAt(i) != chr) restr += cstr.charAt(i);
		}
			
		return restr;
}

function isNumeric(szVal, bAlert){
	var valid = "0123456789"
	var bIsNumeric = 1;
	var temp;
	
	for (var i=0; i< szVal.length; i++) {
		temp = "" +  szVal.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") bIsNumeric = 0;
	}
	
	 if (bIsNumeric == 0 ){
		if (bAlert) alert("Input is not numeric.");
		return false;
	 }
	 
	 return true;

}

function firstNameVal(form,firstNameField) {

  var firstName = document.forms[form].elements[firstNameField].value

  if (removeChr(firstName, " ") == "") {
    alertStr += "\n First Name"
  }
  
  else {
    valid += 1;
  }

}

function lastNameVal(form,lastNameField) {

  var lastName = document.forms[form].elements[lastNameField].value

  if (removeChr(lastName, " ") == "") {
    alertStr += "\n Last Name"
  }
  
  else {
    valid += 1;
  }

}

function validatePassword(password) {
 
  if (password.indexOf(" ") != -1) {
    return "cannot contain spaces";
  }

  if (password.indexOf("<") != -1) {
    return "cannot contain '<'";
  }  

  if (password.indexOf("?") != -1) {
    return "cannot contain '?'";
  }  

  if (password.length < 5) {
    return "must be at least 5 characters";
  }

  if (password.charAt(0) == password.charAt(1) && password.charAt(0) == password.charAt(2)) {
    return "first three characters cannot be the same";
  } 

  return "";

}

function passwordConfirmVal(form,passwordEnterField,passwordConfirmField) {
  
  var passwordEnter = document.forms[form].elements[passwordEnterField].value
  var passwordConfirm = document.forms[form].elements[passwordConfirmField].value

  //==> MMURAO 03/14/2000 - Passwords are case sensitive so we must validate exactly what the 
  //==> user entered rather than validating the upper case version of the entries.
  
  //passwordEnter = passwordEnter.toUpperCase()
  //passwordConfirm = passwordConfirm.toUpperCase()

  if (removeChr(passwordEnter, " ") == "") {
    alertStr += "\n Password"
  }

  else if (validatePassword(passwordEnter) != "") {
    alertStr += "\n Password - " + validatePassword(passwordEnter)
  }

  else if (passwordEnter != passwordConfirm) {
    alertStr += "\n Password - entries do not match"
  }

  else {
     valid += 1;
  }

}

function validateEmail(email) {

  if (email.indexOf(" ") != -1) {
    return "cannot contain spaces";
  }

  if (email.indexOf(",") != -1) {
    return "cannot contain ','";
  }

  if (email.indexOf(";") != -1) {
    return "cannot contain ';'";
  }

  var splitEmailAt = email.split("@");

  if (splitEmailAt.length == 2) { 

    var splitEmailDot = splitEmailAt[1].split(".");  
 
    if (splitEmailDot.length > 1) {
      if (splitEmailAt[0].length < 1 || splitEmailDot[0].length < 1 || splitEmailDot[1].length < 1) { 
        return "must be in the format xxx@xxx.xxx";
      }
      else {
        return "";
      }
    } else {
      return "must be in the format xxx@xxx.xxx";
    }
  } else {
    return "must be in the format xxx@xxx.xxx";      
  }
  
  return "";

}

function emailConfirmVal(form,emailEnterField,emailConfirmField) {

  var emailEnter = document.forms[form].elements[emailEnterField].value
  var emailConfirm = document.forms[form].elements[emailConfirmField].value

  emailEnter = emailEnter.toUpperCase()
  emailConfirm = emailConfirm.toUpperCase()

  if (removeChr(emailEnter, " ") == "") {
    alertStr += "\n E-mail"
  }

  else if (validateEmail(emailEnter) != "") {
    alertStr += "\n E-mail - " + validateEmail(emailEnter)
  }

  else if (emailEnter != emailConfirm) {
    alertStr += "\n E-mail - entries do not match"
  }

  else {
    valid += 1;
  }

}

function validateZip(zip) { 

  if (zip.length == 5) {
    if (isNumeric(zip) == false) {
      return "must contain only numbers";
    }
    else {
      return "";
    }
  }
  
  else if (zip.length == 10) {
	
    var splitZip = zip.split("-");

    if (splitZip.length == 2) {
      if (splitZip[0].length == 5 && splitZip[1].length == 4) {
        if (isNumeric(splitZip[0]) == false || isNumeric(splitZip[1]) == false) {
          return "must be in the format 12345 or 12345-1234";
        }
        else {
          return "";
        }
      } else {
        return "must be in the format 12345 or 12345-1234";
      }
    } else {
      return "must be in the format 12345 or 12345-1234";
    }
  }
  
  else {
    return "must be in the format 12345 or 12345-1234";
  }
  
}

function zipVal(form,zipField) {

  var zip = document.forms[form].elements[zipField].value

  if (removeChr(zip, " ") == "") {
    alertStr += "\n Zip"
  } 

  else if (validateZip(zip) != "") {
    alertStr += "\n Zip - " + validateZip(zip)
  }

  else { 
    valid += 1;
  }

}

function emailVal(form,emailField) {

  var email = document.forms[form].elements[emailField].value

  email = email.toUpperCase()

  if (removeChr(email, " ") == "") {
    alertStr += "\n E-mail"
  }

  else if (validateEmail(email) != "") {
    alertStr += "\n E-mail - " + validateEmail(email)
  }

  else {
    valid += 1;
  }

}

function cityVal(form,cityField) {

  var city = document.forms[form].elements[cityField].value

  if (removeChr(city, " ") == "") {
    alertStr += "\n City"
  }

  else {
    valid += 1;
  }

}

function validateAddress(address) {

  if (address.indexOf("P.O. BOX") != -1 || address.indexOf("PO BOX") != -1 || address.indexOf("POST OFFICE BOX") != -1) { 
    return "You have entered a post office box.  Billing addresses can be post office boxes.  Shipping addresses cannot be post office boxes.";
  }

  return "";
  
}

function addressVal(form,addressField) {

  var address = document.forms[form].elements[addressField].value

  address = address.toUpperCase() 

  if (removeChr(address, " ") == "") {
    alertStr += "\n Address"
  }

  else if (validateAddress(address) != "") {
    alertStr += validateAddress(address)
    valid += 1;
  }  

  else {
    valid += 1;
  }

}

function validatePhone(phone) {

  var splitPhone = phone.split("-");

    if (splitPhone.length == 3) {
      if (splitPhone[0].length == 3 && splitPhone[1].length == 3 && splitPhone[2].length == 4) {
        if (isNumeric(splitPhone[0]) == false || isNumeric(splitPhone[1]) == false || isNumeric(splitPhone[2]) == false) {
          return "must be in the format 123-123-1234";
        }
        else {
          return "";
        }
      } else {
        return "must be in the format 123-456-7890";
      } 
    } else {
      return "must be in the format 123-456-7890";
    }

}

function phoneVal(form,phoneField) {

  var phone = document.forms[form].elements[phoneField].value

  if (removeChr(phone, " ") == "") {
    alertStr += "\n Phone"
  }

  else if (validatePhone(phone) != "") {
    alertStr += "\n Phone - " + validatePhone(phone)
  }  

  else {
    valid += 1;
  }  

}
