﻿/******* VALIDATION START *******/
function DEValidator() {
    // Main static methods to use:
    // DEValidator.addFormError(controlId, message)
    // DEValidator.removeFormError()
}

DEValidator.addFormError = function(controlId, message, isTelerikInput) {
    /// Add error style to the control
    //(isTelerikInput parameter is not used anymore)
    var controlInput = '#' + controlId;
    var controlWrapper = '#' + controlId;

     // special handling for radinput
    /*if (isTelerikInput) {
    controlInput += '_text';
    controlWrapper += '_wrapper';
    var tb = $find(controlId);
    tb.get_styles().EnabledStyle[1] += " DE_error_field";
    tb.get_styles().HoveredStyle[1] += " DE_error_field";
    tb.get_styles().FocusedStyle[1] += " DE_error_field";
    tb.updateCssClass();
    //// Can be done with Telerik's client-side error model
    //tb._invalid = true;   
    //tb.updateCssClass(); 
    } else {
    jQuery(controlInput).addClass("DE_error_field");
    }*/

    var telerikInput = jQuery(controlInput).parents('.RadInput:first').attr('id'); //check if this is radinput
    if (telerikInput && telerikInput.indexOf('_wrapper') > 0) {
        controlWrapper += '_wrapper';
    }
    var errorWrapperId = jQuery(controlWrapper).parent().attr("id");
    if (errorWrapperId.indexOf("_DE_error_wrapper") > -1) {
        jQuery(controlWrapper).parent().removeClass("DE_error_field").addClass("DE_error_field");
        controlWrapper = '#' + errorWrapperId;
    } else {
        jQuery(controlWrapper).wrap("<span class='DE_error_field' id='" + controlId + "_DE_error_wrapper'></span>");
        controlWrapper = '#' + controlId + '_DE_error_wrapper';
    }

    jQuery(controlWrapper + ' + .DE_error_icon').remove();
    jQuery(controlWrapper).after('<img src="/Images/ValidationError.gif"  title="' + message + '" class="DE_error_icon" />');
}

DEValidator.removeFormError = function(inputId) {
    /// Remove error style from the control
    var controlId = inputId;
    var telerikInput = jQuery('#' + inputId).parents('.RadInput:first').attr('id');
    var telerikCombo = jQuery('#' + inputId).parents('.RadComboBox:first').attr('id');
    if (telerikInput && telerikInput.indexOf('_wrapper') > 0) {
        controlId = telerikInput;
    } else if (telerikCombo) {
        controlId = telerikCombo;
    }
    //jQuery('#' + controlId).parent('.DE_error_field').replaceWith(jQuery('#' + controlId));
    jQuery('#' + controlId).parent().removeClass("DE_error_field").next('.DE_error_icon').remove();
}

DEValidator.removeFormErrors = function() {
    /// Removes all error styling on a page (although Telerik controls may require additional handling)
    jQuery('.DE_error_icon').remove();
    jQuery('.DE_error_field').removeClass("DE_error_field");
}

DEValidator.removeErrorsOnBlur = function() {
    /// Removes error styling "onblur" for the controls where css class set to "DEValidate"
    jQuery('input:text.DEValidate').add('.DEValidate input:text').blur(function() { DEValidator.removeFormError(this.id) });
}

function DEValueValidator(valueToValidate) {
    /// DEValueValidator object contains a set of validation methods (see attached prototypes)
    this.value = valueToValidate;
}

DEValueValidator.prototype.checkRequired = function() {
    /// Validates if value is present
    if (trim('' + this.value).length == 0) return false; else return true;
}

DEValueValidator.checkEmail = function(EmailAddress) {
    /// Validates if value looks like email
    //var reg = new RegExp("^([a-zA-Z0-9])+((\\.|_|-)[a-zA-Z0-9_-]+)*@([a-zA-Z0-9])+(\\.[a-zA-Z0-9]+)+$");
    var reg = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return reg.test(EmailAddress);
}

