// JavaScript Document

function initFormRicetta() {
	if ($("#formCercaRicetta").html()) {
		resetFormRicetta();
		
		//setInterval(checkChangeSelectRicetta,100);
		
		$("#avviaRicerca").click(function() {
			var textPrimi = $("select[name=primi]").parent().children("div").children("span").text();
			var primi = $("select[name=primi] > option").filter(function(){
				return $(this).text() == textPrimi;															 
			}).val();
			
			var textSecondi = $("select[name=secondi]").parent().children("div").children("span").text();
			var secondi = $("select[name=secondi] > option").filter(function(){
				return $(this).text() == textSecondi;															 
			}).val();
			
			var textForno = $("select[name=forno]").parent().children("div").children("span").text();
			var forno = $("select[name=forno] > option").filter(function(){
				return $(this).text() == textForno;															 
			}).val();
			
			var textContorni = $("select[name=contorni]").parent().children("div").children("span").text();
			var contorni = $("select[name=contorni] > option").filter(function(){
				return $(this).text() == textContorni;															 
			}).val();
			
			var textSughetti = $("select[name=sughetti]").parent().children("div").children("span").text();
			var sughetti = $("select[name=sughetti] > option").filter(function(){
				return $(this).text() == textSughetti;															 
			}).val();
						
			if (primi+secondi+forno+contorni+sughetti == "") {
				BlockUI("Attenzione","Seleziona almeno un criterio di ricerca");
				return false;
			}
			
			var dataString = "";
				dataString += "primi="+primi;
				dataString += "|secondi="+secondi;
				dataString += "|forno="+forno;
				dataString += "|contorni="+contorni;
				dataString += "|sughetti="+sughetti;

			LoadingBlockUI();
			document.location.href = BASE_URL+"it/ricetta/ricerca/"+dataString;
			return false;
		});
	}
}

function resetFormRicetta() {
	$("select[name=primi]").parent().children("div").children("span").text($("select[name=primi] > option[value='']").text());
	$("select[name=secondi]").parent().children("div").children("span").text($("select[name=secondi] > option[value='']").text());
	$("select[name=forno]").parent().children("div").children("span").text($("select[name=forno] > option[value='']").text());
	$("select[name=contorni]").parent().children("div").children("span").text($("select[name=contorni] > option[value='']").text());
	$("select[name=sughetti]").parent().children("div").children("span").text($("select[name=sughetti] > option[value='']").text());

}

/*var nowPrimi = "";
var nowSecondi = "";
var nowForno = "";
var nowContorni = "";
var nowSughetti = "";

function checkChangeSelectRicetta() {
	
	
	var valPrimi = $("select[name=primi]").parent().children("div").children("span").text();
	if (nowPrimi != valPrimi) {
		nowPrimi = valPrimi;
		var img = $("select[name=primi] > option").filter(function() {
			return $(this).text() == valPrimi;
		}).attr("ref");
	}
	
	var valSecondi = $("select[name=secondi]").parent().children("div").children("span").text();
	if (nowSecondi != valSecondi) {
		nowSecondi = valSecondi;
		var img = $("select[name=secondi] > option").filter(function() {
			return $(this).text() == valSecondi;
		}).attr("ref");
	}
	
	var valForno = $("select[name=forno]").parent().children("div").children("span").text();
	if (nowForno != valForno) {
		nowForno = valForno;
		var img = $("select[name=forno] > option").filter(function() {
			return $(this).text() == valForno;
		}).attr("ref");
	}
	
	var valContorni = $("select[name=contorni]").parent().children("div").children("span").text();
	if (nowContorni != valContorni) {
		nowContorni = valContorni;
		var img = $("select[name=contorni] > option").filter(function() {
			return $(this).text() == valContorni;
		}).attr("ref");
	}
	
	var valSughetti = $("select[name=sughetti]").parent().children("div").children("span").text();
	if (nowSughetti != valSughetti) {
		nowSughetti = valSughetti;
		var img = $("select[name=sughetti] > option").filter(function() {
			return $(this).text() == valSughetti;
		}).attr("ref");
	}
	
}*/
