/* Preload the images for close i-icon button */
var imgIicon_close = new Image();
imgIicon_close.src = "http://cache.netacad-cdn.net/cnamsassets/images/common/btn_iicon_close.gif";

/*
 * Filename: global.js
 * This file contains javascript functionality that are used throughout the CNAMS site.
 * This functionality includes:
 *	- Checking the browser and linking to the appropriate css file.
 *	- Workaround for Netscape 4 window resize bug.
 *	- Function for a small popup window
 *	- Function to do a custom form action
 *	- Functions to select/deselect all checkboxes
*/


// *** Start: Code for checking which CSS file to use ***
// get platform and browser versions
var platform = navigator.platform.substr(0,3);
var browserName = navigator.appName;
var browserVer;

//for IE 5, the first number in "navigator.appVersion" is 4.0, so need to parse the string further 
if (navigator.appVersion.indexOf('MSIE 5') != -1) {
	browserVer=5;
}
else {
	browserVer = parseInt(navigator.appVersion);
}

// Platform is Windows
if (platform=="Win") {

	//Netscape 4 on Windows
	if ((browserName=="Netscape") && (browserVer <=4)) {	
		document.write('<link rel="stylesheet" type="text/css" href="/cnamsassets/css/pc_ns_en.css" >');
	}
	//IE, NS6, (or any other browsers) on Windows 
	else {
		document.write('<link rel="stylesheet" type="text/css" href="/cnamsassets/css/pc_ie_en.css" >');
	}
} 
	
// Platform is Macintosh
else if (platform=="Mac") {

	//IE5 or NS6 on Mac
	if (browserVer >=5) {
		document.write('<link rel="stylesheet" type="text/css" href="/cnamsassets/css/pc_ie_en.css" >');
	}
		
	//Netscape 4, IE4, and all other browsers on Mac
	else {
		document.write('<link rel="stylesheet" type="text/css" href="/cnamsassets/css/pc_ie_en.css" >');
	}
} 

// All other platforms
else {
	document.write('<link rel="stylesheet" type="text/css" href="/cnamsassets/css/pc_ie_en.css" >');
}

// *** End: Code for checking which CSS file to use ***


// *** START: Code for Netscape 4 Resize bug ***
// This is a workaround for a Netscape 4 bug that causes 
// stylesheets to be corrupted when the window is resized.
if (document.layers) {
	var widthCheck = window.innerWidth;
	var heightCheck = window.innerHeight;
	window.onResize = resizeFix;
}

function resizeFix() {
	if (widthCheck != window.innerWidth || heightCheck != window.innerHeight)
	history.go(0);
}

// *** End: Code for Netscape 4 Resize bug ***


// *** START: Code for Opening a new browser popup window ***
// This function is used to open a new small browser window. 
// USAGE: 
//    <a href="javascript:openPopupWin('viewcurriculluminfo.shtml');">View Curriculum Information</a>
//
function openPopupWin(URL) {
	newPopupWin = window.open(URL,'popup','width=643,height=468,scrollbars');
	newPopupWin.focus();

}
// *** End: Code for Opening a new browser popup window ***


// *** START: Code for Opening a new browser window without browser controls that fills up the whole screen ***
// This function is used to open a new browser window which fills up the whole screen area, mostly used in the exam activation module
// USAGE: 
//    <a href="javascript:openFullScreenWin('http://www.yahoo.com');">Open Yahoo! in a new window</a>
//
function openFullScreenWin(url) {
if (document.layers) {
 	windowWidth = screen.width - 9;
	windowHeight = screen.height - 58;
	windowprops = "scrollbars,alwaysRaised,titlebar=0,top=0,left=0,width=" + windowWidth + ",height=" + windowHeight;
	myNewWin = window.open(url, "popupPage", windowprops);
	myNewWin.focus()
}
   else if (document.all) {
  	myNewWin = window.open(url, "popupPage", 'fullscreen=yes,scrollbars=yes');
  	myNewWin.focus()
   }
   else {
   // Fix for Netscape 7.0
  	myNewWin = window.open(url, "popupPage", 'width=' + window.screen.availWidth+',height='+window.screen.availWidth+',scrollbars=yes, screenX = 0, screenY = 0');
  	myNewWin.focus();
   }
}


