/* Preload the images for close i-icon button */
var imgIicon_close = new Image();
// RTN# 070813-000047 | SSL phase 2 | Start | devloper: Mayank
//imgIicon_close.src = "http://cache.netacad-cdn.net/cnamsassets/images/common/btn_iicon_close.gif";
imgIicon_close.src = "/cnamsassets/images/common/btn_iicon_close.gif";
// RTN# 070813-000047 | SSL phase 2 | End

/*
 * 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 ***
function openWin(URL, win_name) {
	var newURL = URL.replace("?","&");
	var url = "/cnams/includes/ChildWindow.jsp?url="+newURL+"&window="+win_name;
	return window.open( url, win_name) ;
}

//QC 1320 | ICG URL throwing error page when accessed from https pages like home pages | Fixed By: Mayank | Reviewed By: Vishal
//code change starts
function openWinAbs(currentDomain, URL, win_name) {
	var newURL = URL.replace("?","&");
	var url = currentDomain + "/cnams/includes/ChildWindow.jsp?url="+newURL+"&window="+win_name;
	return window.open( url, win_name) ;
}
//code change ends

function openExtAbs(currentDomain, URL, win_name) {
        //var newURL = URL.replace("?","&");
        var url = currentDomain + "/cnams/includes/CnamsExternalRedirect.jsp?url="+URL;
        return window.open( url, win_name) ;
}


// RTN# 070813-000047 | SSL phase 2 | Developer: Mayank Rastogi
// Added to change the form action scheme. Can be used where form is being submitted on a particular scheme
// but there are 2 button which leads to pages with different schemes. e.g. if form action is secure i.e. https
// and we have 2 buttons submit and cancel where submit takes to the secure page while cancel takes to the non-secure page.
function changeScheme(formName, isSecure){
	var form = eval("document." + formName);
	var str=form.action;
	if(isSecure){
		if(str.indexOf("http:")== 0){
			form.action=str.replace("http:","https:");
		}
	}else{
		if(str.indexOf("https:") == 0){
			form.action=str.replace("https:","http:");
		}
	}
	return true;
}
// RTN# 070813-000047 | SSL phase 2 | Ends
// QC Defect ID - 1323 : Invalid pop-up when clicking the Add New Class button on Add Students Confirmation page 
// Fixed by : Kunal | Reviewed By : Mayank
// Code Change Starts 
function simulateFormSubmission(pathStr,refStr)
{
	window.location.href = pathStr + "&ref=" + refStr.replace(/\//g,'%2F');
}
// Code Change Ends 


/*
   Moved methods for Getting and Setting Cookies to global.js
 */
// Code Change Starts
function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

function setCookie(c_name,value)
{
	//var exdate=new Date();
	//exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value);
	//+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}


function deleteCookie(cookie_name)
{
	var cookie_date = new Date ( );  // current date & time
	cookie_date.setTime ( cookie_date.getTime() - 1 );
	document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
// Code Change Ends

