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', 'state', 'text', 'isText(str)', null);

var zipCode = new validation('Zip code', 'zipCode', 'text', 'isZipCode(str)', 'xxxxx or xxxxx-xxxx');

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 program = new validation('Program', 'program', 'text', 'isSelect(formObj)', null);

var gradYear = new validation('HS Grad Year', 'gradYear', 'text', 'isSelect(formObj)', null);

var c_id = new validation('Campus', 'c_id', 'text', 'isSelect(formObj)', null);

var elts = new Array(firstName,lastName, address, city, state, zipCode, phone, email, c_id, program, 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;

  if ((l != 5) && (l != 10)) { return false }

  for (j=0; j<l; j++) {

    if ((l == 10) && (j == 5)) {

      if (str.charAt(j) != "-") { return false }

    } else {

      if ((str.charAt(j) < "0") || (str.charAt(j) > "9")) { return false }

    }

  }

  return true;

}

 

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;

}
