
/* Password-Checker, functionally version
 * Author: Michael Burkhardt, www.b-topia.de
 * please leave this comment, if you copy this little programm
 */
 
  function getPWScore(source) {

    var score_pw_length      = 6;
    var score_small_letter   = 1;
    var score_digit          = 2;
    var score_capital_letter = 4;
    var score_special_sign   = 8;

    var is_included_small   = false;
    var is_included_digit   = false;
    var is_included_capital = false;
    var is_included_special = false;
    
    var pw_char_score = 0;
    var pw_included_score = 0;
    
    for (var i = 0; i < source.length; i++) {
      var oneChar = source.charAt(i);
      if (oneChar >= "a" && oneChar <= "z") {
        pw_char_score += score_small_letter;
        is_included_small = true;
      }
      else if (oneChar >= "A" && oneChar <= "Z") {
        pw_char_score += score_capital_letter;
        is_included_capital = true;
      }
      else if (oneChar >= "0" && oneChar <= "9") {
        pw_char_score += score_digit;
        is_included_digit = true;
      }
      else {
        pw_char_score += score_special_sign;
        is_included_special = true;
      }
    }
    
    if (is_included_small) {
      pw_included_score += score_small_letter;
    }
    if (is_included_capital) {
      pw_included_score += score_capital_letter;
    }
    if (is_included_digit) {
      pw_included_score += score_digit;
    }
    if (is_included_special) {
      pw_included_score += score_special_sign;
    }
    
    return ( pw_char_score * pw_included_score / ( (score_small_letter + score_digit + score_capital_letter - 1) * score_pw_length ) );
  }
  

  function calcSecPW() {
    var targetObject = document.getElementById("pw_qual");
    var pwText = document.getElementById("secPW").value;
    var pwScore = getPWScore(pwText);
  	//alert(pwText + ": " + pwScore);
  	
  	if (pwScore < 1.0) {
  	  targetObject.style.backgroundImage = "url(pics/pw_1.gif)";
  	  targetObject.title = "Passwortstärke: schwach";
  	}
  	else if (pwScore < 5.0) {
  	  targetObject.style.backgroundImage = "url(pics/pw_2.gif)";
  	  targetObject.title = "Passwortstärke: mäßig";
  	}
  	else if (pwScore < 10.0) {
  	  targetObject.style.backgroundImage = "url(pics/pw_3.gif)";
  	  targetObject.title = "Passwortstärke: gut";
  	} 
  	else {
  	  targetObject.style.backgroundImage = "url(pics/pw_4.gif)";
  	  targetObject.title = "Passwortstärke: sehr gut";
  	}
  	if (pwText == "") {
  	  targetObject.style.backgroundImage = "url(pics/pw_0.gif)";
  	  targetObject.title = "Passwortstärke";
  	}
  	
  }