    function Engine(){}
    Engine.aziende = 1;
    Engine.utenze  = 2;
    
    function Ricerca(){}
    Ricerca.parcheggi  = "PARCHEGGI";
    Ricerca.benzinai    = "BENZINAI";
    Ricerca.ristoranti  = "RISTORANTI";
    Ricerca.alberghi    = "ALBERGHI";
    Ricerca.ospedali  = "OSPEDALI";
    Ricerca.farmacie  = "FARMACIE";
    Ricerca.metro    = "METRO";
    Ricerca.bus  = "STAZIONI BUS";
    Ricerca.treni  = "STAZIONI TRENO";
    
    function Opec() {
        this.classe; 
        this.fonte;
        this.rank;
        this.posx="null";
        this.posy="null";
        this.longitudine="null";
        this.latitudine="null";
        this.distanza="null";
        this.linea_aria="null";
        this.codiceOPEC;
        this.categorie;
        this.peso_commerciale;
        this.categoria;
        this.nome;
        this.comune;
        this.toponimo;
        this.indirizzo;
        this.cap;
        this.provincia;
    }
    
    function apriSchedaAzienda(opec,rag_soc,p) { 
    	 if(typeof(p) == "undefined") p = "a";
    	 var id_query = document.getElementById("id_query").innerHTML; 
    	 child = window.open('/schedaAzienda.php?opec='+opec+'&rag_soc='+encodeURI(rag_soc)+'&id='+id_query+'&p='+p,'scheda_azienda','width=780, height=455, location=no, menubar=no, status=no, toolbar=no, scrollbars=no, resizable=no, fullscreen=no');
	 if (child.opener == null) child.opener = self;   
    }
    
    function calcolaPercorso(){
		var da_via      = document.getElementById("calcolaP").da_via.value;
		var da_comune   = document.getElementById("calcolaP").da_comune.value;
		var a_via       = document.getElementById("calcolaP").a_via.value;
		var a_comune    = document.getElementById("calcolaP").a_comune.value;
        
		if(da_via == "" || da_via == "indirizzo") alert("Inserisci un indirizzo di partenza");
        else if(da_comune == "" || da_comune == "comune") alert("Inserisci un comune di partenza");
        else if(a_via == "") alert("Inserisci un indirizzo di arrivo");
        else if(a_comune == "") alert("Inserisci un comune di arrivo");
        else if(da_via == a_via && da_comune == a_comune) alert("Attenzione. Gli indirizzi di partenza e di arrivo coincidono.");
        else{            
            cmPHP('','','','calcola percorso','');
            pagina = "/getPercorso.php?da_via="+da_via+"&a_via="+a_via+"&da_comune="+da_comune+"&a_comune="+a_comune;
            document.getElementById("calcolaP").onclick = window.open(pagina, 'percorso', 'width=900,height=550,scrollbars=yes');
            document.getElementById("calcolaP").submit();
        }
    }
    
    function changeBackground(path, color) {
        document.body.style.backgroundImage                     ='url('+path+"/sfondo_fisso_"+color+".png"+')';
        document.getElementById("ricerca").style.backgroundImage='url('+path+"/sfondo_"+color+"_quad.png"+')';
        document.getElementById("rss").style.backgroundImage    ='url('+path+"/rss_"+color+"_quad.png"+')';
                
        switch(color) {
             case 'blu' :  document.getElementById('info').style.backgroundColor = '#3c7faf';
                           break;
             case 'rosso' : document.getElementById('info').style.backgroundColor = '#CE1313';
                            break;
             case 'verde' : document.getElementById('info').style.backgroundColor = '#4EBC27';
                            break;
             default: break;
        }        
    }
    
    function clickAdv(page, tipo){
	cmPHP('','','',tipo,page);
	adv = window.open(page,"adv");
	adv.focus();
    }
        
    function createMarkerOnSchedaAziende(point, icon, codiceOPEC, ragione_sociale) {    
      var marker = new UMarker(point, icon);
      marker.setTitle(ragione_sociale);
      UEvent.addListener(marker,"click", function(){ apriSchedaAzienda(codiceOPEC,ragione_sociale);} );
      return marker;
    }
    
    function getListing(p, cambioPag, lato){
       
        /*if(typeof(document.getElementById("cse-search-box")) != "undefined"){
        	var richiesta = document.getElementById("cse-search-box");
        }
        else{  
        	var richiesta = document.getElementById("richiesta"); 
        }*/
        
        var cosa      = document.getElementById('cosa').value;
        var dove      = "";
        var page      =  ( document.getElementById('page') ) ? document.getElementById('page').value : 1;
        var agisci    = "no";
        
        document.getElementById("p").value = p;
        
        
        if(typeof(cambioPag) == "undefined" || cambioPag == "undefined" || page == "" ) page = "1";
        else{  
        	document.getElementById('tieniLato').value = "yes"; 
        }
        
        document.getElementById('page').value = page;
        
        //effettuo i controlli sui valori dei campi; verifico se il campo 'cosa', obbligatorio, è stato o meno riempito  
        if(typeof(lato) != "undefined"){
        	 document.getElementById('tieniLato').value = "yes";
        	 agisci = "si";
        } 
        else if(cosa == "") {
            //se cambio tab da 'ricerca Web' a 'ricerca Cosa' o 'ricerca Chi'
            if(typeof(document.getElementById("cse-search-box").q.value) != ""){   
            	document.getElementById('cosa').value = document.getElementById('qC').value;
                document.getElementById('dove').value = document.getElementById('qD').value;            
                document.getElementById('usa_dove').checked=1;                
                agisci = "si";
                usa_dove=1;
            } 
            else{
                alert("Riempire il campo '"+chiCosa+"'"); 
            }
        } 
        else{ agisci = "si"; }
        
        //se i valori inseriti sono accettabili...   
        if(agisci == "si") {
            //controllo se è stata effettuata una ricerca di prossimita' (se è stato riempito il form 'vicino a')
            if(p == "a" && geocoder && document.getElementById('dove').value && (document.getElementById('prossimita').value || !document.getElementById('usa_dove').checked)) {
                var address = document.getElementById('prossimita').value;
                //se 'cerca nei comuni' non è abilitato, devo fare una ricerca nazionale centrata nel dove inserito o nel 'vicino a' inserito.
                //Chiamo Ubiest per recuperare posx e posy del 'vicino a' inserito
                geocoder.getPosxPosy(address, function(point) {     
                        if (!point) {
                            alert(address + " not found");
                        } else {
                            document.getElementById('posx').value = point.x;
                            document.getElementById('posy').value = point.y;    
                            document.getElementById("richiesta").method = "post";                        
                            document.getElementById("richiesta").action = "/getListing.php";
                            document.getElementById("richiesta").submit();
                        }
                    }
                );
            } 
            else {
            	if(document.getElementById("cse-search-box") != null && typeof(document.getElementById("cse-search-box")) != "undefined"){
            		if(p != 'w') document.getElementById("cse-search-box").method = "post";      
            		document.getElementById("cse-search-box").action = "/getListing.php";
                	document.getElementById("cse-search-box").submit();
            	}
                else{
                	document.getElementById("richiesta").method = "post";      
                	document.getElementById("richiesta").action = "/getListing.php";
                	document.getElementById("richiesta").submit();
                }
            }
        }
       
    }

    function geocodeInversa() {
        var post  = "posx=" + ((document.getElementById('posx')) ? document.getElementById('posx').value : "");
            post += "&posy="+ ((document.getElementById('posy')) ? document.getElementById('posy').value : "");
        
        //alert(post);
        var xmlhttp = newXMLHttpRequest();            
        if (xmlhttp!=null) {            
            xmlhttp.open("POST","/geocodeInversa.php",true);
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.setRequestHeader("Content-length",post.length);
            //xmlhttp.setRequestHeader("connection","close");
            //xmlhttp.setRequestHeader("Connection", "Keep-Alive");
            xmlhttp.send(post);
            xmlhttp.onreadystatechange = handleResponse(xmlhttp, manageGeocodeInversa, ResponseType.xml);
        } else {
            alert("Il tuo browser non supporta XMLHttpRequest")
        }
    }
    
    function manageGeocodeInversa(XMLResult) {
        if( XMLResult ) {
            var address = XMLResult.getElementsByTagName('ADDRESS')[0];
            if(address) {
                document.getElementById('prossimita').value = address.getAttribute("ADDR")+" "+((address.getAttribute("NUM")!=null) ? address.getAttribute("NUM") : "");        
                document.getElementById('dove').value       = ( address.getAttribute("AMD4") ) ? address.getAttribute("AMD4") : "";
                getListing('a',"undefined",'lato');
            } else {
                if( document.getElementById('posx') ) document.getElementById('posx').value="";
                if( document.getElementById('posy') ) document.getElementById('posy').value="";
            }
        }
    }
            
    function GoToUrl(urlPage){
    	if(urlPage.indexOf("http://") == -1) urlPage = "http://"+urlPage;
        remote=window.open (urlPage,"pagina", "resizable=1,menubar=1,toolbar=1,scrollbars=1,location=1");
        remote.focus();
    }
    
    function lato() {
    	document.getElementById('lato').value = document.getElementById('metri_lato').value;
    	getListing('a',"undefined",'lato');
    }
    
    function manageZoneListing(XMLResult, params){
        
        var row = XMLResult.getElementsByTagName('row');
        var icon = null;
        var _markers = null;
        //var current_marker =  mappa_scheda_azienda.getMarkers();
        
        switch(params) {
            case Ricerca.alberghi : icon = icon_hotel;
                                      _markers = markers_hotel;
                                      break;
                                      
            case Ricerca.benzinai : icon = icon_benzinaio;
                                     _markers = markers_benzinai;
                                     break;
                                     
            case Ricerca.parcheggi : icon = icon_parcheggio;
                                      _markers = markers_parcheggi;
                                      break;
            
            case Ricerca.ristoranti : icon = icon_ristorante;
                                      _markers = markers_ristoranti;
                                      break;
                                      
            case Ricerca.ospedali : icon = icon_ospedale;
                                      _markers = markers_ospedali;
                                      break;
                                      
            case Ricerca.farmacie : icon = icon_farmacia;
                                      _markers = markers_farmacie;
                                      break;
                                      
            case Ricerca.metro : icon = icon_metro;
                                      _markers = markers_metro;
                                      break;
                                      
            case Ricerca.bus : icon = icon_bus;
                                      _markers = markers_bus;
                                      break;
                                      
            case Ricerca.treni : icon = icon_treno;
                                      _markers = markers_treni;
                                      break;
                                      
                                      
            default : break;
        }
        
        var codiceOPEC      = '';
        var ragione_sociale = '';
        
        //mappa_scheda_azienda.clearOverlays();
        
        for(var i = 0 ; i < row.length ; i++ ) {
            codiceOPEC      = row[i].getElementsByTagName('codiceOPEC');            
            ragione_sociale = row[i].getElementsByTagName('ragione_sociale');
            posx = row[i].getElementsByTagName('posx');
            posy = row[i].getElementsByTagName('posy');
            
            if( posx[0].firstChild.nodeValue!="null" && posy[0].firstChild.nodeValue!="null") {
            
                var point  = new UPoint( posx[0].firstChild.nodeValue, posy[0].firstChild.nodeValue);
                var marker = createMarkerOnSchedaAziende(point, icon, codiceOPEC[0].firstChild.nodeValue, ragione_sociale[0].firstChild.nodeValue);
                                
                mappa_scheda_azienda.addOverlay(marker);
                _markers.push(marker);
                
                confini_mappa_scheda_azienda.extend(point);
            }
        }
        //alert(current_marker[0].getTitle());
        //mappa_scheda_azienda.addOverlay(current_marker[0]);
        mappa_scheda_azienda.setCenter(confini_mappa_scheda_azienda.getCenter(), mappa_scheda_azienda.getBoundsZoomLevel(confini_mappa_scheda_azienda));
    }
    
    function pageNext(page,p,x,y,codiceistat) {
    	
    	document.getElementById('codiceistat').value = codiceistat;
    	
        if(document.getElementById('page')) {
            document.getElementById('page').value = page;
        }
        
        if(p == "a") document.getElementById('lato').value = document.getElementById('metri_lato').value;
        
        if(document.getElementById('posx')) {
            document.getElementById('posx').value = x;
            document.getElementById('posy').value = y;
        }
                
        getListing(p,'cambio pagina');
    }
    
    function replaceAll(OldString,FindString,ReplaceString) {
        var SearchIndex = 0;
        var NewString = ""; 
        while (OldString.indexOf(FindString,SearchIndex) != -1)    {
            NewString += OldString.substring(SearchIndex,OldString.indexOf(FindString,SearchIndex));
            NewString += ReplaceString;
            SearchIndex = (OldString.indexOf(FindString,SearchIndex) + FindString.length);         
        }
        NewString += OldString.substring(SearchIndex,OldString.length);
        return NewString;
    }

    function showCat(tipo,showHide,n){
    	disp1 = 'none';
        disp2 = 'block';
    	if(showHide == "show"){
    		 disp1 = 'block';
    		 disp2 = 'none';
    	}
    	document.getElementById("altre"+tipo).style.display=disp2;
    	document.getElementById("nascondi"+tipo).style.display=disp1;
    	for(i=3;i<n;i++){
    		document.getElementById(tipo+"3"+i).style.display=disp1;
    	}
    }
    
    function showInfoWindow(codiceOPEC) {
        html="<table cellpadding=\"0\" cellspacing=\"5\" border=\"0\"><tr><td class=\"wPadding5 bbCode11 grey001\" valign=\"top\" colspan=\"3\"><span class=\"orange002\"><strong>"+opecs[codiceOPEC].nome+"</strong></span><br><strong>";
        html+="</td></tr></table>";
        document.getElementById('tabber').style.visibility="visible";
    }
    
    function tabGoogle(cosa, dove){
    	cosa = decodeURI(cosa);
    	dove = decodeURI(dove);
    	//alert("cosa = "+cosa+", dove = "+dove);
    	document.getElementById("richiesta").method = 'get';
        document.getElementById("richiesta").p.value = 'w';
        document.getElementById("richiesta").q.value = cosa + " " + dove;
    	document.getElementById("richiesta").qC.value = cosa;
    	document.getElementById("richiesta").qD.value = dove;
    	document.getElementById("richiesta").action = "getListing.php";
    	document.getElementById("richiesta").submit();
    }
    
    function trovaInZona(cosa, dove, posx, posy, icon) {
        
        if(cosa!=null) {
             var post = "cosa="      + encodeURI( cosa ) +
                         "&dove="     + encodeURI( dove ) +
                         "&page=1" +
                         "&posx="      + encodeURI( posx ) +
                         "&posy="      + encodeURI( posy ) +
                         "&lato=750"  +                         
                         "&usa_dove=1";
            
            var xmlhttp = newXMLHttpRequest();
            
            if (xmlhttp!=null) {        
                xmlhttp.open("POST","getXMListing.php",true);
                xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xmlhttp.send(post);
                xmlhttp.onreadystatechange = handleResponse(xmlhttp, manageZoneListing, ResponseType.xml, icon);
            } else {
                alert("Il tuo browser non supporta XMLHttpRequest")
            }
        }
    }
    
    function selectMarker(index) {
        if(markers && (index<markers.length) && (markers[index] != 'undefined') ) markers[index].select();
    }
    
    function deselectMarker(index) {
        if(markers && (index<markers.length) && (markers[index] != 'undefined') ) markers[index].deselect();
    }
    
        function checkData(nomeForm){
	var cosa = nomeForm.cosa.value;
	nomeForm.pagina.value = "";
	nomeForm.totPage.value = "";
	if(cosa == "es: ristorante" || cosa == ""){
		alert("Inserire un prodotto, un servizio o un operatore commerciale.");
		return false;
	}
	else if(cosa == "es: mario rossi" || cosa == ""){
		alert("Inserire il nome di un privato o di una azienda.");
		return false;
	}
	else return true;
}
