var xmlHttp = createXmlHttpRequestObject(); 

function createXmlHttpRequestObject()  
{  
  // will store the reference to the XMLHttpRequest object 
  var xmlHttp; 
  // if running Internet Explorer 
  if(window.ActiveXObject) 
  { 
    try 
    { 
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    catch (e)  
    { 
      xmlHttp = false; 
    } 
  } 
  // if running Mozilla or other browsers 
  else 
  { 
    try  
    { 
      xmlHttp = new XMLHttpRequest(); 
    } 
    catch (e)  
    { 
      xmlHttp = false; 
    } 
  } 
  // return the created object or display an error message 
  if (!xmlHttp) 
      alert("Error creating the XMLHttpRequest object."); 
  else  
    return xmlHttp; 
} 
 
function get_cats(brand) 
{ 
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) 
  { 
    xmlHttp.open("GET", "ajax/set_cat.php?brand=" + brand, true);
    xmlHttp.onreadystatechange = handleServerResponse; 
    xmlHttp.send(null); 
  } 
  else{
    setTimeout('get_cats('+brand+')', 1000);
  }  
}
 
// executed automatically when a message is received from the server 
function handleServerResponse()  
{
  if (xmlHttp.readyState == 4)  
  { 
    if (xmlHttp.status == 200)  
    { 
      xmlResponse = xmlHttp.responseXML; 
      xmlDocumentElement = xmlResponse.documentElement; 
      try{
      	var helloMessage = xmlDocumentElement.firstChild.data; 
      	document.getElementById("typec").innerHTML = helloMessage;
      }
	  catch(err)
  	  {
      	document.getElementById("typec").innerHTML = '<select name="type" id="type" class="frontSearch" onchange="if(this.value){set_prod(this.value)}" ><option value="">Select Category</option></select>';
      } 
    }  
    else  
    { 
      alert("There was a problem accessing the server: " + xmlHttp.statusText); 
    } 
  } 
}




function set_prod(type) 
{ 
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) 
  { 
  	var brand = document.getElementById("brand").value;
    xmlHttp.open("GET", "ajax/set_prod.php?type=" + type + "&chbrand=" + brand, true);
    xmlHttp.onreadystatechange = set_prod_html; 
    xmlHttp.send(null); 
  } 
  else{
    setTimeout('set_prod('+type+')', 1000);
  }  
}
 
 
 
// executed automatically when a message is received from the server 
function set_prod_html()  
{
  if (xmlHttp.readyState == 4)  
  { 
    if (xmlHttp.status == 200)  
    { 
      xmlResponse = xmlHttp.responseXML; 
      xmlDocumentElement = xmlResponse.documentElement; 
      var helloMessage = xmlDocumentElement.firstChild.data; 
      document.getElementById("prodc").innerHTML = helloMessage; 
    }  
    else  
    { 
      alert("There was a problem accessing the server: " + xmlHttp.statusText); 
    } 
  } 
}
