
	var google_num_ads;
    var narrowAdsCount = 0;
	var wideAdsCount   = 0;
	var rifrullo = 1;
		  
    // is called with a placemark if the reverse geocode request was successfull
    // sets the result div
    function goodresult(placemark) {
      /*var html = placemark.address + '<br />' + '<b>Country code:</b> ' + placemark.AddressDetails.Country.CountryNameCode;
      info.innerHTML = html;
      var postalcodenumber = rg.getPlacemarkProperty(placemark,"PostalCodeNumber");
      if (postalcodenumber != null) alert("Postal Code Number: " + postalcodenumber);
      else alert("Postal Code Number Unknown");*/
      /*var postalcodenumber = rg.getPlacemarkProperty(placemark,"PostalCodeNumber");      
      var localita        = rg.getPlacemarkProperty(placemark,"LocalityName");
      var address         = rg.getPlacemarkProperty(placemark,"ThoroughfareName");
            
      alert(address+" "+localita+" "+postalcodenumber);*/
      
      
      //alert(rg.getPlacemarkProperty(placemark,"DependentLocalityName"));
      //alert(rg.getPlacemarkProperty(placemark,"SubAdministrativeAreaName"));
      
      document.getElementById('page').value = 1;
      document.getElementById('dove').value = rg.getPlacemarkProperty(placemark,"LocalityName");
      document.getElementById('prossimita').value = rg.getPlacemarkProperty(placemark,"ThoroughfareName");
      document.getElementById('richiesta_aziende').submit();
	}

    // is called if the reverse geocode request was unsuccessfull
    function badresult() {
      document.getElementById('prossimita').value = "";
    }

    // get the input form lat and lng fields and issue a reverse geocode
    // request
    function reverse(){
      var point = new GLatLng(document.getElementById('lat').value, document.getElementById('lng').value);
      rg.reverseGeocode(point);
    }

    // handy method to fill in the lat and lng fields by clicking on the map.
    function handleClicks(marker, point){
        if( point ) {
              document.getElementById('lat').value = point.lat();
              document.getElementById('lng').value = point.lng();
        }
    }
    
    function createMarker(point, codiceOPEC) {
    
            var icon = new GIcon();
            icon.image = "http://css.iakke.com/default/images/marker.png";
            icon.iconAnchor = new GPoint(10, 30);
            icon.infoWindowAnchor = new GPoint(6, 24);
            icon.iconSize = new GSize(18, 30);
            icon.shadow = "http://css.iakke.com/default/images/marker_shadow.png";
            icon.shadowSize = new GSize(40, 30);

            var marker = new GMarker(point, {icon:icon});
            GEvent.addListener(marker, "click", function() {
                if(opecs[codiceOPEC]!=undefined) {
                	html ="<div>";
	                html+="<div class=\"map_ragionesociale\"><strong>"+opecs[codiceOPEC].nome+"</strong></div><br/>";
	                html+="<div class=\"map_address\">"+opecs[codiceOPEC].indirizzo+"</div>";
	                html+="</div>";
                  	marker.openInfoWindowHtml(html);
                }
                
            });

            markers[codiceOPEC] = marker;
        return marker;
    }
    
    function _selectMarker(codiceOPEC) {        
        if( div = document.getElementById("_" + codiceOPEC) ) {
            div.style.backgroundColor="#F5F5F5";
        }
    }
    
    function _deselectMarker(codiceOPEC) {
        if( div = document.getElementById("_" + codiceOPEC) ) {
            div.style.backgroundColor="#FFFFFF";
        }
    }
    
    
   function loadMarkers() {
   
        if(!document.getElementById("nresults")) return;
               
        var nresults = document.getElementById("nresults").firstChild.nodeValue;
        var nresults = ( nresults > 10 ) ? document.getElementById('rp').value : nresults;
        var notfound;
       
        map.clearOverlays();
        if(nresults>0) {        
            opecs.length  = 0;
            markers.length = 0;
            bounds      = new GLatLngBounds();
            var _marker = null;
            
            for(i=0 ; i < nresults ; i++ ){
            	if( document.getElementById("codiceOPEC_"+i) != null ) {
	                var opec 		 = new Opec();
	                opec.codiceOPEC  = document.getElementById("codiceOPEC_"+i).innerHTML;
	                opec.nome		 = document.getElementById("nome_"+i).innerHTML;                
	                opec.indirizzo   = document.getElementById("indirizzo_"+i+"").innerHTML;
	                opec.longitudine = (document.getElementById("longitudine_"+i+"").firstChild!=null) ? document.getElementById("longitudine_"+i+"").firstChild.nodeValue : 0;
	                opec.latitudine  = (document.getElementById("latitudine_"+i+"").firstChild!=null) ? document.getElementById("latitudine_"+i+"").firstChild.nodeValue : 0;
					
	                if(opec.longitudine!=0 && opec.latitudine!=0) {
		                opecs[opec.codiceOPEC] = opec;
	                
	                    var point  = new GLatLng(opec.latitudine, opec.longitudine);
	                    _marker    = createMarker(point, opec.codiceOPEC);
	                    map.addOverlay(_marker);
	                    bounds.extend(point);
	                }
	                else {
	                	var geoindirizzo 	 = str_replace('<br>', ', ', opec.indirizzo);
	                	geoindirizzo  		 = str_replace('<strong>', ', ', geoindirizzo);
	                	geoindirizzo  		 = str_replace('</strong>', ', ', geoindirizzo);
	                	geoindirizzo 		+= ', IT';
	                	
						geocoder.getLocations(geoindirizzo, function(response){
								if (!response || response.Status.code != 200) { } 
								else {
									place = response.Placemark[0];
									var point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
				                    _marker = createMarker(point, opec.codiceOPEC);
				                    map.addOverlay(_marker);
				                    bounds.extend(point);
								}
							});
	                }// end else
				}
            }
            //alert("bounds = "+map.getBoundsZoomLevel(bounds));
            if (!bounds.isEmpty()){ 
            	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
           	}
        }// END  if(nresults>0) 
        else {            
            map.setCenter(new GLatLng(42.200852, 13.311767), 5);
        }
    }    
        
      function resizeIFrame(id, count) {
      		var new_size = "0px";
      		if( count <= 0 ) {
				self.parent.document.getElementById(id).style.height = "0px";
				self.parent.document.getElementById(id).style.display = "none";				
			} else {
				var height_factor = ( ie4 ) ? 101 : 88;
				//var height_factor = 101;
				self.parent.document.getElementById(id).style.height = (25+(height_factor*count))+"px";
				//alert(self.parent.document.getElementById(id).style.height);
				new_size = (25+(height_factor*count))+"px";
			}
			
			return new_size;
      }
    
      function google_afs_request_done(google_ads) {
      
          /*
           * Verify that there are actually ads to display.
           */
           
          google_num_ads = google_ads.length;
          
          //alert('google_num_ads: '+google_num_ads);          

          var wideAds 	= "";   // wide ad unit html text
          var narrowAds = "";   // narrow ad unit html text
		  
		  //var narrowAdsCount = 0;
		  //var wideAdsCount   = 0;
		  
		  res	        = new Array();
		  
          for(i = 0; i < google_num_ads; i++) {
              if (google_ads[i].type=="text/wide") {
              
                  // render a wide ad
                  //wideAds+= '<div class="separate"></div>' +
                  
                  //          '<div class="narrow_container">' + 

                  //				'<div class="rag_soc_google" onclick="GoToUrl(\'' + google_ads[i].url + '\',\'_parent\');">' +
                  //        		'<span class="ad_line_dx">' + google_ads[i].line1 + '</span></div>' +
                          
                  //        		'<div class="ad_text">' + str_replace(',',', ',google_ads[i].line2 )+ '<br/>' + str_replace(',',', ',google_ads[i].line3) + '</div>' +
                            	
                  //          		'<div class="google_ad_url" onclick="GoToUrl(\'' + google_ads[i].url + '\',\'_parent \');return true;">' +
                  //         		'<span class="ad_url">' + google_ads[i].visible_url + '</span></div>' + 
                            
                  //          '</div>';
                            
                  //wideAdsCount++;
                  
              } else {
              
                  // render a narrow ad
                  narrowAds =	'<div class="separate"></div>' +
                  
                  				'<div class="narrow_container">' +
                  				 
	                  				'<div class="rag_soc_google" onclick="GoToUrl(\'' + google_ads[i].url + '\',\'_parent\');">' +
	                          		'<span class="ad_line_dx">' + google_ads[i].line1 + '</span></div>' +
	                          		
	                          		'<div class="ad_text">' + str_replace(',',', ',google_ads[i].line2 )+ '<br/>' + str_replace(',',', ',google_ads[i].line3) + '</div>' +
	                          		
	                          		'<div class="google_ad_url" onclick="GoToUrl(\'' + google_ads[i].url + '\',\'_parent \');return true;">' +
	                          		'<span class="ad_url">' + google_ads[i].visible_url + '</span></div>' + 
                          		
                          		'</div>';
                          		
                  narrowAdsCount++;
                  
              }
              
              res.push(narrowAds); 
              
          }
          
          //alert('narrowAdsCount: '+narrowAdsCount);
                   
          //if (narrowAdsCount==0 && rifrullo==1) { // <=2
          
			//googleParams = document.getElementById('noads');
			//var dove = (document.getElementById('dove') != null) ? document.getElementById('dove').value : '';
			
			//if(googleParams!=null) {
			//	googleParams.setAttribute("src", "http://it.iakke.com/noads.php?sito=verticali");
			//	if(ie4){
			//		googleParams.setAttribute("width","188");
			//		googleParams.setAttribute("height","540");
			//	} else {
			//		googleParams.style.width	= '188px';	
			//		googleParams.style.height	= '540px';		
			//	}			 
	        //    return;
	        //}
          //}
          
          //if (wideAds != "") {
          //    wideAds = '<span class="ad_header_top" onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">Ads by Google</span>&nbsp;&nbsp;' + wideAds;
          //}
          
          // Write HTML for wide and narrow ads to the proper div elements
          //if(document.getElementById("google_listing")!=null) {
          //	document.getElementById("google_listing").innerHTML = wideAds;
          //}
          	
		  //if (narrowAds != "") {
          //     res = '<div ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
          //                 '<span class="ad_header_dx">Ads by Google</span></div>' + res;
          //}
          
          //alert(narrowAdsCount);
          
          if ((document.getElementById("google_center_box")!=null)&&(narrowAdsCount=='10')) {	
          			    
              res1 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[3] + res[4] + res[5] + res[6];
              res2 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[0] + res[1] + res[2]; 
              res3 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[7] + res[8] + res[9];         
          	  
          	  document.getElementById("google_center_box").style.display   = 'block'; 
          	  document.getElementById("google_center_box2").style.display  = 'block'; 
          	  document.getElementById("google_center_box3").style.display  = 'block';
          	            	  
          	  document.getElementById("google_center_box").innerHTML  = res1; 
          	  document.getElementById("google_center_box2").innerHTML = res2;
          	  document.getElementById("google_center_box3").innerHTML = res3;
          	  
		  } else if((document.getElementById("google_center_box")!=null)&&(narrowAdsCount=='9')) {	
          			    
              res1 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[3] + res[4] + res[5] + res[6];
              res2 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[0] + res[1] + res[2]; 
          	  res3 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[7] + res[8];         
          	  
          	  document.getElementById("google_center_box").style.display   = 'block'; 
          	  document.getElementById("google_center_box2").style.display  = 'block'; 
          	  document.getElementById("google_center_box3").style.display  = 'block'; 
          	            	  
          	  document.getElementById("google_center_box").innerHTML  = res1; 
          	  document.getElementById("google_center_box2").innerHTML = res2;
          	  document.getElementById("google_center_box3").innerHTML = res3;  
          	  
		  } else if((document.getElementById("google_center_box")!=null)&&(narrowAdsCount=='8')) {	
          		    
              res1 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[3] + res[4] + res[5] + res[6];
              res2 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[0] + res[1] + res[2]; 
          	  res3 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[7];         
          	  
          	  document.getElementById("google_center_box").style.display   = 'block'; 
          	  document.getElementById("google_center_box2").style.display  = 'block'; 
          	  document.getElementById("google_center_box3").style.display  = 'block'; 
          	            	  
          	  document.getElementById("google_center_box").innerHTML  = res1; 
          	  document.getElementById("google_center_box2").innerHTML = res2;
          	  document.getElementById("google_center_box3").innerHTML = res3; 
          	            	  
		  } else if((document.getElementById("google_center_box")!=null)&&(narrowAdsCount=='7')) {	
          		    
              res1 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[3] + res[4] + res[5] + res[6];
              res2 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[0] + res[1] + res[2]; 
          	  
          	  document.getElementById("google_center_box").style.display   = 'block'; 
          	  document.getElementById("google_center_box2").style.display  = 'block'; 
          	            	            	  
          	  document.getElementById("google_center_box").innerHTML  = res1; 
          	  document.getElementById("google_center_box2").innerHTML = res2;
          	            	  
		  } else if((document.getElementById("google_center_box")!=null)&&(narrowAdsCount=='6')) {	
          		    
              res1 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[2] + res[3] + res[4] + res[5];
              res2 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[0] + res[1]; 
          	  
          	  document.getElementById("google_center_box").style.display   = 'block'; 
          	  document.getElementById("google_center_box2").style.display  = 'block'; 
          	 
          	  document.getElementById("google_center_box").innerHTML  = res1; 
          	  document.getElementById("google_center_box2").innerHTML = res2;
          	           	  
		  } else if((document.getElementById("google_center_box")!=null)&&(narrowAdsCount=='5')) {	
          		    
              res1 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[1] + res[2] + res[3] + res[4];
              res2 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[0]; 
          	  
          	  document.getElementById("google_center_box").style.display   = 'block'; 
          	  document.getElementById("google_center_box2").style.display  = 'block'; 
          	            	            	  
          	  document.getElementById("google_center_box").innerHTML  = res1; 
          	  document.getElementById("google_center_box2").innerHTML = res2;
          	           	  
		  } else if((document.getElementById("google_center_box")!=null)&&(narrowAdsCount=='4')) {	
          		    
              res1 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[0] + res[1] + res[2] + res[3];
                        	  
          	  document.getElementById("google_center_box2").style.display   = 'block';
          	            	            	            	  
          	  document.getElementById("google_center_box2").innerHTML  = res1; 
          	            	           	  
		  } else if((document.getElementById("google_center_box")!=null)&&(narrowAdsCount == '3')) {
		  
              res1 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[0] + res[1] + res[2];
          	  
          	  document.getElementById("google_center_box2").style.display  = 'block'; 
          	            	            	            	  
          	  document.getElementById("google_center_box2").innerHTML = res1; 
		  
		  }	else if((document.getElementById("google_center_box")!=null)&&(narrowAdsCount == '2')) {	
		  		    
              res1 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[0] + res[1];
          	  
          	  document.getElementById("google_center_box2").style.display  = 'block'; 
          	            	            	            	  
          	  document.getElementById("google_center_box2").innerHTML = res1;  
          	  
          }  else if((document.getElementById("google_center_box")!=null)&&(narrowAdsCount == '1')) {	
		  		    
              res1 = '<div class="ad_header_dx" ' + 'onclick="GoToUrl(\'https://www.google.com/adsense/support/bin/request.py?contact=afs_violation\',\'_self\');">' +
                     'Ads by Google&nbsp;</div>' + res[0];
          	  
          	  document.getElementById("google_center_box2").style.display  = 'block'; 
          	            	            	            	  
          	  document.getElementById("google_center_box2").innerHTML  = res1; 
          	  
          }                
 }    