/*
Copyright Reazon Systems, Inc.  All rights reserved.

This code is protected by US and International Copyright laws.  Reproduction, distribution, 
display, modification and use of the code without written permission of the Reazon Systems, Inc. is prohibited..  
*/

//****** MM rollover functions ******* 
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//****** End of MM rollover functions ******* 


/**
	Force the page to open in top frame instead
*/	
function forceTopFrame() {
   if (top.location != document.location) {
      top.location.replace(document.location.href);
   }
}

/**
	check if cookies are enabled
*/	
function checkCookies() {
	document.cookie = 'CookiesEnabled=1';  //try to set the cookie.  
	
	//document.cookie = 'utcMinuteOffset=' + (new Date()).getTimezoneOffset();  //side effect: Save timezone offset to be used by datetimeutils
	
	if (document.cookie <= ''){	//cookies aren't enabled (value of document.cookie is less than (not equal to) blank
		var cookieMessageObj = document.getElementById('cookieMessageContainer');
		if (cookieMessageObj) {
			setInnerHTML(cookieMessageObj, "<strong><center>Warning: Your browser cookies are disabled.</center></strong>This site requires <a href='helpcookiesc.cfm'>cookies</a> to function properly.");
			showElement(cookieMessageObj,'visible');
		}
	} else {  //cookies are enabled
		//side effect: set timezone offset
		setTimeZoneCookies()
	}
}

// Timezone Test function from http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CFB006C45F7
function setTimeZoneCookies() {
	var rightNow = new Date();
	var timezoneOffset = rightNow.getTimezoneOffset();
	
	//check if daylight savings is observed and if it's in daylight savings mode
	var dateJan = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  //Jan 1st of current year
	var dateJun = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);	//Jun 1st of current year
	var dateJanGMT = dateJan.toGMTString();
	var dateJanLocal = new Date(dateJanGMT.substring(0, dateJanGMT.lastIndexOf(" ")-1));  //remove UTC from end of date
	var dateJunGMT = dateJun.toGMTString();
	var dateJunLocal = new Date(dateJunGMT.substring(0, dateJunGMT.lastIndexOf(" ")-1));
	var timezoneOffsetJan = -(dateJan - dateJanLocal) / (1000 * 60);  //Standard timezone offset in january
	var timezoneOffsetJun = -(dateJun - dateJunLocal) / (1000 * 60); //daylight timezone offset in june
	var daylightSavingsObserved = (timezoneOffsetJun != timezoneOffsetJan);
	var isDaylightSavings = daylightSavingsObserved && (timezoneOffset == timezoneOffsetJun);   	

	//adjust timezone if it is daylight saving time
	if (isDaylightSavings)
		timezoneOffset = timezoneOffset - (timezoneOffsetJun - timezoneOffsetJan);

	document.cookie = 'utcMinuteOffset=' + timezoneOffset;
	
	//alert("Sys offset: " + timezoneOffset + "\n Std Offset: " + timezoneOffsetJan + "\n DL offset: " + timezoneOffsetJun + "\n Observer DLS? " + daylightSavingsObserved + "\n Is DLS Now? " + isDaylightSavings);

}


/**
 * Hide "loading" layer.  
 * This function is called by onload event of Body tag in CTHeader
 */
function hideLoading() {
	ld = document.getElementById("loading");

	if (ld) {
		ld.style.visibility="hidden";
	}
}

/*
Hide "loading" layer.  
This function is called by onload event of Body tag in CTHeader
function showLoading() {
	ld = document.getElementById("loading");

	if (ld) {
		ld.style.visibility="visible";
	}
}
*/

/*
function createLoading() {
	ld = document.getElementById("loading");

	if (!ld) {	//create layer if it hasn't been created yet
		//document.open();
		document.write('<Div id="loading" style="position:absolute;top:150;left:0;width:100%;height:100;visibility:hidden;" align=center>');
		document.write('<div style="background-color:#006699; width:150; color:white; Font-family:Verdana,Arial,Helvetica,sans-serif; font-size:20; text-decoration: blink;" align=center>');
		document.write('&nbsp;<BR>loading...<BR>&nbsp;');
		//document.write('<BR><img src="images/lightbulb.jpg" width="100" height="100" alt="" border="0">');
		document.write('</div>');
		document.write('</div>');
		//document.close();
	}

}
*/

/**
 * set sort and order hidden fields and submit the form
 */
