﻿function checkPassword(submit, password1, password2, target, first)
{
	submit.disabled = true;
	if ((password1.value.length == 0) && (password2.value.length == 0)) {
		target.style.color = "red";
		if (first)
			target.innerHTML = "";
		else
			target.innerHTML = "Fehler: Sie müssen ein Passwort angeben.";
		return;
	}
	if ((password1.value.length < 5) && (password2.value.length > 0)) {
		target.innerHTML = "Fehler: Das Passwort muss mindestens 5 Zeichen lang sein!";
		target.style.color = "red";
		return;
	}	
	if (password1.value != password2.value) {
		target.innerHTML = "Fehler: Die beiden Passwörter stimmen nicht überein.";
		target.style.color = "red";
		return;
	}
	
	value = password1.value;
	/* count duplicate characters */
	dupes = 0;
	count = new Array(256);
	for (i = 0; i < 256; ++i)
		count[i] = 0;
	for (i = 0; i < value.length; ++i) {
		count[value.charCodeAt(i)]++;
	}
	for (i = 0; i < count.length; ++i)
		if (count[i] > 1) dupes += count[i] - 1;
		
	/* build basic score depending on "real" length (without dupes) */
	result = (value.length - dupes) * 40 / 8;

	/* honor different character classes */
	contains = 0;
	if (value.match(/[^0-9a-zA-Z]/))
		contains += 2.5;
	if (value.match(/[0-9]/))
		contains += 1.5;
	if (value.match(/[A-Z]/))
		contains += 1.0;
	if (value.match(/[a-z]/))
		contains += 1.0;
	result *= contains;
		
	if (result < 30) {
		target.innerHTML = "Das Passwort ist sehr unsicher. Sie sollten ein anderes wählen.";
		target.style.color = "red";
	} else if (result < 75) {
		target.innerHTML = "Das Passwort ist relativ unsicher. Sie sollten ein anderes wählen.";
		target.style.color = "#e9b700";
		submit.disabled = false;
	} else if (result < 150) {
		target.innerHTML = "Das Passwort ist relativ sicher.";
		target.style.color = "#859a50";
		submit.disabled = false;
	} else {
		target.innerHTML = "Das Passwort ist sehr sicher.";
		target.style.color = "#32d946";
		submit.disabled = false;
	}
	
}

