/* $Id: */


var oDialog, doc, srcEl, closeOnEscKey = true, closeOnBodyClick = false, iframeIEHack;
var dialogProperties = new Array("position", "top", "left", "height", "width", "srcElement", "modal", "draggable",
								 "title", "closeButton", "closeOnEscKey", "transitionType", "transitionInterval","closeOnBodyClick");

/**
* <h3>Example:</h3>
*<br/>	Following are the different ways the showDialog method can be used:
*<br/>	1. showDialog( html);
*<br/>	2. showDialog( html, "position=relative" );
*<br/>	3. showDialog( html, "position=absolute, top=50, left=400, height=200, width=400",event);
*<br/>	4. showDialog( html, "position=relative, top=10, left=-30" );
*<br/>	5. showDialog( html, "position=relative, modal=yes, closeOnEscKey=no" );
*<br/>	6. showDialog( html, "position=relative, transitionType=boxIn, transitionInterval=20" );
*<br/>	
*<br/>
*<br/>	@param sHTML 	Mandatory. String that specifies the HTML content to display. 	
*<br/>		   
*<br/>	@param sFeatures Optional. This String parameter is a list of items separated by commas.
*<br/>			Each item consists of an option and a value, separated by an equals sign
*<br/>			(for example, "position=relative, modal=yes"). 
*<br/>
*<br/>
*<br/>
*<br/><h3> The following features are supported.</h3>
*<br/>	
*<br/>		 	<h3>position = { absmiddle | absolute | relative }</h3>	Specifies how the dialog should be positioned.
*<br/>									The "absmiddle" type displays the dialog exactly
*<br/>									at the center of the page. The "absolute" type
*<br/>									displays the dialog based on the "top" and "left"
*<br/>									(mentioned below) parameters provided. The "relative"
*<br/>									type displays it exactly below the element from
*<br/>									where the dialog is opened or from the element id
*<br/>									specified in "srcElement" parameter. [absmiddle]
*<br/>
*<br/>									Note:- "relative" type also accepts "top" and 
*<br/>									"left" parameters where the top and left values are
*<br/>									relative to the element from where the dialog is
*<br/>									opened or from the element id specified in
*<br/>									"srcElement" parameter.
*<br/>
*<br/>			<h3>srcEl = id</h3>					Specifies the HTML element from where the Dialog
*<br/>									should be positioned relatively. The value given is
*<br/>									the HTML element's id.
*<br/>							
*<br/>			<h3>top = number</h3>					Specifies the top position of the dialog, in pixels.
*<br/>									This value is relative to the upper-left corner of
*<br/>									the screen.
*<br/>
*<br/>			<h3>left = number</h3>					Specifies the left position of the dialog, in pixels.
*<br/>									This value is relative to the upper-left corner of
*<br/>									the screen.					
*<br/>			
*<br/>			<h3>height = number</h3>					Specifies the height of the dialog, in pixels.
*<br/>									If not specified, the default value is the content's
*<br/>									height.					
*<br/>
*<br/>			<h3>width = number</h3>					Specifies the width of the dialog, in pixels.
*<br/>									If not specified, the default value is the content's
*<br/>									width.
*<br/>
*<br/>			<h3>modal = { yes | no }</h3>				Specifies whether the dialog should be opened as a
*<br/>									modal dialog or not. [no]
*<br/>			
*<br/>			<h3>title = text</h3>			Specifies the title text to be displayed.
*<br/>									Note: If title is not specified and also if 
*<br/>									closeButton is set as "no", then the dialog box
*<br/>									displays only the HTML content passed and the
*<br/>									surrounding box won't be displayed.
*<br/>
*<br/>			<h3>closeButton = { yes | no }</h3>	Specifies whether the close button should be 
*<br/>									displayed or not. If the value is "no" the button
*<br/>									won't be displayed, vice versa. [yes]
*<br/>
*<br/>			<h3>closeOnEscKey = { yes | no }</h3>	Specifies whether the dialog should be closed or not
*<br/>									when "ESC" key is pressed. [yes]
*<br/>
*<br/>			<h3>draggable = { yes | no }</h3>			Specifies whether dragging/moving the Dialog Box is
*<br/>									allowed or not. [yes]
*<br/>
*<br/>			<h3>transitionType = { boxIn }</h3>	Specifies the type of transition effect to be used
*<br/>									when the dialog is displayed. [boxIn]
*<br/>
*<br/>			<h3>transitionInterval = number</h3>	Specifies the transition interval, in milliseconds.
*<br/>									[10]
*<br/>
*<br/>
*<br/>
*<br/>			<h3>closeOnBodyClick = { yes | no }</h3>Specifies whether to close on click [no]. 
*<br/>									[10]
*<br/>
*<br/>
*<br/>Road Map:
*<br/>
*<br/>	Following are the features that will be provided in future:
*<br/>	
*<br/>	1. Restrict Dragging
*<br/>	2. Resizeable Dialogs
*<br/>	3. Close Button hiding
*<br/>	4. Different types of transition effects will be provided
*<br/>
*<br/>**/

