/*
 * Set up popup window object to hold references to all
 * popup windows.
 */
if (null == versionID) var versionID = new Array();
versionID[versionID.length] = ("Popup Script: v1.3\n");

function nl_getGELevel()
{
	var t = "/courses/level"
	var f = document.location + "";
	f = f.substring(f.lastIndexOf(t)+t.length,f.lastIndexOf(t)+t.length+1);
	f = parseInt(f,10);
	if (!isNaN(f))
	{
		return f;
	}
	else
	{
		return -1;
	}
}

function NL_popupWindow(bgcolor)
{
this.win = null;
this.color = bgcolor;
}

var nl_popup = new Array();
nl_popup["text"]     = new NL_popupWindow("#CCFFCC");
if (nl_getGELevel() == 1) {
	nl_popup["answer"]   = new NL_popupWindow("#EEEEEE");
} else {
	nl_popup["answer"]   = new NL_popupWindow("#EEEEEE");
}
nl_popup["grammar"]  = new NL_popupWindow("#FFFFFF");
nl_popup["wordlist"] = new NL_popupWindow("#CCCCCC");
nl_popup["tape"]     = new NL_popupWindow("#FFCC99");

function openPopupWindowSpecURL(winArray,linkHref,linkTarget,settings)
{
	if (navigator.appVersion.indexOf("MSIE 3") >= 0) return;

	var theURL = linkHref;
	var theTarget = linkTarget;
	if (null == settings)
	{
		if(String(theTarget) == "grammar")
		{
			settings = 'scrollbars=yes,width=500,height=300,menubar=yes,resizable=yes';
		}
		else
		{
			settings = 'scrollbars=yes,width=300,height=200,menubar=yes,resizable=yes';
		}
	}

	if (null == winArray[theTarget].win) {//    alert("New window or was closed programmatically");
		winArray[theTarget].win = window.open(theURL,theTarget,settings)
	}
	else if (!winArray[theTarget].win.closed) {//    alert("Already open, load new document");
		winArray[theTarget].win.location.assign(theURL)
		winArray[theTarget].win.focus();
	}
	else {//    alert("Closed manually");
		winArray[theTarget].win = window.open(theURL,theTarget,settings)
	}
}

function openPopupWindowURL(winArray,theLink,settings)
{
	if (navigator.appVersion.indexOf("MSIE 3") >= 0) return;

	var theURL = theLink.href;
	var theTarget = theLink.target;

	openPopupWindowSpecURL(winArray,theURL,theTarget,settings);
}

function openSameDir(theLink)
{
	var theURL = theLink.href;
	var theFile = theURL.substring(theURL.lastIndexOf("/"),theURL.length);
	var theDocument = document.location.href.toString();
	var theDirectory = theDocument.substring(0,theDocument.lastIndexOf("/"));
	theURL = theDirectory + theFile;
	document.location = theURL;
}

function openPopupWindowSameDir(winArray,theLink,settings)
{
	if (navigator.appVersion.indexOf("MSIE 3") >= 0) return;

	var theURL = theLink.href;
	var theFile = theURL.substring(theURL.lastIndexOf("/"),theURL.length);
	var theDocument = document.location.href.toString();
	var theDirectory = theDocument.substring(0,theDocument.lastIndexOf("/"));
	theURL = theDirectory + theFile;
	var theTarget = theLink.target;

	openPopupWindowSpecURL(winArray,theURL,theTarget,settings);
}

