function Empty(elem){
	if(elem.value.length === 0){
		return true;
	}
	return false;
}

function isValidPayment(elem){
	if((parseInt(elem.value,10) <30) && (parseInt(elem.value,10) > 0)){
		return false;
	}
	return true;
}

function isValidDay(elem){
	if((parseInt(elem.value,10) < 1) || (parseInt(elem.value,10) > 31)){
		return false;
	}
	return true;
}

function isValidMonth(elem){
	if((parseInt(elem.value,10) < 1) || (parseInt(elem.value,10) > 12)){
		return false;
	}
	return true;
}

function isValidYear(elem){
	if((parseInt(elem.value,10) < 1900) || (parseInt(elem.value,10) > 2011)){
		return false;
	}
	return true;
}

function isValidYearPayment(elem){
	if((parseInt(elem.value,10) < 2009) || (parseInt(elem.value,10) > 2011)){
		return false;
	}
	return true;
}

function isValidDatePayment(gg,mm,aa){
	if(Empty(gg) || Empty(mm) || Empty(aa)){
		return false;
	}
	if(parseInt(aa.value,10) > 2011){
		return false;
	}
	if(parseInt(aa.value,10) < 2009){
		return false;
    }
	if(parseInt(aa.value,10) == 2011){
		var date = new Date();
		if(parseInt(mm.value,10) > (date.getMonth()+1)){
			return false;
        }
		if((parseInt(gg.value,10) > date.getDate()) && (parseInt(mm.value,10) == (date.getMonth()+1))){
			return false;
		}
	}
	return true;
}

function isNumeric(elem){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}
	return false;
}


function checkAllFields(){
	var error_found = false;
	var errors = "";
	if(Empty(document.getElementById('cognome'))){
		errors = errors + 'Cognome non inserito \n';
		error_found = true;
	}
	if(Empty(document.getElementById('nome'))){
		errors = errors + 'Nome non inserito \n';
		error_found = true;
	}
	if(Empty(document.getElementById('email'))){
		errors = errors + 'Indirizzo email non inserito \n';
		error_found = true;
	}
	if(Empty(document.getElementById('gg'))){
		errors = errors + 'Giorno di nascita non inserito \n';
		error_found = true;
	} 
	else if(!isNumeric(document.getElementById('gg')) || !isValidDay(document.getElementById('gg'))){
		errors = errors + 'Giorno di nascita non valido \n';
		error_found = true;
	}
	if(Empty(document.getElementById('mm'))){
		errors = errors + 'Mese di nascita non inserito \n';
		error_found = true;
	} 
	else if(!isNumeric(document.getElementById('mm')) || !isValidMonth(document.getElementById('mm'))){
		errors = errors + 'Mese di nascita non valido \n';
		error_found = true;
	}
	if(Empty(document.getElementById('aa'))){
		errors = errors + 'Anno di nascita non inserito \n';
		error_found = true;
	} 
	else if(!isNumeric(document.getElementById('aa')) || !isValidYear(document.getElementById('aa'))){
		errors = errors + 'Anno di nascita non valido \n';
		error_found = true;
	}
	if(Empty(document.getElementById('societa'))){
		errors = errors + 'Nome societa non inserito \n';
		error_found = true;
	}
	if(Empty(document.getElementById('numeroTessera'))){
		errors = errors + 'Numero Tessera non inserito \n';
		error_found = true;
	}
	if(Empty(document.getElementById('codSocieta'))){
		errors = errors + 'Codice societa non inserito \n';
		error_found = true;
	}
	if(Empty(document.getElementById('ente'))){
		errors = errors + 'Ente non inserito \n';
		error_found = true;
	}
	if(Empty(document.getElementById('cellulare'))){
		errors = errors + 'Numero di cellulare non inserito \n';
		error_found = true;
	}
	if(Empty(document.getElementById('ggpag'))){
		errors = errors + 'Giorno di pagamento non inserito \n';
		error_found = true;
	} 
	else if(!isNumeric(document.getElementById('ggpag'))){
			errors = errors + 'Giorno di pagamento non valido \n';
			error_found = true;
	}
	if(Empty(document.getElementById('mmpag'))){
		errors = errors + 'Mese di pagamento non inserito \n';
		error_found = true;
	} 
	else if(!isNumeric(document.getElementById('mmpag'))){
			errors = errors + 'Mese di pagamento non valido \n';
			error_found = true;
	}
	if(Empty(document.getElementById('aapag'))){
		errors = errors + 'Anno di pagamento non inserito \n';
		error_found = true;
	} 
	else if(!isNumeric(document.getElementById('aapag'))){
			errors = errors + 'Anno di pagamento non valido \n';
			error_found = true;
	}
	if(!isValidDatePayment(document.getElementById('ggpag'),document.getElementById('mmpag'),document.getElementById('aapag'))){
			errors = errors + 'Data di pagamento quota non valida \n';
			error_found = true;
	}
	if(Empty(document.getElementById('pagato'))){
		errors = errors + 'Importo versato non inserito \n';
		error_found = true;
	} 
	else if(!isNumeric(document.getElementById('pagato')) || !isValidPayment(document.getElementById('pagato'))){
			errors = errors + 'Importo pagamento non valido \n';
			error_found = true;
	}
	if(Empty(document.getElementById('versamento'))){
		errors = errors + 'Nome intestatario versamento non inserito \n';
		error_found = true;
	}
	
	if (error_found){
		alert(errors);
		return false;
	}
	else{
		return true;
	}
}

