function login() 
{
	var loginForm = document.getElementById("loginForm");
	if (loginForm.username.value == "") {
		alert("Inserisci lo username.");
		return false;
	}
	if (loginForm.password.value == "") {
		alert("Inserisci la password.");
		return false;
	}
	if (loginForm.codice.value == "") {
		alert("Inserisci il codice di controllo.");
		return false;
	}
	var submitForm = document.getElementById("submitForm");
	submitForm.username.value = loginForm.username.value;
	submitForm.response.value = hex_md5(hex_md5(loginForm.challenge.value)+hex_md5(loginForm.password.value));
	submitForm.codice.value = loginForm.codice.value;
	submitForm.RequestId.value = loginForm.RequestId.value;
	submitForm.submit();
}


function controlla_join_form(theForm)
{
	errore = "";
	var sit_join_form = document.getElementById("sit_join_form");
	
	dato = document.forms['sit_join_form']['first_name'];
	if ((dato.value.length < 2 ) || (dato.value.length > 15 ))errore = errore + "- Inserire nome (max 15)\n";
	
	dato = document.forms['sit_join_form']['last_name'];
	if ((dato.value.length < 2 ) || (dato.value.length > 15 ))errore = errore + "- Inserire cognome (max 15)\n";
	
	dato = document.forms['sit_join_form']['username'];
	if ((dato.value.length < 4 ) || (dato.value.length > 20 ))errore = errore + "- Inserire uno username di almeno 4 caratteri (max 20)\n";
	
	dato = document.forms['sit_join_form']['password'];
	if ((dato.value.length < 6 ) || (dato.value.length > 20 ))errore = errore + "- Inserire una password di almeno 6 caratteri (max 20)\n";
	
	dato1 = document.forms['sit_join_form']['password'];
	dato2 = document.forms['sit_join_form']['password2'];
	if (dato1.value != dato2.value)errore = errore + "- Controllare di aver inserito password coincidenti\n";
	
	dato = document.forms['sit_join_form']['squadra'];
	if ((dato.value.length < 2 ) || (dato.value.length > 50 ))errore = errore + "- Inserire il nome della squadra di almeno 2 caratteri (max 50)\n";
	
	dato = document.forms['sit_join_form']['email_address'].value;
	if (!controllomail(dato)) errore = errore + "- Indirizzo mail non corretto. \n";
	if (errore!="" ) {alert(errore);return false;} else return true;
	
}


function controllomail (emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|it)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
				alert("Indirizzo mail non corretto (controllare caratere @ e .'s)");
				return false;
				}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("indirizzo mail con caratteri non validi.");
			return false;
					}
		}
	
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("il nome di dominio inserito nell'indirizzo mail contiene caratteri non validi.");
			return false;
					}
		}
	
	if (user.match(userPat)==null) {
		alert("indirizzo mail non valido.");
		return false;
					}
	
	var IPArray=domain.match(ipDomainPat);
	
	if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
			alert("indirizzo IP non valido");
			return false;
			   }
		}
		return true;
	}
	
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
	if (domArr[i].search(atomPat)==-1) {
	alert("nome del dominio dell'indirizzo mail non valido.");
	return false;
	   }
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("indirizzo mail non risolto in modo corretto");
		return false;
		}
	
	if (len<2) {
		alert("indirizzo mail non corretto");
		return false;
		}	
		return true;
}

function generatepass(plength){
	var keylist="abcdefghijklmnopqrstuvwxyz123456789ABCDEFHJKLMNOPQRSTUVWXYZ";
	var temp='';
	for (i=0;i<plength;i++)
		temp+=keylist.charAt(Math.floor(Math.random()*keylist.length));
	return (temp);
}

function populateform(enterlength){
	/* var psw = generatepass(enterlength);
	document.frm_utente.password.value=psw;
	document.frm_utente.password2.value=psw;
	document.frm_utente.password.type='text'
	document.frm_utente.password2.type='text' */
	return (true);
}

function controllamodulo(theForm)
{
	var errore = "";
	//tolgo gli spazi da username e password
	document.forms['frm_utente']['username'].value = document.forms['frm_utente']['username'].value.replace(/ /g,"");
	document.forms['frm_utente']['password'].value = document.forms['frm_utente']['password'].value.replace(/ /g,"");
	dato = document.forms['frm_utente']['username'];
	if (dato.value.length < 4 )errore = errore + "Il nome utente deve essere almeno di 4 caratteri.\n";
	dato = document.forms['frm_utente']['password'];
	if (dato.value.lenght > 0){
		if (dato.value.length < 6 )errore = errore + "La password deve essere almeno di 6 caratteri.\n";
	}
	
	if (errore!="" ) {alert(errore);return(false);} 
	else return (true);
}

function avvisa_eliminazione(){
	alert("Sei sicuro di voler eliminare questo utente?");
	return (true);
}
