/* ************************************************************
    Portable by Marius (written by Steve Moitozo)
Created: 20060120
Author:  Steve Moitozo <god at zilla dot us>
Description: This is a quick and dirty password quality meter 
		 written in JavaScript so that the password does 
		 not pass over the network.
License: MIT License (see below)
Modified: 20060620 - added MIT License

---------------------------------------------------------------
Copyright (c) 2006 Steve Moitozo <god at zilla dot us>

Permission is hereby granted, free of charge, to any person 
obtaining a copy of this software and associated documentation 
files (the "Software"), to deal in the Software without 
restriction, including without limitation the rights to use, 
copy, modify, merge, publish, distribute, sublicense, and/or 
sell copies of the Software, and to permit persons to whom the 
Software is furnished to do so, subject to the following 
conditions:

   The above copyright notice and this permission notice shall 
be included in all copies or substantial portions of the 
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 
OR OTHER DEALINGS IN THE SOFTWARE. 
---------------------------------------------------------------


*/

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

  // 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; "
  }


var strength = intScore;
var strengthText = "";

  if (strength == 0) {
    strengthText = "";
    document.getElementById(nam).style.backgroundColor="#CCCCCC";
  } else if (strength < 20) {
    strengthText = "Sehr unsicher";
    document.getElementById(nam).style.backgroundColor="#FF0000";
  } else if (strength < 30) {
    strengthText = "Unsicher";
    document.getElementById(nam).style.backgroundColor="#FFA500";
  } else if (strength < 60) {
    strengthText = "OK";
    document.getElementById(nam).style.backgroundColor="#E5FF3D";
  } else if (strength < 85) {
    strengthText = "Stark";
    document.getElementById(nam).style.backgroundColor="#A1FF3D";
  } else {
    strengthText = "Sehr Stark";
    document.getElementById(nam).style.backgroundColor="#00FF00";
  }

  document.getElementById(nam).innerHTML="<strong>"+strengthText+"</strong>";
}
