//contaclick
function conta(id_attivita, tipologia)
{
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = 'http://admin.abc.sm/register_visit.php?id_attivita=' + id_attivita + '&tipologia=' + tipologia;
	window.document.body.appendChild (jsel);
}

function displayContattiTelefonici(idAttivita)
{
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = 'http://admin.abc.sm/scripts/count_click_view_telefono.php?id_attivita=' + idAttivita + '&id_portale=781';
	window.document.body.appendChild (jsel);
	
	if(!$('contattiTelefonici').visible())
	{
		$('imgContattiTelefonici').hide();	
		$('linkContattiTelefonici').hide();	
		Effect.BlindDown('contattiTelefonici');	
	}
}

function goToHome(lang)
{
	switch(lang)
	{
		case 'ita':
			location.href = '/';
		break;
		
		case 'eng':
			location.href = '/eng-index.php';
		break;
		
		case 'fra':
			location.href = '/fra-index.php';
		break;
		
		case 'deu':
			location.href = '/deu-index.php';
		break;
	}
}

function goToPage(lang)
{
	switch(lang)
	{
		case 'ita':
			document.cng_lang_to_ita.submit();
		break;
		
		case 'eng':
			document.cng_lang_to_eng.submit();
		break;
		
		case 'fra':
			document.cng_lang_to_fra.submit();
		break;
		
		case 'deu':
			document.cng_lang_to_deu.submit();
		break;
	}
}



function countredirect()
{
	if(currentsecond !=1 )
	{
		currentsecond -= 1;
		document.redirect.redirect2.value = currentsecond;
	}
	else
	{
		window.location = targetURL;
		return;
	}
	setTimeout("countredirect()", 1000);
}

function visualizza_testo_mappa()
{
	if(document.getElementById('mappa').style.display == 'none')
	{
		document.getElementById('testo').style.display = 'none';
		document.getElementById('mappa').style.display = 'block';
		document.getElementById('link_mappa').style.display = 'none';
		document.getElementById('link_mappa_ritorno').style.display = 'block';
	}
	else
	{
		document.getElementById('mappa').style.display = 'none';
		document.getElementById('testo').style.display = 'block';
		document.getElementById('link_mappa_ritorno').style.display = 'none';
		document.getElementById('link_mappa').style.display = 'block';
	}
}

function showAddress(address) 
{
	divMappa = document.getElementById('mappa');
	map = new GMap2(divMappa, { size: new GSize(larghezzaMappa, altezzaMappa) } );
	map.setMapType(G_NORMAL_MAP);
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(address, function(point) 
								{
									 if(!point) 
									 {
										 alert(address + " not found");
									 } 
									 else 
									 {
										 map.setCenter(point, 15);
										 map.addControl(new GLargeMapControl());
										 map.addControl(new GMapTypeControl());
										 map.addControl(new GScaleControl());
										 map.enableScrollWheelZoom();
										 bounds = map.getBounds();
									 }
								 }
						);
}

function showAddressCoordinates(lat, lng) 
{
	divMappa = document.getElementById('mappa');
	map = new GMap2(divMappa, { size: new GSize(larghezzaMappa, altezzaMappa) } );
	var point = new GLatLng(lat, lng);
	map.setCenter(point, 15);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GScaleControl());
	map.addControl(new GOverviewMapControl());
	bounds = map.getBounds();
}

function showAddressCostieraRomagnola(address)
{
	var divMappa = document.getElementById('mappa');
	var map = new GMap2(divMappa);
	var geocoder = new GClientGeocoder();
	switch(address)
	{
		case 'generico':
			var divMappa = document.getElementById('mappa');
			var map = new GMap2(divMappa);
			var point = new GLatLng('44.0767', '12.5821');
			map.setCenter(point, 10);
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.addControl(new GScaleControl());
			map.addControl(new GOverviewMapControl());
		break;
		
		case 'Ravenna':
			var divMappa = document.getElementById('mappa');
			var map = new GMap2(divMappa);
			var point = new GLatLng('44.4300', '12.3026');
			map.setCenter(point, 11);
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.addControl(new GScaleControl());
			map.addControl(new GOverviewMapControl());		
		break;
		default:
			geocoder.getLatLng(address, function(point) 
									{
										 if(!point) 
										 {
											 alert(address + " not found");
										 } 
										 else 
										 {
											 map.setCenter(point, 13);
											 map.addControl(new GLargeMapControl());
											 map.addControl(new GMapTypeControl());
											 map.addControl(new GScaleControl());
											 map.addControl(new GOverviewMapControl());
										 }
									 }
								);
	}
}

