// Global Variable Declaration
// ---------------------------
var objAJAXNotice = false;
var objAJAXUpdate = false;
var requestInterval = 5000;
var timerID = null
var timerRunning = false
var responseNotice = "";
var leftPos = 300;
var topPos = 300;
//var requestURL = "http://www.etransx.com/webtrack/webnotice.asp";
//var requestURL = "http://etxmgr/etransxweb/webtrack/webnotice.asp";
var divLayerID = "customerNotice";

//JS Access Denied error thrown for IE when request url 
//contains WWW and location does not and vice versa
//fix below
//var cur_location = window.location;
var requestURL = (window.location.href.indexOf('www') > -1) ? 'http://www.etransx.com/webtrack/webnotice.asp' : 'http://etransx.com/webtrack/webnotice.asp';  

// Global Function Definitions
// ---------------------------
function showCustomerNotice(szDivID, iState) // 1 visible, 0 hidden
{
	if(document.layers)	   //NN4+
	{
		document.layers[szDivID].visibility = iState ? "show" : "hide";
	}
	else if(document.getElementById)	  //gecko(NN6) + IE 5+
	{
		var obj = document.getElementById(szDivID);
		obj.style.visibility = iState ? "visible" : "hidden";
	}
	else if(document.all)	// IE 4
	{
		document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
	}
}

function showCustomerNoticeEx(szDivID, iState) // 1 visible, 0 hidden
{
   var obj = document.layers ? document.layers[szDivID] : document.getElementById ?  document.getElementById(szDivID).style : document.all[szDivID].style;
   obj.visibility = document.layers ? (iState ? "show" : "hide") : (iState ? "visible" : "hidden");
}

function stopNotification()
{   
	if(timerRunning)      
	{
		clearInterval(timerID);   
	}

	timerRunning = false;
}

function startNotification()
{   
	// Make sure the clock is stopped   
	stopNotification();
	
	timerID = setInterval("getCustomerNotice()",requestInterval);   
	timerRunning = true;

	CreateAJAXObject();

	if (!objAJAXNotice )
		alert("Error initializing AJAX Notice Object!");
	
	if( !objAJAXUpdate )
		alert("Error initializing AJAX Update Object!");

	showCustomerNotice(divLayerID,0);
}

function CreateAJAXObject()
{
	try
	{
		objAJAXNotice = new XMLHttpRequest();
		objAJAXUpdate =  new XMLHttpRequest();
	}
	catch (trymicrosoft)
	{
		try
		{
			objAJAXNotice = new ActiveXObject("Msxml2.XMLHTTP");
			objAJAXUpdate = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (othermicrosoft)
		{
			try
			{
				objAJAXNotice = new ActiveXObject("Microsoft.XMLHTTP");
				objAJAXUpdate = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed)
			{
				objAJAXNotice = false;
				objAJAXUpdate = false;
			}  
		}
	}
}

function getCustomerNotice()
{
	objAJAXNotice.open("GET", requestURL, true);
	objAJAXNotice.onreadystatechange = updateCustomerNotice;
	objAJAXNotice.send(null);
}

function updateCustomerNotice()
{
	if (objAJAXNotice.readyState == 4)
	{
		if (objAJAXNotice.status == 200)
		{
			responseNotice = objAJAXNotice.responseText
			if( responseNotice.length > 0 ) 
			{
				showCustomerNotice(divLayerID,1);
				document.getElementById("customerCallText").value = responseNotice;
				objAJAXUpdate.open("GET", requestURL + "?RID=1", true);
				objAJAXUpdate.send(null);
			}
		}
	//	else
	//	{
	//		alert("Error in AJAX Request: " + objAJAXNotice.status);
	//	}
	}
}

function CalculateHScrollPosition()
{
	if (document.all)
	{
		zxcCur='hand';
		zxcWH=document.documentElement.clientHeight;
		zxcWW=document.documentElement.clientWidth;
		zxcWS=document.documentElement.scrollTop;
		if (zxcWH==0)
		{
			zxcWS=document.body.scrollTop;
			zxcWH=document.body.clientHeight;
			zxcWW=document.body.clientWidth;
		}
	}
	else if (document.getElementById)
	{
		zxcCur='pointer';
		zxcWH=window.innerHeight-15;
		zxcWW=window.innerWidth-15;
		zxcWS=window.pageYOffset;
	}
	zxcWC=Math.round(zxcWW/2);
	return [zxcWW,zxcWH,zxcWS];
}

window.onscroll=function()
{
	var cnWinObj =document.getElementById(divLayerID);
	if (!document.all){ cnWinObj.style.position='fixed'; window.onscroll=null; return; }
	if (!cnWinObj.pos){ cnWinObj.pos=cnWinObj.offsetTop; }
	cnWinObj.style.top=(CalculateHScrollPosition()[2]+cnWinObj.pos)+'px';
}

function closeBrowser()
{
	alert('I am closing');
	objAJAXUpdate.open("GET", requestURL + "?RID=0", true);
	objAJAXUpdate.send(null);
}