function sortByColumn(formName, column) {
	
	sortByColumn(formName, column, 'Sort', 'Order');
}

/**
 * set sort and order hidden fields and submit the form
 */
function sortByColumn(formName, column, sortFieldName, orderFieldName) {
	var curForm = document.forms[formName];
	
	if (curForm[sortFieldName].value == column) {
		curForm[orderFieldName].value = (curForm[orderFieldName].value == 'A' ? 'D' : 'A');
	}
	else {
		curForm[sortFieldName].value = column;
		curForm[orderFieldName].value = 'A';
	}
	
	curForm.submit();
}


/*
 * Displays time and day in spanTimeDisplay layer
 */
function displayTime() {
	var timeDisplayObj = document.getElementById('spanTimeDisplay');

	if (timeDisplayObj) {
		var curDate = new Date();
		//timeDisplayObj.innerHTML = curDate.toLocaleString();
		setInnerHTML(timeDisplayObj, curDate.toLocaleDateString() + "<BR>[ " + 
																((curDate.getHours() - 1) % 12 + 1) + "<span class=blink>:</span>" + 
																(curDate.getMinutes() < 10? "0": "") + curDate.getMinutes() + " " +
																(curDate.getHours() > 12 ? "PM": "AM") +
																" ]");
		setTimeout(displayTime, 60000);  //update time every 60 seconds
	}
}




function updateTipContent(divContent) {
	var divTipWin = document.getElementById('divx7y8z9Tip');
	var doc;
	var ifrm = document.getElementById("x7y8z9TipFrame");

	if (document.all)
		doc  = document.x7y8z9TipFrame.document;
	else 
		doc = ifrm.contentDocument; 


	var divTipWinInIFrame = doc.getElementById('divx7y8z9Tip');
	setInnerHTML(divTipWinInIFrame, divContent.innerHTML);

	// set new size for the iframe
	var w = getWidth(divContent) + 1;
	var h = getHeight(divContent) + 1	
	setSize(ifrm, w, h);
	
	adjustWinPosition(divTipWin, w, h);

}

function finishTipLoading() {

	//display a waiting message...
	var divTipWin = document.getElementById('divx7y8z9Tip');
	var doc;
	var ifrm = document.getElementById("x7y8z9TipFrame");

	if (document.all)
		doc  = document.x7y8z9TipFrame.document;
	else 
		doc = ifrm.contentDocument; 
		
	var divTipWinInIFrame = doc.getElementById('divx7y8z9Tip');
	var w =	getWidth(divTipWinInIFrame) + 1;
	var h = getHeight(divTipWinInIFrame) + 1;
	setSize(ifrm, w, h) ;

	// showElement(ifrm, getVisibilityString('visible'));
	// setInnerHTML(divTipWin, divTipWinInIFrame.innerHTML);
	
	adjustWinPosition(divTipWin, w, h);
}


/*
 * adjust win position to viewable area
 * divTipWin: reference to the divTipWin element
 * w: current width of the tip win
 * h: current height of the tip win
 */
function adjustWinPosition(divTipWin, w, h) {
  // TODO: combine this with dynlite.alignPopupWindow
	// adjust top,left position depending on the new size 	
	var sT = document.body.scrollTop;
	var sL = document.body.scrollLeft;
	
	var eH = 20;
	var eW = 20;

	var x = getPageOffsetLeft(divTipWin);	
	var y = getPageOffsetTop(divTipWin);

	var moved = false;


	// if the right of the tip window is off the viewable window
	// then show the tip window on the left of the clicked position rather
	// than in the right.
	//alert(x +"-" + w +">="+ sL +"&&"+ x +"+"+ w +"+"+ eW +">="+ document.body.clientWidth +"+"+ sL);
	if (/*x - w >= sL && */ x + w + eW > document.body.clientWidth + sL) {
		//alert("debug x");
		x = x - w + eW;
		moved = true;
	}
	
	if (x < 0) {
		x = 0;
		moved = true;
	}
	
	// if the bottom of the tip window is off the viewable window
	// then show the tip window on top of the clicked position rather
	// than in the bottom.
	if(y - h >= sT && y + h + eH > document.body.clientHeight + sT) {
		y = y - h - eH;
		moved = true;
	}

	if (y < 0) {
		y = 0;
		moved = true;
	}
	
	if (moved) {
		moveTo(divTipWin, x, y);
	}
}


/**
 * tipUrl = where to load the tip
 * width = used to control the iframe size
 */
