﻿/* NOTE:  THE ONLY FUNCTIONS THAT SHOULD BE CALLED DIRECTLY FROM THE PAGE ARE showModal(), hideModal()
and hideAllModals().  ALL OTHER FUNCTIONS ARE INTERNAL.*/

//Global variables (values are set the first time showModal() is hit per page)
var modalParentDiv;
var modalIframeDiv;
var modalContentDiv;
var modalPopupExtender;
var enableModalLegacyScript = false;
var isIE6;

/*
This function will populate, position, style and display a modal popup box. Inputs:

- childDivID: [optional] The ID of the div that contains the content to be displayed in the modal.
- Specify '' for a modal with an iframe.
    
- modalDivCSSClass: The name of a CSS class to be applied to the parent modal div.
 
- mpeRepositionMode: A number 0-3 that represents when automatic repositioning of the modal popup should take
place (e.g. '3' means the modal will always stay centered in the screen whenever the browser window is scrolled or resized).
- 0: None
- 1: RepositionOnWindowResize
- 2: RepositionOnWindowScroll
- 3: RepositionOnWindowResizeAndScroll
    
- mpeX: The number of pixels over on the browser's X-axis that the modal popup should appear.
- Specify -1 to have the modal auto-center on the X-axis.
- Note: RepositionMode will only work on the X-axis if X is set to -1.
    
- mpeY: The number of pixels down on the browser's Y-axis that the modal popup should appear.
- Specify -1 to have the modal auto-center on the Y-axis.
- Note: RepositionMode will only work on the Y-axis if Y is set to -1.
    
- iframeURL: [optional] The URL of the document to load in an iframe inside the modal.
- Specify '' for a content modal with no iframe.
    
- iframeWidth: [optional] The width in pixels to be applied to the iframe.
- Specify -1 for a content modal with no iframe, or to use the default iframe width.
 
- iframeHeight: [optional] The initial height in pixels to be applied to the iframe before it is automatically
resized once the document finishes loading.
- Specify -1 for a content modal with no iframe, or to use the default iframe height.
- Note: Passing in a rough approximation will help prevent the modal from jumping around when RepositionMode is used.
*/
function showModal(childDivID, modalDivCSSClass, mpeRepositionMode, mpeX, mpeY, iframeURL, iframeWidth, iframeHeight) {
    //Set global references to page elements we need to use.
    if (modalParentDiv == undefined) {
        modalParentDiv = $get("modalParentDiv");
    }
    if (modalIframeDiv == undefined) {
        modalIframeDiv = $get("modalIframeDiv");
    }
    if (modalContentDiv == undefined) {
        modalContentDiv = $get("modalContentDiv");
    }
    if (modalPopupExtender == undefined) {
        modalPopupExtender = $find("modalPopupExtender");
    }

    //Set boolean used for IE6 hacks
    if (isIE6 == undefined) {
        if (navigator.appName == "Microsoft Internet Explorer") {
            var ieVersion = parseFloat(navigator.appVersion.split("MSIE")[1]);
            if (ieVersion < 7) {
                isIE6 = true;
            }
            else {
                isIE6 = false;
            }
        }
        else {
            isIE6 = false;
        }
    }

    //Hide the modal popup (in case we are coming from another modal and hideModal() was not called)
    modalPopupExtender.hide();

    //If we're already in a modal, save the current content before loading the next modal
    if (modalStack.count > 0) {
        var currentModal = modalStack.stack[modalStack.count - 1];
        saveModalContent(currentModal);
    }
    if (iframeURL != '') {

        loadIframe(iframeURL, iframeWidth, iframeHeight);
    }
    else {
        loadContent(childDivID);
    }

    setModalStyleAndPosition(modalDivCSSClass, mpeRepositionMode, mpeX, mpeY);

    //Display the modal popup
    modalPopupExtender.show();

    //Add the current modal to the Modal Stack
    modalStack.pushModal(new Modal(childDivID, modalDivCSSClass, mpeRepositionMode, mpeX, mpeY, iframeURL, iframeWidth, iframeHeight));
}

