// 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 e' 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 e' 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 (dateStr=="")
		return true;
		
	if (matchArray == null) {
		alert("Il formato corretto per la data e' 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'e' 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 e' bisestile.");
			return false;
		}
	}
	return true; // date is valid
}

// controlla se la stringa data in ingresso e' 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 (str=="")
		return true;
		
	if (matchArray == null) {
		alert("Il formato corretto per l'ora e' 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;
}

function is_risultato(str){
	if (str=="") return true;
	
    var pat = /^(\d{1,2})-(\d{1,2})?$/;
	var matchArray = str.match(pat);
	
	if (matchArray == null){
		alert("a) Il formato corretto per il 'RISULTATO' e' <numero>-<numero>.");
		return false;
	}
	
	n1 = matchArray[1];
	n2 = matchArray[2];
	
	if ((n1>=0)&&(n1<99)&&(n2>=0)&&(n2<99)){
		return true;
	}else{
		alert("b) Il formato corretto per il 'RISULTATO' e' <numero>-<numero>.");
		return false;
	}
}

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

	return true;
}

// validazione form giocatore
function fn_check_giocatore(){
	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.dob.value==''){
		alert("Il campo 'DATA DI NASCITA' e' obbligatorio.");
		return false;
	}
	if (!is_date(document.form.dob.value)){
		return false;
	}
	if (!is_date(document.form.ceduto.value)){
		return false;
	}
	if (!is_date(document.form.dataSvincolo.value)){
		return false;
	}
	if (!is_numero(document.form.numero.value)){
		alert("Il campo 'NUMERO DI MAGLIA' deve essere numerico.");
		return false;
	}
	if (document.form.cmbRuolo.value=='-1'){
		alert("Il campo 'RUOLO' e' obbligatorio.");
		return false;
	}	
	if ((document.form.image.value.indexOf("Û")!=-1)
	    || (document.form.image.value.indexOf("'")!=-1)){
		alert("Il nome della foto non puo' contenere simboli, caratteri accentati o apici.");
		return false;
	}
	
	return true;
}

function fn_check_add_img(){
	if (document.form.titoloIMG.value==''){
		alert("Il campo 'TITOLO' e' obbligatorio.");
		return false;
	}	
	if ((document.form.fileIMG.value.indexOf("Û")!=-1)
	    || (document.form.fileIMG.value.indexOf("'")!=-1)){
		alert("Il nome del file non puo' contenere simboli, caratteri accentati o apici.");
		return false;
	}

	return true;
}

function fn_check_add_flv(){
	if (document.form.titoloFLV.value==''){
		alert("Il campo 'TITOLO' e' obbligatorio.");
		return false;
	}	
	if ((document.form.fileFLV.value.indexOf("Û")!=-1)
	    || (document.form.fileFLV.value.indexOf("'")!=-1)){
		alert("Il nome del file non puo' contenere simboli, caratteri accentati o apici.");
		return false;
	}

	return true;
}

//validazione nuova news
function fn_check_news(){
	if (document.form.titolo.value==''){
		alert("Il campo 'TITOLO' e' obbligatorio.");
		return false;
	}
	if (document.form.data.value==''){
		alert("Il campo 'DATA' e' 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 del file non puo' contenere simboli, caratteri accentati o apici.");
		return false;
	}
	if ((document.form.filePDF.value.indexOf("Û")!=-1)
	    || (document.form.filePDF.value.indexOf("'")!=-1)){
		alert("Il nome del file pdf non puo' contenere simboli, caratteri accentati o apici.");
		return false;
	}
	
	return true;
}

// validazione intervistato
function fn_check_intervistato(){
	if (document.form.int_nome.value==""){
		alert("Il campo 'NOME' e' obbligatorio.");
		return false;
	}
	if (document.form.int_cognome.value==""){
		alert("Il campo 'COGNOME' e' obbligatorio.");
		return false;
	}
	if (document.form.file.value==""){
		alert("Il campo 'FOTO' e' obbligatorio.");
		return false;
	}
	
	return true;
}

//validazione nuova intervista (domanda - risposta)
function fn_check_intervista(){
	if ((document.form.cmbGiocatore.value==-1)&&(document.form.cmbIntervistato.value==-1)){
		alert("Il campo 'INTERVISTATO' e' obbligatorio.");
		return false;
	}
	if (document.form.data.value==''){
		alert("Il campo 'DATA' e' obbligatorio.");
		return false;
	}
	if (!is_date(document.form.data.value)){
		return false;
	}	
	if (document.form.domanda.value==""){
		alert("Il campo 'DOMANDA' e' obbligatorio.");
		return false;
	}
	if (document.form.risposta.value==""){
		alert("Il campo 'RISPOSTA' e' obbligatorio.");
		return false;
	}
	
	return true;
}

