var isIE3 = (navigator.appVersion.indexOf('MSIE 3') != -1);
function validation(realName, formEltName, eltType, upToSnuff, format) {
  this.realName = realName;
  this.formEltName = formEltName;
  this.eltType = eltType;
  this.upToSnuff = upToSnuff;
  this.format = format;
}
var firstname = new validation('First Name', 'firstName', 'text', 'isText(str)', null);
var lastname = new validation('Last Name', 'lastName', 'text', 'isText(str)', null);
var address = new validation('Address', 'address', 'text', 'isText(str)', null);
var city = new validation('City', 'city', 'text', 'isText(str)', null);
var state = new validation('State/Providence', 'state', 'select', 'isSelect(formObj)', null);
var zipcode = new validation('ZIP/Postal Code', 'zipCode', 'text', 'isZipCode(str)', 'xxxxx or xxxxx-xxxx or Canadian');
var country = new validation('Country', 'country', 'select', 'isSelect(formObj)', null);
var phone = new validation('Day Phone', 'phone', 'text', 'isPhoneNum(str)', 'xxx-xxx-xxxx');
var email = new validation('E-mail', 'email', 'text', 'isEmail(str)', null);
//var campus = new validation('Campus', 'campus', 'select', 'isSelect(formObj)', null);
var course = new validation('Program of Interest', 'course', 'select', 'isSelect(formObj)', null);
var gradYear = new validation('HS Graduation Year', 'gradYear', 'text', 'isText(str)', null);
var elts = new Array(firstname,lastname,address,city,state,zipcode,country,phone,email,course,gradYear);
var allAtOnce = true;

var beginRequestAlertForText = "Please include ";
var beginRequestAlertGeneric = "Please choose a ";
var endRequestAlert = ".";
var beginInvalidAlert = " is not a valid ";
var endInvalidAlert = ".";
var beginFormatAlert = "  Use this format: ";

function isText(str) {
  return (str != "");
}

function isSelect(formObj) {
  return (formObj.selectedIndex != 0);
}

function isRadio(formObj) {
  for (j=0; j<formObj.length; j++) {
    if (formObj[j].checked) {
      return true;
    }
  }
  return false;
}

function isCheck(formObj, form, begin, num) {
  for (j=begin; j<begin+num; j++) {
    if (form.elements[j].checked) {
      return true;
    }
  }
  return false;
}

function isEmail(str) {
  return ((str != "") && (str.indexOf("@") != -1) && (str.indexOf(".") != -1));
}

function isZipCode(str) {
  var l = str.length; // set "l" equal to the length of the submitted ZIP code

  //Canadian: http://www.sitepoint.com/forums/showthread.php?t=158294
  var is_ok_canadian = (/^[ABCEGHJKLMNPRSTVXY][\d][A-Z] \d[A-Z]\d$/gi.test(str)); // regular expression
  if (!is_ok_canadian) {
    if ((l != 5) && (l != 10)) { return false } // must be 5, 6 or ten characters to continue
    for (j=0; j<l; j++) {
      if ((l == 10) && (j == 5)) { // if there are 10 characters, the 6th must be a "-" (hyphen)
        if (str.charAt(j) != "-") { return false }
      } else {
        if ((str.charAt(j) < "0") || (str.charAt(j) > "9")) { return false }
      }
    }
  }
  return true; //<-- uncomment this to get it to work again
}

function isPhoneNum(str) {
  if (str.length != 12) { return false }
  for (j=0; j<str.length; j++) {
    if ((j == 3) || (j == 7)) {
      if (str.charAt(j) != "-") { return false }
    } else {
      if ((str.charAt(j) < "0") || (str.charAt(j) > "9")) { return false }
    }
  }
  return true;
}

function validateForm(form) {
  var formEltName = "";
  var formObj = "";
  var str = "";
  var realName = "";
  var alertText = "";
  var firstMissingElt = null;
  var hardReturn = "\r\n";

  for (i=0; i<elts.length; i++) {
    formEltName = elts[i].formEltName;
    formObj = eval("form." + formEltName);
    realName = elts[i].realName;

    if (elts[i].eltType == "text") {
      str = formObj.value;

      if (eval(elts[i].upToSnuff)) continue;

      if (str == "") {
        if (allAtOnce) {
          alertText += beginRequestAlertForText + realName + endRequestAlert + hardReturn;
          if (firstMissingElt == null) {firstMissingElt = formObj};
        } else {
          alertText = beginRequestAlertForText + realName + endRequestAlert + hardReturn;
          alert(alertText);
        }
      } else {
        if (allAtOnce) {
          alertText += str + beginInvalidAlert + realName + endInvalidAlert + hardReturn;
        } else {
          alertText = str + beginInvalidAlert + realName + endInvalidAlert + hardReturn;
        }
        if (elts[i].format != null) {
          alertText += beginFormatAlert + elts[i].format + hardReturn;
        }
        if (allAtOnce) {
          if (firstMissingElt == null) {firstMissingElt = formObj};
        } else {
          alert(alertText);
        }
      }
    } else {
      if (eval(elts[i].upToSnuff)) continue;
      if (allAtOnce) {
        alertText += beginRequestAlertGeneric + realName + endRequestAlert + hardReturn;
        if (firstMissingElt == null) {firstMissingElt = formObj};
      } else {
        alertText = beginRequestAlertGeneric + realName + endRequestAlert + hardReturn;
        alert(alertText);
      }
    }
    if (!isIE3) {
      var goToObj = (allAtOnce) ? firstMissingElt : formObj;
      if (goToObj.select) goToObj.select();
      if (goToObj.focus) goToObj.focus();
    }
    if (!allAtOnce) {return false};
  }
  if (allAtOnce) {
    if (alertText != "") {
      alert(alertText);
      return false;
   }
  } 
  return true;
}
