var fieldCheck = new Object;
  
//fieldCheck['name'] = /^((\w+)|(\w.))( +((\w+)|(\w.)))*$/;
//fieldCheck['zip code'] = /^\d{5}(-\d{4})?$/;

fieldCheck['name'] = /^\S+\s?.*?$/;
fieldCheck['city'] = /^\S+\s?.*?$/;
fieldCheck['address'] = /^\S+\s?.*?$/;
//fieldCheck['email address'] = /^(([a-z0-9_\.\+\-\=\?\^\#]){1,64}\@(([a-z0-9\-]){1,251}\.){1,252}[a-z0-9]{2,4})$/i;
fieldCheck['email address'] = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
//fieldCheck['phone number'] = /^((?:\D*\d\D*){7,})$/;
fieldCheck['phone'] = /^\d{3}\-\d{3}-\d{4}$/;
fieldCheck['zip code'] = /^(\s*\d{5}(?:[-]\d{4})?\s*)$/;

fieldCheck['_error_raised'] = 0;

function isValid(type, s) {
  return s.search(fieldCheck[type]) != -1;
}

function checkField(type, s) {
  return (s && !isValid(type, s)) ? ('"' + s + '" is not a valid ' + type + '.') : 0;
}

function checkAndAlert(type, s) {
  var r = checkField(type, s);

  if (r) alert(r + '  Please re-enter.');
}

function getFieldValue(type, id) {
  if (!fieldCheck['_error_raised'])
    alert('must define getFieldValue()');
  fieldCheck['_error_raised'] = 1;
  return null;
}

function accumErrors(type, i) {
  var res = checkField(type, getFieldValue(type, i));
  return res ? (res+'\n') : '';
}

function testcheckFields() {
  
  var s = ""; var i=0;

  fieldCheck['_error_raised'] = 0;


  {
    var t = "";
    t += accumErrors("name",i);
    t += accumErrors("zip code",i);
    t += accumErrors("email address",i);
    // t += accumErrors("phone",i); --  ONLY USE IF PHONE NUMBER INCLUDED

    if (!getFieldValue("prefix",i))
       t += "You must select a prefix.\n";

    if (!getFieldValue("name",i))
       t += "Your name is required.\n";

    if (!getFieldValue("address1",i))
       t += "Your address is required.\n";

    if (!getFieldValue("city",i))
       t += "Your city is required.\n";

    if (!getFieldValue("state",i))
       t += "Your state is required.\n";

    if (!getFieldValue("zip",i))
       t += "Your zip code is required.\n";

    //if (!getFieldValue("phone",i))
    //   t += "Your phone number is required.\n";

    if (!getFieldValue("email address",i))
       t += "An email address is required.\n";

    t += "something"
    s += t
  }

  if (s) {
    alert("Please correct the following errors before submitting:\n\n"+s);
    return false;
  }

  window.onbeforeunload=null;
  return true;
}

function checkFields() {
  
  var s = ""; var i=0;

  fieldCheck['_error_raised'] = 0;

  {
    var t = "";
    t += accumErrors("name",i);
    t += accumErrors("zip code",i);
    t += accumErrors("email address",i);
    //t += accumErrors("phone",i);

    if (!getFieldValue("prefix",i))
       t += "You must select a prefix.\n";

    if (!getFieldValue("name",i))
       t += "Your name is required.\n";

    if (!getFieldValue("address1",i))
       t += "Your address is required.\n";

    if (!getFieldValue("city",i))
       t += "Your city is required.\n";

    if (!getFieldValue("state",i))
       t += "Your state is required.\n";

    if (!getFieldValue("zip",i))
       t += "Your zip code is required.\n";

    //if (!getFieldValue("phone",i))
    //   t += "Your phone number is required.\n";

    if (!getFieldValue("email address",i))
       t += "An email address is required.\n";

    s += t
  }

  if (s) {
    alert("Please correct the following errors before submitting:\n\n"+s);
    return false;
  }

  window.onbeforeunload=null;
  return true;
}

function checkFields2(name) {
  var i=0;
  newval = getFieldValue("name",i);
  if (newval != name) {
    if (confirm("You are about to change the name on this subscription from '"+name+"' to '"+newval+".'  Usually this is an accident and is the result of receiving this link from someone else before they've taken action.  If you previously entered '"+name+"' for your name and are now trying to change it, you can override this check by clicking 'Cancel.'\n\nOtherwise, click 'OK' and then click on the link labeled 'here' following the warning \"If you're not "+name+"\" in the top left-hand corner of the gray box, directly underneath the header 'Add your voice now!.'")) {
      return false
   }
  } else {
    return checkFields();
  }
}

