// JavaScript Document

if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}

var data = new Date();
var km = new Array("0","5000","10000","20000","50000","100000","100000+");
var km_value = new Array("0","5000","10000","20000","50000","100000","-1");
var price = new Array("0","5000","10000","20000","30000","50000","50000+");
var price_value = new Array("0","5000","10000","20000","30000","50000","-1");

function cerca() {
	if ($("input[name=action]").val() == "usato") {
		$("#schedaautousate").load(
			"auto_out.php", 
			{
				'marca': $("select[name=marca]").val(),
				'modello': $("select[name=modello]").val(),
				'alimentazione': $("select[name=alimentazione]").val(),
				'year_start': $("select[name=year_start]").val(),
				'year_end': $("select[name=year_end]").val(),
				'km_start': $("select[name=km_start]").val(),
				'km_end': $("select[name=km_end]").val(),
				'price_start': $("select[name=price_start]").val(),
				'price_end': $("select[name=price_end]").val(),
				'orderby': $("select[name=orderby]").val(),
				'order': $("select[name=order]").val(),
				'perpage': $("select[name=perpage]").val(),
				'page': $("input[name=page]").val(),
				'action': $("input[name=action]").val()
			},
			function() {
				$(".page").text($("input[name=page]").val());
				$(".totpage").text($("input[name=totpage]").val());
			}
		);
	} else {
		$("#schedaautousate").load(
			"auto_out.php", 
			{
				'marca': $("select[name=marca]").val(),
				'modello': $("select[name=modello]").val(),
				'alimentazione': $("select[name=alimentazione]").val(),
				'price_start': $("select[name=price_start]").val(),
				'price_end': $("select[name=price_end]").val(),
				'orderby': $("select[name=orderby]").val(),
				'order': $("select[name=order]").val(),
				'perpage': $("select[name=perpage]").val(),
				'page': $("input[name=page]").val(),
				'action': $("input[name=action]").val()
			},
			function() {
				$(".page").text($("input[name=page]").val());
				$(".totpage").text($("input[name=totpage]").val());
			}
		);
	}
}

function page(pagina) {
	if (pagina == "first") { $("input[name=page]").val(1); cerca(); }
	var value = $("input[name=page]").val();
	var vmax = $(".totpage:first").text();
	if (pagina == "prev" && value > 1) { $("input[name=page]").val(Number(value)-1); cerca(); }
	if (pagina == "next" && value < Number(vmax)) { $("input[name=page]").val(Number(value)+1); cerca(); }
	if (pagina == "last") { $("input[name=page]").val($(".totpage:first").text()); cerca(); }
}

function cercaBody() {
	$("input[name=page]").val(1);
	cerca();
}

$(document).ready(function(){

	$("select[name=marca]").change(function(){
		var valore = $("select[name=marca]").val();
		if (valore != "") {
			$("select[name=modello]").removeAttr("disabled");
			$("select[name=modello]").load(	
				"ricerca_fields.php",
				{
					'marca': $("select[name=marca]").val(),
					'action': $("input[name=action]").val()
				}
			);
		} else {
			$("select[name=modello]").attr("disabled","disabled");
			$("select[name=modello]").html("");
		}
	});
	
	$("select[name=year_start]").change(function(){
		var valore = $("select[name=year_start]").val();
		if (valore > 0) {
			$("select[name=year_end]").text("");
			for (var i = (data.getFullYear()); i > valore-1; i--) {
				$("select[name=year_end]").html($("select[name=year_end]").html()+"<option value='"+i+"'>"+i+"</option>");
			}
		} else {
			$("select[name=year_end]").html($("select[name=year_start]").html());
		}
	});
	
	$("select[name=km_start]").change(function(){
		var valore = $("select[name=km_start]").val();
		if (valore != "") {
			$("select[name=km_end]").text("");
			if (km.indexOf(valore) > -1) {
				for (var i = km.indexOf(valore)+1; i < km.length; i++) {
					$("select[name=km_end]").html($("select[name=km_end]").html()+"<option value='"+km_value[i]+"'>"+km[i]+"</option>");
				}
			} else {
				$("select[name=km_end]").html("<option value='"+km_value[km.length-1]+"'>"+km[km.length-1]+"</option>");
			}
		} else {
			$("select[name=km_end]").html($("select[name=km_start]").html());
		}
	});

	$("select[name=price_start]").change(function(){
		var valore = $("select[name=price_start]").val();
		if (valore != "") {
			$("select[name=price_end]").text("");
			if (price.indexOf(valore) > -1) {
				for (var i = price.indexOf(valore)+1; i < price.length; i++) {
					$("select[name=price_end]").html($("select[name=price_end]").html()+"<option value='"+price_value[i]+"'>"+price[i]+"</option>");
				}
			} else {
				$("select[name=price_end]").html("<option value='"+price_value[price.length-1]+"'>"+price[price.length-1]+"</option>");
			}
		} else {
			$("select[name=price_end]").html($("select[name=price_start]").html());
		}
	});
	
	$("input[name=cerca_submit]").click(function(){
		$("input[name=page]").val(1);
		cerca();
	});
	
	if ($("select[name=modello]").val() != "") {
		var valore = $("select[name=modello]").val();
		$("select[name=modello]").removeAttr("disabled");
		$("select[name=modello]").load(	
			"ricerca_fields.php",
			{
				'marca': $("select[name=marca]").val(),
				'action': $("input[name=action]").val()
			},
			function() {
				$("select[name=modello]").val(valore);
			}
		);
	}
	
	if ($("select[name=marca]").val() != "" && $("select[name=modello]").val() == "") {
		$("select[name=marca]").change();
	}
	
	$("select[name=perpage]").change(function(){
		$("input[name=page]").val(1);
		cerca();
	});
	
});
