// This is the "hash" that will hold the form's initial values when it was loaded
var initialValues = new Object;

// This is the global that gets set when the form's "real" submit button gets
// clicked on.  This is needed to control which submit button(s) on forms with
// multiple submit buttons actually trigger form validation.
var validateAction;

// This function preps the form fields so that they highlight on focus.
// This used to also highlight on mouseover but Bryce felt that was
// a bit too much so now it only highlights on focus.
function prepFormFields() {
   var htmlElements = Array('input', 'textarea');
   var htmlTempArray;
   for (var i = 0; i < htmlElements.length; i++) {
      htmlTempArray = document.getElementsByTagName(htmlElements[i]);
      for (var j = 0; j < htmlTempArray.length; j++) {
         // Here we create an associative array of all of the select/input/textarea form fields
         // on the page.  We use this in the validateForm function to revert the field back
         // to its original value if there was an error.
         initialValues[htmlTempArray[j].name] = htmlTempArray[j].value;
         if (!htmlTempArray[j].getAttribute("nohilight") && !htmlTempArray[j].className) {
            htmlTempArray[j].onfocus = function() {
             this.className = "active";
            }
            htmlTempArray[j].onblur = function() {
             this.className = "";
            }
         }
      }
   }
}

// This function returns whether the specified form element has an initial value
// or not.
function hasInitialValue(elem) {
   if (initialValues[elem.name] != null && initialValues[elem.name].length > 0) {
      return true;
   }
   else {
      return false;
   }
}

// This function returns whether the specified form element is currently empty
// or not.
function isNotEmpty(elem) {
   var str = elem.value;
   if(str == null || str.length == 0) {
      return false;
   }
   else {
      return true;
   }
}

