//--------------------------------------------------------------------------------------------
// AJAXRandomImage.js
//
// Copyright (C) 2007-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 response=null;

xmldoc=null;
xmldoc2=null;
if (window.XMLHttpRequest)
  {
  xmldoc=new XMLHttpRequest();
  xmldoc2=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  xmldoc=new ActiveXObject("Microsoft.XMLHTTP");
  xmldoc2=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmldoc!=null)
  {
  xmldoc.open("GET",'../sitebuilder/catalog.xml',false);
  if (window.ActiveXObject)
	{
		xmldoc.onreadystatechange=onResponse;
	}
	else if (document.implementation && document.implementation.createDocument)
	{
		
	}
  xmldoc.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP. Please UPGRADE your browser");
}

if (xmldoc2!=null)
  {
  xmldoc2.open("GET",'../sitebuilder/catupdates.xml',false);
  if (window.ActiveXObject)
	{
		xmldoc2.onreadystatechange=onResponse2;
	}
	else if (document.implementation && document.implementation.createDocument)
	{
		xmldoc2.onload = parse_data;
	}
  xmldoc2.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))
  {
        true;
  }
}

function onResponse2() 
{
  if(checkReadyState(xmldoc2))
  {
        parse_data();
  }
}

function update_catalogIE()
{
  var prods = xmldoc2.responseXML.documentElement.getElementsByTagName("Item");
  for (var i=0; i<prods.length; i++) {
     response.appendChild(prods[i]);
  }
}

function update_catalog()
{
  var prods = xmldoc2.responseXML.documentElement.getElementsByTagName("Item");
  for (var i=0; i < prods.length; i++) {
     var objImportedNode = document.importNode(prods[i], true);
     response.appendChild(objImportedNode);  
  }
}

function parse_data()
{
	if (xml == 1)
	{
                response = xmldoc.responseXML.documentElement;
		if (document.implementation && document.implementation.createDocument) response.normalize();
                if (window.ActiveXObject) {
                   update_catalogIE();
                }
                else {
                   update_catalog();}
		var items = response.getElementsByTagName("ItemField");
                var iv = 0;
		for (var c = 0; c < items.length; c++)
		{
			var f0 = items[c].getAttribute("Value");
                        c = c + 5;
			var f5 = items[c].getAttribute("Value");
                        c = c + 2;
			var f7 = items[c].getAttribute("Value");
                        c = c + 4;
			var f11 = items[c].getAttribute("Value");
                        c = c + 9;
			var f20 = items[c].getAttribute("Value");
                        s[iv] = f11 + '^' + f0 + '^' + f7 + '^' + f5 + '^' + f20;
                        iv = iv + 1;

		}
	}
}


function getRandomIMGs(category)
{
     var images = new Array();
     var x = 0;
     for (var c=0; c < s.length; c++)
     {
         var prod = s[c].split('^');
         var end = prod[0].indexOf('-');
         var code = prod[0].slice(0,end);
         if (code == category)
         {
            images[x]=prod;
            x = x + 1;
         }
     }
        var rnum = Math.random() ;
        var rand = Math.round( (x-1) * rnum);
        var es = images[rand];
        end = es[3].indexOf('>');
        var img = es[3].slice(39,end);
        document.write('<a href="', es[2], '"><img src="', img, '" title="', es[1], '" alt="', es[1], '" border=0 width=', image_width, ' height=', image_height, ' </a><br />');
        document.write('<b class="small">' + es[1] + '</b>'); document.write('<br />');
        document.write('<b class="small">$' + es[4] + '</b>'); document.write('<p />');
        rnum = Math.random();
        var rand2 = Math.round( (x-1) * rnum);
        while (rand == rand2)
        {
          rand2 = Math.round( (x-1) * Math.random() );
        }
        var es = images[rand2];
        end = es[3].indexOf('>');
        var img = es[3].slice(39,end);
        document.write('<a href="', es[2], '"><img src="', img, '" title="', es[1], '" alt="', es[1], '" border=0 width=', image_width, ' height=', image_height, ' </a><br />');
        document.write('<b class="small">' + es[1] + '</b>'); document.write('<br />');
        document.write('<b class="small">$' + es[4] + '</b>'); document.write('<p />');
        rnum = Math.random();
        var rand3 = Math.round( (x-1) * rnum);
        while (rand == rand3 | rand2 == rand3)
        {
          rand3 = Math.round( (x-1) * Math.random() );
        }
        var es = images[rand3];
        end = es[3].indexOf('>');
        var img = es[3].slice(39,end);
        document.write('<a href="', es[2], '"><img src="', img, '" title="', es[1], '" alt="', es[1], '" border=0 width=', image_width, ' height=', image_height, ' </a><br />');
        document.write('<b class="small">' + es[1] + '</b>'); document.write('<br />');
        document.write('<b class="small">$' + es[4] + '</b>');
}

function getRandomProd(category)
{
    if (category=='PET'|| category=='FVR') return true;  // need to have more products
    image_height = 140;
    image_width = 140;
     var prods = new Array();
     var x = 0;
     for (var c=0; c < s.length; c++)
     {
         var prod = s[c].split('^');
         var end = prod[0].indexOf('-');
         var code = prod[0].slice(0,end);
         if (code == category)
         {
            prods[x]=prod;
            x = x + 1;
         }
     }
     var rnum = Math.random();
     var rand = Math.round( (x-1) * rnum);
     var p = prods[rand];
     end = p[3].indexOf('>');
     var img = p[3].slice(39,end);
     document.write('<br /><br /><a href="', p[2], '"><img src="', img, '" title="', p[1], '" alt="', p[1], '" border=0 width=', image_width, ' height=', image_height, ' </a><br />');
     document.write('<b class="small">' + p[1] + '</b>'); document.write('<br />');
}