function showModalwithClose(iframeURL, iframeWidth, iframeHeight, iframeclosecaption, iframecloseimgurl) {
    //Set global references to page elements we need to use.
    if (modalParentDiv == undefined) {
        modalParentDiv = $get("modalParentDiv");
    }
    if (modalIframeDiv == undefined) {
        modalIframeDiv = $get("modalIframeDiv");
    }
    if (modalContentDiv == undefined) {
        modalContentDiv = $get("modalContentDiv");
    }
    if (modalPopupExtender == undefined) {
        modalPopupExtender = $find("modalPopupExtender");
    }

    //Set boolean used for IE6 hacks
    if (isIE6 == undefined) {
        if (navigator.appName == "Microsoft Internet Explorer") {
            var ieVersion = parseFloat(navigator.appVersion.split("MSIE")[1]);
            if (ieVersion < 7) {
                isIE6 = true;
            }
            else {
                isIE6 = false;
            }
        }
        else {
            isIE6 = false;
        }
    }

    //Hide the modal popup (in case we are coming from another modal and hideModal() was not called)
    modalPopupExtender.hide();
    loadIframewithclose(iframeURL, iframeWidth, iframeHeight, iframeclosecaption, iframecloseimgurl);

    setModalStyleAndPosition('modalPopup', 3, -1, -1);

    //Display the modal popup
    modalPopupExtender.show();

    //Add the current modal to the Modal Stack
    modalStack.pushModal(new Modal('', 'modalPopup', 3, -1, -1, iframeURL, iframeWidth, iframeHeight));

}

/* Similar to showModal(), but does not save the currently displaying modal, since we are closing modals and navigating
back down the modal stack.  This function should only be called by hideModal(), not by the page itself. */
function reshowModal(childDivID, modalDivCSSClass, mpeRepositionMode, mpeX, mpeY, iframeURL, iframeWidth, iframeHeight) {
    if (iframeURL != '') {
        loadIframe(iframeURL, iframeWidth, iframeHeight);
    }
    else {
        loadContent(childDivID);
    }

    setModalStyleAndPosition(modalDivCSSClass, mpeRepositionMode, mpeX, mpeY);

    //Display the modal popup
    modalPopupExtender.show();

    //Add the current modal to the Modal Stack
    modalStack.pushModal(new Modal(childDivID, modalDivCSSClass, mpeRepositionMode, mpeX, mpeY, iframeURL, iframeWidth, iframeHeight));
}

/* Set the styling and positioning properties for the modal to be displayed */
function setModalStyleAndPosition(modalDivCSSClass, mpeRepositionMode, mpeX, mpeY) {
    //Set the modalPopupExtender's positioning properties
    modalPopupExtender.set_repositionMode(mpeRepositionMode);
    modalPopupExtender.set_X(mpeX);

    if (isIE6 == true) {
        //Hack for IE6 and older: the toolkit already adjusts for how far the page has scrolled, so subtract that amount from the Y coordinate.
        modalPopupExtender.set_Y(mpeY - WebForm_GetScrollY());
    }
    else {
        modalPopupExtender.set_Y(mpeY);
    }

    //Give the modal div the specified CSS class
    modalParentDiv.className = modalDivCSSClass;
}

/* Create or find an iframe based on the parameters and add it to the modal */
function loadIframe(iframeURL, iframeWidth, iframeHeight) {
    var thisIframe;
    var savedIframe = $get("modalIframe" + modalStack.count);

    if (savedIframe != undefined) {
        //If we have this iframe saved, redisplay it
        savedIframe.id = "modalIframe";   //Change ID back to standard so it can always be found inside the modal
        savedIframe.style.display = "block";
    }
    else {
        //If we don't have this iframe saved, create it
        thisIframe = document.createElement("iframe")
        thisIframe.id = "modalIframe";
        thisIframe.marginHeight = "0px";
        thisIframe.marginWidth = "0px";
        thisIframe.frameBorder = "0";
        thisIframe.scrolling = "auto";

        if (isIE6 == true) {
            //IE6 and older: don't set the iframe source yet; a rendering bug prevents its contents from displaying (set it below after adding the iframe to the page).
            thisIframe.src = "javascript:;";
        }
        else {
            thisIframe.src = iframeURL;
        }

        //Set the iframe's dimensions, if specified
        if (iframeWidth != -1) {
            thisIframe.style.width = iframeWidth + "px";
        }
        if (iframeHeight != -1) {
            thisIframe.style.height = iframeHeight + "px";
        }

        //Add the iframe to the modal div
        modalIframeDiv.appendChild(thisIframe);

        if (isIE6 == true) {
            //Find the iframe we just added and set its source (done AFTER adding iframe to page to avoid IE6 display bugs with dynamically loaded iframes)
            $get("modalIframe").src = iframeURL;
        }
    }
}


