﻿var gModalDialogMask = null;
var gModalDialogContainer = null;
var gModalDialogFrame = null;
var gReturnFunc;
var gModalDialogIsShown = false;
var gHideSelects = false;

var gTabIndexes = new Array();
// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags = new Array("A", "BUTTON", "TEXTAREA", "INPUT", "IFRAME");

webServerIP = document.domain;
/**
* Initializes ModalDialog code on load.	
*/
function initModalDialog() {
    // Add the HTML to the body
    var body = document.getElementsByTagName('body')[0];
    var ModalDialogmask = document.createElement('div');
    ModalDialogmask.id = 'modelDialogMask';
    var ModalDialogcont = document.createElement('div');
    ModalDialogcont.id = 'modelDialogContainer';
    ModalDialogcont.innerHTML = '' +
		'<div id="modelDialogInner">' +
			'<div id="modelDialogTitleBar">' +
				'<div id="modelDialogTitle"></div>' +
				'<div id="modelDialogControls">' +
					'<a onclick="hideModalDialog(false);">Sluiten</a>' +
				'</div>' +
			'</div>' +
			'<div style="height:100%;" id="modelDialogContent"></div>' +
		'</div>';
    body.appendChild(ModalDialogmask);
    body.appendChild(ModalDialogcont);

    gModalDialogMask = document.getElementById("modelDialogMask");
    gModalDialogContainer = document.getElementById("modelDialogContainer");
    gModalDialogFrame = document.getElementById("modelDialogContent");

    // check to see if this is IE version 6 or lower. hide select boxes if so
    // maybe they'll fix this in version 7?
    var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
    if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
        gHideSelects = true;
    }
}

/**
* @argument width - int in pixels
* @argument height - int in pixels
* @argument url - url to display
* @argument returnFunc - function to call when returning true from the window.
*/
function showModalDialog(title, text, width, height, returnFunc) 
{
    //if (text) {
    //    text = text.replace(/\n/g, "<br />"); //vervang alle linebreaks door <br>
    //}
    text = text;
    gModalDialogIsShown = true;
    disableTabIndexes();
    gModalDialogMask.style.display = "block";
    gModalDialogContainer.style.display = "block";

    var titleBarHeight = parseInt(document.getElementById("modelDialogTitleBar").offsetHeight, 10);

    document.getElementById("modelDialogTitle").innerHTML = title;
  
    gModalDialogContainer.style.width = width + "px";
    if (height) 
    {
        gModalDialogContainer.style.height = (height + titleBarHeight) + "px";
    }
    //gModalDialogFrame.style.width = parseInt(document.getElementById("ModalDialogTitleBar").offsetWidth, 10) + "px";
    if (height != null) {
        gModalDialogFrame.style.height = height + "px";
    }
    if (text) 
    {
        gModalDialogFrame.innerHTML = text;
    }
     resizeModalDialogMask();
     centerModalDialog(width, height);
    //gReturnFunc = returnFunc;
    // for IE
    if (gHideSelects == true) {
        hideSelectBoxes();
    }
}

function showModalDialogWebService(title, paginaBlokID, identity, width, height, returnFunc) {

	//webservice aanroepen en resultaat in text plaatsen
    var webServiceUrl = "http://" + webServerIP + "/WebServices/MobileShop/MobileShopService.asmx";
    var webMethodName = "GetPaginaBlokHtml";
    var requestXML = "<paginaBlokID>" + paginaBlokID + "</paginaBlokID>";
    requestXML += "<identity>" + identity + "</identity>";
    var responseXML = callWebService(webServiceUrl, webMethodName, requestXML);

    if(getFirstNodeByElementName(responseXML, "Message").nodeValue=="Exception")
        title = "Er is een fout opgetreden";

    //text = getFirstNodeByElementName(responseXML, "XmlString").nodeValue;
    text = selectSingleNodeValue(responseXML, "XmlString")
    
    showModalDialog(title, text, width, height, returnFunc);
}

function showModalDialogWebServicePerShop(title, paginaBlokID, identity, width, height, returnFunc, shopID) {

    //webservice aanroepen en resultaat in text plaatsen
    var webServiceUrl = "http://" + webServerIP + "/WebServices/MobileShop/MobileShopService.asmx";
    var webMethodName = "GetPaginaBlokHtmlPerShop";

    var requestXML = "<shopID>" + shopID + "</shopID>";
    requestXML += "<paginaBlokID>" + paginaBlokID + "</paginaBlokID>";
    requestXML += "<identity>" + identity + "</identity>";
    var responseXML = callWebService(webServiceUrl, webMethodName, requestXML);

    if (getFirstNodeByElementName(responseXML, "Message").nodeValue == "Exception")
        title = "Er is een fout opgetreden";

    //text = getFirstNodeByElementName(responseXML, "XmlString").nodeValue;
    text = selectSingleNodeValue(responseXML, "XmlString")

    showModalDialog(title, text, width, height, returnFunc);
}


