var xmlhttp;
var caricamentoRegioniInCorso = false;

var TempComuni;
var TempRegioni;
var TempProvincie;
			
//function settaDdlId(r,p,c)
//{
//	
//	comuni = c;
//	provincie = p;
//	regioni = r;
//}
			
function ParseXmlAndBind(ddl,str) 
{
	var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false";
	xmlDoc.loadXML(str);
	for(i=0; i<xmlDoc.documentElement.childNodes.length; i++)
	{
		var x = xmlDoc.documentElement.childNodes[i];
		AddOption(ddl,x.attributes[1].value,x.attributes[0].value);
	}
}
			
function removeChild(ddl)
{
	var l = ddl.options.length;

	for(i=0; i<l; i++)
	{
		ddl.options.remove(i);
	}
	if(ddl.options.length != 0)
		removeChild(ddl);
}
			
function addAllChild(ddl,xmlstr)
{
	ParseXmlAndBind(ddl,xmlstr);
}
		
function GetSelectedId(ddl)
{
	var id = '0';
	
	for(i=0; i<ddl.options.length; i++)
	{
		if (ddl.options[i].selected)
		{
			id = ddl.options[i].value;break;
		}
	}
	return id;
}
			
function CambiaProvincia(regClientID,provClientID,comClientID)
{
	
	// devo caricare i comuni
	if (!caricamentoRegioniInCorso)
	{
		var idR =GetSelectedId(document.getElementById(regClientID));
		var idP =GetSelectedId(document.getElementById(provClientID));
		if (idP != '0')
		{
			xmlhttp = new XMLHttpRequest();
			TempComuni=comClientID;
			xmlhttp.onreadystatechange = state_change_Provincie;
			xmlhttp.open("GET","provincie.prov?prov_id="+idP+"&reg_id="+idR,true);
			xmlhttp.send(null);
		} 
		else 
		{
			removeChild(document.getElementById(comClientID));
			AddOption(document.getElementById(comClientID), "[Comuni]","0");
		}
	}
}
			
function CambiaRegione(regClientID,provClientID,comClientID)
{  
	
	
	removeChild(document.getElementById(provClientID));
	removeChild(document.getElementById(comClientID));
	
	AddOption(document.getElementById(comClientID), "[Comuni]","0");
	
	caricamentoRegioniInCorso = true; 
	var id =GetSelectedId(document.getElementById(regClientID));
	
	if (id != '0')
	{
		xmlhttp = new XMLHttpRequest();
		TempProvincie=provClientID;
		xmlhttp.onreadystatechange = state_change_Regioni;
		xmlhttp.open("GET","provincie.prov?reg_id="+id,true);
		xmlhttp.send(null);
	} 
	else 
	{
		AddOption(document.getElementById(provClientID), "[Provincie]","0");
	}
}
			
function state_change_Provincie()
{
	if (xmlhttp.readyState == 4)
	{
		var ddlistR = document.getElementById(TempComuni);
		removeChild(ddlistR);
		var str = xmlhttp.responseText;
		addAllChild(ddlistR,str);
	}
}
			
function state_change_Regioni()
{
	
	
	if (xmlhttp.readyState == 4)
	{
		
		var ddlistR = document.getElementById(TempProvincie);
		removeChild(ddlistR);
		var str = xmlhttp.responseText;
		addAllChild(ddlistR,str);
	}
	caricamentoRegioniInCorso = false;
}
			
function AddOption(ddl,text,value)
{
	var oOption = document.createElement("OPTION");
	oOption.text=text;
	oOption.value=value;
	ddl.add(oOption);
}		