// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
// holds the remote server address and parameters
var serverAddress = "http://www.naturescolours.com/products-by-manufacturer.php?manufacturer=";
// variables that establish how often to access the server 
var updateInterval = 5; // how many seconds to wait to get new message
var errorRetryInterval = 30; // seconds to wait after server error
// when set to true, display detailed error messages
var debugMode = true;

var currentSelectBox="";
var currentProduct="";
 
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function showProducts(manufacturerId, selectBox, defaultValue)
{
  if (xmlHttp)
  {
    try
    {
	  currentSelectBox = selectBox;
	  currentProduct = defaultValue;
      xmlHttp.open("GET", serverAddress+manufacturerId, true);
      xmlHttp.onreadystatechange = processProducts;
      xmlHttp.send(null);
    }
    catch(e)
    {
      displayError(e.toString());
    }
  }
}

// handles the response received from the server
function processProducts()
{
  idArray = new Array();
  nameArray = new Array();
  if (xmlHttp.readyState == 4) 
  {
  	if (xmlHttp.status == 200) 
	{
		response = xmlHttp.responseXML.documentElement;
	    if (response.childNodes.length)
		{
			idArray = xmlToArray(response.getElementsByTagName("id")); 
			nameArray =  xmlToArray(response.getElementsByTagName("name")); 
		}
		  
		if (currentSelectBox)
		{
			while (currentSelectBox.options.length>0)
				currentSelectBox.remove(0);
			
			for (i=0;i<idArray.length;i++)
			{
				//isSelected = (idArray[i] == currentRoomType);
				var newOpt = new Option(nameArray[i], idArray[i]);
  				var selLength = currentSelectBox.length;
  				currentSelectBox.options[selLength] = newOpt;
			}
			
			if (currentProduct != "")
				currentSelectBox.value = currentProduct;
		}
	}
  }
}

function xmlToArray(resultsXml)
{
  // initiate the resultsArray
  var resultsArray= new Array();  
  // loop through all the xml nodes retrieving the content  
  for(i=0;i<resultsXml.length;i++)
  {
    resultsArray[i]=resultsXml.item(i).firstChild.data;
  }
  
  // return the node's content as an array
  return resultsArray;
}

function displayError(message)
{
  // display error message, with more technical details if debugMode is true
  alert("Error accessing the server! "+
        (debugMode ? "\n" + message : ""));
}