function loadTip(tipUrl) {
	//alert('error in load tip');
	//display a waiting message...
	//var divTipWin = document.getElementById('divx7y8z9Tip');
	//setInnerHTML(divTipWin, 'waiting...');
	
	frames['x7y8z9TipFrame'].location.href = tipUrl;
}


/** show quick help 
 * tipUrl is defined ---> load on demand
 */
function showTip(ev, id, tipUrl) {
	var tipWinId = 'divx7y8z9Tip';
	var divTipContent = document.getElementById(tipWinId + id);
	var needLoadTip = (tipUrl != '');
	
	if(isOldNS)
		return;
		
	if(!divTipContent || !(el=document.getElementById(tipWinId)))
		return;

	var x=15,y=20;

	
	if(window.event){
		ev=window.event;
		x+=ev.clientX+document.documentElement.scrollLeft+document.body.scrollLeft;
		y+=ev.clientY+document.documentElement.scrollTop+document.body.scrollTop;
	}
	else if(isDOM){
		
		x+=ev.clientX+window.scrollX;
		y+=ev.clientY+window.scrollY;
	}
	else if(isOldNS){
		x+=ev.pageX-window.pageXOffset;
		y+=ev.pageY-window.pageYOffset;
	}
	else 
		return;

	moveTo(el,x,y);

	if (needLoadTip) 
		loadTip(tipUrl);
	else
		updateTipContent(divTipContent);
		
	fadeElement(el,'visible');
}

/* id param is obsolete */
function closeTip(ev, id){

	// reset the currently selected popup menu icon to its default value
	// need to call this inside the closeTip function because
	// the tip and popup feature use the same divElement to display 
	resetCurPopupMenuIcon();
		
	var tipWinId = 'divx7y8z9Tip';
	var tipWinEl;
	if (isIE)
		fadeOut(tipWinId);
	else {
		tipWinEl = document.getElementById(tipWinId);
		showElement(tipWinEl, getVisibilityString(false));
		setPosition(tipWinEl, "-1000px", "-1000px");
	}
}


var curPopupMenuId = 0; // currently selected popup menu id
var curPopupMenuItemImg = null;
var curPopupMenuItemDefaultIcon = null;


function resetCurPopupMenuIcon() {

	if (curPopupMenuItemImg != null) {
		// reset the currently selected popup menu icon to its default value
		curPopupMenuItemImg.src = curPopupMenuItemDefaultIcon;
		curPopupMenuItemImg = null;				
		curPopupMenuItemDefaultIcon = null;
	}
}

/* show the popup menu
	id = the unique subfix of the div that content the popup menu's content
	position = right or left
*/
function showPopupMenu(ev, id, position) {
	curPopupMenuId = id;
	if(isOldNS) {
		alert('Sorry, your browser is an older version and does not support this function.  Please use an up-to-date browser program.');
		return;
	}

	var tipWinId = 'divx7y8z9Tip';
	var popupWinId = 'divx7y8z9PopupMenu' + id;
	var popupWin = document.getElementById(popupWinId);
	var tipWin = document.getElementById(tipWinId);
	var popupWinWidth = getWidth(popupWin);

	var x=0,y=0;  //horizontal and vertical offset
	
	if (position == "right") {
		x -= popupWinWidth;
	}
	
	if(window.event){
		ev=window.event;
		x+=ev.clientX+document.documentElement.scrollLeft+document.body.scrollLeft;
		y+=ev.clientY+document.documentElement.scrollTop+document.body.scrollTop;
	}
	else if(isDOM){
		
		x+=ev.clientX+window.scrollX;
		y+=ev.clientY+window.scrollY;
	}
	else if(isOldNS){
		x+=ev.pageX-window.pageXOffset;
		y+=ev.pageY-window.pageYOffset;
	}
	else 
		return;

	moveTo(tipWin, x, y);
	
	updateTipContent(popupWin);
	showElement(tipWin,'visible');
}

function closePopupMenu(ev, id) {
		curPopupMenuId = 0;
		closeTip(ev, id);
}


function togglePopupMenu(ev, id, position, icon, iconActive) {

	var imgIcon = document.getElementById('imgx7y8z9PopupMenu' + id);
	
	 if (id == curPopupMenuId) {
		// close the currently openned popup menu
		closePopupMenu(ev, id);
		// imgIcon.src = icon;
	 } else {	
		resetCurPopupMenuIcon();																					
	 	curPopupMenuItemImg = imgIcon;				// currently selected image
		curPopupMenuItemDefaultIcon = icon;		// default icon
																					// notes that this image need to be reset to its default icon
																					// whenever the tip window is closed (see closeTip() function)
		
		showPopupMenu(ev, id, position);
		imgIcon.src = iconActive;
	 }
}