// *** End: Code for Opening a new browser window without browser controls that fills up the whole screen ***

// *** START: Code for Opening a new browser window for the About This Page content ***
// This function is used to open a new browser window which fills up the whole screen area, mostly used in the exam activation module
// USAGE: 
//    <a href="javascript:openAboutWin('http://www.yahoo.com');">About This Page</a>
//
function openAboutWin(url) {
	aboutWin = window.open(url, "popupPage", "scrollbars,width=460,height=500");
	aboutWin.focus();	
}
// *** End: Code for Opening a new browser window for the About This Page content ***

// *** START: Code for Opening a new browser popup window for Wego Links with all browser controls***
// This function is used to open a new browser window which would open a Wego Link. 
// Has been implemented for opening WEGO Links in a single target window.
// USAGE: 
//    <a href="javascript:openWegoWin('http://usinstr.academycommunity.net/');">Message Title</a>
//
function openWegoWin(url) {
	windowprops = "toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes";
	wNewWin = window.open(url, "wPopupPage", windowprops);
	wNewWin.focus();	
}
// *** End: Code for Opening a new browser popup window for Wego Links with all browser controls***

// *** START: Custom form action function ***
// This function is used to change the action URL ('theUrl') for 'theForm'. 
// It is used on pages where there is one form, but different buttons trigger different actions. 
// Arguments: 'theForm' is the name of the form containing the buttons.
//			  'the Url' is the URL of the action template associated with the button.
// USAGE: 
//    <input type="button" name="cancel2" value="Cancel" class="button" onClick="doFormAction('contactFrm','contacts.jhtml');" />
//
function doFormAction(theForm, theUrl) {
	eval('document.' + theForm + '.action=\"' + theUrl + '\"');
	eval('document.' + theForm + '.submit()');
	}
// *** END: Custom form action function ***

// *** START: Functions to select/deselect all checkboxes ***
// This function is used to check or uncheck all the checkboxes names of which are provided in an array
// Arguments: 'formname' is the name of the form containing the buttons.
//	      
// USAGE: 
//    
//

function SelectAll(formName,getArr,whattodo) {
	getArr = eval(getArr);
	
	if (whattodo == "check") {
		for (var i=0;i<getArr.length;i++) {
		myBox = getArr[i];
		var e = eval("document." + formName + "." + myBox);
		e.checked = true;
		}
	}
	else {
		for (var i=0;i<getArr.length;i++) {
		myBox = getArr[i];
		var e = eval("document." + formName + "." + myBox);
		e.checked = false;
	}			
}
}
// *** END: Functions to select/deselect all checkboxes ***

// *** START: Second set of functions to select/deselect all checkboxes ***
// This function is used to check or uncheck all the checkboxes having the same name but diff values
// Arguments:'formname' is the name of the form containing the buttons.
//		'chkset' is the name of the checkboxes you want to modify the state of
//		'whattodo' tells the function whether to check or uncheck the set of checkboxes
// USAGE: 
//    <a href="javascript:SelectAllSame('myform1','chk1','check');">Select All</a><img src="http://download20fp.netacad.net/cnamsassets/images/common/spacer.gif" width="10" height="1" alt="" border="0" /><a href="javascript:SelectAllSame('myform1','chk1','uncheck');">Deselect All</a>
//

function SelectAllSame(formName,chkset,whattodo) {
	count = 0;
	if(eval("document." + formName + "." + chkset)) {
	if(eval("document." + formName + "." + chkset + ".length"))
	{
		count = eval("document." + formName + "." + chkset + ".length");
		}
	else		
	{
	if (whattodo == "check") {
		eval("document." + formName + "." + chkset + ".checked = true");
		return;
	}
	else
	{
		eval("document." + formName + "." + chkset + ".checked = false");
		return;	
	}
	}	
	}
	if (whattodo == "check") {
		for (var i=0;i<count;i++) {
		var e = eval("document." + formName + "." + chkset + "[" + i + "]");
		e.checked = true;
		}
	}
	else {
		for (var i=0;i<count;i++) {
		var e = eval("document." + formName + "." + chkset + "[" + i + "]");
		e.checked = false;
	}
}
}
// *** END: Functions to select/deselect all checkboxes ***