/* Create or find an iframe with close based on the parameters and add it to the modal */
function loadIframewithclose(iframeURL, iframeWidth, iframeHeight, closecaption, closeimgurl) {

    modelTable = document.createElement('table');
    modelTable.id = 'modalIframe'; //'modelTableIframe';   
    modelTable.cellSpacing = '0px';
    modelTable.cellPadding = '0px';
    rows1 = modelTable.insertRow(-1);
    cells1 = rows1.insertCell(-1);
    cells1.id = 'cells1';
    modelInnerTable1 = document.createElement('table');
    modelInnerTable1.id = 'modelInnerTable1Iframe';
    modelInnerTable1.cellSpacing = '0px';
    modelInnerTable1.cellPadding = '0px';
    modelInnerTable1.style.width = "100%";
    cells1.appendChild(modelInnerTable1);
    modelInnerTable1rows = modelInnerTable1.insertRow(-1);
    modelInnerTable1cells1 = modelInnerTable1rows.insertCell(-1);
    modelInnerTable1cells1.style.width = "84%";
    modelInnerTable1cells2 = modelInnerTable1rows.insertCell(-1);


    createCloseDiv = document.createElement('div');
    createCloseDiv.id = 'close_Div';
    createCloseDiv.style.textAlign = "right";
    createCloseCaption = document.createElement('a');
    createCloseCaption.href = "javascript:;";
    createCloseCaption.onclick = function() { parent.hideModal(); };
    createCloseCaption.innerHTML = closecaption;
    createCloseDiv.appendChild(createCloseCaption);

    createCloseCaptionImg = document.createElement('a');

    createImg = document.createElement('img');
    createImg.style.width = "16%";
    createImg.style.height = "16%";
    createImg.style.border = "0px";
    createImg.className = "alignMiddle";
    createImg.alt = "";
    createImg.src = closeimgurl;
    createImg.onclick = function() { parent.hideModal(); };
    createCloseCaptionImg.appendChild(createImg);
    createCloseDiv.appendChild(createCloseCaptionImg);

    modelInnerTable1cells2.appendChild(createCloseDiv);


    rows2 = modelTable.insertRow(-1);
    cells2 = rows2.insertCell(-1);
    cells2.id = 'cells2';

    createIframe = document.createElement('iframe');
    createIframe.id = 'newIframe';
    createIframe.scrolling = "auto";
    createIframe.marginheight = "0px";
    createIframe.marginwidth = "0px";
    createIframe.setAttribute("frameBorder", 0);
    createIframe.src = "javascript:;"
    createIframe.style.height = iframeHeight + "px";
    createIframe.style.width = iframeWidth + "px";
    cells2.appendChild(createIframe)
    modalIframeDiv.appendChild(modelTable);

    var ifrm = $get("newIframe");
    if (iframeURL.indexOf("?") > 0) {
        iframeURL = iframeURL + "&rsl=" + pagesslenabled;
    }
    else {
        iframeURL = iframeURL + "?rsl=" + pagesslenabled;
    }
    ifrm.src = iframeURL;
}

function FindWinScrX() {
    return self.pageXOffset ? self.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body ? document.body.scrollLeft : 0;
}

function FindWinScrY() {
    return self.pageYOffset ? self.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body ? document.body.scrollTop : 0;
}



function framewindowload(ifrm) {

    var winheight = FindWinH();
    var winwidth = FindWinW();
    var WinScrY = FindWinScrY();
    var WinScrX = FindWinScrX();


    var iBottom = FindWinScrY() + FindWinH();
    var iLeft = FindWinScrX() + FindWinW();



    if (ifrm.contentWindow.document.body.scrollHeight > winheight) {
        ifrm.style.height = winheight - 60 + "px";
        modalParentDiv.style.top = ysDiv + 'px';

    }
    else {
        ifrm.style.height = ifrm.contentWindow.document.body.scrollHeight + "px";
        var h = ifrm.contentWindow.document.body.scrollHeight;
        var winchildheight = parseInt((winheight - h) / 2);
        modalParentDiv.style.top = FindWinScrY() + winchildheight + "px";
    }

    if (ifrm.contentWindow.document.body.scrollWidth > winwidth) {
        ifrm.style.width = winwidth - 20 + "px";
        modalParentDiv.style.left = xsDiv + 'px';

    }
    else {
        var w = ifrm.contentWindow.document.body.scrollWidth;
        var winchildwidth = parseInt((winwidth - w) / 2);
        modalParentDiv.style.top = winchildwidth + "px";
    }
    // modalParentDiv.style.position="fixed";
}


var locationrefresh = 0;


function FindWinW() {
    return self.innerWidth ? self.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body ? document.body.clientWidth : 0;
}

function FindWinH() {
    return self.innerHeight ? self.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body ? document.body.clientHeight : 0;
}


function FindWinScrX() {
    return self.pageXOffset ? self.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body ? document.body.scrollLeft : 0;
}

function FindWinScrY() {
    return self.pageYOffset ? self.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body ? document.body.scrollTop : 0;
}



