var win = null;
var lastShownUrl = null;
var isNav = (navigator.appName.indexOf("Netscape") != -1);
var isIE = (navigator.appName.indexOf("Microsoft") != -1);

function openWindow(url, name, width, height)
{
	try
	{
		if (win != null && !win.closed)
		{
			win.focus();
			if (lastShownUrl != url)
			{
				lastShownUrl = url;
				win.resizeTo(width, height);
				win.location = url;
			}
		}
		else
		{
			lastShownUrl = url;
			win = window.open(url, name, "width=" + width + ",height=" + height + ",scrollbars=yes,resizable=no");
			win.focus();
		}
	}
	catch (e)
	{
		alert("error.openwindow.js.openWindow()\n" + e + " - " + e.description);
	}
}

function killWindow()
{
	try
	{
		if (win != null && !win.closed)
			win.close();
	}
	catch (e)
	{
		alert("error.openwindow.js.killWindow()\n" + e + " - " + e.description);
	}
}

function logout(context)
{
	try
	{
		window.open(context + "/Logout.do", "logout", "width=10px, height=10px, left=" + screen.width + "px, top=" + screen.height + "px, scrollbars=yes, resizable=no");
	}
	catch (e)
	{
		alert("error.openwindow.js.logout()\n" + e + " - " + e.description);
	}
}

function urlEncode(sStr)
{
	try
	{
		return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\</g, '%3C').replace(/\>/g, '%3E');
	}
	catch (e)
	{
		alert("error.openwindow.js.urlEncode()\n" + e + " - " + e.description);
	}
}

function openSearch(context, searchType, fieldName, constraints, fieldName2)
{
	try
	{
		if (constraints == undefined || constraints == null)
			constraints = "";
		if (fieldName2 == undefined || fieldName2 == null)
			fieldName2 = "";
	
		var uri = context + "/search/Index.do?searchType=" + searchType + "&fieldName=" + fieldName;
		uri += "&constraints=" + urlEncode(constraints) + "&fieldName2=" + fieldName2;
		openModal(this, uri, 450, 500);
	}
	catch (e)
	{
		alert("error.openwindow.js.openSearch()\n" + e + " - " + e.description);
	}
}

//------ modal -------

var modalDialog;

function openModal(src, url, width, height)
{
	try
	{
		openModal(src, url, width, height, true);
	}
	catch (e)
	{
		alert("error.openwindow.js.openModal()\n" + e + " - " + e.description);
	}
}

function openModal(src, url, width, height, scroll)
{
	try
	{
		closeModal(src);
		if(window.showModalDialog)
			modalDialog = src.window.showModalDialog(url, src.window, "help:no;resizable:yes;status:no;dialogWidth:" + width + "px;dialogHeight:" + height + "px;");
		else
			modalDialog = src.window.open(url, getDialogName(src), "location=no,menubar=no,resizable=yes,scrollbars=" + (scroll ? "yes" : "no") + ",status=no,toolbar=no,modal=yes,width=" + width + ",height=" + height);
	}
	catch (e)
	{
		alert("error.openwindow.js.openModal()\n" + e + " - " + e.description);
	}
}

function getDialogName(src)
{
	try
	{
		return "modalDialog_" + src.window.name;
	}
	catch (e)
	{
		alert("error.openwindow.js.getDialogName()\n" + e + " - " + e.description);
	}
}

function getOpener(src)
{
	try
	{
		if(document.all)
			return src.window.dialogArguments;
		else
			return src.window.opener;
	}
	catch (e)
	{
		alert("error.openwindow.js.getOpener()\n" + e + " - " + e.description);
	}
}

function closeModal(src)
{
	try
	{
		if (src.window.modalDialog && !src.window.modalDialog.closed)
			src.window.modalDialog.close();
	}
	catch (e)
	{
		alert("error.openwindow.js.closeModal()\n" + e + " - " + e.description);
	}
}