/****************************************************/
/*	BEGIN : Index page settings						*/
/****************************************************/
var autoTimeout = 2700000;
// Change request to 15 minutes - 2004-08-25
// var autoTimeout = 90;
var autoTimeout = 900000;

var checkTimout = 60000;
var checkWins = 10000;
var sw = screen.availWidth-10;
var sh = screen.availHeight-34;
var appIndex = 0;
var debug=false;
var loginWin = false;
var pwdWin = false;
var apps = new Array();
var loggedOut = new Array();
var loginWindow = new Object();
var pwdChange = new Object();
var CHECKWINS, CHECKTIMOUT;

if(debug)alert('Debugging mode enabled.');

function init(){
	if(debug)alert('init');
	setCookie("VIP_ENTERED", "TRUE");
	if(isLoggedOn()){
		CHECKWINS = setTimeout("checkWindows()", checkWins);
		CHECKTIMOUT = setTimeout("autoLogout()", checkTimout);
	}else{
		deleteCookie("LOGGEDON");
		deleteCookie("AUTO");
	}
	if(CollectGarbage)CollectGarbage();
}

function autoLogout(){
	if(debug)alert('autoLogout ('+(new Date()).getTime() + ' > '+ (parseFloat(getCookie("AUTO")) + autoTimeout)+')');
	if((new Date()).getTime() > (parseFloat(getCookie("AUTO")) + autoTimeout)){
		if(debug)alert("autoLogout commencing");
		if(!loginWin){
			loginWin = true;
			loginWindow = window.showModalDialog("/screens/autologout.htm", window, "dialogHeight:360px;dialogWidth:310px;center:yes;edge:sunken;help:no;resizable:no;scroll:no;status:no;unadorned:yes;");
		}
	}
	clearTimeout(CHECKTIMOUT)
	CHECKTIMOUT = setTimeout("autoLogout()", checkTimout);
	if(CollectGarbage)CollectGarbage();
}

function showChangePassword(){
	top.document.location = "/screens/forcechangepwd.asp";
	return;
	if(!pwdWin){
		pwdWin = true;
		pwdChange = window.showModalDialog("/screens/pwdchange.htm", window, "dialogHeight:327px;dialogWidth:375px;center:yes;edge:sunken;help:no;resizable:no;scroll:no;status:no;unadorned:yes;");
	}
}

function logonCancelled(parm){// Called from auto logout screen.
	if(debug)alert('logonCancelled');
	top.menuFSetFrm.logonFrm.location="/screens/login.asp?PAGEPARM=LOGOFF&SESSIONDIR="+parm;
	alert("Re-Login cancelled. Logging client out of the Vulindlela Information Center.");
	loginWin = false;
	pwdWin = false;
	loginWindow = null;//new Object();
	pwdChange = null;//new Object();
}

function chgLogon(username, password){// Called from auto logout screen.
	if(debug)alert('chgLogon');
	top.menuFSetFrm.logonFrm.location="/screens/login.asp?PAGEPARM=LOGIN&USERNAME="+username+"&PASSWORD="+password;
	loginWindow = null;//new Object();
	pwdChange = null;//new Object();
}

function isLoggedOn(){
	if(debug)alert('isLoggedOn = ' + getCookie("LOGGEDON"));
	if(getCookie("LOGGEDON")=="TRUE") return true;
	return false;
}

function reload(){
	if(debug)alert('reload');
	top.main.menuFSetFrm.location=top.main.menuFSetFrm.location;
	top.main.contentFrm.location=top.main.contentFrm.location;
	top.main.toolFrm.location=top.main.toolFrm.location;
	if(CollectGarbage)CollectGarbage();
}

function logon(relogin){
	if(debug)alert('logon');
	pwdWin = false;
	loginWin = false;
	setCookie("LOGGEDON", "TRUE");
	setCookie("AUTO", (new Date()).getTime(), null, "/");
	clearTimeout(CHECKTIMOUT)
	CHECKTIMOUT = setTimeout("autoLogout()", checkTimout);
	if(!relogin) setCookie("LOGGINGON", "TRUE");
	if(debug)alert('end:logon');
	if(CollectGarbage)CollectGarbage();
}

