// getPageSize() : Taken from LightBox
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
function getPageSize()
{
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function createCoverDiv()
{
	// Get the body element
	var body = document.getElementsByTagName("body")[0];
	
	// Create a body-covering DIV if we don't already have one
	var coverDiv = document.getElementById("coverDiv");
	if (!coverDiv)
	{
		coverDiv = document.createElement("DIV");
		coverDiv.id = "coverDiv"
		coverDiv.style.visibility = "hidden";
		coverDiv.style.opacity = 0.0;
		coverDiv.style.position = "absolute";
		coverDiv.style.left = "0px";
		coverDiv.style.top = "0px";
		
		if (window.navigator.appName == "Microsoft Internet Explorer")
		{
			coverDiv.style.width = (parseInt(body.clientWidth) + parseInt(body.currentStyle.marginLeft) + parseInt(body.currentStyle.marginRight)) + "px";
			coverDiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
			if (window.navigator.appVersion.indexOf("MSIE 8.0") >= 0)
			{
				coverDiv.style.height = "100%";
			}
		}
		else
		{
			coverDiv.style.width = "100%";
		}
		
		coverDiv.style.background = "#000000";
		coverDiv.zIndex = 10001;
		body.appendChild(coverDiv);
	}
	
	pageSize = getPageSize();
	coverDiv.style.height = pageSize[1] + "px";
	
	return coverDiv;
}

function createDialog(id, message, title, type)
{
	// Get the body element
	var body = document.getElementsByTagName("body")[0];
	
	// Create a message-box DIV if we don't already have one
	var messageBox = document.getElementById(id);
	if (!messageBox)
	{
		var messageBox = document.createElement("DIV");
		messageBox.id = id;
		messageBox.style.position = "absolute";
		messageBox.style.left = "50%";
		messageBox.style.top = "20%";
		messageBox.style.width = "400px";
		messageBox.style.minHeight = "150px";
		messageBox.style.marginLeft = "-200px"
		messageBox.style.opacity = 0.0;
		messageBox.zIndex = 10002;
		messageBox.style.visibility = "visible";
		body.appendChild(messageBox);
	}

	// Ensure it's ready to fade in
	messageBox.style.opacity = 0.0;
	messageBox.style.visibility = "visible";

	// Set up the contents of the message box
	if (type == 1)
	{
		messageBox.innerHTML = "<div class=\"messageBox\"><div class=\"messageBoxT\"><div class=\"messageBoxL\"><div class=\"messageBoxR\"><div class=\"messageBoxB\"><div class=\"messageBoxTL\"><div class=\"messageBoxTR\"><div class=\"messageBoxBL\"><div class=\"messageBoxBR\"><div class=\"messageBoxContent\"><div class=\"messageBoxTitle\">" + title + "</div><div id=\"" + id + "Message\" class=\"messageBoxMessage\">" + message + "</div><div class=\"messageBoxButtons\"><input type=\"button\" class=\"messageBoxYes\" value=\"Yes\" id=\"mBYes\" /><input type=\"button\" class=\"messageBoxNo\" value=\"No\" id=\"mBNo\" /></div></div></div></div></div></div></div></div></div></div></div>";
	}
	else
	{
		messageBox.innerHTML = "<div class=\"messageBox\"><div class=\"messageBoxT\"><div class=\"messageBoxL\"><div class=\"messageBoxR\"><div class=\"messageBoxB\"><div class=\"messageBoxTL\"><div class=\"messageBoxTR\"><div class=\"messageBoxBL\"><div class=\"messageBoxBR\"><div class=\"messageBoxContent\"><div class=\"messageBoxTitle\">" + title + "</div><div id=\"" + id + "Message\" class=\"messageBoxMessage\">" + message + "</div><div class=\"messageBoxButtons\"><input type=\"button\" class=\"messageBoxOK\" value=\"OK\" id=\"mBOK\" /></div></div></div></div></div></div></div></div></div></div></div>";
	}
	
	return messageBox;
}

function confirmDialog(message, onConfirm, onDeny, title, confirmText, denyText)
{
	// Ensure cover div is ready to fade in
	var coverDiv = createCoverDiv();
	coverDiv.style.opacity = 0.0;
	coverDiv.style.top = document.documentElement.scrollTop + "px";
	coverDiv.style.visibility = "visible";

	// Create a message-box DIV if we don't already have one
	var messageBox = createDialog("messageBox", message, title ? title : "Confirmation", 1);

	// Set up "Yes" click handler
	if (confirmText)
	{
		document.getElementById("mBYes").value = confirmText;
	}
	document.getElementById("mBYes").onclick = function()
	{
		hideDialog('messageBox');
		onConfirm();
	};

	// Set up "No" click handler	
	if (denyText)
	{
		document.getElementById("mBNo").value = denyText;
	}
	document.getElementById("mBNo").onclick = function()
	{
		hideDialog('messageBox');
		onDeny();
	};

	// Fade everything in	
	startFadeIn(coverDiv, 0.5, 0.1);
	startFadeIn(messageBox, 1.0, 0.2);
}

function hideDialog(id)
{
	startFadeOut(document.getElementById('coverDiv'), 0.0, 0.1, true);
	startFadeOut(document.getElementById(id), 0.0, 0.2, true);
}

function messageDialog(message, onClose, title, okText)
{
	// Ensure cover div is ready to fade in
	var coverDiv = createCoverDiv();
	coverDiv.style.opacity = 0.0;
	coverDiv.style.top = document.documentElement.scrollTop + "px";
	coverDiv.style.visibility = "visible";

	// Create a message-box DIV if we don't already have one
	var messageBox = createDialog("messageBox2", message, title ? title : "Notice", 2);

	// Set up "Yes" click handler
	if (okText)
	{
		document.getElementById("mBOK").value = okText;
	}
	document.getElementById("mBOK").onclick = function()
	{
		hideDialog('messageBox2');
		if (onClose)
		{
			onClose();
		}
	};

	// Fade everything in	
	startFadeIn(coverDiv, 0.5, 0.1);
	startFadeIn(messageBox, 1.0, 0.2);
}
