﻿
function actionAfterValidate(source, isValid) {
    if ($.validationEngine) {
        //add something for action
        if (isValid) {
            //ChangeLabelColor(source, "");
            ShowValidationEngineBubble(source, false);
        } else {
            //ChangeLabelColor(source, "red");
            ShowValidationEngineBubble(source, true);
        }
    }

    if (typeof (formValidator_CallBack) == 'function') {
        formValidator_CallBack(source, isValid);
    }
}

/*Show validation engine buble*/
function ShowValidationEngineBubble(source, isShow) {
    if (isShow) {
        $.validationEngine.buildPrompt("#" + source.controltovalidate, source.errormessage, "error");
        //clear if they clicked
        $(".formError").live("click", function () {	 // REMOVE BOX ON CLICK
            $(this).fadeOut(150, function () { $(this).remove() })
        })

        source.display = 'none'; //hide source

        //scroll
        if (true) {
            
            var destination = $(".formError:not('.greenPopup'):first").offset().top;
            $(".formError:not('.greenPopup')").each(function () {
                testDestination = $(this).offset().top;
                if (destination > testDestination) destination = $(this).offset().top;
            })
            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination }, 1100);
        }

    }
    else {
        $.validationEngine.closePrompt("#" + source.controltovalidate)
    }
}


//Remove all validation error - call this anytime postback in update panel, so when you hide the it won't show
function removeAllValidationError()
{
    $(".formError").remove();
}


function ChangeLabelColor(source, color) {

    var labels = document.getElementsByTagName('label');
    for (i = 0; i < labels.length; i++) {
        if (labels[i].htmlFor == source.controlToValidate) {
            labels[i].style.color = color;
        }
    }
}

function Trim(s) {
    if (s) {
        var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
        return (m == null) ? "" : m[1];
    }
    else {
        return "";
    }
}

function RequiredField(source, arguments) {
    if (Trim(document.getElementById(source.controltovalidate).value) != "") {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
    actionAfterValidate(source, arguments.IsValid);
}


function ValidateMatch(source, arguments, compareValue) {

    if (Trim(document.getElementById(source.controltovalidate).value) == compareValue) {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
    actionAfterValidate(source, arguments.IsValid);
}

function ValidateCheckboxChecked(source, arguments, val) {
    
    if (val && val != "") {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
    
    //$.log(arguments.IsValid);
    actionAfterValidate(source, arguments.IsValid);
}

function ValidateDropDown(source, arguments) {
    if (Trim(document.getElementById(source.controltovalidate).value) != "") {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
    actionAfterValidate(source, arguments.IsValid);
}

function ValidateEmail(source, arguments) {
    if (Trim(document.getElementById(source.controltovalidate).value) != "") {
        var emailRegex = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        var results = document.getElementById(source.controltovalidate).value.match(emailRegex);

        if (results != null) {
            arguments.IsValid = true;
        }
        else {
            arguments.IsValid = false;
        }
    }
    else {
        arguments.IsValid = false;
    }
    actionAfterValidate(source, arguments.IsValid);
}



function ValidateDocFile(source, arguments) {

    if (Trim(document.getElementById(source.controltovalidate).value) != "") {
        var docFileRegex = /.(doc|Doc|DOC|docx|Docx|DOCX|pdf|Pdf|PDF)$/;
        //alert(document.getElementById(source.controltovalidate).value);
        var results = document.getElementById(source.controltovalidate).value.match(docFileRegex);

        if (results != null) {
            arguments.IsValid = true;
        }
        else {
            arguments.IsValid = false;
        }
    }
    else {
        arguments.IsValid = false;
    }
    actionAfterValidate(source, arguments.IsValid);
}

