function random_nb(min, max) {
	return (Math.floor(Math.random() * (max - min)) + min);
}

function random_char(string) {
	return string.charAt(random_nb(0, string.length));
}

function is_checked(name) {
	return document.getElementsByName(name)[0].checked;
}

function get_value(name) {
	return document.getElementsByName(name)[0].value;
}

function htmlentities(text) {
	text = text.replace(/&/g,'&amp;');
	text = text.replace(/"/g, "&quot;");
	text = text.replace(/</g, "&lt;");
	text = text.replace(/>/g, "&gt;");
	return text;
}

function create_psw(string, length) {
	var password = '';

	for(var i = 0; i < length; i++) {
		password += random_char(string);
	}
	
	return htmlentities(password);
}

function create_easy_psw(string_easy, strong_string, length) {
	var password = create_psw(string_easy, length);
	var psw_array = new Array();
	//jusqu'à 2 caractères spéciaux pour les mots de passe courts et 3 pour les longs
	var length_special_chars = length >= 8 ? 3 : 2;
	
	for(var i = 0; i < length; i++)
		psw_array[i] = password.charAt(i);

	for(var i = 0; i < length_special_chars; i++)
		psw_array[random_nb(0, length)] = random_char(strong_string);

	return htmlentities(psw_array.join(''));
}

function generate_passwords() {
	var string = '';
	var result = '';
	var length = get_value('length');
	var nb_psw = get_value('nb_psw');
	
	if (is_checked('is_easy') == true) {		
		if(length > 10) {
			if(confirm('La longueur de votre mot de passe est trop longue pour être retenu.\nSi vous souhaitez l\'ajuster à 10 caractères, cliquez sur "OK".')) {
				length = 10;
				document.getElementsByName('length')[0].value = '10';
			}
		}
		
		if(length < 4) {
			if(confirm('La longueur de votre mot de passe est trop courte, celui-ci risque d\'être très peu sûr.\nSi vous souhaitez l\'ajuster à 4 caractères, cliquez sur "OK".')) {
				length = 4;
				document.getElementsByName('length')[0].value = '4';
			}
		}
		
		//on prend tout en compte pour plus de sécurité
		document.getElementsByName('is_uppercases')[0].checked = true;
		document.getElementsByName('is_lowercases')[0].checked = true;
		document.getElementsByName('is_numbers')[0].checked = true;
		document.getElementsByName('is_specials')[0].checked = true;

		for(var i = 0; i < nb_psw; i++) {
			var easy_string = get_value('lowercases') + get_value('numbers');
			var strong_string = get_value('uppercases') + get_value('specials');
			if(is_checked('is_space')) strong_string += ' ';
			
			result += '<input name="psw_'+ i +'" type="text" value="'+ create_easy_psw(easy_string, strong_string, length) +'" size="'+ length +'" onfocus="select()" /><br />';
		}
	}
	else {
		if (is_checked('is_uppercases') == true)	string += get_value('uppercases');
		if (is_checked('is_lowercases') == true)	string += get_value('lowercases');
		if (is_checked('is_numbers') == true)		string += get_value('numbers');
		if (is_checked('is_specials') == true)		string += get_value('specials');
		if (is_checked('is_space') == true)			string += ' ';
	
		for(var i = 0; i < nb_psw; i++) {
			result += '<input name="psw_'+ i +'" type="text" value="'+ create_psw(string, length) +'" size="'+ length +'" onfocus="select()" /><br />';
		}
	}
	
	document.getElementById('result').innerHTML = result;
}

function show_infos_easy_psw(visible) {
	document.getElementById('infos_easy_psw').style.display = visible ? 'block' : 'none';
}

function alert_bad_psw(visible) {
	document.getElementById('bad_psw').style.display = visible ? 'block' : 'none';
}

function verif_psw() {
	if(parseInt(document.getElementById('length').value) < 8)
		alert_bad_psw(true);
	else
		alert_bad_psw(false);
}

function increase_length() {
	document.getElementById('length').value = parseInt(document.getElementById('length').value) + 1;
	verif_psw();
}

function decrease_length() {
	if( parseInt(document.getElementById('length').value) > 4 )
		document.getElementById('length').value = parseInt(document.getElementById('length').value) - 1;
	verif_psw();
}
