/**
* strongPass.js
*
* This script provides a rudimentary check of the
* strength of a password. It is not able to check
* the password against a dictionary to prevent users
* from using common words or phrases, but it does
* check that the password contains upper- and lower-
* case letters, numerals, and symbols. For an 
* example, check out:
*    http://projects.jamessocol.com/scripts/javascript/strongPass.js.htm
*
* LICENSE: This product is freeware. Please feel 
* free to use, modify and distribute, and to learn
* from it. If you use it on your site, please leave
* me some credit, like "By..." if you use it as-is
* or "Based on..." if you modify it. Thanks!
*
* USAGE: To implement this script, simply create some
* <DIV> with a fixed width (in pixels) or a table cell
* and give it the following ID attribute:
*    id="passStrength"
* for example:
*    <td id="passStrength">
* Make sure no other elements of the page use this ID.
* Then add the following attribute to your password field:
*    onKeyUp="strongPass(this);"
* for example:
*    <input type="password" onKeyUp="strongPass(this)">
* Finally, add the following to the <HEAD> section of 
* your page:
*    <script language="javascript" src="strongPass.js"
*      type="text/javascript"></script>
* Then you're done!
* 
* @package	turnThePage
* @author	James Socol <me@jamessocol.com>
* @version	1.0
* @date		20 July 2006
*/

function strongPass (input)
{
	var text   = new Array('<table border="0" width="220" style="margin-left: 10px" cellpadding="0" cellspacing="0"><tr><td width="75" height="18"><table border="1" width="75" style="border-collapse: collapse" cellspacing="0" cellpadding="0"><tr><td width="75" bgcolor="#','" height="16" align="center" style="font-size: 8pt">','</td></tr></table></td><td width="75" height="18"><table border="1" width="75" style="border-collapse: collapse" cellspacing="0" cellpadding="0"><tr><td width="75" bgcolor="#','" height="16" align="center" style="font-size: 8pt">','</td></tr></table></td><td width="75" height="18"><table border="1" width="75" style="border-collapse: collapse" cellspacing="0" cellpadding="0"><tr><td width="75" bgcolor="#','" height="16" align="center" style="font-size: 8pt">','</td></tr></table></td></tr></table>');
	var empty  = new Array('E4E4E4', ' ', 'E4E4E4', '<font color="#808080">Non-évalué</font>', 'E4E4E4', ' ');
	var weak   = new Array('CC0000', '<b>Faible</b>', 'E4E4E4', ' ', 'E4E4E4', ' ');
	var ok     = new Array('CCCC00', ' ', 'CCCC00', '<b>Medium</b>', 'E4E4E4', ' ');
	var strong = new Array('00CC00', ' ', '00CC00', ' ', '00CC00', '<b>Fort</b>');
	var very   = new Array('00CC00', ' ', '00CC00', ' ', '00CC00', '<b>Fort</b>');
	var error  = '<div style="width:100%; background:#c00; color:#fff; font-weight:bold; font-size:65%; font-family:Tahoma">Erreur</div>';
	var password = input.value;
	var strength = 0;

	if (password.match(/[a-z]/)) { strength = strength + 10; }
	if (password.match(/[A-Z]/)) { strength = strength + 10; }
	if (password.match(/[0-9]/)) { strength = strength + 10; }
	if (password.match(/[^a-zA-Z0-9]/)) { strength = strength + 10; }
	if (password.length >= 6) { strength = strength + 15; }
	if (password.length >= 8) { strength = strength + 5; }
	if (password.length >= 14) { strength = strength + 10; }
	if (password.length == 0) {
		text = text[0] + empty[0] + text[1] + empty[1] + text[2] + empty[2] + text[3] + empty[3] + text[4] + empty[4] + text[5] + empty[5] + text[6];
	} else if (strength <= 30) {
		text = text[0] + weak[0] + text[1] + weak[1] + text[2] + weak[2] + text[3] + weak[3] + text[4] + weak[4] + text[5] + weak[5] + text[6];
	} else if (strength <= 40) {
		text = text[0] + ok[0] + text[1] + ok[1] + text[2] + ok[2] + text[3] + ok[3] + text[4] + ok[4] + text[5] + ok[5] + text[6];
	} else if (strength <= 50) {
		text = text[0] + strong[0] + text[1] + strong[1] + text[2] + strong[2] + text[3] + strong[3] + text[4] + strong[4] + text[5] + strong[5] + text[6];
	} else if (strength <= 60) {
		text = text[0] + very[0] + text[1] + very[1] + text[2] + very[2] + text[3] + very[3] + text[4] + very[4] + text[5] + very[5] + text[6];
	} else {
		text = error;
	}
	if (document.getElementById) {
		id = document.getElementById('passStrength');
		id.innerHTML = '';
		id.innerHTML = text;
	} else if (document.all) {
		id = document.all('passStrength');
		id.innerHTML = text;
	}
}


