// JavaScript Document

function popup(mylink){
	if (! window.focus)
		return true;
	var href;
	
	if (typeof(mylink) == 'string')
   		href=mylink;
	else
   		href=mylink.href;
		
	window.open(href, '', 'scrollbars=yes');
	return false;
}

function printModule(){
	if(document.getElementById('privacy').checked){
		print();
	} else {
		alert('Acconsentire al trattamento dei dati prima di stampare.');
	}
}

var current="TEMPO SCADUTO!!"
var montharray=new  Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function countdown_to_festivart(yr,m,d){
theyear=yr;themonth=m;theday=d
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec
futurestring=montharray[m-1]+" "+d+", "+yr
dd=Date.parse(futurestring)-Date.parse(todaystring)
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
if(dday<=0&&dhour<=0&&dmin<=0&&dsec<=1){
document.getElementById('countdown').innerHTML=""
return
}
else
document.getElementById('countdown').innerHTML="... mancano ancora <strong>" + (Number(dday) > 0 ? (" " +dday + " giorni") : "") 
											+ (Number(dhour) > 0 ? (" " + dhour + " ore") : "")
											+ (Number(dmin) > 0 ? (" " + dmin + " minuti") : "")
											+ (Number(dsec) > 0 ? ((Number(dmin) > 0 ? " e " : " ") + dsec + " secondi!") : "!</strong>");
setTimeout("countdown_to_festivart(theyear,themonth,theday)",1000)
}