function showDialog(content, features, callBackFunc) {
	if (typeof(features) == "undefined") var features = "position=absmiddle";
	features = features.split(",");
	
	var featurePresent;
	for (var i = 0; i < dialogProperties.length; i++) {
		featurePresent = false;
		for (var j = 0; j < features.length; j++) {
			if (features[j].indexOf(dialogProperties[i]) >= 0) {
				featurePresent = true;
				break;
			}
 		}
		
		self["dialog_" + dialogProperties[i]] = (featurePresent) ? features[j].substr(features[j].indexOf("=") + 1, features[j].length).trim() : "undefined";
	}
	
	if (document.getElementById("_DIALOG_LAYER") == null) {
		oDialog = document.createElement("DIV");
		oDialog.id = "_DIALOG_LAYER";
		document.body.appendChild(oDialog);
	} else {
		oDialog = document.getElementById("_DIALOG_LAYER");
        oDialog.innerHTML = "";
        oDialog.style.width = "";
        oDialog.style.height = "";
		closeDialog();
	}
	
	var content = '<div id="_DIALOG_CONTENT">' + content + '</div>';
	
	var box = '<table class="DialogBox" border="0" cellspacing="0" cellpadding="0"><tr><td class="boxTL"><img src="/images/spacer.gif" width="24" height="1"></td>';

	if (dialog_draggable != "undefined" && dialog_draggable == "no") box += '<td class="boxHeader">';
	else box += '<td class="boxHeader drag" onMouseDown="captureDialog(event)">';

	if (dialog_title != "undefined") {
		if (dialog_title.charAt(0) == "'" && dialog_title.charAt(dialog_title.length - 1) == "'") 
			dialog_title = dialog_title.substr(1, dialog_title.length - 2);
		if (dialog_title.trim().length == 0) 
			dialog_title = "&nbsp;";
	} else dialog_title = "&nbsp;";

	box += dialog_title + '</td><td class="boxCtrlButtonPane">';
	
	if (dialog_closeButton != "undefined" && dialog_closeButton == "no") box += '&nbsp;</td>';
	else box += '<input type="button" class="closeButton" onClick="closeDialog()"></td>';
	
	box += '<td class="boxTR"><img src="/images/spacer.gif" width="24" height="1"></td></tr><tr><td class="boxML"><img src="/images/spacer.gif" width="1" height="1"></td><td colspan="2" class="boxContent">' + content + '</td><td class="boxMR"><img src="/images/spacer.gif" width="1" height="1"></td></tr>';
	box += '<tr><td class="boxBL"><img src="/images/spacer.gif" width="1" height="1"></td><td class="boxBC" colspan="2"><img src="/images/spacer.gif" width="1" height="1"></td><td class="boxBR"><img src="/images/spacer.gif" width="1" height="1"></td></tr></table>';
	
	oDialog.style.visibility = "visible";
	
	var showInBox = true;
	if (dialog_closeButton != "undefined")
		if (dialog_title == "&nbsp;" && dialog_closeButton == "no") showInBox = false;

	if (showInBox) 
		oDialog.innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td height='100%' style='display:block'>" + box + "</td></tr></table>";	
	else
		oDialog.innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td height='100%' style='display:block'>" + content + "</td></tr></table>";	

	oDialog.style.position = "absolute";
	oDialog.style.left = "-1000px";
	oDialog.style.top = "-1000px";
	oDialog.style.zIndex = "100";

	var scriptTags = oDialog.getElementsByTagName("SCRIPT");
	for (var i = 0; i < scriptTags.length; i++) {
		var scriptTag = document.createElement("SCRIPT");
		scriptTag.type = "text/javascript";
		scriptTag.language = "javascript";
		if (scriptTags[i].src != "") { scriptTag.src = scriptTags[i].src;}
		scriptTag.text = scriptTags[i].text;
		
		if (typeof document.getElementsByTagName("HEAD")[0] == "undefined") {
			document.createElement("HEAD").appendChild(scriptTag)
		} else {
			document.getElementsByTagName("HEAD")[0].appendChild(scriptTag);
		}			
	}

	if (browser_opera) {
		var temp = content;
		var styleTags = oDialog.getElementsByTagName("STYLE");
		for (var i = 0; i < styleTags.length; i++) {
			styleTags[i].innerHTML = temp.substring(temp.indexOf("<style>") + 7, temp.indexOf("</style>") - 1);
			temp = temp.substring(temp.indexOf("</style>") + 8, temp.length);
		}
	}

	if (dialog_width != "undefined") {
		if (browser_ie)	oDialog.childNodes[0].style.width = parseInt(dialog_width) + "px";
		else if (browser_nn4 || browser_nn6) oDialog.childNodes.item(0).style.width = parseInt(dialog_width) + "px";
	}
	
	if (dialog_height != "undefined") {
		if (browser_ie) oDialog.childNodes[0].style.height = parseInt(dialog_height) + "px";
		else if (browser_nn4 || browser_nn6) oDialog.childNodes.item(0).style.height = parseInt(dialog_height) + "px";
	}

	oDialogContent = getObj("_DIALOG_CONTENT");
	
	var left = 0, top = 0;

	if (browser_opera) {
		if (dialog_width != "undefined") {
			oDialogContent.style.width = parseInt(dialog_width) + "px";
		} else {
			oDialogContent.style.width = oDialogContent.offsetWidth + "px";
			oDialog.style.width = oDialogContent.offsetWidth + "px";
		}
	}

	if (browser_nn4 || browser_nn6 || browser_ie) {
		if (dialog_width != "undefined") oDialogContent.style.width = parseInt(dialog_width) + "px";
		else oDialogContent.style.width = (oDialogContent.offsetWidth + 20) + "px";
	}

	if (dialog_height != "undefined") {
		if (browser_ie && (parseInt(dialog_height) < oDialogContent.offsetHeight)) left = -15;
		oDialogContent.style.height = parseInt(dialog_height) + "px";
    }
    oDialogContent.style.overflow = "auto";

	var width = oDialog.offsetWidth;
	var height = oDialog.offsetHeight;
	doc = findDocDim();

	if (dialog_closeOnEscKey != "undefined" && dialog_closeOnEscKey == "no") closeOnEscKey = false;
	else closeOnEscKey = true;
	
	if (dialog_closeOnBodyClick != "undefined" && dialog_closeOnBodyClick == "yes") closeOnBodyClick = true;
	else closeOnBodyClick = false;
	
	if (!browser_opera) {
		if (dialog_modal != "undefined" && dialog_modal == "yes") freezeBackground();
		else if (document.getElementById("FreezeLayer") != null) document.body.removeChild(document.getElementById("FreezeLayer"));
	}

	if (dialog_left != "undefined")	left += parseInt(dialog_left);
	if (dialog_top != "undefined") top += parseInt(dialog_top);

	if (dialog_position != "undefined" && dialog_position == "relative") {
		if (dialog_srcElement != "undefined") srcEl = getObj(dialog_srcElement);
		else if (srcEl == null) srcEl = document.body;
		
		if ((width - (doc.width - findPosX(srcEl))) + scrollConst > 0)
			left += findPosX(srcEl) + ((srcEl.offsetWidth) ? srcEl.offsetWidth : 0) - width;
		else
			left += findPosX(srcEl);

		if ((height - (doc.height - findPosY(srcEl))) + scrollConst > 0)
			top += findPosY(srcEl) - height;
		else
			top += findPosY(srcEl) + ((srcEl.offsetHeight) ? srcEl.offsetHeight : 0) + 2;
	} else if (dialog_position != "undefined" && dialog_position == "absolute") {
		left += document.body.scrollLeft;
		top += document.body.scrollTop;		
	} else {
		left = (doc.width / 2 ) - (width / 2) + document.body.scrollLeft;
		top = (doc.height / 2) - (height / 2) + document.body.scrollTop;		
	}
	
	left = (left > 0) ? left : 0;
	top = (top > 0) ? top : 0;

	if (dialog_transitionType != "undefined") {
		if (dialog_transitionInterval == "undefined") dialog_transitionInterval = 10;	
		MC_Effect.init( 
			{ 
				type : dialog_transitionType, speed : dialog_transitionInterval ,
				layerId : "_DIALOG_LAYER", layerTop : top, layerLeft : left
			 } 
		);
		MC_Effect.display();		
	} else {
		oDialog.style.left = parseInt(left) + "px";
		oDialog.style.top = parseInt(top) + "px";
	}

	if (browser_ie && !browser_opera) {
		iframeIEHack = document.createElement("IFRAME");
		iframeIEHack.scrolling = "no";
		iframeIEHack.frameBorder = 0;
                if(window["CONTEXT_PATH"] != null)
                {
                   iframeIEHack.src= CONTEXT_PATH + "/framework/html/blank.html";
                }
		iframeIEHack.style.position = "absolute";
		iframeIEHack.style.zIndex = "98";
		iframeIEHack.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
		
		if (dialog_modal != "undefined" && dialog_modal == "yes") {
			iframeIEHack.style.width = document.getElementById("FreezeLayer").style.width;
			iframeIEHack.style.top = "0px";
			iframeIEHack.style.left = "0px";
		} else { 
			iframeIEHack.style.width = oDialog.offsetWidth + "px";
			iframeIEHack.style.height = oDialog.offsetHeight + "px";
			iframeIEHack.style.top = parseInt(top) + "px";
			iframeIEHack.style.left = parseInt(left) + "px";
		}
		
		document.body.appendChild(iframeIEHack);
	}
	
/*
	scrollEnd = (height - (doc.height - (top - document.body.scrollTop))) + scrollConst;
	if (scrollEnd > 0) {
		cnt = 0;
		scrollInterval = setInterval("scrollPage()", 7);
	}
*/
	
	if (!browser_opera) {
		if (dialog_modal != "undefined" && dialog_modal == "yes") {
			document.getElementById("FreezeLayer").style.height = (document.body.offsetHeight + document.body.scrollHeight) + "px";
			if (browser_ie) iframeIEHack.style.height = document.getElementById("FreezeLayer").style.height;
		}
	}
	
	if (callBackFunc){	callBackFunc();}
	
/*
	if (dialog_closeOnBodyClick == "yes") {
		
		if (browser_ie) {
			window.event.cancelBubble = true;
		} else if (browser_nn6) {
			event.stopPropagation();
		}
		document.onclick = closeDialog;
	}
*/

	return oDialog;
}

