//javascript
function valida(form){
	var error=0;
	var testo="ERRORE! Dati immessi non validi:\n";
	whiteall(form);
	for (i=1; i<form.length-1; i++){
		if (form.elements[i].value=="") {
			red(form.elements[i]);
			if (error==0) testo=testo+"-I seguenti box sono vuoti: "+form.elements[i].name;
				else testo=testo+", "+form.elements[i].name;
			error=1;
			}
	}
	
	if(form.username.value!=""){
		var usrn=form.username.value;
		if(usrn.search(/[^a-zA-Z0-9]/)!=-1){
			testo=testo+"\n-L'username contiene caratteri non validi";
			red(form.username);
			error=1;
		}
	}
	
	if ((form.password.value != form.confermapassword.value)){
			red(form.password);
			red(form.confermapassword);
			error=1;
			testo=testo+"\n-Le password non coincidono.";
	}
	
	if ((form.password.value.length<6)&&(form.password.value.lenght!="")){
			error=1;
			red(form.password);
			red(form.confermapassword);
			error=1;
			testo=testo+"\n-La password inserita è troppo corta";
	}
	
	
	if (echeck(form.email.value)){
		if ((form.email.value != form.confermaemail.value)){
				red(form.email);
				red(form.confermaemail); 
				error=1;
				testo=testo+"\n-Le email non coincidono.";
				}
			}
		else{
			red(form.email);
			red(form.confermaemail); 
			testo=testo+"\n-L'email inserita non è valida";
			error=1;
	}
	
	
	
	if (error>0) {
		alert(testo);
		return false;
		}
		else return true;
}



function whiteall(ogg){
	for(i=1; i<ogg.length-1; i++) ogg.elements[i].style.backgroundColor="#FFF";
}



function red(ogg){
	ogg.style.backgroundColor="#ff7171";
}



function echeck(str) {
		if (str=="" || str==null) return false;
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
}



function hide(elem){
	var a = document.getElementById(elem);
	a.style.display="none";
}



function show(elem){
	var a = document.getElementById(elem);
	a.style.display="block";
}



function changeop(elem,n){
	var a = document.getElementById(elem);
	a.options[0].selected="selected";
	if(elem=="opt"){
		if (n=="s"){
			a.options[3].style.display="block";
			a.options[4].style.display="block";
			a.options[5].style.display="block";
			}
		if (n=="h"){
			a.options[3].style.display="none";
			a.options[4].style.display="none";
			a.options[5].style.display="none";
		}
	}
	else{
		if (n=="s"){
			a.options[3].style.display="block";
			}
		if (n=="h"){
			a.options[3].style.display="none";
		}
	}
}



function conferma(what){
	var risp = confirm('Sei sicuro '+what+'?');
	if (risp)
		return true;
	else
		return false;
}



function insnewmail(){
	var cond=true;
	while(cond){
		var mail = prompt("ATTENZIONE: La nuova mail inserita sara' quella necessaria per il login al sito.\nInserisci la nuova mail:");
		var mail2 = prompt("Ripeti la nuova mail:");
		if (mail==mail2) cond=false;
			else alert("ATTENZIONE! Le mail inserite non coincidono!");
	}
	risp=echeck(mail);
	document.usernewmail.newmail.value = mail;
	if (risp) return true;
		else {
				alert("La mail inserita non risulta valida!");
				return false;
			}
}



function insnewusername(){
	var username = prompt("ATTENZIONE: Inserisci il nuovo username (di lunghezza massima 15caratteri, solo lettere e numeri):");
		
	if (username==null) return false;
		
	if((username!="") && (username.length<=15))
		if(username.search(/[^a-zA-Z0-9]/)!=-1){
				alert("Lo username inserito non risulta valido!");
				return false;
				}
		else {
				document.usernewusername.newusername.value = username;
				return true;	
			}
	else return false;


}

function insnewpass(){
	var ctrl=true;
	var password="";
	var password2="";
	while(ctrl) {
		password=prompt("ATTENZIONE: La password deve essere lunga almeno 6 caratteri. Si ricorda che e' Case Sensitive.\nInserisci la nuova password:");
		if((password=="")||(password.length<6)) alert("La password inserita non risulta valida");
			else{
				password2=prompt("Reinserisci la nuova password:");		
				if(password == password2) ctrl=false;
					else alert("Le password non coincidono");
			}
		}
	document.usernewpass.newpass.value = password;
	document.usernewpass.submit();
}


function insnewname(){
	var name = prompt("Inserisci il nuovo nome:");
		
	if (name==null) return false;
		
	if((name!="")){
				document.usernewname.newname.value = name;
				return true;	
			}
	else return false;
}


function changecolor(a){
	if (a==1)
	document.styleSheets[0].cssRules[5].style.backgroundColor = "green";
	else if (a==0)
		document.styleSheets[0].cssRules[5].style.backgroundColor = "orange";
		else document.styleSheets[0].cssRules[5].style.backgroundColor = "red";
}