function showPoint(latitudine, longitudine, icona, testo, infoOpen) 
{
	var point = new GLatLng(latitudine, longitudine);
	
	markerOptions = { icon: icona };
		
	var marker = new GMarker(point, markerOptions);
	
	if(infoOpen)
	{
		setTimeout(function() {map.addOverlay(marker); marker.openInfoWindowHtml(testo); bounds.extend(point);}, 1000);
	}
	else
	{
		setTimeout(function() {map.addOverlay(marker); bounds.extend(point);}, 1000);
		GEvent.addListener(marker, "click", function() 
										{
											marker.openInfoWindowHtml(testo);
										});
	}
}

function showPointAddress(indirizzo, icona, testo, infoOpen) 
{	
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(indirizzo, function(point) 
								  {
									if(!point) 
									{
										alert(indirizzo + " not found");
									} 
									else
									{
										markerOptions = { icon: icona };
			
										var marker = new GMarker(point, markerOptions);
										bounds.extend(point);
										if(infoOpen)
										{
											setTimeout(function() {map.addOverlay(marker); marker.openInfoWindowHtml(testo); bounds.extend(point);}, 1000);
										}
										else
										{
											setTimeout(function() {map.addOverlay(marker); bounds.extend(point);}, 1000);
											GEvent.addListener(marker, "click", function() 
																			{
																				marker.openInfoWindowHtml(testo);
																			});
										}
									}
								  }
						);
}

function showStructure(latitudine, longitudine, icona)
{
	divMappa = document.getElementById('testo_mappa');
	var point = new GLatLng(latitudine, longitudine);
	
	map = new GMap2(divMappa, { size: new GSize(larghezzaMappa, altezzaMappa) } );
	map.setMapType(G_NORMAL_MAP);
	map.setCenter(point, 15);
	map.addControl(new GSmallZoomControl());
	map.addControl(new GMapTypeControl());
	
	markerOptions = { icon: icona };
	var marker = new GMarker(point, markerOptions);
	map.addOverlay(marker);								 
}

function showStructureAddress(indirizzo, icona)
{
	divMappa = document.getElementById('testo_mappa');
	var geocoder = new GClientGeocoder();
	
	map = new GMap2(divMappa, { size: new GSize(larghezzaMappa, altezzaMappa) } );
	map.setMapType(G_NORMAL_MAP);
	geocoder.getLatLng(indirizzo, function(point) 
								  {
									if(!point) 
									{
										alert(indirizzo + " not found");
									} 
									else 
									{
										map.setCenter(point, 15);
										map.addControl(new GSmallZoomControl());
										map.addControl(new GMapTypeControl());
													
										markerOptions = { icon: icona };
										var marker = new GMarker(point, markerOptions);
										map.addOverlay(marker);
									}
								  }
						);
}

function showStructureLarge(latitudine, longitudine, icona)
{
	document.getElementById('ingrandisci').style.display = 'none';
	document.getElementById('calcola_percorso').style.display = 'none';
	document.getElementById('chiudi').style.display = 'block';
	document.getElementById('mappa_grande').style.display = 'block';
	divMappaGrande = document.getElementById('mappa_ingrandita');
	var point = new GLatLng(latitudine, longitudine);
	
	mappaGrande = new GMap2(divMappaGrande, { size: new GSize(larghezzaMappaGrande, altezzaMappaGrande) } );
	mappaGrande.setCenter(point, 13);
	mappaGrande.addControl(new GLargeMapControl());									
	mappaGrande.addControl(new GMapTypeControl());
	mappaGrande.addControl(new GScaleControl());
	mappaGrande.addControl(new GOverviewMapControl());
	
	markerOptions = { icon: icona };
	var marker = new GMarker(point, markerOptions);
	mappaGrande.addOverlay(marker);								 
}

