/* Allow for automatic trimming of strings. */
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, "");
}

/* ArrayList class */
function ArrayList() {
  this.Count = 0;
  this.Arr = new Array(10);
  this.ArrIndex = -1;
}

ArrayList.prototype.Add = function(objAdd) {
  this.ArrIndex++;
  this.Count++;
  if (this.ArrIndex >= this.Arr.length)
    this.Arr = Array.concat(this.Arr, new Array(10));
  this.Arr[this.ArrIndex] = objAdd;
  return this.ArrIndex;
}

ArrayList.prototype.RemoveAt = function(intIndex) {
  if (intIndex < 0 || intIndex > this.ArrIndex)
    throw "Index out of bounds.";
  else {
    for (var intInd = intIndex; intInd < this.ArrIndex; intInd++)
      this.Arr[intInd] = this.Arr[intInd + 1];
    this.ArrIndex--;
    this.Count--;
  }
}

ArrayList.prototype.ToArray = function() {
  this.Arr.length = this.Count;
  return this.Arr;
}

ArrayList.prototype.Item = function(intIndex) {
  if (intIndex < 0 || intIndex > this.ArrIndex)
    throw "Index out of bounds.";
  else
    return this.Arr[intIndex];
}

/* This class is setup so that you can use sta as a namespace. */
var sta = new __sta();

function __sta() {
  this.namespace = "sta";
}

