/**
 * PasswordCheck
 *
 * used in template editpassword.isml
 */
 
var nCrackrate = 1000000; 
var nTreshholddays = 365 * 100;
var nSteps = 10;

function contains(strText, strPattern)
{
	for (i = 0; i < strText.length; i++)
	{
	if (strPattern.indexOf(strText.charAt(i)) > -1) return true;
	}
	return false;
}

function checkPassword(strPass, strId, strClassname, strTextId)
{
	var nCombinationCount = 0;
	var pwdState = 0;

	strToCheck = '0123456789';
	if (contains(strPass, strToCheck)) nCombinationCount += strToCheck.length;
	strToCheck = 'abcdefghijklmnopqrstuvwxyz'; 
	if (contains(strPass, strToCheck)) nCombinationCount += strToCheck.length;
	strToCheck = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
	if (contains(strPass, strToCheck)) nCombinationCount += strToCheck.length;
	strToCheck = ',;:-_=+|//?^&!.@$£*()%~<>{}[]';
	if (contains(strPass, strToCheck)) nCombinationCount += strToCheck.length;
	
	var nDays = ((Math.pow(nCombinationCount, strPass.length) / nCrackrate) / 2) / 86400;
	var nStrongness = Math.round(nDays / nTreshholddays * 100);
	if (nStrongness < (strPass.length * 5)) nStrongness += strPass.length * 5;
	if (nStrongness > 100) nStrongness = 100; 
	nStrongness = Math.round(nStrongness / (100 / nSteps)); 
	
	if (strPass.length < 6 && strPass.length != 0) { 
		nStrongness = 0; 
		pwdState = 1;
	}
	else if (strPass.toLowerCase().indexOf(new String(firstName).toLowerCase()) != -1) { 
		nStrongness = 0; 
		pwdState = 2;
	}
	else if (strPass.toLowerCase().indexOf(new String(lastName).toLowerCase()) != -1) { 
		nStrongness = 0; 
		pwdState = 2;
	}
	else if (strPass.toLowerCase() == new String(loginName).toLowerCase()) { 
		nStrongness = 0; 
		pwdState = 3;
	}
	else { pwdState = 0; }
	
	oId = document.getElementById(strId);
	oId.className = strClassname + '-' + nStrongness;
	
	otId = document.getElementById(strTextId);
	
	if (aTexts)
	{
		if (pwdState == 0) {			
			nKey = Math.round((aTexts.length - 1) / nSteps * nStrongness);
			otId.innerHTML = aTexts[nKey];
		}
		else if (pwdState == 1) { otId.innerHTML = aTextsErrors[0]; }
		else if (pwdState == 2) { otId.innerHTML = aTextsErrors[1]; }
		else if (pwdState == 3) { otId.innerHTML = aTextsErrors[2]; }
	}
}
