// JavaScript Document
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
var posx = 0;
var posy = 0;
function getMouse(e){
	var ev=(!e)?window.event:e;//IE:Moz
	if (ev.pageX)
	{//Moz
		posx=ev.pageX+window.pageXOffset;
		posy=ev.pageY+window.pageYOffset;
	}
	else if(ev.clientX)
	{//IE
		posx=ev.clientX+document.body.scrollLeft;
		posy=ev.clientY+document.body.scrollTop;
	}
	else{return false}//old browsers
}
document.onmousemove=getMouse;
function aprigiornale(pagina, width, height) {
		nocache = Math.floor(Math.random()*100);
		div_width = width - 300;
		div_height = height - 100;
		mtop = div_height / 2;
		mleft = div_width / 2;
		document.getElementById('giornale').style.width = div_width+'px';		
		document.getElementById('giornale').style.height = div_height+'px';				
		document.getElementById('giornale').style.marginTop = '-'+mtop+'px';		
		document.getElementById('giornale').style.marginLeft = '-'+mleft+'px';			
		document.getElementById('giornale').style.visibility = 'visible';
		completeAHAH.ahah('view_pdf.php?nocache='+nocache+'&file='+pagina+'&altezza='+div_height+'&larghezza='+div_width, 'giornale','','GET','');
}
function nascondi_giornale(){
		document.getElementById('giornale').style.visibility = 'hidden';
}

function email_valida(email) {

var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/; 
if (!email_reg_exp.test(email) || (email == "") || (email == "undefined"))
   	return false;
else
 	return true;
}

function recupera_password(email) {
	if( email == '' )
	{
		alert('Per registrarti o recuperare la password inserisci il tuo indirizzo di posta elettronica nel campo e-mail e clicca di nuovo su questo link');
	}
	else if( !email_valida(email) )
	{
		alert('Controllare l\'email inserita');
	}
	else if( confirm('Vuoi recuperare la password con l\'indirizzo di posta elettronica \''+email+'\'?') )
	{
		div_x = (screen.width / 2) + 46;
		document.getElementById('risultato_utenti').style.left = div_x+'px';
		document.getElementById('risultato_utenti').style.top = posy;		
		document.getElementById('risultato_utenti').style.visibility = 'visible';
		nocache = Math.floor(Math.random()*100);
		completeAHAH.ahah('recupera_password.php?nocache='+nocache+'&email='+email, 'risultato_utenti','','GET','');
	}
}

function checkChangePWD(){
	var pwdInput = document.getElementById('textNewPwd');
	var pwdInput2 = document.getElementById('textNewPwdR');
	if(pwdInput.value == ""){
		alert("Attenzione devi inserire una password di almeno 6 caratteri");
		pwdInput.focus();
		return false;
	}
		
	if(pwdInput.value.lenght < 6){
		alert("Attenzione devi inserire una password di almeno 6 caratteri");
		pwdInput.focus();
		return false;
	}
	
	if(pwdInput2.value == ""){
		alert("Attenzione devi inserire una password di almeno 6 caratteri");
		pwdInput2.focus();
		return false;
	}
		
	if(pwdInput2.value.lenght < 6){
		alert("Attenzione devi inserire una password di almeno 6 caratteri");
		pwdInput2.focus();
		return false;
	}
	document.getElementById('resultChange').style.display = 'inline';
	completeAHAH.ahah('aggiornaPassword.php?newPWDR='+document.getElementById('textNewPwdR').value+'&newPWD='+document.getElementById('textNewPwd').value, 'resultChange','','GET','');
//	document.getElementById('changePw').style.visibility = 'visible';
//	document.getElementById('insertNewPwd').style.visibility = 'hidden';
	document.getElementById('changePw').style.display = 'inline';
	document.getElementById('insertNewPwd').style.display = 'none';
	
}

function changePwd(){
//	document.getElementById('changePw').style.visibility = 'hidden';
//	document.getElementById('insertNewPwd').style.visibility = 'visible';
	document.getElementById('textNewPwd').value = '';
	document.getElementById('textNewPwdR').value = '';
	document.getElementById('changePw').style.display = 'none';
	document.getElementById('resultChange').style.display = 'none';
	document.getElementById('insertNewPwd').style.display = 'inline';
}

