function init(productLineName)
{
  if (navigator.appName != "Microsoft Internet Explorer")
    window.addEventListener("click",hideProductDetail,false);

  getProductXml(productLineName);
}


function productDetail (name, line, type, scent, sizes, upcs)
{
  this.name  = name;
  this.line  = line;
  this.type  = type;
  this.scent = scent;
  this.sizes = sizes.split("|");
  this.upcs  = upcs.split("|");
}


// xmlHttpReq is a global var defined in common.js
function getProductXml (productLineName)
{
  getXmlHttpReq ();

  if (xmlHttpReq)
  {
    xmlHttpReq.onreadystatechange = parseResponse;
    xmlHttpReq.open('POST', "/GetXml.do?productLineName="+productLineName, true);
    xmlHttpReq.send("parameter");
  }
}


function parseResponse()
{
  if (xmlHttpReq.readyState == 4)
  {
    if (xmlHttpReq.status == 200)
    {
      xml = xmlHttpReq.responseXML.xml;
      //alert ("xmlHttpReq.responseXML.xml = " + xml);
      //alert ("xmlHttpReq.responseXML.documentElement = " + xmlHttpReq.responseXML.documentElement);
      // just for debug

      // IE support
      if (window.ActiveXObject)
      {
        var doc          = getParser (xml);
        //var doc        = getParser (xmlHttpReq.responseXML.xml);
        var productNodes = doc.getElementsByTagName("product");
      }
      else
      {
        // for firefox
        // var doc       = getParser (xmlHttpReq.responseXML);
        var productNodes = xmlHttpReq.responseXML.getElementsByTagName("product");
      }

      //alert ("productNodes.length = " + productNodes.length);
      var ndxProducts;

      for (ndxProducts = 0; ndxProducts < productNodes.length; ndxProducts++)
      {
        saveProductData (productNodes[ndxProducts].attributes.getNamedItem("imagePath").value,
                         productNodes[ndxProducts].attributes.getNamedItem("name")     .value,
                         productNodes[ndxProducts].attributes.getNamedItem("line")     .value,
                         productNodes[ndxProducts].attributes.getNamedItem("type")     .value,
                         productNodes[ndxProducts].attributes.getNamedItem("scent")    .value,
                         productNodes[ndxProducts].attributes.getNamedItem("size")     .value,
                         productNodes[ndxProducts].attributes.getNamedItem("upc")      .value);
      }
    }
  }
}

var products = new Array ();
function productData (imagePath, name, line, type, scent, size, upc)
{
  this.imagePath = imagePath;
  this.name      = name;
  this.line      = line;
  this.type      = type;
  this.scent     = scent;
  this.sizes     = size;
  this.upcs      = upc;
}


function saveProductData (imagePath, name, line, type, scent, size, upc)
{
  if (products[imagePath])
  {
    products[imagePath].sizes += " | " + size;
    products[imagePath].upcs += "|" + upc;
  }
  else
    products[imagePath] = new productData (imagePath, name, line, type, scent, size, upc);
}

function buyProductNow(imagePath)
{
  var imagePath = products[imagePath].imagePath;
  var name      = products[imagePath].name .replace (/ /g, "%20");
  var line      = products[imagePath].line .replace (/ /g, "%20");
  var type      = products[imagePath].type .replace (/ /g, "%20");
  var scent     = products[imagePath].scent.replace (/ /g, "%20");
  var sizes     = products[imagePath].sizes.replace (/ /g, "%20");
  var upcs      = products[imagePath].upcs.replace  (/ /g, "%20");

  var url       = "/wheretobuy/index.jsp"
                + "?line="      + line
                + "&type="      + type
                + "&scent="     + scent;

  document.location = url;
}

function hideProductDetail ()
{
  unblock("productDetailDisplay");
  return false; // i.e. follow the link
}

function launchScent ()
{
  document.location = "/ScentExpressions.do";
}
function launchClinical ()
{
  document.location = "/ClinicalStrength.do";
}
function launchOlay ()
{
  document.location = "http://www.olay.com";
}
