function toggleInfo() {
  if (document.getElementById('MoreInformation').checked==true) {
    document.getElementById('dMoreInformation').style.display="block";}
  else {
    document.getElementById('dMoreInformation').style.display="none";
    }
}

function validateForm(theForm) {
  var why = "";
  if (document.getElementById('Saved').checked==false&&document.getElementById('MoreInformation').checked==false){
    why = "<li>This form is only for those who got saved or want more information.</li>";
  } else {
    if (document.getElementById('MoreInformation').checked==true) {
      why += checkName(theForm.first_name.value,"first");
      why += checkName(theForm.last_name.value,"last");
      why += checkPhone(theForm.phone.value);
      why += checkAddress(theForm.address1.value,"a");
      why += checkAddress(theForm.city_town.value,"c");
      why += checkPostalCode(theForm.zip_postal_code.value);
      if (document.getElementById('country').value=="United States") {
        why += checkSRP(theForm.state.value);}
    }
    why += checkEmail(theForm.email.value);
  }
  if (why != "") {
    document.getElementById('errorDetails').innerHTML=why;
    window.scrollTo(0,1130);
    return false;
  } else {
    document.getElementById('Submit').style.display="none";
    return true;}
}

function checkAddress(strng,name) {
  var error="";
  if (strng.length==0||strng.value=="") {
    if (name=="a") {
      error = "<li>You did not enter an address.</li>"}
    else if (name=="c") {
      error = "<li>You did not enter a city or town.</li>"}
  }
  else if (strng.length<2) {
    if (name=="a") {
      error = "<li>Your address is too short.</li>"}
    else if (name=="c") {
      error = "<li>Your city or town is too short.</li>"}
    }
  return error;
}

function checkCountry() {
  if (document.getElementById('country').value=="United States") {
    document.getElementById('state').style.display="block";
    document.getElementById('rProvince').style.display="inline";
    document.getElementById('region_province').style.display="none";}
  else {
    document.getElementById('state').style.display="none";
    document.getElementById('rProvince').style.display="none";
    document.getElementById('region_province').style.display="block";}
}

function checkEmail (strng) {
  var error="";
  if (strng == "") {
    error = "<li>You did not enter an email address.</li>";}
  var emailFilter=/^.+@.+\..{2,4}$/;
  if (!(emailFilter.test(strng))) { 
    error = "<li>Please enter a valid email address.</li>";}
  else {
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
    if (strng.match(illegalChars)) {
      error = "<li>Your email address contains illegal characters.</li>";}
  }
  return error;    
}

function checkMessage(strng) {
  var error = "";
  if (strng.length == 0) {
    error = "<li>You did not enter a message.</li>"}
  if (strng.length != 0 && strng.length < 15) {
    error = "<li>Your message is too short.</li>"}
  return error;    
}

function checkName(strng,name) {
  var error = "";
  if (strng.length == 0) {
    error = "<li>You did not enter a "+name+" name.</li>"}
  if (strng.length != 0 && strng.length < 2) {
    error = "<li>Your "+name+" name should be at least 2 letters long.</li>"}
  //var illegalChars = /\W/; //allow only letters, numbers, and underscores
  var illegalChars = /[^A-Z a-z]/;
  if (illegalChars.test(strng)) {
    error = "<li>Your "+name+" name should only contain alphabetical letters.</li>";
  } 
  return error;    
}

function checkPhone (strng) {
  //test function for in domestic numbers and international numbers (length)
  var error=""
  if (strng != ""||strng.length>0) {
    var phoneFilter=/[^0-9\(\)\-\ \+]/;
    if ((phoneFilter.test(strng))) {
      error = "<li>Your phone number must only contain numbers, dashes, parentheses, and a plus for international numbers.</li>";}
    else {
      if (strng.length<10) {
        error = "<li>Your phone number is too short. Don't forget to include your area code.</li>";}
    }
  }
  return error;
}

function checkPostalCode(strng) {
  var error="";
  if (document.getElementById('country').value=="United States") {
    zipFilter = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
    if (!zipFilter.test(strng)) {
      error="<li>You did not enter a valid zip code</li>";
    }
  }
  else {
    if (strng.length <3) {
      error="<li>Your postal code is too short</li>";}
  }
  return error;
}

function checkSRP (strng) {
  var error=""
  if (strng == "default") {
    error = "<li>You did not select a state or region.</li>";}
  return error;
}