﻿function $$(elementId) {
    return document.getElementById(elementId);
}
function RemoveFromArray(ar, from, to) {
    var rest = ar.slice((to || from) + 1 || ar.length);
    ar.length = from < 0 ? ar.length + from : from;
    return ar.push.apply(ar, rest);
}

function blockUI(element) {
    var obj = $(element);
    var offset = obj.offset();
    var iX = Math.floor((obj.width() - 35) / 2) + offset.left;
    var iY = Math.floor((obj.height() - 35) / 2) + offset.top;

    var ovrl = $('#ShowAnimation');
    ovrl.css({ top: iY, left: iX });
    ovrl.show();
}

function unblockUI() {
    $('#ShowAnimation').hide();
}
function EditControl(sUrl) {
    window.open('/Admin/EditControl.aspx?' + sUrl, 'editor', "");
}
function clearForm(form) {
    // iterate over all of the inputs for the form
    // element that was passed in
    $('input', form).each(function() {
        var type = this.type;
        // it's ok to reset the value attr of text inputs,
        // password inputs, and textareas
        if ((type == 'text') || (type == 'password')) {
            this.value = "";
        }
        // checkboxes and radios need to have their checked state cleared
        // but should *not* have their 'value' changed
        else if (type == 'checkbox' || type == 'radio') {
            this.checked = false;
        }
    });
    $('textarea', form).each(function() {
        this.value = "";
    });
    $('select', form).each(function() {
        var index = this.selectedIndex;
    });
}

function serializeForm(form) {
    var sData = "";
    $('input', form).each(function() {
        var type = this.type;

        // it's ok to reset the value attr of text inputs,
        // password inputs, and textareas
        if (type == 'text' || type == 'password' || type == 'hidden') {
            sData += "&" + this.name + "=" + escape(this.value);
        }
        else if (type == 'checkbox' || type == 'radio') {
            if (this.checked) {
                sData += "&" + this.name + "=" + escape(this.value);
            }
        }
    });
    $('textarea', form).each(function() {
        sData += "&" + this.name + "=" + escape(this.value);
    });
    $('select', form).each(function() {
        var index = this.selectedIndex;
        if (index >= 0) {
            ops = this.options;
            for (var i = 0; i < ops.length; i++) {
                var op = ops[i];
                if (op.selected) {
                    // extra pain for IE...
                    var v = $.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value;
                    sData += "&" + this.name + "=" + escape(v);
                }
            }
        }
    });
    return sData;
}

function populateData(form, values) {
    $('input', form).each(function() {
        var type = this.type;

        // it's ok to reset the value attr of text inputs,
        // password inputs, and textareas
        if (type == 'text' || type == 'password' || type == 'hidden') {
            sData = values[this.name];
            if (typeof (sData) != 'undefined') {
                this.value = sData;
            }
        }
        else if (type == 'checkbox') {
            sData = values[this.name];
            if (sData == this.value)
                this.checked = true;
            else
                this.checked = false;
        }
        else if (type == 'radio') {
            sData = values[this.name];
            if (sData == this.value)
                this.checked = true;
            else
                this.checked = false;
        }
    });
    $('textarea', form).each(function() {
        sData = values[this.name];
        if (typeof (sData) == 'undefined')
            sData = '';
        this.value = sData;
    });
    $('select', form).each(function() {
        sData = values[this.name];
        if (typeof (sData) == 'undefined')
            sData = '';
        $(this).val(sData);
    });

}

function ValidateRequired(value, options) {
    if (value == null)
        return false;
    if (value.length == 0)
        return false;
    return true;
}
function ValidateNumber(value) {
    if (value == null)
        return true;
    if (value.length == 0)
        return true;
    return /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
}

function GetStandardValidatefunction(sValidateType) {
    switch (sValidateType) {
        case 'required':
            return ValidateRequired;
        case 'number':
            return ValidateNumber;
    }
}
function StandardHighlightFailAction($obj, rule) {
    $obj.addClass('error');
    alert(rule.failMessage);
}
function StandardHighlightSuccessAction($obj, rule) {
    $obj.removeClass('error');
}

function GetStandardFailFunction(sFailAction) {
    if (sFailAction == null) {
        return StandardHighlightFailAction;
    }
    switch (sFailAction) {
        case 'highlight':
            return StandardHighlightFailAction;
    }
}

function GetStandardSuccessFunction(sSuccessAction) {
    if (sSuccessAction == null) {
        return StandardHighlightSuccessAction;
    }
    switch (sFailAction) {
        case 'highlight':
            return StandardHighlightSuccessAction;
    }
}

/*
rules{
Id:firstname,
rule:{
failMessage:'element is not valid',
failAction:'highlight',
validateFunction:func,
validateType:'number',
validationOptions:{min:2, max:5}
}
}

*/
function ValidateForm(rules) {
    var bResult = true;
    if (rules == null)
        return true;
    if (rules.length == 0)
        return true;

    var failAction, successAction;
    failAction = rules.failAction;
    if ((failAction == null) || (typeof (failAction) === "string"))
        failAction = GetStandardFailFunction(failAction);

    successAction = rules.successAction;
    if ((successAction == null) || (typeof (successAction) === "string"))
        successAction = GetStandardSuccessFunction(successAction);

    var i, j;
    if (rules.ruleSet.length == null) {
        bResult = bResult & ValidateRuleSet(rulse.ruleSet, failAction, successAction);
    }
    else {
        for (i = 0; i < rules.ruleSet.length; i++) {
            bResult = bResult & ValidateRuleSet(rules.ruleSet[i], failAction, successAction);
        }
    }
    return bResult;
}

function ValidateRuleSet(ruleSet, failAction, successAction) {
    var bResult = true;
    var $obj = $(ruleSet.Id);
    var sValue = $obj.val();
    if (ruleSet.rules.length == 0) {
        bResult = ValidateRule(sValue, $obj, ruleSet.rules, failAction, successAction);
    }
    else {
        for (j = 0; j < ruleSet.rules.length; j++) {
            var rule = ruleSet.rules[j];
            bResult = bResult & ValidateRule(sValue, $obj, rule, failAction, successAction);
        }
    }
    return bResult;
}

function ValidateRule(sValue, $obj, rule, failAction, successAction) {
    var validateFunction;
    validateFunction = rule.validateFunction;
    if (validateFunction == null)
        validateFunction = GetStandardValidatefunction(rule.validateType);
    if (!validateFunction(sValue, rule.validateOptions, $obj)) {
        failAction($obj, rule);
        return false;
    }
    successAction($obj, rule);
    return true;
}


function MakeActiveTab(iIndex) {
    for (i = 0; i <= 10; i++) {
        var objTab = document.getElementById("tab" + i);
        if (objTab == null)
            continue;
        var objTabPanel = document.getElementById("tb" + i);
        if (i == iIndex) {
            objTab.className = 'selectedtab';
            objTabPanel.style.display = 'block';
        }
        else {
            objTab.className = 'tabheader';
            objTabPanel.style.display = 'none';
        }

    }
}
