var updateStrength = function(pw) {
	var strength = getStrength(pw);
	return (100/32)*strength;
}

var getStrength = function(passwd) {
	intScore = 0;
	if (passwd.match(/[a-z]/)) // [verified] at least one lower case letter
		intScore = (intScore+1)
	if (passwd.match(/[A-Z]/)) // [verified] at least one upper case letter
		intScore = (intScore+5)
	if (passwd.match(/\d+/)) // [verified] at least one number
		intScore = (intScore+5)
	if (passwd.match(/(\d.*\d.*\d)/)) // [verified] at least three numbers
		intScore = (intScore+5)
	if (passwd.match(/[!,@#$%^&*?_~]/)) // [verified] at least one special character
		intScore = (intScore+5)
	if (passwd.match(/([!,@#$%^&*?_~].*[!,@#$%^&*?_~])/)) // [verified] at least two special characters
		intScore = (intScore+5)
	if (passwd.match(/[a-z]/) && passwd.match(/[A-Z]/)) // [verified] both upper and lower case
		intScore = (intScore+2)
	if (passwd.match(/\d/) && passwd.match(/\D/)) // [verified] both letters and numbers
		intScore = (intScore+2)
	if (passwd.match(/[a-z]/) && passwd.match(/[A-Z]/) && passwd.match(/\d/) && passwd.match(/[!,@#$%^&*?_~]/))
		intScore = (intScore+2)
	return intScore;
}

function changeInfo(champs, info, texte) {
	$('#info_'+champs).html('<img src="http://www.robotix.fr/templates/images/'+info+'.gif" alt="'+info+'" /> '+texte).removeClass().addClass('info_'+info);
	return true;
}

function actualiseCaptcha() {
  var d = new Date();
  $('#captcha').attr('src', 'libs/image.php?refresh='+d.getHours()+d.getMinutes()+d.getSeconds());
  return true;
}

function verif_pseudo(text) {
	var longueurText = text.length;
	if (text && longueurText > 0) {
		if (longueurText < 3)
			changeInfo('pseudo', 'erreur', 'Le pseudo doit faire au minimum 3 caractères ('+longueurText+' actuellement)');
		else if (longueurText > 15)
			changeInfo('pseudo', 'erreur', 'Le pseudo doit faire au maximum 15 caractères ('+longueurText+' actuellement)');
		else if ($.get('/templates/js/ajax/register.php', {type: "pseudo", pseudo: escape(text)}) == 1)
			changeInfo('pseudo', 'erreur', 'Le pseudo '+escape(text)+' est déjà utilisé');
		else
			changeInfo('pseudo', 'ok', '');
	}
	return true;
}

function verif_pass(pass) {
	if (pass.length > 0)
		changeInfo('password', 'ok', '');
	else
		changeInfo('password', 'erreur', 'Le mot de passe doit faire au minimum 1 caractère');
	return true;
}

function verif_pass_match(pass1, pass2) {
	if (pass1 != pass2)
		changeInfo('confirm', 'erreur', 'Les deux mots de passe ne correspondent pas');
	else if (pass1.length > 0)
		changeInfo('confirm', 'ok', '');
	return true;
}

function verif_mail(mail) {
	if (mail.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))
		changeInfo('mail', 'ok', '');
	else
		changeInfo('mail', 'erreur', 'L\'adresse e-mail n\'est pas valide');
}
