// JavaScript Document
// Password strength meter v1.0
// Matthew R. Miller - 2007
// www.codeandcoffee.com
// Based off of code from  http://www.intelligent-web.co.uk

// Settings
// -- Toggle to true or false, if you want to change what is checked in the password
function PasswordSet(options)
{
	this._shortPass = 'Too short'
	this._badPass = 'Bad'
	this._goodPass = 'Good'
	this._strongPass = 'Strong'
	this._minimumLength = 4;

	
	if(!!options.password_field)
	{
		if(typeof(options.password_field)=='string')
			this._password_field = document.getElementById(options.password_field);
		else
			this._password_field = options.password_field;
		addEvent(this._password_field, 'keyup', function()
		{
			this.runPassword(this._password_field.value);
		}.bind(this));
	}
	
	if(!!options.confirm_password_field)
	{
		if(typeof(options.confirm_password_field)=='string')
			this._confirm_password_field = document.getElementById(options.confirm_password_field);
		else
			this._confirm_password_field = options.confirm_password_field;
			
		addEvent(this._confirm_password_field, 'keyup', function()
		{
			this.confirmPassword(this._confirm_password_field.value);
		}.bind(this));
	}
	
	if(!!options.meter_div)
	{
		if(typeof(options.meter_div)=='string')
			this._meter_div = document.getElementById(options.meter_div);
		else
			this._meter_div = options.meter_div;
			
		this._meter_div.className = 'password_meter';
		
		this._meter_bar_container = document.createElement('div');
		this._meter_bar_container.className = 'password_meter_bar_container';
		
		this._meter_bar = document.createElement('div');
		this._meter_bar.className = 'password_meter_bar';
		this._meter_bar_container.appendChild(this._meter_bar);
		
		this._meter_text = document.createElement('div');
		this._meter_text.className = 'password_meter_text';
		this._meter_text.innerHTML = this._shortPass;
		
		this._meter_div.appendChild(this._meter_text);
		this._meter_div.appendChild(this._meter_bar_container);
		
	}

}

// Check password
PasswordSet.prototype.checkPassword = function (password)
{
	score = 0 
    
    //password < 4
    if (password.length < this._minimumLength ) { return this._shortPass }
    
    //password length
    score += password.length * 4
    score += ( this.checkRepetition(1,password).length - password.length ) * 1
    score += ( this.checkRepetition(2,password).length - password.length ) * 1
    score += ( this.checkRepetition(3,password).length - password.length ) * 1
    score += ( this.checkRepetition(4,password).length - password.length ) * 1

    //password has 3 numbers
    if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5 
    
    //password has 2 sybols
    if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5 
    
    //password has Upper and Lower chars
    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10 
    
    //password has number and chars
    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15 
    //
    //password has number and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15 
    
    //password has char and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15 
    
    //password is just a nubers or chars
    if (password.match(/^\w+$/) || password.match(/^\d+$/) )  score -= 10 
    
    //verifing 0 < score < 100
    if ( score < 0 )  score = 0 
    if ( score > 100 )  score = 100 
    
    if (score < 34 )  return this._badPass 
    if (score < 68 )  return this._goodPass
    return this._strongPass
}
 
// Runs password through check and then updates GUI 
PasswordSet.prototype.runPassword = function () 
{
	// Check password
	strength = this.checkPassword(this._password_field.value);
	 // Get controls
    	var ctlBar = this._meter_bar;
    	var ctlText = this._meter_text;
    	if (!ctlBar || !ctlText)
    		return;
    	
    	
 	if(strength == this._shortPass)
 	{
 		strColor = "gray";
		width = "0%";
 	}
 	else if(strength == this._badPass)
 	{
 		strColor = "red";
		width = "33%";
	}
 	else if (strength == this._goodPass)
 	{
 		strColor = "#DBE00A";
		width = "67%";
 	}
 	else
 	{
 		strColor = "#3bce08";
		width = "118px";
 	}
	ctlBar.style.backgroundColor = strColor;
	ctlBar.style.width = width;
	
	ctlText.innerHTML =  strength;
}
 
PasswordSet.prototype.confirmPassword = function () 
{
	var password = this._password_field.value;
	var confirm_password = this._confirm_password_field.value;
	
	if(password!=confirm_password)
		this._meter_text.innerHTML = 'Passwords don\'t match';
	else
		this._meter_text.innerHTML = 'Passwords match';
}
 
PasswordSet.prototype.checkRepetition = function (pLen,str) 
{
    res = ""
    for ( i=0; i<str.length ; i++ ) 
	{
        repeated=true
        for (j=0;j < pLen && (j+i+pLen) < str.length;j++)
            repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen))
        if (j<pLen) repeated=false
        if (repeated) 
		{
            i+=pLen-1
            repeated=false
        }
        else 
		{
            res+=str.charAt(i)
        }
    }
    return res
}
