﻿var ParseResponse=function(ajaxContext){var contentType=ajaxContext.get_response().getResponseHeader('Content-Type');if((contentType)&&(contentType.indexOf('application/x-javascript')===-1)){EvalScripts(jQuery(ajaxContext.get_updateTarget()));}}
var EvalScripts=function(elem){$('script',elem).each(function(){if(!$(this).attr('src')||$(this).attr('src').length==0){jQuery.globalEval($(this).html());$(this).remove();}});}

var AlertPopup = function (text, title, buttons) {
    var alertPopup = $('#AlertPopup');
    
    if (alertPopup.length == 0 && window.parent != null) {
        if (window.parent) {
            window.parent.AlertPopup(text, title, buttons);
        }
        return;
    }

    alertPopup.children().html(text);
    if (title != undefined && title.length > 0) {
        alertPopup.dialog('option', 'title', title);
    }
    if (buttons != null) {
        alertPopup.dialog('option', 'buttons', buttons);
    }
    else {
        alertPopup.dialog('option', 'buttons', { "OK": function (event) { $(this).dialog('close'); } });
    }
    alertPopup.dialog('open');
}

var CloseAlertPopup = function() {
    var alertPopup = $('#AlertPopup');

    if (alertPopup.length == 0 && window.parent != null) {
        if (window.parent) {
            window.parent.CloseAlertPopup();
        }
        return;
    }

    alertPopup.dialog('close');
}

function IsNumeric(value) {
    var number = parseFloat(value); return !isNaN(number);
}

String.prototype.startsWith = function (str) {
    return (this.match("^" + str) == str)
}

function validate(formId, customValidator) {
    var isValid = true; var message = '<p><strong>Please fix the following fields:</strong></p><p>'; $(formId + ' input, ' + formId + ' select, ' + formId + ' textarea').each(function (index) {
        if ($(this).hasClass('required')) { if ($(this).val().length == 0) { message += '&middot; ' + $(this).attr('dname') + ' is required.<br />'; $(this).addClass('invalid'); $(this).one('focus', function () { $(this).removeClass('invalid'); }); isValid = false; return true; } }
        if (($(this).hasClass('numeric') || $(this).hasClass('decimal')) && $(this).val().length > 0) { if (isNaN(parseFloat($(this).val()))) { message += '&middot; ' + $(this).attr('dname') + ' must be a valid number.<br />'; $(this).addClass('invalid'); $(this).one('focus', function () { $(this).removeClass('invalid'); }); isValid = false; return true; } }
        if ($(this).hasClass('date') && $(this).val().length > 0) { if (!IsValidDate($(this).val())) { message += '&middot; ' + $(this).attr('dname') + ' must be a valid date.<br />'; $(this).addClass('invalid'); $(this).one('focus', function () { $(this).removeClass('invalid'); }); isValid = false; return true; } }
        if ($(this).hasClass('time') && $(this).val().length > 0) { if (!IsValidTime($(this).val())) { message += '&middot; ' + $(this).attr('dname') + ' must be a valid time.<br />'; $(this).addClass('invalid'); $(this).one('focus', function () { $(this).removeClass('invalid'); }); isValid = false; return true; } } 
    }); if (customValidator != null) { var customMessage = customValidator(formId); if (customMessage.length > 0) { isValid = false; message += customMessage; } }
    message += '</p>'; if (!isValid)
        AlertPopup(message, 'Error'); return isValid;
}

function ValidateForm(sender) {
    var formElements = document.forms[0].elements; var errorMessages = ""; var i = 0; for (i = 0; i < formElements.length; i++) {
        var elem = formElements[i]; if (elem.getAttribute("Validate")) {
            if (elem.getAttribute("Validate").toLowerCase() == "true") {
                var senderGroup = ""; var elemGroup = ""; if (sender.getAttribute("ElementGroup"))
                    senderGroup = sender.getAttribute("ElementGroup"); if (elem.getAttribute("ElementGroup"))
                    elemGroup = elem.getAttribute("ElementGroup"); if (senderGroup == elemGroup) {
                    var retVal = ValidateElement(elem); if (retVal.length > 0)
                        errorMessages += CRLF + "- " + retVal;
                } 
            } 
        } 
    }
    if (errorMessages.length > 0)
    { alert(ERR_MSG_HEADER + errorMessages); return false; }
    else
    { return true; }
}

function ValidateElement(elem) {
    var tagName = elem.tagName.toLowerCase(); var isValid = true; RestoreValidControl(elem); if (elem.getAttribute("Required")) {
        if (elem.getAttribute("Required").toLowerCase() == "true") {
            if (tagName == "select") {
                if (elem.selectedIndex < 0)
                    isValid = false; else if (elem.options[elem.selectedIndex].value.length == 0)
                    isValid = false;
            }
            else {
                if (Trim(elem.value).length == 0)
                    isValid = false;
            }
            if (!isValid) {
                HighlightInvalidControl(elem); var fieldName = elem.getAttribute("FriendlyName"); if (fieldName == null)
                    fieldName = ""; if (Trim(fieldName).length == 0)
                    fieldName = "required"; return "The " + QUOT + fieldName + QUOT + " field is required.";
            } 
        } 
    }
    return "";
}

function IsValidDate(value) {
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/; return (value.match(RegExPattern) && value.length > 0)
}

function IsValidTime(value) {
    var hasMeridian = false; var RegExPattern = /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/; if (!RegExPattern.test(value))
    { return false; }
    if (value.toLowerCase().indexOf("p") != -1) { hasMeridian = true; }
    if (value.toLowerCase().indexOf("a") != -1) { hasMeridian = true; }
    var values = value.split(":"); if ((parseFloat(values[0]) < 0) || (parseFloat(values[0]) > 23)) { return false; }
    if (hasMeridian)
    { if ((parseFloat(values[0]) < 1) || (parseFloat(values[0]) > 12)) { return false; } }
    if ((parseFloat(values[1]) < 0) || (parseFloat(values[1]) > 59)) { return false; }
    if (values.length > 2)
    { if ((parseFloat(values[2]) < 0) || (parseFloat(values[2]) > 59)) { return false; } }
    return true;
}

var ValidateInteger = function (element) {
    var isValid = /^-?\d+$/.test(element.value);

    if (isValid) {
        isValid &= parseFloat(element.value) > 0;
    }

    if (!isValid) {
        alert('Please enter a valid number greater than zero');
        if (element.getAttribute('oldvalue')) {
            element.value = element.getAttribute('oldvalue');
        }
        return false;
    }

    return true;
}
