﻿/*
Contains functions that override default asp.net validator functions for enhanced UI experience.
*/

$(document).ready(function() {
    $(".clbc-ButtonSearch").click(function() {
        $(".clbc-Loading").addClass("clbc-LoadingActive");
    });
});

ValidatorUpdateIsValid = function() {
    Page_IsValid = AllValidatorsValid(Page_Validators);
    ClearValidatorCallouts();
    SetValidatorCallouts();
}

ValidatorValidate = function(val, validationGroup, event) {
    val.isvalid = true;
    if ((typeof (val.enabled) == 'undefined' || val.enabled != false) && IsValidationGroupMatch(val, validationGroup)) {
        if (typeof (val.evaluationfunction) == 'function') {
            val.isvalid = val.evaluationfunction(val);
            //alert(val.id +' '+val.isvalid);
            if (!val.isvalid && Page_InvalidControlToBeFocused == null &&
                typeof (val.focusOnError) == 'string' && val.focusOnError == 't') {
                ValidatorSetFocus(val, event);
            }
        }
    }

    ClearValidatorCallouts();
    SetValidatorCallouts();
    //ValidatorUpdateDisplay(val);
}

SetValidatorCallouts = function() {
    var i;
    var pageValid = true;
    for (i = 0; i < Page_Validators.length; i++) {
        var inputControl = document.getElementById(Page_Validators[i].controltovalidate);
        if (!Page_Validators[i].isvalid) {
            if (pageValid) {
                if (inputControl != null) {
                    inputControl.focus();
                }
            }
            //WebForm_AppendToClassName(inputControl, 'error');
            pageValid = false;
            $(inputControl).parents('div.clbc-Form-Field:first').addClass('error');
//            if ($(inputControl).siblings('span').text().match('Required')) {
//                $(inputControl).parents('div.clbc-Form-Field:first').addClass('required');
//            }
            $(".clbc-Loading").removeClass("clbc-LoadingActive");
            //var fieldId = ($(inputControl).parent().attr("id")).replace("clbcField", "");
            //$('#clbcError' + fieldId).addClass('show');
            //$('.clbc-ErrorMessage').addClass('error');
        }
    }

    return pageValid;
}

ClearValidatorCallouts = function() {
    var i;
    var invalidConrols = [];
    for (i = 0; i < Page_Validators.length; i++) {
        var inputControl = document.getElementById(Page_Validators[i].controltovalidate);
        //WebForm_RemoveClassName(inputControl, 'error');
        if (inputControl != null) {//added by schweppes
            //var fieldId = ($(inputControl).parent().attr("id")).replace("clbcField", "");
            //$('#clbcError' + fieldId).removeClass('show');
            //if ($('.clbc-ErrorMessage .msg span').hasClass('show') == false) {
            //    $('.clbc-ErrorMessage').removeClass('error');
            //}
            $(inputControl).parents('div.clbc-Form-Field:first').removeClass('error');
            $(inputControl).parents('div.clbc-Form-Field:first').removeClass('required');
        }
    }
}

//ValidatorOnChange = function(event) {
//    if (!event) {
//        event = window.event;
//    }
//    
//    Page_InvalidControlToBeFocused = null;
//    var targetedControl;
//    if ((typeof (event.srcElement) != "undefined") && (event.srcElement != null)) {
//        targetedControl = event.srcElement;
//    }
//    else {
//        targetedControl = event.target;
//    }
//    var vals;

//    if (typeof (targetedControl.Validators) != "undefined") {
//        vals = targetedControl.Validators;
//    }
//    else {
//        if (targetedControl.tagName.toLowerCase() == "label") {
//            targetedControl = document.getElementById(targetedControl.htmlFor);
//            vals = targetedControl.Validators;
//        }
//    }
//    var i;

//    //if date picker caused it in IE...which can't find the textbox to get the list of validators for the control, so revalidate whole page
//    if (vals == null)
//        vals = Page_Validators;

//    for (i = 0; i < vals.length; i++) {
//        ValidatorValidate(vals[i], null, event);
//    }

//    ValidatorUpdateIsValid();
//}

//ValidatorUpdateDisplay = function(val) {
//    if (typeof (val.display) == "string") {
//        if (val.display == "None") {
//            return;
//        }
//        if (val.display == "Dynamic") {
//            //changed this block to not set display to inline, but to remove style attribute entirely
//            //undo comments if not using jQuery
//            if (val.isvalid)
//                $(val).hide("fast"); //val.style.display = "none"; 
//            else
//                $(val).show("fast"); //val.removeAttribute("style");
//            return;
//        }
//    }
//    if ((navigator.userAgent.indexOf("Mac") > -1) && (navigator.userAgent.indexOf("MSIE") > -1)) {
//        val.style.display = "inline";
//    }
//    val.style.visibility = val.isvalid ? "hidden" : "visible";
//}

// This is a client side validation function for the reCaptcha control, it will essentially
//      take the source (which is a span that is contained within the div we place all of our
//      form controls before validation), ensure that the contained input has at least something 
//      in it, and add the pre-defined validation error class accordingly
function RecaptchaCustomClientSideValidation(source, arguments)
{
    var $this = $(source);
    var $this_input = $('#recaptcha_response_field', $this.parent());
    var $outer_div = $this.parent();

    // Required field validation (server side control handles content based validation
    if (0 >= $this_input.val().length)
    {
        arguments.IsValid = false;
        $outer_div.addClass('error');
    }
    else
    {
        arguments.IsValid = true;
        $outer_div.removeClass('error');
    }

    return true;
}
