/* ************************************************************
Password Strength Factors and Weightings

password length:
level 0 (3 point): less than 4 characters
level 1 (6 points): between 5 and 7 characters
level 2 (12 points): between 8 and 15 characters
level 3 (18 points): 16 or more characters

letters:
level 0 (0 points): no letters
level 1 (5 points): all letters are lower case
level 2 (7 points): letters are mixed case

numbers:
level 0 (0 points): no numbers exist
level 1 (5 points): one number exists
level 1 (7 points): 3 or more numbers exists

special characters:
level 0 (0 points): no special characters
level 1 (5 points): one special character exists
level 2 (10 points): more than one special character exists

combinatons:
level 0 (1 points): letters and numbers exist
level 1 (1 points): mixed case letters
level 1 (2 points): letters, numbers and special characters 
					exist
level 1 (2 points): mixed case letters, numbers and special 
					characters exist

*/
// Returns a numeric score between 0 and 100 representing password strength
function passwordStrength(passwd, username) {
  var intScore   = 0;
  var strLog     = "";

  if (passwd == username)               // Password matches username. Not good.
  {
    intScore = 1;
    return intScore;
  }

  // PASSWORD LENGTH
  if ((passwd.length > 0) && (passwd.length <= 4))        // length 4 or less
  {
    intScore = 2;
    strLog   = strLog + "2 points for length (" + passwd.length + "); ";
  }
  else if ((passwd.length>=5) && (passwd.length<=7))      // length 5 to 7
  {
    intScore = passwd.length / 0.6;
    strLog   = strLog + intScore + " points for length (" + passwd.length + "); ";
  }
  else if ((passwd.length>=8) && (passwd.length<=10))     // length 8 to 10
  {
    intScore = (passwd.length * 1.5) / 0.6;
    strLog   = strLog + intScore + " points for length (" + passwd.length + "); ";
  }
  else if (passwd.length >= 11)                           
  {
    intScore = 25;
    strLog   = strLog + intScore + " points for length (" + passwd.length + "); ";
  } 

  
  if (passwd.match(/[a-z]/))                              // [verified] at least one lower case letter
  {
    intScore = (intScore + 2)
    strLog   = strLog + "2 point for at least one lower case char; "
  }

  if (passwd.match(/[A-Z]/))                              // [verified] at least one upper case letter
  {
    intScore = (intScore + 5)
    strLog   = strLog + "5 point for at least one upper case char; "
  }

  // NUMBERS
  if (passwd.match(/\d+/))                                 // [verified] at least one number
  {
    intScore = (intScore + 5)
    strLog   = strLog + "5 points for at least one number; "
  }

  if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
  {
    intScore = (intScore + 13)
    strLog   = strLog + "13 points for at least three numbers; "
  }


  // SPECIAL CHAR
  if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
  {
    intScore = (intScore + 8)
    strLog   = strLog + "8 points for at least one special char; "
  }

                              // [verified] at least two special characters
  if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
  {
    intScore = (intScore + 11)
    strLog   = strLog + "11 points for at least two special chars; "
  }


  // COMBOS
  if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
  {
    intScore = (intScore + 8)
    strLog   = strLog + "8 points for upper and lower combo; "
  }

  if (passwd.match(/(\d.*\D)|(\D.*\d)/))                    // [FAILED] both letters and numbers, almost works because an additional character is required
  {
    intScore = (intScore + 10)
    strLog   = strLog + "10 points for letter and number combo; "
  }

                                // [verified] letters, numbers, and special characters
  if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
  {
    intScore = (intScore + 13)
    strLog   = strLog + "13 points for letter, number and special char combo; "
  }

//  psDebug(intScore + " : " + strLog);

  return intScore;
}

function showStrength(strength) {
  var strengthText = "";

  if (strength == 0) {
    strengthText = "Password Strength";
  } else if (strength < 20) {
    strengthText = "Very weak";
  } else if (strength < 30) {
    strengthText = "Weak";
  } else if (strength < 60) {
    strengthText = "Moderate";
  } else if (strength < 85) {
    strengthText = "Strong";
  } else {
    strengthText = "Very strong";
  }

  updateBar(strength, 'barMask', 'barBG', 'barText', strengthText);
}

function updateBar(val, maskId, bgId, textId, newHTML) {
  // val should be between 0-100
  
  barWidth = parseInt(document.getElementById(bgId).style.width);
  maskWidth = barWidth - (barWidth * (val/100));

  document.getElementById(maskId).style.width = maskWidth + "px";
  if (!newHTML) {
    document.getElementById(textId).innerHTML = Math.floor((curLeft / parseInt(document.getElementById(bgId).offsetWidth))*100) + "%";
  } else {
    document.getElementById(textId).innerHTML = newHTML;
  }
}

// ======================= Show Layer Functions ==============================
function showLayer(whichLayer, isVisible, altDisplayMode) {
  var displayMode = "block";
  if (altDisplayMode) {
    displayMode = altDisplayMode;
  }

  var theLayer ;
  if (document.getElementById) {
    // this is the way the standards work
    theLayer = document.getElementById(whichLayer) ;

  } else if (document.all) {
    // this is the way old msie versions work
    theLayer = document.all[whichLayer] ;

  } else if (document.layers) {
    // this is the way nn4 works
    theLayer = document.layers[whichLayer] ;
  }

  if ( theLayer ) {
    var style2 = theLayer.style;
    if (isVisible) {
      style2.display = displayMode;
    } else {
      style2.display = "none";
    }
  }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function(){showLayer('barContainer', 1)});