__sta.prototype.createHttpObject = function() {
  var objRequest = false;
  try {
    objRequest = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (objErr1) {
    try {
      objRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (objErr2) {
      try {
        objRequest = new XMLHttpRequest();
      }
      catch (objErr3) {
        objRequest = false;
      }
    }
  }
  return objRequest;
}

__sta.prototype.createObject = function(objXmlNode) {
  var objRetVal = new Object();
  var objQuoteRegEx = new RegExp("\"", "g");
  var objLineBreakRegEx = new RegExp("(\\r|\\n|\\r\\n)", "g");
  for (var intIndex = 0; intIndex < objXmlNode.childNodes.length; intIndex++) {
    if (objXmlNode.childNodes[intIndex].nodeName != "#text") {
      if (objXmlNode.childNodes[intIndex].childNodes.length > 0) {
        if (objXmlNode.childNodes[intIndex].childNodes[0].nodeValue == null) {
          eval("objRetVal." +  objXmlNode.childNodes[intIndex].nodeName + " = \"\"");
        }
        else {
          eval("objRetVal." +  objXmlNode.childNodes[intIndex].nodeName + " = \"" + objXmlNode.childNodes[intIndex].childNodes[0].nodeValue.replace(objQuoteRegEx, "\\\"").replace(objLineBreakRegEx, "") + "\"");
        }
      }
      else {
        eval("objRetVal." +  objXmlNode.childNodes[intIndex].nodeName + " = null");
      }
    }
  }
  return objRetVal;
}

__sta.prototype.fillCollection = function(objXmlNode) {
  var objRetVal;
  var intIndex, intArrayIndex, intLength;
  intLength = 0;
  for (intIndex = 0; intIndex < objXmlNode.childNodes.length; intIndex++)
    if (objXmlNode.childNodes[intIndex].nodeName != "#text")
      intLength++;
  objRetVal = new Array(intLength);
  intArrayIndex = 0;
  for (intIndex = 0; intIndex < objXmlNode.childNodes.length; intIndex++)
    if (objXmlNode.childNodes[intIndex].nodeName != "#text")
      objRetVal[intArrayIndex++] = sta.createObject(objXmlNode.childNodes[intIndex]);
  return objRetVal;
}

__sta.prototype.loadXML = function(strMethod, strUrl, strVars, objFunction, objParams) { 
  var objHttpRequest = sta.createHttpObject();
  var strQueryString, strForm;
  objHttpRequest.onreadystatechange = function() { 
    if (objHttpRequest.readyState == 4) {
      if (objHttpRequest.status == 200)
        objFunction(objHttpRequest.responseXML, objParams);
      else
        objFunction(null, objParams);
    } 
  };
  if (strMethod != "GET" && strMethod != "POST")
    strMethod = "GET";
  if (strMethod == "GET") {
    strForm = null;
    if (strVars == null || strVars == "")
      strQueryString = "";
    else
      strQueryString = "?" + strVars;
  }
  else {
    strQueryString = "";
    if (strVars == null || strVars == "")
      strForm = null;
    else
      strForm = strVars;
  }
  objHttpRequest.open(strMethod, strUrl + strQueryString, true);
  if (strMethod == "POST")
    objHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  objHttpRequest.send(strForm);
}

__sta.prototype.setVisibility = function(objControl, bolVisible) {
  if (bolVisible)
    objControl.style.visibility = "visible";
  else
    objControl.style.visibility = "hidden";
}

__sta.prototype.setPanelVisibility = function (objPanel, bolVisible) {
  if (bolVisible) {
    objPanel.style.visibility = "visible";
    objPanel.style.display = "inline";
  }
  else {
    objPanel.style.visibiltiy = "hidden";
    objPanel.style.display = "none";
  }
}

/* Ecometry Product Methods */

function getProductName(objProduct) {
  var strRetVal = "";
  if (objProduct.BrandName != null && objProduct.BrandName != "")
    strRetVal += objProduct.BrandName + " ";
  if (objProduct.WebName != null && objProduct.WebName != "")
    strRetVal += objProduct.WebName;
  else if (objProduct.StyleName != null && objProduct.StyleName != "")
    strRetVal += objProduct.StyleName;
  else
    strRetVal += objProduct.ItemName;
  return strRetVal;
}

function getCartProductTotal(objCart) {
  var dblTotal = 0.0;
  for (var intIndex = 0; intIndex < objCart.length; intIndex++) {
    dblTotal += getProductUserPrice(objCart[intIndex]) * objCart[intIndex].Quantity;
  }
  return dblTotal;
}

function getProductUserPrice(objProduct) {
  if (new Number(objProduct.BogoPrice) > 0)
    return new Number(objProduct.BogoPrice);
  else if (new Number(objProduct.MultipricingPrice) > 0)
    return new Number(objProduct.MultipricingPrice);
  else if (new Number(objProduct.SourceCodePrice) > 0)
    return new Number(objProduct.SourceCodePrice);
  else
    return new Number(objProduct.Price);
}


/* Used for DHTML Popups */

function Dimension() {
  this.Width = 0.0;
  this.Height = 0.0;
}

function Position() {
  this.x = 0.0;
  this.y = 0.0;
}

var objCurPosition = new Position();
var isInternetExplorer = (navigator.userAgent != null && navigator.userAgent.indexOf("MSIE") != -1);
var strIFrameHtml = "<iframe id=\"DynamicIFrame\" style=\"z-index: 6000; position: absolute;\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" src=\"/js/Blank.htm\"></iframe>";
var bolWindowShowing = false;
var strCurWindowName = "";

function showWindowPanel(strPanelName, strPanelHtml, strIEPanelHeader) {
  var objWndPanel, objDocument;
  objWndPanel = document.getElementById("BackgroundPanel");
  if (isInternetExplorer) {
    objWndPanel.innerHTML = strIFrameHtml;
    objDocument = getWindowDocument();
    objDocument.open();
    objDocument.write("<html><body style=\"text-align: left; padding: 0px; background-image: none; background-color: white;\">" + strIEPanelHeader + strPanelHtml + "</body></html>");
    objDocument.close();
  }
  else
    objWndPanel.innerHTML = strPanelHtml;
  strCurWindowName = strPanelName;
  bolWindowShowing = true;
  positionWindowPanel();
}

function getWindowDocument() {
  var objRetVal, objContainer;
  if (isInternetExplorer) {
    objContainer = document.getElementById("DynamicIFrame");
    objRetVal = objContainer.contentDocument;
    if (objRetVal == null && objContainer.contentWindow != null)
      objRetVal = objContainer.contentWindow.document;
  }
  else
    objRetVal = document;
  return objRetVal;
}

function positionWindowPanel() {
  var objWndSize, objPnlSize;
  var objWndPanel, objPnlPanel;
  var objContainer, objDocument;
  var intWidth, intHeight;
  objWndPanel = document.getElementById("BackgroundPanel");
  if (isInternetExplorer) {
    objContainer = document.getElementById("DynamicIFrame");
    objDocument = objContainer.contentDocument;
    if (objDocument == null && objContainer.contentWindow != null)
      objDocument = objContainer.contentWindow.document;
    objPnlPanel = objDocument.getElementById(strCurWindowName);
  }
  else {
    objPnlPanel = document.getElementById(strCurWindowName);
    objContainer = objPnlPanel;
  }
  objWndSize = getWindowSize();
  objPnlSize = getPanelSize(objPnlPanel);
  objContainer.style.width = objPnlSize.Width;
  objContainer.style.height = objPnlSize.Height;
  if (objWndSize.Width < objPnlSize.Width || objWndSize.Height < objPnlSize.Height) {
    intWidth = 0;
    if (objWndSize.Width < objPnlSize.Width)
      intWidth = (objPnlSize.Width - objWndSize.Width) + 20;
    intHeight = 0;
    if (objWndSize.Height < objPnlSize.Height)
      intHeight = (objPnlSize.Height - objWndSize.Height) + 20;
    try {
      window.resizeBy(intWidth, intHeight);
    } catch (ex) {}
    objWndSize = getWindowSize();
    objPnlSize = getPanelSize(objPnlPanel);
  }
  objWndPanel.style.top = objCurPosition.y;
  objWndPanel.style.left = objCurPosition.x;
  objWndPanel.style.width = objWndSize.Width;
  objWndPanel.style.height = objWndSize.Height;
  objWndPanel.style.visibility = "visible";
  intWidth = (objWndSize.Width - objPnlSize.Width) / 2;
  if (intWidth < 0)
    intWidth = 0;
  objContainer.style.left = intWidth;
  intHeight = (objWndSize.Height - objPnlSize.Height) / 2;
  if (intHeight < 0)
    intHeight = 0;
  objContainer.style.top = intHeight;
  objContainer.style.visibility = "visible";
}

function windowResizeHandler() {
  if (bolWindowShowing)
    positionWindowPanel();
}

function hideWindowPanel() {
  var objPanel = document.getElementById("BackgroundPanel");
  objPanel.innerHTML = "";
  objPanel.style.visibility = "hidden";
  bolWindowShowing = false;
}

function getWindowSize() {
  var objRetVal = new Dimension();
  if (typeof(window.innerWidth) == "number") {
    objRetVal.Width = window.innerWidth;
    objRetVal.Height = window.innerHeight;
  }
  else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    objRetVal.Width = document.documentElement.clientWidth;
    objRetVal.Height = document.documentElement.clientHeight;
  }
  else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    objRetVal.Width = document.body.clientWidth;
    objRetVal.Height = document.body.clientHeight;
  }
  return objRetVal;
}

