﻿function messageBox(browserRef)
{
    this.browserRef = browserRef;
    this.parentPath = "../";
    this.focusedElement = null;
    this.marginLeft = null;
    this.marginTop = null;
    this.specialObjectsInitialStates = null;

    this.showDivOverLay = showDivOverLay;
    this.closeDivOverLay = closeDivOverLay;
    this.hideSpecialObjects = hideSpecialObjects;
    this.showSpecialObjects = showSpecialObjects;
    this.showMessage = showMessage;
    this.showQuestionMessage = showQuestionMessage;
    this.showMessageButtonless = showMessageButtonless;
    this.closeMessageWindow = closeMessageWindow;
    this.centerDivOverLay = centerDivOverLay;
    this.centerDivMessageWindow = centerDivMessageWindow;
    this.isValidForm = isValidForm;
    this.showValidationMessage = showValidationMessage;
    this.postBackConfirm = postBackConfirm;
    this.addFocusEvent = addFocusEvent;
    this.postBackFunction = postBackFunction;

    addScrollEvent(
                        function()
                        {
                            var divOverLayRef = document.getElementById('divOverLay');
                            var divMessageWindowRef = document.getElementById('divMessageWindow');

                            if (divOverLayRef != null)
                            {
                                divOverLayRef.style.left = getScrollCoordinatesX() + 'px';
                                divOverLayRef.style.top = getScrollCoordinatesY() + 'px';
                                divOverLayRef.style.width = getClientWidthValue() + 'px';
                                divOverLayRef.style.height = getClientHeightValue() + 'px';
                            }

                            if (divMessageWindowRef != null)
                            {
                                divMessageWindowRef.style.marginLeft = messageBoxRef.marginLeft + getScrollCoordinatesX() + 'px';
                                divMessageWindowRef.style.marginTop = messageBoxRef.marginTop + getScrollCoordinatesY() + 'px';
                            }
                        }
                  );

    addResizeEvent(
                        function()
                        {
                            var divOverLayRef = document.getElementById('divOverLay');
                            if (divOverLayRef != null)
                            {
                                divOverLayRef.style.width = getClientWidthValue() + 'px';
                                divOverLayRef.style.height = getClientHeightValue() + 'px';
                            }
                        }
                  );

    function showDivOverLay()
    {
        if (browserRef.isIE && browserRef.version < 7)
            this.hideSpecialObjects();

        var divOverLay = document.createElement('div');
        divOverLay.setAttribute('id', 'divOverLay');
        divOverLay.style.width = getClientWidthValue() + 'px';
        divOverLay.style.height = getClientHeightValue() + 'px';
        document.body.appendChild(divOverLay);
        this.centerDivOverLay();
        divOverLay.focus(); //to move focus from back components to divOverLay
    }

    function closeDivOverLay()
    {
        document.body.removeChild(document.getElementById('divOverLay'));

        if (browserRef.isIE && browserRef.version < 7)
            this.showSpecialObjects();
    }

    function hideSpecialObjects()
    {
        var specialObjectsArr = $$('select', 'object');
        this.specialObjectsInitialStates = new Array();

        for (var i = 0; i < specialObjectsArr.length; i++)
        {
            this.specialObjectsInitialStates[i] = specialObjectsArr[i].style.visibility;
            specialObjectsArr[i].style.visibility = 'hidden';
        }
    }

    function showSpecialObjects()
    {
        var specialObjectsArr = $$('select', 'object');
        for (var i = 0; i < specialObjectsArr.length; i++)
            specialObjectsArr[i].style.visibility = this.specialObjectsInitialStates[i];
    }

    function showMessage(htmlMessage, messageBoxIcon, btnOKText, focusedElement, func)
    {
        this.focusedElement = focusedElement;

        htmlMessage = htmlMessage.replace(/\[\[([^\]\]]*)\]\]/g, "<$1>"); // Google Chrome not support code behind html as a parameter

        this.showDivOverLay();

        var divMessageWindow = document.createElement('div');
        divMessageWindow.setAttribute('id', 'divMessageWindow');

        divMessageWindow.innerHTML = "<table id='tblMessageWindow' cellpadding='0px' cellspacing='0px'>" +
                                        "<tr id='trMessageWindowHeader'>" +
                                            "<td>" +
                                                "<img src='" + this.parentPath + "images/messageBox/messageBoxHeader.jpg' />" +
                                                "<div style='position: absolute; top: 0px; right: 0px;'>" +
                                                    "<input id='btnCloseMessageWindow' onclick='messageBoxRef.closeMessageWindow();' type='button' value='x' />" +
                                                "</div>" +
                                            "</td>" +
                                        "</tr>" +
                                        "<tr id='trMessageWindowContent'>" +
                                            "<td>" +
                                                "<br />" +
                                                "<table cellpadding='0px' cellspacing='0px'>" +
                                                    "<tr>" +
                                                        "<td style='width: 5px;'></td>" +
                                                        "<td>" +
                                                            "<img src='" + this.parentPath + "images/messageBox/" + messageBoxIcon + "' />" +
                                                        "</td>" +
                                                        "<td style='width: 5px;'></td>" +
                                                        "<td>" +
                                                            "<div id='divMessage'>" +
                                                                htmlMessage +
                                                            "</div>" +
                                                        "</td>" +
                                                        "<td style='width: 5px;'></td>" +
                                                    "</tr>" +
                                                "</table>" +
                                                "<br />" +
                                                "<div style='text-align: center; vertical-align: bottom;'>" +
                                                    "<input class='messageBoxButton' onclick='messageBoxRef.closeMessageWindow();' type='button' value='" + btnOKText + "' id='btnOK' />" +
                                                "</div>" +
                                                "<div style='height: 5px;'></div>" +
                                            "</td>" +
                                        "</tr>" +
                                    "</table>";

        document.body.appendChild(divMessageWindow);
        if (func)
        {
            addClickEvent(document.getElementById('btnOk'), func);
            addClickEvent(document.getElementById('btnCloseMessageWindow'), func);
        }
        this.centerDivMessageWindow();
    }

    function showQuestionMessage(htmlMessage, messageBoxIcon, btnYesText, btnNoText, func, focusedElement)
    {
        this.focusedElement = focusedElement;

        this.showDivOverLay();

        var divMessageWindow = document.createElement('div');
        divMessageWindow.setAttribute('id', 'divMessageWindow');

        divMessageWindow.innerHTML = "<table id='tblMessageWindow' cellpadding='0px' cellspacing='0px'>" +
                                        "<tr id='trMessageWindowHeader'>" +
                                            "<td>" +
                                                "<img src='" + this.parentPath + "images/messageBox/messageBoxHeader.jpg' />" +
                                            "</td>" +
                                        "</tr>" +
                                        "<tr id='trMessageWindowContent'>" +
                                            "<td>" +
                                                "<br />" +
                                                "<table cellpadding='0px' cellspacing='0px'>" +
                                                    "<tr>" +
                                                        "<td style='width: 5px;'></td>" +
                                                        "<td>" +
                                                            "<img src='" + this.parentPath + "images/messageBox/" + messageBoxIcon + "' />" +
                                                        "</td>" +
                                                        "<td style='width: 5px;'></td>" +
                                                        "<td>" +
                                                            "<div id='divMessage'>" +
                                                                htmlMessage +
                                                            "</div>" +
                                                        "</td>" +
                                                    "</tr>" +
                                                "</table>" +
                                                "<br />" +
                                                "<div style='text-align: center; vertical-align: bottom;'>" +
                                                    "<input class='messageBoxButton' onclick='messageBoxRef.closeMessageWindow();' type='button' value='&nbsp;&nbsp;" + btnYesText + "&nbsp;&nbsp;' id='btnYes' />" +
                                                    " &nbsp; " +
                                                    "<input class='messageBoxButton' onclick='messageBoxRef.closeMessageWindow();' type='button' value='&nbsp;&nbsp;" + btnNoText + "&nbsp;&nbsp;' id='btnNo' />" +
                                                "</div>" +
                                                "<div style='height: 5px;'></div>" +
                                            "</td>" +
                                        "</tr>" +
                                    "</table>";

        document.body.appendChild(divMessageWindow);
        addClickEvent(document.getElementById('btnYes'), func);
        this.centerDivMessageWindow();

        return false;
    }

    function showMessageButtonless(htmlMessage, messageBoxIcon)
    {
        this.showDivOverLay();

        var divMessageWindow = document.createElement('div');
        divMessageWindow.setAttribute('id', 'divMessageWindow');

        divMessageWindow.innerHTML = "<table id='tblMessageWindow' cellpadding='0px' cellspacing='0px'>" +
                                        "<tr id='trMessageWindowHeader'>" +
                                            "<td>" +
                                                "<img src='" + this.parentPath + "images/messageBox/messageBoxHeader.jpg' />" +
                                            "</td>" +
                                        "</tr>" +
                                        "<tr id='trMessageWindowContent'>" +
                                            "<td>" +
                                                "<br />" +
                                                "<table cellpadding='0px' cellspacing='0px'>" +
                                                    "<tr>" +
                                                        "<td style='width: 5px;'></td>" +
                                                        "<td>" +
                                                            "<img src='" + this.parentPath + "images/messageBox/" + messageBoxIcon + "' />" +
                                                        "</td>" +
                                                        "<td style='width: 5px;'></td>" +
                                                        "<td>" +
                                                            "<div id='divMessage'>" +
                                                                htmlMessage +
                                                            "</div>" +
                                                        "</td>" +
                                                        "<td style='width: 5px;'></td>" +
                                                    "</tr>" +
                                                "</table>" +
                                                "<br />"
                                            "</td>" +
                                        "</tr>" +
                                    "</table>";

        document.body.appendChild(divMessageWindow);
        this.centerDivMessageWindow();
    }

    function closeMessageWindow()
    {
        this.closeDivOverLay();
        document.body.removeChild(document.getElementById('divMessageWindow'));
        if (this.focusedElement != null)
            this.focusedElement.focus();
    }

    function centerDivOverLay()
    {
        var scrollCoordinatesArray = getScrollCoordinates();

        var divOverLayRef = document.getElementById('divOverLay');
        divOverLayRef.style.left = scrollCoordinatesArray[0] + 'px';
        divOverLayRef.style.top = scrollCoordinatesArray[1] + 'px';
    }

    function centerDivMessageWindow()
    {
        var scrollCoordinatesArray = getScrollCoordinates();

        var divMessageWindowRef = document.getElementById('divMessageWindow');
        this.marginLeft = Math.round(divMessageWindowRef.offsetWidth / -2);
        this.marginTop = Math.round(divMessageWindowRef.offsetHeight / -2);

        divMessageWindowRef.style.marginLeft = this.marginLeft + scrollCoordinatesArray[0] + 'px';
        divMessageWindowRef.style.marginTop = this.marginTop + scrollCoordinatesArray[1] + 'px';
    }

    function isValidForm(validationGroup, validationSummaryDivID)
    {
        var scrollToFunc = window.scrollTo;
        window.scrollTo = function() {};
        var isPageValid = Page_ClientValidate(validationGroup);
        setTimeout(function() { window.scrollTo = scrollToFunc; }, 1); //to prevent to load old function before second validation control

        if (isPageValid)
            return true;
        else
        {
            this.showValidationMessage(validationSummaryDivID);
            this.addFocusEvent(validationGroup);
            return false;
        }

        
    }

    function showValidationMessage(divID)
    {
        var divUpValidationsRef = document.getElementById(divID);
        this.showMessage(divUpValidationsRef.innerHTML, "stop.gif", "Tamam");
    }

    function postBackConfirm(questionMessage, element)
    {
        this.showQuestionMessage(
                                    questionMessage,
                                    'question.gif',
                                    'Evet',
                                    'Hayır',
                                    function()
                                    {
                                        var oldFunction = element.onclick;
                                        element.onclick = function() { };
                                        element.click();
                                        element.onclick = oldFunction;
                                    }
                                );
    }

    function addFocusEvent(validationGroup)
    {
        var firstErrorIndex = -1;
        for (var i = 0; i < Page_Validators.length; i++)
        {
            if (Page_Validators[i].validationGroup == validationGroup && (Page_Validators[i].isvalid == false || Page_Validators[i].isvalid == 'False')) //'False' returns from the ServerValidate function
            {
                firstErrorIndex = i;
                break;
            }
        }

        if (firstErrorIndex != -1)
        {
            var btnOKRef = document.getElementById('btnOK');
            var btnCloseMessageWindowRef = document.getElementById('btnCloseMessageWindow');

            addClickEvent(btnOKRef, function() { try { document.getElementById(Page_Validators[firstErrorIndex].controltovalidate).focus(); } catch (err) { } });
            addClickEvent(btnCloseMessageWindowRef, function() { try { document.getElementById(Page_Validators[firstErrorIndex].controltovalidate).focus(); } catch (err) { } });
        }
    }

    function postBackFunction(formID, eventTarget, eventValue)
    {
        var formRef = document.forms[formID];
        if (!formRef)
            eval("formRef = document." + formID + ";");

        try
        {
            if (!formRef.onsubmit || (formRef.onsubmit() != false))
            {
                formRef.__EVENTTARGET.value = eventTarget;
                formRef.__EVENTARGUMENT.value = eventValue;
                formRef.submit();
            }
        }
        catch (err)
        {
        }
    }
}

var messageBoxRef = new messageBox(browserRef);
