var popWin;
var lastColorUsed;
var blocked;
var isIE;

blocked = false;

//when checking if the browser is IE be sure to exclude the Mac IE browser
//because it doesnt have the same support as the PC side
isIE = ((document.all) && (navigator.platform == 'Win32'));

function highlight(row, enable) {
	//enable is true when the mouse goes over a row, and false when it leaves the row
	if (enable) {
		lastColorUsed = row.style.backgroundColor;
		row.style.backgroundColor = '#FFFFCC';
	}
	else { row.style.backgroundColor = lastColorUsed; }
}

function popUpBlocked(winReference) {
	//only used in this include
	var isNull, isUndefined;
	
	isUndefined = ( typeof(winReference) == "undefined" );
	isNull = ( winReference == null );
	
	if ( !isUndefined && !isNull ) {
		if ( !winReference.closed ) { return false; }
		else { return false; }
	}else if ( isUndefined ) { return true; }
	else if ( isNull ) { return true; }	
}

function pop(url,menustatus,height,width) {
	//this one is used all over the place
	if (menustatus == '') menustatus = 'no';
	if (height == '') height = '600';
	if (width == '') width = '800';
	
	if (url == 'login.aspx?format=popup') {
		height = '350';
		width = '620';
	}
	
	if (pop.arguments.length > 4) {
		if (pop.arguments[4] == 'documentview') { popWin = window.open(url, pop.arguments[4], 'menubar=' + menustatus + ',scrollbars=yes,resizable=yes,status=no,height=' + height + ',width=' + width); }
		else { popWin = window.open(url, pop.arguments[4], 'menubar=' + menustatus + ',scrollbars=yes,resizable=yes,status=yes,height=' + height + ',width=' + width); }
	}
	else { popWin = window.open(url, 'popWin', 'menubar=' + menustatus + ',scrollbars=yes,resizable=yes,status=yes,height=' + height + ',width=' + width); }
	
	blocked = popUpBlocked(popWin);
	
	if (blocked) { alert('It appears that you may have popup blocking software installed, which is closing important windows.  To use this website properly you will need to configure your popup blocker to work with this website, or disable it temporarily.'); }
	else { popWin.focus(); }
}

function popall(url,menustatus,height,width) {
	//this one is used in documentview to open a web/opac link with all the things turned on
	if (menustatus == '') menustatus = 'no';
	if (height == '') height = '600';
	if (width == '') width = '800';
	
	popWin = window.open(url, popall.arguments[4], 'menubar=yes,toolbar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes,height=' + height + ',width=' + width);

	blocked = popUpBlocked(popWin);
	
	if (blocked) { alert('It appears that you may have popup blocking software installed, which is closing important windows.  To use this website properly you will need to configure your popup blocker to work with this website, or disable it temporarily.'); }
	else { popWin.focus(); }
}

function post(cid,messageid,level,action) {
	//used only in coursepage.aspx, functions_coursepage.aspx, classes_chatuser.aspx, and the other bb pages
	window.open('messages.aspx?cid=' + cid + '&messageid=' + messageid + '&level=' + level + '&action=' + action,'messages','menubar=no,scrollbars=yes,resizable=yes,status=no,height=600,width=800');
}

function closeAndFocus() {
	//used all over the place
	if (closeAndFocus.arguments.length > 0) {
		if(window.opener.document.images['selectedImage'] != null) {
			if (closeAndFocus.arguments[0] == 'yes') { window.opener.document.images['selectedImage'].src = 'images/textbox_full.gif'; }
			else { window.opener.document.images['selectedImage'].src = 'images/textbox_empty.gif'; }
		}
	}
	top.window.close();
	top.window.opener.focus();
}

function closeFocusRefresh() {
	//used in the footer, messages.aspx and login
	window.close();
	window.opener.focus();
	window.opener.location.reload();
}

function reloadParent() {
	if (reloadParent.arguments.length == 0) { window.opener.location = window.opener.location; }
	else { window.opener.location = reloadParent.arguments[0]; }
}

function email(queryString) {
	//used in a bunch of places
	pop('email.aspx?' + queryString,'','','','email');
}

