var errorStyle = "formLabelError";
// JQuery function to replace the "onsubmit"
$(document).ready(function(){
    $("#theForm").submit(function() {
        var success = true;
        success = ValidateForm();
        if (success == 0) {
            success = false;
        }
        return success;
    });
    
    $("input").focus(function () {
        var tempName = "#" + this.name + "_label";
        $(tempName).removeClass(errorStyle);
    });
    


});
var arrayRequiredFields = new Array("first_name", "last_name", "email", "phone", "company");
var stdMessage = "Items on the form need your attention.<BR>Please correct any highlighted items and try again.";
function OverrideErrorStyles(newErrorStyle)
{
 errorStyle = newErrorStyle;
}
function OverrideRequiredFields(newReqFields)
{
 arrayRequiredFields = newReqFields;
}
function SetError(fieldName)
{
 $(fieldName).addClass(errorStyle);
}
function RemoveError(fieldName)
{
 $(fieldName).removeClass(errorStyle);
}
// Expects successIndex to be 0 (failed) or 1 (passed)
function SetStyles(fieldName, successIndex) {
 fieldName += "_label";

 if (successIndex != 0) {
  successIndex = 1;
  
 }
 if (successIndex == 0) {
  SetError(fieldName);
 }
}
function ValidateForm()
{
 var tempCheck = 1;
 for (var i=0; i<arrayRequiredFields.length; i++) {
  var temp_field = arrayRequiredFields[i];
  
  switch(temp_field) {
   case "email":
   case "cc_email":
    tempCheck *= ValidateEmail(temp_field);
    break;
   case "phone":
   case "cc_phone":
    tempCheck *= ValidatePhone(temp_field);
    break;
   case "agreement":
    tempCheck *= ValidateAgreement(temp_field);
    break;
   case "cc_type":
    tempCheck *= ValidateSelect(temp_field);
    break;
   case "cc_expy_month":
   case "cc_expy_year":
    tempCheck *= ValidateCCExpy(temp_field);
   default:
    tempCheck *= ValidateFieldForEmptiness(temp_field);
    break;
  }
 }
 return tempCheck;
}
function ValidateFieldForEmptiness(fieldName)
{
 var validateSuccess = 1;
 fieldName = "#" + fieldName;
 
 if ($(fieldName).val() == "") {
  MessageBox('successMessage','smAttention',stdMessage);
  validateSuccess = 0;
 }
 SetStyles(fieldName, validateSuccess)
 return validateSuccess;
}
function ValidateCCExpy(ccFieldName)
{
    var tempCCExpyCheck = 1;
    ccFieldName = "#" + ccFieldName;
    if ($(ccFieldName).val() == "") {
        tempCCExpyCheck = 0;
    }
    SetStyles("#cc_expy", tempCCExpyCheck);
    return tempCCExpyCheck;
}
function ValidateEmail(emailFieldName)
{
 var tempEmailCheck = 1;
 var emailRegExp = /^(.+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,6})$/;
 
 emailFieldName = "#" + emailFieldName;
 emailString = $(emailFieldName).val() + "";  // to force it to be a string
 if (!emailString.match(emailRegExp)) {
  tempEmailCheck = 0;
  MessageBox('successMessage', 'smAttention', stdMessage);
 }
 SetStyles(emailFieldName, tempEmailCheck);
 return tempEmailCheck;
}
function ValidatePhone(phoneFieldName)
{
 //Phone number Validation #2 - Check If numeric and has acceptable non-numerics (e.g. dash or dot)
 phoneFieldName = "#" + phoneFieldName;
  
 var tempPhoneCheck = 1;
 var phoneRegExp = /\d+/;
 phoneString = $(phoneFieldName).val() + "";  // to force it to be a string
 if (!phoneString.match(phoneRegExp)) {
  tempPhoneCheck = 0;
  MessageBox('successMessage', 'smAttention', stdMessage);
 }
 SetStyles(phoneFieldName, tempPhoneCheck);
 return tempPhoneCheck;
}
function ValidateSelect(selectFieldName)
{
    selectFieldName = "#" + selectFieldName;
    var tempSelectCheck = 1;
    if ($(selectFieldName).val() == "") {
        tempSelectCheck = 0;
    }
    SetStyles(selectFieldName + "1", tempSelectCheck);
    return tempSelectCheck;
}
function MessageBox(smBox,smClass,smText)
{
 document.getElementById(smBox).className = smClass;
 document.getElementById(smBox).innerHTML = smText;
 self.scrollTo(0,0);
}
function ValidateAgreement(agreementFieldName)
{
 var tempAgreementCheck = 1;
 agreementFieldName = "#" + agreementFieldName;
 if ($(agreementFieldName + ":checked").val() != "Y") {
  tempAgreementCheck = 0;
  MessageBox('successMessage', 'smAttention', stdMessage);
 }
 SetStyles(agreementFieldName, tempAgreementCheck);
 return tempAgreementCheck;
}