//////////////////////////////////////
// general helper functions

function StringReplace(cSearch, cFind, cReplace) {
var iPos = cSearch.indexOf(cFind);
while (iPos > -1)
  { 
    cSearch = cSearch.substr(0,iPos) + cReplace + cSearch.substr(iPos+cFind.length);
    iPos = cSearch.indexOf(cFind);
  }
  
return cSearch;
}


function RandomizeURL(cURL) {
var dDate = new Date();
var iPos = cURL.indexOf("&RandomValue=");
if (iPos > 0)
  {
    var iPos2 = cURL.indexOf("&",iPos+1);
    if (iPos2 > -1)
      cURL = cURL.substr(0,iPos) + cURL.substr(iPos2);
    else
      cURL = cURL.substr(0,iPos);
  }

cURL += "&RandomValue=" + dDate.valueOf();    

return cURL;
}


function GenerateCenteredWindowLeftTop(iWindowWidth, iWindowHeight) {
var iLeft = Math.round((window.screen.width - iWindowWidth)/2);
var iTop = Math.round((window.screen.height - iWindowHeight)/2);

return "top=" + iTop + ",left=" + iLeft;
}


function GetInnerHTML(oElem)
{
  var sHTML = oElem.innerHTML;
  while (sHTML.indexOf("©") > -1)
    sHTML = sHTML.replace("©","&copy;");
  while (sHTML.indexOf("®") > -1)
    sHTML = sHTML.replace("®","&reg;");
  while (sHTML.indexOf("™") > -1)
    sHTML = sHTML.replace("™","&trade;");
  return sHTML;
}

//////////////////////////////////////
// cross-platform functions


function IsIE() {
return (window.navigator.userAgent.indexOf("MSIE") > -1 ? true : false);
}



function NavigateWindow(oWin, cURL, bUseCached) {
if (oWin == null)
  oWin = window;
if (cURL == null || cURL == "")
  {
    if (oWin.location)
      cURL = oWin.location.href;
    else 
      cURL = oWin.src;
  }

if (!bUseCached && cURL.indexOf("?") > -1)
  cURL = RandomizeURL(cURL);

if (IsIE() && oWin.navigate)
  oWin.navigate(cURL);
else
  {
    if (oWin.location)
      oWin.location = cURL;
    else
      oWin.src = cURL;
  }
}


function GetFirstChild(oElem) {
if (IsIE())
  return oElem.children[0];

var oRetval = oElem.firstChild;
while (oRetval && oRetval.nodeType != 1)
  oRetval = oRetval.nextSibling;

return oRetval;
}


function GetLastChild(oElem) {
if (IsIE())
  return oElem.children[oElem.children.length-1];

var oRetval = oElem.lastChild;
while (oRetval && oRetval.nodeType != 1)
  oRetval = oRetval.previousSibling;

return oRetval;
}


function GetChildCount(oElem) {
if (IsIE())
  return oElem.children.length;

var iRetval = 0;
var oElem = oElem.firstChild;
while (oElem)
  {
    if (oElem.nodeType == 1)
      iRetval++;
    oElem = oElem.nextSibling;
  }

return iRetval;
}


function GetNextSibling(oElem) {
do 
  oElem = oElem.nextSibling; 
while (oElem && oElem.nodeType != 1); 

return oElem; 
}


function GetPrevSibling(oElem) {
do 
  oElem = oElem.previousSibling; 
while (oElem && oElem.nodeType != 1); 

return oElem;
}


function FindChildElem(oParentElem,cElemName)
{
  var oElem;
  var oChild = GetFirstChild(oParentElem);
  while (oChild)
    {
      if (oChild.name == cElemName || oChild.id == cElemName)
        return oChild;
      
      if (GetChildCount(oChild))
        oElem = FindChildElem(oChild,cElemName);
      
      if (oElem)
        return oElem;

      oChild = GetNextSibling(oChild);
    }
  return null;
}


function GetInnerText(oElem) {
return (IsIE() ? oElem.innerText : oElem.textContent);
}


