//globals 
var first = "cdMake"; //id of first SELECT 
var second = "cdModel"; //id of second SELECT 
// 
function sendRequest(url,Sel,params,HttpMethod) { 
if(!HttpMethod) { //check if http method is defined, if not, set it to GET 
   HttpMethod="GET"; 
} 
//alert("Value received in sendRequest is "+Sel);
// 
// initialize request object 
req=null; 
if(window.XMLHttpRequest){ 
   req=new XMLHttpRequest; //mozilla/safari 
} else if(window.ActiveXObject){ 
   req=new ActiveXObject("Microsoft.XMLHTTP"); //internet explorer 
} 
// 
//define callback handler 
if(req) { 
// 
   req.onreadystatechange=function(){onReadyState(Sel)}; 
   req.open(HttpMethod,url,true); 
   req.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
   req.send(params); 
} 
} 
// 
function onReadyState(Sel) { 
// 
var ready=req.readyState; 
var data=null; 

if(ready==4){ //check ready state 
   data=req.responseText; //read response data 
   var items = data.split(','); 
   var length = items.length; 
   for(var i = 0; i < length; i++) { 
      var childEl = document.createElement('option'); //create option 
      var El = document.getElementById("cdModel"); 
      //alert("Value received in onClicked is "+El);
      El.appendChild(childEl); //then append it to the second dropdown list 
      childEl.value = items[i]; 
	  if(items[i] ==Sel)
	  {childEl.selected="selected";}
      childEl.innerHTML = items[i]; 
   } 
} 
} 
 
function clicked(Sel) { 

var el = document.getElementById("cdMake"); 
var selected = el.selectedIndex; 

var ob2 = document.getElementById("cdModel"); 
var method="GET";
//alert("Value received in onClicked is "+Sel);

while(ob2.hasChildNodes()) { //removes items from dropdown if some already exist 
   ob2.removeChild(ob2.firstChild); 
}             
if(selected!= 0) { //if they choose something other than the first select-->"Select topic first" 
	//alert("Value received in onClicked is "+Sel);
   var childEl = document.createElement('option'); 
   ob2.appendChild(childEl);
   childEl.innerHTML = 'Any'; 
   
   sendRequest('http://www.autoweb.co.uk/02NEWincludes/fetch.asp?topic='+el.options[selected].value,Sel); 
   //sendRequest('fetch.asp?topic='+el.options[selected].value,Sel); 
   ob2.disabled=0; 
   
} else { //otherwise add the Select Topic First option and disable it 
   var childEl = document.createElement('option'); 
   //alert("Value received in onClicked is "+Sel);
   ob2.appendChild(childEl);
   childEl.innerHTML = 'Select Make'; 
   ob2.disabled=1;     
} 
}
