// 2007-01-29: Work on timer functionality to keep session alive. /LN

var verbose = 0;
var totalMin = 9; // Time between refresh posts, in minutes, e.g, 7.5 or 8
var courseScreenRefresh = true;

var hoverBGColor = '#0a246a';  // cbcbde
var hoverFGColor = '#ffffff';

var stdBGColor = '#ffffff';
var stdFGColor = '#000000';

var baseURL = '';

ver = navigator.appVersion;
len = ver.length;
for(x=0; x < len; x++)
	if(ver.charAt(x) == "(")
		break;
netscape = (ver.charAt(x+1).toUpperCase() != "C");

var timerID = null;
var timerCount = 0;
var halfMin = 30000;

function loadPage(url, newWindow) {
	var link = document.getElementById(url);
	//alert(link.href);
	link.click();
}

function getBaseURL() {
	var x, y, val = top.location.href;
	
	x = val.indexOf("://") + 3;
	y = val.length;
	
	for(; x <= y; x++)
		if (val.charAt(x) == "/")
			break;
	
	baseURL = val.substring(0, x);	
}

function newWin(url, name, w, h) {

	l = (screen.availWidth-10 - w) / 2;
	t = (screen.availHeight-20 - h) / 2;
	features = "width="+w+",height="+h+",left="+l+",top="+t;
	features += ",screenX="+l+",screenY="+t+",copyhistory=1";
	features += ",scrollbars=1,resizable=1,location=1";
	features += ",menubar=1,toolbar=1,status=1,directories=0";
	if (window.open(url, name, features) == null)
		showPopupPage();
}

function popup(url, name, w, h) {
	l = (screen.availWidth - w + 40) / 2;
	t = (screen.availHeight - h + 20) / 2;
	features = "width="+w+",height="+h+",left="+l+",top="+t;
	features += ",screenX="+l+",screenY="+t+",copyhistory=1";
	features += ",scrollbars=0,resizable=1,location=0";
	features += ",menubar=0,toolbar=0,status=0,directories=0";
	if (window.open(url, name, features) == null)
		showPopupPage();
}

function popupWithScrollBars(url, name, w, h) {
	l = (screen.availWidth-10 - w) / 2;
	t = (screen.availHeight-20 - h) / 2;
	features = "width="+w+",height="+h+",left="+l+",top="+t;
	features += ",screenX="+l+",screenY="+t+",copyhistory=1";
	features += ",scrollbars=1,resizable=1,location=0";
	features += ",menubar=0,toolbar=0,status=0,directories=0";
	if (window.open(url, name, features) == null)
		showPopupPage();
}

