function UpperFirst(fieldname, strWord){
	objStrWord = new String(strWord);
	objStrWord = Trim(objStrWord);
	//converto la stringa tutta in minuscolo
	objStrWord = objStrWord.toLowerCase(strWord);
	first = objStrWord.slice(0,1)
	first = first.toUpperCase();
	rest = objStrWord.slice(1);
	strWord	= first + rest;
	if (fieldname == 'nome'){	
		document.frmUserRegister.nome.value = strWord;
	}
	if (fieldname == 'cognome'){	
		document.frmUserRegister.cognome.value = strWord;
	}
}

function Trim(TRIM_VALUE){
	
	if(TRIM_VALUE.length < 1){
		return "";
	}
	
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	
	if(TRIM_VALUE==""){
		return "";
	}
	else{
		return TRIM_VALUE;
	}

} //End Function


function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
	return"";
	}
	var iTemp = v_length -1;
	
	while(iTemp > -1){
	if(VALUE.charAt(iTemp) == w_space){
	}
	else{
	strTemp = VALUE.substring(0,iTemp +1);
	break;
	}
	iTemp = iTemp-1;
	
	} //End While
	return strTemp;

} //End Function

function LTrim(VALUE){
	
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
	return"";
	}
	
	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;

	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
		strTemp = VALUE.substring(iTemp,v_length);
		break;
		}
		iTemp = iTemp + 1;
		} //End While
	
	return strTemp;
	
}



var maxDivs = 8;

function checkEmail(strIndirizzoEmail){
	
	objIndirizzoEmail = new String(strIndirizzoEmail);
	var controlloIndirizzo = objIndirizzoEmail.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.local)|(\..{2,2}))$)\b/gi);
	
	if (controlloIndirizzo){
		controlloIndirizzo = 0; //nessun errore
		}
	else{
		controlloIndirizzo = 1; // errore
	}
	
	return controlloIndirizzo;
}

function checkDataDiNascita(dataDiNascita){
	
	strTemp = new String(dataDiNascita);
	strTempA = strTemp.split("/"); 
	giorno = parseInt(strTempA[0]);
	mese = parseInt(strTempA[1]);	
	anno = parseInt(strTempA[2]);
	tempAnnoCorrente = new Date();
	annoCorrente = tempAnnoCorrente.getFullYear();
	
	if(isNaN(giorno) || isNaN(mese) || isNaN(anno)){
		check = 1;
	}
	else{
		if((giorno>=1) && (giorno<=31)  && (mese>=1) && (mese<=12) && (anno>1900) && (anno<annoCorrente)){
			check = 0;
		}
		else{
		check = 2;		
		}
	}
	
	return check;
}

function  hideHelp(id){
	if (id == 'all'){
		for(var i = 1; i <= maxDivs; i++){
			document.getElementById('div' + i).style.visibility = 'hidden';
			document.getElementById('div' + i).style.display = 'none';
		}
	}
	else{
		document.getElementById(id).style.visibility = 'hidden';
		document.getElementById(id).style.display = 'none';
	}	
}

function checkUsername(strUsername){
	
	objUsername = new String(strUsername);
	var controlloUsername = objUsername.match(/^\w+$/);

	if (controlloUsername){
		controlloUsername = 0; //nessun errore
		}
	else{
		controlloUsername = 1; // errore
	}
	
	return controlloUsername;
	
}

function checkRegFields(){
	
	var username = new String(document.frmUserRegister.username.value);
	var password = document.frmUserRegister.password.value;
	var password2 = document.frmUserRegister.password2.value;
	
	var cognome = document.frmUserRegister.cognome.value;
	var nome = document.frmUserRegister.nome.value;
	
	var dataDiNascita = document.frmUserRegister.dataDiNascita.value;
	var indirizzo = document.frmUserRegister.indirizzo.value;
	var telefono = document.frmUserRegister.telefono.value;
	var telefonoCellulare = document.frmUserRegister.telefonoCellulare.value;
	var email = document.frmUserRegister.email.value;
	var tipoDoc = document.frmUserRegister.tipoDoc.value;
	var numdocumento = document.frmUserRegister.numdocumento.value;
	var professione = document.frmUserRegister.professione.value;
	var titoloDiStudio = document.frmUserRegister.titoloDiStudio.value;
	var controlloEmail = checkEmail(email );
	var controlloDataDiNascita = checkDataDiNascita(dataDiNascita);
	var controlloUsername = checkUsername(username);

	if (username.length<3){
		alert('lo username non è valido : min 3 caratteri');
		document.frmUserRegister.username.focus();
		return false;	
	}

	//controllo username solo caratteri [a-z] [A-Z]
	if (controlloUsername==1){
		alert('lo username non è valido : solo caratteri e numeri');
		document.frmUserRegister.username.focus();
		return false;	
	}

	if ((password=='') || (password2=='')){
		alert('le password non possono essere nulle');
		document.frmUserRegister.password.focus();
		return false;
	}

	if ((password.length<6) || (password2.length<6)){
		alert('le password non possono essere \ndi lunghezza minore di 6 caratteri');
		return false;		
	}

	if(password2 != password){
		alert('le password non coincidono');
		document.frmUserRegister.password2.focus();
		return false;
	}

	if (nome.length<1) {
		alert('Il nome non è valido');
		document.frmUserRegister.nome.focus();
		return false;		
	}

	if (cognome.length<1) {
		alert('Il cognome non è valido');
		document.frmUserRegister.cognome.focus();
		return false;		
	}

	if (indirizzo.length<1){
		alert('indirizzo non valido');
		document.frmUserRegister.indirizzo.focus();
		return false;		
	}

	if ((telefono=='') && (telefonoCellulare=='')){
		alert('specificare almeno un telefono');
		if (telefono=='')
			document.frmUserRegister.telefono.focus();
		return false;		
	}

	
	//se ci sono errori vale 1
	if(controlloEmail != 0){
		alert('email non valida');
		document.frmUserRegister.email.focus();
		return false;		
	}

	//se ci sono errori vale 1
	if(controlloDataDiNascita != 0){
		alert('controlla il formato della \n data di nascita \n [gg/mm/anno] \ncod:' + controlloDataDiNascita);
		document.frmUserRegister.dataDiNascita.focus();
		return false;
	}

	if (numdocumento.length<1){
		alert('controllare il numero del documento');
		document.frmUserRegister.numdocumento.focus();
		return false;
	}
	document.frmUserRegister.submit();
}

function checkNLreg(){
	var operazione;
	var email='';
	//alert(document.newsletter.operazioneSI.checked + ' ' + document.newsletter.operazioneNO.checked);
	if(document.all){
		email=Trim(document.newsletter.email.value);
	}
	if(document.getElementById){
		email=Trim(document.getElementById('email').value);
	}
	if(email!=''){		
		if(checkEmail(email)==0){
			document.newsletter.submit();	
		}
		else{
			alert("Attenzione controllare l\'indirizzo email");
		}
	} 
	else{
		alert("Attenzione controllare l\'indirizzo email");
	}
}



function checkCode(){
	document.confermaRegistrazione.submit();
}
