
var bCheckNumbers = true;
var bCheckLetters = true;
var bCheckCharacter = true;
var totalPoints = 0;
var nCombinations = 0;

//calculate Points
function calcPoints(number,number2,type) {
  var points = 0;
  
  if(type =='number') {
    if(number > 0)
      nCombinations++;
      
    if(number < 3)
      points = 10;
    if(number >= 3)
      points = 20;
  }
  
  if(type =='letter') {
    if(number > 0)
      nCombinations++;
      
    if(number > 0 || number2 > 0)
      points = 10;
    if(number > 0 && number2 > 0)
      points = 20;
  }
  if(type == 'char') {
    if(number > 0)
      nCombinations++;
      
    if(number > 0)
      points = 10;
    if(number > 1)
      points = 25;
  }
  if(type == "length") {
    if(number <= 4)
      points = 5;
    if(number > 4)
      points = 10;
    if(number > 7)
      points = 25;
  }
  if(type == "bonus") {  
    if(number > 1)
      points = 2;
    if(number > 2)
      points = 3;
  }
  
  return points;
}
// Check password
function checkPassword(strPassword)
{
// Reset combination count
nCombinations = 0;
totalPoints = 0;

//Check length
totalPoints += calcPoints(strPassword.length,0,"length");

// Check numbers
if (bCheckNumbers)
{
  nNumberCount = strPassword.replace(/[^0-9]/g,'').length;
  totalPoints += calcPoints(nNumberCount,0,"number");
}

// Check letters
if (bCheckLetters)
{
  nLowerCount = strPassword.replace(/[^a-z]/g,'').length;
  nUpperCount = strPassword.replace(/[^A-Z]/g,'').length;
  totalPoints += calcPoints(nLowerCount,nUpperCount,"letter");
}

// Check punctuation
if (bCheckCharacter)
{
  nCharacterCount = strPassword.replace(/[^!@#$%^&*?_~]/g,'').length;
  totalPoints += calcPoints(nCharacterCount,0,"char");
}

//check bonus
totalPoints += calcPoints(nCombinations,0,"bonus"); 
return totalPoints;
}

// Runs password through check and then updates GUI
function runPassword(strPassword, strFieldID)
{
// Check password
nRound = 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 < (strPassword.length * 5))
{
nRound += strPassword.length * 5;
}*/

if (nRound > 100)
  nRound = 100;


// Color and text
if(strPassword.length >= 6) {
ctlBar.style.width = nRound + "%";

  if (nRound > 90)
  {
    strText = "Very Secure";
    strColor = "#339900";
  }
  else if (nRound >= 80)
  {
    strText = "Secure";
    strColor = "#339900";
  }
  else if (nRound >= 70)
  {
    strText = "Very strong";
    strColor = "#33cc00";
  }
  else if (nRound >= 60)
  {
    strText = "Strong";
    strColor = "#66ff00";
  }
  else if (nRound >= 50)
  {
    strText = "Average";
    strColor = "#ffff00";
  }
  else if (nRound >= 25)
  {
    strText = "Weak";
    strColor = "#cc6600";
  }
  else 
  {
    strText = "Very weak";
    strColor = "#e50000";
  }
  
  ctlBar.style.visibility = "visible";
}
else
{
  ctlBar.style.width = 0+"%";
  strText = "Too short";
  strColor = "#e50000";
  ctlBar.style.visibility = "hidden";
}
ctlBar.style.backgroundColor = strColor;
ctlText.innerHTML = "" + strText + "";
}

// Checks a string for a list of characters
function doesContain(strPassword, strCheck)
{
nCount = 0;

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

return nCount;
}