/**
 *@private
 */
function freezeBackground() {
	var oFreezeLayer = document.createElement("DIV");
	oFreezeLayer.id = "FreezeLayer";
	oFreezeLayer.className = "freezeLayer";
	oFreezeLayer.style.width = "100%";
	oFreezeLayer.style.zIndex = "99";

	document.body.appendChild(oFreezeLayer);
}

var diffLeft=0, diffTop=0;
/**
 *@private
 */
function captureDialog(ev) {
	oDialog.style.cursor = "move";
	
	if (browser_ie) {
		diffLeft = window.event.clientX + document.body.scrollLeft - parseInt(findPosX(oDialog));
		diffTop = window.event.clientY + document.body.scrollTop - parseInt(findPosY(oDialog));
	} else if (browser_nn4 || browser_nn6) {
		diffLeft = ev.pageX - parseInt(findPosX(oDialog));
		diffTop = ev.pageY - parseInt(findPosY(oDialog));
	}
	
	document.onmousemove = moveDialog;
	document.onmouseup = releaseDialog;
}

/**
 *@private
 */
function moveDialog(ev) {
	clearTextSelection();
	if (browser_ie) {
		var left = window.event.clientX + document.body.scrollLeft - diffLeft;
		var top = window.event.clientY + document.body.scrollTop - diffTop;
		
		left = (left >= 0) ? left : 0;
		top = (top >= 0) ? top : 0;
	
		if (document.getElementById("FreezeLayer") != null || browser_opera) {
			oDialog.style.left = left + "px";
			oDialog.style.top = top + "px";
		} else {
			oDialog.style.left = iframeIEHack.style.left = left + "px";
			oDialog.style.top = iframeIEHack.style.top = top + "px";
		}
	} else if (browser_nn4 || browser_nn6) {
		oDialog.style.left = ((ev.pageX - diffLeft > 0) ? ev.pageX - diffLeft : 0) + "px";
		oDialog.style.top = ((ev.pageY - diffTop > 0) ? ev.pageY - diffTop : 0) + "px";	
	}
}