function check_passwd ()
{
	var error = '';
	var message = '';
	var password1 = document.getElementById('password1').value;
	var password2 = document.getElementById('password2').value;
	var strength = 0;

	if (password1.match(/[a-z]/)) { strength = strength + 10; }
	if (password1.match(/[A-Z]/)) { strength = strength + 10; }
	if (password1.match(/[0-9]/)) { strength = strength + 10; }
	if (password1.match(/[^a-zA-Z0-9]/)) { strength = strength + 10; }
	if (password1.length >= 6) { strength = strength + 15; }
	if (password1.length >= 8) { strength = strength + 5; }
	if (password1.length >= 14) { strength = strength + 10; }
	if (password1 != password2) {
		error = 'Les mots de passe entrés ne sont pas identiques.';
	} else if (password1.length == 0) {
		error = 'Le mot de passe ne peut pas être vide.';
	} else if (strength <= 30) {
		error = 'Le mot de passe entré est trop faible.';
	} else {
		message = 'Le mot de passe entré est acceptable.';
	} 

	if (error != '') {
		document.getElementById('passError').style.display = 'block';
		document.getElementById('passError').style.color = 'red';
		document.getElementById('passError').innerHTML = error;
	} else {
		document.getElementById('passError').style.display = 'block';
		document.getElementById('passError').style.color = 'green';
		document.getElementById('passError').innerHTML = message;
		document.getElementById('sbn4').className = 'login';
		document.getElementById('sbn4').removeAttribute("disabled");
	}
}

function check_change_passwd ()
{
	var error = '';
	var message = '';
	var password1 = document.getElementById('password1').value;
	var password2 = document.getElementById('password2').value;
	var strength = 0;

	if (password1.match(/[a-z]/)) { strength = strength + 10; }
	if (password1.match(/[A-Z]/)) { strength = strength + 10; }
	if (password1.match(/[0-9]/)) { strength = strength + 10; }
	if (password1.match(/[^a-zA-Z0-9]/)) { strength = strength + 10; }
	if (password1.length >= 6) { strength = strength + 15; }
	if (password1.length >= 8) { strength = strength + 5; }
	if (password1.length >= 14) { strength = strength + 10; }
	if (password1 != password2) {
		error = 'Les mots de passe entrés ne sont pas identiques.';
	} else if (password1.length == 0) {
		error = 'Le mot de passe ne peut pas être vide.';
	} else if (strength <= 30) {
		error = 'Le mot de passe entré est trop faible.';
	} else {
		message = 'Le mot de passe entré est acceptable.';
	} 

	if (error != '') {
		document.getElementById('passError').style.display = 'block';
		document.getElementById('passErrorMessage').style.color = 'red';
		document.getElementById('passErrorMessage').innerHTML = error;
	} else {
		document.getElementById('passError').style.display = 'block';
		document.getElementById('passErrorMessage').style.color = 'green';
		document.getElementById('passErrorMessage').innerHTML = message;
		document.getElementById('password_submit').className = 'button';
		document.getElementById('password_submit').removeAttribute("disabled");
	}
}