function countdown(yr,m,d){
theyear=yr;themonth=m;theday=d
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec
futurestring=montharray[m-1]+" "+d+", "+yr
dd=Date.parse(futurestring)-Date.parse(todaystring)
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
if(dday<=0&&dhour<=0&&dmin<=0&&dsec<=1){
document.getElementById('conta').innerHTML=current
return
}
else
document.getElementById('conta').innerHTML="quindi affrettati: hai ancora<br /><strong>" + (Number(dday) > 0 ? (" " +dday + " giorni") : "") 
											+ (Number(dhour) > 0 ? (" " + dhour + " ore") : "")
											+ (Number(dmin) > 0 ? (" " + dmin + " minuti") : "")
											+ (Number(dsec) > 0 ? ((Number(dmin) > 0 ? " e " : " ") + dsec + " secondi!") : "!</strong>");
setTimeout("countdown(theyear,themonth,theday)",1000)
}


	function isANumber(string) {
	    if (string.length == 0)
        	return false;
    	for (var i=0;i < string.length;i++)
	        if ((string.substring(i,i+1) < '0') || (string.substring(i,i+1) > '9'))
        	    return false;
    
    	return true;
	}

	function verifyEmail(){
		var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	    
		if (document.forms[0].Referente_EMail.value.search(emailRegEx) == -1) {
        	  return true;
    	 }
	     else if (document.forms[0].Referente_EMail.value != document.forms[0].Referente_EMail2.value) {
        	  return true;
    	 }
        return false;
     }
	 
	 function verifySpettacolARTPartecipanti(){
		 return false;
		 var regex = /^\s*[A-Zòàèéì]+,\s*[A-Zòàèéì]+,\s*\d{2}\s*\(\s*[0-9]*\s*[A-Z]+\s*\)$|^\s*[A-Zòàèéì]+,\s*[A-Zòàèéì]+,\s*\d{2}\s*$/i;
		 var content = document.forms[0].SpettacolART_Partecipanti.value;
		 content.replace(/(\x0a\x0d|\x0d\x0a|\r\n|\r)/g,"\n");
		 var e = content.split("\n");
		 var i;
		 alert(e);
		 for (i=0;i<e.length;i++){
			 alert(e[i]);
			if(e[i].search(regex) == -1){
				return true;
			}
		 }
		 
		 return false;
	 }
	 
	 function verifyCreativARTPartecipanti(){
		 return false;
		 var regex = /^\s*[A-Zòàèéì]+,\s*[A-Zòàèéì]+,\s*\d{2}\s*\(\s*[0-9]*\s*[A-Z]+\s*\)$|^\s*[A-Zòàèéì]+,\s*[A-Zòàèéì]+,\s*\d{2}\s*$/i;
		 var content = document.forms[0].CreativART_Partecipanti.value;
		 content.replace(/(\x0a\x0d|\x0d\x0a)/g,"\n");
		 var e = content.split("\n");
		 var i;
		 
		 for (i=0;i<e.length;i++){
			 alert(e[i]);
			if(e[i].search(regex) == -1){
				return true;
			}
		 }
		 
		 /*for each(var e in content.split("\n")){
			if(e.search(regex) == -1){
				return true;
			}
		 }*/
		 
		 return false;
	 }
	 
	 function verifySpettType(){
		 var counter = 0;
		 
		 if(document.forms[0].SpettacolART_Tipologia_CanzoneInedita.checked){
			 counter++;
		 }
		 
		 if(document.forms[0].SpettacolART_Tipologia_MusicalRecitalTeatro.checked){
			 counter++;
		 }
		 
		 if(document.forms[0].SpettacolART_Tipologia_DanzaBans.checked){
			 counter++;
		 }
		 
		 if(document.forms[0].SpettacolART_Tipologia_Clownerie.checked){
			 counter++;
		 }
		 
		 if(document.forms[0].SpettacolART_Tipologia_HipHop.checked){
			 counter++;
		 }
		 
		 if(document.forms[0].SpettacolART_Tipologia_Cortometraggio.checked){
			 counter++;
		 }
		 
		 return !(counter > 0);
	 }
	
	 function verifyCreatType(){
		 var counter = 0;
		 
		 if(document.forms[0].CreativART_Tipologia_CanzoneInedita.checked){
			 counter++;
		 }
		 
		 if(document.forms[0].CreativART_Tipologia_MusicalRecitalTeatro.checked){
			 counter++;
		 }
		 
		 if(document.forms[0].CreativART_Tipologia_DanzaBans.checked){
			 counter++;
		 }
		 
		 if(document.forms[0].CreativART_Tipologia_Clownerie.checked){
			 counter++;
		 }
		 
		 if(document.forms[0].CreativART_Tipologia_HipHop.checked){
			 counter++;
		 }
		 
		 if(document.forms[0].CreativART_Tipologia_Cortometraggio.checked){
			 counter++;
		 }
		 
		 return !(counter > 0);
	 }
	 
	function validateForm() { 
		var validationReport = 'Attenzione! I seguenti campi contengono informazioni NON valide:\n';
		var isValid = true;
		
		
		//Check OratorioParrochia
		if(document.forms[0].OratorioParrocchia.value == ""){
			validationReport += '  - Nome della Parrocchia\n';
			isValid = false;
		}
	
		//Check SpettacolART-Descrizione
		if(document.forms[0].SpettacolART_Descrizione.value == ""){
			validationReport += '  - SpettacolART: Descrizione performance\n';
			isValid = false;
		}
		
		//Check SpettacolART type
		if(verifySpettType()){
			validationReport += '  - SpettacolART: Categoria\n';
			isValid = false;
		}
		
		//Check SpettacolART-NumPartecipanti
		if(document.forms[0].SpettacolART_NumPartecipanti.value == "" || !isANumber(document.forms[0].SpettacolART_NumPartecipanti.value)){
			validationReport += '  - SpettacolART: Numero partecipanti\n';
			isValid = false;
		}
		
		if(verifySpettacolARTPartecipanti()){
			validationReport += '  - SpettacolART: Elenco partecipanti (es. Nome, Cognome, 15 (1 superiore))\n';
			isValid = false;	
		}
		
		//Check CreativART-NumPartecipanti
		if(document.forms[0].CreativART_NumPartecipanti.value == "" || !isANumber(document.forms[0].CreativART_NumPartecipanti.value)){
			validationReport += '  - CreativART: Numero partecipanti\n';
			isValid = false;
		}

		if(verifyCreativARTPartecipanti()){
			validationReport += '  - CreativART: Elenco partecipanti (es. Nome, Cognome, 15 (1 superiore))\n';
			isValid = false;	
		}
		
		//Check Referente-Nominativo
		if(document.forms[0].Referente_Nominativo.value == ""){
			validationReport += '  - Referente: Nominativo (Nome e cognome)\n';
			isValid = false;
		}
		
		//Check CreativART type
		if(verifyCreatType()){
			validationReport += '  - CreativART: Ambiti workshop\n';
			isValid = false;
		}
		
		//Check Referente-EMail
		if(verifyEmail()){
			validationReport += '  - Referente: E-mail (es. esempio@dominio.it)\n';
			isValid = false;
		}
		
		//Check Referente-Telefono
		if(document.forms[0].Referente_Telefono.value == "" || !isANumber(document.forms[0].Referente_Telefono.value) ||
			document.forms[0].Referente_Telefono.value.length < 9){
			validationReport += '  - Referente: Recapito Telefonico (solo numeri, senza spazi)\n';
			isValid = false;
		}
		
		if(!isValid){
			alert(validationReport);	
		}
		
		return isValid; 
	} 
