function trimString (str) {
  str = this != window ? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
String.prototype.trim = trimString;

// determine browser type

var isNav = false;
var isNav6 = false;
var isIE = false;
var isIE4WIN = false;
var isIE4Mac = false;
var isNav4Mac = false;
var isNav4Win = false;

var isMac = (navigator.appVersion.indexOf('Mac') != -1);

if (navigator.appName.indexOf("Netscape") != -1){
	isNav = true;
	if (navigator.userAgent.indexOf("Netscape6") != -1)
	{
		isNav6 = true;
	}
	else if (isMac && (navigator.userAgent.indexOf("Netscape6") == -1) )
	{
		isNav4Mac = true;
	}
	else if (navigator.userAgent.indexOf("Netscape6") == -1)
	{
		isNav4Win = true;
	}
} else if (navigator.appName.indexOf("Microsoft") != -1){
	isIE = true;
	if (isMac && (navigator.appVersion.indexOf('5.') == -1) )
	{
		isIE4Mac = true;
	}
	else if (navigator.userAgent.indexOf('MSIE 4') != -1)
	{
		isIE4WIN = true;
	} 
}
//var isNav = (navigator.appName.indexOf("Netscape") != -1);
//var isNav6 = (navigator.userAgent.indexOf("Netscape6") != -1);
//var isIE = (navigator.appName.indexOf("Microsoft") != -1);
//var isIE4WIN = ((navigator.appName.indexOf("Microsoft") != -1) && (navigator.userAgent.indexOf('msie 4') != -1));
//var isIE4Mac = ((navigator.appName.indexOf("Microsoft") != -1) && (navigator.appVersion.indexOf('5.') == -1) && (navigator.appVersion.indexOf('Mac') != -1));
//var isNav4Mac = ((navigator.appName.indexOf("Netscape") != -1) && (navigator.userAgent.indexOf("Netscape6") == -1) && (navigator.appVersion.indexOf('Mac') != -1));

// following var should be overwritten by popups to ensure that they're closed and that activity is tracked in opener
var isPopup = false ;

// following var should be overwritten by frames to ensure that activity is tracked in parent
var isFrame = false ;

// following vars should be overwritten by teacher or student javascript
var timeToWarning = 780000 ;	// If negative, no warning.
var timeToLogoff = 120000 ;
//var timeToWarning = 5000 ;
//var timeToLogoff = 5000 ;

// Override with your own code to replace the default warning:
var timeoutWarningCode = "timeoutWarning()";

// following vars should be set by each component,
// however, leave the var declarations here to prevent IE undeclared
// variable errors
var logoffURL = null ;
var warningWindowURL = null ;

var popup = null ;
var warningWindow = null ;

// ids for timeouts
var warningTimeoutID = null;
var logoffTimeoutID = null;

// following can be used to reference a special function to perform cleanup/submit ( eg. timeout during taking a test )

var specialTimeout = null ;

var theWindow = false; // Remove to restore original popupWindow functionality.
var helpWIndow = false;  //  This is not used in the school app because it doesn't seem necessary.
var privacyWindow = false;
var termsWindow = false;
var reportWindow = false;
var legendWindow = false;
var creditsWindow = false;


// This function uses the variables above as handles to the windows so that they can be controlled separately.
function popupWindow2(theWindow, sWindowName, iHeight, iWidth, sResizable, sUrl, sScrollbars, addTime ){
	var tempWindow;

	var sLocalUrl, sOptionString, sLocalResizable, sLocalScrollbars ;
	var dTop, dLeft ;

        var bAddTime = false;
        if ((null != addTime) && (addTime == "yes"))
		bAddTime = true;

	if ( ( sUrl == null ) || ( sUrl == '' ) )
		sLocalUrl = '';
	else
		sLocalUrl = sUrl;

        if (bAddTime){
		var nowTime = new Date();
		var nowMs = nowTime.getTime() + "";
		if (sLocalUrl.indexOf("?") == -1)
   			sLocalUrl += "?";
		else
			sLocalUrl += "&";
		sLocalUrl += ("wxyz=" + nowMs);
        }

	if ( ( sResizable == null ) || ( sResizable == '' ) )
		sLocalResizable = 'yes' ;
	else
		sLocalResizable = sResizable ;

	if ( ( sScrollbars == null ) || ( sScrollbars == '' ) )
		sLocalsScrollbars = 'yes' ;
	else
		sLocalsScrollbars = sScrollbars ;

	dTop =  ((screen.availHeight/2.0) - (iHeight/2.0));
	dLeft = ((screen.availWidth/2.0) - (iWidth/2.0));
 
	sOptionString = ('width=' + iWidth + ',height=' + iHeight + ',top=' + parseInt(dTop) + ',left=' + parseInt(dLeft) + 
		',status=no,menubar=no,resizable=' + sLocalResizable + ',toolbar=no,scrollbars=' + sLocalsScrollbars);	
	
	if (theWindow == "privacyWindow")
	{
		if ((privacyWindow) && !(privacyWindow.closed)) 
		{
			privacyWindow.focus();
			eval("privacyWindow.location.href = '" + sLocalUrl + "'");
		}
		else 
		{
			privacyWindow = window.open(sLocalUrl, sWindowName, sOptionString); 
			privacyWindow.focus(); // In an earlier incarnation, this line was unnecessary and caused errors under Windows. It's necessary now and does not seem to be problematic.
			return privacyWindow;
		}
	}
	else if (theWindow == "termsWindow")
	{
		if ((termsWindow) && !(termsWindow.closed)) 
		{
			termsWindow.focus();
			eval("termsWindow.location.href = '" + sLocalUrl + "'");
		}
		else 
		{
			termsWindow = window.open(sLocalUrl, sWindowName, sOptionString); 
			termsWindow.focus(); // In an earlier incarnation, this line was unnecessary and caused errors under Windows. It's necessary now and does not seem to be problematic.
			return termsWindow;
		}
	}
	else if (theWindow == "creditsWindow")
	{
		if ((creditsWindow) && !(creditsWindow.closed)) 
		{
			creditsWindow.focus();
			eval("creditsWindow.location.href = '" + sLocalUrl + "'");
		}
		else 
		{
			creditsWindow = window.open(sLocalUrl, sWindowName, sOptionString); 
			creditsWindow.focus(); // In an earlier incarnation, this line was unnecessary and caused errors under Windows. It's necessary now and does not seem to be problematic.
			return creditsWindow;
		}
	}
	else if (theWindow == "reportWindow")
	{
		if ((reportWindow) && !(reportWindow.closed)) 
		{
			reportWindow.focus();
			eval("reportWindow.location.href = '" + sLocalUrl + "'");
		}
		else 
		{
			reportWindow = window.open(sLocalUrl, sWindowName, sOptionString); 
			reportWindow.focus(); // In an earlier incarnation, this line was unnecessary and caused errors under Windows. It's necessary now and does not seem to be problematic.
			return reportWindow;
		}
	}
	else if (theWindow == "legendWindow")
	{
		if ((legendWindow) && !(legendWindow.closed)) 
		{
			legendWindow.focus();
			eval("legendWindow.location.href = '" + sLocalUrl + "'");
		}
		else 
		{
			legendWindow = window.open(sLocalUrl, sWindowName, sOptionString); 
			legendWindow.focus(); // In an earlier incarnation, this line was unnecessary and caused errors under Windows. It's necessary now and does not seem to be problematic.
			return legendWindow;
		}
	}


}


// The following function and is the original one that does not use the separate variables for each window instance.  It uses the single variable theWindow for all windows it opens.
function popupWindow(sWindowName, iHeight, iWidth, sResizable, sUrl, sScrollbars, addTime ){
	var sLocalUrl, sOptionString, sLocalResizable, sLocalScrollbars ;
	var dTop, dLeft ;

        var bAddTime = false;
        if ((null != addTime) && (addTime == "yes"))
		bAddTime = true;

	if ( ( sUrl == null ) || ( sUrl == '' ) )
		sLocalUrl = '';
	else
		sLocalUrl = sUrl;

        if (bAddTime){
		var nowTime = new Date();
		var nowMs = nowTime.getTime() + "";
		if (sLocalUrl.indexOf("?") == -1)
   			sLocalUrl += "?";
		else
			sLocalUrl += "&";
		sLocalUrl += ("wxyz=" + nowMs);
        }

	if ( ( sResizable == null ) || ( sResizable == '' ) )
		sLocalResizable = 'yes' ;
	else
		sLocalResizable = sResizable ;

	if ( ( sScrollbars == null ) || ( sScrollbars == '' ) )
		sLocalsScrollbars = 'yes' ;
	else
		sLocalsScrollbars = sScrollbars ;

	dTop =  ((screen.availHeight/2.0) - (iHeight/2.0));
	dLeft = ((screen.availWidth/2.0) - (iWidth/2.0));
 
	sOptionString = ('width=' + iWidth + ',height=' + iHeight + ',top=' + parseInt(dTop) + ',left=' + parseInt(dLeft) + 
		',status=no,menubar=no,resizable=' + sLocalResizable + ',toolbar=no,scrollbars=' + sLocalsScrollbars);
//	return window.open(sLocalUrl, sWindowName, sOptionString) // Restore to return to original functionality.
	

// Remove everything below here to return to original functionality.
	if (!(navigator.appName != 'Netscape')) 
	{
		if ((theWindow) && !(theWindow.closed)) 
		{
			theWindow.focus();
			eval("theWindow.location.href = '" + sLocalUrl + "'");
		}
		else 
		{
			theWindow = window.open(sLocalUrl, sWindowName, sOptionString); 
//			theWindow.focus(); // This line is unnecessary and causes errors under Windows.
			return theWindow;
		}
	}
	else theWindow = window.open(sLocalUrl, sWindowName, sOptionString);  
}

function testPopupWindow(sWindowName, iHeight, iWidth, sResizable, sUrl, sScrollbars ){
	var sLocalUrl, sOptionString, sLocalResizable, sLocalScrollbars ;
	var dTop, dLeft ;

	if ( ( sUrl == null ) || ( sUrl == '' ) )
		sLocalUrl = '';
	else
		sLocalUrl = sUrl;

	if ( ( sResizable == null ) || ( sResizable == '' ) )
		sLocalResizable = 'yes' ;
	else
		sLocalResizable = sResizable ;

	if ( ( sScrollbars == null ) || ( sScrollbars == '' ) )
		sLocalsScrollbars = 'yes' ;
	else
		sLocalsScrollbars = sScrollbars ;

	dTop =  ((screen.availHeight/2.0) - (iHeight/2.0));
	dLeft = ((screen.availWidth/2.0) - (iWidth/2.0));
 
	sOptionString = ('width=' + iWidth + ',height=' + iHeight + ',top=' + parseInt(dTop) + ',left=' + parseInt(dLeft) + 
		',status=no,menubar=no,resizable=' + sLocalResizable + ',toolbar=no,scrollbars=' + sLocalsScrollbars);
	return window.open(sLocalUrl, sWindowName, sOptionString) // Restore to return to original functionality.
}

//////////////////////////////////////////////////////////////////
//
//	function: logOff
//	purpose:  called to log off user (eg., for timeout)
//	notes:	  global var logoffURL MUST be set locally in
//			page's HTML since it requires a 
//			WOActionURL
//
//////////////////////////////////////////////////////////////////
function logOff () {

	// close current window if popup
	if ( popup != null ) 
		if ( !popup.closed )
			popup.close() ;

	// close warning window if open
	if ( warningWindow != null )
		if ( !warningWindow.closed ) {
			warningWindow.timedOut = true;
			warningWindow.close();
		}

	if ( logoffURL != null ) {
		if (null != specialTimeout )
			specialTimeout() ;
		else
			window.location = logoffURL ;
		window.close() ;
	}
}

function assignTimeoutHandlers() {
	// if navigator, call capture events

	if ( isNav ) 
		document.captureEvents ( Event.CLICK | Event.CHANGE | Event.SELECT | Event.KEYPRESS ) ;

	// add timeout handler to document onclick, onchange, and onselect
	document.onclick = resetOnEvent ;	
	document.onchange = resetOnEvent ;	
	document.onselect = resetOnEvent ;	
	if (!isIE4Mac) document.onkeypress = resetOnEvent ;	

	//start timer
	var timingWindow = getTimingWindow();
	timingWindow.resetTimer() ;	
}

function resetOnEvent (e) {
	var timingWindow = getTimingWindow();
	timingWindow.resetTimer() ;	
	if ( isIE ) 		
		window.event.cancelBubble = true;
	if ( isNav && ! isNav6) {
		document.routeEvent(e);
	}
	return true;
}

function getTimingWindow () {
	var timingWindow = window ;
	if ( ( isFrame == true ) && ( window.parent != null ) )
		timingWindow =  window.parent ;
	else if ( ( isPopup == true ) && ( window.opener != null ) ) {
		timingWindow =  window.opener ;
		timingWindow.popup = window ;
	}
	
	return ( timingWindow ) ;	
}

function resetTimer() {
	// we have to put an extra check for popup here for recursive popups
	if ( ( isPopup == true ) && ( window.opener != null ) && (!window.opener.closed) ) {
		window.opener.popup = window ;
		window.opener.resetTimer() ;
		return ;
	}

	if ( warningTimeoutID != null )
		window.clearTimeout(warningTimeoutID) ; 
	if (logoffTimeoutID != null )
		window.clearTimeout(logoffTimeoutID) ;

	// check here to see if warning window is open - if so close
	if ( warningWindow != null )
		if ( !warningWindow.closed )
			warningWindow.close();

	if (timeToWarning >= 0) {
		warningTimeoutID = window.setTimeout ( timeoutWarningCode, timeToWarning ) ;
	}
}

function timeoutWarning ( ){
	// open alert warning window
	logoffTimeoutID = window.setTimeout ( "logOff();",timeToLogoff ) ;
	popupWindow("warning", 160, 500, "", warningWindowURL ) ;
}


function safePrint() {

/*alert (
"isMac = " + isMac + "\n" +
"isNav = " + isNav + "\n" +
"isNav6 = " + isNav6 + "\n" +
"isIE = " + isIE + "\n" +
"isIE4Mac = " + isIE4Mac + "\n" +
"isNav4Mac = " + isNav4Mac + "\n" +
"isIE4WIN = " + isIE4WIN + "\n"+
"navigator.userAgent = " + navigator.userAgent + "\n"+
"navigator.appName = " + navigator.appName
);*/
	if ( isMac && ! isNav6 ) { 		
		alert ("Please use Command + P to print this page");
	} else if ( isIE4WIN )
	{
		alert ("Please use Ctrl + P to print this page");
	}
	else {
		print();
	}
}

function go_to(page, wintarget){ 
	var newlocation = document.links[page].href;
	wintarget.location = newlocation; 
} 
function itemSelected(parSel){
   if (!parSel) return true;
   return (parSel.selectedIndex > -1);
}
function doNada(){ ; } // do nothing


function disableRightClick(e){
	var eventNumber = 0;
	if (isNav) eventNumber = e.which;
	else if (isIE) eventNumber = event.button;

	if (!allowRightClick)
	{
		if ( eventNumber==2 || eventNumber==3 )
		{
			if (isIE) alert ("The right mouse button has been disabled."); // Necessary for this to work on IE.
		        return (false);
	        }
	}

	return true;
}

function returnFalse() {
	return false;
}

if (!allowRightClick) document.oncontextmenu=returnFalse;

document.onmousedown=disableRightClick;
document.onmouseup=disableRightClick;
if (document.layers) window.captureEvents(Event.MOUSEDOWN);
if (document.layers) window.captureEvents(Event.MOUSEUP);
window.onmousedown=disableRightClick;
window.onmouseup=disableRightClick;

function reDo()
{
	if (isNav4Mac || isNav4Win) 
	{
		location.reload(true);
	}
}

// Checks or unchecks all the checkboxes in the given form
// depending on the boolean value of "checked".
function setAllCheckboxes(form, checked) {
   return setAllCheckboxesWithPrefix(form, checked, null);
}

// Checks or unchecks all the checkboxes in the given form whose name
// begins with "prefix" depending on the boolean value of "checked".
function setAllCheckboxesWithPrefix(form, checked, prefix) {
   if (form) {
      var elements = form.elements;
      for (var i = 0; i < elements.length; i++) {
         var element = elements[i];
         if ("checkbox" == element.type && 
	  (null == prefix || (element.name.indexOf(prefix) > -1))) {
            element.checked = checked;
         }
      }
   }
}
