﻿//Examples of calling
//CallPageMethod("TestNoParams", [], AjaxSucceeded, AjaxFailed); //No parameters
//CallPageMethod("TestWithParams", ["a", "value", "b", 2], AjaxSucceeded, AjaxFailed); //With parameters
function CallPageMethod(fn, paramArray, successFn, errorFn) {
    var pagePath = window.location.pathname;
    //Create list of parameters in the form:
    //{"paramName1":"paramValue1","paramName2":"paramValue2"}
    var paramList = '';
    if (paramArray.length > 0) {
        for (var i = 0; i < paramArray.length; i += 2) {
            if (paramList.length > 0) paramList += ',';
            paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
        }
    }
    paramList = '{' + paramList + '}';
    //Call the page method
    jQuery.ajax({
        type: "POST",
        url: pagePath + "/" + fn,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: successFn,
        error: errorFn
    });
}

function showHide(layerid) {
    if (document.getElementById(layerid).style.visibility != "hidden") {
        document.getElementById(layerid).style.visibility = "hidden";
    }
    else {
        document.getElementById(layerid).style.visibility = "visible";
    }
}

// MM_showHideLayers('divProgressImage', '', 'hide');

function MM_showHideLayers() { //v9.0
    var i, p, v, obj, args = MM_showHideLayers.arguments;
    for (i = 0; i < (args.length - 2); i += 3)
        with (document) if (getElementById && ((obj = getElementById(args[i])) != null)) {
        v = args[i + 2];
        if (obj.style) { obj = obj.style; v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v; }
        obj.visibility = v;
    }
}

function MM_setTextOfLayer(objId, x, newText) { //v9.0
    with (document) if (getElementById && ((obj = getElementById(objId)) != null))
        with (obj) innerHTML = unescape(newText);
}

function IsNumeric(sText) {
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}

function removeSelectedOptions(selectBox) {
    for (var i = selectBox.options.length - 1; i >= 0; i--) {
        if (selectBox.options[i].selected)
            selectBox.remove(i);
    }

}

function addOption(selectbox, text, value) {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    //selectbox.options.add(optn);

    try {
        //elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
        selectbox.add(optn, null);
    }
    catch (ex) {
        selectbox.options.add(optn); // IE only
    }
}





//Email Validation ValidateEmailAddress
function ValidateEmailAddress(EmailAddress) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(EmailAddress) == false) {
        return false;
    }
    else {
        return true;
    }
}

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function ltrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}
function rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}