/**
 *@private
 */
function releaseDialog() {
	oDialog.style.cursor = "default";
	document.onmousemove = null;
	document.onmouseup = null;
}

function closeDialog(callBackFunc) {
	if (oDialog != null && oDialog.style.visibility != "hidden") {
		oDialog.style.visibility = "hidden";
		if (document.getElementById("FreezeLayer") != null) document.body.removeChild(document.getElementById("FreezeLayer"));
		if (browser_ie && !browser_opera) {
			document.body.removeChild(iframeIEHack);
			iframeIEHack = null;
		}
		closeOnBodyClick = false;

		if (typeof callBackFunc != "undefined")	callBackFunc();
	}
	if(document.getElementById('_CUSTOMALERTFRAME') != null) {
		document.getElementById("_CUSTOMALERTFRAME").src = '/framework/html/blank.html';
	}

/*
	if(document.onclick == closeDialog)
	{
		document.onclick = null;
	}
*/
}

document.onkeydown = function(ev) {
	if (browser_ie) var keyCode = window.event.keyCode;
	else if (browser_nn4 || browser_nn6) var keyCode = ev.which;
	
	if (keyCode == 27 && closeOnEscKey == true && oDialog != null && oDialog.style.visibility != "hidden")
		closeDialog();
}

document.onmousedown = function(ev) {
	if (browser_ie) {
		srcEl = window.event.srcElement;		
		var x = window.event.x;
		var y = window.event.y;
	} else if (browser_nn4 || browser_nn6) {
		srcEl = ev.target;
		var x = ev.pageX;
		var y = ev.pageY;
	}

	if (typeof closeOnBodyClick != "undefined" && closeOnBodyClick == true && oDialog != null && oDialog.style.visibility != "hidden") {
		if ((x < findPosX(oDialog) || x >= findPosX(oDialog) + oDialog.offsetWidth) || (y < findPosY(oDialog) || y >= findPosY(oDialog) + oDialog.offsetHeight))
			closeDialog();
	}
}

var scrollEnd = 0, cnt = 0;

/**
 *@private
 */
function scrollPage() {
	if (cnt <= scrollEnd) {
		document.body.scrollTop += 10;
		cnt += 10;
	} else {
		scrollEnd = cnt = 0;
		clearInterval(scrollInterval);
	}
}


/**
 * Used to show the contents of the url in a dialog.
 * @param sURL		Mandatory. String that specifies the URL to display.
 * @param Features	Optional. Sames as mentioned in showDialog() function
 * <h3>Example:</h3>
 *  showURLInDialog( "InstantFeedback.cc", "position=relative" );	
 */
function showURLInDialog(url, features) {
	var xmlhttp = getXMLHttpRequest();
        if(parent["USESUBREQESTINDIALOG"])
        {
           if(url.indexOf("SUBREQUEST") == -1)
           {                            
              url = getURLSuffixed(url);
              url += "SUBREQUEST=true";
           }
        }

	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			showDialog(xmlhttp.responseText, features);
		}
	}	
	xmlhttp.send(null);
}
