﻿Array.prototype.indexOf = function(obj)
{
  for (var i = 0; i < this.length; i++)
    {
      if (this[i] == obj)
        return i;
    }
  return -1;
}

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 GetElemOffsets(oElem)
{
  var iOffsetLeft = oElem.offsetLeft;
  var iOffsetTop = oElem.offsetTop;
  while (oElem.offsetParent)
    {
      iOffsetLeft += oElem.offsetParent.offsetLeft;
      iOffsetTop += oElem.offsetParent.offsetTop;
      oElem = oElem.offsetParent;
    }
  var oOffset = {Left:iOffsetLeft,Top:iOffsetTop};
  return oOffset;
}

function StringReplace(cSearch, cFind, cReplace)
{
  if (cSearch)
    {
      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, bCheckQuestion)
{
  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 += (((cURL.indexOf("?") == -1) && (bCheckQuestion)) ? "?" : "&") + "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 HexToRGB(cHex)
{
  var cRGB = "rgb(0,0,0)";
  if (typeof(cHex) == "string")
    {
      cHex = cHex.replace("#","");
      try {
        var r = parseInt(cHex.substring(0,2),16);
        var g = parseInt(cHex.substring(2,4),16);
        var b = parseInt(cHex.substring(4,6),16);
        cRGB = "rgb(" + r + "," + g + "," + b + ")";
      }
      catch(e){}
    }
  return cRGB;
}

function RGBToHex(cRGB)
{
  var cHex = "#000000";
  if (typeof(cRGB) == "string")
    {
      cRGB = cRGB.toLowerCase();
      cRGB = cRGB.replace("rgb","");
      cRGB = cRGB.replace("(","");
      cRGB = cRGB.replace(")","");
      var aRGB = cRGB.split(",");
      if (aRGB.length == 3)
        {
          try {
            cHex = "#" + ((aRGB[2]*1) + (aRGB[1]*256) + (aRGB[0]*65536)).toString(16);
          }
          catch(e){}
        }
    }
  return cHex;
}

function URLEncode(cValue) 
{
  if (cValue == null)
    return "";
  else
    return encodeURIComponent(cValue);
}

function JSONEncode(oObj)
{
  if (oObj.tagName)
    return ((oObj.id && oObj.id.length) ? "document.getElementById(\"" + oObj.id + "\")" : "");
  var cName = "", iCount = 0;
  var cText = "{";
  for (cName in oObj)
    {
      if (iCount)
        cText += ",";
      cText += cName + ":" + JSONValueEncode(oObj[cName]);
      iCount++;
    }
  cText += "}";
  return cText;
}


function JSONStringEncode(cStr)
{
  cStr = StringReplace(cStr,"\\","\\\\");  
  cStr = StringReplace(cStr,"\r","\\r");  
  cStr = StringReplace(cStr,"\n","\\n");  

  cStr = StringReplace(cStr,"\"","\\\"");

  return cStr;
}

function JSONValueEncode(oObj)
{
  if (typeof(oObj) == "number")
    return oObj.toString();
  else if (typeof(oObj) == "string")
    return "\"" + JSONStringEncode(oObj.toString()) + "\"";
  else if (typeof(oObj) == "boolean")
    return (oObj ? "true" : "false");
  else if (oObj && typeof(oObj) == "object")
    {
      if (oObj.length != undefined)
        return JSONArrayEncode(oObj);
      else
        return JSONEncode(oObj);
    }
  else
    return "null";
}

function JSONArrayEncode(oObj)
{
  var cName = "", iCount = 0;
  var cText = "[";
  for (cName in oObj)
    {
      if (cName != "indexOf")
        {
          if (iCount)
            cText += ",";
          cText += JSONValueEncode(oObj[cName]);
          iCount++;
        }
    }
  cText += "]";
  return cText;
}


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;
    }
}

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 (!oElem || !oElem.childNodes)
    return null;
  for (var iLup = 0; iLup < oElem.childNodes.length; iLup++)
    {
      if (oElem.childNodes[iLup].nodeType == 1)
        return oElem.childNodes[iLup];
    }
  return null;
}

