var CARS_ALL_XML = null; 

$(document).ready(function(){
	
	$('#select_make').change(function(){
		while ($(this)[0].options[0].value == '%')
			$(this)[0].removeChild($(this)[0].childNodes[0]);
		load_model();
		
		$('#select_engine').attr('disabled', true);
		//$('#select_disel').attr('disabled', true);
		$('#select_year').attr('disabled', true);
		
		});
	
	$('#select_model').change(function(){
		while ($(this)[0].options[0].value == '%')
			$(this)[0].removeChild($(this)[0].childNodes[0])
		load_engine();
		//$('#select_disel').attr('disabled', true);
		$('#select_year').attr('disabled', true);
	});
	
	$('#select_engine').change(function(){
		while ($(this)[0].options[0].value == '%')
			$(this)[0].removeChild($(this)[0].childNodes[0])
		//$('#select_disel').attr('disabled', false);
		//$('#select_disel').attr('selectedIndex', 0);
		//$('#select_year').attr('disabled', true);
		//$('#select_year').attr('selectedIndex', 0);
		$('#select_year').attr('disabled', false);
		if ($('#select_year').attr('selectedIndex') != 0)
		{
			$('#catalogueFormID').each(function()
			{
				$(this).submit();
			});
		}
	});
	
	/*$('#select_disel').change(function(){
		//while ($(this)[0].options[0].value == '%')
		//	$(this)[0].removeChild($(this)[0].childNodes[0])
		$('#select_year').attr('disabled', false);
		$('#select_year').attr('selectedIndex', 0);
	});*/
	
	$('#select_year').change(function(){
		//while ($(this)[0].options[0].value == '%')
		//	$(this)[0].removeChild($(this)[0].childNodes[0])
		$('#catalogueFormID').each(function()
		{
			$(this).submit();
		});
	});
	
	$('#catalogueFormID').ajaxForm({
		target: '#resultMainID',
	  	beforeSubmit: function() {
	  			$('#resultMainID').html('Please wait... <img src="'+SITE_ROOT_HTTP+'/themes/'+THEME+'/images/please_wait.gif" style="width:25px; vertical-align:middle; padding-left:10px;">');
	  			}
	}); 
	

 	$.ajax({
	  type: "GET",
	  url: SITE_ROOT_HTTP+'/ajax/carsAll.xml.php',
	  dataType: "xml",
	  beforeSend: function() {
	  			$('#loadMainID').html('Please wait... <img src="'+SITE_ROOT_HTTP+'/themes/'+THEME+'/images/please_wait.gif" style="width:25px; vertical-align:middle; padding-left:10px;">');
	  			},
	 complete: function (XMLHttpRequest, textStatus) {
	 			  CARS_ALL_XML = XMLHttpRequest.responseXML;
	 			  $('#loadMainID').html('');
	 			  if (document.getElementById('select_make').value != '%')
	 			  {
				  	while (document.getElementById('select_make').options[0].value == '%')
						document.getElementById('select_make').removeChild(document.getElementById('select_make').childNodes[0]);
					load_model();
	 			  }
				},
	 error: function (XMLHttpRequest, textStatus, errorThrown) {
			  alert('XML LOAD ERROR: '+textStatus);
			}
	});

	
});

function load_model()
{
	if (CARS_ALL_XML == null)
	{
		setTimeout("load_model()",250);
		return; 
	}
	var make = $('#select_make')[0].value; 
	$(CARS_ALL_XML).find('make').each(function() {
		if($(this).attr('v') == make)
		{
			$('#select_model').attr('disabled', false);
			$('#select_model').attr('length', 0);
			var option = new Option();
			option.text = TRANSLATION_13+':';
			option.value = '%';
			$('#select_model')[0].options[0] = option;
			var i=1;
			$(this).find('model').each(function() {
				var option = new Option();
				option.value = option.text = $(this).attr('v');
				$('#select_model')[0].options[i] = option;
				i++;			
			});
		}
	});
}

function load_engine()
{
	var make = $('#select_make')[0].value; 
	$(CARS_ALL_XML).find('make').each(function() {
		if($(this).attr('v') == make)
		{
			var model = $('#select_model')[0].value;  
			$(this).find('model').each(function() {
				if($(this).attr('v') == model)
				{
					$('#select_engine').attr('disabled', false);
					$('#select_engine').attr('length', 0);
					var option = new Option();
					option.text = TRANSLATION_13+':';
					option.value = '%';
					$('#select_engine')[0].options[0] = option;
					var i=1;
					$(this).find('engine').each(function()
					{
						var option = new Option();
						option.value = option.text = $(this).attr('v');
						$('#select_engine')[0].options[i] = option;
						i++;
					});
				}
			});
		}
	});
}