// var timeOutCounter = 0;

function sessionTimeOutMessage (timeLeftSecs) {
	var minsLeft = Math.floor(timeLeftSecs /60);

	var expTime = new Date();
	var mm = expTime.getMinutes();	//get current minutes
	

	expTime.setMinutes(mm + minsLeft);	//advance time

	
	// display popup
		result = confirm("Your session is expiring in " + minsLeft + 
						" minutes (by " + 
						((expTime.getHours() - 1) % 12 + 1) + ":" + 
						(expTime.getMinutes() < 10? "0": "") + expTime.getMinutes() + " " +
						(expTime.getHours() > 12 ? "PM": "AM") +
						").  \n\nPress [ok] to continue working, or\npress [cancel] to let your session expire.");
		
		if(result) {  //user wants to keep the session alive
			document.sessionExpireImage.src = "__blank.cfm";
			startSessionTimeOutCounter(timeLeftSecs * 4);	//restart the count.  timeOutSecs = .25 * timeOutLeft (which is 4 times the other way around)
		} else {
			alert('You will be logged out soon.  Please save your work.');
		} 
}

function startSessionTimeOutCounter(timeOutSecs) {
	//  DEBUG: sessionTimeOutMessage(200);
	
	// timeout message around 75% into the timeout
	setTimeout('sessionTimeOutMessage(' + parseInt(timeOutSecs * .25) + ')', timeOutSecs * .75 * 1000);
	
	/*
	var needToRenew = true;
	var newTimeOut;
	var result;
	timeOutCounter++;
	
	var timeOutCounter = 0;
	if (timeOutCounter == 4) {
		// display popup
		result = alert("session is timing out in 10 minutes");
		
		if(result) 
			timeOutCounter = 0;
		else
			needToRenew = false;
	}
	
	if (needToRenew) {
		if (timeOutCounter == 3)   {
			// 1200000 = 20 * 60 * 1000 ms	=	20 minutes
			// newTimeOut = 1200000;
			newTimeOut = 100;
		} else {
			// 1800000 = 30 * 60 * 1000 ms	=	30 minutes
			// newTimeOut = 1800000;
			newTimeOut = 100;
		}
	}
	
	if (needToRenew) {
		frames['x7y8z9SessionControlFrame'].location.href = "__blank.htm";
		setTimeout(startSessionTimeOutCounter, newTimeOut);
	}
	*/
}

// this helper function will trim a given string (remove all
// left and right trailing characters, replace all double spaces
// by single space.
function trimName(s) {
	
	k = s.indexOf('  ');
	while (k >= 0) {
		s = s.substring(0, k)  + s.substring(k+1, s.length);
		k = s.indexOf('  ');
	}
	// remove left trailing
	while (s.length > 0 && s[0] == ' ') s = s.substring(1, s.length);
	// remove right traliling
	while (s.length > 0 && s[s.length-1] == ' ') s = s.substring(0, s.length-1);		
	return s;
}

var __debug_popup = false;			// set this var to true to enable debugging supports

// these elements can be found in CTForm.cfm and mainpopup.cfm
var __mainPopupWinId = 'x7y8z9MainPopup';
var __mainPopupWinTitleId = 'x7y8z9PopupWinTitle';
var __mainPopupWinIfrm = 'x7y8z9PopupIfrm';
var __mainPopupWinTable = 'x7y8z9PopupTable';

function setPopupWindowSize(ifrmId, width, height) {
	var popupIfrmElement = document.getElementById(ifrmId);	// an element which has visibility property	
	setSize(popupIfrmElement, width, height);
}

function setMainPopupWindowSize(width, height) {
	setPopupWindowSize(__mainPopupWinId, width, height); 
}


// hide the specified popup window 
function hidePopupWindow(winId) {
	var popupIfrmElement = document.getElementById(winId);
	
	if (!popupIfrmElement)
		return;
		
	showElement(popupIfrmElement, getVisibilityString(false));
	setPosition(popupIfrmElement, "-1000px", "-1000px");
}

function hideMainPopupWindow() {
	hidePopupWindow(__mainPopupWinId);
}


