﻿//////////////////////////////////////
// Prototype extension
Array.prototype.indexOf = function(obj) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == obj)
      return i;
  }
  return -1;
}


//////////////////////////////////////
// general helper functions

function GetWindowSize()
{
  var iWidth = 0, iHeight = 0;
  if (typeof(window.innerWidth) == 'number')
    {
      iWidth = window.innerWidth;
      iHeight = window.innerHeight;
    }
  else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
      iWidth = document.documentElement.clientWidth;
      iHeight = document.documentElement.clientHeight;
    }
  else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
      iWidth = document.body.clientWidth;
      iHeight = document.body.clientHeight;
    }
  return {'width': iWidth, 'height': iHeight}
}



function StringReplace(cSearch, cFind, cReplace) {
if (!cReplace)
  cReplace = "";
var iPos = cSearch.indexOf(cFind);
while (iPos > -1)
  { 
    cSearch = cSearch.substr(0,iPos) + cReplace + cSearch.substr(iPos + cFind.length);
    iPos = cSearch.indexOf(cFind,iPos + cReplace.length);
  }
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 GetURLParamValue(cParamName, oWin)
{
  var cHRef = "";
  
  if (oWin == null)
    cHRef = document.location.href;
  else
    cHRef = oWin.document.location.href;

  var iPos = cHRef.indexOf("?");
  if (iPos > -1)
    {
      cHRef = cHRef.substr(iPos+1);
      var aValues = cHRef.split("&");
      var aParamInfo;
      for (var iLup = 0; iLup < aValues.length; iLup++)
        {
          aParamInfo = aValues[iLup].split("=");
          if (aParamInfo && aParamInfo[0] == cParamName)
            return aParamInfo[1]; 
        }
    }
    
  return "";
}


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;
}



function URLEncode(cValue) 
{
  if (cValue == null)
    return "";
  else
    return encodeURIComponent(cValue);
}



function SetLinkTargets(cTarget,oParent,aAllowedTargets)
{
  if (!cTarget || typeof(cTarget) != "string")
    cTarget = "_blank";
  if (!oParent || typeof(oParent) != 'object')
    oParent = document;
  if (typeof(aAllowedTargets) != 'object' || !aAllowedTargets.length)
    aAllowedTargets = null;
    
  var aLinks = oParent.getElementsByTagName("A");
  for (var iLinkLup = 0; iLinkLup < aLinks.length; iLinkLup++)
    {
      if (aLinks[iLinkLup].href.indexOf("#") > -1)
        continue;
      if (!aAllowedTargets || (aAllowedTargets.indexOf(aLinks[iLinkLup].target) == -1))
        aLinks[iLinkLup].target = cTarget;
    }
}


//////////////////////////////////////
// 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 FindChildElems(oParentElem, cElemName, aChildElem)
{
  var bReturn = false;
  if (!aChildElem)
    {
      bReturn = true;
      aChildElem = new Array();
    }
    
  var oChild = GetFirstChild(oParentElem);
  while (oChild)
  {
    if (oChild.name == cElemName || oChild.id == cElemName)
      aChildElem.push(oChild);

    if (GetChildCount(oChild))
      FindChildElems(oChild, cElemName, aChildElem);

    oChild = GetNextSibling(oChild);
  }
  if(bReturn)
    return aChildElem;
}


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 IEVersion() {
return (parseFloat(window.navigator.appVersion.split("MSIE")[1]));
}

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,"Server.nxp?LASCmd=AI:"+InstanceID+";O:FirefoxWinClose.htm");
 else
  window.close();
}

function AddEventHandler(oElem,cEvent,fnHandler)
{
  if (oElem.addEventListener)
    oElem.addEventListener(cEvent,fnHandler,false);
  else if (oElem.attachEvent)
    oElem.attachEvent("on"+cEvent,fnHandler);
}

function RemoveEventHandler(oElem,cEvent,fnHandler)
{
  if (oElem.removeEventListener)
    oElem.removeEventListener(cEvent,fnHandler,false);
  else if (oElem.detachEvent)
    oElem.detachEvent("on"+cEvent,fnHandler);
}