function checkAllFieldsGruppo(){
	var error_found = false;
	var errors = "";
	if(Empty(document.getElementById('societa'))){
		errors = errors + 'Societa\' di appartenenza non inserita \n';
		error_found = true;
	}
	if(Empty(document.getElementById('codsocieta'))){
		errors = errors + 'Codice societa\' non inserito \n';
		error_found = true;
	}
	if(Empty(document.getElementById('ente'))){
		errors = errors + 'Ente non inserito \n';
		error_found = true;
	}
	if(Empty(document.getElementById('cellulare'))){
		errors = errors + 'Telefono cellulare non inserito \n';
		error_found = true;
	}
	if(Empty(document.getElementById('numero_iscritti'))){
		errors = errors + 'Numero iscritti non inserito \n';
		error_found = true;
	}
	if(Empty(document.getElementById('email'))){
		errors = errors + 'Indirizzo email non inserito \n';
		error_found = true;
	}
	else if(!isNumeric(document.getElementById('numero_iscritti'))){
			errors = errors + 'Numero iscritti non valido \n';
			error_found = true;
	}

	if(Empty(document.getElementById('ggpag'))){
		errors = errors + 'Giorno di pagamento non inserito \n';
		error_found = true;
	}
	else if(!isNumeric(document.getElementById('ggpag')) || !isValidDay(document.getElementById('ggpag'))){
			errors = errors + 'Giorno di pagamento non valido \n';
			error_found = true;
	}
	if(Empty(document.getElementById('mmpag'))){
		errors = errors + 'Mese di pagamento non inserito \n';
		error_found = true;
	}
	else if(!isNumeric(document.getElementById('mmpag'))|| !isValidMonth(document.getElementById('mmpag'))){
			errors = errors + 'Mese di pagamento non valido \n';
			error_found = true;
	}
	if(Empty(document.getElementById('aapag'))){
		errors = errors + 'Anno di pagamento non inserito \n';
		error_found = true;
	}
	else if(!isNumeric(document.getElementById('aapag')) || !isValidYearPayment(document.getElementById('aapag'))){
			errors = errors + 'Anno di pagamento non valido \n';
			error_found = true;
	}
	if(!isValidDatePayment(document.getElementById('ggpag'),document.getElementById('mmpag'),document.getElementById('aapag'))){
			errors = errors + 'Data di pagamento quota non valida \n';
			error_found = true;
	}
	if(Empty(document.getElementById('pagamento'))){
		errors = errors + 'Importo versato non inserito \n';
		error_found = true;
	}
	else if(!isNumeric(document.getElementById('pagamento')) || !isValidPayment(document.getElementById('pagamento'))){
			errors = errors + 'Importo pagamento non valido \n';
			error_found = true;
	}
	if(Empty(document.getElementById('versamento'))){
		errors = errors + 'Nome intestatario versamento non inserito \n';
		error_found = true;
	}
	
	if (error_found){
		alert(errors);
		return false;
	}
	else{
		return true;
	}
}


function checkAllFieldsSingoli(n){
	var error_found = false;
	var errors = "";
	
	var i=1;
	var atleti = parseInt(n,10);
	while(i<=atleti){
		if(Empty(document.getElementById('cognome'+i))){
			errors = errors + 'Cognome atleta ' + i + ' non inserito \n';
			error_found = true;
		}
		if(Empty(document.getElementById('nome'+i))){
			errors = errors + 'Nome atleta ' + i + ' non inserito \n';
			error_found = true;
		}
		if(Empty(document.getElementById('gg'+i))){
			errors = errors + 'Giorno di nascita atleta ' + i + ' non inserito \n';
			error_found = true;
		}
		else if(!isNumeric(document.getElementById('gg'+i)) || !isValidDay(document.getElementById('gg'+i))){
				errors = errors + 'Giorno di nascita atleta ' + i + ' non valido \n';
				error_found = true;
		}
		if(Empty(document.getElementById('mm'+i))){
			errors = errors + 'Mese di nascita atleta ' + i + ' non inserito \n';
			error_found = true;
		}
		else if(!isNumeric(document.getElementById('mm'+i))  || !isValidMonth(document.getElementById('mm'+i))){
				errors = errors + 'Mese di nascita atleta ' + i + ' non valido \n';
				error_found = true;
		}
		if(Empty(document.getElementById('aa'+i))){
			errors = errors + 'Anno di nascita atleta ' + i + ' non inserito \n';
			error_found = true;
		}
		else if(!isNumeric(document.getElementById('aa'+i)) || !isValidYear(document.getElementById('aa'+i))){
				errors = errors + 'Anno di nascita atleta ' + i + ' non valido \n';
				error_found = true;
		}
	
		if(Empty(document.getElementById('numeroTessera'+i))){
			errors = errors + 'Numero Tessera atleta ' + i + ' non inserito \n';
			error_found = true;
		}
	i = i + 1;	
	}
	
	if (error_found){
		alert(errors);
		return false;
	}
	else{
		return true;
	}
}