// this method is called when the popup window finishes loading
function finishPopupWindowLoading(winId, winTitleId, newTitle, width, height) {

	if (__debug_popup) {
		// print tracing 
		alert("finishPopupWindowLoading(\n" + winId + ", \n" + winTitleId + ", \n" + newTitle + ", \n" + width+ ", \n" + height + ");");
	}
	
	var popupIfrm = getIFrame(winId);								// element which has location property 
	var popupIfrmElement = document.getElementById(winId);	// an element which has visibility property	
	var popupIfrmDoc = getIFrameContentDocument(popupIfrm);	// the document object of the iframe

	var divTitle;

	if (!popupIfrmElement || !popupIfrm || !popupIfrmDoc) {
		alert('could not find popupwindow');
		return;
	}
	
	// change window title
	// change window's status -> loading
	if (winTitleId != "") {
		divTitle = popupIfrmDoc.getElementById(winTitleId);
		if (divTitle) 
				setInnerHTML(divTitle, newTitle);			
	}
	
	if (width != "" && height != "")
	{
		setSize(popupIfrmElement, width, height);
		
		
		if (isSafari) {
			// quick fix for salari browser
			// 		need to set the x7y8z9PopupTable element to a fixed-length height instead of 100%
			//		because salari cannot adjust the table height automatically when we change the height of the
			// 		outer document
			var tab = popupIfrmDoc.getElementById(__mainPopupWinTable);	// assume that the current window is the main 
																																	// popup window. This assumption helps to
																																	// avoid adding a new parameter to all existing functions
			var h = parseInt(height) - 40;
			var w = parseInt(width) - 40;
			setSize(tab, w, h);
		}		
	}
}



function finishMainPopupWindowLoading(newTitle, width, height) {
	/*
	if (width == "")	
		width = "350px";

	if (height == "")
		height = "350px";
	*/
	finishPopupWindowLoading(__mainPopupWinId, __mainPopupWinTitleId, newTitle, width, height);
}


// show the popup window 
function showPopupWindow(winId, winTitleId, ifrmId, url, waitMessage) {
	if (__debug_popup) {
		// print tracing 
		alert("showPopupWindow(\n" + 
						winId + ",\n" + 
						winTitleId + ",\n" + 
						ifrmId + ",\n" + 
						url + ",\n" + 
						waitMessage + ");");
	}

	var popupIfrm = getIFrame(winId);								// element which has location property 
	var popupIfrmElement = document.getElementById(winId);	// an element which has visibility property	
	var popupIfrmDoc = getIFrameContentDocument(popupIfrm);	// the document object of the iframe

	var divTitle;
	var contentIfrm;

	// display window
	showElement(popupIfrmElement, getVisibilityString(true));
		
	// change window's status -> loading
	if (winTitleId != "") {
		divTitle = popupIfrmDoc.getElementById(winTitleId);
		if (divTitle) {
			if (waitMessage == "")
				waitMessage = "loading...";
				setInnerHTML(divTitle, waitMessage);			
		}
	}

	if (document.all) 
		contentIfrm = popupIfrmDoc.frames[ifrmId];
	else 
		contentIfrm = popupIfrmDoc.getElementById(ifrmId).contentDocument;

	/*	
	if (document.all) 
		contentIfrm = popupIfrmDoc.frames[ifrmId];
		// contentIfrm = getIFrameContentDocument(popupIfrmDoc.getElementById(ifrmId));
	else 
		contentIfrm = getIFrameContentDocument(popupIfrmDoc.getElementById(ifrmId));
	*/
	if (!contentIfrm)	{
		alert("could not find popup window's content iframe");
		return;
	}
	
	// load iframe, after finished loading, the ifrm need to 
	// fire the finishPopupWindowLoading function
	contentIfrm.location.href = url;
}


function togglePopupWindow(winId, winTitleId, ifrmId, url, waitMessage, top, left) {
	//popupWin = getIFrameElement(winId);
	popupWin = document.getElementById(winId);
	var popupIfrmElement ;
	
	if (!popupWin) {
		alert('could not find popupwindow');
		return;
	}
	
	if (isVisibleElement(popupWin)) {
		hidePopupWindow(winId);
	} else {
		popupIfrmElement = document.getElementById(winId);	// an element which has visibility property	
		setPosition(popupIfrmElement, top, left);
		showPopupWindow(winId, winTitleId, ifrmId, url, waitMessage);
	}
}


