var wndChat ;

function goChatWindow( strURL )
{
	var strTop = (screen.Height-325)/2 ;
	var strLeft = (screen.Width-320)/2 ;
	var strARG = "height=325,width=380,top=" + strTop + ",left=" + strLeft ;

	if ( wndChat == null )	
	{
		wndChat = window.open(strURL,"chat",strARG) ;
		if ( !wndChat.opener )
			wndChat.opener = window ;
	}
	else if ( wndChat.closed ) 
	{
		wndChat = window.open(strURL,"chat",strARG) ;
		if ( !wndChat.opener )
			wndChat.opener = window ;
	}
	else
		wndChat.location = strURL ; 

	wndChat.focus(); 
}

function goChat2( strCid, strSite, strBid ) {
	var wil;

	if (getChatCookie()) {
		wil = 'here';
	} else {
		setChatCookie();
		//var strURL = "/support/chat/livehelp.aspx?cid=" + strCid + "&site=" + strSite + "&bid=" + strBid;
		//var strURL = "/support/chat/chat_start.asp?site=" + strSite + "&cid=" + strCid + "&bid=" + strBid;
		if ( strCid == null ) {
			var strURL = "/support/chat/chat_start.asp?site=" + strSite;
		} else if (strCid == '' ) {
			var strURL = "/support/chat/chat_start.asp?site=" + strSite;
		} else {
			var strURL = "/support/chat/chat_start.asp?cid=" + strCid + "&site=" + strSite + "&bid=" + strBid;
		}
		goChatWindow( strURL ) ;			
	}
}

function setChatCookie() {
	var now = new Date();
	var Later = new Date(now.getTime() + (1000 * 60 * 30));
	document.cookie = "lhopen=1;expires=" + Later.toString() + ";path=/";
}

function getChatCookie() {
	if (document.cookie.indexOf("lhopen") > -1) {
		return true;
	} else {
		return false;
	}
}

function killChatCookie() {
	var before = new Date();
	before.setTime(before.getTime()-1);
	document.cookie = "lhopen=1;expires=" + before.toString() + ";path=/";
}

// only monitor activity if user is logged in
if (document.cookie.indexOf("economy%5Fsid") > -1 || document.cookie.indexOf("economy_sid") > -1) {
	//var ecAM = new ecActivityMonitor();
	//document.onmousedown = LogActivity;
	//document.onkeydown = LogActivity;
	//ecActivityMonitor_Tick();
}

/////////////////////////////////////////
// ecActivityMonitor class
//	10/09/2006, WW 	
//			checks to see if user is active on our sites 
//			to allow us to proactively contact client
/////////////////////////////////////////
function ecActivityMonitor() {
	var sCookie = document.cookie;
	var sCookieName = "ecSession=";
	var sCookieValue = "";

	//######################################
	// On load of class object
	//######################################

	// if cookie does not exist, then get a guid and write cookie
	// otherwise, get the cookie value
	if (sCookie.indexOf(sCookieName) == -1) {
		var url = "/support/ecActivityMonitor_proc.aspx?cmd=1";
		var oXmlHttp = new ActiveXObject("Microsoft.XmlHttp")
		if (oXmlHttp) {
			oXmlHttp.open("POST", url, false);
			oXmlHttp.send("");
			sCookieValue = oXmlHttp.responseText; // gets a guid

			var sCookieExpr = sCookieName + sCookieValue;
			document.cookie=sCookieExpr;
		}
	} else {
		var iStart = sCookie.indexOf(sCookieName) + sCookieName.length;
		var iEnd = sCookie.indexOf(";", iStart);
		sCookieValue = sCookie.substring(iStart, iEnd);
	}

	//######################################
	// PROPERTIES
	//######################################

	// initialize properties variables
	this.__interval = 5000;							// interval to check for writing to SQL
	this.__session = sCookieValue;			// session cookie
	this.__dtSession = new Date();			// session date

	//######################################
	// FUNCTIONS
	//######################################

	// update the property __dtSession
	this.LogActivity = function() { this.__dtSession = new Date(); }

	// send session to process page to write to SQL
	this.LogSQL = function() {
		var url = "/support/ecActivityMonitor_proc.aspx?cmd=2&ecs=" + this.__session ;
		var oXmlHttp = new ActiveXObject("Microsoft.XmlHttp")
		if (oXmlHttp) {
			oXmlHttp.open("POST", url, true);
			oXmlHttp.send("");

			// update the last log date
			this.__dtLastLog = this.__dtSession
		}
	}

	// check to see if there is a support chat session from client services
	this.CheckSupport = function() {
		var url = "/support/ecActivityMonitor_proc.aspx?cmd=3&ecs=" + this.__session ;
		var oXmlHttp = new ActiveXObject("Microsoft.XmlHttp")
		if (oXmlHttp) {
			oXmlHttp.open("POST", url, false);
			oXmlHttp.send("");
			return oXmlHttp.responseText;
		}
	}
}


/////////////////////////////////////////////
// ecActivityMonitor_Tick()
//	- check to see if anything needs to be logged to SQL
/////////////////////////////////////////////
function ecActivityMonitor_Tick() {
	if (ecAM.__dtLastLog != ecAM.__dtSession)
		ecAM.LogSQL();

	var cid = ecAM.CheckSupport()
	if ( !getChatCookie() && cid.length > 0 )
		goChat2( cid, 2 ) 
	
	setTimeout("ecActivityMonitor_Tick()", ecAM.__interval);
}

/////////////////////////////////////////////
// LogActivity()
//	- event handler to trap for activity on webpage
/////////////////////////////////////////////
function LogActivity() { ecAM.LogActivity(); }






















/*
	this.LogActivity2 = function() {
		var sCookie = document.cookie;
		var iStart = sCookie.indexOf(sCookieName) + sCookieName.length;
		var iEnd = sCookie.indexOf(";", iStart);
		var sCookieValue = sCookie.substring(iStart, iEnd);

		// store cookie values and parse into session id and session date
		this.__session = sCookieValue;
		this.__sessionID = sCookieValue.substring(0, sCookieValue.indexOf("|"));
		this.__sessionDate = sCookieValue.substring(sCookieValue.indexOf("|")+1);
		this.__dtSession = new Date(this.__sessionDate);

		var dtCur = new Date();
		//var dtSession = new Date(this.__sessionDate);
		var sMon = dtCur.getMonth() + 1;
		var sHr = dtCur.getHours();
		var sAP = "AM";
		var sMin = dtCur.getMinutes();
		var sSec = dtCur.getSeconds();

		if (sHr == 0) {
			sHr = 12; 
			sAP = "AM";
		} else if (sHr > 12) {
			sHr = sHr-12; 
			sAP = "PM";
		} else {
			sAP = "AM";
		}

		if (sMin < 10)
			sMin = "0" + sMin;

		if (sSec < 10)
			sSec = "0" + sSec;

		var sCookieExpr = sCookieName +this.__sessionID + "|" + sMon + "/" + dtCur.getDate() + "/" + dtCur.getFullYear() + " " + sHr + ":" + sMin + ":" + sSec + " " + sAP;
		document.cookie=sCookieExpr;
	}
	*/