var PasswordMeter = new Class({
							  
	initialize : function(el){
		
		// set up element
		this.el = $(el);
		
		// add keyup event
		this.el.addEvent('keyup',this.updateMeter.bindWithEvent(this));
		
		// focus event
		this.el.addEvent('focus',function(e) {
			this.objMeter.addClass('strengthMeter-focus');
		}.bindWithEvent(this));
		
		// blur event
		this.el.addEvent('blur',function(e) {
			this.objMeter.removeClass('strengthMeter-focus');
		}.bindWithEvent(this));
		
		this.objMeter = new Element('div',{'class':'strengthMeter'});
		this.objMeter.inject(this.el,'after');
		
		this.scoreBar = new Element('div',{'class':'scoreBar'});
		this.objMeter.adopt(this.scoreBar);
		
	},
		
	updateMeter : function(e){
		var score = 0 
		var p = e.target.value;
		
		var maxWidth = this.objMeter.getSize().x - 2;
		
		var nScore = this.calcStrength(p);
		
		// Set new width
		var nRound = Math.round(nScore * 2);

		if (nRound > 100) {
			nRound = 100;
		}

		var scoreWidth = (maxWidth / 100) * nRound;
		this.scoreBar.tween('width',scoreWidth);
	},

	calcStrength: function(p) {
		var intScore = 0;

		// PASSWORD LENGTH
		intScore += p.length;
		
		if(p.length > 0 && p.length <= 4) {                    // length 4 or less
			intScore += p.length;
		}
		else if (p.length >= 5 && p.length <= 7) {	// length between 5 and 7
			intScore += 6;
		}
		else if (p.length >= 8 && p.length <= 15) {	// length between 8 and 15
			intScore += 12;
			//alert(intScore);
		}
		else if (p.length >= 16) {               // length 16 or more
			intScore += 18;
			//alert(intScore);
		}
		
		// LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
		if (p.match(/[a-z]/)) {              // [verified] at least one lower case letter
			intScore += 1;
		}
		if (p.match(/[A-Z]/)) {              // [verified] at least one upper case letter
			intScore += 5;
		}
		// NUMBERS
		if (p.match(/\d/)) {             	// [verified] at least one number
			intScore += 5;
		}
		if (p.match(/.*\d.*\d.*\d/)) {            // [verified] at least three numbers
			intScore += 5;
		}
		
		// SPECIAL CHAR
		if (p.match(/[!,@,#,$,%,^,&,*,?,_,~]/)) {           // [verified] at least one special character
			intScore += 5;
		}
		// [verified] at least two special characters
		if (p.match(/.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~]/)) {
			intScore += 5;
		}
		
		// COMBOS
		if (p.match(/(?=.*[a-z])(?=.*[A-Z])/)) {        // [verified] both upper and lower case
			intScore += 2;
		}
		if (p.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/)) { // [verified] both letters and numbers
			intScore += 2;
		}
		// [verified] letters, numbers, and special characters
		if (p.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!,@,#,$,%,^,&,*,?,_,~])/)) {
			intScore += 2;
		}

		return intScore;
	
	}
	
});
