var http_request = false;
   function makePOSTRequest(url, parameters,divtochange,funzione,old) {
	  http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      //alert(funzione);
      http_request.onreadystatechange = function() { funzione(divtochange,old); }; 
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

function alertContents(divtochange,old) { 

if (http_request.readyState == 4)
 { 
	if (http_request.status == 200)
	 {
		document.getElementById(divtochange).innerHTML = http_request.responseText;
	} 
	else
	 {
	alert('Si Sono verificati problemi nell\'elaborazione dei dati.');	
	} 
}else
{	
	document.getElementById(divtochange).innerHTML ="<img alt='Caricamento...' border='0' src='../../images/ajax-loader_risp.gif'/>";
} 
}

function checkexist_function(divtochange,old) { 

if (http_request.readyState == 4)
 { 
	if (http_request.status == 200)
	 {
		 if (http_request.responseText=='ok')
		 {
			 document.getElementById(divtochange).innerHTML ="<span style='font-weight:bold;color:green'>DISPONIBILE</span>";
		 }
		 else
		 {
			 document.getElementById(divtochange).innerHTML ="<span style='font-weight:bold;color:red'>NON DISPONIBILE</span>";
		 }
	} 
	else
	 {
	alert('Si Sono verificati problemi nell\'elaborazione dei dati.');	
	} 
}else
{	
	document.getElementById(divtochange).innerHTML ="<img alt='Caricamento...' border='0' src='../../images/ajax-loader_risp.gif'/>";
} 
}

function associazione_offerte(divtochange,old) { 

if (http_request.readyState == 4)
 { 
	if (http_request.status == 200)
	 {
		document.getElementById(divtochange).innerHTML = http_request.responseText;
		//window.location.href = window.location.href;
	} 
	else
	 {
	alert('Si Sono verificati problemi nell\'elaborazione dei dati.');	
	} 
}else
{	
	document.getElementById(divtochange).innerHTML ="<img alt='Caricamento...' border='0' src='../../images/ajax-loader_risp.gif'/>";
} 
}

function caricalista(divtochange,old) { 

if (http_request.readyState == 4)
 { 
	if (http_request.status == 200)
	 {
		var list = eval(http_request.responseText);
		divtochange.options.length = 0;
		if (list)
	{
		for(i=1;i<list.length;i+=2)
		{
			if (old == list[i-1]){
				divtochange.options[divtochange.options.length]= new Option(list[i],list[i-1],true,true);
			}else{
				divtochange.options[divtochange.options.length]= new Option(list[i],list[i-1]);
			}
			
			
		}
	}
	} 
	else
	 {
	alert('Si Sono verificati problemi nell\'elaborazione dei dati.');	
	} 
}
}


function addvisit_function(link) { 

if (http_request.readyState == 4)
 { 
	if (http_request.status == 200)
	 {
		var test = window.open('http://'+link);
		if (!test) location.href ='http://'+link;
	} 
	else
	 {
	alert('Si Sono verificati problemi nell\'elaborazione dei dati.');	
	} 
}
}

function addDown_function(link) { 

if (http_request.readyState == 4)
 { 
	if (http_request.status == 200)
	 {
		var test = window.open(link);
		if (!test) location.href = link;
	} 
	else
	 {
	alert('Si Sono verificati problemi nell\'elaborazione dei dati.');	
	} 
}
}


   function get(obj,div) {
      var poststr = "tabella=" + encodeURI( obj.tabella.value ) +
                    "&id_tabella=" + encodeURI( obj.id_tabella.value ) +
                    "&id_mezzo=" + encodeURI( obj.id_mezzo.value ) +
					"&id=" + encodeURI( obj.id.value ) +
					"&posizione_attuale=" + encodeURI( obj.posizione_attuale.value ) +
					"&dir=" + encodeURI( obj.dir.value ) +
					"&t=" + encodeURI( obj.t.value );
     makePOSTRequest('sequenza_risp.asp', poststr,div,alertContents);
   }
   
   function get_stato(obj,div) {
      var poststr = "t=" + encodeURI( obj.t.value ) +
                    "&idn=" + encodeURI( obj.idn.value ) +
                    "&id=" + encodeURI( obj.id.value );
     makePOSTRequest('../change_stato.asp', poststr,div,alertContents);
   }
   
   function get_associato(obj,div) {
      var poststr = "idq=" + encodeURI( obj.idq.value ) +
					"&scadenza=" + encodeURI( obj.scadenza.value ) +
                    "&idc=" + encodeURI( obj.idc.value );
     makePOSTRequest('../associa_cq.asp', poststr,div,associazione_offerte);
   }
   
   function checkexist(obj,div) {
      var poststr = "username=" + encodeURI( obj.username.value ) +
                    "&tabella=" + encodeURI( obj.tabella.value );
     makePOSTRequest('../checkexist.asp', poststr,div,checkexist_function);
   }
   
    function addvisit(obj,link) {
      var poststr = "id_marchio=" + obj;
     makePOSTRequest('addvisit.asp', poststr,link,addvisit_function);
   }
   
   function addDown(obj,link) {
      var poststr = "id_corr_uo=" + obj;
     makePOSTRequest('addDown.asp', poststr,link,addDown_function);
   }
   
   function elimina_allegato(obj,div,news) {
      var poststr = "id_allegato=" + obj +
      				"&id_news=" + news;
     makePOSTRequest('elimina_allegato.asp', poststr,div,alertContents);
   }
   
   function getCityList(obj,old){
   var countryCode = obj.options[obj.selectedIndex].value;
	if(countryCode.length>0){
       var poststr = "countryCode=" + countryCode;
	   makePOSTRequest('getComuni.asp', poststr,document.forms.form1.elements.comuni,caricalista,old);
	}
	else
	{
		document.forms.form1.elements.comuni.options.length = 0;
		document.forms.form1.elements.comuni.options[document.forms.form1.elements.comuni.options.length]= new Option('----------','');
	}
   }