

/* Dependent on common.js */
var moveableObj = null;
var offsetX = 0;
var offsetY = 0;
var enableMove = false;
var isModal = false;

function resizeIframe() {
	if (parent == window) return false;
	var ifrmWin = parent.frames["featuredPlan"];
	if (ifrmWin) {
		var ifrm;
		if (parent.window.document.getElementById) {
			ifrm = parent.window.document.getElementById("featuredPlan");
		}
		else {
			if (parent.window.document.all) {
				ifrm = parent.window.document.all["featuredPlan"];
			}
		}
		if (ifrm) {
			ifrm.style.height = "auto"; 
			ifrm.style.height = getHeight() + "px";
		}
	}
}

function resizeIframeDialog() {
	if (parent == window) {return false};
	
	var ifrm;
	
	if (parent.window.document.getElementById) {
		ifrm = parent.window.document.getElementById("dialogWin");
	}
	else if (parent.window.document.all) {
			ifrm = parent.window.document.all["dialogWin"];
	}
	
	if (parent && parent.setDialogTitle) {
		parent.setDialogTitle(document.title);
	}
	
	//The below script was overriding the dimensions set in showdialog	
	/*
	if (ifrm) {
		ifrm.style.height = "auto"; 
		ifrm.style.height = getHeight() + "px";
		ifrm.style.width = "auto";
		ifrm.style.width = getWidth() + "px";
		
	}*/

}

function showDialog(url)
{
	//hideDialog();
	var dialog = document.getElementById ("dialog");	
	moveableObj = dialog;
	
	var w = 300;
	var h = 300;
	
	/*if (url.indexOf('login') != -1) {
		w = 500;
		h = 150;	
	}
	else if (url.indexOf('help') != -1) {
		var w = 300;
		var h = 300;
	}*/
	
	if (url.indexOf('HelpLarge') != -1) {
		w = 500;
		h = 500;
	}
	else if (url.indexOf('help') != -1) {
		w = 300;
		h = 300;
	}

	/*For task 2360 - if an eplans page is displayed inside an iframe of another site,
	  then we need to fix the dialog iframe to display in the left edge of the eplans
	  iframe, otherwise the whole help dialog may not show in the iframe of the other site.
	  We can determine if the current eplans page is in the iframe of another site because
	  the iframe's url will contain 'show-results.iniframe'.
	*/	
	var eplansIframe = parent.frames["eplans"];
	
	if(eplansIframe)
	{		//The current eplans page is an iframe inside another site, so we have to display
		//the dialog iframe inside it.  To make sure the dialog iframe is visible, display
		//it starting from the left edge of its parent (which is the eplans iframe), and the top based
		//on the relative position of scrollTop and the parent.
		positionFromLeftEdge(dialog, w, h);
	}
	else if(w > 300)
	{
		positionFromBottomLeftCenterWell(dialog, w, h);
	}
	else
	{
		positionCenter(dialog);
	}
	
	setDialogTitle("Loading...");
	
	dialog.style.display = "block";
	//dialog.style.height = h + 20 + 'px';
	dialog.style.width = w + 'px';
	
	var ifrmWin = window.frames["dialogWin"];
	ifrmWin.location.href = url;
	
	var ifrm;
	
	
	if (document.getElementById) {
		ifrm = document.getElementById("dialogWin");
	}
	else if (document.all) {
		ifrm = document.all["dialogWin"];
	}
	
	if (ifrm)
	{
		ifrm.style.height = h + 'px';
		ifrm.style.width = w + 'px';
	}
	
	if (!isModal) negotiateSelects(moveableObj, true, false);
}


function hideDialog()
{
	var ifrmWin = window.frames["dialogWin"];
	//if (ifrmWin) {
	//	ifrmWin.document.write('');
	//}
	
	var dialog = getEl ("dialog");
	hideDialogEl(dialog);
}

function hideDialogEl(dialog)
{
	if (!dialog) {
		alert("No dialog");
		return;
	}
	
	var dialogBody = document.getElementById("dialogBody");
	
	dialog.style.display = "none";
	
	moveableObj = dialog;
	
	if (!isModal) negotiateSelects(moveableObj, true, false);
}

function hideParentDialog()
{
	var dialog = getParentEl("dialog");
	hideDialogEl(dialog);	
}

function setDialogTitle(title)
{
	var dlgTitle;	
	
	if (document.getElementById) {
		dlgTitle = top.document.getElementById("dialogTitle");
	}
	else if (document.all) {
		dlgTitle = document.all["dialogTitle"];
	}
	
	if (dlgTitle)
	{
		dlgTitle.innerHTML = title;
	}
}

function downDialog(e) 
{
	if (!e) 
	{
		e = event;
	}
	enableMove = true;
	offsetX = getOffsetX(e);
	offsetY = getOffsetY(e);
	
	var tg = (e.target) ? e.target : e.srcElement;

	moveableObj = selectDialog(tg);
	return false;
}


function selectDialog(el) 
{
	if (el && el.onmousedown == downDialog) return el;
	else if (el) return (selectDialog(getParentElement(el)));
	return moveableObj;
}


function upDialog(e) 
{
	if (!e) 
	{
		e = event;
	}
	enableMove = false;
	return false;
}


function moveDialog(e) 
{
	if (!e) 
	{
	 e = event;
	}
	if (enableMove && moveableObj) 
	{
		moveableObj.style.top = e.clientY + getScrollTop() - offsetY + 'px';
		moveableObj.style.left = e.clientX + getScrollLeft() - offsetX + 'px';
		if (!isModal) negotiateSelects(moveableObj, true, false);
	}
	return false;
}

function autoShowDialog(idString)
{
	var el = getEl(idString);

	if (el != null && self == top && top.location.href.indexOf('Action.Save') != -1) 
	{
		var reUriInJavaScript = /.*'([^']*)'.*/i;
		var href = el.getAttribute("href");
		var uri = href.replace(reUriInJavaScript, "$1");
		
		showDialog(uri);
	}
}