
function check_data(){
	var data = document.getElementById('data_di_nascita_REQUIRED').value;
	var espressione = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
	if (!espressione.test(data)){
		alert(L_reg_dataFormat);
		return false;
	}
	else{
		anno = parseInt(data.substr(6),10);
		mese = parseInt(data.substr(3, 2),10);
		giorno = parseInt(data.substr(0, 2),10);
		
		var data = new Date(anno, mese-1, giorno);
		if(data.getFullYear() == anno && data.getMonth()+1 == mese && data.getDate() == giorno) return true;
		else{
			alert(L_reg_dataFormat);
			return false;
		}
	}
}

function check_sesso(){
	var sesso = document.getElementsByName('sesso');
	if((!sesso[0].checked)&&(!sesso[1].checked)){
		alert(L_reg_sesso);
		return false;
	}
	return true;
}

function check_termini(){
	var termini = document.getElementById('accetto_termini');
	if(!termini.checked){
		alert(L_termini);
		return false;
	}
	else return true;
}

function check_emails(){
	var email = document.getElementById('email_REQUIRED').value;
	var email2 = document.getElementById('email2').value;
	if(!checkEmail(email)){
		alert(L_reg_emailValida);
		return false;
	}
	if(email != email2){
		alert(L_reg_emailCoinc);
		return false;
	}
	return true;
}

function check_username(){
	var username = document.getElementById('username_REQUIRED').value;
	if(username.length < 5){
		alert(L_reg_user_1);
		return false;
	}
	return true;
}

function check_passwords(){
	var password = document.getElementById('password_REQUIRED').value;
	var password2 = document.getElementById('password2').value;
	
	if(password.indexOf(' ') >= 0){
		alert(L_reg_pass_1);
		return false;
	}
	
	if(password.length < 5){
		alert(L_reg_pass_2);
		return false;
	}
	if(password != password2){
		alert(L_reg_pass_3);
		return false;
	}
	return true;
}

function is_gruppo(group){
	if(group == '')						return true;
	if(group == 'mip_bus') 				return true;
	if(group == 'mip_phd') 				return true;
	if(group == 'ABTC_demo') 			return true;
	if(group == 'ELIS_demo') 			return true;
	if(group == 'ERIC_demo') 			return true;
	if(group == 'CHAR_demo') 			return true;
	return false;
}

function check_groups(){
	var group = document.getElementById('codice_gruppo').value;
	
	if(is_gruppo(group)) return true;
	
	alert(L_reg_gruppo_1);
	document.getElementById('codice_gruppo').value = '';
	return false;
}

function verifica_username(){
	idForm = 'registrazione';
	var form = document.getElementById(idForm);
	if(!check_username()) 	return false;
	document.getElementById('OK_REGISTRAZIONE').disabled = true;
	form.submit();
}

function check_RegistrazioneForm(){
	idForm = 'registrazione';
	
	var form = document.getElementById(idForm);
	var input = form.getElementsByTagName('input');
	var i, value, idInput, pos;
	
	i = 0;
	while(i<input.length){
		idInput = input.item(i).id;
		value = input.item(i).value;
		pos = idInput.indexOf('REQUIRED');
		if(pos >= 0){
			if ((value == '') || (value == 'undefined') || (value == undefined)){
				name = input.item(i).name;
				alert(L_reg_campo_1 + name + L_reg_campo_2);
				input.item(i).focus();
				input.item(i).select();
				return false;
			}
		}
		i++;
	}
	
	if(!check_username()) 	return false;
	if(!check_data()) 		return false;
	if(!check_sesso()) 		return false;
	if(!check_passwords()) 	return false;
	if(!check_emails()) 	return false;
	if(!check_groups()) 	return false;
	if(!check_termini()) 	return false;
	form.submit();
}

function open_local_verifica(page){
	window.open(page,'Verifica locale di registrazione','width=350,height=250');
}
