function setRequestRead(queryStr)
{
    var xmlHttpReq = false;
    var self = this;

    // Mozilla/Safari
    if( window.XMLHttpRequest )
    {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if( window.ActiveXObject )
    {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var strURL = 'http://www.casashare.it/application/setReadRequest.ajax.php';
    
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function()
    {
        if( self.xmlHttpReq.readyState == 4 )
        {
        	 var retVal = self.xmlHttpReq.responseText;
        }
    }

    self.xmlHttpReq.send(queryStr);
}

function setSpyRead(queryStr)
{
    var xmlHttpReq = false;
    var self = this;

    // Mozilla/Safari
    if( window.XMLHttpRequest )
    {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if( window.ActiveXObject )
    {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var strURL = 'http://www.casashare.it/application/setReadRequest.ajax.php';
    
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function()
    {
        if( self.xmlHttpReq.readyState == 4 )
        {
        	 var retVal = self.xmlHttpReq.responseText;
        }
    }

    self.xmlHttpReq.send(queryStr);
}

function checkAndOpen(obj_id_imm)
{
    var xmlHttpReq = false;
    var self = this;

    // Mozilla/Safari
    if( window.XMLHttpRequest )
    {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if( window.ActiveXObject )
    {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var strURL = 'http://www.casashare.it/application/checkImm.ajax.php';
    var queryStr = 'id_imm=' + obj_id_imm.value;
    
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function()
    {
        if( self.xmlHttpReq.readyState == 4 )
        {
   	        //obj_image.src = "/images/LenteCS.png";
            if( self.xmlHttpReq.responseText == 'OK' )
            {
            	 var url2open = "http://www.casashare.it/application/det_vet.php?refresh=0&fromCShp=1&id_imm=" + obj_id_imm.value;
               //var winDett = window.open(url2open, 'DettaglioImmobile', 'width=1024, height=700, status=0, resizable=1, scrollbars=1');
               var winDett = window.open('', 'DettaglioImmobile', 'width=1024, height=700, status=0, resizable=1, scrollbars=1');
               winDett.document.write('<HTML><HEAD><META HTTP-EQUIV="REFRESH" CONTENT="0; URL=');
               winDett.document.write(url2open + '"></HEAD><BODY></BODY></HTML>');
               winDett.document.close();
            	 obj_id_imm.value = '';
            }
            else
            {
            	 alert("ATTENZIONE!! L'immobile con il codice specificato non e' presente!");
            	 obj_id_imm.focus();
            }
        }
    }

    self.xmlHttpReq.send(queryStr);
}

function searchImm()
{
   var obj_id_imm = document.getElementById("idCS");
   //var obj_image = document.getElementById("lenteCS");
   
   if( obj_id_imm.value != 0 )
   {
   	  //obj_image.src = "/images/loading2.gif";
   	  checkAndOpen(obj_id_imm);
   }
   else
   {
   	  //obj_image.src = "/images/LenteCS.png";
      alert("ATTENZIONE!! Devi specificare un codice immobile!");
      obj_id_imm.focus();
   }
}

function modificaImmobile(idImm, idDiv, fShow, idxDiv)
{
   var xmlHttpReq = false;
   var self = this;

   // Mozilla/Safari
   if( window.XMLHttpRequest )
   {
      self.xmlHttpReq = new XMLHttpRequest();
   }
   // IE
   else if( window.ActiveXObject )
   {
           self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
   }

   var strURL = 'http://www.casashare.it/application/modImm.ajax.php';
   var queryStr = 'id_imm=' + idImm + '&fShow=' + fShow + '&idxDiv=' + idxDiv;
    
   self.xmlHttpReq.open('POST', strURL, true);
   self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   self.xmlHttpReq.onreadystatechange = function()
   {
      if( self.xmlHttpReq.readyState == 4 )
      {
         var retVal = self.xmlHttpReq.responseText;
         if( retVal == "KO" )
            alert("ATTENZIONE!!! Errore nella gestione immobile. Contattare amministratore di sistema.");
         else
         	  document.getElementById(idDiv).innerHTML = retVal;
      }
   }
   self.xmlHttpReq.send(queryStr);
}

function salvaImmobile(idImm, idDiv)
{
	 var comune = document.getElementById('comuneImm').value;
	 var zona = document.getElementById('zonaImm').value;
	 var tipologia = document.getElementById('tipologiaImm').value;
	 var locali = document.getElementById('localiImm').value;
	 var mq = document.getElementById('mqImm').value;
	 var prezzo = document.getElementById('prezzoImm').value;
	 var prov = document.getElementById('provImm').value;

   var xmlHttpReq = false;
   var self = this;

   // Mozilla/Safari
   if( window.XMLHttpRequest )
   {
      self.xmlHttpReq = new XMLHttpRequest();
   }
   // IE
   else if( window.ActiveXObject )
   {
           self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
   }

   var strURL = 'http://www.casashare.it/application/saveImm.ajax.php';
   var queryStr = 'id_imm=' + idImm + '&comune=' + escape(comune) + '&zona=' + escape(zona) + '&tipologia=' + escape(tipologia) + '&locali=' + locali + '&mq=' + mq + '&prezzo=' + prezzo + '&prov=' + prov;
    
   self.xmlHttpReq.open('POST', strURL, true);
   self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   self.xmlHttpReq.onreadystatechange = function()
   {
      if( self.xmlHttpReq.readyState == 4 )
      {
         var retVal = self.xmlHttpReq.responseText;
         if( retVal == "KO" )
            alert("ATTENZIONE!!! Errore nell'aggiornamento immobile. Contattare amministratore di sistema.");
         else
            modificaImmobile(idImm, idDiv, 1, 0);
      }
   }
   self.xmlHttpReq.send(queryStr);
}
