var fhRegioneSelezionata="";

function fhAttivaRegione(regione)
{
	fhRegioneSelezionata = regione;
	var elFiliali = document.getElementById('fhFiliali');
	new Ajax.Updater('fhFiliali','/richieste/gestXmlHome.aspx?a=req&regione=' + escape(regione),{method:'get',encoding:'ISO-8859-1'});
	elFiliali.style.display = "";	
	
	new Ajax.Request('/richieste/gestXmlProvincie.aspx?a=req&reg=' + regione,{method:'get',encoding:'ISO-8859-1',onComplete: fhCompleta});

}

function fhCompleta(originalRequest){
	parentnode=originalRequest.responseXML.getElementsByTagName("province")[0].childNodes;
    var selectBox = document.getElementById('fhSelectBoxProvincia');
    
    //svuoto tutti prima
    while (selectBox.firstChild) {
        selectBox.removeChild(selectBox.firstChild);
    }
    
    //metto i nuovi
	for (i=0;i<parentnode.length;i++) {
		if (parentnode[i].nodeType == 1){
            var provID = parentnode[i].getAttribute("val");
            var provName = parentnode[i].childNodes[0].nodeValue;
            
            if(navigator.userAgent.indexOf("MSIE") != -1)
            {
                var anOption = document.createElement("option");
                selectBox.options.add(anOption);
                anOption.innerText = provName;
                anOption.value = provID;
		    } else {
		        var elOption = document.createElement("option");
		        elOption.setAttribute("value",provID)
		        elOption.text = provName;
		        selectBox.appendChild(elOption);		    
            }
	    }
	}
	//okToProceed=1;
	var elForm = document.getElementById('fhForm');
	elForm.style.display = "";
	var elStepOne = document.getElementById('fhStepOne');
	elStepOne.style.display = "none";
}

function fhIndietro()
{
	var elForm = document.getElementById('fhForm');
	elForm.style.display = "none";
	
	var elStepOne = document.getElementById('fhStepOne');
	elStepOne.style.display = "";
	
	var elFiliali = document.getElementById('fhFiliali');
	elFiliali.innerHTML = "";
	fhRegioneSelezionata = "";
}

function fhInviaRichiesta()
{
	//fhRegioneSelezionata
	if(document.getElementById('fhNome').value != '')
	{
		var nome = escape(document.getElementById('fhNome').value);
	}
	if(document.getElementById('fhSelectBoxProvincia').value != '')
	{
		var prov = escape(document.getElementById('fhSelectBoxProvincia').value);
	}
	if(document.getElementById('fhTel').value != '')
	{
		var tel = escape(document.getElementById('fhTel').value);
	}
	if(document.getElementById('fhEmail').value != '')
	{
		var email = escape(document.getElementById('fhEmail').value);
	}
	if(document.getElementById('fhTesto').value != '')
	{
		var testo = escape(document.getElementById('fhTesto').value);
	}
	regione=escape(fhRegioneSelezionata);
	if(document.getElementById('fhCheck').checked == true)
	{
		if(nome != undefined && tel != undefined && email != undefined && testo != undefined && regione != undefined)
		{
			var param = 'nome=' + nome + '&prov=' + prov + '&tel=' + tel + '&email=' + email + '&testo=' + testo + '&regione=' + regione;
			document.getElementById('icoLoader').style.visibility = 'visible';
			new Ajax.Request('/richieste/gestXmlHome.aspx?a=post',{method:'post',encoding:'ISO-8859-1',postBody:param,onSuccess:fhCompletaPost,onFailure:fhFallita});
		}
		else
		{
			alert('Devi compilare tutti i campi obbligatori.');
		}
	}
	else
	{
		alert('Devi accettare le condizioni di utilizzo.');
	}
}

function fhFallita(originalRequest)
{
	alert('Si e\' verificato un errore. Ricompila la form.');
	fhIndietro();
}

function fhCompletaPost(originalRequest)
{
	if(originalRequest.status = 200)
	{
	    sendtrack('registrazione_effettuatax');
	    
		var elForm = document.getElementById('fhForm');
		elForm.style.display = "none";
		
		var elStepOne = document.getElementById('fhStepOne');
		elStepOne.style.display = "none";
		
		var elFiliali = document.getElementById('fhFiliali');
		elFiliali.innerHTML = "";
		fhRegioneSelezionata = "";
		
		var elSuccess = document.getElementById('fhSuccess');
		elSuccess.style.display = "";
	}
	else
	{
		alert('Si &egrave; verificato un errore. Ricompila la form.');
		fhIndietro();
	}
}
