// Anzahl möglicher Versuche das Passwort zu Knacken pro Sekunde
var nCrackrate = 1000;

// Schwellwert, ab wann das Passwort als sicher gilt
var nTreshholddays = 365 * 10; 

// Anzahl der Schritte, die im CSS angehangen werden
var nSteps = 2; 


function contains(strText, strPattern) 
{
	for (i = 0; i < strText.length; i++) 
	{
		if (strPattern.indexOf(strText.charAt(i)) > -1) {
			return true;
		}
	}
	return false;
}

function checkSecurePassword(strPass) {

	nCombinationCount = 0;

	// überprüfen ob Ziffern vormmen
	strToCheck = "0123456789";
	
	if (contains(strPass, strToCheck)) {
		nCombinationCount += strToCheck.length;
	}

	// überprüfen ob kleine Buchstaben vorkommen
	strToCheck = "abcdefghijklmnopqrstuvwxyz"; 
	
	if (contains(strPass, strToCheck)) {
		nCombinationCount += strToCheck.length;
	}

	// überprüfen ob grosse Buchstaben vorkommen
	strToCheck = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	if (contains(strPass, strToCheck)) {
		nCombinationCount += strToCheck.length;
	}

	// übersprüfen ob Sonderzeichen vorkommen
	strToCheck = ",;:-_=+|//?^&!.@$£#*()%~<>{}[";

	if (contains(strPass, strToCheck)) {
		nCombinationCount += strToCheck.length;
	}
	
	// Wieviele Tage benötigt man das passwort zu knacken
	var nDays = ((Math.pow(nCombinationCount, strPass.length) / nCrackrate) / 2) / 86400; 

	// Stärke errechnen
	var nStrongness = Math.round(nDays / nTreshholddays * 100);
	
	// Zeichenlänge für Stärke berücksichtigen
	if (nStrongness < (strPass.length * 5)){
		nStrongness += strPass.length * 5;
	}

	// Max 100% zulassen
	if (nStrongness > 100){
		nStrongness = 100;
	}
	
	 // Max Schritte
	nStrongness = Math.round(nStrongness / (100 / nSteps));

	// falls was eingegeben wurde anzeigen
	if (strPass.length > 0) {

		if (nStrongness == 0) 
		{
			$("#pwInfoText1").css("visibility", "visible");
			$("#pwInfoText2").css("visibility", "hidden");
			$("#pwInfoText3").css("visibility", "hidden");

			$("#pwInfoColor1").removeClass();
			$("#pwInfoColor1").addClass("pwSecureState pwStrengthColor1");
			$("#pwInfoColor2").removeClass();
			$("#pwInfoColor2").addClass("pwSecureState pwStrengthColor0");
			$("#pwInfoColor3").removeClass();
			$("#pwInfoColor3").addClass("pwSecureState pwStrengthColor0");
		}

		if (nStrongness == 1) 
		{
			$("#pwInfoText1").css("visibility", "hidden");
			$("#pwInfoText2").css("visibility", "visible");
			$("#pwInfoText3").css("visibility", "hidden");

			$("#pwInfoColor1").removeClass();
			$("#pwInfoColor1").addClass("pwSecureState pwStrengthColor2");
			$("#pwInfoColor2").removeClass();
			$("#pwInfoColor2").addClass("pwSecureState pwStrengthColor2");
			$("#pwInfoColor3").removeClass();
			$("#pwInfoColor3").addClass("pwSecureState pwStrengthColor0");
		}

		if (nStrongness == 2) 
		{
			$("#pwInfoText1").css("visibility", "hidden");
			$("#pwInfoText2").css("visibility", "hidden");
			$("#pwInfoText3").css("visibility", "visible");

			$("#pwInfoColor1").removeClass();
			$("#pwInfoColor1").addClass("pwSecureState pwStrengthColor3");
			$("#pwInfoColor2").removeClass();
			$("#pwInfoColor2").addClass("pwSecureState pwStrengthColor3");
			$("#pwInfoColor3").removeClass();
			$("#pwInfoColor3").addClass("pwSecureState pwStrengthColor3");
		}
	} 
	else 
	{
		$("#pwInfoText1").css("visibility", "hidden");
		$("#pwInfoText2").css("visibility", "hidden");
		$("#pwInfoText3").css("visibility", "hidden");

		$("#pwInfoColor1").removeClass();
		$("#pwInfoColor1").addClass("pwSecureState pwStrengthColor0");
		$("#pwInfoColor2").removeClass();
		$("#pwInfoColor2").addClass("pwSecureState pwStrengthColor0");
		$("#pwInfoColor3").removeClass();
		$("#pwInfoColor3").addClass("pwSecureState pwStrengthColor0");
	}
}