DEValueValidator.checkPhoneNumber = function(PhoneNumber) {
    /// Validates if value looks like email
    var reg = new RegExp(/^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/);
    return reg.test(PhoneNumber);
}

DEValueValidator.checkUrl = function(WebSite) {
    var str = WebSite.toLowerCase().replace("http://", "").replace("https://", "");
    var reg = /^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|ca|edu)$/;
    return reg.test(str);
}

DEValueValidator.checkCanadianPostalCode = function (PostalCode) {
    var reg = /^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/;
    return reg.test(PostalCode.toUpperCase());
}

DEValueValidator.checkUSZip = function(ZipCode) {
    var reg = /^\d{5}(-\d{4})?$/;
    return reg.test(ZipCode);
}

DEValueValidator.checkCreditCard = function() {
    var ccnum = this.value;
    //http://www.breakingpar.com
    // Remove all dashes for the checksum checks to eliminate negative numbers
    ccnum = ccnum.split("-").join("");
    if (ccnum.length > 16 || ccnum.length < 14) return false;
    // Checksum ("Mod 10")
    // Add even digits in even length strings or odd digits in odd length strings.
    var checksum = 0;
    for (var i = (2 - (ccnum.length % 2)); i <= ccnum.length; i += 2) {
        checksum += parseInt(ccnum.charAt(i - 1));
    }
    // Analyze odd digits in even length strings or even digits in odd length strings.
    for (var i = (ccnum.length % 2) + 1; i < ccnum.length; i += 2) {
        var digit = parseInt(ccnum.charAt(i - 1)) * 2;
        if (digit < 10) { checksum += digit; } else { checksum += (digit - 9); }
    }
    if ((checksum % 10) == 0) return true; else return false;
}

DEValidator.addFormErrorMsg = function (controlId, message, isTelerikInput) {
    /// Add error style to the control
    //(isTelerikInput parameter is not used anymore)
    var controlInput = '#' + controlId;
    var controlWrapper = '#' + controlId;

    var telerikInput = jQuery(controlInput).parents('.RadInput:first').attr('id'); //check if this is radinput
    if (telerikInput && telerikInput.indexOf('_wrapper') > 0) {
        controlWrapper += '_wrapper';
    }
    var errorWrapperId = jQuery(controlWrapper).parent().attr("id");
    if (errorWrapperId.indexOf("_DE_error_wrapper") > -1) {
        jQuery(controlWrapper).parent().removeClass("DE_error_field").addClass("DE_error_field");
        controlWrapper = '#' + errorWrapperId;
    } else {
        jQuery(controlWrapper).wrap("<span class='DE_error_field' id='" + controlId + "_DE_error_wrapper'></span>");
        controlWrapper = '#' + controlId + '_DE_error_wrapper';
    }

    jQuery(controlWrapper + ' + .DE_error_icon').remove();
    jQuery(controlWrapper + ' + .DE_error_msg').remove();
    jQuery(controlWrapper).after('<img src="/Images/ValidationError.gif"  title="' + message + '" class="DE_error_icon" />' + '<span class="DE_error_msg">' + message + '</span>');
}

DEValidator.removeFormErrorMsg = function (inputId) {
    /// Remove error style from the control
    var controlId = inputId;
    var telerikInput = jQuery('#' + inputId).parents('.RadInput:first').attr('id');
    var telerikCombo = jQuery('#' + inputId).parents('.RadComboBox:first').attr('id');
    if (telerikInput && telerikInput.indexOf('_wrapper') > 0) {
        controlId = telerikInput;
    } else if (telerikCombo) {
        controlId = telerikCombo;
    }
    jQuery('#' + controlId).parent().removeClass("DE_error_field").next('.DE_error_icon').remove();
    jQuery('#' + controlId).parent().removeClass("DE_error_field").next('.DE_error_msg').remove();
}



/******* VALIDATION END *******/


