// JavaScript Document

// chiede conferma relativamente alla cancellazione di un oggetto
function fn_confirm(){
	if (confirm("Procedere con la cancellazione?")){
		return true;
	}else{
		return false;
	};
}

// data una stringa controlla che sia un valido indirizzo email 
function is_email(str) {
	if (window.RegExp) {
    	var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    	var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    	var regnv = new RegExp(nonvalido);
    	var regv = new RegExp(valido);
    	if (!regnv.test(str) && regv.test(str))
      		return true;
    	return false;
	} else {
    	if(str.indexOf("@") >= 0)
      		return true;
    	return false;
  	}
}

// data una stringa controlla che sia un numero
function is_numero(str){
	var nr="1234567890";
	
	// se la stringa è vuota va bene
	if (str=="")
		return true;
		
	for (i=0; i<=(str.length-1); i++)
		if (nr.indexOf(str.charAt(i))==(-1))
			return false;

	return true;		
}

function is_date(dateStr) {
	//var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
	var datePat = /^(\d{2})(\/)(\d{2})(\/)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) {
		alert("Il formato corretto per la data è gg/mm/aaaa.");
		return false;
	}
	
	// p@rse date into variables
	day 	= matchArray[1];
	month 	= matchArray[3]; 
	year 	= matchArray[5];
	
	if (day < 1 || day > 31) {
		alert("Il giorno deve essere compreso fra 1 e 31.");
		return false;
	}

	if (month < 1 || month > 12) { 
		alert("Il mese deve essere compreso fra 1 e 12..");
		return false;
	}
		
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("30 dì conta Novembre con April Giugno e Settembre, di 28 ce n'è uno tutti gli altri ne han 31!")
		return false;
	}
	
	// check for february 29th
	if (month == 2) { 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			alert("Quest'anno non è bisestile.");
			return false;
		}
	}
	return true; // date is valid
}

function fn_menu_on(id){
	//alert("on");
	document.getElementById(id).style.visibility = "visible";
}
function fn_menu_off(id, filePHP){
	filePHP = filePHP.toLowerCase();
	if ((id+".php") == filePHP)
		document.getElementById(id).style.visibility = "visible";
	else
		document.getElementById(id).style.visibility = "hidden";
}

function fn_check_login(){
	if (document.form.username.value==''){
		alert("Il campo 'USERNAME' è obbligatorio.");
		return false;
	}
	if (document.form.password.value==''){
		alert("Il campo 'PASSWORD' è obbligatorio.");
		return false;
	}

	return true;
}

function fn_check_contatti(){
	if (document.form.nome.value==''){
		alert("Il campo 'NOME' è obbligatorio.");
		return false;
	}
	if (document.form.cognome.value==''){
		alert("Il campo 'COGNOME' è obbligatorio.");
		return false;
	}
	if (document.form.email.value==''){
		alert("Il campo 'EMAIL' è obbligatorio.");
		return false;
	}
	if (!is_email(document.form.email.value)){
		alert("Il campo 'EMAIL' non è corretto.");
		return false;
	}
	if (document.form.note.value==''){
		alert("Il campo 'NOTE' è obbligatorio.");
		return false;
	}
	if (!document.form.chkPrivacy.checked){
		alert("Il consenso alla 'PRIVACY' è obbligatorio.");
		return false;
	}

	return true;
}

function fn_check_evento(){
	if (document.form.titoloIT.value==''){
		alert("Il campo 'TITOLO IT' è obbligatorio.");
		return false;
	}
	if (document.form.titoloEN.value==''){
		alert("Il campo 'TITOLO EN' è obbligatorio.");
		return false;
	}
	if (document.form.testoIT.value==''){
		alert("Il campo 'TESTO IT' è obbligatorio.");
		return false;
	}
	if (document.form.testoEN.value==''){
		alert("Il campo 'TESTO EN' è obbligatorio.");
		return false;
	}

	return true;
}

