var marca;
var modello;

$(document).ready(function(){

	$('#modello').attr('disabled', 'disabled');
	$('#modelSelect2').attr('disabled', 'disabled');

	registerEvents(); 

});

function registerEvents(){
	
	$("#marca_sel").bind("change", function(event){
		if($(this).val()==-1){
			var options = '';
			options += '<option value="-24">Modello</option>';
			$("#modello").html(options);
			$('#modello option:first').attr('selected', 'selected');				
			$("#modello").attr("disabled", "disabled");	
			$("#modello").addClass("disabilitato");
			return;
		}

		$("#modello").removeAttr("disabled");		
		$("#modello").removeClass("disabilitato");
		marca=$(this).val();
		/* CHIAMATA CACHATA COME MARCA? o JSON COME SOPRA? */
		$.get("/2.0/pages/usato/automobile/scripts/elenco_modelli_marca.php",{"tipo": 'marca_modelli', "marca": marca,"ajax": 'true'}, function(z){
			$("select#modello").html(z);
			if(modello_get!=''){ 
				///alert(modello_get);
				$("select#modello").val(modello_get);
				$('#do_search').click();

			}
			if(marca_get!=''){ 
				//$('#do_search').click();
			}
		});
		//alert('messi i modelli');

	});

	$("#makerSelect2").bind("change", function(event){
		if($(this).val()==-1){
			var options = '';
			options += '<option value="-24">Modello</option>';
			$("#modelSelect2").html(options);
			$('#modelSelect2 option:first').attr('selected', 'selected');				
			$("#modelSelect2").attr("disabled", "disabled");	
			$("#modelSelect2").addClass("disabilitato");
			return;
		}
		// else
		$("#modelSelect2").removeAttr("disabled");		
		$("#modelSelect2").removeClass("disabilitato");
		marca=$(this).val();

		/* CHIAMATA CACHATA COME MARCA? o JSON COME SOPRA? */
		$.get("/2.0/pages/usato/automobile/scripts/elenco_modelli_marca.php",{"tipo": 'marca_modelli', "marca": marca,"ajax": 'true'}, function(z){
			$("select#modelSelect2").html(z);
		});

		// FINE AJAX populate.php modello
	});

	elencoCacheMarca("cache_marca", "marca_sel");

	//updateModelliList();
	artSearchHead();
	artSelectModelHead();
}

function elencoCacheMarca(tipo, elenco){
	var tt=tipo;
	//alert("Metto elenco marca per elenco '"+elenco+"' e tipo "+tt);
	$.get("/2.0/pages/usato/automobile/scripts/elenco_marca.php",{ tipo: tt}, function(j){

		$("select#marca_sel").html(j);
		$('select#marca_sel option:first').attr('selected', 'selected');
		$("select#makerSelect2").html(j);
		$('select#makerSelect2').attr('selected', 'selected');

	})
	// FINE AJAX populate.php modello
}


function artSearchHead(){
	$('#do_search_head_art').click(function() {
		
		var currurl='http://www.omniauto.it/usato/?checkusato=true';
		
		marca= $('#makerSelect2').val();
		if(marca==-1){ alert('Seleziona una marca'); return false; }
		currurl = currurl+"&marca="+marca;

		modello= $('#modelSelect2').val();
		if(modello!=-1 && modello!=''){ currurl = currurl+"&modello="+modello; }
		
		window.location=currurl;
		
		return false;
	});	
}

function artSelectModelHead(){
	$("#modelSelect2").bind("change", function(event){
			var currurl='http://www.omniauto.it/usato/?checkusato=true';
			
			marca= $('#makerSelect2').val();
			if(marca==-1){ alert('Seleziona una marca'); return false; }
			currurl = currurl+"&marca="+marca;
	
			modello= $('#modelSelect2').val();
			if(modello!=-1 && modello!=''){ currurl = currurl+"&modello="+modello; }

			window.location=currurl;
			
			return false;

	  	});
}


