﻿// myregistry_ajax.js and jquery.js must be on the page
var MrRegistryObject = {
    "MrModalPanel": function (innerHTML, CssClass, id, PanelWidth) {
        var pageToCall = "/WebHandlers/GenericHandler.ashx";
        var params = "?param=1&HandlerType=GetMyRegistyModalPanel";
        params += "&innerHTML=" + encodeURIComponent(innerHTML);
        params += "&panelId=" + encodeURIComponent(id);
        params += "&CssClass=" + encodeURIComponent(CssClass);
        params += "&windowWidth=" + encodeURIComponent(MrRegistryObject.GetWindowWidth());
        params += "&PanelWidth=" + encodeURIComponent(PanelWidth);
        MyRegistry_CallAjaxFunction_Post(pageToCall, params, function () {
            if (responseObject.readyState == 4) {
                var result = responseObject.responseText;
                var divMrModalPanelContainer = document.getElementById("divMrModalPanelContainer");
                if (divMrModalPanelContainer) { }
                else {
                    divMrModalPanelContainer = document.createElement("div");
                    divMrModalPanelContainer.id = "divMrModalPanelContainer";
                }
                divMrModalPanelContainer.innerHTML = result;
                document.body.appendChild(divMrModalPanelContainer);
            }
        });
    },
    "MouseSelectionFunctionHolder_onselectstart": null,
    "MouseSelectionFunctionHolder_onmousedown": null,
    "DisableMouseSelection": function () {
        // Internet Explorer:
        if (document.all) {
            MrRegistryObject.MouseSelectionFunctionHolder_onselectstart = document.onselectstart;
            document.onselectstart = function () { return false; }
        }
        // Netscape 4:
        if (document.layers) {
            document.captureEvents(Event.MOUSEDOWN);
            document.onmousedown = function (evt) { return false; }
        }
        // Netscape 6:
        MrRegistryObject.MouseSelectionFunctionHolder_onmousedown = document.onmousedown;
        document.onmousedown = function () { return false; };
    },
    "EnableMouseSelection": function () {
        document.onselectstart = MrRegistryObject.MouseSelectionFunctionHolder_onselectstart;
        document.onmousedown = MrRegistryObject.MouseSelectionFunctionHolder_onmousedown;
    },
    "ReplaceAllString": function (textParam, stringToSearch, stringToReplace) {
        while (textParam.indexOf(stringToSearch) > -1) {
            textParam = textParam.replace(stringToSearch, stringToReplace);
        }
        return textParam;
    },
    "GetInputPressedKeyCode": function (e, elementParam) {
        // onkeypress="return Password_onkeypress(event, this);"
        var unicode = e.keyCode ? e.keyCode : e.charCode;
        return unicode;
    },
    "getMouseXY": function (e) {
        var tempX = 0;
        var tempY = 0;
        if (!e) var e = window.event;
        if (e.pageX || e.pageY) {
            tempX = e.pageX;
            tempY = e.pageY;
        }
        else if (e.clientX || e.clientY) {
            tempX = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
            tempY = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
        }
        // tempX and tempY contain the mouse position relative to the document
        // Do something with this information
        if (document.getElementById("hiddenFieldMouseX")) {
            document.getElementById("hiddenFieldMouseX").value = tempX;
            document.getElementById("hiddenFieldMouseY").value = tempY;
        }
        else {
            var hiddenFieldMouseX = document.createElement("input");
            hiddenFieldMouseX.id = "hiddenFieldMouseX";
            hiddenFieldMouseX.type = "hidden";
            hiddenFieldMouseX.value = tempX;
            var hiddenFieldMouseY = document.createElement("input");
            hiddenFieldMouseY.id = "hiddenFieldMouseY";
            hiddenFieldMouseY.type = "hidden";
            hiddenFieldMouseY.value = tempX;
            document.body.appendChild(hiddenFieldMouseX);
            document.body.appendChild(hiddenFieldMouseY);
        }
        return true;
    },
    "GetElementPositionLeft": function (obj) {
        var curleft = curtop = 0;
        if (obj.offsetParent) {
            curleft = obj.offsetLeft
            curtop = obj.offsetTop
            while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
            }
        }
        return curleft;
    },
    "Trim": function (stringToTrim) {
        return stringToTrim.replace(/^\s+|\s+$/g, "");
    },
    "GetElementPositionTop": function (obj) {
        var curleft = curtop = 0;
        if (obj.offsetParent) {
            curleft = obj.offsetLeft
            curtop = obj.offsetTop
            while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
            }
        }
        return curtop;
    },
    "GetParentElementByAttributeName": function (elementParam, attributeName, attributeValue) {
        var tempParentNode = elementParam.parentNode;
        while (tempParentNode != null) {
            if (tempParentNode.nodeName == "#document") {
                return null;
            }
            else if (tempParentNode.getAttribute(attributeName) != null) {
                if (tempParentNode.getAttribute(attributeName) == attributeValue) {
                    return tempParentNode;
                }
            }
            tempParentNode = tempParentNode.parentNode;
        }
        return null;
    },
    "GetMrRandomId": function () {
        return "mrrandomid_" + (Math.floor(Math.random() * 110000)).toString();
    },
    "GetFormSubmitParameters": function (formId) {
        var str = '';
        var elem = document.getElementById(formId).elements;
        for (var i = 0; i < elem.length; i++) {
            if (elem[i].value.length > 0) {
                // it means radio button is not checked
                if (elem[i].type == "radio" && elem[i].value == "on") {
                    continue;
                }
                str += elem[i].name + "=" + encodeURIComponent(elem[i].value) + "&";
            }
        }
        return str;
    },
    "RemoveAllChildNodes": function (parentElement) {
        if (parentElement.hasChildNodes()) {
            while (parentElement.childNodes.length >= 1) {
                parentElement.removeChild(parentElement.firstChild);
            }
        }
    },
    "MultipleAjaxFunctionInterval": null,
    "CallMultipLeAjaxFunctions": function (FunctionArray) {
        // first added first execute
        FunctionArray.reverse();
        var functionInterval = setInterval(function () {
            if (FunctionArray.length == 0) {
                clearInterval(functionInterval);
            }
            else {
                if (MrRegistryObject.MultipleAjaxFunctionInterval == null) {
                    MrRegistryObject.MultipleAjaxFunctionInterval = true;
                    var functionToCall = FunctionArray.pop();
                    eval(functionToCall);
                }
            }
        }, 500);
    },
    "IsValidDateString": function (sDate) {
        var scratch = new Date(sDate);
        if (scratch.toString() == "NaN" || scratch.toString() == "Invalid Date") {
            return false;
        }
        else {
            return true;
        }
    },
    "SplitStringByString": function (textToSearch, stringSplit) {
        var resultArray = new Array();
        var indexOf = textToSearch.indexOf(stringSplit);
        while (indexOf > -1) {
            var stringPart = textToSearch.substr(0, indexOf);
            if (stringPart.length > 0) {
                resultArray.push(stringPart);
            }
            else {
                resultArray.push(" ");
            }
            var replaceString = stringPart + stringSplit;
            textToSearch = textToSearch.replace(replaceString, "");
            indexOf = textToSearch.indexOf(stringSplit);
        }
        if (textToSearch.length > 0) {
            resultArray.push(textToSearch);
        }
        return resultArray;
    },
    "imgCloseMrModalPanel_click": function (panelId) {
        var modalPanel = document.getElementById(panelId);
        modalPanel.style.display = "none";
    },
    "GetWindowWidth": function () {
        if (parseInt(navigator.appVersion) > 3) {
            if (navigator.appName == "Netscape") {
                winW = window.innerWidth;
            }
            if (navigator.appName.indexOf("Microsoft") != -1) {
                winW = document.body.offsetWidth;
            }
        }
        return winW;
    },
    "GetWindowHeight": function () {
        if (parseInt(navigator.appVersion) > 3) {
            if (navigator.appName == "Netscape") {
                winH = window.innerHeight;
            }
            if (navigator.appName.indexOf("Microsoft") != -1) {
                winH = document.body.offsetHeight;
            }
        }
        return winH;
    },
    "GetPageYOffset": function () {
        var ScrollTop = document.body.scrollTop;
        if (ScrollTop == 0) {
            if (window.pageYOffset)
                ScrollTop = window.pageYOffset;
            else
                ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
        }
        return ScrollTop;
    },
    "GetSelectedIndexByText": function (DropDownList, TextParam) {
        var selectedIndex = -1;
        var options = DropDownList.options;
        for (var i = 0; i < options.length; i++) {
            if (options[i].text == TextParam) {
                selectedIndex = i;
                break;
            }
        }
        return selectedIndex;
    },
    "GetElementsByTypeByTagName": function (ParentElement, TagName, AttributeName, AttributeValue) {
        var ResultCollection = new Array();
        var ElementCollection = new Array();
        if (ParentElement == null) {
            ElementCollection = document.getElementsByTagName(TagName);
        }
        else {
            ElementCollection = ParentElement.getElementsByTagName(TagName);
        }
        for (var i = 0; i < ElementCollection.length; i++) {
            if (ElementCollection[i].getAttribute(AttributeName) != null) {
                if (ElementCollection[i].getAttribute(AttributeName) == AttributeValue) {
                    ResultCollection.push(ElementCollection[i]);
                }
            }
        }
        return ResultCollection;
    },
    "CreateTransparentDivLayout": function (zIndexParam) {
        setWindowSize();
        var newdiv = document.createElement("div");
        newdiv.id = "divMrTransparentBackground";
        newdiv.style.width = myWidth + "px";
        newdiv.style.height = myHeight + "px";
        newdiv.style.top = "0px";
        newdiv.style.left = "0px";
        newdiv.style.paddingLeft = "0px";
        newdiv.style.paddingTop = "0px";
        newdiv.style.zIndex = zIndexParam;
        newdiv.style.backgroundColor = "#000000";
        newdiv.style.opacity = "0.63";
        newdiv.style.filter = "alpha(opacity=63)";
        if (navigator.appName.toString().toLowerCase().search("microsoft") != -1) {
            newdiv.style.position = "absolute";
        }
        else {
            newdiv.style.position = "fixed";
        }
        newdiv.style.top = "0px";
        newdiv.style.left = "0px";
        newdiv.style.zIndex = zIndexParam;
        document.body.appendChild(newdiv);
    }
,
    "nullParam": null
}