function nascondi_registrazione() {
	document.getElementById('risultato_utenti').style.visibility = 'hidden';	
}
function invia_registrazione()
{
	invio = document.getElementById('privacy').checked;
	if( invio )
	{
		 valore = document.getElementById('email').value;
 		 nocache = Math.floor(Math.random()*100);
		 completeAHAH.ahah('registrazione_utenti.php?nocache='+nocache+'&email='+valore+'&privacy=1', 'risultato_utenti','','GET','');
	}
	else
	{
		alert('Per continuare bisogna accettare l\'autorizzazione dei dati');
	}
}
function invia_articolo_mail(id)
{
	nomebrowser = BrowserDetect.browser.toLowerCase();
	document.getElementById('invia_articolo_email').style.visibility = 'visible';	
 	scrolltop = document.documentElement.scrollTop;
	scrollleft = document.documentElement.scrollLeft;
	leftPosition = posx + scrollleft;
	topPosition = posy + scrolltop;
	if( nomebrowser != 'explorer' )
	{
		leftPosition = posx - scrollleft;
		topPosition = posy - scrolltop;
	}
	document.getElementById('invia_articolo_email').style.top = topPosition+'px';	
	document.getElementById('invia_articolo_email').style.left = leftPosition+'px';	
	nocache = Math.floor(Math.random()*100);
	completeAHAH.ahah('invio_articolo_mail.php?nocache='+nocache+'&id='+id, 'invia_articolo_email','','GET','');
}
function nascondi_invia_articolo()
{
		document.getElementById('invia_articolo_email').style.visibility = 'hidden';	
}
function invia_articolo()
{
	id_articolo = document.getElementById('id_invio_articolo').value;
	nome_sender = document.getElementById('nome_sender_articolo').value;
	mail_sender = document.getElementById('mail_sender_articolo').value;
	nome_receiver = document.getElementById('nome_receiver_articolo').value;
	mail_receiver = document.getElementById('mail_receiver_articolo').value;
	if( !email_valida(mail_sender) )
		alert('Controllare il proprio indirizzo email inserito');
	else if( nome_sender == '' )
		alert('Inserire il proprio nome');
	else if( !email_valida(mail_receiver) )
		alert('Controllare l\'indirizzo email a cui si vuole inviare l\'articolo');	
	else if( nome_sender == '' )
		alert('Inserire nome dell\'amico');
	else
	{
		nocache = Math.floor(Math.random()*100);
		completeAHAH.ahah('invio_articolo_mail.php?nocache='+nocache+'&id='+id_articolo+'&nome_sender='+nome_sender+'&mail_sender='+mail_sender+'&nome_receiver='+nome_receiver+'&mail_receiver='+mail_receiver, 'invia_articolo_email','','GET','');
	}	
}
function FitPage() {
	pagina = document.getElementById('img_pagina_giornale');
	window.scrollTo(0,0);
	pagina.style.width = '972px';
}
function ReadPage(default_w,pagina_doppia) {
	pagina = document.getElementById('img_pagina_giornale');
	pagina.style.width = default_w+'px';
	if( pagina_doppia == 1)
		window.scrollTo((default_w/2),0);
	else
		window.scrollTo(0,0);

}
function zoomPage(percentuale, default_w) {
	pagina = document.getElementById('img_pagina_giornale');
	if (pagina.style.width == '' )
	{
		larghezza = parseInt(default_w);
	}
	else
	{
		pxstop = pagina.style.width.length - 2;
		larghezza = parseFloat(pagina.style.width.substring(0,pxstop));
	}
	new_larghezza = parseInt(larghezza + (larghezza / 100 * percentuale));
	pagina.style.width = new_larghezza+'px';
}
function RzoomPage(percentuale, default_w) {
	pagina = document.getElementById('img_pagina_giornale');
	if (pagina.style.width == '' )
		pagina.style.width = default_w;
	pxstop = pagina.style.width.length - 2;
	larghezza = parseFloat(pagina.style.width.substring(0,pxstop));
	new_larghezza = parseInt(larghezza - (larghezza / 100 * percentuale));
	if( new_larghezza < 972 ) new_larghezza = 972;
	pagina.style.width = new_larghezza+'px';
}
function controlla_registrazione() {
	nome = document.getElementById('nome').value;	
	cognome = document.getElementById('cognome').value;	
	sesso = document.getElementById('sesso').value;	
	data_nascita = document.getElementById('data_nascita').value;	
	citta_nascita = document.getElementById('citta_nascita').value;	
	nazione_nascita = document.getElementById('nazione_nascita').value;	
	username = document.getElementById('username').value;		
	email = document.getElementById('email').value;	
	password = document.getElementById('password').value;	
	conf_password = document.getElementById('conf_password').value;	
	privacy = document.getElementById('privacy').value;	

	if( nome == "" ) 
	{
		alert('Attenzione!\r\nIl campo nome \u00E8 obbligatorio per procedere alla registrazione');
		return false;
	}
	else if( cognome == "" ) 
	{
		alert('Attenzione!\r\nIl campo cognome \u00E8 obbligatorio per procedere alla registrazione');
		return false;
	}
	else if( sesso == "" ) 
	{
		alert('Attenzione!\r\nIl campo sesso \u00E8 obbligatorio per procedere alla registrazione');
		return false;
	}
	else if( username == "" ) 
	{
		alert('Attenzione!\r\nIl campo username \u00E8 obbligatorio per procedere alla registrazione');
		return false;
	}	
	else if( username.length < 8 ) 
	{
		alert('Attenzione!\r\nIl campo username deve essere almeno di 8 caratteri');
		return false;
	}	
	else if( email == "" ) 
	{
		alert('Attenzione!\r\nIl campo email \u00E8 obbligatorio per procedere alla registrazione');
		return false;
	}
	else if( !email_valida(email) )
	{
		alert('Attenzione!\r\nControllare l\'email inserita');
	}
	else if( password == "" ) 
	{
		alert('Attenzione!\r\nIl campo password \u00E8 obbligatorio per procedere alla registrazione');
		return false;
	}
	else if( conf_password == "" ) 
	{
		alert('Attenzione!\r\nIl campo conferma password \u00E8 obbligatorio per procedere alla registrazione');
		return false;
	}
	else if( password != conf_password ) 
	{
		alert('Attenzione!\r\La password inserita \u00E8 diversa dalla conferma');
		return false;
	}	
	else if( password.length < 6 )
	{
		alert('Attenzione!\r\La password deve essere almeno di 6 caratteri');
		return false;	
	}
	else if( (data_nascita =! "") && ( (data_nascita.length > 10) || (data_nascita.length < 9) ) ) 
	{
		alert('Attenzione!\r\nIl campo data di nascita \u00E8 obbligatorio per procedere alla registrazione');
		return false;
	}		
	else if( privacy != 1 )
	{
		alert('Attenzione!\r\Per procedere alla registrazione \u00E8 obbligatorio autorizzare il trattamento dei dati');
		return false;		
	}
	else
		return true;
}
function display_errore(errore)
{
	if( errore == "username_presente" )
	{
		alert("L\'username selezionato \u00E8 gi\u00E0 presente in archivio, la preghiamo di selezionarne un altro");
		document.getElementById('username').style.backgroundColor = '#FFDFE1';	
		document.getElementById('username').style.fontWeight = 'bold';	
		document.getElementById('username').style.color = '#DF000E';	
		document.getElementById('username').focus();	
	}
}
function attiva_ricerca(selezione)
{
	div_selezionato = 'box_'+selezione;
	valore = document.getElementById(selezione).checked;
	if( valore )
		document.getElementById(div_selezionato).style.display="block";
	else		
		document.getElementById(div_selezionato).style.display="none";
}
function effettua_ricerca()
{
	
	selezionato = 0;
	if( document.getElementById('check_parola').checked )
	{
		selezionato = 1;
		if( document.getElementById('ricerca_per_parola').value == "" )
		{
			alert('Inserire almeno una parola per effettuare una ricerca per parola');
			selezionato = 2;
		}
	}
	if( document.getElementById('check_autore').checked )
	{
		selezionato = 1;
		if( document.getElementById('ricerca_per_autore').value == "" )
		{
			alert('Inserire almeno una parola per effettuare una ricerca per autore');
			selezionato = 2;		
		}
	}
	if( document.getElementById('check_argomento').checked )
	{
		selezionato = 1;
		if( document.getElementById('ricerca_per_argomento').value == "" )
		{
			alert('Inserire almeno una parola per effettuare una ricerca per argomento');
			selezionato = 2;	
		}
	}	
	if( document.getElementById('check_data').checked )
	{
		selezionato = 1;
		if( (document.getElementById('data_dal').value == "") && (document.getElementById('data_al').value == "") )
		{
			alert('Inserire almeno una data per effettuare una ricerca per data');
			selezionato = 2;		
		}
	}		
	if( selezionato == 0 )
		alert('Selezionare il tipo di ricerca da effettuare');
	else if( selezionato == 1 )
		document.getElementById('ricerca_archivio').submit();
}
function play_video()
{
	document.getElementById('MediaPlayer').controls.play();
}
function fwd_video()
{
	document.getElementById('MediaPlayer').controls.FastForward();
}
function rwd_video()
{
	document.getElementById('MediaPlayer').controls.FastReverse();
}
function full_screen()
{
	document.getElementById('MediaPlayer').FullScreen = "true";
}
function naviga_video_sx()
{
	xdiv = document.getElementById('container_navigazione_video');
	sinistra = xdiv.scrollLeft - 81;
	xdiv.scrollLeft = sinistra;
}
function naviga_video_dx()
{
	xdiv = document.getElementById('container_navigazione_video');
	sinistra = xdiv.scrollLeft + 81;
	xdiv.scrollLeft = sinistra;
}
function ajax_video(video, titolo)
{
	nocache = Math.floor(Math.random()*100);
	document.getElementById('titolo_playing').innerHTML = titolo;
	completeAHAH.ahah('carica_video.php?nocache='+nocache+'&video='+video+'&titolo='+titolo, 'object_video','','GET','');
}
var filmato_play = 0;
function ajax_youtube(video, titolo, filmato_selezionato, start)
{
	if( filmato_play == 0 ) filmato_play = start;
	nocache = Math.floor(Math.random()*100);
	document.getElementById('prw_'+filmato_play).style.filter = 'alpha(opacity=100)';
	document.getElementById('prw_'+filmato_play).style.opacity = '1';	
	document.getElementById('prw_'+filmato_selezionato).style.filter = 'alpha(opacity=50)';
	document.getElementById('prw_'+filmato_selezionato).style.opacity = '0.5';		
	youtube_html = '<object width="290" height="240"><param name="movie" value="http://www.youtube.com/v/'+video+'&hl=it&fs=1&color1=0x006699&color2=0x00354F&showsearch=0&egm=0&showinfo=0&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+video+'&hl=it&fs=1&color1=0x006699&color2=0x00354F&showsearch=0&egm=0&showinfo=0&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="290" height="240"></embed></object>';
	document.getElementById('box_video').innerHTML = youtube_html;
	// completeAHAH.ahah('carica_video.php?nocache='+nocache+'&video='+video+'&titolo='+titolo, 'object_video','','GET','');
	if( filmato_play != filmato_selezionato) filmato_play = filmato_selezionato;
}
function ajax_mediaplayer(video, titolo, filmato_selezionato, start)
{
	if( filmato_play == 0 ) filmato_play = start;
	nocache = Math.floor(Math.random()*100);
	document.getElementById('prw_'+filmato_play).style.filter = 'alpha(opacity=100)';
	document.getElementById('prw_'+filmato_play).style.opacity = '1';	
	document.getElementById('prw_'+filmato_selezionato).style.filter = 'alpha(opacity=50)';
	document.getElementById('prw_'+filmato_selezionato).style.opacity = '0.5';		
	video_html = '<object id=MediaPlayer classid=CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6 standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject" width="290" height="240"><param name="url" value="video/serbatoio/'+video+'"><param name="AutoStart" value="false"><param name="ShowControls" value="1"><param name="uiMode" value="mini"><param name="PreviewMode" value="true" /><param name="wmode" value="transparent" /><embed type="application/x-mplayer2" wmode="transparent" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" width=290 height=240 src="video/serbatoio/'+video+'" autostart="0" loop="0" DisplaySize="4" showcontrols="1" showdisplay="1" showstatusbar="1" transparentatStart="true"></embed></object>';
	document.getElementById('box_video').innerHTML = video_html;
	// completeAHAH.ahah('carica_video.php?nocache='+nocache+'&video='+video+'&titolo='+titolo, 'object_video','','GET','');
	if( filmato_play != filmato_selezionato) filmato_play = filmato_selezionato;
}
i = 0;
var speed = 2;
var play = 1;
/*
function scroll_news() {
	i = i + speed;
	var div = document.getElementById("lista_notizie");
	div.scrollTop = i;
		if (i > div.scrollHeight) 
		{
			i = 0;
		}
		t1=setTimeout("scroll_news()",100)
}
*/
function seleziona_giorno()
{
	var anno = 0;
	var mese = 0;
	anno = document.getElementById('anno_pdf').value;
	mese = document.getElementById('mese_pdf').value;
	if( anno == 0 )
		alert('Selezionare l\'anno');
	else if( mese == 0 )
		alert('Selezionare il mese');
}
function seleziona_anno()
{
	nocache = Math.floor(Math.random()*100);
	anno = document.getElementById('anno_pdf').value;
	completeAHAH.ahah('mese_pdf.php?nocache='+nocache+'&anno='+anno, 'box_mese_pdf','','GET','');
}
function seleziona_mese()
{
	nocache = Math.floor(Math.random()*100);
	mese = document.getElementById('mese_pdf').value;
	anno = document.getElementById('anno_pdf').value;
	completeAHAH.ahah('giorno_pdf.php?nocache='+nocache+'&anno='+anno+'&mese='+mese, 'box_giorno_pdf','','GET','');
}
function apri_edizione()
{
	var giorno = 0;
	var mese = 0;
	var anno = 0;
	giorno = document.getElementById('giorno_pdf').value;
	mese = document.getElementById('mese_pdf').value;
	anno = document.getElementById('anno_pdf').value;
	if( anno == 0 )
		alert('Selezionare l\'anno');
	else if( mese == 0 )
		alert('Selezionare il mese');
	else if( giorno == 0 )
		alert('Selezionare il giorno');
	else
		window.location = 'opinionePDF.php?data='+anno+'-'+mese+'-'+giorno;
}
function Popup(apri) {
	var stile = "top=0, left=0, width=640, height=480, status=no, menubar=no, toolbar=no scrollbars=no resizable=no";
	window.open(apri, "popup_designer", stile);
}
function controlla_form_direttore()
{
	nominativo = document.getElementById('nominativo').value;
	email = document.getElementById('email').value;
	testo_lettera = document.getElementById('testo_lettera').value;	
	
	if( nominativo == '' )
	{
		alert("Attenzione, inserire il proprio nominativo!");
		return false;
	}
	else if( email == '' )
	{
		alert("Attenzione, inserire la propria email!");
		return false;	
	}
	else if( email == '' )
	{
		alert("Attenzione, inserire la propria email!");
		return false;	
	}
	else if( !email_valida(email) )
	{
		alert("Attenzione, controllare la propria email!");
		return false;			
	}
	else if( testo_lettera == '' )
	{
		alert("Attenzione, inserire un testo da inviare al Direttorel!");
		return false;			
	}
	else
		return true;
}
function open_banner(id_banner, url_banner)
{
	window.open ("open_banner.php?id="+id_banner+"&url="+url_banner, "banner","menubar=1,resizable=1,width=300,height=250"); 
}
function hide_promo()
{
	document.getElementById('popup_promo').innerHTML = '';
	document.getElementById('popup_promo').style.visibility = 'hidden';
	document.getElementById('overlay_popup').style.visibility = 'hidden';
}