function GetLastChild(oElem)
{
  if (!oElem || !oElem.childNodes)
    return null;
  for (var iLup = oElem.childNodes.length-1; iLup >= 0; iLup--)
    {
      if (oElem.childNodes[iLup].nodeType == 1)
        return oElem.childNodes[iLup];
    }
  return null;
}

function GetChildCount(oElem)
{
  var iCount = 0;
  if (oElem && oElem.childNodes)
    {
      for (var iLup = 0; iLup < oElem.childNodes.length; iLup++)
        iCount += (oElem.childNodes[iLup].nodeType == 1 ? 1 : 0);
    }
  return iCount;
}

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 FindChildElemWithAttribute(oParentElem, cAttrName, cAttrValue)
{
  var oElem;
  var oChild = GetFirstChild(oParentElem);
  while (oChild)
    {
      if (oChild.getAttribute(cAttrName) == cAttrValue)
        return oChild;

      if (GetChildCount(oChild))
        oElem = FindChildElemWithAttribute(oChild, cAttrName, cAttrValue);

      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 EventObj(oEvent)
{
  if (!oEvent && window.event)
    oEvent = window.event;

  this.m_oEvent = oEvent;
  this.m_bCancelBubble = false;
  this.m_bReturnValue = null;

  this.ReturnValue = function(bReturnValue) {
    this.m_bReturnValue = bReturnValue;
    if (this.m_oEvent) {
      this.m_oEvent.returnValue = bReturnValue;
      if (!bReturnValue && this.m_oEvent.preventDefault)
        this.m_oEvent.preventDefault();
    }
  }

  this.CancelBubble = function(bCancel) {
    this.m_bCancelBubble = bCancel;
    if (this.m_oEvent) {
      this.m_oEvent.cancelBubble = bCancel;
      if (bCancel && this.m_oEvent.stopPropagation)
        this.m_oEvent.stopPropagation();
    }
  }

  this.GetCancelBubble = function() {return this.m_bCancelBubble;}
  this.GetReturnValue = function() {return this.m_bReturnValue;}

  if (oEvent)
    {
      this.type = oEvent.type;
      this.srcElement = (oEvent.target ? oEvent.target : oEvent.srcElement);
      this.fromElement = (oEvent.relatedTarget ? oEvent.relatedTarget : oEvent.fromElement);
      this.toElement = (oEvent.currentTarget ? oEvent.currentTarget : oEvent.toElement);
      this.altKey = oEvent.altKey;
      this.ctrlKey = oEvent.ctrlKey;
      this.shiftKey = oEvent.shiftKey;
      this.keyCode = (oEvent.keyCode ? oEvent.keyCode : oEvent.charCode);
      this.clientX = oEvent.clientX;
      this.clientY = oEvent.clientY;
      this.screenX = oEvent.screenX;
      this.screenY = oEvent.screenY;
    }
  else
    {
      this.type = null;
      this.srcElement = null;
      this.fromElement = null;
      this.toElement = null;
      this.altKey = false;
      this.ctrlKey = false;
      this.shiftKey = false;
      this.keyCode = 0;
      this.clientX = 0;
      this.clientY = 0;
      this.screenX = 0;
      this.screenY = 0;
    }
}

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);
}

/////////////////////////////////////////////////////////////////
// Video

function GetExtension(cPath)
{
  return cPath.substr(cPath.lastIndexOf(".")+1).toUpperCase();
}

function SetExtension(cPath,cExt)
{
  return cPath.substr(0,Math.max(cPath.lastIndexOf("."),0))+cExt;
}

function FlashInstalled()
{
	var bRetval = false;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) 
    {
  		if (navigator.plugins["Shockwave Flash"]) 
        bRetval = true;
    }
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1)
    {
      bRetval = true;
    }
	else if (IsIE()) 
    {
      try {
        var oF = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
        if (oF)
          bRetval = true;
      } catch (e) {}
    }
    
	return bRetval;
}


