
var g_sUserID = "";

function getPage(pageID)
{
	sTemp = getCookieSub("FSPage", pageID);
	
	if (sTemp == "")
	{
		return pageID + ".asp";
	}
	else if (pageID = "jobs" && top.mainFrame.globalFleetID == 2517) //Fowler Welch hack to override jobs.asp to be trips_assigned.asp instead.
	{
		return "trips_assigned.asp";
	}
	else
	{
		return sTemp + ".asp";
	}
}

function setUserID(sUserID)
{
	if (sUserID != g_sUserID)
	{
		// store locally for mail checks, also turns on checks
		g_sUserID = sUserID;
		checkMail();
	}
}

function checkMail()
{
	var iMailCount = 0;
	
	// do not show mail status if user not logged in!
	if (g_sUserID == "")
	{
		mailOff();
		return;
	}
	
	// do mail check
	window.status = "Checking mail...";
	try
	{
		var d = new Date();
		var xmlhttpObj = new ActiveXObject("MSXML2.XMLHTTP.3.0");
		xmlhttpObj.open("GET", "/Data/CheckMail.asp?t=" + d.getTime(), false);
		xmlhttpObj.setRequestHeader("cookie", document.cookie); //this value is ignored, but the step is necessary
		xmlhttpObj.setRequestHeader("cookie", document.cookie);
		xmlhttpObj.send();
		
		// alert(xmlhttpObj.responseText);

		var resultXML = new ActiveXObject("MSXML2.DOMDocument");
		
		resultXML.loadXML(xmlhttpObj.responseText);
		
		xmlDOMDoc = null;
		xmlhttpObj = null;
		
		iMailCount = resultXML.selectSingleNode("/root/mail").text;
		
		if (iMailCount > 0)
		{
			mailOn(iMailCount);
		}
		else
		{
			mailOff();
		}
		window.status = "";
	}
	catch(e)
	{
		window.status = "Error checking mail.";
		mailOff();
	}
	
}

function mailOn(iShowCount)
{
	mailIcon.src = "images/icons/message-icon.gif";
	mailIcon.title = sms_08.replace(/!!NUMBER!!/g, iShowCount);
}

function mailOff()
{
	mailIcon.src = "images/icons/message-icon-off.gif";
	mailIcon.title = sms_08.replace(/!!NUMBER!!/g, 0);
}


function GetCookie(name)
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
		{
      return getCookieVal(j);
		}
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return '';
}

function getCookieVal (offset)
{
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return document.cookie.substring(offset, endstr);
}


// -------------------------------------------------------------------------
// A method for extracting cookie values from cookie collections
//
// eg. Called with "FLEETSTAR" as family name and then say "UserID" as the member
// -------------------------------------------------------------------------

// Re-written by Rob 2002-03-13 - Errors in searching for correct member

function getCookieSub(family, member)
{
	str = GetCookie(family);
	//alert(str);

	if (str.length == 0) return "";
	
	i = str.indexOf("&"+member+"=") + member.length + 2;
	
	// str command will return -1 if not found
	// so test for (member.length + 2 + -1) = member.length + 1
	
	if (i == member.length + 1)
	{
		// member not found - check first item in cookie
		if (str.indexOf(member+"=") == 0)
		{
			i = member.length + 1;
		}
		else
		{
			// member just ain't there!
			return "";
		}
	}

	j = str.indexOf("&", i);
	if (j == -1)
	{
		j = str.indexOf(";", i);
	}
	if (j == -1)
	{
		j = str.length;
	}
	return unescape(str.substring(i, j));
}

window.setInterval(checkMail, 300000)  // 5 minutes