function logoff(fromWhere){// Called from outside (ASP).
	if(debug)alert('logoff');
	if(debug)alert("LOGGINGON="+getCookie("LOGGINGON"));
	if(getCookie("LOGGINGON")=="TRUE"){
		if(debug)alert("Just logged on.");
		deleteCookie("LOGGINGON");
	}else if(fromWhere=='top'&&!isLoggedOn()){
		if(debug)alert("Just closing shop.");
		return false;
	}else{
		if(debug)alert("Logoff process.");
		deleteCookie("LOGGEDON");
		deleteCookie("AUTO");
		if(closeClientWindows()){
			top.location=top.location;
		}
	}
	if(CollectGarbage)CollectGarbage();
}

function openApp(appName, pageURL, width, height){
	if(debug)alert('openApp');
	x = findApp(appName);
	if( x < 0){
		if(width==0){width=screen.availWidth-20}
		if(height==0){height=screen.availHeight-20}
		_top=parseInt(((screen.availHeight-height)/2))-5;
		_left=parseInt(((screen.availWidth-width-10)/2));
		height-=20;
		try {
			appWin = window.open(pageURL, appName, "copyhistory=no,status=no,location=no,directories=no,scrollbars=no,toolbar=no,menubar=no,resizable=no,top="+_top+",left="+_left+",width="+width+",height="+height);
		}
		catch (ex) {
			appWin = window.open("about:blank", appName, "copyhistory=no,status=no,location=no,directories=no,scrollbars=no,toolbar=no,menubar=no,resizable=no,top="+_top+",left="+_left+",width="+width+",height="+height);
			appWin.document.location = pageURL;
		};
		appWin.focus();
		apps[appIndex] = new App(appName, appWin, pageURL, this);
		appWindow = apps[appIndex++];
	}else{
		apps[x].appWindow.focus();
	}
	if(CollectGarbage)CollectGarbage();
}

function App(appName, appWindow, pageURL, parentObj){
	this.appName = appName;
	this.appWindow = appWindow;
	this.pageURL = pageURL;
	this.parentObj = parentObj;
	if(CollectGarbage)CollectGarbage();
}

function closeClientWindows(){
	if(debug)alert('closeClientWindows');
	for(i=0;i<apps.length;i++)
		if(!apps[i].appWindow.closed)
			apps[i].appWindow.close();
	if(CollectGarbage)CollectGarbage();
	return true;
}

function openPopupCentered(appName, pageURL, width, height){
	if(debug)alert('openPopupCentered');
	l = (screen.width - 10 - width) / 2;
	t = (screen.availHeight - 34 - height) / 2;
	window.open(pageURL, "null", "copyhistory=no,status=no,location=no,directories=no,scrollbars=no,toolbar=no,menubar=no,resizable=no,top="+t+",left="+l+",width="+width+",height="+height);
	if(CollectGarbage)CollectGarbage();
}

function closeApp(appName){
	if(debug)alert('closeApp');
	x = findApp(appName);
	if(x != -1){
		apps[x].appWindow.close();
	}
	if(CollectGarbage)CollectGarbage();
}

function findApp(appName){
	if(debug)alert('findApp');
	if(apps.length > 0){
		for(i=0;i<apps.length;i++){
			if(!apps[i].appWindow.closed){
				if(apps[i].appName == appName)
					return i;
			}
		}
	}
	return -1;
}

function checkWindows(){
	if(debug)alert('checkWindows');
	if(!isLoggedOn())
		closeClientWindows();
	else{
		clearTimeout(CHECKWINS)
		CHECKWINS = setTimeout("checkWindows()", checkWins);
	}
	if(CollectGarbage)CollectGarbage();
}

function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function deleteCookie(name, path, domain) {
	var value = getCookie(name);
	if (value != null) document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	return value;
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}
	else begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

/****************************************************/
/*	END : Index page settings						*/
/****************************************************/

/****************************************************/
/*	BEGIN : Pageload settings						*/
/****************************************************/
setCookie("AUTO", (new Date()).getTime(), null, "/");
init();
/****************************************************/
/*	END : Pageload settings							*/
/****************************************************/
