﻿function GAjax() {
    this.successFunctions = new Array();
    this.globalUrl = "";
}
GAjax.prototype.registerSuccessFunction = function(op, url, func, blockElement, failfunc) {
    var obj = { url: url, operation: op.toLowerCase(), func: func, failfunc: failfunc, blockElement: blockElement };
    this.successFunctions.push(obj);
}

GAjax.prototype.findOperation = function(sOperation) {
    var i = 0;
    sOperation = sOperation.toLowerCase();
    for (i = 0; i < this.successFunctions.length; i++) {
        if (this.successFunctions[i].operation == sOperation)
            return i;
    }
}

GAjax.prototype.doFormAjax = function(sOperation, data, param) {
    var iIndex = this.findOperation(sOperation);
    blockUI(this.successFunctions[iIndex].blockElement);
    //convert param to JSON string
    if (typeof (param) == 'undefined')
        param = {};
    var o = "";
    for (sProperty in param) {
        o += sProperty;
        o += ":'";
        o += param[sProperty];
        o += "',";
    }

    o += "operation:" + iIndex;
    $$('resultback').value = o;
    document.frm1.action = this.globalUrl + this.successFunctions[iIndex].url;
    document.frm1.submit();
}

GAjax.prototype.doAjax = function(sOperation, data, param) {
    var iIndex = this.findOperation(sOperation);
    var $this = this;
    if (this.successFunctions[iIndex].blockElement != null)
        blockUI(this.successFunctions[iIndex].blockElement);

    if (typeof (param) == 'undefined')
        param = {};
    if (typeof (data) == 'undefined')
        data = '{}';
    param.operation = iIndex;
    $.ajax({
        type: 'POST',
        url: this.globalUrl + this.successFunctions[iIndex].url,
        data: data,
        dataType: 'json',
        success: function(result) {
            gAjax.callSuccessFunction(result, param);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            if ($this.successFunctions[iIndex].blockElement != null)
                unblockUI();
        }
    });
}

GAjax.prototype.callSuccessFunction = function(result, o) {
    var iIndex = o.operation;
    if (result.d.ErrorCode != 0) {
        var sErrMsg = result.d.ErrorMessage.replace(new RegExp('<br>', 'g'), "\n"); ;
        alert(sErrMsg + "\r\n" + result.d.InternalErrorMessage);
        if (this.successFunctions[iIndex].failfunc != null)
            this.successFunctions[iIndex].failfunc();
        if (this.successFunctions[iIndex].blockElement != null)
            unblockUI();
        if (result.d.ErrorCode == 3) {
            window.location.href = "/admin/login.aspx";
        }
        return;
    }
    var iIndex = o.operation;
    var successFunction = this.successFunctions[iIndex].func;
    if (typeof (successFunction) != 'undefined')
        successFunction(result, o);
    if (this.successFunctions[iIndex].blockElement != null)
        unblockUI();
}

var gAjax = new GAjax();

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 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 serializeFormToObject(form, o) {
    var obj = o;
    $('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') {
            obj[this.name] = this.value;
        }
        else if (type == 'checkbox' || type == 'radio') {
            if (this.checked) {
                obj[this.name] = this.value;
            }
        }
    });
    $('textarea', form).each(function() {
        obj[this.name] = 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;
                    obj[this.name] = v;
                }
            }
        }
    });
}
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);
    });

}

