//------------------------------------------------------------------------------
//
//	This group of functions are executed in the page that hosts the dialog frames
//
//------------------------------------------------------------------------------
var DLG_dialogLevel		= 0;
var DLG_captionHeight	= 15;

function DLG_ShowDialog_Impl(url, OnCloseFn, bOpaque, DoOnExitFn)
{
	DLG_dialogLevel += 1;
	
	// Text that we might one day want to localize
	var kDialogLoading	= "Dialog loading...";
	var kClose				= "Close";
	var kInfoAndHelp		= "Info &amp; help";

	var html;
	html = '<div id="divDialogBackground' + DLG_dialogLevel + '"';
	html += 'style="TOP: 0; LEFT: 0; WIDTH: 1000; HEIGHT: 1000; Z-INDEX: 9; ';
	html += 'POSITION: ABSOLUTE; DISPLAY: NONE;background-color: gray; filter: alpha(opacity=50);">';
	var objBgd;
	objBgd = document.createElement(html);
	objBgd = document.body.appendChild(objBgd);
	
	var InitCaption = kDialogLoading; 	// will be set when dialog initializes
	
	html = '<div id="divDialog' + DLG_dialogLevel + '" ';
	html += 'style="TOP: 100; LEFT: 200; HEIGHT: 100; WIDTH: 300; Z-INDEX: 10; ';
	html += 'POSITION: absolute; DISPLAY: none; background-color: white;';
	if (bOpaque == false)
		html += ' filter: alpha(opacity=90);';
	html += '">';
	var objDlg;
	objDlg = document.createElement(html);
	
	html = '<table border="1" cellpadding="2" cellspacing="0" rules="none" class="DialogCaptionTable">';
	html += '<tr height="' + DLG_captionHeight + '">';
	html += '<td class="DialogCaptionText">';
	html += '<div id="divDialogCaption' + DLG_dialogLevel + '" style="cursor: move;" onselectstart="return false;" onmousedown="DLG_onmousedownInCaption();">' + InitCaption + '</div>';
	html += '</td>';
	html += '<td valign="middle" align="right">';
	html += '<img id="imgHelp1' + DLG_dialogLevel + '" style="display: none;" src="/mapscapev3/1033/images/info-i-grey-14h.gif" ';
	html += 'width="8" height="14" border="0" alt="' + kInfoAndHelp + '" style="cursor: hand;" onclick="DLG_ShowHelp(\'\');">';
	html += '<img id="imgHelp2' + DLG_dialogLevel + '" style="display: none;" src="/mapscapev3/1033/images/tp.gif" width="4" height="1" border="0">';
	html += '<img src="/mapscapev3/1033/images/mapformimages/Win2000CloseButton.gif" ';
	html += 'width="16" height="14" border="0" alt="' + kClose + '" style="cursor: hand;" onclick="DLG_CloseDialog_Impl(\'\');"></td>';
	html += '</tr>';
	html += '</table>';

	html += '<table border="1" cellpadding="0" cellspacing="0" width="100%" height="100%"';
	if (bOpaque == false)
		html += ' filter: alpha(opacity=90);';
	html += '">';

	html += '<tr>';
	html += '<td valign="middle" align="center">';
	html += '<div id="divDialogContents' + DLG_dialogLevel + '">';
	html += '<iframe name="divDialogFrame' + DLG_dialogLevel + '" id="divDialogFrame' + DLG_dialogLevel + '" ';
	html += 'src="' + url + '" scrolling="auto" width="100%" height="100%" ';
	html += 'marginwidth="0" marginheight="0" frameborder="0" align="center">';
	html += '</iframe>';
	html += '</div>';
	html += '</td>';
	html += '</tr>';
	html += '</table>';

	objDlg.innerHTML = html;
	objDlg = document.body.appendChild(objDlg);
	objDlg.OnCloseFn = OnCloseFn;
	objDlg.DoOnExitFn = DoOnExitFn;
	
	// Get window size
	var wid = document.body.clientWidth;
	var hgt = document.body.clientHeight;
		
	// center dialog horizontally
	objDlg.style.pixelLeft		= (wid - objDlg.style.pixelWidth)/2;

	// center dialog vertically (top third of screen)
	objDlg.style.pixelTop		= (hgt - objDlg.style.pixelHeight)/3;
	
	objDlg.style.display			= '';
	objDlg.style.visibility		= 'visible';

	objBgd.style.display			= '';
	objBgd.style.pixelHeight	= (document.body.scrollTop		> 0) ? document.body.scrollHeight	: document.body.clientHeight;
	objBgd.style.pixelWidth		= (document.body.scrollLeft	> 0) ? document.body.scrollWidth		: document.body.clientWidth;

	objBgd.style.zIndex			= 10;
	
	return document.getElementById("divDialogFrame" + DLG_dialogLevel).contentWindow; 	
} // DLG_ShowDialog_Impl
//------------------------------------------------------------------------------
function DLG_CloseDialog_Impl(retVal)
{
	var dlg = document.getElementById('divDialog'			   + DLG_dialogLevel);
	var bgd = document.getElementById('divDialogBackground'  + DLG_dialogLevel);
	var cnt = document.getElementById('divDialogContents'    + DLG_dialogLevel);
	var cap = document.getElementById('divDialogCaption'     + DLG_dialogLevel);
	
	// perform any on exit functions
	if (dlg.DoOnExitFn!='')
	{
		var frm = document.getElementById('divDialogFrame'+DLG_dialogLevel);
		if (frm && frm.contentWindow)
		{
			var frmWin = frm.contentWindow.window;
			if (eval('frmWin && frmWin.'+dlg.DoOnExitFn))
				eval('frmWin.'+dlg.DoOnExitFn+'();');
		}
	}	

	// reduce nested dialog level
	DLG_dialogLevel -= 1;
	
	document.body.removeChild(dlg);
	document.body.removeChild(bgd);

//	// hide the dialog and its opaque background
//	dlg.style.display	= 'none';
//	bgd.style.display	= 'none';	

//	// kill the content IFRAME
//	cnt.innerHTML = '';
//	// kill the caption
//	cap.innerHTML = '';

	// pass the results back to the caller
	if (dlg.OnCloseFn)
		dlg.OnCloseFn(retVal);
} // DLG_CloseDialog_Impl
//------------------------------------------------------------------------------
function DLG_ResizeDialog_Impl(w, h)
{
	var d = document.getElementById('divDialog' + DLG_dialogLevel);

	var l = (document.body.scrollWidth - w)/2;
	var t = document.body.scrollTop + ((document.body.clientHeight - (h + DLG_captionHeight))/2); // position to the middle of the screen
	d.style.pixelLeft		= l;
	d.style.pixelTop		= t;
	d.style.pixelWidth	= w;
	d.style.pixelHeight	= h;
	
	if (d.style.pixelLeft < 0) d.style.pixelLeft = 0;
	if (d.style.pixelTop  < 0) d.style.pixelTop  = 0;
	if ((d.style.pixelTop + d.style.pixelHeight) > (document.body.scrollTop + document.body.clientHeight-DLG_captionHeight-20))
	{
		d.style.pixelTop		= document.body.scrollTop;
		d.style.pixelHeight	= (document.body.clientHeight-DLG_captionHeight-20);	
	}
	if ((d.style.pixelLeft + d.style.pixelWidth) > (document.body.scrollLeft + document.body.clientWidth-20))
	{
		d.style.pixelLeft		= document.body.scrollLeft+10;
		d.style.pixelWidth	= (document.body.clientWidth-20);	
	}
} // DLG_ResizeDialog_Impl
//------------------------------------------------------------------------------
function DLG_SetCaption_Impl(caption)
{
	document.getElementById('divDialogCaption'  + DLG_dialogLevel).innerHTML = caption;
	
	DLG_SetupHelp();
} // DLG_SetCaption_Impl
//------------------------------------------------------------------------------
function DLG_onmousedownInCaption_Impl()
{
	var w = window.event.srcElement;
	// point to divDialogn
	w = w.parentElement.parentElement.parentElement.parentElement.parentElement;

	w.DLG_mouseMoveX	= window.event.screenX;
	w.DLG_mouseMoveY	= window.event.screenY;
				
	document.body.setCapture(true);
	document.DLG_mouseMoveOb = w;
	document.DLG_onmousemove = document.onmousemove;
	document.DLG_onmouseup   = document.onmouseup;
	document.onmousemove = DLG_onmousemoveInCaption;
	document.onmouseup   = DLG_onmouseupInCaption;

	window.event.returnValue = false;
	window.event.cancelBubble = true;				
	
} // DLG_onmousedownInCaption_Impl
//------------------------------------------------------------------------------
function DLG_onmousemoveInCaption()
{
	var w = document.DLG_mouseMoveOb;
	if (w != null)
	{
		w.style.pixelLeft += window.event.screenX-w.DLG_mouseMoveX;
		w.style.pixelTop  += window.event.screenY-w.DLG_mouseMoveY;
		if (w.style.pixelTop < 0)
			w.style.pixelTop = 0;
		if (w.style.pixelTop > (document.body.clientHeight-20))
			w.style.pixelTop = document.body.clientHeight-20;
			
		w.DLG_mouseMoveX = window.event.screenX;
		w.DLG_mouseMoveY = window.event.screenY;		
	}
} // DLG_onmousemoveInCaption
//------------------------------------------------------------------------------
function DLG_onmouseupInCaption()
{
	var w = document.DLG_mouseMoveOb;
	if (w != null)
	{
		document.DLG_mouseMoveOb = null;
		document.onmousemove = document.DLG_onmousemove;
		document.onmouseup   = document.DLG_onmouseup;
		document.releaseCapture();
	}
} // DLG_onmouseupInCaption
//------------------------------------------------------------------------------
function DLG_SetupHelp()
{
	var pageName = document.getElementById("divDialogFrame" + DLG_dialogLevel).contentWindow.document.location.href;
	
	var xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
	xmlDoc.async = false;
	var cmd;
	cmd = '<command cmd="setuphelp">'
								+ '<pagename>'  +	'<![CDATA[' + pageName + ']]>' + '</pagename>'
								+ '</command>';
	
		
	if (!xmlDoc.loadXML(cmd))
	{
		alert ("Error: DLG_SetupHelp failed calling loadXML()");
		return;	
	}
	
	var url = "HelpSupport.asp";	// needs a redirect if invoked from directory such as InsertExpress?
	if (url.indexOf("InsertExpress") != -1)
		url = "../" + url;
	var oXML	= DLG_AJAX_CallServerSynchronous2 (url, xmlDoc.xml, false);
	xmlDoc	= null;
	if (oXML == null)
		// e.g. 404 since HelpSupport.asp is not found.
		return;
		
	var errorNode = oXML.documentElement.selectSingleNode("/error");
	if (errorNode)
	{
		alert ('Error: in DLG_SetupHelp: ' + errorNode.getAttribute("desc"));
		return;
	}
	var helpUrl   = oXML.documentElement.selectSingleNode("/result").text;
	if (helpUrl.length > 0)
	{
		// setup the help
		document.getElementById('imgHelp1'  + DLG_dialogLevel).style.display = "";
		document.getElementById('imgHelp2'  + DLG_dialogLevel).style.display = "";	
		document.getElementById('imgHelp1'  + DLG_dialogLevel).onclick = new Function("DLG_ShowHelp('" + helpUrl + "');");
	}
} // DLG_SetupHelp
//------------------------------------------------------------------------------
function DLG_ShowHelp(url)
{
	DLG_NewWindow(url, 'TacHelp', 200, 200, 'yes');
} // DLG_ShowHelp
//------------------------------------------------------------------------------
function DLG_NewWindow(mypage, myname, w, h, scroll)
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	var winprops = 'height='+h
				+',width='+w
				+',top='+wint
				+',left='+winl
				+',scrollbars='+scroll
				+',resizable'
	top.newWin = window.open(mypage, myname, winprops)
	top.newWin.focus();
} // DLG_NewWindow
//------------------------------------------------------------------------------
function DLG_AJAX_CallServerSynchronous2(url, postData, bAsForm)
{
	var objXmlHttpSync = DLG_AJAX_GetXmlHttp();
	if (objXmlHttpSync)
	{
		objXmlHttpSync.open("POST", url, false);	// false = sync
		if (bAsForm)
			objXmlHttpSync.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		objXmlHttpSync.send(postData);
		return DLG_AJAX_ProcessResponse(objXmlHttpSync);
	}
	return null;
} // DLG_AJAX_CallServerSynchronous2
//------------------------------------------------------------------------------
function DLG_AJAX_GetXmlHttp()
{
	var x = null;
	try
	{
		x = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try{x = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e)
	{
		x = null;
	}
	}
	if(!x&&typeof XMLHttpRequest != "undefined")
	{
		x = new XMLHttpRequest();
	}
	return x;
} // DLG_AJAX_GetXmlHttp
//------------------------------------------------------------------------------
function DLG_AJAX_ProcessResponse(objXmlHttp)
{
	var oXML = objXmlHttp.responseXML;
	// strip off any ASP debug metadata
	// ("real" response, being XML, starts with <?xml...
	var i = objXmlHttp.responseText.indexOf('<result');
	if (i < 0)
		i = objXmlHttp.responseText.indexOf('<progress');
	if (i < 0)
		i = objXmlHttp.responseText.indexOf('<Data');	// returned by RW_ViewData_Svr.asp
	if (i < 0)
		i = objXmlHttp.responseText.indexOf('<error');
	if (i < 0)
	{
		// reply contains neither <result... nor <progress... nor <error... nor <Data...
		// so ignore it
		oXML			= null;
	}
	else
	{
		oXML.loadXML (objXmlHttp.responseText.substr(i));
		if (oXML.parseError.errorCode != 0)
		{
			oXML.loadXML ('<error desc="' + oXML.parseError.reason + '"/>');
		}
	}
	return oXML;
} // DLG_AJAX_ProcessResponse
//------------------------------------------------------------------------------