function SilverlightInstalled() {
  var bRetval = false;

  try {
    try {
      var oControl = new ActiveXObject('AgControl.AgControl');
      if (oControl)
        bRetval = true;
    }
    catch (e) {
      var oPlugin = navigator.plugins["Silverlight Plug-In"];
      if (oPlugin)
        bRetval = true;
    }
  } catch (e) { }

  return bRetval;
}

/////////////////////////////////////////////////////////////////
// Checks and Flags (browser, flash, os)

function IsWindows() {
  return (window.navigator.platform.toLowerCase().indexOf("win") > -1 ? true : false);
}

function IsMac() {
  return (window.navigator.platform.toLowerCase().indexOf("mac") > -1 ? true : false);
}

function IsLinux() {
  return (window.navigator.platform.toLowerCase().indexOf("linux") > -1 ? true : false);
}

function IsIPad() {
  return (window.navigator.platform.toLowerCase().indexOf("ipad") > -1 ? true : false);
}

function IsAndroid() {
  return (window.navigator.appVersion.toLowerCase().indexOf("android") > -1 ? true : false);
}

function IsIE() {
  return (window.navigator.userAgent.indexOf("MSIE") > -1 ? true : false);
}

function IsSafari() {
  var cUserAgent = window.navigator.userAgent;
  return ((cUserAgent.indexOf("Safari") >= 0 && (cUserAgent.indexOf("3.") > 0 || cUserAgent.indexOf("4.") > 0)) ? true : false);
}

function IsFirefox() {
  return (window.navigator.userAgent.indexOf("Firefox") > -1 ? true : false);
}

function IsNetscape() {
  return (window.navigator.userAgent.indexOf("Netscape") > -1 ? true : false);
}

function IsChrome() {
  return (window.navigator.userAgent.indexOf("Chrome/") > -1 ? true : false);
}

function IsOpera() {
  return (window.navigator.userAgent.indexOf("Opera") > -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 GetFlashVersion() {
  var iMajorVersion = -1;
  var iMinorVersion = -1;
  var iRevision = -1;
  //
  if (navigator.plugins && navigator.plugins.length > 0) {
    var cType = "application/x-shockwave-flash";
    var oMimeTypes = navigator.mimeTypes;
    if (oMimeTypes && oMimeTypes[cType] && oMimeTypes[cType].enabledPlugin && oMimeTypes[cType].enabledPlugin.description) {
      var aDescParts = oMimeTypes[cType].enabledPlugin.description.split(/ +/);
      var aMajorMinor = aDescParts[2].split(/\./);
      var cRevisionStr = aDescParts[3];

      iMajorVersion = parseInt(aMajorMinor[0], 10);
      iMinorVersion = parseInt(aMajorMinor[1], 10);
      iRevision = parseInt(cRevisionStr.replace(/[a-zA-Z]/g, ""), 10);
    }
  }
  else if (window.execScript && IsIE() && IsWindows() && !IsOpera()) {
    var oActiveX;
    var cVersion = "-1 -1,-1,-1";

    try {
      // version will be set for 7.X or greater players
      oActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
      cVersion = oActiveX.GetVariable("$version");
    } catch (e) { }

    if (!cVersion) {
      try {
        // version will be set for 6.X players only
        oActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");

        // installed player is some revision of 6.0
        // GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
        // so we have to be careful. 

        // default to the first public version
        cVersion = "WIN 6,0,21,0";

        // throws if AllowScripAccess does not exist (introduced in 6.0r47)
        oActiveX.AllowScriptAccess = "always";

        // safe to call for 6.0r47 or greater
        cVersion = oActiveX.GetVariable("$version");
      } catch (e) { }
    }

    var aVersionArray = cVersion.split(",");
    iMajorVersion = parseInt(aVersionArray[0].split(" ")[1], 10);
    iMinorVersion = parseInt(aVersionArray[1], 10);
    iRevision = parseInt(aVersionArray[2], 10);
  }

  return { "major": iMajorVersion, "minor": iMinorVersion, "revision": iRevision };
}

