

var submitButtonId;
var passwordInputId;
var passwordAgainInputId;

var strengthInfo = new Array("Ihr Kennwort ist ok.", "Bitte beachten Sie: Das Kennwort muss mindestens 8 Zeichen lang sein und sowohl Zahlen, als auch Gro&szlig;- und Kleinbuchstaben enthalten.", "Ihr Kennwort enth&auml;lt ung&uuml;ltige Zeichen.");
var compareInfo = new Array("Kennw&ouml;rter stimmen &uuml;berein.", "Kennw&ouml;rter stimmen nicht &uuml;berein.");

function testPassword(passwd, passwd_again, submit, strict) {
	passwordInputId = passwd;
	passwordAgainInputId = passwd_again;
	submitButtonId = submit;
	
	if(strict == null) strict = false;
	
	var pwdValue = document.getElementById(passwordInputId).value;
	var pwdAgain = document.getElementById(passwordAgainInputId).value;
	
	// password field is empty -> dont display any messages and return
	if(pwdValue.length == 0) {
		if(strict) {
			// password field empty
			if(document.getElementById(submitButtonId))
				document.getElementById(submitButtonId).setAttribute("disabled", "disabled");
		} else {
			if(document.getElementById(submitButtonId))
				document.getElementById(submitButtonId).removeAttribute("disabled");
		}
		// display no message about password strength
		passwordNone();
		charactersOk();
		compareNone();
		return;
	}

	var intScore   = 0;
	var strVerdict = "weak";
	var strLog     = "";

	// 4 = strong, <4 = weak
	var strength = 0;
	if(pwdValue.length >= 8) {
		strength++;
	}
	if (pwdValue.match(/[a-z]/)) {
		strength++;
	}
	if (pwdValue.match(/[A-Z]/)) {
		strength++;
	}
	if (pwdValue.match(/[0-9]/)) {
		strength++;
	}
	// allowed
	var allowed = 1;
	var match = pwdValue.match(/[a-zA-Z0-9]*/);
	if(match != pwdValue && match != null) allowed = 0;
	
	// only possible combination, when the password is correct 
	if((allowed == 1 && strength >= 4) || pwdValue.length == 32) {
		passwordStrong();
		charactersOk();
		if(pwdValue == pwdAgain) {
			compareOk();
			if(document.getElementById(submitButtonId))
				document.getElementById(submitButtonId).removeAttribute("disabled");
		} else {
			compareNot();
			if(document.getElementById(submitButtonId))
				document.getElementById(submitButtonId).setAttribute("disabled", "disabled");
		}
	} else {
		passwordWeak();
		compareNone();
		if(allowed == 0) charactersWrong();
		else charactersOk();
		if(document.getElementById(submitButtonId))
			document.getElementById(submitButtonId).setAttribute("disabled", "disabled");
	}
}

// helping functions

function charactersWrong() {
	document.getElementById('password_alert').className = "password-alert-alert";
	document.getElementById('password_alert').alt = strengthInfo[2];
	document.getElementById('password_alert').title = strengthInfo[2];
	document.getElementById('password_alert_text').innerHTML = strengthInfo[2];
	document.getElementById('password_alert_text').className = "password-error";
}

function charactersOk() {
	document.getElementById('password_alert').className = "password-alert-none";
	document.getElementById('password_alert').alt = "";
	document.getElementById('password_alert').title = "";
	document.getElementById('password_alert_text').innerHTML = "";
	document.getElementById('password_alert_text').className = "";
}

function passwordNone() {
	document.getElementById('password_strength').className = "";
	document.getElementById('password_strength').alt = "";
	document.getElementById('password_strength').title = "";
	document.getElementById('password_strength_text').innerHTML = "";
	document.getElementById('password_strength_text').className = "";
}

function passwordStrong() {
	document.getElementById('password_strength').className = "password-strength-strong";
	document.getElementById('password_strength').alt = strengthInfo[0];
	document.getElementById('password_strength').title = strengthInfo[0];
	document.getElementById('password_strength_text').innerHTML = strengthInfo[0];
	document.getElementById('password_strength_text').className = "";
}

function passwordWeak() {
	document.getElementById('password_strength').className = "password-strength-weak";
	document.getElementById('password_strength').alt = strengthInfo[1];
	document.getElementById('password_strength').title = strengthInfo[1];
	document.getElementById('password_strength_text').innerHTML = strengthInfo[1];
	document.getElementById('password_strength_text').className = "password-error";
}

function compareNone() {
	document.getElementById('password_compare').className = "password-compare-none";
	document.getElementById('password_compare').alt = "";
	document.getElementById('password_compare').title = "";
	document.getElementById('password_compare_text').innerHTML = "";
	document.getElementById('password_compare_text').className = "";
}

function compareOk() {
	document.getElementById('password_compare').className = "password-compare-ok";
	document.getElementById('password_compare').alt = compareInfo[0];
	document.getElementById('password_compare').title = compareInfo[0];
	document.getElementById('password_compare_text').innerHTML = compareInfo[0];
	document.getElementById('password_compare_text').className = "";
}

function compareNot() {
	document.getElementById('password_compare').className = "password-compare-not";
	document.getElementById('password_compare').alt = compareInfo[1];
	document.getElementById('password_compare').title = compareInfo[1];
	document.getElementById('password_compare_text').innerHTML = compareInfo[1];
	document.getElementById('password_compare_text').className = "password-error";
}



