// conferma cancellazione
function fn_confirm(){
	if (confirm("Sei sicuro di voler procedere con la cancellazione?")){
		return true;
	}else{
		return false;
	};
}

// data una stringa controlla che sia un valido indirizzo email 
function indirizzoEmailValido(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;		
}

// controlla se la stringa data in ingresso è nel formato gg/mm/aaaa
function is_date(dateStr) {
	var datePat = /^(\d{1,2})(\/)(\d{1,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;
	}
	
	// parse 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
}

// controlla se la stringa data in ingresso è nel formato hh:mm
function is_hour(str) {
	var pat = /^(\d{1,2}):(\d{2})?$/;
	var matchArray = str.match(pat); // is the format ok?
	
	if (matchArray == null) {
		alert("Il formato corretto per l'ora è hh:mm.");
		return false;
	}
	
	// parse hour into variables
	hour 	= matchArray[1];
	minute 	= matchArray[2]; 
	
	if (hour < 0  || hour > 23) {
		alert("L'ora deve essere compresa fra 0 e 23.");
		return false;
	}
	if (minute < 0 || minute > 59) {
		alert ("I minuti devono essere compresi fra 0 e 59.");
		return false;
	}
	
	return true;
}

// conta quanti caratteri restano da scrivere in una textarea
function conta(){
	alert("qui");
	/*
	var lenMax = 135;
	var strlength = document.form.testoShort.value.length;
	total = eval("135");
	char  = eval(document.form.testoShort.value.length);
	left  = eval(total - char);
	if (left <= "-1"){
		var dif = eval(char - lenMax);
		var value = document.form.testoShort.value.substr(0, char-dif);
		document.form.testoShort.value = value;
		var left = "0";
	}
	document.form.quanti.value = left;
	*/
}

// validazione form login
function fn_check_login(){
	if (document.form.log.value==''){
		alert("Il campo 'USERNAME' è obbligatorio.");
		return false;
	}
	if (document.form.pwd.value==''){
		alert("Il campo 'PASSWORD' è obbligatorio.");
		return false;
	}

	return true;
}

//validazione nuova news
function fn_check_news(){
	if (document.form.titolo.value==''){
		alert("Il campo 'TITOLO' è obbligatorio.");
		return false;
	}
	if (document.form.data.value==''){
		alert("Il campo 'DATA' è obbligatorio.");
		return false;
	}
	if (!is_date(document.form.data.value)){
		return false;
	}	
	if ((document.form.ora.value!='')&&(!is_hour(document.form.ora.value))){
		return false;
	}
	if ( (document.form.image.value.indexOf("€")!=-1) || (document.form.image.value.indexOf("'")!=-1) ){
		alert("Il nome della foto non può contenere simboli come '€' o apice singolo.");
		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 (!indirizzoEmailValido(document.form.email.value)){
		alert("Il campo 'INDIRIZZO EMAIL' non è corretto.");
		return false;
	}
	if (document.form.note.value==''){
		alert("Il campo 'NOTE' è obbligatorio.");
		return false;
	}
	if (document.form.cod_utente.value=''){
		alert("Il campo 'CODICE DI SICUREZZA' è obbligatorio.");
		return false;
	}

	return true;
}

// controllo campi del form contatti_immagnet.php 
function fn_check_contatti_immagnet(){
	if (document.form.nome.value==''){
		alert("Il campo 'NOME' e' obbligatorio.");
		return false;
	}
	if (document.form.cognome.value==''){
		alert("Il campo 'COGNOME' e' obbligatorio.");
		return false;
	}
	if (document.form.email.value==''){
		alert("Il campo 'E-MAIL' e' obbligatorio.");
		return false;
	}
	if (!indirizzoEmailValido(document.form.email.value)) {
		alert("Il campo 'E-MAIL' non e' corretto.");
		return false;
	}
	if (document.form.note.value=='') {
		alert("Il campo 'RICHIESTA INFORMAZIONI' e' obbligatorio.");
		return false;
	}
	if (document.form.cod_captcha_digit.value=='') {
		alert("Il campo 'SICUREZZA' e' obbligatorio.");
		return false;
	}
	if (!document.form.chkPrivacy.checked){
		alert("Il consenso sulla 'PRIVACY' e' obbligatorio.");
		return false;
	}
	if (!is_numero(document.form.tel.value)) {
		alert("Il campo 'TEL' deve essere numerico.");
		return false;
	}

	return true;
}

function fn_popupImmagnet(){
	var stile = "top=10, left=10, width=500, height=500, status=no, menubar=no, toolbar=no scrollbar=no";
	url = "popup_contatti_immagnet.php";
	window.open(url, "", stile);
}