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, boxname) {
	//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');
}