function quickHelp(event,key) {
	var xVal;
	var yVal;
	var helpWin; 
	
	if (!document.layers) {
		xVal = event.screenX;
		yVal = event.screenY;
	}
	else {
		xVal = event.pageX;
		yVal = event.pageY;
	}
	xVal = (xVal - 200);
	yVal = (yVal - 75);
	
	helpWin = window.open('help_content.aspx?key=' + key, 'quickHelp', 'menubar=no,scrollbars=yes,resizable=yes,status=no,height=150,width=400,top=' + yVal + ',screenY='+ yVal + ',left=' + xVal + ',screenX=' + xVal);
	helpWin.moveTo(xVal,yVal);
	helpWin.focus();
	
	return true;
}

function checkAll() {
	var myString;
	for (i=0; i < document.form1.length; i++) {
		if (document.form1[i].type == 'checkbox') {
			//make sure to only check those boxes that have a name of "*checkbox_moddel*"
			myString = document.form1[i].name;
			if (document.form1[i].name.indexOf('checkbox_moddel') != -1) {
				document.form1[i].checked = document.form1.checkAllBox.checked;
			}
		}
	}
}

function showAlert(format,parameter) {
	switch (format) {
		case 'fileAttachDuringCreate':
			alert('You must save the current ' + parameter + ' before you can attach files to it.');
			break;
		
		case 'tooManySelected':
			alert('This tab is only accessible when working with a single item.');
			break;				
		
		default: alert(format);
	}
}

function clickFooterLink() {
	//this is used in header.ascx to tie the header close link to the footer close link, that way they have the same functionality
	//it only works in IE for now, so we just use the normal close link for FireFox/Netscape/Mac.  make sure to verify that the DIV
	//is an object before trying to refernece it or you will get an error on pages like print_content.aspx that dont contain the DIV at the bottom
	if ((isIE) && (document.getElementById('closeDiv'))) {
		document.getElementById('closeDiv').click();
	}
	else {
		closeAndFocus();
	}
}

function confirmNoSelect() {
	//this is used in the footer to alert people when they are leaving a selection window without saving their selection
	if (confirm('Warning! Using the close window link will not save any selections that you have made.  Click OK to close this window or Cancel to go back and save your selections via the "Save Selections" button.')) {
		closeAndFocus();
	}
}

function closeOnLogout() {
	//this is used to close the window on logout; only used in login.aspx
	if (navigator.appName == 'Microsoft Internet Explorer') { top.window.close(); }
}

function popExport() {
	//this is used by a lot of scripts to open the export window
	pop('export.aspx','yes','500','700','export');
}

function selectRow(clientID) {
	//only call the toggle if they have clicked on a TD tag, not a checkbox
	//FireFox seem to handle the window.event, so we need to make sure its an object
	//http://www.webswapp.com/codesamples/viewsource.aspx?file=~/codesamples/aspnet20/gridview_multiplerows_selection/vb.aspx
	if(window.event) {
		if (window.event.srcElement.tagName=="TD") {
    		var row = window.event.srcElement.parentNode;
    		var chk = row.cells[0].firstChild.firstChild;
			chk.checked = !chk.checked;
		}
	}
}

function findY(objName) {
	//function to locate an object by name, and return its Y (vertical) location on the page
	//function will return zero if the element is not found
	var curtop = 0;
	var obj = document.getElementById(objName);
	
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) { curtop += obj.y; }
	return curtop;
}

function getWinHeight() {
	//function to return the height of the current window; returns zero if the
	//appVersion cannot be determined as Netscape or Microsoft correctly
	var winH = 0;
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") { winH = window.innerHeight; }
		if (navigator.appName.indexOf("Microsoft")!=-1) { winH = document.body.offsetHeight; }
	}
	return winH;
}

function setListingHeight() {
	//function to set the size of the DIV that contains the datagrid; the function will not execute
	//if the DIV cannot be found.  note, the  "60" listed below will just give us a little buffer so that the window
	//scrollbars do not activate even when the DIV scrollbars are turned on
	if ((isIE) && (document.getElementById('listDiv'))) {
		var currDivHeight = (findY('footerStart') - findY('listDiv'));
		var newDivHeight = (getWinHeight() - findY('listDiv') - (findY('footerStop') - findY('footerStart')) - 60);

		if ((currDivHeight > newDivHeight) && (newDivHeight > 0)) {
			if (newDivHeight < 200) { document.getElementById('listDiv').style.height = '200px'; }
			else { document.getElementById('listDiv').style.height = newDivHeight + 'px';	}
		}
	}
}