function getPanelSize(objPanel) {
  var objRetVal = new Dimension();
  var strTemp;
  strTemp = objPanel.style.width;
  if (strTemp.substr(strTemp.length - 2, 2) == "px")
    strTemp = strTemp.substr(0, strTemp.length - 2);
  objRetVal.Width = strTemp;
  strTemp = objPanel.style.height;
  if (strTemp.substr(strTemp.length - 2, 2) == "px")
    strTemp = strTemp.substr(0, strTemp.length - 2);
  objRetVal.Height = strTemp;
  return objRetVal;
}

function getDocumentPosition() {
  var objRetVal = new Position();
  if (typeof(window.pageYOffset) == "number") {
    objRetVal.y = window.pageYOffset;
    objRetVal.x = window.pageXOffset;
  }
  else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
    objRetVal.y = document.body.scrollTop;
    objRetVal.x = document.body.scrollLeft;
  }
  else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
    objRetVal.y = document.documentElement.scrollTop;
    objRetVal.x = document.documentElement.scrollLeft;
  }
  if (isNaN(objRetVal.y))
    objRetVal.y = 0;
  if (isNaN(objRetVal.x))
    objRetVal.x = 0;
  return objRetVal;
}

function captureScroll() {
  var objPos = getDocumentPosition();
  if (objPos.x != objCurPosition.x || objPos.y != objCurPosition.y) {
    objCurPosition = objPos;
    if (bolWindowShowing)
      positionWindowPanel();
    }
}

window.onresize = windowResizeHandler;
window.setInterval("captureScroll()", 333);