﻿var _Validators = new Array();


$(document).ready(function() {
    InitializeValidators();

    //DOM Updates after AJAX calls may orphan the inputs associated with Validators.
    // We need to reInitialize our validators after each request is completed.
    //Sys.WebForms.PageRequestManager.getInstance().add_endRequest(InitializeValidators);
});


// This function creates custom Validator objects from all DOM elements 
// marked with a "Validate[type]" Css class name.
function InitializeValidators() {
    // Lets start by releasing all the existing validators. This will only happen on
    // return from an AJAX call.  On the initial page load, _Validators will be empty.
    $.each(_Validators, function(item) { item = null });
    _Validators = new Array();

    //Ok, lets start making Validators!
    $('.ValidateAreaCodeOrRange').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9]{1,3}-?[0-9]{0,3}?$')); });
    $('.ValidateSicOrRange').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9]{1,6}-?[0-9]{0,6}?$')); });
    $('.ValidateZipOrRange').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9]{1,5}[-?[0-9]{0,5}]?$')); });
    $('.ValidateRoute').each(function(input, element) { _Validators.push(new Validator(element, '^[cCrRbBhH]{1}[0-9]{0,3}?$')); });
    $('.ValidateText').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9a-zA-Z _-]*$')); });
    $('.ValidateNumber').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9]*$')); });
    $('.ValidateZip4').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9]{0,4}?$')); });
    $('.ValidateDecimal').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9.]*$')); });
    $('.ValidatePhone').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9.()-]*$')); });
    $('.ValidateEmail').each(function(input, element) { _Validators.push(new Validator(element, '^[0-9a-zA-Z@._-]*$')); });
    $('.ValidateMultilineEmail').each(function(input, element) { _Validators.push(new Validator(element, '^[\\s0-9a-zA-Z@,._-]*$')); });
    $('.ValidateExtendedText').each(function(input, element) { _Validators.push(new Validator(element, '^[\\s0-9a-zA-Z .,;\[\\]:\'@#$%()&+?*_-]*$')); });
}

// CLASSES
function Validator(input, expression, options) {
    this.keyExpression = expression;
    this.blurExpression = expression;
    this.oldValue = input.value;
    this.errorColor = '#f33';
    this.inputId = input.id;

    // override default options if provided.
    if (options) {
        if (options.errorColor)
            this.errorColor = options.errorColor;

        if (options.blurExpression)
            this.blurExpression = options.blurExpression;
    }

    input.onblur = function() { return validate(this, 'blur') };
    input.onkeyup = function() { return validate(this, 'keyup') };
}

//validates the input for the control against the correct regular expression
function validate(obj, event) {

    var validator;
    //look up the validator
    for (var i = 0; i < _Validators.length; i++) {
        if (_Validators[i].inputId == obj.id) {
            validator = _Validators[i];
            break;
        }
    }

    //abort if could not look up validator
    if (validator == null)
        return;

    //grab the expression
    var expression = event == 'blur' ? validator.blurExpression : validator.keyExpression;
    var myRegX = new RegExp(expression);

    //test it, return if true
    if (obj.value.length == 0 || myRegX.test(obj.value)) {
        validator.oldValue = obj.value;
        return true;
    }

    //invalid input, highlight the error and restor previous value
    $(obj).effect("highlight", { color: '#F33000' }, 1000);
    obj.value = validator.oldValue;
    return false;
}