/*
 * 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="/contentassets/css/pc_ns_en.css" >');
		
	}
	//IE, NS6, (or any other browsers) on Windows 
	else {
		document.write('<link rel="stylesheet" type="text/css" href="/contentassets/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="/contentassets/css/pc_ie_en.css" >');
	}
		
	//Netscape 4, IE4, and all other browsers on Mac
	else {
		document.write('<link rel="stylesheet" type="text/css" href="/contentassets/css/pc_ie_en.css" >');
	}
} 

// All other platforms
else {
	document.write('<link rel="stylesheet" type="text/css" href="/contentassets/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)
	document.location.href = document.location.href;
}

// *** 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');
}
function openLoginPopup(URL) {
	newLoginPopup = window.open(URL,'popup','width=205,height=205,screenX=450,screenY=450,left=550,top=300,resizable=no,status=no,toolbar=no,scrollbars=no');
}
// *** End: Code for Opening a new browser popup window ***


// *** 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.
//	      '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 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 ***



// Get the BaseTag, if specified in the current page
function get_baseTag() {
	var baseTag = "";
	if ( document.all ) {	
		var baseTagsCol = document.all.tags("BASE");
		if (baseTagsCol.length!=0){
			baseTag = baseTagsCol[0].href;
		}
	} else if (window.opera) {
		var baseTag
		if (document.getElementById('basehref')){
			baseTag = document.getElementById('basehref').href;
			baseTag = baseTag.substring(0, baseTag.length-1);
		}
	}
	return baseTag;
}

// Drop-down location.href redirection
function dropdown_redirect(select_name,reset) {
	if (reset == null) { reset = true };
	var theselect=eval(select_name);
	var tmp=theselect.selectedIndex;
	if (reset) { theselect.options[0].selected=true; }
	if(theselect.options[tmp].value != "") {
		location.href=get_baseTag()+theselect.options[tmp].value;
	}
}

// Area drop-down redirection
var areanav_current = 0;
function set_areanav_current( index ) {
	// xsl position starts at 1, array index starts at 0
	areanav_current = index-1;
}
function areanav_redirect(select_name) {	
  var theselect=eval(select_name);
  var tmp=theselect.selectedIndex;
  if(theselect.options[tmp].value != "") {
	 theselect.options[areanav_current].selected=true;
	 location.href=get_baseTag()+theselect.options[tmp].value;
  }
}
	// Rollover Images
    	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];}}
	}

	function MM_findObj(n, d) { //v4.0
	  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 && document.getElementById) x=document.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];}
	}
