// !!!!!!!!!!!!!
//
// array of fields we doValidate in javascript
//
// it is array of description arrays
// of the following structure:
// [0] = actual form field name
// [1] = regular expression
// [2] = error message
//
// to add new form field add the following line before
// the last element of this array
//
// new Array(FORM_FIELD_NAME, REGULAR_EXPRESSION, ERROR_MESSAGE),
var validator= new Array(
////////////// input name         regexp  error msg
    new Array("Contact_FirstName",/^[^\d]{1,25}$/mgi,
        new Array(
            "- First Name is required (1-25 characters)",
            "- First Name must contain at least 1 character",
            "- First Name may not exceed 25 characters"
            ),
            1,25
        ),
    new Array("Contact_LastName",/^[^\d]{1,30}$/mgi,
        new Array(
            "- Last Name is required (1-30 characters)",
            "- Last Name must contain at least 1 character",
            "- Last Name may not exceed 25 characters"
            ),
            1,30
        ),
    new Array("Account_Experience",/^[\d]+$/mgi,
        new Array("- Experience must be a number")
        ),
    new Array("Account_Address_PostalCode",/^[\d]{5}$/mgi,
        new Array("- Zip code must be a 5-digit number")
        ),
    new Array("Account_Address_StateProvName",/^[A-Za-z]{2}$/mgi,
        new Array("- State must be in abbreviated form (2 letters)")
        ),
    new Array("lmphone",/^\((\d){3}\)(\d){3}-(\d){4}$/mgi,
        new Array("- Phone number must be in the format (000)000-0000")
        ),
    new Array("Contact_EMail",/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/mgi,
        new Array("- E-mail address is required")
        )
);

function trim(sInString) {
  if(sInString!=null){
      sInString = sInString.replace( /^\s+/g, "" );// strip leading
      return sInString.replace( /\s+$/g, "" );// strip trailing
  } else {
    return "";
  }
}

function getValue(obj){
    // if it is radiobutton length would be >0, otherwise... umm...
    if(obj.type == 'radio' || (obj.type == null && obj.length != null)){ // dunno, but obj.type is undefined for radiobuttons
        for(var i=0;i<obj.length;i++){
            if(obj[i].checked){
                return trim(obj[i].value);
            }
        }
        return "";
    } else {
        obj.value = trim(obj.value);
        return obj.value;
    }
}


// !!!!!!!!!!!!
// validate given form against given validator
// and display alert box with all found errors
function doValidate(form, validator){
    var errors = "";
    for(var i=0; i<validator.length; i++){
      //alert("testing " + validator[i][0] + "("+ getValue(form[validator[i][0]]) +") against " + validator[i][1]);
        if(form[validator[i][0]]!= null) { // we have control with such name
            var exp = validator[i][1];
            // trim value
            var val = getValue(form[validator[i][0]]);
            if(val == null || !val.match(exp) ){
                errors = validator[i][2][0] + "\n" + errors
            } else {
                // min length check
                if(validator[i][3]!=null){
                    if(val.length < validator[i][3]){
                        errors = validator[i][2][1] + "\n" + errors;
                    }
                }
                // max length check
                if(validator[i][4]!=null){
                    if(val.length > validator[i][4]){
                        errors = validator[i][2][2] + "\n" + errors;
                    }
                }
            }
        }
    }
    
    if(errors.length > 0){
        alert("Please correct the following errors:\n\n"+errors);
        return false;
    } else {
        return true;
    }
}

var mailMask = new Array(null, "Example: sample@legalmatch.com", null, /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
function attachMailValidator(obj){
    attach(obj,mailMask);
}

var zipMask = new Array("55555","Postal code format: 55555", null);
function attachZipValidator(obj) {
    attach(obj,zipMask);
}

// !!!!!! javascript callback for onsubmit method
function submitForm(form){
    return doValidate(form,validator);
}

function setStatus(){ return false; }

function init(){
    attachPhoneValidator('lmphone');
    attachMailValidator('Contact_EMail');
    attachZipValidator('Account_Address_PostalCode');
}