/* Move the content from the specified child div into the modal div */
function loadContent(childDivID) {
    var childDiv = $get(childDivID);
    if (childDiv == undefined) {
        //If specified child div wasn't found on parent page, look for it inside each iframe page
        var frames = window.frames;
        for (var i = 0; i < frames.length; i++) {
            childDiv = frames[i].document.getElementById(childDivID);
            if (childDiv != undefined) {
                break;
            }
        }
    }
    if ((navigator.appName == "Netscape") || (!enableModalLegacyScript)) {
        var childDivContents = childDiv.removeChild(childDiv.getElementsByTagName('DIV')[0]);
        modalContentDiv.appendChild(childDivContents);
    }
    else {
        //Need to use innerHTML instead of the DOM for IE because otherwise IE doesn't maintain radio button selections 
        if (childDiv != null) {
            var childDivContents = childDiv.innerHTML;
            childDiv.innerHTML = "";
            modalContentDiv.innerHTML = childDivContents;
        }
    }
}

/* Save the current modal's content, either back to its original location (content modals) or to the iframe holding area (iframe modals) */
function saveModalContent(currentModal) {
    if (currentModal.iframeURL != '') {
        //It's an iframe modal; hide the currently displaying iframe
        var thisIframe = $get("modalIframe");
        if (thisIframe != null) {
            thisIframe.style.display = "none";
            thisIframe.id = "modalIframe" + (modalStack.count - 1);   //Change ID so they're numbered according to the modal stack count
        }
    }
    else {
        //It's a content modal; save the current content back to its original location
        var originDiv = $get(currentModal.childDivID);
        if (originDiv == undefined) {
            //If specified origin div wasn't found on parent page, look for it inside each iframe page
            var frames = window.frames;
            for (var i = 0; i < frames.length; i++) {
                originDiv = frames[i].document.getElementById(currentModal.childDivID);
                if (originDiv != undefined) {
                    break;
                }
            }
        }
        if (originDiv != null) {
            if ((navigator.appName == "Netscape") || (!enableModalLegacyScript)) {
                var childDivContents = modalContentDiv.removeChild(modalContentDiv.getElementsByTagName('DIV')[0]);
                originDiv.appendChild(childDivContents);
            }
            else {
                //Need to use innerHTML instead of the DOM for IE because otherwise IE doesn't maintain radio button selections 
                var childDivContents = modalContentDiv.innerHTML;
                modalContentDiv.innerHTML = "";
                originDiv.innerHTML = childDivContents;
            }
        }
    }
}

/* Hides the currently displaying modal popup box */
function hideModal() {

    if (modalPopupExtender == null) return;

    //Hide the modal popup
    modalPopupExtender.hide();

    //Remove and get a reference to the current modal from the stack
    var currentModal = modalStack.popModal();

    if (currentModal.iframeURL != '') {
        //It's an iframe modal; clear the current iframe from the modal
        if ($get("modalIframe") != null) {
            modalIframeDiv.removeChild($get("modalIframe"));

            if (locationrefresh) {
                locationrefresh = 0;

                //Refresh page, removing the experience from the query string
                var refreshQS = window.location.search.replace(/&?exp=[^&]+/i, "");
                var refreshURL = window.location.protocol + "//" + window.location.hostname + window.location.pathname + refreshQS;
                window.location.replace(refreshURL);
            }
        }
    }
    else {
        //It's a content modal; save the contents so it remains the same when reopening
        saveModalContent(currentModal);
    }

    //Reshow the previous modal, if there was one
    if (modalStack.count > 0) {
        var prevModal = modalStack.popModal();
        reshowModal(prevModal.childDivID, prevModal.modalDivCSSClass, prevModal.mpeRepositionMode, prevModal.mpeX, prevModal.mpeY, prevModal.iframeURL, prevModal.iframeWidth, prevModal.iframeHeight)
    }
}

/* Automatically hides all modals in the current modal stack. */
function hideAllModals() {
    while (modalStack.count > 0) {
        hideModal();
    }
}

//Prototype the ModalStack class and functions
ModalStack.prototype.pushModal = pushModal;
ModalStack.prototype.popModal = popModal;
function ModalStack() {
    this.stack = new Array();
    this.count = 0;
}
function pushModal(modal) {
    this.count = this.count + 1;
    this.stack.push(modal);
}
function popModal() {
    if (this.count <= 0) {
        return "Error: empty stack"
    }
    else {
        this.count = this.count - 1;
        return this.stack.pop();
    }
}

//Prototype the Modal class
function Modal(childDivID, modalDivCSSClass, mpeRepositionMode, mpeX, mpeY, iframeURL, iframeWidth, iframeHeight) {
    this.childDivID = childDivID;
    this.modalDivCSSClass = modalDivCSSClass;
    this.mpeRepositionMode = mpeRepositionMode;
    this.mpeX = mpeX;
    this.mpeY = mpeY;
    this.iframeURL = iframeURL;
    this.iframeWidth = iframeWidth;
    this.iframeHeight = iframeHeight;
}

//Instantiate a modal stack for this page
var modalStack = new ModalStack();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();