var shortPass		= 'Too short';
var badPass			= 'Bad';
var goodPass		= 'Good';
var strongPass		= 'Strong';
var userMatchPass	= 'Pass matches username';
var passMatch		= 'Password does not match';

function passwordStrength( p1, p2, names ) {
	var score = 0;

	if( p1.length < 8 )		return shortPass;
//	if( p2 && p1 != p2 )	return passMatch;

	var low = p1.toLowerCase();
	$( names ).each(function(k,v){
		if( v.toLowerCase && low == v.toLowerCase() )	
			return userMatchPass;
	});

	//password length
	score += p1.length * 4;
	for( var i = 1; i < 5; i++ )
		score += ( checkRepetition(i,p1).length - p1.length ) * 1;

	if( /(.*[0-9].*[0-9].*[0-9])/.test(p1) )					score += 5;		// has 3 numbers
	if( /(.*[!@#$%^&*?_~].*[!@#$%^&*?_~])/.test(p1) )			score += 5;		// has 2 symbols
	if( /([a-z].*[A-Z])|([A-Z].*[a-z])/.test(p1) )				score += 10;	// has upper and lower chars
	if( /([a-zA-Z])/.test(p1) && /([0-9])/.test(p1) )			score += 15;	// has number and chars
	if( /([!@#$%^&*?_~])/.test(p1) && /([0-9])/.test(p1) )		score += 15;	// has number and symbol
	if( /([!@#$%^&*?_~])/.test(p1) && /([a-zA-Z])/.test(p1) )	score += 15;	// has char and symbol
	if( /^\w+$/.test(p1) || /^\d+$/.test(p1) )					score -= 10;	// is just a numbers or chars

	if( score < 34 ) return badPass;
	if( score < 68 ) return goodPass;

	return strongPass
}

// checkRepetition(1,'aaaaaaabcbc')		= 'abcbc'
// checkRepetition(2,'aaaaaaabcbc')		= 'aabc'
// checkRepetition(2,'aaaaaaabcdbcd')	= 'aabcd'

function checkRepetition( pLen, str ) {
	var res = "";
	for( i=0; i<str.length ; i++ ) {
		var 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;
}
