/*
  $Id: $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2006 BGSys.dk

  Released under the GNU General Public License
*/

 var xmlHttp = new Array();
 xmlHttp['car_make'];
 xmlHttp['car_model'];
 xmlHttp['car_year'];
 xmlHttp['car_cat'];

 if (!window.XMLHttpRequest){
    XMLHttpRequest = function() {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
 }

 function populateBox(currentBox, nextBox){
   _loadXML('GET', 'next_box=' + nextBox + '&' + currentBox.id + '=' + currentBox.options[currentBox.options.selectedIndex].value, true, false);
  
   xmlHttp.onreadystatechange = function() {
                                  if (xmlHttp.readyState == 4) {
                                     if (xmlHttp.status == 200)
                                        fillDropBox(xmlHttp);
                                     else
                                        alert("There was a problem retrieving the XML data:\n" + xmlHttp.statusText);
                                  }
                                };
 }

 function populateBox2(currentBox, currentBoxSelected, nextBox, nextBoxSelected){
   _loadXML2('GET', 'next_box=' + nextBox + '&' + currentBox + '=' + currentBoxSelected, true, false, currentBox);
  
   xmlHttp[currentBox].onreadystatechange = function() {
                                  if (xmlHttp[currentBox].readyState == 4) {
                                     if (xmlHttp[currentBox].status == 200)
                                        fillDropBox(xmlHttp[currentBox], nextBoxSelected);
                                     else
                                        alert("There was a problem retrieving the XML data:\n" + xmlHttp[currentBox].statusText);
                                  }
                                };
 }

 function fillDropBox(xmlHttp, selected){
   var element = document.getElementById(xmlHttp.responseXML.getElementsByTagName('next_box')[0].firstChild.data);

   var newOptions = xmlHttp.responseXML.getElementsByTagName('options')[0].getElementsByTagName('option');
   for (i=0; i<newOptions.length; i++){
	 element.options[i+1] = new Option(newOptions[i].firstChild.data, newOptions[i].getAttribute('id'));
	 if (selected == newOptions[i].getAttribute('id')){
	    element.options[i+1].selected = true;
	 }
   }
   element.disabled = false;
 }

 function disableBoxes(box1, box2, box3){
   if (box1 != false){
	  box1 = document.getElementById(box1);
	  if (box1.disabled != true){
	     for (var i=1; i<box1.options.length; i++){
		   box1.options[i] = null;
	     }
	     box1.disabled = true;
	  }
   }
   if (box2 != false){
	  box2 = document.getElementById(box2);
	  if (box2.disabled != true){
	     for (var j=1; j<box2.options.length; j++){
		   box2.options[j] = null;
	     }
	     box2.disabled = true;
	  }
   }
   if (box3 != false){
	  box3 = document.getElementById(box3);
	  if (box3.disabled != true){
	     for (var k=1; k<box3.options.length; k++){
		   box3.options[k] = null;
	     }
	     box3.disabled = true;
	  }
   }
 }

 function resetBox(elementName){
   var element = document.getElementById(elementName);
   
   var newOptions = element.options;
   for (i=newOptions.length; i>0; i--){
	 element.options[i] = null;
   }
   element.disabled = true;
 }

 function _loadXML (method, getVars, async, appendSID){ 
   xmlHttp = new XMLHttpRequest(); 
   if (appendSID){ 
      getVars = getVars + '&' + osCSessionName + '=' + osCSessionID;
   }
   if (method == 'GET'){
      xmlHttp.open('GET', 'ajax_search_box.php?' + getVars, async);
      xmlHttp.send(null);
   }else{
      xmlHttp.open('POST', 'ajax_search_box.php', async);
      xmlHttp.send(getVars);
   }
 }

 function _loadXML2 (method, getVars, async, appendSID, currentBox){ 
   xmlHttp[currentBox] = new XMLHttpRequest(); 
   if (appendSID){ 
      getVars = getVars + '&' + osCSessionName + '=' + osCSessionID;
   }
   if (method == 'GET'){
      xmlHttp[currentBox].open('GET', 'ajax_search_box.php?' + getVars, async);
      xmlHttp[currentBox].send(null);
   }else{
      xmlHttp[currentBox].open('POST', 'ajax_search_box.php', async);
      xmlHttp[currentBox].send(getVars);
   }
 }

 function _unLoadXML(){
   xmlHttp = null;
 }
