/************************************************************
 *                                                          *
 * Php Fusion Register Password Check v1                    *
 * Script Author: Michael Burkhardt, www.b-topia.de         *
 * Angepasst an PHP Fusion: Mr.Devil, http://dug-portal.com *
 *                                                          *
 * Dieses Copyright darf nicht entfernt werden!!            *
 *                                                          *
 ************************************************************/

// Function (getPWScore) Start
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   = 0;

    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 (calcPlainPW) Start
  function calcPlainPW() {
   
    var targetObject = document.getElementById("plainScore");
    var pwText = document.getElementById("plainPW").value;
    var pwScore = getPWScore(pwText);
    
    while(targetObject.firstChild) {
      targetObject.removeChild(targetObject.firstChild);
    }
// alert(pwText + ": " + pwScore);
    var textNode = document.createTextNode(" ");
	targetObject.appendChild(textNode);

// Hier kann die Empfindlichkeit der Check Funktion eingestellt werden
	if (pwScore < 1.0) {
	  targetObject.style.backgroundColor = "#CC0000";
	  targetObject.style.border = "solid 1px #700";
	}
	else if (pwScore < 2.0) {
	  targetObject.style.backgroundColor = "#FFE97F";
	  targetObject.style.border = "solid 1px #FFD800";
	}
	else if (pwScore < 3.0) {
	  targetObject.style.backgroundColor = "#0094FF";
	  targetObject.style.border = "solid 1px #0026FF";
	} 
	else {
	  targetObject.style.backgroundColor = "#009900";
	  targetObject.style.border = "solid 1px #007F0E";
	}
  }
  
function psDebug(aMsg) {
  setTimeout(function() { throw new Error("[debug] " + aMsg); }, 0);
}

// Returns a numeric score between 0 and 100 representing password strength
function passwordStrength(passwd) {
  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; "
  }

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

  return intScore;
}


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

  if (strength == 0) {
    strengthText = "Password Strength";
  } else if (strength < 10) {
    strengthText = "Very weak";
  } else if (strength < 20) {
    strengthText = "Weak";
  } else if (strength < 40) {
    strengthText = "Moderate";
  } else if (strength < 50) {
    strengthText = "Strong";
  } else {
    strengthText = "Very strong";
  }

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


// ======================= Password Strength Functions ==============================
/*

 Simple script to update a progress bar. Inspired by:
   http://slayeroffice.com/code/gradientProgressBar/

 Originally authored by steve@slayeroffice.com and licensed under
 the Creative Commons License:
   http://creativecommons.org/licenses/by-nc-sa/1.0/legalcode
*/

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