/*
	Javascript module that handles the cookie and child window management functionality 
	for the Single Logon to Federated Services


	Written by: Samuel Koh, Organic.com Pte Ltd
	Date: 10 Mar 2000


	Modified by: Ho Sheh Pien, Citibank N.A.
	Date: 31 March 2000
	Desc: - 30% logic changes
		removed var msg for debugging, added alert msg
		created LOGOFFALL and DELETECOOKIE functions


	Modified by: Ho Sheh Pien, Citibank N.A.
	Date: 20 April 2000
	Desc: - changed property settings in winOpen to prevent menu bar from appearing


	Modified by: Ho Sheh Pien, Citibank N.A.
	Date: 24 April 2000
	Desc: - changed access server url


	Modified by: Ho Sheh Pien, Citibank N.A.
	Date: 25 April 2000
	Desc: - modified LOGOFFALL function to remove "no action" items
		and added checking for _ACCESSID_ cookie


	Modified by: Ho Sheh Pien, Citibank N.A.
	Date: 17 May 2000
	Desc: - modified LogoffAll procedure to allow _FS_<service>_ cookie update, retaining
		the format as status|function|login mode.
	      - added logic to allow for only one pending login screen. ie. while child 
		window has been invoked, it should not allow for another child window in pending
		login mode


	Modified by: Ho Sheh Pien, Citibank N.A.
	Date: 19 May 2000
	Desc: - modified access server call for including timestamp as one of the param.
	      - modified access server url


	Modified by: Ho Sheh Pien, Citibank N.A.
	Date: 29 May 2000
	Desc: - rectified error in focussing on pending logon screen. "==" sign used for

		comparison instead of "=".


	Modified by: Ho Sheh Pien, Citibank N.A.
	Date: 30 May 2000
	Desc: - modified LogoffAll() to avoid prompting of authentication extension if it is
		a guest logon in ALERT service


	Modified by: Ho Sheh Pien, Citibank N.A.
	Date: 1 June 2000
	Desc: - modified DetectService() to create _HPLogonWin_ only is _ACCESSID_ cookie exist 
		rather than _ACCESSHOST_ cookie
	      - added alerts to detect for AccessID and AccessWindow
	
	Modified by: Ho Sheh Pien, Citibank N.A.
	Date: 9 June 2000
	Desc: - modified DetectService() to create _HPLogonWin_ under different scenarios


	Modified by: Ho Sheh Pien, Citibank N.A.
	Date: 3 July 2000
	Desc: - added codes to control opening and closing of icard window in function detectService()
	      - removed unnecessary commented codes


*/




// opens a new child window
function winOpen(url,winname,servicecall) {
	if (servicecall == "ICARD") {
		return window.open(url,winname,'alwaysRaised=yes,toolbar=no,scrollbars=yes,status=yes,resizable=no,menubar=no,width=640,height=500');
	}
	else {
		return window.open(url,winname,'alwaysRaised=yes,toolbar=no,scrollbars=yes,status=yes,resizable=yes,menubar=no,width=640,height=480');
	}
}




// close child Window
function getWinHandle(name) {
	var win = window.open('',name);
	return win;
}




// sets value of cookie with optional expiry date
function setCookie(name, value, expire) {
	var hours = expire;
	var exp = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
	var cooky = name + "=" + escape(value) + "; path=/" + "; domain=.citibank.com.sg" + ";expires=" + exp;
	document.cookie = cooky
}




// returns value of cookie or null if cookie does not exist
function getCookie(name) {
	var result = null;
	var myCookie = " " + document.cookie + ";";
	var searchName = " " + name + "=";
	var startOfCookie = myCookie.indexOf(searchName);
	var endOfCookie;


	if (startOfCookie != -1) {
		startOfCookie += searchName.length; // skip past name of cookie
		endOfCookie = myCookie.indexOf(";", startOfCookie);
		result = unescape(myCookie.substring(startOfCookie, endOfCookie));
	}
	return result;
}




// delete a cookie
function deleteCookie(name) {
	var exp = (new Date((new Date()).getTime()).toGMTString());
	document.cookie=name + "=; expires=" + exp +
		"; path=/" +
		"; domain=.citibank.com.sg";
}