function URLencode(str) {
    return escape(str).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

function showPopupPage()
{
	var oldURL = URLencode(location.href);
	var i = location.href.indexOf("://");
	var i = location.href.indexOf("/", i + 3)
	var s = location.href.substring(0, location.href.indexOf("/", i + 1)) + "/admin/Popup.aspx";

	alert ("An error occurred trying to open a new (popup) window.\r\n\nYou will be redirected to a page explaining the use of popup windows on this site.");
	eval("top.location.href='" + s + "?rtn=" + oldURL +  "'");
}

function displayCourseDesc(cid, psid) {
	if(baseURL == '')
		getBaseURL();
	//alert(baseURL);
	if(psid)
		//popupWithScrollBars(baseURL + "/napaTraining/coursedesc.aspx?c=" + cid + "&i=" + psid, "desc", 500, 340)
	    popupWithScrollBars("../Course/coursedesc.aspx?c=" + cid + "&i=" + psid, "desc", 500, 340)
	else
		//popupWithScrollBars(baseURL + "/napaTraining/coursedesc.aspx?c=" + cid, "desc", 500, 340)
	    popupWithScrollBars("../Course/coursedesc.aspx?c=" + cid, "desc", 500, 340)
}

function displayAlaCourseDesc(cid) {
	if(baseURL == '')
		getBaseURL();
	popupWithScrollBars("../Course/alacoursedesc.aspx?c=" + cid, "desc", 500, 380)
}

function doHover(obj) {
    var o = document.getElementById(obj.id);
	o.style.color = hoverFGColor;
	o.style.background = hoverBGColor;
	o.style.cursor = 'pointer';
}

function clearHover(obj) {
    var o = document.getElementById(obj.id);
	o.style.color = stdFGColor;
	o.style.background = stdBGColor;
	o.style.cursor = 'auto';
}

// Macromedia Dreamweaver Image Swapping Functions
function MM_swapImgRestore() {
	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() {
	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) {
	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 && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() {
	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];}
	
	//alert(a[i+2]);
}

function LoadRequest(req) {
	document.Form1.action = "../store_personnel/loadmic.aspx"
	document.Form1.reqVal.value = req;
	document.Form1.submit();
	return;
}

function loadCourse(cid) {
	document.Form1.action = "../store_personnel/loadCourse.aspx"
	document.Form1.reqVal.value = cid;
	document.Form1.submit();
	return;
}

function popupCourse(cid) {
	if (verbose>0) window.status="popupCourse";
	features = "width=" + (screen.availWidth - 10) + 
		",height=" + (screen.availHeight - 10) + ",left=" + 0 +
		",top="+ 0 + ",screenX=" + 0 + ",screenY=" + 0 +
		",copyhistory=0,scrollbars=1,resizable=1,location=0" +
		",menubar=0,toolbar=0,status=0,directories=0";
	childWindow = window.open('../store_personnel/loadCourse.aspx?reqVal=' + cid, "Training", features);
	// save parent window to child window 'opener' so child window can call parent window again (if necessary)
	if (childWindow != null && childWindow.opener == null) {
		childWindow.opener = self;
		//if (verbose > -1) alert ("set opener");
		// example usage, accessing parent form from child window:
		// opener.document.Form1.Label1.Text = document.childForm.cf1.value;
	}
	if (childWindow == null) {
		showPopupPage();
	}
	/*
	else if (courseScreenRefresh && document.getElementById ("KeepAlive")) {
		timerID = setTimeout("doRefresh();", halfMin);
	}
	*/
}
 
function doRefresh() {
	if (verbose>0) window.status="doRefresh";
	if (verbose > 0) alert ("timerCount = " + timerCount);
	if (true) //timerCount > ((totalMin * 2) - 0.25))
	{
		var k = document.getElementById ("KeepAlive");
		if (k)
			k.text = "1"; // value
		if (verbose > 0) alert ("doRefresh: submit");
		document.Form1.submit();
		//window.location.reload(false);
	}
	else
	{
		timerCount ++;
		timerID = setTimeout("doRefresh();", halfMin);
	}
}

function initRefresh ()
{
	if (verbose>0) window.status="initRefresh";
	var k = document.getElementById ("KeepAlive");
	if ((k) && (k.text == "1")) //value
	{
		timerID = setTimeout("doRefresh();", halfMin);
	}
}

function stopRefresh ()
{
	if (verbose>0) window.status="stopRefresh";
	if (timerID)
	{
		clearTimeout (timerID);
	}

	var k = document.getElementById ("KeepAlive");
	if (k)
	{
		k.text = "0";
	}
}

function doProcess(str, aid) {
	if(baseURL == '')
		getBaseURL();
	switch (str) {
		case 'chgPW':
			//popup(baseURL + '/napaTraining/admin/password.aspx?i=' + aid, 'setup', 370, 200);
			popup('../admin/password.aspx?i=' + aid, 'setup', 370, 200);
			break;
		default:
			break;
	}
}

function expandContract(tid, o) {	
	var tbl = document.getElementById(tid)
	var bit = o.id.substring(o.id.length-1, o.id.length) - 1;
	
	if (tbl.className.indexOf('expand') != -1) {
		tbl.className = tbl.className.replace('expand', 'contract');
		o.src = o.src.replace('minus.bmp', 'plus.bmp');
	}
	else {
		tbl.className = tbl.className.replace('contract', 'expand');
		o.src = o.src.replace('plus.bmp', 'minus.bmp');
	}
}