function openPopupWindowHTML(winArray,winName,title,content,settings)
{
	var theDocument = document.location.href.toString();
	var loadingURL
	if (theDocument.indexOf("/englishforwork/students/") > 0)
		loadingURL = theDocument.substring(0,theDocument.lastIndexOf("/englishforwork/students/"))
	else
		loadingURL = theDocument.substring(0,theDocument.lastIndexOf("/courses/"))

	loadingURL += "/javascript/loading.htm"

	var tophtml = "<html><head><title>";
	var midhtml = "</title></head><body bgcolor="+winArray[winName].color+" text=#000000>";
	var bothtml = "<form onsubmit='return false'><center><input type=button onclick='window.close()' value='OK'></center></form></body></html>";

	if (null == settings) settings = 'scrollbars=yes,width=300,height=400,menubar=yes,resizable=yes';

	if (navigator.appVersion.indexOf("MSIE 3") >= 0)
	{
		var win = window.open(loadingURL,winName,settings);
		win.document.open();
		win.document.write(tophtml + title + midhtml + content + bothtml);
		win.document.close();
		return;
	}

//  if (winArray[winName].win == null) {//    alert("New window or was closed programmatically");
	if (!winArray[winName].win) {//    alert("New window or was closed programmatically");
		winArray[winName].win = window.open(loadingURL,winName,settings)
		winArray[winName].win.document.open();
		winArray[winName].win.document.write(tophtml + title + midhtml + content + bothtml);
		winArray[winName].win.document.close();
	}
	else if (!winArray[winName].win.closed) {//    alert("Already open, write new document");
		winArray[winName].win.document.open();
		winArray[winName].win.document.write(tophtml + title + midhtml + content + bothtml);
		winArray[winName].win.document.close();
		if (navigator.appVersion.indexOf("MSIE 3") < 0) winArray[winName].win.focus();
	}
	else {//    alert("Closed manually");
		winArray[winName].win = window.open(loadingURL,winName,settings)
		winArray[winName].win.document.open();
		winArray[winName].win.document.write(tophtml + title + midhtml + content + bothtml);
		winArray[winName].win.document.close();
	}
}

function openPopupWindowFrames(winArray,leftContent,rightContent,winName,title,settings)
{
	var theDocument = document.location.href.toString();
	var loadingURL = theDocument.substring(0,theDocument.lastIndexOf("/englishforwork/students/"))
	loadingURL += "/javascript/loading.htm"

	if (null == settings) settings = 'scrollbars=yes,width=500,height=250,menubar=yes,resizable=yes';
	if (null == title) title = 'Grammar Reference';
	if (null == winName) winName = 'grammar';

	var tophtml = "<html><head><title>"+title+"</title></head>\n" +
		"<frameset cols=\"156,*\" bordercolor=\"#000000\" border=\"0\">\n<frame src=\"";
	var midhtml = "\" scrolling=\"YES\" bordercolor=\"#FFFFFF\" frameborder=\"NO\" name=\"left\">\n" +
		"<frame src=\"";
	var bothtml = "\" scrolling=\"AUTO\" frameborder=\"NO\" bordercolor=\"#FFFFFF\" name=\"right\">"+
		"</frameset></html>";

	if (navigator.appVersion.indexOf("MSIE 3") >= 0)
	{
		var win = window.open(loadingURL,winName,settings);
		win.document.open();
		win.document.write(tophtml + leftContent + midhtml + rightContent + bothtml);
		win.document.close();
		return;
	}

	if (winArray[winName].win == null) {//    alert("New window or was closed programmatically");
		winArray[winName].win = window.open(loadingURL,winName,settings)
		winArray[winName].win.document.open();
		winArray[winName].win.document.write(tophtml + leftContent + midhtml + rightContent + bothtml);
		winArray[winName].win.document.close();
	}
	else if (!winArray[winName].win.closed) {//    alert("Already open, write new document");
		winArray[winName].win.document.open();
		winArray[winName].win.document.write(tophtml + leftContent + midhtml + rightContent + bothtml);
		winArray[winName].win.document.close();
		if (navigator.appVersion.indexOf("MSIE 3") < 0) winArray[winName].win.focus();
	}
	else {//    alert("Closed manually");
		winArray[winName].win = window.open(loadingURL,winName,settings)
		winArray[winName].win.document.open();
		winArray[winName].win.document.write(tophtml + leftContent + midhtml + rightContent + bothtml);
		winArray[winName].win.document.close();
	}
}

function closePopupWindows(popupArray)
{
if (navigator.appVersion.indexOf("MSIE 3") >= 0) return;

if (popupArray["text"].win != null)
	if (!popupArray["text"].win.closed)
		popupArray["text"].win.close();
popupArray["text"].win = null;

if (popupArray["answer"].win != null)
//if (popupArray["answer"].win)
	if (!popupArray["answer"].win.closed)
		popupArray["answer"].win.close();
popupArray["answer"].win = null;

if (popupArray["grammar"].win != null)
	if (!popupArray["grammar"].win.closed)
		popupArray["grammar"].win.close();
popupArray["grammar"].win = null;

if (popupArray["wordlist"].win != null)
	if (!popupArray["wordlist"].win.closed)
		popupArray["wordlist"].win.close();
popupArray["wordlist"].win = null;

if (popupArray["tape"].win != null)
	if (!popupArray["tape"].win.closed)
		popupArray["tape"].win.close();
popupArray["tape"].win = null;
}

document.nl_config.add("onunload","closePopupWindows(nl_popup);");