function toggleMainPopupWindowAndSetTopLeft(url, waitMessage, top, left) {
	togglePopupWindow(__mainPopupWinId, __mainPopupWinTitleId, __mainPopupWinIfrm, url, waitMessage, top, left);
}

function toggleMainPopupWindow(url, waitMessage) {
	togglePopupWindow(__mainPopupWinId, __mainPopupWinTitleId, __mainPopupWinIfrm, url, waitMessage, "350px", "200px");
}

function gotoClassPage_Faculty(clsId, cmd, urlSuffix) {
	var url = "";

	if (cmd == "roster") {
		url = "gradebooklistshowf.cfm?view=students&id=" + clsId;
	} else	if (cmd == "message") {
		url="msglistshowc.cfm?view=A&gid='C" + clsId + "'";
	} else	if (cmd == "coursework") {
		url = "gradableslistc.cfm?Cls_Id=" + clsId;
	} else	if (cmd == "grade") {
		url = "rostershelleditf.cfm?purpose=showpoints&Cls_Id=" + clsId;
	} else	if (cmd == "team") {
		url = "rostershelleditf.cfm?purpose=team&Cls_Id=" + clsId;
	}
	
	if (url != '')
		document.location = url + urlSuffix;
	
}

function gotoClassPage_Student(clsId, cmd, urlSuffix) {
	var url = "";

	if (cmd == "message") {
		url="msglistshowc.cfm?view=A&gid='C" + clsId + "'";
	} else	if (cmd == "gradable") {
		url = "gradableslistc.cfm?purpose=gradable&view=bydate&cls_id=" + clsId;
	} else	if (cmd == "grade") {
		url = "gradableslistc.cfm?purpose=grade&view=all&Cls_Id=" + clsId;
	}
	
	if (url != '')
		document.location = url + urlSuffix;
}

/*
	java script function for CTItemSelector
*/
function itemSelector_Select(curPage, queryString, paramName, selectedValue, rsUrlToken) {
	var len = selectedValue.length;
	var href;
	
	if (len > 4 && selectedValue.substring(0, 4) == "URL:") {
		// the current item is an url
		href = selectedValue.substring(4, len);
	}
	else {
 		href = curPage;
		if (href.substring(len - 1, len) != "?")
			href = href + "?";

		if (queryString != "")
			href = href + queryString + "&";
		href = href + paramName + "=" + selectedValue;
			
	}
	
	document.location = href + "&" + rsUrlToken;
}

/*	
This function collapses one layer and expands the other layer.

Side Effect:
	Set the value of a form field (if exists) to the given value
	
Parameters: 
			layerToShowName: expand this layer
			layerToHideName: collapse this layer
			formFieldName: set the value of this form field to the fieldValue parameter
			fieldValue: (See formFieldName)
*/
/*
function swapLayers(formName, layerToShowName, layerToHideName, formFieldName, fieldValue) {
	var curForm = document.forms[formName];

	//update the form field
	// var formField = eval("document.<CFOUTPUT>#lcFormName#</CFOUTPUT>." + formFieldName);
	formField = curForm[formFieldName);
	if (formField) {
		formField.value = fieldValue;
	}
	
	var layerToShow = document.getElementById(layerToShowName);
	var layerToHide = document.getElementById(layerToHideName);
	layerToShow.style.display="";
	layerToHide.style.display="none";
}
*/

/* invoke a void url (a url that doesn't return anything) thru image */
function invokeVoidUrl(url) {
	var obj = document.getElementById("imgVoidUrl");
	if (obj)  {
		obj.src = url;	//invoke url
		obj.src = "images/pixels/pixelblank.gif"; //set this or the image will show a broken image
	}
	else
		alert("Error 9384: Can't communicate with the server...");
}

/* invoke a url (that does return something) thru iframe */
function invokeUrl(url) {
	//renderToggleMainPopupWithEverything(url, " ", " ", 50, 50, 100, 100);
	toggleMainPopupWindowAndSetTopLeft(url + "&ispopup=yes&popuptitle=hello&popupwidth=50px&popupheight=50px", ' ', '50px', '50px');
	//showPopupWindow(__mainPopupWinId, __mainPopupWinTitleId, __mainPopupWinIfrm, url, 'Please wait');
}


//Macromedia functions
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


