﻿//*** Developed by CJ Amodeo
//*** The only requirement to use this script is to declare display, width, and height in a style
//*** of the div to be displayed (the id of the div passed to show()). This is for IE6 compatibility.
//*** The script tag calling this file should be at the bottom of the page right before </body>.
//*** Page should be XHTML 1.0 Transitional
var modDia_grayoutID = "hypModPopDivGrayout";

function ModalDialog()
{
    this.dialogID = null;
    this.divWidth = 0;
    this.divHeight = 0;
    this.grayoutDivID = modDia_grayoutID;
    this.grayoutDivObj = document.getElementById(modDia_grayoutID);
	this.userAgent = window.navigator.userAgent.toLowerCase();
	this.userAgentVersion = (this.userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1];
    this.IE = /msie/.test(this.userAgent);
    this.IE6 = (this.IE && (parseInt(this.userAgentVersion) >= 6)); //includes IE7
    this.IE7 = (this.IE && (parseInt(this.userAgentVersion) >= 7));
    
    this.getGrayWidth = function() {
        var w = 0;
        if (this.IE) {
            if (this.IE6) {
                w = document.documentElement.clientWidth;
            } else {
                w = document.body.clientWidth;
            }
        } else {
            w = window.innerWidth;
        }
        return w;
    }
    
    this.getGrayHeight = function() {
        var h = 0;
        if (this.IE) {
            if (this.IE6) {
                h = document.documentElement.clientHeight;
            } else {
                h = document.body.clientHeight;
            }
        } else {
            h = window.innerHeight;
        }
        return h;
    }
    
    this.getCenterX = function() {
        var x = 0;
        if (this.IE) {
            if (this.IE6) {
                x = (document.documentElement.clientWidth-this.divWidth)/2;
            } else {
                x = (document.body.clientWidth-this.divWidth)/2;
            }
        } else {
            x = (window.innerWidth-this.divWidth)/2;
        }
        if (!this.IE) x-= 10;
	    if (x < 0) x = 0;
        return x;
    }
    
    this.getCenterY = function() {
        var y = 0;
        if (this.IE) {
            if (this.IE6) {
                y = ((document.documentElement.clientHeight-this.divHeight)/2) + document.documentElement.scrollTop;
            } else {
                y = ((document.body.clientHeight-this.divHeight)/2) + document.body.scrollTop;
            }
        } else {
            y = (window.innerHeight-this.divHeight)/2;
        }
        if (!this.IE) y-= 10;
	    if (y < 0) y = 0;
        return y;
    }
    
    this.getScrollY = function() {
        var y = 0;
        if (this.IE) {
            if (this.IE6) {
                y = document.documentElement.scrollTop;
            } else {
                y = document.body.scrollTop;
            }
        } else {
            y = 0;
        }
        return y;
    }
    
    this.positionGray = function() {
        var div = document.getElementById(this.dialogID);
        if (div != null) {
            div.style.left = this.getCenterX()+"px";
            div.style.top = this.getCenterY()+"px";
        }       
        this.grayoutDivObj.style.width = this.getGrayWidth() + "px";
        this.grayoutDivObj.style.height = this.getGrayHeight() + "px";
        this.grayoutDivObj.style.top = this.getScrollY() + "px";
    }

    this.show = function(divDialogID) {
        this.dialogID = divDialogID
        this.divWidth = parseInt(document.getElementById(this.dialogID).style.width);
        this.divHeight = parseInt(document.getElementById(this.dialogID).style.height);
        this.grayoutDivObj.style.display = "inline";
        var div = document.getElementById(this.dialogID);
        div.style.display = "inline";
        div.style.zIndex = 99;
        if (this.IE) {
            div.style.position = "absolute";
            this.grayoutDivObj.style.position = "absolute";
            if (this.IE6 && !this.IE7) {
                var elems = document.getElementsByTagName("select");
                for (var i = 0; i < elems.length; i++) {
                    elems[i].style.visibility = "hidden";
                }
                elems = div.getElementsByTagName("select");
                for (var i = 0; i < elems.length; i++) {
                    elems[i].style.visibility = "visible";
                }
            }
        } else {
            div.style.position = "fixed";
            this.grayoutDivObj.style.position = "fixed";
        }
        this.positionGray();

	$("html").css('overflow','hidden');	
	$("body").css('overflow','hidden');
    }
    
    this.hide = function() {
        document.getElementById(this.dialogID).style.display = "none";
        this.grayoutDivObj.style.display = "none";
        this.grayoutDivObj.style.width = "0px"; //OSX Firefox Hack
        this.grayoutDivObj.style.height = "0px";
        if (this.IE6 && !this.IE7) {
            var elems = document.getElementsByTagName("select");
            for (var i=0; i<elems.length; i++) {
                elems[i].style.visibility = "visible";
            }
        }

	$("html").css('overflow','auto');
	$("body").css('overflow','auto');
    }
	
	this.addWindowEvent = function(eventName, func) {
		if (this.IE) {
			window.attachEvent("on"+eventName, func);
		} else {
			window.addEventListener(eventName, func, false);
		}
	}
}

var grayoutStyle = "position:fixed;left:0px;top:0px;width:0px;height:0px;display:none;background-color:#FFFFFF;z-index:98;filter:alpha(opacity=60);opacity:0.60;";
document.writeln("<div style=\""+grayoutStyle+"\" id=\""+modDia_grayoutID+"\"></div>");

//INSTANCE CREATED. USE IT
var modalDialog = new ModalDialog();

if (modalDialog.IE6) {
	modalDialog.addWindowEvent("scroll", function(e) {
        if (modalDialog.dialogID != null) {
            if (document.getElementById(modalDialog.dialogID).style.display == "inline") {
                modalDialog.positionGray();
            }
        }
    });
}
if (!modalDialog.IE) {
    window.captureEvents(Event.RESIZE);
}
modalDialog.addWindowEvent("resize", function(e) {
    if (modalDialog.dialogID != null) {
        if (document.getElementById(modalDialog.dialogID).style.display == "inline") {
            modalDialog.positionGray();
        }
    }
});