/**
* @argument callReturnFunc - bool - determines if we call the return function specified
* @argument returnVal - anything - return value 
*/
function hideModalDialog() {
    gModalDialogIsShown = false;
    restoreTabIndexes();
    if (gModalDialogMask == null) {
        return;
    }
    gModalDialogMask.style.display = "none";
    gModalDialogContainer.style.display = "none";

    // display all select boxes
    if (gHideSelects == true) {
        displaySelectBoxes();
    }
}

function resizeModalDialogMask()
{   
    var htmlheight = getScrollHeight();
    var windowheight = getViewportHeight();
    if (htmlheight < windowheight)
    {
        gModalDialogMask.style.height = windowheight + "px";
    }
    else
    {
        gModalDialogMask.style.height = htmlheight + "px";
    }
}

/**
* @argument width - int - width of ModalDialog 
* @argument height - int - height of ModalDialog
*/
function centerModalDialog(width, height) {
    if (gModalDialogIsShown == true) {

        var scrollTop = getScrollTop();
        var marginTop = 100;
        var top = scrollTop + marginTop;
        if (top < 0) { top = 0; }
    
        if (width == null || isNaN(width)) {
            width = gModalDialogContainer.offsetWidth;
        }

        var fullWidth = getViewportWidth();
        var scLeft = getScrollLeft();
        var left = scLeft + ((fullWidth - width) / 2);

        gModalDialogContainer.style.top = top + "px";
        gModalDialogContainer.style.left = left + "px";
    }
}

function getViewportHeight() {
    if (window.innerHeight != window.undefined) return window.innerHeight;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientHeight;
    if (document.body) return document.body.clientHeight;
    return window.undefined;
}

function getViewportWidth() {
    if (window.innerWidth != window.undefined) return window.innerWidth;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientWidth;
    if (document.body) return document.body.clientWidth;
    return window.undefined;
}

function getScrollLeft() {
    if (self.pageYOffset) {
        scLeft = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollLeft) {
        scLeft = document.documentElement.scrollLeft;
    } else if (document.body) {
        scLeft = document.body.scrollLeft;

    }
    return scLeft;
}

function getScrollTop() {
    if (self.pageYOffset) {
        scTop = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
        scTop = document.documentElement.scrollTop;
    }
    else if (document.body) {
        scTop = document.body.scrollTop;
    }
    return scTop;
}

function getScrollHeight() {
    if (document.body) {
        scHeight = document.body.parentNode.scrollHeight;
    }
    else if (self.pageYOffset) {
        scHeight = self.scrollHeight
    }
    else if (document.documentElement && document.documentElement.scrollHeight) {
        scHeight = document.documentElement.scrollHeight;
    }

    return scHeight;
}


// @argument e - event - keyboard event that caused this function to be called.
function keyDownHandler(e) {

    //key = e ? e.which : event.keyCode;
    //bovenstaande werkt niet in IE8
    
    var isIE = (window.event != null);
    var key = (isIE) ? event.keyCode : e.which;
    
    //if (gModalDialogIsShown && key == 9) return false; // backspace key uitsluiten

    // Onderstaande code weg, omdat het dialog moet sluiten bij iedere key
    /*
    if (key == 13 || key == 0 || key == 27 || key == 88 || key == 128) {
        hideModalDialog()
        return false;
    }
    */

    if (gModalDialogIsShown && key != 9) {
        hideModalDialog();
        return false;
    }
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes() {
    if (document.all) {
        var i = 0;
        for (var j = 0; j < gTabbableTags.length; j++) {
            var tagElements = document.getElementsByTagName(gTabbableTags[j]);
            for (var k = 0; k < tagElements.length; k++) {
                gTabIndexes[i] = tagElements[k].tabIndex;
                tagElements[k].tabIndex = "-1";
                i++;
            }
        }
    }
}

// For IE. Restore tab-indexes.
function restoreTabIndexes() {
    if (document.all) {
        var i = 0;
        for (var j = 0; j < gTabbableTags.length; j++) {
            var tagElements = document.getElementsByTagName(gTabbableTags[j]);
            for (var k = 0; k < tagElements.length; k++) {
                tagElements[k].tabIndex = gTabIndexes[i];
                tagElements[k].tabEnabled = true;
                i++;
            }
        }
    }
}

/**
* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
* Thanks for the code Scott!
*/
function hideSelectBoxes() {
    for (var i = 0; i < document.forms.length; i++) {
        for (var e = 0; e < document.forms[i].length; e++) {
            if (document.forms[i].elements[e].tagName == "SELECT") {
                document.forms[i].elements[e].style.visibility = "hidden";
            }
        }
    }
}

/**
* Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*/
function displaySelectBoxes() {
    for (var i = 0; i < document.forms.length; i++) {
        for (var e = 0; e < document.forms[i].length; e++) {
            if (document.forms[i].elements[e].tagName == "SELECT") {
                document.forms[i].elements[e].style.visibility = "visible";
            }
        }
    }
}

/**
* X-browser event handler attachment and detachment
* @argument obj - the object to attach event to
* @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
* @argument fn - function to call
*/
function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    } else {
        return false;
    }
}

addEvent(window, "resize", centerModalDialog);
addEvent(document, "keypress", keyDownHandler);
addEvent(window, "load", initModalDialog);
addEvent(window, "scroll", centerModalDialog);