var ourwindow = null;
var serverName = "learn";
var doExit = false;

function getExitCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function ReadExitCookie (name) {
  for(var i = 0; i < name.length; i++)
		if (name.charAt(i) == " ")
			name = name.substring(0, i) + "+" + name.substring(i+1, name.length);

  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
	{
	  //modified to escape + signs, 8/26/98, John Richter
	  var outVal = getExitCookieVal(j);
	  for (i = 0; i < outVal.length; i++)
		if (outVal.charAt(i) == "+")
			outVal = outVal.substring(0, i) + " " + outVal.substring(i+1, outVal.length);
      return outVal;
	}
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function exitCourse(navPage)
{
//	window.opener.focus();
//	window.self.close();
	document.location.href="/oep/templogin.asp"
}

function exit(navPage, bgcolor, fontcolor, fromquiz)
{
	var altURL = ReadExitCookie("EXITNOTALLOWED");
	if (altURL != null && altURL != "")
	{
		top.location.href = altURL;
		return;
	}

	var theDate = new Date();
	var dateString;
	var yearString;
	yearString=theDate.getYear();
	if (yearString<1000) yearString+=1900;
	dateString = (theDate.getMonth() + 1)+"/"+theDate.getDate()+"/"+yearString+" "+theDate.getHours()+":"+theDate.getMinutes()+":"+theDate.getSeconds();
	if (bgcolor == null)
		bgcolor = "white";
	if (fontcolor == null)
		fontcolor = "black";
	if (ourwindow == null)
		ourwindow = window.open("", "Nada", "width=300,height=140");
	else
		if (ourwindow.closed)
			ourwindow = window.open("", "Nada", "width=300,height=140");
		else
			ourwindow.focus();
	ourwindow.document.write("<HTML><TITLE>Are you sure?</TITLE>");
	ourwindow.document.write("<HEAD>");
	ourwindow.document.write("<SCRIPT>\n");
	ourwindow.document.write("function finishExit()\n");
	ourwindow.document.write("{\n");
	ourwindow.document.write("	window.opener.doExit = true;\n");
//	ourwindow.document.write("	document.forms[0].submit();\n");
	ourwindow.document.write("  window.self.close();");
	ourwindow.document.write("}\n");
	ourwindow.document.write("</SCRIPT>\n");

	ourwindow.document.write("</HEAD>");
	if (fromquiz)
		ourwindow.document.write("<BODY BGCOLOR='"+bgcolor+"'><FONT COLOR='"+fontcolor+"'><CENTER>Are you sure you want to exit? You cannot resume a partially completed quiz. When you return, you will have to start the quiz again at the beginning.<BR>");
	  else
		ourwindow.document.write("<BODY BGCOLOR='"+bgcolor+"'><FONT COLOR='"+fontcolor+"'><CENTER>Are you sure you want to exit? You will be able to return to the current page when you reenter.<BR>");
	ourwindow.document.write("<FORM METHOD=POST ACTION='http://"+serverName+".caim.yale.edu/Testing/logins/exit.asp'><INPUT TYPE=HIDDEN NAME=NextPage VALUE='CLOSE'><INPUT TYPE=HIDDEN NAME=LastPage VALUE='"+top.location.href+"'><INPUT TYPE=HIDDEN NAME=LastPageName VALUE='"+top.document.title+"'><INPUT TYPE=HIDDEN NAME=ExitDate VALUE='"+dateString+"'><INPUT TYPE=BUTTON VALUE='Yes' ONCLICK='finishExit();'> <INPUT TYPE=BUTTON VALUE='Cancel' ONCLICK='window.close();'></CENTER>");
	ourwindow.document.write("</FORM></BODY></HTML>");
	ourwindow.document.close();
	if (navPage != null)
		setTimeout("waitForClose('"+navPage+"');", 100);
	else
		setTimeout("waitForClose(null);", 100);
}

function waitForClose(navPage)
{
	if (!ourwindow.closed)
		setTimeout("waitForClose('"+navPage+"');", 100);
	else
		if (doExit)
		{
			if (window.opener == null)
				if (navPage == null || navPage == "null")
					top.location.href = "http://"+serverName+".caim.yale.edu/Testing/logins/old/welcome.asp"
				else
					top.location.href = navPage;
			else
				if (window.opener.closed)
					if (navPage == null)
						top.location.href = "http://"+serverName+".caim.yale.edu/Testing/logins/old/welcome.asp"
					else
						top.location.href = navPage;
				else
					window.close();
		}
}

function hsExit(xurl)
{
	if (confirm("If you exit now, you will not receive credit for taking the course.\n\nThe next time you enter the course, you will need to start at the beginning.\n\nClick OK to exit the course or Cancel to continue with the course."))
	{  
//	  document.location.href="/";
	  top.opener.location.href=xurl;
	  window.self.close();
	}
}