// 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_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_news(){
	if (document.form.titolo.value==''){
		alert("Il campo 'TITOLO' è obbligatorio.");
		return false;
	}
	if (document.form.testo.value==''){
		alert("Il campo 'TESTO' è obbligatorio.");
		return false;
	}

	return true;
}

function fn_check_sponsor(){
	if (document.form.link.value==''){
		alert("Il campo 'LINK' è obbligatorio.");
		return false;
	}
	if (document.form.image.value==''){
		alert("Il campo 'IMMAGINE' è obbligatorio.");
		return false;
	}

	return true;
}

function fn_check_gallery(){
	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.testoIT.value==''){
		alert("Il campo 'TESTO IT' è obbligatorio.");
		return false;
	}
	/*if (document.form.image.value==''){
		alert("Il campo 'IMMAGINE o PDF' è obbligatorio.");
		return false;
	}*/

	return true;
}

// ---------- AJAX ----------
function createRequestObject() {   
    var ro;   
    var browser = navigator.appName;   
    if(browser == "Microsoft Internet Explorer"){   
        ro = new ActiveXObject("Microsoft.XMLHTTP");   
    }else{   
        ro = new XMLHttpRequest();   
    }   
    return ro;   
}   
var http = createRequestObject();   

// ----- PER LA GESTIONE DELLA ROTAZIONE DEGLI SPONSOR -----
setTimeout("sndReqSponsor()", 7000);
function sndReqSponsor(){
    http.open('get', 'ajax_sponsor.php');   
    http.onreadystatechange = handleResponseSponsor;   
    http.send(null);   
}
function handleResponseSponsor() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('sponsor').innerHTML = http.responseText;   
			setTimeout("sndReqSponsor()", 7000);
        }   
    }   
}  

