﻿// JScript File
var bAlert = true; //Uses the Browser Alert Box to Tell user about validation errors.
///    !!!DO NOT make any changes below this line!!!          ///
var lf = {}; //used as namespace to avoid conflicts
$(document).ready(function() {
    lf.bindEvents();
});
/* validation functions */
lf.valReq = function valReq(obj, errorMsg, onValidFunction) {
    var sVal = lf.getTrimmedValue(obj)
    var isValid = false;
    if (sVal == "") {
        lf.setError(obj, errorMsg);
    }else {
        isValid = true;
        lf.clearError(obj);
        if (onValidFunction != undefined) {
            onValidFunction(obj);
        }
    }
    return isValid;
}
lf.valReg = function valReg(obj, regExp, errorMsg, onValidFunction) {
    var sVal = lf.getTrimmedValue(obj);
    if (sVal != '') {
        if (sVal.match(regExp) == null) {
            lf.setError(obj, errorMsg);
        }else {
            lf.clearError(obj);
            if (onValidFunction != undefined) {
                onValidFunction(obj);
            }
        }
    }else {//not enought to validate, just clear error
        lf.clearError(obj);
    }
}
lf.valComp = function valComp(obj, compareID, errorMsg, onValidFunction) {
    var sVal = lf.getTrimmedValue(obj);
    var sCompVal = lf.getTrimmedValue($("#" + compareID));
  
    if (sVal != sCompVal) {
        lf.setError(obj, errorMsg);
    }else {
        lf.clearError(obj);
        if (onValidFunction != undefined) {
            onValidFunction(obj);
        }
    }
}
lf.setError = function setError(obj, errorMsg) {
    $(obj).attr('class', 'lf_error_control');
    $("#" + $(obj).attr('id') + "_img").attr('alt', errorMsg).attr('title', errorMsg).show();
}
lf.clearError = function clearError(obj) {
    $("#" + $(obj).attr('id') + "_img").hide();
    $(obj).removeAttr('class');
}
lf.submitForm = function submitForm() {
    lf.formValidate();
    if (lf.isFormValid() == true) {
        //simple moves
        $("#phone_home").val($("#phone_home1").val() + $("#phone_home2").val() + $("#phone_home3").val());
        $('#lf_app_form').submit();
    } else {
        if (bAlert){
            var sAlert = 'Please Correct the following:\n';
            $.each($('.lf_img_error:visible'), function(i, n) {
                if (i < 11){
                    sAlert += '\n' + $(n).attr('alt');
                }
                else{
                    sAlert += '\n\n' + 'We\'ve found too many errors...';
                    return false;
                }
            });
            alert(sAlert);
        }
    }
}
lf.formValidate = function formValidate() {
    $('#lf_app_form').find('input').blur();
    $('#lf_app_form').find('select').change();
    $('.lf_error_control:first').focus();
}
lf.isFormValid = function isFormValid() {
    return ($('.lf_error_control').length == 0);
}
lf.getTrimmedValue = function getTrimmedValue(obj) {
    var sVal = $(obj).val();
    if (sVal.length > 0) { //test first and last
        if ((sVal[0] == ' ') || (sVal[sVal.length - 1] == ' ')) { //replace
            sVal = trim(sVal);
            $(obj).val(sVal);//replace contents
        }
    }
    return sVal;
}
lf.trim = function trim(i_value ) {
    return $.trim(i_value);
}
lf.bindEvents = function bindEvents() {
    $('#email').blur(function() { lf.valReq(this, 'Email is required', function(obj) { lf.valReg(obj, '^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$', 'Email is invalid - Valid Format: xxxxxx@xxxxxx.xxx'); }); });
}

