﻿var commonPasswords = new Array('password', 'pass', '1234', '1246'); 
 
var numbers = "0123456789"; 
var lowercase = "abcdefghijklmnopqrstuvwxyz"; 
var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
var punctuation = "!.@$£#*()%~<>{}[]";


    
function checkPassword_Strength() 
{
     
    
    var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
    var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
    var enoughRegex = new RegExp("(?=.{6,}).*", "g");
    
    var pwd = document.getElementById("ctl00_ContentPlaceHolder_txtPassword");

     if(pwd.value.length > 0)
     {
            document.getElementById("PasswordStrength").style.display = '';
            document.getElementById('ctl00_ContentPlaceHolder2_MySummaryLogin').style.display = 'none';
                document.getElementById('ctl00_ContentPlaceHolder2_MySummaryRegister').style.display = 'none';
     }
        else
            document.getElementById("PasswordStrength").style.display = 'none';
            
    if (pwd.value.length==0) {
    document.getElementById("passwordDescription").innerHTML = 'Type Password';
    } else if (false == enoughRegex.test(pwd.value)) {
    document.getElementById("passwordDescription").innerHTML = 'More Characters<br/><span style="font-size:10px;color:#666666;">Min of 8 characters, including at least <br/>one numeral, uppercase, lowercase <br/>and a symbol</span>';
    document.getElementById("progressBar1").className = "progrs_bar";
    } else if (strongRegex.test(pwd.value)) {
        document.getElementById("progressBar1").className = "progrs_barStrong";
    document.getElementById("passwordDescription").innerHTML = 'Strong<br/><span style="font-size:10px;color:#666666;">Min of 8 characters, including at least <br/>one numeral, uppercase, lowercase <br/>and a symbol</span>';
    } else if (mediumRegex.test(pwd.value)) {
        document.getElementById("progressBar1").className = "progrs_barMedium";
    document.getElementById("passwordDescription").innerHTML = 'Medium Security<br/><span style="font-size:10px;color:#666666;">Min of 8 characters, including at least <br/>one numeral, uppercase, lowercase <br/>and a symbol</span>';
    } else {
        document.getElementById("progressBar1").className = "progrs_barWeak";
    document.getElementById("passwordDescription").innerHTML = 'Weak Security<br/><span style="font-size:10px;color:#666666;">Min of 8 characters, including at least <br/>one numeral, uppercase, lowercase <br/>and a symbol</span>';
    }
}   

 
function checkPassword() { 
 
    var desc = new Array();
	desc[0] = "Very Weak Security";
	desc[1] = "Weak Security";
	desc[2] = "Better Security";
	desc[3] = "Medium Security";
	desc[4] = "Strong Security";
	desc[5] = "Strongest Security";
	
    var password = document.getElementById("ctl00_ContentPlaceHolder_txtPassword").value;
    
    var combinations = 0; 
    var score   = 0;

	//if password bigger than 6 give 1 point
	if (password.length > 6)
	{
	    score++;
	    combinations += 6;
	 }

	//if password has both lower and uppercase characters give 1 point	
	if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) )
	{
	    combinations += 5;
	 score++;
	 }

	//if password has at least one number give 1 point
	if (password.match(/\d+/))
	{
	    score++;
	  combinations += 10; 
	 }

	//if password has at least one special caracther give 1 point
	if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) )	
	{
	    score++;
	     combinations += punctuation.length; 
	    }

	//if password bigger than 12 give another 1 point
//	if (password.length > 12) score++;
//	
//	 if (contains(password, numbers) > 6) { 
//        combinations += 6; 
//        score++;
//        
//    } 
// 
//    if (contains(password, numbers) > 0) { 
//        combinations += 10; 
//        
//    } 
// 
//    if (contains(password, lowercase) > 0) { 
//        combinations += 26; 
//    } 
// 
//    if (contains(password, uppercase) > 0) { 
//        combinations += 26; 
//    } 
// 
//    if (contains(password, punctuation) > 0) { 
//        combinations += punctuation.length; 
//    } 
// 
    // work out the total combinations 
    var totalCombinations = Math.pow(combinations, password.length); 
 
    // if the password is a common password, then everthing changes... 
    if (isCommonPassword(password)) { 
        totalCombinations = 75000 // about the size of the dictionary 
    } 
 
    // work out how long it would take to crack this (@ 200 attempts per second) 
    var timeInSeconds = (totalCombinations / 200) / 2; 
 
    // this is how many days? (there are 86,400 seconds in a day. 
    var timeInDays = timeInSeconds / 86400 
 
    // how long we want it to last 
    var lifetime = 365; 
 
    // how close is the time to the projected time? 
    var percentage = timeInDays / lifetime; 
 
    var friendlyPercentage = cap(Math.round(percentage * 100), 100); 
    if (totalCombinations != 75000 && friendlyPercentage < (password.length * 2)) { 
        friendlyPercentage += password.length * 2; 
    } 
 
 
    if(password.length > 0)
        document.getElementById("PasswordStrength").style.display = '';
    else
        document.getElementById("PasswordStrength").style.display = 'none';
        
     document.getElementById("progressBar1").className = "progrs_barMedium";
    
    document.getElementById("passwordDescription").innerHTML = desc[score];
    var progressBar = document.getElementById("progressBar"); 
    
    if(friendlyPercentage >= 115)
        friendlyPercentage = 115;
    
    progressBar.style.height = "8px";
    progressBar.style.width = friendlyPercentage + "px"; 
    
 
    if (percentage > 1) { 
        // strong password 
        progressBar.style.backgroundColor = "#3bce08"; 
        return; 
    } 
 
    if (percentage > 0.5) { 
        // reasonable password 
        progressBar.style.backgroundColor = "#ffd801"; 
        return; 
    } 
 
    if (percentage > 0.10) { 
        // weak password 
        progressBar.style.backgroundColor = "orange"; 
        return; 
    } 
 
    // useless password! 
    if (percentage <= 0.10) { 
        // weak password 
        progressBar.style.backgroundColor = "red"; 
        return; 
    } 
 
 
} 
 
function cap(number, max) { 
    if (number > max) { 
        return max; 
    } else { 
        return number; 
    } 
} 
 
function isCommonPassword(password) { 
 
    for (i = 0; i < commonPasswords.length; i++) { 
        var commonPassword = commonPasswords[i]; 
        if (password == commonPassword) { 
            return true; 
        } 
    } 
 
    return false; 
 
} 
 
function contains(password, validChars) { 
 
    count = 0; 
 
    for (i = 0; i < password.length; i++) { 
        var char = password.charAt(i); 
        if (validChars.indexOf(char) > -1) { 
            count++; 
        } 
    } 
 
    return count; 
} 
 
 
 
 
 
