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 first_name = new validation('First name', 'first_name', 'text', 'isText(str)', null);
var last_name = new validation('Last name', 'last_name', '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', 'isSelect(formObj)', null);
var postal_code = new validation('Zip code', 'postal_code', 'text', 'isZipCode(str)', 'xxxxx or xxxxx-xxxx');
var phone = new validation('Day Phone', 'phone', 'text', 'isPhoneNum(str)', 'xxx-xxx-xxxx');
var phone2 = new validation('Work Phone', 'phone2', 'text', 'isPhoneNum(str)', 'xxx-xxx-xxxx');
var email = new validation('E-mail', 'email', 'text', 'isEmail(str)', null);
var email2 = new validation('Confirm E-mail', 'custom1', 'text', 'isEmail(str)', null);
var country = new validation('Country', 'country', 'text', 'isSelect(formObj)', null);
var program_code = new validation('Program', 'program_code', 'text', 'isSelect(formObj)', null);
var grad_year = new validation('Graduation Year', 'grad_year', 'text', 'isSelect(formObj)', null);
var enrolldate = new validation('Enroll date', 'plan_to_start', 'text', 'isSelect(formObj)', null);
var studytime = new validation('Study Time', 'time_to_commit', 'text', 'isSelect(formObj)', null)
var rating = new validation('Learner Rating', 'self_directed_rating', 'text', 'isSelect(formObj)', null)
var writing = new validation('Writing Ability', 'english_writing_ability', 'text', 'isSelect(formObj)', null)
var nursing_question = new validation('Nursing Question', 'nursing_question', 'text', 'isText(str)', null)

var elts = new Array(first_name, last_name, email, email2, address, city, state, postal_code, country, phone, phone2, program_code, grad_year, enrolldate, studytime, rating, writing, nursing_question);

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 showNursingQuestion(pc) {
    //alert(pc);
    //pc == '' ? document.getElementById('nursingQuestion').style.display = 'block' : document.getElementById('nursingQuestion').style.display = 'none';
}

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].formEltName == "nursing_question" && document.getElementById('nursingQuestion').style.display == 'none') {
        continue;
    }

    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 OnButton1();
  return true;
}