//--------------------------------- top menu functions ---------------------------------------
	
	//posSubmenuShell();
	
	//obsolete
	function posSubmenuShell() {
		submenuShell = document.getElementById('submenuShell');
		cornerImage = document.getElementById('cornerImage');
		
		alert(submenuShell);
		alert(cornerImage);
		moveTo(submenuShell, getTop(cornerImage), getLeft(cornerImage));
		
	}
		
	//show submenu 
	//params: 
	//	newSubmenuName: submenu name
	//
	//Called on the mouseover event of top menu items to show their submenu
	function showMenu(topMenuTDId, newSubmenuName)	{
    	var newSubmenu = document.getElementById(newSubmenuName);
		
		var topMenuTD = document.getElementById(topMenuTDId);
		//topMenuTD.className = 'topmenuitemuncurrent';
			
		showMenuObj(topMenuTD, newSubmenu, "click");
	}

	function toggleSubmenuWarningDisplay(action) {
		if (origSubmenu) {
			el = document.getElementById('spanSubmenuWarning');
			if (el.style.display) {
				if (action) {  //if action is defined
					el.style.display = action;
					if (action == 'none') 
						//change class to uncurrent submenu
						origSubmenu.className = 'DivSubmenuContainerCurrent';
					
					else
						//change class to uncurrent submenu
						origSubmenu.className = 'DivSubmenuContainerUncurrent';
					
				}
				else { //action is not defined
					if (el.style.display == 'none') {
						//show flashing arrow
						el.style.display = 'block';

						
						//change class to uncurrent submenu
						origSubmenu.className = 'DivSubmenuContainerUncurrent';

						if (submenuWarningDisplayTimeout != null) 
							clearTimeout(submenuWarningDisplayTimeout);
						submenuWarningDisplayTimeout = setTimeout("toggleSubmenuWarningDisplay('none');", 10000);
					}
					else {
						//hide flashing arrow
						el.style.display = 'none';

						//change class to current submenu
						origSubmenu.className = 'DivSubmenuContainerCurrent';
					}
				}
			}
			
			//click timeout to avoid multiple timeouts when user keeps clicking
			//if (action == 'none') 
			//	
		}
	};

	//show the submenu object.
	//params: 
	//	newSubmenu: submenu object
	//
	//Called by initSubmenu and showMenu
	function showMenuObj(topMenuTD, newSubmenu, action)	{
		//alert(displayedSubmenu);
		//if there are any setTimeouts to revert the menu, cancel it
		if (submenuTimeoutId != null) {
			clearTimeout(submenuTimeoutId);
		}

		if (displayedSubmenu == newSubmenu ) {  //if displayed submenu has not changed
			if (action == "click") {  //if user has clicked a menu again and this was not called by setTimeout
				doHideMenu = false;
				if (displayedSubmenu) { //if a submenu is in view
					if (newSubmenu == origSubmenu) { //orig menu was clicked
						//hide submenu and fade it in to get users attention
						//showElement(displayedSubmenu, 'hidden');
						//fadeElement(displayedSubmenu, 'visible', 0.5);
			
						//alert('Please select an option from the sub-menu');
						
						//make the submenu blink and move
						//tempText = origSubmenu.innerHTML;
						//origSubmenu.innerHTML = "" + displayedSubmenu.innerHTML;
						//setTimeout('setSubmenuInnerHTML(tempText);', 1000);
						
						toggleSubmenuWarningDisplay();
					}
					else { 	//new menu was clicked again.  hide it submenu and switch over to orig submenu.  
						// Do NOT use hideMenu=true becase it would hide all submenus.

						showMenuObj(origMenu, origSubmenu, "auto");
					}
					return;
					
				}
				
			}
			else	//if action = auto, called by set timeout
				doHideMenu = false;
		}
		else {  //if displayed submenu has changed
			
			doHideMenu = true;
		}
	
		if (doHideMenu) {
			
			//alert('hiding');
			if (topMenuTD) { // if topMenuTD is valid
				if (topMenuTD == origMenu) { // if origmenu was selected
					topMenuTD.className = g_topMenuTDBaseClass[topMenuTD.id] + 'selected'; //highlight the selected menu to orig top menu style (generic baseclass is topmenuitem)
				} else {
					topMenuTD.className = g_topMenuTDBaseClass[topMenuTD.id] + 'uncurrent'; //highlight the selected menu to hovered top menu style
				}
			}
			
			if (newSubmenu) {  //if submenu exists
				fadeElement(newSubmenu, 'visible', 0.2);//display the hovered submenu
				//showElement(newSubmenu, 'visible');  
			}
			
			if (displayedSubmenu) { //if a submenu is already in view, hide it
				if (displayedMenu) 
					displayedMenu.className = 'topmenuitemoff';  //unselect displayed topmenu
		
				fadeElement(displayedSubmenu, 'hidden', 0.2);//hide the last submenu
				//showElement(displayedSubmenu, 'hidden');  
			}
			
			//if (origSubmenu) { //if original submenu is set by the caller page
				if (topMenuTD != origMenu) { //if displayed submenu is different from the original, hide the original
					if (origMenu)
						origMenu.className = 'topmenuitemorigoff';  //unselect displayed topmenu
				
					if (origSubmenu)
						fadeElement(origSubmenu, 'hidden', 0.2);//hide the last submenu
					//showElement(origSubmenu, 'hidden');  
				}
				
				//revert to current submenu in a few seconds
				submenuTimeoutId = setTimeout('showMenuObj(origMenu, origSubmenu, "auto")', 15000);
			//}
			
			displayedSubmenu = newSubmenu;
			displayedMenu = topMenuTD;
		}
	}
