// -------------------------------------------------------------------------
//
// This file contains some globally used JavaScript functions 
//
// -------------------------------------------------------------------------
//alert("global.js load");
// Run-time globs for this user as required by the Maplet etc
var globalUserID = getCookieSub("FLEETSTAR", "UserID");
var globalFleetID = getCookieSub("FLEETSTAR", "FleetID");
var globalUserGroup = getCookieSub("FLEETSTAR", "UserGroup");
var globalLang = getCookieSub("FLEETSTAR", "lang");

var globalSessionID = globalUserID;
//alert("global.js userid:" + globalUserID);
if (globalUserID == '')
{
	try	{
		mainPageTest();
	}
	catch (e) {
		alert("Invalid User Alert!");
	}
}

function globalOpenWin(urlIn)
{
	globalOpenNamedWin("Fleetstar_Reports", urlIn);
}

function globalOpenNamedWin(winName, urlIn)
{
	window.open(urlIn,winName,"menubar=1,toolbar=0,scrollbars=1,resizable=1,width=990,height=660");
}

function WaitCursorOff()
{
	mainLeftSection.style.cursor = "";
}

function WaitCursorOn()
{
	mainLeftSection.style.cursor = "wait";
}
// END Wait Cursor Code



// -------------------------------------------------------------------------
// CPR's Cookie Functions
// -------------------------------------------------------------------------
// Example of how to setup an expiry date:
//	var expdate = new Date ();
//	expdate.setTime(expdate.getTime() + (1000 * 24 * 60 * 60 * 1000));	// 1000 days from now from now!

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));
}

function SetCookieSub(family, member, value, expdate, path, domain, secure)
{
	cookieData = "&" + GetCookie(family) + "&";
	// alert(cookieData);
	cookieRegExp = new RegExp("&" + member + "=[^&]*&")
	if(cookieData.search(cookieRegExp) == -1)
	{
		if (cookieData.length > 2) cookieData = cookieData + "&";
		cookieData = cookieData.substr(1, cookieData.length - 2) + member + "=" + value;
	}
	else
	{
		cookieData = cookieData.replace(cookieRegExp, "&" + member + "=" + value + "&");
		cookieData = cookieData.substr(1, cookieData.length - 2);
	}
	// alert("setting to: " + cookieData);
	SetCookie(family, cookieData, expdate, path, domain, secure);
}

function SetCookie (name,value,expires,path,domain,secure)
{
  document.cookie = name + "=" + value +
    ((expires)	? "; expires=" + expires.toGMTString()	: "") +
    ((path)		? "; path=" + path						: "") +
    ((domain)	? "; domain=" + domain					: "") +
    ((secure)	? "; secure"							: "");
}

function DeleteCookie (name,path,domain)
{
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path)	? "; path=" + path		: "") +
      ((domain) ? "; domain=" + domain	: "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// END Cookie Functions

function PostToServer(strXML, strLocation)
{
	var xmlhttpObj = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	
	var xmlDOMDoc = new ActiveXObject("MSXML2.DOMDocument");
	
	xmlDOMDoc.loadXML(strXML);
	
	// display debug info
	//alert("Sending:\n" + xmlDOMDoc.xml);

	xmlhttpObj.open("POST", strLocation, false);

	// MS problem, do it twice
	xmlhttpObj.setRequestHeader("Cookie", document.cookie);
	xmlhttpObj.setRequestHeader("Cookie", document.cookie);

	// Send the request.  Get a response.  Whoa, rocket-science.
	xmlhttpObj.send(xmlDOMDoc);
	
	// display debug info
	//alert("Received:\n" + xmlhttpObj.responseText);

	var sResponseText = xmlhttpObj.responseText
	
	xmlhttpObj.Close;
	
	xmlDOMDoc = null;
	xmlhttpObj = null;

	return sResponseText;
}

function pause(ms)
{
	start = new Date();
	var now = null;

	do
	{
		var now = new Date();
	}
	while(now - start < ms);
}

function xmlEscape(sIn)
{
	var sTemp = sIn.replace("&", "&amp;");
	sTemp = sTemp.replace("\"", "&quot;");
	sTemp = sTemp.replace("'", "&apos;");
	sTemp = sTemp.replace("<", "&lt;");
	sTemp = sTemp.replace(">", "&gt;");
	return sTemp;
}

function PostToUrl(url,params) {
	var ifr = document.createElement("iframe");
	ifr.setAttribute("id","PSIFrame");
	ifr.style.border="0px";
	ifr.style.width="0px";
	ifr.style.height="0px";
	var IFrameObj = document.body.appendChild(ifr);
	var IFrameDoc = IFrameObj.contentWindow.document;
	var frm = IFrameDoc.createElement("form");
	frm.setAttribute("method","POST");
	frm.setAttribute("action",url);
	for (var key in params) {
		var hiddenField = IFrameDoc.createElement("input");
		hiddenField.setAttribute("type","hidden");
		hiddenField.setAttribute("name",key);
		hiddenField.setAttribute("value",params[key]);
		frm.appendChild(hiddenField);
	}
	IFrameDoc.appendChild(frm);
	frm.submit();
	document.body.removeChild(IFrameObj);
}

