	//Esegue la chiamata alla pagina asp che restituisce i dati
	var arrayPreventivo;	
	var arrayProdotti;
	var currentProdotto;
	
	
	function GetDataFromXML(){
	myurl='../xml/Preventivo.xml'
	//alert (myurl);
		new Ajax.Request(myurl,   
		{     method:'get',
		      asynchronous :true
		      ,
				onCreate: function()
				{
				
				}
				,
				onSuccess: function(transport)
				{       
					
					var responsexml = transport.responseText || "no response text"; 
					//alert(responsexml);
					response = xml2json(parseXml(responsexml),'');
					//alert(response);
		
					try
					{
						arrayPreventivo=response.evalJSON() ;
					}
					catch (e) 
					{
				 		alert('Errore dulante la verifica dei dati JSON')
				    }
				    
				    if (arrayPreventivo!='')
				    {
						popolaTipologie();
					}
				}
				,     
				onFailure: function()
				{ 
					alert('Something went wrong...');
				}   
				,
				onComplete: function()
				{
					
				}
		}); 

	}


//Variazione Combo ********************************************************************************************
	function RichiedenteCambiato()
	{
		SvuotaTuttiCombo();
		if ($('Richiedente').selectedIndex>0)
		{
		PopolaProdotti();
		}
	}
	
	function ProdottoCambiato()
	{
		SvuotaComboDipendentiDalProdotto();
		if ($('Prodotto').selectedIndex>0)
		{
		PopolaAnticipoDurata();
		}
	}


	function DurataCambiata()
	{
		$('Riscatto').options.length = 1;
		PopolaRiscatto();
	}