//--------------------------------- end: top menu functions ---------------------------------------

// repeat a string N-times
function repeatStr(s, n) {
   var r = "";
   while (--n >= 0) 
	r += s;
   return r;
}

//replaces substr1 with substr2 in string str
function replaceStr(str, substr1, substr2) {
	var re="/" + substr1 + "/";
	return str.replace(re, substr2);
/*
p = lbl.indexOf('|');
		while (p >= 0) {
			//alert(p);
			lbl = lbl.substring(0, p) + "_" + lbl.substring(p+1);
			p = lbl.indexOf('|');
		}
*/
}


	
function checkBookImg(imgName, numIterations, defaultUrl) {
	oImgBook = document.images[imgName];
	if (oImgBook.complete) { // if image has been fully loaded --->
//alert(oImgBook.height);
		if (oImgBook.height <= 35) { // if image is not found on ecampus, for some reason it's height is set to 30 --->

			// load image from ecampus --->
			oImgBook.src = defaultUrl;
			
			if (defaultUrl != 'http://www.tbxn.com/images/image_not_available.gif') {
				//since this image is being loaded from defaultURL, checkBookImg again to make sure it loads, this time set defaultUrl to image_not_available in case image is not loading 
				setTimeout('checkBookImg("' + imgName + '",3,"http://www.tbxn.com/images/image_not_available.gif")', 500);
			}
		}
	}
	else { // loading is not complete.  try again in 1 second --->
		if (numIterations >= 0) {  // avoid infinite loop by putting a counter on number of trials --->
			setTimeout('checkBookImg("' + imgName + '",' + (numIterations - 1) + ',"' + defaultUrl + '")', 500);
		} else {  // image is not loading in the time given.  Show an image of a book --->
			oImgBook.src = 'http://www.tbxn.com/images/image_not_available.gif';
		}
	}
}


/* star "on" on mouse over */
function overstar(idElement,rate){
	for(i=1; i<=5; i++){
		if(i <= rate){
			document.getElementById(idElement+'_'+i).style.backgroundPositionY = "20px";
		} else {
			document.getElementById(idElement+'_'+i).style.backgroundPositionY = "10px";
		}
	}
}

/* star "off" on mouse out */
function outstar(idElement,rate){
	if(outstar_check == 0){
		rate = rate;
	} else {
		rate = rateN;
	}
	for(i=1; i<=5; i++){
		if(i<=rate){
			document.getElementById(idElement+'_'+i).style.backgroundPositionY = "30px";
		} else {
			document.getElementById(idElement+'_'+i).style.backgroundPositionY = "10px";
		}
	}
}


function collapseExpand(parent, child, expanderElement, expandInnerHtml, collapseInnerHtml) {	
	var parentObj = document.getElementById(parent);  
	
	var childObj = document.getElementById(child);  
	//alert(childObj);
	
	if (childObj) {
		var parentIcon = document.getElementById(expanderElement);  
		if (childObj.style.display == "") {
			setInnerHTML(parentIcon, expandInnerHtml);
			childObj.style.display = "none";
		}
		else {
			setInnerHTML(parentIcon, collapseInnerHtml);
			childObj.style.display = "";
		}
	}
}

function bindError_siteGallery(p1, p2) { 
	alert('Cannot load some websites.  Please reload the page.'); 
}