// validazione form squadre
function fn_check_squadra(){
	if (document.form.nome.value==''){
		alert("Il campo 'NOME' e' obbligatorio.");
		return false;
	}
	
	return true;
}

function fn_check_calendario(){
	if (document.form.cmbStagione.value==-1){
		alert("Il campo 'STAGIONE' e' obbligatorio.");
		return false;
	}
	if (document.form.cmbSquadraCasa.value==-1){
		alert("Il campo 'SQUADRA IN CASA' e' obbligatorio.");
		return false;
	}
	if (document.form.cmbSquadraOspite.value==-1){
		alert("Il campo 'SQUADRA OSPITE' e' obbligatorio.");
		return false;
	}
	if (document.form.cmbSquadraCasa.value==document.form.cmbSquadraOspite.value){
		alert("Una squadra non puo' giocare contro se stessa.");
		return false;
	}
	if (document.form.data.value==''){
		alert("Il campo 'DATA' e' obbligatorio.");
		return false;
	}
	if (!is_date(document.form.data.value)){
		return false;
	}	
	if (document.form.ora.value==''){
		alert("Il campo 'ORA' e' obbligatorio.");
		return false;
	}
	if (!is_hour(document.form.ora.value)){
		return false;
	}
	if (!is_risultato(document.form.risultato.value)){
		return false;
	}	

	return true;
}

function fn_check_calendario_ins(){
	if (document.form.cmbStagione.value==-1){
		alert("Il campo 'STAGIONE' e' obbligatorio.");
		return false;
	}
	if (document.form.cmbTorneo.value==-1){
		alert("Il campo 'TORNEO' e' obbligatorio.");
		return false;
	}
	if (document.form.dataAndata.value==''){
		alert("Il campo 'DATA ANDATA' e' obbligatorio.");
		return false;
	}
	if (!is_date(document.form.dataAndata.value)){
		return false;
	}	
	if (document.form.oraAndata.value==''){
		alert("Il campo 'ORA ANDATA' e' obbligatorio.");
		return false;
	}
	if (!is_hour(document.form.oraAndata.value)){
		return false;
	}
	if (!is_date(document.form.dataRitorno.value)){
		return false;
	}	
	if (!is_hour(document.form.oraRitorno.value)){
		return false;
	}
	/*
	if (document.form.dataRitorno.value==''){
		alert("Il campo 'DATA RITORNO' e' obbligatorio.");
		return false;
	}
	if (document.form.oraRitorno.value==''){
		alert("Il campo 'ORA RITORNO' e' obbligatorio.");
		return false;
	}
	*/
	return true;
}

function fn_check_foto_tifoso(){
	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 'EMAIL' e' obbligatorio.");
		return false;
	}
	if (!indirizzoEmailValido(document.form.email.value)){
		alert("Il campo 'INDIRIZZO EMAIL' non e' corretto.");
		return false;
	}
	if (document.form.titoloIMG.value==''){
		alert("Il campo 'TITOLO' e' obbligatorio.");
		return false;
	}
	if (document.form.fileIMG.value==''){
		alert("Il campo 'FOTO' e' obbligatorio.");
		return false;
	}
	if ((document.form.fileIMG.value.indexOf("Û")!=-1)
	    || (document.form.fileIMG.value.indexOf("'")!=-1)){
		alert("Il nome della foto non puo' contenere simboli, caratteri accentati o apici.");
		return false;
	}
	if (!document.form.chkPrivacy.checked){
		alert("Il consenso alla 'PRIVACY' e' obbligatorio.");
		return false;
	}
	return true;
}

function fn_check_contatti(){
	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 'EMAIL' e' obbligatorio.");
		return false;
	}
	if (!indirizzoEmailValido(document.form.email.value)){
		alert("Il campo 'INDIRIZZO EMAIL' non e' corretto.");
		return false;
	}
	if (document.form.note.value==''){
		alert("Il campo 'MESSAGGIO' e' obbligatorio.");
		return false;
	}
	return true;
}

function fn_check_pdf_giovanile(){
	if (document.form.cmbStagione.value==-1){
		alert("Il campo 'STAGIONE' e' obbligatorio.");
		return false;
	}
	if (document.form.filePDF.value==""){
		alert("Il campo 'FILE PDF' e' obbligatorio.");
		return false;
	}
	file = document.form.filePDF.value.split(".");
	if (file[1].toLowerCase()!="pdf"){
		alert("Il campo 'FILE PDF' deve essere di tipo PDF.");
		return false;
	}
	if ((document.form.filePDF.value.indexOf("Û")!=-1)
	    || (document.form.filePDF.value.indexOf("'")!=-1)){
		alert("Il nome della pdf non puo' contenere simboli, caratteri accentati o apici.");
		return false;
	}
	
	return true;
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function fn_setTipoIMG(tipoIMG){
	document.form.tipoIMG.value = tipoIMG;
	//alert(tipoIMG);
	return true;
}

function fn_conta(){
	alert('ciao');
}