function showStructureAddressLarge(indirizzo, icona)
{
	document.getElementById('ingrandisci').style.display = 'none';
	document.getElementById('calcola_percorso').style.display = 'none';
	document.getElementById('chiudi').style.display = 'block';
	document.getElementById('mappa_grande').style.display = 'block';
	divMappaGrande = document.getElementById('mappa_ingrandita');
	var geocoder = new GClientGeocoder();
	
	mappaGrande = new GMap2(divMappaGrande, { size: new GSize(larghezzaMappaGrande, altezzaMappaGrande) } );
	geocoder.getLatLng(indirizzo, function(point) 
								  {
									if(!point) 
									{
										alert(indirizzo + " not found");
									} 
									else 
									{
										mappaGrande.setCenter(point, 13);
										mappaGrande.addControl(new GLargeMapControl());									
										mappaGrande.addControl(new GMapTypeControl());
										mappaGrande.addControl(new GScaleControl());
										mappaGrande.addControl(new GOverviewMapControl());
													
										markerOptions = { icon: icona };
										var marker = new GMarker(point, markerOptions);
										mappaGrande.addOverlay(marker);
									}
								  }
						);
}

function erase_value(element)
{
	element.value= "";
}

function closeMappaPercorso()
{
	document.getElementById('map').innerHTML = '';
	document.getElementById('directions').innerHTML = '';
	document.getElementById('mappa_percorso').style.display = 'none';
	document.getElementById('mappa_grande').style.display = 'none';
	document.getElementById('ingrandisci').style.display = 'block';
	document.getElementById('calcola_percorso').style.display = 'block';
	document.getElementById('chiudi').style.display = 'none';
}

function showPercorso(fromAddress, toAddress)
{
	if (GBrowserIsCompatible())
	{
		document.getElementById('ingrandisci').style.display = 'none';
		document.getElementById('calcola_percorso').style.display = 'none';
		document.getElementById('chiudi').style.display = 'block';
		document.getElementById('mappa_percorso').style.display = 'block';
		mapPercorso = new GMap2(document.getElementById("map"));
		gdir = new GDirections(mapPercorso, document.getElementById("directions"));
		
		//GEvent.addListener(gdir, "error", handleErrors);
		GEvent.addListener(gdir, "error", reloadPercorso);	
	
		setDirections(fromAddress, toAddress, localeVariable);
  	}
}

function showPopupPercorso(id, toAddress)
{
	var fromAddress = document.getElementById('percorso').value;
	popupStampa = window.open("pagina_stampa.php?fromAddress=" + encodeURIComponent(fromAddress) + "&toAddress=" + encodeURIComponent(toAddress) + "&id_attivita=" + id, "pagina_stampa", "width=955,height=720,scrollbars");
	popupStampa.focus;
}

function setDirections(fromAddress, toAddress, locale) 
{
  gdir.load("from: " + fromAddress + " to: " + toAddress,
			{ "locale": locale });
}

function reloadPercorso()
{
	if(latitudinePercorso != '' && longitudinePercorso != '')
	{
		var coordinates = new GLatLng(latitudinePercorso, longitudinePercorso);
		
		var geocoder2 = new GClientGeocoder();
		geocoder2.getLocations(coordinates, function(addresses) 
							  {
								if(addresses.Status.code != 200) 
								{
									handleErrors();	
								} 
								else 
								{
									var myAddress = addresses.Placemark[0].address;
									GEvent.addListener(gdir, "error", handleErrors);	
	
									setDirections(document.getElementById('percorso').value, myAddress, localeVariable);
								}
							  }
							);
	}
	else
	{
		handleErrors();	
	}
}