function TrimUnits(cValue) {
if (cValue == null || cValue == "")
  return 0;

var iPos = cValue.indexOf("px");
if (iPos > -1)
  cValue = cValue.substr(0,iPos);

return (cValue * 1);
}


function EventObjSetCancelBubbleIE(bCancel) {
this.m_bCancelBubble = bCancel;
window.event.cancelBubble = bCancel;
}
function EventObjSetCancelBubbleFF(bCancel) {
this.m_bCancelBubble = bCancel;
if (bCancel)
  this.m_oMozillaEvent.stopPropagation();
}
function EventObjGetCancelBubble() {
return this.m_bCancelBubble;
}

function EventObjSetReturnValueIE(bReturnValue) {
this.m_bReturnValue = bReturnValue;
window.event.returnValue = bReturnValue;
}
function EventObjSetReturnValueFF(bReturnValue) {
this.m_bReturnValue = bReturnValue;
if (!bReturnValue)
  this.m_oMozillaEvent.preventDefault();
}
function EventObjGetReturnValue() {
return this.m_bReturnValue;
}


function EventObj(aEvent) {
if (IsIE())
  {
    this.m_oMozillaEvent = null;
    aEvent = null;
  }
else
  this.m_oMozillaEvent = aEvent;

this.m_bCancelBubble = false;
this.m_bReturnValue = null;


if (aEvent)
  this.ReturnValue = EventObjSetReturnValueFF;
else
  this.ReturnValue = EventObjSetReturnValueIE;

if (aEvent)
  this.CancelBubble = EventObjSetCancelBubbleFF;
else
  this.CancelBubble = EventObjSetCancelBubbleIE;

this.GetCancelBubble = EventObjGetCancelBubble;
this.GetReturnValue = EventObjGetReturnValue;

if (aEvent || window.event)
  {
    this.srcElement = (aEvent ? aEvent.target : window.event.srcElement);
    this.fromElement = (aEvent ? aEvent.relatedTarget : window.event.fromElement);
    this.toElement = (aEvent ? aEvent.currentTarget : window.event.toElement);

    this.altKey = (aEvent ? aEvent.altKey : window.event.altKey);
    this.ctrlKey = (aEvent ? aEvent.ctrlKey : window.event.ctrlKey);
    this.shiftKey = (aEvent ? aEvent.shiftKey : window.event.shiftKey);

    this.clientX = (aEvent ? aEvent.clientX : window.event.clientX);
    this.clientY = (aEvent ? aEvent.clientY : window.event.clientY);
    this.screenX = (aEvent ? aEvent.screenX : window.event.screenX);
    this.screenY = (aEvent ? aEvent.screenY : window.event.screenY);

    if (aEvent)
      this.keyCode = (aEvent.keyCode ? aEvent.keyCode : aEvent.charCode);
    else
      this.keyCode = window.event.keyCode;

    this.type = (aEvent ? aEvent.type : window.event.type);
  }
else
  {
    this.srcElement = null;
    this.fromElement = null;
    this.toElement = null;
    this.altKey = false;
    this.ctrlKey = false;
    this.shiftKey = false;
    this.clientX = 0;
    this.clientY = 0;
    this.screenX = 0;
    this.screenY = 0;
    this.keyCode = 0;
    this.type = null;
  }

return this;
}

function IsSafari() {
return (window.navigator.userAgent.indexOf("Safari") > -1 ? true : false);
}

function IsFirefox() {
return (window.navigator.userAgent.indexOf("Firefox") > -1 ? true : false);
}

function IsBadFirefox() {

var bBadFirefox = false;

  if (/Firefox[\/\s](\d+\.\d+.\d+.\d+)/.test(window.navigator.userAgent))
  {
    var FFVersion = new String(RegExp.$1);
    if (FFVersion == "2.0.0.13")
      bBadFirefox = true;
  }
  
  return bBadFirefox;
}


function WindowClose(InstanceID)
{
 if (IsBadFirefox())
  NavigateWindow(window,"InXpo.nxp?LASCmd=AI:"+InstanceID+";O:FirefoxWinClose.htm");
 else
  window.close();
}