// Usage: Pass the password string to passwordStrength and string with time it will take is returned

function passwordStrength(password)
{
	// Get password length
	var length = password.length;
	
	// Check password against common passwords of that length to see if it's in top 500
	if (length > 2 && length < 9)
	{
		for (var i=0; i<arrayOfPasswords[length].length; i++)
		{
			if (password.toLowerCase() == arrayOfPasswords[length][i])
			{
				return 'Una de las 500 contraseñas más habituales...';
			}
		}
	}


	// Calculations per second. Ten million is roughly the number a decent PC could manage uninhibited
	var calculationsPerSecond = 10000000;
	
	
	// Keep track of how many character sets are used
	var possibleCharacters = 0;
	
	// Lowercase
	if (password.match(/[a-z]/)) { possibleCharacters += 26; }
	
	// Uppercase
	if (password.match(/[A-Z]/)) { possibleCharacters += 26; }
	
	// Numbers
	if (password.match(/\d+/)) { possibleCharacters += 10; }
	
	// Symbols
	if (password.match(/[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) ) {possibleCharacters += 13};
	
	
	// Work out the number of possible combinations: possible characters to the power of the password length
	var possibleCombinations = Math.pow(possibleCharacters, password.length);
	
	// Divide the number of possible combinations by the calculations a PC can do per second
	var computerTimeInSecs = possibleCombinations / calculationsPerSecond;
	
	
	// Set up an array of periods with their lenghts in seconds
	var arrayOfPeriods = new Array();
	arrayOfPeriods['minuto'] = 60;
	arrayOfPeriods['hora'] = arrayOfPeriods['minuto'] * 60;
	arrayOfPeriods['dia'] = arrayOfPeriods['hora'] * 24;
	arrayOfPeriods['año'] = arrayOfPeriods['dia'] * 365.25;
	arrayOfPeriods['miles de años'] = arrayOfPeriods['año'] * 1000;
	arrayOfPeriods['millones de años'] = arrayOfPeriods['miles de años'] * 1000;
	arrayOfPeriods['años luz'] = arrayOfPeriods['millones de años'] * 1000;
	
	
	// Period type is 'seconds' by default
	var periodType = 'segundo';
	var strength = '';
	
	// If it will take less than a second to crack the password, show exactly how long
	if (computerTimeInSecs < 1) { strength = computerTimeInSecs+" segundos";}
	
	// Otherwise show a rounded-down number
	else
	{
		
		// Adds the 's' onto anything before a thousand years
		var intoThousands = 's';
		
		// Adds the 'About' to anything after seconds
		var intoMinutes = '';
		
		// Round down the time
		var newTime = Math.floor(computerTimeInSecs);
		
		// Go through different period lengths
		for (var i in arrayOfPeriods)
		{
			// Stop when got to right period
			if (computerTimeInSecs < arrayOfPeriods[i]) { break; }
			else
			{
				if (i == "miles de años") { intoThousands = ''; }
				if (i == "minuto") { intoMinutes = 'Aprox. '; }
				newTime = Math.floor(computerTimeInSecs / arrayOfPeriods[i]);
				periodType = i;
			}
		}
		
		// If singular
		if (newTime == 1)
		{
			var aType = '';
			
			// Puts 'an' where needed
			if (periodType == 'hora' || periodType == 'octillones de años')
			{
				aType = '';
			}
			
			strength = "Aprox. "+aType+periodType;
		}
		else
		{
			// Convert newTime to string
			newTime += '';
			
			// Split with commas every three digits
			var regex = /(\d+)(\d{3})/;
			
			while (regex.test(newTime))
			{
				newTime = newTime.replace(regex, '$1' + ',' + '$2');
			}
			
			// Combine with 'about' if needed, the time it will take, the period type, and the 's' if needed
			strength = intoMinutes+newTime+" "+periodType+intoThousands;
		}
	}
	
	return strength;
}


var arrayOfPasswords = new Array();

arrayOfPasswords[3] = ['god','sex'];

arrayOfPasswords[4] = ['1234','cool','1313','star','golf','bear','dave','pass','aaaa','6969','jake','matt','1212','fish','fuck','porn','4321','2000','4128','test','shit','love','baby','cunt','mark','3333','john','sexy','5150','4444','2112','fred','mike','1111','tits','paul','mine','king','fire','5555','slut','girl','2222','asdf','time','7777','rock','xxxx','ford','dick','bill','wolf','blue','alex','cock','beer','eric','6666','jack'];

arrayOfPasswords[5] = ['beach','great','black','pussy','12345','frank','tiger','japan','money','naked','11111','angel','stars','apple','porno','steve','viper','horny','ou812','kevin','buddy','teens','young','jason','lucky','girls','lover','brian','kitty','bubba','happy','cream','james','xxxxx','booty','kelly','boobs','penis','eagle','white','enter','chevy','smith','chris','green','sammy','super','magic','power','enjoy','scott','david','video','qwert','paris','women','juice','dirty','music','peter','bitch','house','hello','billy','movie'];

arrayOfPasswords[6] = ['123456','prince','guitar','butter','jaguar','united','turtle','muffin','cooper','nascar','redsox','dragon','zxcvbn','qwerty','tomcat','696969','654321','murphy','987654','amanda','brazil','wizard','hannah','lauren','master','doctor','eagle1','gators','squirt','shadow','mickey','mother','monkey','bailey','junior','nathan','abc123','knight','alexis','iceman','fuckme','tigers','badboy','bonnie','purple','debbie','angela','jordan','andrea','spider','harley','ranger','dakota','booger','iwantu','aaaaaa','lovers','player','flyers','suckit','hunter','beaver','morgan','matrix','boomer','runner','batman','scooby','edward','thomas','walter','helpme','gordon','tigger','jackie','casper','robert','booboo','boston','monica','stupid','access','coffee','braves','xxxxxx','yankee','saturn','buster','gemini','barney','apples','soccer','rabbit','victor','august','hockey','peanut','tucker','killer','canada','george','johnny','sierra','blazer','andrew','spanky','doggie','232323','winter','zzzzzz','brandy','gunner','beavis','compaq','horney','112233','carlos','arthur','dallas','tennis','sophie','ladies','calvin','shaved','pepper','giants','surfer','fender','samson','austin','member','blonde','blowme','fucked','daniel','donald','golden','golfer','cookie','summer','bronco','racing','sandra','hammer','pookie','joseph','hentai','joshua','diablo','birdie','maggie','sexsex','little','biteme','666666','topgun','ashley','willie','sticky','cowboy','animal','silver','yamaha','qazwsx','fucker','justin','skippy','orange','banana','lakers','marvin','merlin','driver','rachel','marine','slayer','angels','asdfgh','bigdog','vagina','apollo','cheese','toyota','parker','maddog','travis','121212','london','hotdog','wilson','sydney','martin','dennis','voodoo','ginger','magnum','action','nicole','carter','erotic','sparky','jasper','777777','yellow','smokey','dreams','camaro','xavier','teresa','freddy','secret','steven','jeremy','viking','falcon','snoopy','russia','taylor','nipple','111111','eagles','131313','winner','tester','123123','miller','rocket','legend','flower','theman','please','oliver','albert'];

arrayOfPasswords[7] = ['porsche','rosebud','chelsea','amateur','7777777','diamond','tiffany','jackson','scorpio','cameron','testing','shannon','madison','mustang','bond007','letmein','michael','gateway','phoenix','thx1138','raiders','forever','peaches','jasmine','melissa','gregory','cowboys','dolphin','charles','cumshot','college','bulldog','1234567','ncc1701','gandalf','leather','cumming','hunting','charlie','rainbow','asshole','bigcock','fuckyou','jessica','panties','johnson','naughty','brandon','anthony','william','ferrari','chicken','heather','chicago','voyager','yankees','rangers','packers','newyork','trouble','bigtits','winston','thunder','welcome','bitches','warrior','panther','broncos','richard','8675309','private','zxcvbnm','nipples','blondes','fishing','matthew','hooters','patrick','freedom','fucking','extreme','blowjob','captain','bigdick','abgrtyu','chester','monster','maxwell','arsenal','crystal','rebecca','pussies','florida','phantom','scooter','success'];

arrayOfPasswords[8] = ['firebird','password','12345678','steelers','mountain','computer','baseball','xxxxxxxx','football','qwertyui','jennifer','danielle','sunshine','starwars','whatever','nicholas','swimming','trustno1','midnight','princess','startrek','mercedes','superman','bigdaddy','maverick','einstein','dolphins','hardcore','redwings','cocacola','michelle','victoria','corvette','butthead','marlboro','srinivas','internet','redskins','11111111','access14','rush2112','scorpion','iloveyou','samantha','mistress'];