(function($) {
	$.widget("ui.dbdpassword", {
		options: {
			imageDir:null,
			weakImage:null,
			moderateImage:null,
			strongImage:null,
			password:'',
			id:''
		},
		properties:{
			'scores' : {
				'length': 3,
				'lowercase': 2,
				'uppercase': 5,
				'one_number': 5,
				'three_numbers': 10,
				'one_special_char': 7,
				'two_special_char': 15,
				'upper_lower_combo': 10,
				'letter_number_combo': 10,
				'letter_number_char_combo': 20
			},
			'validation': {
				'length': function (word, score) {
					var wordlen = word.length;
					return score * wordlen;
				},
				'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;
				}
			},
		},
		/*************
       * Initialize *
       **************/
      _create : function(){
         var self = this;
         $('#'+self.element.attr('id'))
		  	.append($('<input/>',{'type':'password','id':'dbdpassbox-'+self.options.id,'name':'dbdpassbox'})
		  		.val(self.options.password)
		  		.keyup(function(event){self._onKeyUp($(this).val());}))
		  	.append($('<br />'))
		    .append($('<img/>',{'id':'pass_meter-'+self.options.id,'src':self.options.imageDir+'/'+self.options.weakImage}));
		 self._onKeyUp(self.options.password);
      },
      
      _onKeyUp:function(word){
	      var self = this;
	      self._calculate(word);
	   },
	  
	  _calculate: function (word) {
		var self = this;
		var totalscore = 0;
		for(var key in self.properties.scores){
				var score = self.properties.scores[key];
				var result = self.properties.validation[key](word, score);
				if (result) { 
					totalscore += result;
				}
		}
		if(totalscore < 33){
			$('#pass_meter-'+self.options.id).attr('src',self.options.imageDir+'/'+self.options.weakImage);
		}
		else if(totalscore > 33 && totalscore < 66){
			$('#pass_meter-'+self.options.id).attr('src',self.options.imageDir+'/'+self.options.moderateImage);
		}
		else if(totalscore > 66){
			$('#pass_meter-'+self.options.id).attr('src',self.options.imageDir+'/'+self.options.strongImage);
		}
	}
});
})(jQuery);
