﻿//Modified from http://seancode.blogspot.com/2008/01/modal-dialog-box-in-javascript.html
function openModalWindow() {
    var div = $('modalWindow');
    var bgDiv = $('modalBackgroundDiv');
    var docDim = getPageSizeWithScroll(document.body);

    //get the size of the window and calculate where the box should be placed
    var wDim = getBrowserWindowSize();
    var dDim = Element.getDimensions(div);
    var vDim = document.viewport.getScrollOffsets(); 

    div.style.top = (((wDim.height - dDim.height*2) / 2) + vDim.top) + 'px';
    div.style.left = (((wDim.width - dDim.width) / 2) + vDim.left) + 'px';

    if (docDim.height > wDim.height) {
        wDim.height = docDim.height;
    }
	if (docDim.width > wDim.width) {
		wDim.width = docDim.width;
	}

    bgDiv.style.width = wDim.width + 'px';
    bgDiv.style.height = wDim.height + 'px';

    Element.show(div);
    Element.show(bgDiv);
}
function closeModalWindow() {
    Element.hide('modalWindow');
    Element.hide('modalBackgroundDiv');
}

function openModalDoubleEmail() {
    var div = $('modalDoubleEmail');
    var bgDiv = $('modalBackgroundDiv');
    var docDim = getPageSizeWithScroll(document.body);

    //get the size of the window and calculate where the box should be placed
    var wDim = getBrowserWindowSize();
    var dDim = Element.getDimensions(div);
    var vDim = document.viewport.getScrollOffsets(); 

    div.style.top = (((wDim.height - dDim.height*2) / 2) + vDim.top) + 'px';
    div.style.left = (((wDim.width - dDim.width) / 2) + vDim.left) + 'px';

    if (docDim.height > wDim.height) {
        wDim.height = docDim.height;
    }
	if (docDim.width > wDim.width) {
		wDim.width = docDim.width;
	}

    bgDiv.style.width = wDim.width + 'px';
    bgDiv.style.height = wDim.height + 'px';

    Element.show(div);
    Element.show(bgDiv);
}
function closeModalDoubleEmail() {
    Element.hide('modalDoubleEmail');
    Element.hide('modalBackgroundDiv');
}

function getBrowserWindowSize() {
    var winW = 750, winH = 1100;
    if (parseInt(navigator.appVersion)>3) {
        if (navigator.appName=="Netscape") {
            winW = window.innerWidth;
            winH = window.innerHeight;
        }
        if (navigator.appName.indexOf("Microsoft")!=-1) {
            winW = document.body.offsetWidth;
            winH = document.body.offsetHeight;
        }
    }
    var rval = {
        width: winW,
        height: winH
    };
    return rval;
}
function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight + document.body.offsetLeft;
		xWithScroll = document.body.offsetWidth + document.body.offsetTop;
	}
	var rval = {
		width: xWithScroll,
		height: yWithScroll
	};
	return rval;
}