function oggettoAjax() {
var XHR = null;
var browserUtente = navigator.userAgent.toUpperCase();
 if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
 {
 XHR = new XMLHttpRequest();
 }
 else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) 
 {
         if(browserUtente.indexOf("MSIE 5") < 0)
         {XHR = new ActiveXObject("Msxml2.XMLHTTP");}
         else
         {XHR = new ActiveXObject("Microsoft.XMLHTTP");}
 }
return XHR;
} 

function prendiElementoDaId(id_elemento) {
 var elemento;
 if(document.getElementById)
  {elemento = document.getElementById(id_elemento);}
 else
  {elemento = document.all[id_elemento];}
 return elemento;
}


// Variabili per settare il timer
var timerID = 0;
var tStart  = null;
var timeout = 4;

function UpdateTimer() {
   if(timerID) {
      clearTimeout(timerID);
      clockID  = 0;
   }

   if(!tStart)
      tStart   = new Date();

   var   tDate = new Date();
   var   tDiff = tDate.getTime() - tStart.getTime();

   tDate.setTime(tDiff);
   //var tempo = prendiElementoDaId("numero");
   //tempo.innerHTML = tDate.getSeconds();
      if (tDate.getSeconds()>timeout) {
	     XMLHTTP.onreadystatechange = function(){return;};
	     XMLHTTP.abort();
	     var errore = prendiElementoDaId("modelli");
         errore.innerHTML = '<font color="#0099CC" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px"><b>Il server non risponde... <span style="cursor:pointer; color:#FF0000" onclick="Richiesta();">Riprova</span></b></font>';
         var loader = prendiElementoDaId("loader");
         loader.innerHTML = '<img src="images/cerca.gif">';
      }
      else {
         timerID = setTimeout("UpdateTimer()", 1000);
      }
}

function StartTimer() {
   tStart   = new Date();
   timerID  = setTimeout("UpdateTimer()", 1000);
}

function StopTimer() {
   if(timerID) {
      clearTimeout(timerID);
      timerID  = 0;
   }
   tStart = null;
}

function ResetTimer() {
	if (XMLHTTP.readyState == 4) {
		StopTimer();
	}
	else {
        tStart = null;
	}
}



function CaricaModelli()
{
ResetTimer();
if (XMLHTTP.readyState == 4 && XMLHTTP.status == 200)
    {
    var modelli = prendiElementoDaId("modelli");
	modelli.innerHTML = XMLHTTP.responseText;
	var loader = prendiElementoDaId("loader");
	loader.innerHTML = '<img src="images/cerca.gif">';
	}
	if (XMLHTTP.readyState == 4 && XMLHTTP.status != 200)
	{
	var loader = prendiElementoDaId("loader");
	loader.innerHTML = '<img src="images/cerca.gif">';
    var modelli = prendiElementoDaId("modelli");
	modelli.innerHTML = "Server error "+XMLHTTP.status;
	}
}

function Richiesta()
{
var marca = document.ricerca.marca.options[document.ricerca.marca.selectedIndex].value;
if (marca != "")
{
//document.cookie = "searchtype="+tipo+";path=/";
var loader = prendiElementoDaId("loader");
loader.innerHTML = '<img src="images/ajax-loader.gif">';
var modelli = prendiElementoDaId("modelli");
modelli.innerHTML = '<select name="modelli" style="width:210px; height:22px; color:#666666; font-size:13px; font-family: Century Gothic, Verdana, Tahoma, Arial"><option>attendere ................................</option></select>';
var url = "modelli.asp?"+'marca='+marca;
XMLHTTP = oggettoAjax();
XMLHTTP.open("GET", url, true);
XMLHTTP.setRequestHeader("connection", "close");
XMLHTTP.onreadystatechange = CaricaModelli;
StartTimer();
XMLHTTP.send(null);
}
else{
var modelli = prendiElementoDaId("modelli");
modelli.innerHTML = '<select name="modelli" style="width:210px; height:22px; color:#666666; font-size:13px; font-family: Century Gothic, Verdana, Tahoma, Arial"><option> .................................................</option></select>';
}
}

function Vai() {
document.ricerca.marca.value='';
window.location.href=document.ricerca.modelli.options[document.ricerca.modelli.selectedIndex].value;
}
