
/*\
|*|  Dave Sugar
|*|  Dolphin Inc
|*|  $Id: windows.js,v 1.13 2007/03/21 17:12:46 johncri Exp $
\*/


var aWindows = new Array ();
var kWIN_MAIN = 1;
var kWIN_HELP = 2;
var kWIN_REPORT = 3;
var kWIN_PRINT = 4;
var kWIN_SUPPORT = 5;
var kWIN_QUESTION = 6;
var kWIN_HANDBOOK = 7;
var kWIN_REVISESPELL = 7;
var kWIN_STANDARD_DESCRIPTION = 8;
var kWIN_EMAIL_FEEDBACK = 9;
var kWIN_PLAN = 10;
var kWIN_EXPANDED_SUMMARY = 11;

// attempt to close all open subordinate windows
function closeAll ()
{
//alert ("windows:: closeAll");
	var nLen = aWindows.length;
	for (var i = 0; i < nLen; i++)
	{
		if (typeof aWindows[i] == "object" && !aWindows[i].closed)
			aWindows[i].close ();
	}
}

// set window reference for later
function setWin (i_nIndex, win)
{
	var pWin = getController ();
	
	if (pWin)
		pWin.setWin (i_nIndex, win);
	else
		aWindows[i_nIndex] = win;
}

// get window reference - important for openMainWindow
function getWin (i_nIndex)
{
	var pWin = getController ();
	if (pWin)
		return (pWin.getWin (i_nIndex));
		
	if (aWindows[i_nIndex])
		return (aWindows[i_nIndex]);
	return (null);
}

// logout - close main window and others
function logout ()
{
	var pWin = getController ();
	if (pWin)
		pWin.logout ();
	else
	{
		if (top.main && top.main.document)
			top.main.document.onChildClose ();
		closeAll ();
	}
}


// get controller window
function getController ()
{
	var pWin = top;
	if (pWin.wodniw == this)
		return (null);

	while (pWin && !pWin.wodniw)
	{
		if (pWin.opener)
		{
			pWin = pWin.opener.top;
			if (pWin)
				pWin = pWin.top;
		}
		else
			pWin = null;
	}

	if (pWin && pWin.wodniw)
		return (pWin.wodniw);

	return (null);
}

function openMainWindow (i_szPage, i_bMaximize, i_bEvenIfAlreadyOpen)
{
	var nHeight = 700;
	var nWidth = 1000;

	var nTop = (screen.availHeight - nHeight)/2;
	var nLeft = (screen.availWidth - nWidth)/2;
	var bForceTop = false;


	if (screen.availHeight < 700 || screen.availWidth < 900)
	{
		nTop = 0;
		nLeft = 0;
		bForceTop = true;
	}

	var szOptions = "directories=no, menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no";
	if (i_bMaximize)
	{
	
		var nHeight = screen.availHeight - 30;
		var nWidth = screen.availWidth - 10;
		var nTop = 0;
		var nLeft = 0;
		bForceTop = true;
		
//		szOptions += ", fullscreen=yes";
	}

	szOptions += ", height=" + nHeight + ", width=" + nWidth + ", screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop;

	var winMain = getWin (kWIN_MAIN);
	var bOpen = !winMain || winMain.closed;
	if (i_bEvenIfAlreadyOpen)
		bOpen = true;
	if (typeof bOpen != "boolean")
		bOpen = true;

	if (bOpen)
	{		// if the iwndow already exists - just change location
			// we don't want to affect the opener
		if (winMain && !winMain.closed)
			winMain.location = i_szPage;
		else
			winMain = window.open (i_szPage, "MainWindow", szOptions);
	}

	if (winMain)
		winMain.focus ();

	if (bForceTop && winMain && winMain.screenY != nTop)
		winMain.screenY = nTop;

	setWin (kWIN_MAIN, winMain);
}

