// mostra riquadro di anteprima
function anteprima(textarea,w,h) {
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;	
	//var inf = document.form.testo.value;
	var inf = document.getElementById(textarea).value;
	inf = inf.split(/\n/).join("<br />");
	var win = window.open(", ", "popup", "width="+w+",height="+h+",top="+TopPosition+",left="+LeftPosition+",scrollbars=yes");
	win.document.open();	
	win.document.writeln("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
	win.document.writeln("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>");
	win.document.writeln("<head><title>Anteprima</title>");	
	win.document.writeln("<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />");
	win.document.writeln("<link href='inc/style.css' rel='stylesheet' type='text/css' />");
	win.document.writeln("</head><body>");
	win.document.writeln("<div style='text-align:right; margin-bottom:20px;'><a href='javascript:window.close()'>Chiudi</A></div>");
	win.document.writeln("" + inf + "");
	win.document.writeln('</body></html>');
	win.document.close();
}


function controllaData(frm,gg,mm,aa,nome){
if (document.forms[frm][aa].value.length == 1 || document.forms[frm][aa].value.length == 3 )
	{
		alert("Attenzione! " + nome + " errata. Inserire un valore corretto per l'anno")
		return false
	}
if (isNaN(document.forms[frm][aa].value) || isNaN(document.forms[frm][mm].value) || isNaN(document.forms[frm][gg].value)) {
	alert("Attenzione! I dati inseriti per il campo " + nome + " non risultano corretti.\nVerificare di non aver inserito lettere o segni di punteggiatura");
	return false
	}
if (document.forms[frm][aa].value.length == 2 ){
	if (document.forms[frm][aa].value < 30 ) {
		anno = "20" + document.forms[frm][aa].value 
		document.forms[frm][aa].value = anno
		}
	else {
		anno = "19" + document.forms[frm][aa].value 
		document.forms[frm][aa].value = anno
		}
	}
if (document.forms[frm][aa].value < 1800 && document.forms[frm][aa].value != "" ) {
	alert("Attenzione! L'anno del campo " + nome + " inserita è fuori dal range consentito")
	return false
	}
	
if (document.forms[frm][mm].value > 12 ) {
	alert("Attenzione! " + nome + " errata. Inserire un valore corretto per il mese")
	return false
	} 

if (document.forms[frm][mm].value == "2" ||  document.forms[frm][mm].value == "02" ) {
	if ((document.forms[frm][aa].value % 4 ) == 0 ){
		if (document.forms[frm][gg].value > 29 ) {
			alert("Attenzione! " + nome + " errata. Inserire un valore corretto per il giorno")
			return false
			} 
		}
	else if (document.forms[frm][gg].value > 28 ) {
			alert("Attenzione! " + nome + " errata. Inserire un valore corretto per il giorno")
			return false
			}
	}
else if (document.forms[frm][mm].value == "04" ||  document.forms[frm][mm].value == "4" || document.forms[frm][mm].value == "06" ||   document.forms[frm][mm].value == "6"  || document.forms[frm][mm].value == "09" ||   document.forms[frm][mm].value == "9"  ||  document.forms[frm][mm].value == "11") {
	if (document.forms[frm][gg].value > 30 ) {
		alert("Attenzione! " + nome +" errata. Inserire un valore corretto per il giorno")
		return false
		}
	}
else { 
	if (document.forms[frm][gg].value > 31 ) {
		alert("Attenzione! " + nome +" errata. Inserire un valore corretto per il giorno")
		return false
	}
} 

return true
}


//popup
var win = null;
function popup(mypage,w,h,scrollb){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	h = h + 10;
	w = w + 10;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scrollb+',resizable'
	winName =  'winName'+w+h;
	window.open(mypage,winName,settings);
}


//toggle
function toggle(obj,tipo) {
	var el = document.getElementById(obj);
	if (!tipo) {
		if ( el.style.display != 'none' ) {
			el.style.display = 'none';
		}	else {
			el.style.display = 'block';
		}
	} else if (tipo == 'mostra') { el.style.display = 'block';
	} else if (tipo == 'nascondi') { el.style.display = 'none';}
}

function cambiaClass(obj,classe) {
	var el = document.getElementById(obj);
		if (el.className == classe) {
				el.className = '';
		} else {
				el.className = classe;
		}
}

/*V*/
function controllaMail(EmailAddr){
 Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
 if (Filtro.test(EmailAddr))
    return true;
 else
    return false;
}

function check_login(lingua){
	var str="";
	if (document.getElementById('userid').value == "" && lingua=='it') str = str + "Inserire l\'user id\n";
	if (document.getElementById('userid').value == "" && lingua=='en') str = str + "Insert user id\n";
	if (document.getElementById('password').value == "" && lingua=='it') str = str + "Inserire la password\n";
	if (document.getElementById('password').value == "" && lingua=='en') str = str + "Insert password\n";
		if (str != ""){
		alert (str);
		return false
	}
	return true;
}

function check_privacy(lingua){
	var str="";
	if (document.getElementById('nome_news').value == "" && lingua=='it') str = str + "Inserire il nome newsletter\n";
	if (document.getElementById('nome_news').value == "" && lingua=='en') str = str + "Insert name\n";
	if (document.getElementById('cognome_news').value == "" && lingua=='it') str = str + "Inserire il cognome newsletter\n";
	if (document.getElementById('cognome_news').value == "" && lingua=='en') str = str + "Insert surname\n";
	if ((controllaMail(document.getElementById('email_news').value) == false || document.getElementById('email_news').value == "") && lingua=='it') str = str + "Inserire un indirizzo email valido\n";
	if ((controllaMail(document.getElementById('email_news').value) == false || document.getElementById('email_news').value == "") && lingua=='en') str = str + "Insert a valid email address\n";
	//if (document.getElementById('privacy_box').checked == false) str = str + "Per iscriversi e\' necessario acconsentire al trattamento dei dati personali\n";
	if (str != ""){
		alert (str);
		return false
	}
	return true;
}


//	Funzione che appende al campo hidden un determinato id
function opere_checked(hidden,id){
	var check_id = "";
	//var array = new Array();
	check_id = 'check_opera'+id;
	if(document.getElementById(check_id).checked == true) {
		document.getElementById(hidden).value = document.getElementById(hidden).value + id + '|';
	}
	else {
		var stringa = "";
		stringa = id + '|';
		document.getElementById(hidden).value = document.getElementById(hidden).value.replace(stringa,'');
	}
	//alert ('Hidden vale: '+ document.getElementById(hidden).value);
}

function mantieni_check(hidden){
	if(hidden != ''){
		var array_split = new Array();
		var check_id = "";
		array_split = hidden.split('|');
		// Non considero l'ultimo elemento dell'array altrimenti avrei anche uno spazio vuoto	
		for(i=0;i<=(array_split.length)-1;i++){
			check_id = 'check_opera'+array_split[i];
			if(document.getElementById(check_id) != null) document.getElementById(check_id).checked = true;	
		}
	}
	//else alert ('Hidden Vuoto!');
}

function fun_nav(){
	var new_link = '';
	new_link = location.href + "?&sel=" + document.getElementById('sel').value;
	self.location.href = new_link;
}

function link_prenota(pagina){
	var new_link = '';
	new_link = pagina + "?&sel=" + document.getElementById('sel').value;
	//alert ('NuovoLink: '+new_link);
	self.location.href = new_link;
}

function check_all(opere){	
	array_split = opere.split("|");
	//alert(array_split.length);
	for(i=0;i<=(array_split.length)-1;i++){	
		check_id = 'check_rifiuto_'+array_split[i];
		check_id_mot = 'divMr_'+array_split[i];
		if(document.getElementById(check_id) != null) {
			document.getElementById(check_id).checked = true;
			document.getElementById(check_id_mot).style.display = "block";
		}	
	}
}

function almeno_un_check(lingua){
	var str= "";
	
	if (document.getElementById('sel').value == "" && lingua== 'it') str = str + "Selezionare almeno un'opera\n";
	if (document.getElementById('sel').value == "" && lingua== 'en') str = str + "You must select at least an artwork\n";
	if (str != ""){
		alert (str);
		return false
	}
	return true;
}