// Password strength meter
// This jQuery plugin is written by Firas Kassem [2007.04.05] and modified by Amin Rajaee [2009.07.26]
// Firas Kassem  phiras.wordpress.com || phiras at gmail {dot} com
// Amin Rajaee  rajaee at gmail (dot) com

// for more information : http://phiras.wordpress.com/2007/04/08/password-strength-meter-a-jquery-plugin/

var shortPass = 'Password is too short'
var badPass = 'Weak - Try letters & numbers'
var goodPass = 'OK - Try special characters'
var strongPass = 'Strong Password'
var sameAsUsername = 'Password same as email'

function passwordStrength(password,email) {
  score = 0 
    
  //password < 6
  if (password.length < 6 ) { return shortPass }
    
  //password == email
  if (password.toLowerCase() == email.toLowerCase()) return sameAsUsername
    
  //password length
  score += password.length * 4
  score += ( checkRepetition(1,password).length - password.length ) * 1
  score += ( checkRepetition(2,password).length - password.length ) * 1
  score += ( checkRepetition(3,password).length - password.length ) * 1
  score += ( checkRepetition(4,password).length - password.length ) * 1

  //password has 3 numbers
  if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5 
    
  //password has 2 symbols
  if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5 
    
  //password has upper and lower chars
  if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10 
    
  //password has number and chars
  if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15 

  //password has number and symbol
  if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15 
    
  //password has char and symbol
  if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15 
    
  //password is just a nubers or chars
  if (password.match(/^\w+$/) || password.match(/^\d+$/) )  score -= 10 
    
  //verifing 0 < score < 100
  if (score < 0) score = 0 
  if (score > 100) score = 100 

  if (score < 34) return badPass 
  if (score < 68) return goodPass

  return strongPass
}

function passwordStrengthPercent(password, email) {
  score = 0 

  //password < 6
  if (password.length < 6 ) { return 0 }

  //password == email
  if (password.toLowerCase()==email.toLowerCase()) return 0

  //password length
  score += password.length * 4
  score += ( checkRepetition(1,password).length - password.length ) * 1
  score += ( checkRepetition(2,password).length - password.length ) * 1
  score += ( checkRepetition(3,password).length - password.length ) * 1
  score += ( checkRepetition(4,password).length - password.length ) * 1

  //password has 3 numbers
  if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5 

  //password has 2 sybols
  if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5 

  //password has Upper and Lower chars
  if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10 

  //password has number and chars
  if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15 

  //password has number and symbol
  if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15 

  //password has char and symbol
  if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15 

  //password is just a nubers or chars
  if (password.match(/^\w+$/) || password.match(/^\d+$/) )  score -= 10 
  if (score > 100) return 100

  return (score)
}

function checkRepetition(pLen,str) {
  res = ""
  for ( i=0; i<str.length ; i++ ) {
    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
}

jQuery(document).ready(function() {

var bpos = "";
var perc = 0 ;
var minperc = 0 ;
$('#password').css( {backgroundPosition: "0 0"} );
$('#email').keyup(function(){
  $('#result').html(passwordStrength($('#password').val(),$('#email').val())) ;
  perc = passwordStrengthPercent($('#password').val(),$('#email').val());

  bpos=" $('#colorbar').css( {backgroundPosition: \"0px -" ;
  bpos = bpos + perc + "px";
  bpos = bpos + "\" } );";
  bpos=bpos +" $('#colorbar').css( {width: \"" ;
  bpos = bpos + (perc * 1.62) + "px";
  bpos = bpos + "\" } );";
  eval(bpos);
      $('#percent').html(" " + perc  + "% ");
                })
$('#password').keyup(function(){
  $('#result').html(passwordStrength($('#password').val(),$('#email').val())) ;
  perc = passwordStrengthPercent($('#password').val(),$('#email').val());

  bpos=" $('#colorbar').css( {backgroundPosition: \"0px -" ;
  bpos = bpos + perc + "px";
  bpos = bpos + "\" } );";
  bpos=bpos +" $('#colorbar').css( {width: \"" ;
  bpos = bpos + (perc * 1.62) + "px";
  bpos = bpos + "\" } );";
  eval(bpos);
    $('#percent').html(" " + perc  + "% ");
  })
})