// THIS FUNCTION PERFORMS VALIDATION BASED ON A SET OF CUSTOM HTML ATTRIBUTES
function validateForm(form) {
   var attrVal, attrReg, attrEq, attrFail, disabledVal, strTemp;

   if (validateAction) {
      for (var i = 0; i < form.length; i++) {
         attrVal = form[i].getAttribute("validate");
         disabledVal = form[i].getAttribute("disabled");
         if (attrVal != null && attrVal.length > 0 && (disabledVal == null || disabledVal.length == 0)) {
            validations = attrVal.split('_');
            for (var j = 0; j < validations.length; j++) {
               alertMesg = '';
               switch (validations[j]) {
                  case 'required' :
                     //required fields ignore the failure message and use the default here
                     if (!isNotEmpty(form[i])) {
                        alertMesg = 'You must complete all required form fields.';
                        fieldName = form[i].getAttribute("displayName");
                        if (fieldName != null && fieldName.length != 0) {
                           alertMesg += ' "' + fieldName + '" was left blank.';
                        }
                        alert(alertMesg);
                        form[i].focus();
                        if (hasInitialValue(form[i])) {
                           form[i].value = initialValues[form[i].name];
                        }
                        return false;
                     }
                     break;

                  case 'regex' :
                     if (isNotEmpty(form[i])) {
                        attrReg = form[i].getAttribute("regex");
                        if (attrReg != null && attrReg.length != 0) {
                           var regex = new RegExp(attrReg);
                           strTemp = form[i].value;
                           if (!strTemp.match(regex)) {
                              attrFail = form[i].getAttribute("failure");

                              if (attrFail) {
                                 alert(attrFail);
                              }
                              else {
                                 fieldName = form[i].getAttribute("displayName");
                                 if (fieldName != null && fieldName.length != 0) {
                                    alert('Invalid data format detected in "' + fieldName + '".');
                                 }
                                 else {
                                    alert('Invalid data format at field "' + form[i].name + '".');
                                 }
                              }
                              form[i].focus();
                              if (hasInitialValue(form[i])) {
                                 form[i].value = initialValues[form[i].name];
                              }
                              return false;
                           }
                        }
                     }
                     break;

                  case 'email' :
                     if (isNotEmpty(form[i]) && window.RegExp) {
                        var dotatom = '\\w!#$%&\'*+\\/=?^`{\\\\}~\\-';
                        eval("var regex = /^["+dotatom+"\\.]+\\@[a-z\\d]["+dotatom+"]*(\\.[a-z\\d]["+dotatom+"]+)+$/i");
                        strTemp = form[i].value;
                        if (!strTemp.match(regex)) {
                           attrFail = form[i].getAttribute("failure");

                           if (attrFail) {
                              alert(attrFail);
                           }
                           else {
                              fieldName = form[i].getAttribute("displayName");
                              if (fieldName != null && fieldName.length != 0) {
                                 alert('"' + form[i].value + '" is not a valid email address at form field "' + fieldName + '".');
                              }
                              else {
                                 alert(form[i].value + ' is not a valid email address.');
                              }
                           }
                           form[i].focus();
                           if (hasInitialValue(form[i])) {
                              form[i].value = initialValues[form[i].name];
                           }
                           return false;
                        }
                     }
                     break;

                  case 'zipcode' :
                     if (isNotEmpty(form[i])) {
                        var regex = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
                        strTemp = form[i].value.replace(/^\s+|\s+$/g,'');
                        if (!strTemp.match(regex)) {
                           attrFail = form[i].getAttribute("failure");

                           if (attrFail) {
                              alert(attrFail);
                           }
                           else {
                              fieldName = form[i].getAttribute("displayName");
                              if (fieldName != null && fieldName.length != 0) {
                                 alert('"' + form[i].value + '" is not a valid US zip code at form field "' + fieldName + '".');
                              }
                              else {
                                 alert(form[i].value + ' is not a valid US zip code.');
                              }
                           }
                           form[i].focus();
                           if (hasInitialValue(form[i])) {
                              form[i].value = initialValues[form[i].name];
                           }
                           return false;
                        }
                     }
                     break;

                  case 'numeric' :
                     if (isNotEmpty(form[i])) {
                        var regex = /\D+/;
                        strTemp = form[i].value;
                        if (strTemp.match(regex)) {
                           attrFail = form[i].getAttribute("failure");

                           if (attrFail) {
                              alert(attrFail);
                           }
                           else {
                              fieldName = form[i].getAttribute("displayName");
                              if (fieldName != null && fieldName.length != 0) {
                                 alert('Field "' + fieldName + '" may only contain numbers (0-9).');
                              }
                              else {
                                 alert('Field may only contain numbers (0-9).');
                              }
                           }
                           form[i].focus();
                           if (hasInitialValue(form[i])) {
                              form[i].value = initialValues[form[i].name];
                           }
                           return false;
                        }
                     }
                     break;

                  case 'alpha' :
                     if (isNotEmpty(form[i])) {
                        var regex = /[^a-zA-Z]+/;
                        strTemp = form[i].value;
                        if (strTemp.match(regex)) {
                           attrFail = form[i].getAttribute("failure");

                           if (attrFail) {
                              alert(attrFail);
                           }
                           else {
                              fieldName = form[i].getAttribute("displayName");
                              if (fieldName != null && fieldName.length != 0) {
                                 alert('Field "' + fieldName + '" may only contain letters (A-Z).');
                              }
                              else {
                                 alert('Field may only contain letters (A-Z).');
                              }
                           }
                           form[i].focus();
                           if (hasInitialValue(form[i])) {
                              form[i].value = initialValues[form[i].name];
                           }
                           return false;
                        }
                     }
                     break;

                  case 'alphanumeric' :
                     if (isNotEmpty(form[i])) {
                        var regex = /[^A-Za-z0-9]+/;
                        strTemp = form[i].value;
                        if (strTemp.match(regex)) {
                           attrFail = form[i].getAttribute("failure");

                           if (attrFail) {
                              alert(attrFail);
                           }
                           else {
                              fieldName = form[i].getAttribute("displayName");
                              if (fieldName != null && fieldName.length != 0) {
                                 alert('Field "' + fieldName + '" may only contain letters or numbers (A-Z, 0-9).');
                              }
                              else {
                                 alert('Field may only contain letters or numbers (A-Z, 0-9).');
                              }
                           }
                           form[i].focus();
                           if (hasInitialValue(form[i])) {
                              form[i].value = initialValues[form[i].name];
                           }
                           return false;
                        }
                     }
                     break;

                  case 'equals' :
                     attrEq = form[i].getAttribute("equals");
                     var objEq = document.getElementById(attrEq);
                     if (objEq) {
                        if (form[i].value != objEq.value) {
                           attrFail = form[i].getAttribute("failure");

                           if (attrFail) {
                              alert(attrFail);
                           }
                           else {
                              alert('Form fields do not match');
                           }
                           form[i].focus();
                           if (hasInitialValue(form[i])) {
                              form[i].value = initialValues[form[i].name];
                           }
                           return false;
                        }
                     }
                     break;
               } //End switch
            }  //End for
         } //End if
      } //End for
   } //End if

   return true;
}

document.ready = function () {
   prepFormFields();
}