//var genericAccess = "https://home2.citidirect.citibank.com.sg/AccessSession/";
var genericAccess = "http://home.citidirect.citibank.com.sg/AccessSession/";
//var genericAccess = "https://sit.citidirect.citibank.com.sg/AccessSession/";
//var genericAccess = "http://citidirect1.citi-sg.citibank.com.sg/AccessSession/";
//var genericAccess = "https://citidirect1.citi-sg.citibank.com.sg/AccessSession/";
//var genericAccess = "http://home.citidirect.citibank.com.sg/AccessSession/";
var accessSession = "AccessSession.asp";




function findAccessWindow() {
	// check for _ACCESSWINDOW_ cookie
	var win = getCookie("_ACCESSWINDOW_");
	return win;

}




function findAccessHost() {
	// check for _ACCESSHOST_ cookie
	var host = getCookie("_ACCESSHOST_");
	// No _ACCESSHOST_ cookie
	if (host==null) {
		return genericAccess+accessSession;
	}
	// _ACCESSHOST_ cookie found
	else {
		return "https://"+host+"/AccessSession/"+accessSession;
	}
}




function LogoffAll() {


	// check existence of any ACTIVE/INACTIVE FS cookies
	var vCitiDirect = getCookie("_FS_CITIDIRECT_");
	var vAlert = getCookie("_FS_ALERT_");
	var vICard = getCookie("_FS_ICARD_");
	var vActive = false;


	if ((vCitiDirect!=null) && (vCitiDirect.indexOf("ACTIVE") > -1)) {
		vActive = true;
	}


	if ((vAlert!=null) && (vAlert.indexOf("ACTIVE") > -1)) {
		vActive = true;
	}


	if ((vICard!=null) && (vICard.indexOf("ACTIVE") > -1)) {
		vActive = true;
	}


	// if any of the service is found to be active/inactive
	if (vActive == true) {


		// do not prompt in the case of guest login for ALERT service only
		if ((vAlert!=null) && (vAlert.indexOf("GUEST") > -1)) {



			setCookie("_ACCESSEXTEND_",0,500);


		}
		else {
			var vConfirm = (confirm("You can use other secure services without having to sign-on with your Citicard/Credit card number and ATM PIN within the next 5 minutes. Do you wish to continue?"));


			if (vConfirm == true) {
				setCookie("_ACCESSEXTEND_",5,500);
			}
			else {
				setCookie("_ACCESSEXTEND_",0,500);
			}
		}


		// for any existing FS cookies, set to LOGOFF status
		vCitiDirect = getCookie("_FS_CITIDIRECT_"); 
		if (vCitiDirect!=null) {
			arSplitValue=vCitiDirect.split('|');
			setCookie("_FS_CITIDIRECT_", "LOGOFF|"+arSplitValue[1]+"|"+arSplitValue[2],500);
		}


		vAlert = getCookie("_FS_ALERT_"); 
		if (vAlert!=null) {
			arSplitValue=vAlert.split('|');
			setCookie("_FS_ALERT_", "LOGOFF|"+arSplitValue[1]+"|"+arSplitValue[2],500);
		}
		
		vICard = getCookie("_FS_ICARD_"); 
		if (vICard!=null) {
			arSplitValue=vICard.split('|');
			setCookie("_FS_ICARD_", "LOGOFF|"+arSplitValue[1]+"|"+arSplitValue[2],500);
		}	


	}
	else {
		// check existence of 
		var tAccessID = getCookie("_ACCESSID_");
		if (getCookie("_ACCESSID_")!=null) {
			// check existence of _ACCESSWINDOW_ cookie
			var win = findAccessWindow();
			if (win!=null) {
				// if _ACCESSWINDOW_ exist
				var winhandle = getWinHandle(win);
				var host = getCookie("_ACCESSHOST_");
				setCookie("_ACCESSEXTEND_",0,500);
				winhandle.location.href="https://"+host+"/AccessSession/AccessSessionLogoff.asp";
				winhandle.focus();
				
			}
			else {
				var host = getCookie("_ACCESSHOST_");
				setCookie("_ACCESSEXTEND_",0,500);
				setCookie("_ACCESSWINDOW_", "ACCESSWIN",500);
				var childwin=winOpen(host,"ACCESSWIN","");
				childwin.focus();
			}
		}


	}


}




// launch service
function detectService(service,func)

{
browser();
//start_citidirect();

}

function detectServiceC(service,func)

{

start_citidirectC();

}
