﻿/*	PASSWORD STRENGTH METER
		Vijay Nair
		
		This is a result of various scripts taken from various places in the web. 
		
	Password Strength Algorithm:
	
	Password Length:
		4 Points: Less than 4 characters
		8 Points: 5 to 7 characters
		9 - 15 Points: 1 point for each characters above 8 - max 15 points
		
	Letters:
		0 Points: No letters
		7 Points - Lower characters 
		15 - Points upper characters
		25 Points: A mix of Upper case letters and Lower case letters
		-10 points : For all characters are letters only in single case

	Numbers:
		0 Points: No numbers
		10 Points: 1 number
		15 points: 2 numbers
		20 Points: 3 or more numbers
	-5 pOINTS: For all characters are numbers only		

	Characters:
		0 Points: No special characters
		10 Points: 1 special character
		25 Points: More than 1 special character

	Bonus:
		5 Points: Letters and numbers
		10 Points: Letters, numbers, and characters
		15 Points: Mixed case letters, numbers, and characters
		
	Password Text Range:
	
		>= 90: Très sécurisée
		>= 80: Sécurisée
		>= 70: Très forte
		>= 60: Forte
		>= 50: Moyenne
		>= 25: Faible
		>= 0: Très faible
		
*/


// Settings
// -- Toggle to true or false, if you want to change what is checked in the password
var m_strUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var m_strLowerCase = "abcdefghijklmnopqrstuvwxyz";
var m_strNumber = "0123456789";
var m_strCharacters = "!@#$%^&*?_~"

// Check password
function checkPassword(strPassword)
{

	// Reset combination count
	var nScore = nUpperCount = nLowerCount = 0;
	
	// Password length
	// -- Less than 4 characters
	if (strPassword.length < 5)
	{
		nScore += 4;
	}
	// -- 5 to 7 characters
	else if (strPassword.length > 4 && strPassword.length < 8)
	{
		nScore += 8;
	}
	// -- 8 or more
	else if (strPassword.length > 7)
	{
		nScore += 8 + (strPassword.length - 7);
		if (nScore > 15) nScore=15;
	}

	// Letters
	var nUpperCount = countContain(strPassword, m_strUpperCase);
	var nLowerCount = countContain(strPassword, m_strLowerCase);
	var nLowerUpperCount = nUpperCount + nLowerCount;
	// -- All Letters in either lower case or upper case only, not a mix
	if (nLowerCount > 0 ) 
	{ 
		nScore += 7; 
	} else if (nUpperCount > 0 )
	{
		nScore += 15; 
	}
	// -- Letters are upper case and lower case
	else if (nUpperCount > 0 && nLowerCount > 0) 
	{ 
		nScore += 25; 
	}
	
	if (nLowerCount > 0 && nLowerCount == strPassword.lentgh)
	{
		nScore -= 10; 
	} else if (nUpperCount > 0 && nUpperCount == strPassword.lentgh)
	{
		nScore -= 10; 
	}	
	// Numbers
	var nNumberCount = countContain(strPassword, m_strNumber);
	// -- 1 number
	if (nNumberCount > 0 && strPassword.length == nNumberCount) 
	{
		nScore -= 5;
	} else if (nNumberCount == 1)
	{
		nScore += 10;
	} else if (nNumberCount == 1)
	{
		nScore += 15;
	} else if (nNumberCount >= 3) 	
	{
	// -- 3 or more numbers
		nScore += 20;
	}
	
	// Characters
	var nCharacterCount = countContain(strPassword, m_strCharacters);
	// -- 1 character
	if (nCharacterCount == 1)
	{
		nScore += 10;
	}	else if (nCharacterCount > 1)
	{
	// -- More than 1 character
		nScore += 25;
	}
	
	// Bonus
	// -- Letters and numbers
	if (nNumberCount > 0 && nUpperCount > 0 && nLowerCount > 0 && nCharacterCount > 0)
	{
		nScore += 15;
	} else if (nNumberCount > 0 && nLowerUpperCount > 0 && nCharacterCount > 0)
	{
		nScore += 10;
	} else if (nNumberCount > 0 && nLowerUpperCount > 0)
	{
		nScore += 5;
	}
		
	return nScore;
}
 
