    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){
     	var richiesta = "";
        var chiCosa   = "";
        var cosa      = "";
        var agisci    = "no";
        var page = document.getElementById('page').value;
        
        if(p == "a"){
            richiesta = document.getElementById("richiesta");
            chiCosa = "COSA";
        } else if(p == "u"){
            richiesta = document.getElementById("richiesta_chi");
            chiCosa = "CHI";
        }
        
        if(typeof(cambioPag) == "undefined" || cambioPag == "undefined" || page == "" ) page = "1";
        else{  
        	richiesta.tieniLato.value = "yes"; 
        }
        
        cosa = richiesta.cosa.value;
        richiesta.page.value = page;
        if(typeof(lato) != "undefined"){
        	 richiesta.tieniLato.value = "yes";
        	 agisci = "si";
        }
        else 
        if(cosa == "") {
        	alert("Riempire il campo '"+chiCosa+"'"); 
        } else { 
        	agisci = "si"; 
        }
                    
        if(agisci == "si") {
        	if(p == "a" && geocoder && document.getElementById('dove').value && (document.getElementById('prossimita').value)) {
	        	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.
	                if(!document.getElementById('usa_dove').checked && address == "") address = document.getElementById('dove').value;
	                geocoder.getPosxPosy(address, function(point) {     
	                        if (!point) {
	                            alert(address + " not found");
	                        } else {
	                            document.getElementById('posx').value = point.x;
	                            document.getElementById('posy').value = point.y;
	                            richiesta.action = "/getListing_categorici.php";
	                            richiesta.submit();
	                        }
	                    }
	                );
	            } else {
	                richiesta.action = "/getListing_categorici.php";
	                richiesta.submit();
	            }
        }
       
    }

    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) {
    	document.getElementById('page').value = page;
        if(p == "a") document.getElementById('lato').value = document.getElementById('metri_lato').value;
        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){
    	document.getElementById("ricercaGoogle").q.value = cosa + " " + dove;
    	document.getElementById("ricercaGoogle").qC.value = cosa;
    	document.getElementById("ricercaGoogle").qD.value = dove;
    	document.getElementById("ricercaGoogle").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();
    }
    
    

