//--------------------------------------------------------------------------------------------
// AJAXgetProduct.js
//
// Copyright (C) 2006-2008 Glen A. Reece, Ph.D. for The Silken Thread, LLC
//
// If you use or modify this code please contact us at email@thesilkenthread.com
// This header MUST remain in tact
//
//--------------------------------------------------------------------------------------------

var xml = 1;
var image_height = 90;
var image_width = 90;
var xmldoc;
var s = new Array();
var result = new Array();

xmldoc=null;
async=true;
if (window.XMLHttpRequest)
  {
  xmldoc=new XMLHttpRequest();
  async=false;
  }
else if (window.ActiveXObject)
  {
  xmldoc=new ActiveXObject("Microsoft.XMLHTTP");
  async=true;
  }
if (xmldoc!=null)
  {
  xmldoc.open("GET",'../sitebuilder/catalog.xml',async);
  if (window.ActiveXObject)
	{
		xmldoc.onreadystatechange=onResponse;
	}
	else if (document.implementation && document.implementation.createDocument)
	{
		xmldoc.onload = parse_catalog;
	}
  xmldoc.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP. Please UPGRADE your browser");
}


function checkReadyState(obj)
{
  if(obj.readyState == 4)
  {
    if(obj.status == 200)
    {
      return true;
    }
    else
    {
      alert("Problem retrieving Catalog data");
    }
  }
}

function onResponse() 
{
  if(checkReadyState(xmldoc))
  {
        parse_catalog();
  }
}

function parse_catalog()
{

}

function getStatProdInfo(product)
{
   var response = xmldoc.responseXML.documentElement;
   if (document.implementation && document.implementation.createDocument) response.normalize();
   var items = response.getElementsByTagName("Item");
   var prodndx = 1;
   for (var i = 0; i < items.length; i++)
   {
     if (items[i].getAttribute("ID") == product) prodndx = i;
   }       
   if (prodndx == 1 || product.charAt(0) != 's') {
     alert('ERROR: No such stationery product [' + product + '].');
     window.history.back();
     return false;
   }
   // result: name, image, inset, price, sale-price
   itemdata = response.getElementsByTagName("ItemField");
   result[0] = itemdata[prodndx*21].getAttribute("Value");
   result[1] = itemdata[(prodndx*21)+17].getAttribute("Value");
   result[2] = itemdata[(prodndx*21)+10].getAttribute("Value");
   result[3] = itemdata[(prodndx*21)+20].getAttribute("Value");
   var sp = itemdata[(prodndx*21)+14].getAttribute("Value");
   result[4] = (sp == undefined || sp == "") ? 0 : sp;
}

function getProdInfo(product)
{
   var response = xmldoc.responseXML.documentElement;
   if (document.implementation && document.implementation.createDocument) response.normalize();
   var items = response.getElementsByTagName("Item");
   var prodndx = 1;
   for (var i = 0; i < items.length; i++)
   {
     if (items[i].getAttribute("ID") == product) prodndx = i;
   }       
  // result: name, image, inset, price, sale-price
   itemdata = response.getElementsByTagName("ItemField");
   result[0] = itemdata[prodndx*21].getAttribute("Value");
   result[1] = itemdata[(prodndx*21)+17].getAttribute("Value");
   result[2] = itemdata[(prodndx*21)+10].getAttribute("Value");
   result[3] = itemdata[(prodndx*21)+20].getAttribute("Value");
   var sp = itemdata[(prodndx*21)+14].getAttribute("Value");
   result[4] = (sp == undefined || sp == "") ? 0 : sp;
   result[3] = parseFloat(result[3]).toFixed(2);
   result[4] = parseFloat(result[4]).toFixed(2);
}

function addSelectOptions(product,zoption,fname,sname)
{

   var response = xmldoc.responseXML.documentElement;
   if (document.implementation && document.implementation.createDocument) response.normalize();
   var items = response.getElementsByTagName("Item");
   var prodndx = 1;
   for (var i = 0; i < items.length; i++)
   {
     if (items[i].getAttribute("ID") == product) prodndx = i;
   }       
   var values = new Array();
   options = items[prodndx].getElementsByTagName("Option");
   for (var po = 0; po < options.length; po++)
   {
     if (options[po].getAttribute("Key") == zoption) values = options[po].getElementsByTagName("OptionValue");
   }
   var pd = document.getElementById(sname);
   var s;
   var maxI = values.length-1;
   //if (zoption == "Shipping") maxI = values.length-2;
   for (var I = 0; I <= maxI; I++ ) {
    s = values[I].getAttribute("Value");
    pd.options[I] = new Option(s,s);
   }
}