// Runs password through check and then updates GUI 
function runPassword(strPassword, strFieldID) 
{
	// Check password
	var nScore = checkPassword(strPassword);
	
	// Get controls
	var ctlBar = document.getElementById(strFieldID + "_bar"); 
	var ctlText = document.getElementById(strFieldID + "_text");
	if (!ctlBar || !ctlText)
		return;

	// Set new width
	ctlBar.style.width = (nScore*2)+'px';

 	// Color and text
	// -- Very Secure
 	if (nScore >= 90)
 	{
 		var strText = "Très sécurisée";
 		var strColor = "#0ca908";
 	}
 	// -- Secure
 	else if (nScore >= 80)
 	{
 		var strText = "Sécurisée";
 		var strColor = "#7ff27c";
	}
	// -- Very Strong
 	else if (nScore >= 70)
 	{
 		var strText = "Très forte";
 		var strColor = "#1740ef";
	}
	// -- Strong
 	else if (nScore >= 60)
 	{
 		var strText = "Forte";
 		var strColor = "#5a74e3";
	}
	// -- Average
 	else if (nScore >= 50)
 	{
 		var strText = "Moyenne";
 		var strColor = "#e3cb00";
	}
	// -- Weak
 	else if (nScore >= 25)
 	{
 		var strText = "Faible";
 		var strColor = "#e7d61a";
	}
	// -- Very Weak
 	else
 	{
 		var strText = "Très faible";
 		var strColor = "#e71a1a";
	}
	ctlBar.style.backgroundColor = strColor;
	ctlText.innerHTML = "<span style='color: " + strColor + ";'>" + strText +  "</span>";
}
 
// Checks a string for a list of characters
function countContain(strPassword, strCheck)
{ 
	// Declare variables
	var nCount = 0;
	
	for (i = 0; i < strPassword.length; i++) 
	{
		if (strCheck.indexOf(strPassword.charAt(i)) > -1) 
		{ 
	        	nCount++;
		} 
	} 
 
	return nCount; 
} 



$(document).ready(function() {

	$("#txtusername").blur(function()
	{
	 $("#pseudo-dispo").removeClass().addClass('pseudo-dispo').text('Recherche en cours...').fadeIn("slow");
	 $.post("user_availability.php",{ user_name:$(this).val() } ,function(data)
	 {
	  if(data=='no')
	  {

	   $("#pseudo-dispo").fadeTo(200,0.1,function()
	   {
			document.getElementById('module_mon_profil').style.display = 'none';
			document.getElementById('module_mon_adresse').style.display = 'none';
			document.getElementById('module_mes_preferences').style.display = 'none';
			document.getElementById('module_mon_about').style.display = 'none';
			document.getElementById('module_ma_photo').style.display = 'none';
			
	    $(this).html('Ce pseudo est déjà utilisé, <br>veuillez en choisir un autre.').addClass('pseudo-dispo-error').fadeTo(900,1);
	     
	
		
	   });
	  }
	  else if(data=='yes')
	  {
			document.getElementById('module_mon_profil').style.display = '';
			document.getElementById('module_mon_adresse').style.display = '';
			document.getElementById('module_mes_preferences').style.display = '';
			document.getElementById('module_mon_about').style.display = '';
			document.getElementById('module_ma_photo').style.display = '';
			
			
			
			
			
			
	   $("#pseudo-dispo").fadeTo(200,0.1,function()
	   {
	    $(this).html('Pseudo libre !').addClass('pseudo-dispo-ok').fadeTo(900,1);
	   });
	  }
	 });
	});

});

  $(document).ready(function() {

	$("#txtemail").blur(function()
	{
	 $("#email-dispo").removeClass().addClass('email-dispo').text('Recherche en cours...').fadeIn("slow");
	 $.post("email_availability.php",{ email:$(this).val() } ,function(data)
	 {
	  if(data=='no')
	  {
	   $("#email-dispo").fadeTo(200,0.1,function()
	   {
			document.getElementById('module_mon_adresse').style.display = 'none';
			document.getElementById('module_mes_preferences').style.display = 'none';
			document.getElementById('module_mon_about').style.display = 'none';
			document.getElementById('module_ma_photo').style.display = 'none';
	    $(this).html('Cet email a déjà été utilisé !').addClass('email-dispo-error').fadeTo(900,1);
	   });
	  }
	  else if(data=='yes')
	  {
	 
			document.getElementById('module_mon_adresse').style.display = '';
			document.getElementById('module_mes_preferences').style.display = '';
			document.getElementById('module_mon_about').style.display = '';
			document.getElementById('module_ma_photo').style.display = '';
	   $("#email-dispo").fadeTo(200,0.1,function()
	   {
	    $(this).html('Email libre !').addClass('email-dispo-ok').fadeTo(900,1);
	   });
	  }
	 });
	});

}); 



