//TODO save points in attribute
window.pStrengthOptions = window.pStrengthOptions || {};
pStrengthOptions.password = {	
   'defaults' : {
      'minChar': 5,
      'colors': ["#f16e30", "#f9b45b", "#f9d877", "#f0f480", "#bbe268", "#81cb4d"],
      'scores': [10, 22, 27, 32, 40],
      'raisePower': 1.4
   },'ruleScores' : {
      'length': 0,
      'lowercase': 2,
      'uppercase': 2,
      'one_number': 3,
      'three_numbers': 5,
      'one_special_char': 3,
      'two_special_char': 5,
      'upper_lower_combo': 2,
      'letter_number_combo': 3,
      'letter_number_char_combo': 2
   },'rules' : {
      'length': true,
      'lowercase': true,
      'uppercase': true,
      'one_number': true,
      'three_numbers': true,
      'one_special_char': true,
      'two_special_char': true,
      'upper_lower_combo': true,
      'letter_number_combo': true,
      'letter_number_char_combo': true
   },'validationRules': {
      'length': function (word, score) {
         pStrengthOptions.password.tooShort = false;
         var wordlen = word.length;
         var lenScore = Math.pow(wordlen, pStrengthOptions.password.options.raisePower);
         if (wordlen < pStrengthOptions.password.options.minChar) {
            lenScore = (lenScore - 100);
            pStrengthOptions.password.tooShort = true;
         }
         return lenScore;
      },'lowercase': function (word, score) {
         return word.match(/[a-z]/) && score;
      },'uppercase': function (word, score) {
         return word.match(/[A-Z]/) && score;
      },'one_number': function (word, score) {
         return word.match(/\d+/) && score;
      },'three_numbers': function (word, score) {
         return word.match(/(.*[0-9].*[0-9].*[0-9])/) && score;
      },'one_special_char': function (word, score) {
         return word.match(/.[!,@,#,$,%,\^,&,*,?,_,~]/) && score;
      },'two_special_char': function (word, score) {
         return word.match(/(.*[!,@,#,$,%,\^,&,*,?,_,~].*[!,@,#,$,%,\^,&,*,?,_,~])/) && score;
      },'upper_lower_combo': function (word, score) {
         return word.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/) && score;
      },'letter_number_combo': function (word, score) {
         return word.match(/([a-zA-Z])/) && word.match(/([0-9])/) && score;
      },'letter_number_char_combo' : function (word, score) {
         return word.match(/([a-zA-Z0-9].*[!,@,#,$,%,\^,&,*,?,_,~])|([!,@,#,$,%,\^,&,*,?,_,~].*[a-zA-Z0-9])/) && score;
      }
   },'attachWidget': function (element) {
   },'addRule': function (name, method, score, active) {
      pStrengthOptions.password.rules[name] = active;
      pStrengthOptions.password.ruleScores[name] = score;
      pStrengthOptions.password.validationRules[name] = method;
      return true;
   },'init': function (element, options) {
      pStrengthOptions.password.options = jQuery.extend({}, pStrengthOptions.password.defaults, options);
      pStrengthOptions.password.attachWidget(element);
      jQuery(element).keyup(function () {
         pStrengthOptions.password.calculateScore(jQuery(this).val(),jQuery(this));
      });
   },'calculateScore': function (word,usedInputBox) {
      pStrengthOptions.password.totalscore = 0;
      pStrengthOptions.password.width = 0;
      for (var key in pStrengthOptions.password.rules) if (pStrengthOptions.password.rules.hasOwnProperty(key)) {
         if (pStrengthOptions.password.rules[key] === true) {
            var score = pStrengthOptions.password.ruleScores[key];
            var result = pStrengthOptions.password.validationRules[key](word, score);
            if (result) {
               pStrengthOptions.password.totalscore += result;
            }
         }
         var bgColor = '';
         var scores = pStrengthOptions.password.options.scores;
         var total = pStrengthOptions.password.totalscore;
         var colors = pStrengthOptions.password.options.colors;
         if (total <= scores[0])
            bgColor = colors[0];
         else {
            for (var i=0; i < (scores.length-1); i++) {
               if (total > scores[i] && total <= scores[(i+1)]) {
                  bgColor = colors[(i+1)];
               }
            }
         }
         if (bgColor == '')
            bgColor = colors[(colors.length-1)];
         jQuery(usedInputBox).css({'background-color': bgColor});
      }
   }
};
jQuery.extend(jQuery.fn, {
   'pstrength': function (options) {
      return this.each(function () {
         pStrengthOptions.password.init(this, options);
      });
   }
});
jQuery.extend(jQuery.fn.pstrength, {
   'addRule': function (name, method, score, active) {
      pStrengthOptions.password.addRule(name, method, score, active);
      return true;
   },'changeScore': function (rule, score) {
      pStrengthOptions.password.ruleScores[rule] = score;
      return true;
   },'ruleActive': function (rule, active) {
      pStrengthOptions.password.rules[rule] = active;
      return true;
   }
});