function handleErrors()
{
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	 alert("La posizione geografica corrispondente non è stata trovata!. Ciò può essere dovuto dal fatto che l'indirizzo è relativamente nuovo, o può essere errato.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	 alert("Una richiesta Geocoding non ha avuto successo, tuttavia il motivo esatto per il guasto non è conosciuto.\n Error code: " + gdir.getStatus().code);
   
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	 alert("Il parametro della query HTTP non ha avuto valore. Questo significa che un indirizzo vuoto è stato specificato come input.\n Error code: " + gdir.getStatus().code);

//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	 
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	 alert("La chiave fornita non è valida o non abbina il dominio per cui sia stata data.\n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("Una richiesta di calcolo del percorso non ha potuto essere analizzata con successo.\n Error code: " + gdir.getStatus().code);
	
   else alert("Errore sconosciuto.");
}

function addslashes(str) {
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
return str;
}

function centradiv(nome_div,space) { //Centers a div element on the page
		
		var divobj = document.getElementById(''+ nome_div +''); 
		
		if(space=='') var space = 60;
		
		var ie=document.all && !window.opera
		var dom=document.getElementById
		
		var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body 
		
		var scroll_top=(ie)?  standardbody.scrollTop : window.pageYOffset
		var scroll_left=(ie)?  standardbody.scrollLeft : window.pageXOffset
		var docwidth=(ie)?  standardbody.clientWidth : window.innerWidth
		var docwidth2 = document.body.clientWidth;
		var docheight=(ie)?  standardbody.clientHeight: window.innerHeight
		var docheightcomplete=( standardbody.offsetHeight> standardbody.scrollHeight)?  standardbody.offsetHeight :  standardbody.scrollHeight //Full scroll height of document
		var objwidth=divobj.offsetWidth //width of div element
		var objheight=divobj.offsetHeight //height of div element
		var topposition=(docheight>objheight)? scroll_top+docheight/2-objheight/2+"px" : scroll_top+10+"px" //Vertical position of div element: Either centered, or if element height larger than viewpoint height, 10px from top of viewpoint
		
		divobj.style.display = 'block'; 
		divobj.style.width = (docwidth2 - space) + 'px'; //100px or 100%
		divobj.style.height = ((docheight - scroll_top) - space) + 'px'; //100px or 100% 
		divobj.style.top = (scroll_top + (space/2)) + 'px'; //100px or 100%  
		divobj.style.left = ( (docwidth/2) - (divobj.offsetWidth/2) ) +"px"; //100px or 100%  
		
	
	}
	
	function maximize_div(nome_div) { //Centers a div element on the page
		
		var divobj3 = document.getElementById(''+ nome_div +''); 
		var ie=document.all && !window.opera
		var dom=document.getElementById
		var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body 
		
		var scroll_top=(ie)?  standardbody.scrollTop : window.pageYOffset
		var scroll_left=(ie)?  standardbody.scrollLeft : window.pageXOffset
		var docwidth=(ie)?  standardbody.clientWidth : window.innerWidth
		var docwidth2 = document.body.clientWidth;
		var docheight=(ie)?  standardbody.clientHeight: window.innerHeight
		var docheightcomplete=( standardbody.offsetHeight> standardbody.scrollHeight)?  standardbody.offsetHeight :  standardbody.scrollHeight //Full scroll height of document
		var objwidth=divobj3.offsetWidth //width of div element
		var objheight=divobj3.offsetHeight //height of div element
		var topposition=(docheight>objheight)? scroll_top+docheight/2-objheight/2+"px" : scroll_top+10+"px" //Vertical position of div element: Either centered, or if element height larger than viewpoint height, 10px from top of viewpoint
		
		divobj3.style.display = 'block'; 
		//
		if(ie){
			divobj3.style.width = docwidth + 'px'; //100px or 100%
		}else{
			divobj3.style.width = '100%'; //100px or 100%	
		}
		
		divobj3.style.height = docheight + 'px'; //100px or 100% 
		divobj3.style.top = scroll_top + 'px'; //100px or 100%  
		divobj3.style.left = '0px'; //100px or 100%    
		
		//alert(divobj3.style.top);
 	}
	
	function opacizza_site(){

		var b = document.getElementById("opacity");
		var opacity = 60;
		var opaque = (opacity / 100);
		var ie = document.all && !window.opera
		var dom = document.getElementById
		var scroll_top = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;

		if(b){
			maximize_div("opacity");
			b.style.top =  scroll_top +'px';
			b.style.display='block'; 
			b.style.opacity=opaque;                      
			b.style.MozOpacity=opaque;                   
			b.style.filter='alpha(opacity='+opacity+')';  
		}
	}
	
	function restore_opacita_site(){

		var b = document.getElementById("opacity");
		var opacity = 0;
		var opaque = (opacity / 100);
		if(b){
			b.style.display='none'; 
			b.style.opacity=opaque;                      
			b.style.MozOpacity=opaque;                   
			b.style.filter='alpha(opacity='+opacity+')'; 
			if(document.getElementById("preview_content")) document.getElementById("preview_content").innerHTML = '';
			if(document.getElementById("div_preview_content")) document.getElementById("div_preview_content").style.display='none';
			if(document.getElementById("video_youtube")) document.getElementById("video_youtube").style.display='block';
		}
	}
	
	function preview_video(codice,descrizione){	
		
		var ie = document.all && !window.opera
		var dom = document.getElementById
		var scroll_top = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		
		opacizza_site();
		var div_prev = document.getElementById("div_preview_content");
		

		maximize_div("div_preview_content");
		div_prev.style.display='block';
		div_prev.style.top =  (scroll_top + 100) +'px';
		if(document.getElementById("preview_title")) document.getElementById("preview_title").innerHTML = '<b>'+descrizione+'</b>';
		insert_youtubevideo('preview_content',codice,'410','344');
		if(document.getElementById("video_youtube")) document.getElementById("video_youtube").style.display='none';
		
	}
	
	function insert_youtubevideo(nome_div,codice,larg,alt){
		
		if(alt=='') alt = '344';
		if(larg=='') larg = '410';
		
		var code_html = '<object width="'+larg+'" height="'+alt+'" style="z-index:2000">';
		code_html += '<param name="movie" value="http://www.youtube.com/v/'+codice+'&hl=it&fs=1&rel=0&autoplay=1"></param>';
		code_html += '<param name="allowFullScreen" value="true"></param>';
		code_html += '<embed src="http://www.youtube.com/v/'+codice+'&hl=it&fs=1&rel=0&autoplay=1&fmt=6" style="z-index:2000" type="application/x-shockwave-flash" allowfullscreen="true" width="'+larg+'" height="'+alt+'"></embed>';
		if(document.getElementById(''+nome_div+'')) document.getElementById(''+nome_div+'').innerHTML = '<span>'+code_html+'</span>';
	}
	
	function hideshow(id) { //Centers a div element on the page 
		
		var obj = document.getElementById(''+ id +''); 
		
		if ( document.all ) { // se ie
				
				if(obj.style.display=='none'){
					obj.style.display="block";
				}else{
					obj.style.display="none";
				}
		
			
		}else{ // se firefox
			
				if(obj.style.display=='none'){
					obj.style.display="table-row";
				}else{
					obj.style.display="none";
				}
			
		}
	
	}
	
function caricaPrevisioniMeteo(lang, frazione)
{
	var service = "web_services.php";	
	
	new Ajax.Request(service, {
								async: true,
								method: 'get',
								parameters: 'lang=' + lang + '&frazione=' + frazione,
								onSuccess: function(transport) 
										   {
											   eval(transport.responseText);
										   }
							  });
}

function checkform()
{
	var d = document.iscr_newsletter; 
	var error = '';

	if(d.email.value == '') 
		error += 'Inserire l\'indirizzo email' + "\r\n";
	if(d.azione.value == 'insert_user')
	{
		if(!d.privacy.checked) 
			error += 'Privacy Policy';
	}
	else
	{
		if(d.codice.value == '') 
			error += 'Inserire il codice di controllo' + "\r\n";	
	}
	
	if(error != '')
	{
		alert(error);
		return(false);
	}
	else
	{
		send_data(d, 'http://www.abc.sm/iscr_newsletter_portali.php')
	}
}

function send_data(form, url)
{
	var qstring = setquerystring(form);
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = url + '?' + qstring;
	window.document.body.appendChild (jsel);
} 

function setquerystring(form) 
{
   var querystring = '';
   var numberElement = form.elements.length;

	for(var i = 0; i < numberElement; i++)
	{
		if(i < numberElement - 1)
		{ 
		   querystring += form.elements[i].name + "=" + encodeURIComponent(form.elements[i].value) + "&";
		}
		else
		{
			querystring += form.elements[i].name + "=" + encodeURIComponent(form.elements[i].value);
		}
	}

	return querystring;
}