function openHelpWindow (i_szPageName)
{
	if (i_szPageName)
	{
		var nHeight = 480;
		var nWidth = 640;
	
		var nTop = (screen.availHeight - nHeight)/4;
		var nLeft = (screen.availWidth - nWidth) - 12;
		
		var winHelp = window.open ("../help/help.php?help_page=" + i_szPageName, "HelpWindow", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
		setWin (kWIN_HELP, winHelp);
		if (winHelp)
			winHelp.focus ();
	}
}


function openReportWindow (i_nUserId, i_nAttemptDt, i_nAssignmentID, i_nLevelID, rev)
{
	var nHeight = 600;
	var nWidth = 860;

	var nTop = (screen.availHeight - nHeight)/2;
	var nLeft = (screen.availWidth - nWidth)/2;


	if (screen.availHeight < 700 || screen.availWidth < 900)
	{
		nTop = 0;
		nLeft = 0;
	}

	if (rev == 1)
	{
		var winReport = window.open ("../report/report.php?user_id=" + i_nUserId + "&attempt_dt=" + i_nAttemptDt + "&assign_id=" + i_nAssignmentID + "&level_id=" + i_nLevelID + "&revising=1", "ReportWindow", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
	}
	else
	{
		var winReport = window.open ("../report/report.php?user_id=" + i_nUserId + "&attempt_dt=" + i_nAttemptDt + "&assign_id=" + i_nAssignmentID + "&level_id=" + i_nLevelID, "ReportWindow", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
	}
	setWin (kWIN_REPORT, winReport);
	if (winReport)
		winReport.focus();
}

function openHScoreDetailWindow (i_nLevelId, i_nAssignmentID, i_nScore, i_nStartDate, i_nStopDate, i_nShowScore)
{
	var nHeight = 500;
	var nWidth = 620;

	var nTop = (screen.availHeight - nHeight)/2;
	var nLeft = (screen.availWidth - nWidth)/2;


	if (screen.availHeight < 700 || screen.availWidth < 900)
	{
		nTop = 0;
		nLeft = 0;
	}

	var winReport = window.open ("../report/hscoreDetail.php?assign_id=" + i_nAssignmentID + "&level_id=" + i_nLevelId + "&hscore=" + i_nScore + "&startDate=" + i_nStartDate + "&stopDate=" + i_nStopDate + "&show_score=" + i_nShowScore, "HolisticScoreSummaryWindow", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
	setWin (kWIN_REPORT, winReport);
	if (winReport)
		winReport.focus();
}

function openWATReportWindow (i_nUserId, i_nAttemptDt, i_nAssignmentID, i_nSection, i_nLevelID)
{
	var nHeight = 600;
	var nWidth = 860;

	var nTop = (screen.availHeight - nHeight)/2;
	var nLeft = (screen.availWidth - nWidth)/2;


	if (screen.availHeight < 700 || screen.availWidth < 900)
	{
		nTop = 0;
		nLeft = 0;
	}

	var winReport = window.open ("../report/reportWAT.php?user_id=" + i_nUserId + "&attempt_dt=" + i_nAttemptDt + "&assign_id=" + i_nAssignmentID + "&nsection=" + i_nSection + "&level_id=" + i_nLevelID, "ReportWindow", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
	setWin (kWIN_REPORT, winReport);
	if (winReport)
		winReport.focus();
}

function openPrinterVersionWindow (i_szPage)
{
	var nHeight = 400;
	var nWidth = 780;

	var nTop = (screen.availHeight - nHeight)/2;
	var nLeft = (screen.availWidth - nWidth)/2;

	if (screen.availHeight < 700 || screen.availWidth < 900)
	{
		nTop = 0;
		nLeft = 0;
	}

	var winPrint = window.open (i_szPage, "PrintWindow", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=yes, resizable=yes, scrollbars=yes, status=no, toolbar=yes, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
//	"directories=no, height=560, width=795, menubar=yes, resizable=yes, scrollbars=yes, status=no, toolbar=yes");
	setWin (kWIN_PRINT, winPrint);
	if (winPrint)
		winPrint.focus ();
}


function backToIndex2 (i_szPath)
{
	focus ();
	top.location = i_szPath + 'index.php';
}

function openSupportingDocWindow (i_szPageName)
{
	var nHeight = 400;
	var nWidth = 300;

	var nTop = (screen.availHeight - nHeight)/4;
	var nLeft = (screen.availWidth - (nWidth *2));

	var winSupportDoc = window.open (i_szPageName, "SupportingDocWindow", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
	setWin (kWIN_SUPPORT, winSupportDoc);
	if (winSupportDoc)
		winSupportDoc.focus ();
}

function openViewQuestionWindow (i_nAssignmentID, i_nPageID)
{
	var nHeight = 200;
	var nWidth = 400;

	var nTop = (screen.availHeight - nHeight)/4;
	var nLeft = (screen.availWidth - (nWidth *2));

	var szURL = "../report/viewQuestion.php?assign_id=" + i_nAssignmentID + "&page_id=" + i_nPageID;
	var winQuestion = window.open (szURL, "ViewQuestion", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
	setWin (kWIN_SUPPORT, winQuestion);
	if (winQuestion)
		winQuestion.focus ();
}
function openViewPlanWindow(assign_id, user_id)
{
	var nHeight = 550;
	var nWidth = 700;

	var nTop = (screen.availHeight - nHeight)/4;
	var nLeft = (screen.availWidth - (nWidth)*2);
	var szURL = '../report/reportViewPlan.php?user_id=' + user_id + '&assign_id=' + assign_id;
	var winPlan = window.open(szURL, "ViewPlan", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
	setWin(kWIN_PLAN, winPlan);
	if (winPlan)
		winPlan.focus ();
}
function openWriterHandbookWindow (i_nHandbookID, i_nErrCode, i_nSection, level_id)
{
 	var nHeight = 500;
	var nWidth = 700;

	var nTop = (screen.availHeight - nHeight)/4;
	var nLeft = (screen.availWidth - nWidth) - 12;

	var szURL = "../wrhandbook/";
	if (typeof (i_nErrCode) == "undefined" && typeof (i_nSection) == "undefined")
		szURL = szURL + "wrhContents.php?handbook=" + i_nHandbookID;
	else
		szURL = szURL + "wrhMain.php?handbook=" + i_nHandbookID + "&code=" + i_nErrCode;
	
	if (typeof (i_nSection) != "undefined")
		szURL = szURL + "&nsection=" + i_nSection;
		
	if (typeof (level_id) != "undefined")
		szURL = szURL + "&level_id=" + level_id;

	var winHandbook = window.open (szURL, "WritersHandbook", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
	setWin (kWIN_HANDBOOK, winHandbook);
	if (winHandbook)
		winHandbook.focus ();
}

function openStandardDescription (i_nStandardID)
{
	var nHeight = 250;
	var nWidth = 400;

	var nTop = (screen.availHeight - nHeight)/4;
	var nLeft = (screen.availWidth - (nWidth *2));

	var szURL = "../assignment/standardDesc.php?standard_id=" + i_nStandardID;

	var winStandardDesc = window.open (szURL, "SandardDescWindow", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
	setWin (kWIN_STANDARD_DESCRIPTION, winStandardDesc);
	if (winStandardDesc)
		winStandardDesc.focus ();

}

// added for instructor level or above (probably could be combine with openEmailFeedback as some point)
function openInstructorEmailFeedback (user_id, attempt_dt, assign_id, level_id, stuName, assignName)
{
	var nHeight = 400;
	var nWidth = 400;
	var nTop = (screen.availHeight - nHeight)/4;
	var nLeft = (screen.availWidth - (nWidth *2));

	var szURL = "../report/instructorEmailWATReport.php?user_id=" + user_id + "&attempt_dt=" + attempt_dt + "&assign_id=" + assign_id + "&level_id=" + level_id + "&stuName=" + stuName + "&assignName=" + assignName;

	var winEmailFeedback = window.open (szURL, "EmailFeedbackWindow", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
	setWin (kWIN_EMAIL_FEEDBACK, winEmailFeedback);
	if (winEmailFeedback)
		winEmailFeedback.focus ();

}

function openEmailFeedback (user_id, attempt_dt, assign_id, level_id)
{
	var nHeight = 300;
	var nWidth = 400;

	var nTop = (screen.availHeight - nHeight)/4;
	var nLeft = (screen.availWidth - (nWidth *2));

	var szURL = "../report/emailWATReport.php?user_id=" + user_id + "&attempt_dt=" + attempt_dt + "&assign_id=" + assign_id + "&level_id=" + level_id;

	var winEmailFeedback = window.open (szURL, "EmailFeedbackWindow", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
	setWin (kWIN_EMAIL_FEEDBACK, winEmailFeedback);
	if (winEmailFeedback)
		winEmailFeedback.focus ();

}

function openExpandedSummary (user_id, attempt_dt, assign_id, level_id)
{
	var nHeight = 400;
	var nWidth = 780;

	var nTop = (screen.availHeight - nHeight)/2;
	var nLeft = (screen.availWidth - nWidth)/2;

	if (screen.availHeight < 700 || screen.availWidth < 900)
	{
		nTop = 0;
		nLeft = 0;
	}

	var szURL = "../report/emailWATSummary.php?user_id=" + user_id + "&attempt_dt=" + attempt_dt + "&assign_id=" + assign_id + "&level_id=" + level_id;

	var winExpandedSummary = window.open (szURL, "ExpandedSummaryWindow", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=yes, resizable=yes, scrollbars=yes, status=no, toolbar=yes, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
		
	setWin (kWIN_EXPANDED_SUMMARY, winExpandedSummary);
	if (winExpandedSummary)
		winExpandedSummary.focus ();

}

// added by John
// used to open the spell check window when revising in the split essay revision screen
// if blank == 1, open a blank window, ignores the i_szUrl
// if blank == 0, set the window to i_szUrl
function openReviseSpellingWindow (i_szUrl, blank)
{
	var nHeight = screen.availHeight;
	var nWidth = screen.availWidth;

	var nTop = 0;
	var nLeft = 0;

	if (blank == 0)
	{	
		//var winReviseSpell = window.open (i_szUrl, "ReviseSpellingWindow", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
		var winReviseSpell = getWin(kWIN_REVISESPELL);
		winReviseSpell.location = i_szUrl;
		
		if (winReviseSpell)
			winReviseSpell.focus ();
	}
	else
	{
		var winReviseSpell = window.open ("", "ReviseSpellingWindow", "directories=no, height=" + nHeight + ", width=" + nWidth + ", menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no, screenX=" + nLeft +", left=" + nLeft + ", screenY=" + nTop + ", top=" + nTop);
		setWin (kWIN_REVISESPELL, winReviseSpell);	
	}
}

function checkForBackspace (i_Event) 
{
	if (typeof i_Event == "undefined")
		i_Event = window.event;

	// for Netscape
	if (i_Event.target && 
		typeof i_Event.target.type != "undefined"
		&& (i_Event.target.type.match ("text")
		|| i_Event.target.type.match ("password")
		|| i_Event.target.type.match ("file"))
		)
	{
		return true;
	}

	//we can backspace in a textbox 		// for IE
	if (i_Event.srcElement && 
		typeof i_Event.srcElement.type != "undefined"
		&& (i_Event.srcElement.type.match ("text")
		|| i_Event.srcElement.type.match ("password")
		|| i_Event.srcElement.type.match ("file"))
		) 
	{
		return true;
	}

	if (i_Event && i_Event.keyCode == 8) 
	{
		// try to cancel the backspace
		if (i_Event.cancelBubble)
			i_Event.cancelBubble = true;
		if (i_Event.returnValue)
			i_Event.returnValue = false;
		return false;
	}
}


// DJS 17-Jan-2004
// Taken from http://www.codingforums.com/archive/index.php/t-38175
// with some minor modifications
//so backspace doesn't go back
if (document.all) 
{
	document.onkeydown = checkForBackspace;
}
else
{
	document.onkeypress = checkForBackspace;
	document.captureEvents (Event.KEYPRESS)
}
