/*
var pmContainer = document.getElementById('pmContainer');
var pmGradient = document.getElementById('pmGradient');
var pmMask = document.getElementById('pmMask');
var pmProgressIndicator = document.getElementById('pmProgressIndicator');
var maxScore = 53;
*/

/* ************************************************************
Created: 20060120
Author:  Steve Moitozo <god at zilla dot us> -- geekwisdom.com
(modified by Valentyn Gatsuk)
Description: This is a quick and dirty password quality meter 
		 written in JavaScript so that the password does 
		 not pass over the network.
License: MIT License (see below)
Modified: 20060620 - added MIT License
Modified: 20061111 - corrected regex for letters and numbers
                     Thanks to Zack Smith -- zacksmithdesign.com
---------------------------------------------------------------
Copyright (c) 2006 Steve Moitozo <god at zilla dot us>

Permission is hereby granted, free of charge, to any person 
obtaining a copy of this software and associated documentation 
files (the "Software"), to deal in the Software without 
restriction, including without limitation the rights to use, 
copy, modify, merge, publish, distribute, sublicense, and/or 
sell copies of the Software, and to permit persons to whom the 
Software is furnished to do so, subject to the following 
conditions:

   The above copyright notice and this permission notice shall 
be included in all copies or substantial portions of the 
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 
OR OTHER DEALINGS IN THE SOFTWARE. 
---------------------------------------------------------------


************************************************************ */

Array.prototype.sum = function(){
	for(var i=0,sum=0;i<this.length;sum+=this[i++]);
	return sum;
}