//Popolazione Combo *********************************************************************************************
	function popolaTipologie()
	{
		$('Richiedente').options.length = 1;
		//$('Richiedente').options[$('Richiedente').options.length]= new Option('-- Seleziona --','0');
		arrayPreventivo.preventivo.richiedente.each(
			function(item)
			{
				$('Richiedente').options[$('Richiedente').options.length]= new Option(item.destiporichiedente,item.destiporichiedente);			
			}
		)
		AggiungiHandler();
	}
	
	function PopolaProdotti()
	{
		$('Prodotto').innerHTML='';
		
		objOption=document.createElement("option");
		objOption.innerHTML = '-- Seleziona --';
		objOption.value = '0' ;
		$('Prodotto').appendChild(objOption);
					
		arrayPreventivo.preventivo.richiedente.each(
			function(item)
			{
				if(item.destiporichiedente==$F('Richiedente'))
				{
					arrayProdotti=item;
				}
			}
		)
	
	
		arrayProdotti.options.option.each(
			function(item)
			{
				try
				{
					arraysubitems = item.options.option;
					
					optGroup = document.createElement('optgroup')
					optGroup.label = item.des

					//$('Prodotto').options[$('Prodotto').options.length] = new Option(item.des,item.des);
					
					$('Prodotto').appendChild(optGroup)
					arraysubitems.each(
						function(itemsub)
						{
							objOption=document.createElement("option");
							objOption.innerHTML = itemsub.des;
							objOption.value = itemsub.des ;
	
							optGroup.appendChild(objOption);
						}
					)
				}
				catch(e)
				{
					objOption=document.createElement("option");
					objOption.innerHTML = item.des;
					objOption.value = item.des ;
					$('Prodotto').appendChild(objOption);
				}
			}
		)
		//$('Prodotto').options[$('Prodotto').options.length]= new Option(item.desprodotto,item.desprodotto);	
	}
	
	function PopolaAnticipoDurata()
	{
		arrayProdotti.prodotti.prodotto.each(
			function(item)
			{
				if(item.desprodotto==$F('Prodotto'))
				{
					currentProdotto = item;
				}			
			}
		)
		
		currentProdotto.anticipi.anticipo.each(
			function(item)
			{
				$('Anticipo').options[$('Anticipo').options.length]= new Option(item,item);
			}
		)
		
		currentProdotto.durate.durata.each(
			function(item)
			{
				$('Durata').options[$('Durata').options.length]= new Option(item.desDurata,item.desDurata);
			}
		)
	}
	
	
	function PopolaRiscatto()
	{
		var currentDurata
		currentProdotto.durate.durata.each(
			function(item)
			{
				if($F('Durata')==item.desDurata)
				{
					currentDurata = item;
				}
			}
		)
		
		try
		{
			currentDurata.riscatti.riscatto.each(
				function(item){$('Riscatto').options[$('Riscatto').options.length]= new Option(item,item);}
			)
		}
		catch (e) 
		{
			$('Riscatto').options[$('Riscatto').options.length]= new Option(currentDurata.riscatti.riscatto,currentDurata.riscatti.riscatto);
	    }

	}
	
	function SvuotaTuttiCombo()
	{
		$('Prodotto').options.length = 1;
		SvuotaComboDipendentiDalProdotto();
	}
	
	
	function SvuotaComboDipendentiDalProdotto()
	{
		$('Anticipo').options.length = 1;
		$('Durata').options.length = 1;
		$('Riscatto').options.length = 1;	
	}


	function AggiungiHandler()
	{
	  Event.observe('Richiedente', 'change', RichiedenteCambiato);	  	
	  Event.observe('Prodotto', 'change', ProdottoCambiato);
	  Event.observe('Durata', 'change', DurataCambiata);	  
	}

	Event.observe(window, 'load',GetDataFromXML);	
	
	
	//VALIDAZIONE DELLA FORM PER SUBMIT*******************************************************	
	function showInvia()
	{
		$('invia').show();	
	}
	
	function hideInvia()
	{
		$('invia').hide();
	}
	
	
	function FormValidator(theForm)
	{
		
		if($('invia').visible=false){return (false);}

		if (combovuoto(theForm.Richiedente,"Seleziona la Tipologia richiedente") == "-1"){return (false);}
				
		if (testovuoto(theForm.Azienda,"il campo Ragione sociale/Cognome Nome è vuoto") == "-1"){return (false);}
		if (testovuoto(theForm.sede_amministrativa_via,"il campo Sede Amministrativa è vuoto") == "-1"){return (false);}
		if (testovuoto(theForm.sede_amministrativa_civico,"il campo Sede Amministrativa è vuoto") == "-1"){return (false);}
		if (testovuoto(theForm.sede_amministrativa_citta,"il campo Sede Amministrativa è vuoto") == "-1"){return (false);}
		if (testovuoto(theForm.sede_legale_via,"il campo Sede Legale è vuoto") == "-1"){return (false);}
		if (testovuoto(theForm.sede_legale_civico,"il campo Sede Legale è vuoto") == "-1"){return (false);}
		if (testovuoto(theForm.sede_legale_citta,"il campo Sede Legale è vuoto") == "-1"){return (false);}
	//	if (testovuoto(theForm.Cognome,"il campo Ragione sociale/Cognome Nome è vuoto") == "-1"){return (false);}
	//	if (testovuoto(theForm.Sede,"il campo Sede legale/Comune Residenza è vuoto") == "-1"){return (false);}
		
		
	//	if (LunghezzaCampoUgualeA(theForm.CodiceFiscale,16,"Il campo Codice Fiscale deve essere lungo 16 caratteri") == "-1") {return (false);}		
		if (combovuoto(theForm.Provincia,"Seleziona la Provincia") == "-1"){return (false);}
		if (testovuoto(theForm.Rif,"il campo Nominativo riferimento è vuoto") == "-1"){return (false);}
		if (testovuoto(theForm.Email,"il campo Email è vuoto") == "-1"){return (false);}
		if (emailvalidator(theForm.Email," email non valido") == "-1"){return (false);}
		if (testovuoto(theForm.Tel,"il campo Telefono è vuoto") == "-1"){return (false);}
	//	if (combovuoto(theForm.Prodotto,"Seleziona un Prodotto") == "-1"){return (false);}
	//	if (testovuoto(theForm.Desc,"il campo Descrizione del bene è vuoto") == "-1"){return (false);}
	//	if (testovuoto(theForm.Fornitore,"il campo Nome del fornitore è vuoto") == "-1"){return (false);}
		if (testovuoto(theForm.Importo,"il campo Importo del bene (al netto dell\'IVA) è vuoto") == "-1"){return (false);}
		if (combovuoto(theForm.Anticipo,"Seleziona una % Anticipo") == "-1"){return (false);}
		if (combovuoto(theForm.Prodotto,"Seleziona un Prodotto") == "-1"){return (false);}
		if (combovuoto(theForm.Durata,"Seleziona una Durata") == "-1"){return (false);}
		if (combovuoto(theForm.Riscatto,"Seleziona una % Riscatto") == "-1"){return (false);}
		
		if (theForm.Richiedente.value == "Privato")
		{
			if (testovuoto(theForm.CodiceFiscale,"il campo Codice Fiscale è vuoto") == "-1"){return (false);}
			if (ControllaCF(theForm.CodiceFiscale, "Codice fiscale non valido")=="-1"){return (false);}
		}
		else
		{
			if (testovuoto(theForm.PIVA,"il campo Partita IVA è vuoto") == "-1"){return (false);}
			if (ControllaPIVA(theForm.PIVA, "Partita IVA non valida")=="-1"){return (false);}
			if (combovuoto(theForm.Attivita,"Seleziona Attività") == "-1"){return (false);}
		}
		
	}
	
	




