// Make the error box go away
//
function clearErrors() {
  var e = document.getElementById("errors");
  e.style.display = "none";
  return true;
}

// 
function checkForm(aForm) {
  var msg = "";
  
  if (aForm.firstName.value == "") msg += "<li>Please enter a first name.</li>";
  if (aForm.lastName.value == "") msg += "<li>Please enter a last name.</li>";
  if (aForm.organization.value == "") msg += "<li>Please enter an organization name.</li>";
  if (aForm.hearAbout.value == "") msg += "<li>Please tell us how you heard about MPV.</li>";

  msg += checkState(aForm.state.value);
  msg += checkPhone(aForm.telephone.value);
  msg += checkEmail(aForm.emailAddress.value);
  
  if (msg != "") {
    var e = document.getElementById("errors");
    e.innerHTML = "<ul>" + msg + "</ul>";
    e.style.display = "block";
    return false;
  }
  return true;
}

// Two-digit State Abbreviation
//
function checkState(value) {
  var error="";
  var regex = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NE|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i;
  
  var gotIt = regex.exec(value)
  if(!gotIt) {
    error = "<li>Please enter a valid state abbreviation.</li>";
  }
  return error;
}

// email

function checkEmail (strng) {
  var error="";
  
  // Check for an empty address
  if (strng == "") {
     error = "<li>You didn't enter an email address.</li>";
  }

  // Check for missing @'s and .'
  var emailFilter=/^.+@.+\..{2,3}$/;
  if (!(emailFilter.test(strng))) { 
    error = "<li>Please enter a valid email address.</li>";
  }
  else {
    //test email for illegal characters
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
    if (strng.match(illegalChars)) {
      error = "<li>The email address contains illegal characters.</li>";
    }
  }
  return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
  var error = "";
  if (strng == "") {
    error = "<li>You didn't enter a phone number.</li>";
  }

  var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
  if (isNaN(parseInt(stripped))) {
    error = "<li>The phone number contains illegal characters.</li>";
  }
    
  if (!(stripped.length == 10)) {
	  error = "<li>The phone number is the wrong length. Make sure you included an area code.</li>";
  }
    
  return error;
}
