$(document).ready(function(){
	var scegli = '<option value="0">Scegli...</option>';
	var attendere = '<option value="-1">Attendere...</option>';
	$("select#Diametro").html(scegli);
	$("select#Diametro").attr("disabled", "disabled");
	$("select#TipoSupporto").html(scegli);
	$("select#TipoSupporto").attr("disabled", "disabled");
	$("select#TipoLamiera").html(scegli);
	$("select#TipoLamiera").attr("disabled", "disabled");
	$("select#TipoPavimenti").html(scegli);
	$("select#TipoPavimenti").attr("disabled", "disabled");
	$("select#volvenza").html(scegli);
	$("select#volvenza").attr("disabled", "disabled");
	$("select#TipoRuota").html(scegli);
	$("select#TipoRuota").attr("disabled", "disabled");
	$("select#Portata").html(scegli);
	$("select#Portata").attr("disabled", "disabled");
	$("select#SerieSupporti").html(scegli);
	$("select#SerieSupporti").attr("disabled", "disabled");
	$("select#applicazione").change(function(){ SelezionaTabelle(); });
	$("select#comeRuota").change(function(){ SelezionaTabelle(); });
	$("select#Diametro").change(function(){ ContaRecords() });
	$("select#TipoSupporto").change(function(){ ContaRecords() });
	$("select#TipoLamiera").change(function(){ ContaRecords() });
	$("select#TipoPavimenti").change(function(){ ContaRecords() });
	$("select#volvenza").change(function(){ ContaRecords() });
	$("select#TipoRuota").change(function(){ ContaRecords() });
	$("select#Portata").change(function(){ ContaRecords() });
	$("select#SerieSupporti").change(function(){ ContaRecords() });
	function SelezionaTabelle() 
	{
		var applic = $("select#applicazione option:selected").attr('value');
		var comeR  = $("select#comeRuota option:selected").attr('value');
		// applicazione non scelto è di default Collettività
		// comeruota non scelta è di default Ruota montata
		if(applic==0) {applic=1}; if(comeR==0) {comeR=3};
		var lang = 'it';
		loadXMLDoc('select#Diametro', 'Diametro', applic, comeR, lang);
		loadXMLDoc('select#TipoSupporto', 'Supporto', applic, comeR, lang);
		loadXMLDoc('select#TipoLamiera', 'Lamiera', applic, comeR, lang);
		loadXMLDoc('select#TipoPavimenti', 'Pavimenti', applic, comeR, lang);
		loadXMLDoc('select#volvenza', 'Volvenza', applic, comeR, lang);
		loadXMLDoc('select#TipoRuota', 'Ruota', applic, comeR, lang);
		loadXMLDoc('select#Portata', 'Portata', applic, comeR, lang);
		loadXMLDoc('select#SerieSupporti', 'SerieSup', applic, comeR, lang);
		ContaRecords(0);
		return;
	}
 
	function loadXMLDoc(form_tag, funzione, applic, comeR, lang)
	{
		$(form_tag).html(attendere);
		$(form_tag).attr("disabled", "disabled");
if (1==0){//	alert('*** METODO 1 ***');
		$.post("characteristics.php", {funzione:funzione, id:applic, id2:comeR, lng:lang},
		function(data)
		{
// alert(data);
			$(form_tag).removeAttr("disabled"); 
			$(form_tag).html(data);
		});
}
if (1==1){//	alert('*** METODO 2 ***');
		$.ajax ({   
			type: "GET",
			url: "characteristics.php",
			data:
			{
				funzione: funzione,
				id	: applic,
				id2	: comeR,
				lng	: lang
			},
			dataType: "html",
			success: function(data)
			{
// alert(data);
				$(form_tag).removeAttr("disabled"); 
				$(form_tag).html(data);
			},
			error: function() { alert("malfunzionamento "+form_tag); }
		});
}
if (1==0){//	alert('*** METODO 3-a ***');
		var xmlhttp;
		if (window.XMLHttpRequest)	{ xmlhttp=new XMLHttpRequest(); }
		else				{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				data = xmlhttp.responseText;
// alert(data);
				$(form_tag).removeAttr("disabled"); 
				$(form_tag).html(data);
			}
		}
		xmlhttp.open("POST","characteristics.php?timestamp=" + new Date().getTime(),true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlhttp.send
		(
			"funzione="	+ funzione
		+	"&id="		+ applic
		+	"&id2="		+ comeR
		+	"&lng="		+ lang
		);
}
if (1==0){//	alert('*** METODO 3-b ***');
		var xmlhttp=catchthebrowser();
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				data = xmlhttp.responseText;
// alert(data);
				$(form_tag).removeAttr("disabled"); 
				$(form_tag).html(data);
			}
		}
		xmlhttp.open("POST","characteristics.php?timestamp=" + new Date().getTime(),true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlhttp.send
		(
			"funzione="	+ funzione
		+	"&id="		+ applic
		+	"&id2="		+ comeR
		+	"&lng="		+ lang
		);
}
	}
	function ContaRecords(i)
	{
		i++; if (i > 10) { return false; }
		var applic	= $("select#applicazione option:selected").attr('value');
		var comeR	= $("select#comeRuota option:selected").attr('value');
		var v_dmt	= $("select#Diametro option:selected").attr('value');
		var v_suppt	= $("select#TipoSupporto option:selected").attr('value'); 
		var v_lamier	= $("select#TipoLamiera option:selected").attr('value');
		var v_pavt	= $("select#TipoPavimenti option:selected").attr('value'); 
		var v_vol	= $("select#volvenza option:selected").attr('value'); 
		var v_Ruot	= $("select#TipoRuota option:selected").attr('value');
		var v_por	= $("select#Portata option:selected").attr('value');
		var v_seriesup	= $("select#SerieSupporti option:selected").attr('value'); 
		if(applic==0) {applic=1}; if(comeR==0) {comeR=3};
		if (
			v_dmt==-1
		||	v_suppt==-1
		||	v_lamier==-1
		||	v_pavt==-1
		||	v_vol==-1
		||	v_Ruot==-1
		||	v_por==-1
		||	v_seriesup==-1
		) { setTimeout(function () { ContaRecords(i); }, 500); return false; };
		var xmlhttp=catchthebrowser();
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				data = xmlhttp.responseText;
				$("#result").html(data);
			}
		}
		xmlhttp.open("GET","elementscount.php?timestamp=" + new Date().getTime(),true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlhttp.send
		(
			"id="		+ applic
		+	"&id2="		+ comeR
		+	"&dmt="		+ v_dmt
		+	"&suppt="	+ v_suppt
		+	"&lamier="	+ v_lamier
		+	"&pavt="	+ v_pavt
		+	"&vol="		+ v_vol
		+	"&Ruot="	+ v_Ruot
		+	"&por="		+ v_por
		+	"&seriesup="	+ v_seriesup
		);
		return true;
	}
	function catchthebrowser()
	{
		var xmlhttp=false;
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
// alert('passo1');
			try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
			catch (e)
			{
// alert('passo2');
				try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
				catch (e) { xmlhttp = false; }
			}
		@end @*/
		if (!xmlhttp && typeof XMLHttpRequest!='undefined')
		{
// alert('passo3');
			try { xmlhttp = new XMLHttpRequest(); }
			catch (e) { xmlhttp = false; }
		}
		if (!xmlhttp && window.createRequest)
		{
// alert('passo4');
			try { xmlhttp = window.createRequest(); }
			catch (e) { xmlhttp = false; }
		}
		return xmlhttp;
	}
});