//-- Funzione per il caricamento dell'oggetto XMLHttpRequest
//-- compatibile con i browsers pių recenti e diffusi
function getXMLHttpRequest() {
	// lista delle variabili locali
	var XHR = null;
	// informazioni sul nome del browser
	var browserUtente = navigator.userAgent.toUpperCase();
	// browser standard con supporto nativo
	// non importa il tipo di browser
	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object") {
		 // browser Internet Explorer
		 // č necessario filtrare la versione 4
  		XHR = new XMLHttpRequest();
	}
    else if ( window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0 ) {
		// la versione 6 di IE ha un nome differente
		// per il tipo di oggetto ActiveX
		if(browserUtente.indexOf("MSIE 5") < 0) {
			XHR = new ActiveXObject("Msxml2.XMLHTTP");
		}
		else {
			// le versioni 5 e 5.5 invece sfruttano lo stesso nome
		    XHR = new ActiveXObject("Microsoft.XMLHTTP");
		}
	 }
	 return XHR;
} 

//-- Functione che permette di svuotare una select
function svuotaHtmlSelect(sel) {
	num_option=sel.options.length;	
	for(a=num_option;a>=0;a--) {		
		sel.options[a]=null;	 
	}		
}

function aggiungiItemSelect(sel, valore, testo) {
	num_option=sel.options.length;	
	sel.options[num_option]=new Option(testo,valore,false,false);
}

//-- Funzione per il caricamento delle versioni vettura
//-- Questa funzione presuppone che ci sia una combo box da popolare con
function getVersioneVettura() {
	var ajaxObj = getXMLHttpRequest();
	var vettura = document.getElementById('vettura')
	var elemento = document.getElementById('versione');
	
	// svuoto la select e il campo prezzo
	svuotaHtmlSelect(elemento);

	// inizializzo la richiesta in post
	ajaxObj.open("post", "tfs_ajaxSrvVetture.asp", true);
	// imposto il giusto header
	ajaxObj.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    // imposto la callback per la ricezione dei dati
    ajaxObj.onreadystatechange = function() {  
      // nel caso in cui la funzione sia completata verifico l'esito
      if(ajaxObj.readyState == 4) {
        // verifico la risposta del server
        if(ajaxObj.status == '200') {
          // operazione avvenuta con successo carico la lista di vetture
		  if ( ajaxObj.responseText.length > 0 ) {
			  aggiungiItemSelect(elemento,'','Selezionare')
			  var arrVersioni = ajaxObj.responseText.split(',');
			  for ( i=0; i<arrVersioni.length; i++ ) {
			  		aggiungiItemSelect(elemento, arrVersioni[i], arrVersioni[i]);
			  }
		  }
		}
        else {
          // errore di caricamento
		  alert('Errore riscontrato: ' + ajaxObj.status);
        }
      } 
    }
	// effettuo la richiesta inviando la variabile leggi con contenuto Dante
	ajaxObj.send("op=carica_versione&vettura="+escape(vettura.value)); 
}

function attivaClasseMerito()
{
	if (document.getElementById('assicurato').value == '')
	{
		document.getElementById('classedimeritocip').value="14";
		document.getElementById('classedimeritocip').disabled="yes";
	}
	else
	{
		document.getElementById('classedimeritocip').disabled="";
	}	
}

function calcola_rca() {
	var ajaxObj = getXMLHttpRequest();
	var vettura = document.getElementById('vettura').value;
	var versione = document.getElementById('versione').value;	
	var provincia = document.getElementById('provincia').value;	
	var tipoPacchetto = document.getElementById('tipoPacchetto').value;	
	var infortunio = document.getElementById('infortunio').value;		
	var classedimeritocip = document.getElementById('classedimeritocip').value;			
	var satellitare = document.getElementById('satellitare').value;		
	var gancio_traino = document.getElementById('gancio_traino').value;		
	// inizializzo la richiesta in post
	ajaxObj.open("post", "tfs_ajaxSrvRcaStandalone.asp", true);
	// imposto il giusto header
	ajaxObj.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    // imposto la callback per la ricezione dei dati
    ajaxObj.onreadystatechange = function() {  
      // nel caso in cui la funzione sia completata verifico l'esito
      if(ajaxObj.readyState == 4) {
        // verifico la risposta del server
        if(ajaxObj.status == '200') {
          // operazione avvenuta con successo carico la lista di vetture
		  if ( ajaxObj.responseText.length > 0 ) {
			  var arr = ajaxObj.responseText.split('#');
			  document.getElementById('totale_calcolo').innerHTML = "<strong>Premio finito Toyota&nbsp;&euro; "+arr[3]+"<strong>";
			  if ( arr[3] != '0,00' ) {
				  document.getElementById('dettaglio_rca').innerHTML = "<img src=\"multimedia/generale/grafica/list_arrow.gif\" border=\"0\"/>&nbsp;<a onClick=\"MM_openBrWindow('multimedia/calcolo/info/tfs_info_rca.asp?&comune="+provincia+"&durata=12&netto="+arr[0]+"&tasse="+arr[1]+"&ssn="+arr[2]+"&premio="+arr[3]+"','','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=350,height=200')\" href=\"#\" class=\"link_grigio\">Leggi tutti i dettagli</a>";
			  }
		  }
		}
        else {
          // errore di caricamento
		  alert('Errore riscontrato: ' + ajaxObj.status);
        }
      } 
    }
	// effettuo la richiesta inviando la variabile leggi con contenuto Dante
	ajaxObj.send("vettura="+escape(vettura)+"&versione="+escape(versione)+"&provincia="+escape(provincia)+"&tipoPacchetto="+tipoPacchetto+"&infortunio="+infortunio+"&classedimeritocip="+classedimeritocip+"&satellitare="+satellitare+"&gancio_traino="+escape(gancio_traino));
}