function pwdTest(passwd)
{
	var intScore   = 0
	var strVerdict = ""
	var strLog     = ""

/***************************************************************
Password Strength Factors and Weightings:
***************************************************************/
	/*
	password length:
	level 0 (+3 point): less than 4 characters
	level 1 (+3 points): between 5 and 7 characters
	level 2 (+6 points): between 8 and 15 characters
	*/
	var pwdlength_lvl =		new Array(3, 3, 6);

	/*
	letters:
	level 0 (+3 points): at least one lowercase latter
	level 1 (+3 points): at least one uppercase letter
	level 2 (+8 points): letters are mixed case
	*/
	var letter_lvl =		new Array(3, 3, 8);

	/*
	numbers:
	level 0 (+3 points): at least one number exists
	level 1 (+2 points): two or more numbers exists
	*/
	var number_lvl =		new Array(3, 2);

	/*
	special characters:
	level 0 (+5 points): one special character exists
	level 1 (+2 points): more than one special character exists
	*/
	var specialchr_lvl =	new Array(5, 2);

	/*
	combinatons:
	level 0 (+10 points): letters and numbers exist
	level 1 (+10 points): letters or numbers and special characters exist
	*/
	var combos_lvl =		new Array(10, 10);

	var maxScore = pwdlength_lvl.sum() + letter_lvl.sum() + number_lvl.sum() +
			specialchr_lvl.sum() + combos_lvl.sum();
	
	var pmContainer = document.getElementById('pmContainer');
	var pmGradient = document.getElementById('pmGradient');
	var pmMask = document.getElementById('pmMask');
	var pmProgressIndicator = document.getElementById('pmProgressIndicator');

	// do nothing if any of required dom elements are not present
	if (!(pmContainer && pmGradient && pmMask && pmProgressIndicator) ) 
	{
		return 0;
	}
	
	// PASSWORD LENGTH
	// length 4 or less
	if (passwd.length<5 && passwd.length>0)
	{
		intScore = (intScore+pwdlength_lvl[0]);
		strLog   = strLog + "+" + pwdlength_lvl[0] + 
				" points for length (" + passwd.length + ")\n";
	}
	// length between 5 and 7
	else if (passwd.length>4 && passwd.length<8) 
	{
		intScore = (intScore+pwdlength_lvl[0]+pwdlength_lvl[1]);
		strLog   = strLog + "+" + (pwdlength_lvl[0] + pwdlength_lvl[1]) + 
				" points for length (" + passwd.length + ")\n";
	}
	// length between 8 and 16
	else if (passwd.length>7 )
	{
		intScore = (intScore+pwdlength_lvl.sum() );
		strLog   = strLog + "+" + pwdlength_lvl.sum() + 
				" points for length (" + passwd.length + ")\n";
	}
	
	// LETTERS
	// at least one lower case letter
	if (passwd.match(/[a-z]/))
	{
		intScore = (intScore+letter_lvl[0]);
		strLog   = strLog + "+" + letter_lvl[0] + 
				" points for at least one lower case char\n";
	}
	
	// at least one upper case letter
	if (passwd.match(/[A-Z]/))
	{
		intScore = (intScore+letter_lvl[1]);
		strLog   = strLog + "+" + letter_lvl[1] + 
				" points for at least one upper case char\n";
		}
	
	// both upper and lower case
	if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))
	{
		intScore = (intScore+letter_lvl[2]);
		strLog   = strLog + "+" + letter_lvl[2] + 
				" points for both upper and lower case chars\n";
	}
	
	// NUMBERS
	// at least one number
	if (passwd.match(/\d+/))
	{
		intScore = (intScore+number_lvl[0]);
		strLog   = strLog + "+" + number_lvl[0] + 
				" points for at least one number\n";
	}
	
	// at least two numbers
	if (passwd.match(/(.*[0-9].*[0-9])/))
	{
		intScore = (intScore+number_lvl[1]);
		strLog   = strLog + "+" + number_lvl[1] + 
				" points for at least 2 numbers\n";
	}
	
	// SPECIAL CHAR
	
	// at least one special character
	if (passwd.match(/.*[\W|_]/))
	{
		intScore = (intScore+specialchr_lvl[0]);
		strLog   = strLog + "+" + specialchr_lvl[0] + 
				" points for at least one special char\n";
	}
	
	// at least two special characters
	if (passwd.match(/(.*[\W|_].*[\W|_])/))
	{
		intScore = (intScore+specialchr_lvl[1]);
		strLog   = strLog + "+" + specialchr_lvl[1] + 
				" points for at least two special chars\n";
	}

	
	// COMBOS
	

	// both letters and numbers
	if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) 
	{
		intScore = (intScore+combos_lvl[0]);
		strLog   = strLog + "+" + combos_lvl[0] + 
			" points for both letters and numbers\n";
	}

	// letters or numbers, and special characters
	if (passwd.match(/([a-zA-Z0-9].*[\W|_])|([\W|_].*[a-zA-Z0-9])/))
	{
		intScore = (intScore+combos_lvl[1]);
		strLog   = strLog + "+" + combos_lvl[1] +
			" points for letters or numbers and special chars\n";
	}

	// scoring system (note that regardless of score pwd<6 chars is weak)
	var level_incr = Math.round(maxScore/3);
	if (passwd.length>0)
	{
		if( intScore <=level_incr || passwd.length<6 )
		{
		   strVerdict = "oops";
		   pmGradient.style.backgroundColor = '#D00000';
		}
		else if (intScore > level_incr && intScore <= level_incr*2)
		{
		   strVerdict = "medium";
		   pmGradient.style.backgroundColor= '#D0D000';
		}
		else if (intScore > level_incr*2 && intScore <= maxScore)
		{
		   strVerdict = "ok";
		   pmGradient.style.backgroundColor = '#00D000';
		}
	}
	
	pmProgressIndicator.innerHTML = strVerdict;
	var maskSize = Math.round((pmContainer.offsetWidth/maxScore)*intScore);
	pmMask.style.left = maskSize +"px";
	pmMask.style.width = pmContainer.offsetWidth - maskSize + "px";
	strLog = strLog + "total points: " + intScore + "\n";
	strLog = strLog + "max points: " + maxScore + "\n";
	strLog = strLog + "left: " + 
			Math.round((pmContainer.offsetWidth/maxScore)*intScore) +"px\n";
	strLog = strLog + "container width:" +pmContainer.offsetWidth +"\n";
	//document.getElementById('debug').innerHTML = strLog;
	
}


