/*
  $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 getSearchString(currentId)
 {
 	var str = '';
	var found = false;
 	jQuery('.ajax_search_field').each(function(index){
 		
 		var obj = jQuery(this);
 		
 		if (obj.val() != "" && found == false)	
	 		str += this.id + "=" + obj.val() + '&';
		
		if (found == true) {
			obj.attr("disabled", "true");
		}
 		
 		if (currentId == this.id) found = true;
 	});
 	
 	return str;
 }
 
 function populateBox1(obj, next){
 	
 	var queryString = getSearchString(obj.id);
 	
 	if (next == "")
 	{
 		document.forms['ajax_search'].keyword.value = '';
 		document.forms['ajax_search'].submit();
 	}
 	else
 	{
   		_loadXML('GET', queryString+'next='+next, 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 populateBox(currentBox, nextBox){
   
   var value = currentBox.options[currentBox.options.selectedIndex].value;
   _loadXML('GET', 'next_box=' + nextBox + '&' + currentBox.id + '=' + 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 createYearSelectOptions(next)
 {
	var parent = document.getElementById("middle");
	var child = document.getElementById("Year");
	var child2 = document.getElementsByClassName("multiSelectOptions");
	
	//parent.removeChild(child);
	//parent.removeChild(chil2);
	
	var html = '<select name="Year" id="Year" class="ajax_search_field" style="width: 120px;margin-right:5px" onchange="populateBox1(this, \''+next+'\')">' + 
				'<option value="" SELECTED>Select Year</option></select>';
				
	document.getElementById("Year").innerHTML = html;
 }
 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');
   var html = "";
   
   element.options.length = 1;
   for (i=0; i<newOptions.length; i++){
   	 if (element.id == 'Year')
   	 {
   	 	//addOption(newOptions[i].getAttribute('id'), newOptions[i].firstChild.data);
   	 	//continue;
   	 }
	 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;
 }

