// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();

// 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 display_price(attstr, optionValue, pID){
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {    
        xmlHttp.open("GET", 'ajaxPrice.php'+ "?" + 'attribute=' + attstr + '&optionValue=' + optionValue + '&pID=' + pID, true);
        xmlHttp.onreadystatechange = function(){
            displayResult();
            }  
        xmlHttp.send(null);
      }
 }

 function displayResult(){
 if (xmlHttp.readyState == 4) {
            var response = xmlHttp.responseXML; 

            var attr_price = xmlToArray(response.getElementsByTagName("price")); 
            var stock = xmlToArray(response.getElementsByTagName("stock"));
            //var pID = xmlToArray(response.getElementsByTagName("pID"));
            //var optionID = xmlToArray(response.getElementsByTagName("optionID"));
                       
            /*
            if(stock < 1){
                var noStock = 1;  
                document.getElementById("pID" + optionID).innerHTML = 'cross'; 
            } else {
                document.getElementById("pID" +optionID).innerHTML = 'tick';
            }
            */
            
            if(stock == 'special'){
                document.getElementById("stockDisplay").innerHTML = 'Special Order<br>Delivery: Up to 3 Weeks';
            } else if (stock == 'components'){
                document.getElementById("stockDisplay").innerHTML = 'In Stock<br>Delivery: 3-5 Working Days'; 
            } else if (stock == 'prebuilt'){ 
                document.getElementById("stockDisplay").innerHTML = 'In Stock<br>Delivery: 1-2 Working Days';
            } else {
                document.getElementById("stockDisplay").innerHTML = 'Custom built Solution<br>Delivery: 30 days approx.';
            }
            
            document.getElementById("attr_price").innerHTML = '£'+attr_price;
            document.getElementById("attr_price2").innerHTML = '£'+attr_price;   
 }
 } 

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;
}


