function check_nick(box, exists){
	var casella=document.getElementById(box);
	var nick=document.getElementById(box).value;
	if((exists.match("%"+nick+"%"))||(nick.length<=3)){
		//casella.setAttribute('class','input_ko');
		casella.className='input_ko';
		document.getElementById(box+"_txt").innerHTML="<span style='color:red;'>Nick già in uso</span>";
		if(nick.length<=3){
			document.getElementById(box+"_txt").innerHTML="<span style='color:red;'>Nick troppo corto</span>";
		}
		return false;
	}
	else{
		//casella.setAttribute('class','input_ok');
		casella.className='input_ok';
		document.getElementById(box+"_txt").innerHTML="<span style='color:green;'>Nick Valido</span>";
		return true;
	}
}

function check_password_change(box){
	var casella=document.getElementById(box);
	if (casella.value.length>0) {
		return true;
	}
	return false;
}

function check_password(box){
	var casella=document.getElementById(box);
	
	var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g");
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
var enoughRegex = new RegExp("(?=.{6,}).*", "g");
	var pwd = casella;
	if (pwd.value.length==0) {
		casella.className='input_ko';
		document.getElementById(box+"_txt").innerHTML="<span style='color:red;'>Password troppo corta</span>";
		return false;
	} else if (false == enoughRegex.test(pwd.value)) {
		casella.className='input_ko';
		document.getElementById(box+"_txt").innerHTML="<span style='color:red;'>Password troppo corta</span>";
		return false;
	} else if (strongRegex.test(pwd.value)) {
		casella.className='input_ok';
		document.getElementById(box+"_txt").innerHTML="<span style='color:green;'>Password forte</span>";
		return true;
	} else if (mediumRegex.test(pwd.value)) {
		casella.className='input_medium';
		document.getElementById(box+"_txt").innerHTML="<span style='color:orange;'>Password media</span>";
		return true;
	} else { 
		casella.className='input_medium';
		document.getElementById(box+"_txt").innerHTML="<span style='color:red;'>Password debole</span>";
		return true;
	}
}

function verifier(box, check){
	var casella=document.getElementById(box);
	var verificatore=document.getElementById(check);
	
	if(casella.value!=verificatore.value){
		verificatore.className='input_ko';
		document.getElementById(check+"_txt").innerHTML="<span style='color:red;'>Non coincide</span>";
		return false;
	}
	else{
		verificatore.className='input_ok';
		if(box=='password'){
			check_password(check);
		}
		document.getElementById(check+"_txt").innerHTML="<span style='color:green;'>Coincide</span>";
		return true;
	}
}

function check_mail(box) {
var casella = document.getElementById(box);
var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(casella.value)) {
	casella.className='input_ko';
	document.getElementById(box+"_txt").innerHTML="<span style='color:red;'>Formato non valido</span>";
	return false;
}
else{
	casella.className='input_ok';
	document.getElementById(box+"_txt").innerHTML="<span style='color:green;'>Formato valido</span>";
	return true;
}
}

function check_all(exists){

	if(exists.length>0){
		if(!check_nick('nick_reg',exists)){
			return false;
		}
	}
	else{
			if(!check_password_change('password_now')){
				return false;
			}
			if(!check_mail('mail_now')){
				return false;
			}
	}
	if(!check_password('password')){
		return false;
	}
	if(!check_mail('mail')){
		return false;
	}
	if(!verifier('password','confpassword')){
		return false;
	}
	if(!verifier('mail','confmail')){
		return false;
	}
	document.getElementById("createfrm").submit();
}
