// Check password
function checkPassword(strPassword) {
	nValue = 0;
	nBit = 0;
	nArrays = 0;

	ContainNumbers = doesContain(strPassword, "0123456789");
	nValue += ContainNumbers * 1;
	nArrays += (ContainNumbers ? 1 : 0);

	ContainUpperCase = doesContain(strPassword, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	nValue += ContainUpperCase * 2;
	nArrays += (ContainUpperCase ? 1 : 0);

	ContainLowerCase = doesContain(strPassword, "abcdefghijklmnopqrstuvwxyz");
	nValue += ContainLowerCase * 1;
	nArrays += (ContainLowerCase ? 1 : 0);

	ContainPunctuation = doesContain(strPassword, ";:-_=+\|//?^&!.@$?#*()%~<>{}[]");
	nValue += ContainPunctuation * 3;
	nArrays += (ContainPunctuation ? 1 : 0);

	// 2 arrays = bonus 5
	if(nArrays>1) nValue += 5;
	if(nArrays>2) nValue += 10;
	if(nArrays>3) nValue += 15;

	if(strPassword.length>5){
		nValue += strPassword.length*2;
	}
	if(strPassword.length>10){
		nValue += strPassword.length*3;
	}
	if(strPassword.length>15){
		nValue += strPassword.length*4;
	}


	return nValue/100;
}

function runPassword(strPassword, strFieldID, MinLength) {
	// Check password
	nPerc = checkPassword(strPassword);

	 // Get controls
	var ctlBar = document.getElementById(strFieldID + "_bar");
	var ctlText = document.getElementById(strFieldID + "_text");
	if (!ctlBar || !ctlText)
		return;

	// Set new width
	var nRound = Math.round(nPerc * 100);
	if (nRound > 100)
		nRound = 100;

	if(strPassword.length<MinLength) {
		nRound = 0;
	}

	ctlBar.style.width = nRound + "%";

	// Color and text
	if (nRound > 95)
	{
		strText = "Bardzo bezpieczne";
		strColor = "#3bce08";
	}
	else if (nRound > 75)
	{
		strText = "Bezpieczne";
		strColor = "orange";
	}
	else if (nRound > 50)
	{
		strText = "Średne";
		strColor = "#ffd801";
	}
	else
	{
		strColor = "red";
		strText = "Słabe";
	}

	if(strPassword.length<MinLength) {
		strColor = "#CCCCCC";
		strText = "Za krótkie";
	}
	ctlBar.style.backgroundColor = strColor;
	ctlText.innerHTML = "<div style='float:left'>Siła hasła:</div> <div align=right style='float:none;color: " + strColor + ";'>" + strText + "</div>";
}

function doesContain(strPassword, strCheck) {
	nCount = 0;

	for (i = 0; i < strPassword.length; i++)
	{
		if (strCheck.indexOf(strPassword.charAt(i)) > -1)
		{
			nCount++;
		}
	}

	return nCount;
}

// Check password
function checkPasswordEn(strPassword) {
	nValue = 0;
	nBit = 0;
	nArrays = 0;

	ContainNumbers = doesContain(strPassword, "0123456789");
	nValue += ContainNumbers * 1;
	nArrays += (ContainNumbers ? 1 : 0);

	ContainUpperCase = doesContain(strPassword, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	nValue += ContainUpperCase * 2;
	nArrays += (ContainUpperCase ? 1 : 0);

	ContainLowerCase = doesContain(strPassword, "abcdefghijklmnopqrstuvwxyz");
	nValue += ContainLowerCase * 1;
	nArrays += (ContainLowerCase ? 1 : 0);

	ContainPunctuation = doesContain(strPassword, ";:-_=+\|//?^&!.@$?#*()%~<>{}[]");
	nValue += ContainPunctuation * 3;
	nArrays += (ContainPunctuation ? 1 : 0);

	// 2 arrays = bonus 5
	if(nArrays>1) nValue += 5;
	if(nArrays>2) nValue += 10;
	if(nArrays>3) nValue += 15;

	if(strPassword.length>5){
		nValue += strPassword.length*2;
	}
	if(strPassword.length>10){
		nValue += strPassword.length*3;
	}
	if(strPassword.length>15){
		nValue += strPassword.length*4;
	}


	return nValue/100;
}

function runPasswordEn(strPassword, strFieldID, MinLength) {
	// Check password
	nPerc = checkPasswordEn(strPassword);

	 // Get controls
	var ctlBar = document.getElementById(strFieldID + "_bar");
	var ctlText = document.getElementById(strFieldID + "_text");
	if (!ctlBar || !ctlText)
		return;

	// Set new width
	var nRound = Math.round(nPerc * 100);
	if (nRound > 100)
		nRound = 100;

	if(strPassword.length<MinLength) {
		nRound = 0;
	}

	ctlBar.style.width = nRound + "%";

	// Color and text
	if (nRound > 95)
	{
		strText = "Very safe";
		strColor = "#3bce08";
	}
	else if (nRound > 75)
	{
		strText = "Safe";
		strColor = "orange";
	}
	else if (nRound > 50)
	{
		strText = "Medium";
		strColor = "#ffd801";
	}
	else
	{
		strColor = "red";
		strText = "Weak";
	}

	if(strPassword.length<MinLength) {
		strColor = "#CCCCCC";
		strText = "Too short";
	}
	ctlBar.style.backgroundColor = strColor;
	ctlText.innerHTML = "<div style='float:left'>Password strength:</div> <div align=right style='float:none;color: " + strColor + ";'>" + strText + "</div>";
}

function doesContainEn(strPassword, strCheck) {
	nCount = 0;

	for (i = 0; i < strPassword.length; i++)
	{
		if (strCheck.indexOf(strPassword.charAt(i)) > -1)
		{
			nCount++;
		}
	}

	return nCount;
}


