/*
OpenLogin.org
Java script write Eugene Krivoruchko
(c) 2008
*/
 var filterEmail  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

 var digits = "0123456789";
 var lowerLettersEng = "abcdefghijklmopqrstuvwxyz";
 var upperLettersEng = "ABCDEFGHIJKLMOPQRSTUVWXYZ";
 var otherSingsLogin = "~`!@#$%^&*+=|\\/()[]{}<>,.;:?\"\'";
 var lowerLettersRus = "ячсмитьбюэждлорпавыфйцукенгшщзхъё";
 var upperLettersRus = "ЯЧСМИТЬБЮЭЖДЛОРПАВЫФЙЦУКЕНГШЩЗХЪЁ";
 var otherSigns = "~`!@#$%^&*+-=_|\\/()[]{}<>,.;:?\"\'";

 var passWidth = 254;
 var passHeight = 4;
 var passLabel = new Array('недопустимые символы', 'слишком короткий', 'слабенький', 'слабый', 'нормальный', 'сложный', 'очень сложный');
 var passRation = passWidth / (passLabel.length - 2);

 var passDesc = new Array();
 passDesc[0] = '<p><small><b>'+passLabel[0]+'</b></small></p><div style="width: '+passWidth+'px; height: '+passHeight+'px; background: #ccc;"></div>';
 passDesc[1] = '<p><small><b>'+passLabel[1]+'</b></small></p><div style="width: '+passWidth+'px; height: '+passHeight+'px; background: #ccc;"></div>';
 passDesc[2] = '<p><small><b>'+passLabel[2]+'</b></small></p><div style="width: '+passWidth+'px; height: '+passHeight+'px; background: #ccc;"><div style="width: '+(passRation * 1)+'px; height: '+passHeight+'px; background: #c00;"></div></div>';
 passDesc[3] = '<p><small><b>'+passLabel[3]+'</b></small></p><div style="width: '+passWidth+'px; height: '+passHeight+'px; background: #ccc;"><div style="width: '+(passRation * 2)+'px; height: '+passHeight+'px; background: #f69;"></div></div>';
 passDesc[4] = '<p><small><b>'+passLabel[4]+'</b></small></p><div style="width: '+passWidth+'px; height: '+passHeight+'px; background: #ccc;"><div style="width: '+(passRation * 3)+'px; height: '+passHeight+'px; background: #9fc;"></div></div>';
 passDesc[5] = '<p><small><b>'+passLabel[5]+'</b></small></p><div style="width: '+passWidth+'px; height: '+passHeight+'px; background: #ccc;"><div style="width: '+(passRation * 4)+'px; height: '+passHeight+'px; background: #9f9;"></div></div>';
 passDesc[6] = '<p><small><b>'+passLabel[6]+'</b></small></p><div style="width: '+passWidth+'px; height: '+passHeight+'px; background: #ccc;"><div style="width: '+(passRation * 5)+'px; height: '+passHeight+'px; background: #093;"></div>';

 function onPasswordChange() {
  var password = gid('password1').value;

  var hasDigits = false;
  var hasLowerLettersEng = false;
  var hasUpperLettersEng = false;
  var hasLowerLettersRus = false;
  var hasUpperLettersRus = false;
  var hasOtherSigns = false;

  for(i = 0; i < password.length; i++) {
   hasDigits |= digits.indexOf(password.charAt(i)) >= 0;
   hasLowerLettersEng |= lowerLettersEng.indexOf(password.charAt(i)) >= 0;
   hasUpperLettersEng |= upperLettersEng.indexOf(password.charAt(i)) >= 0;
   hasLowerLettersRus |= lowerLettersRus.indexOf(password.charAt(i)) >= 0;
   hasUpperLettersRus |= upperLettersRus.indexOf(password.charAt(i)) >= 0;
   hasOtherSigns |= otherSigns.indexOf(password.charAt(i)) >= 0;
  }

  var m =
   (hasDigits? 10: 0) +
   (hasLowerLettersEng? 26: 0) +
   (hasUpperLettersEng? 26: 0);

  var u =
   (hasLowerLettersRus? 1: 0) +
   (hasUpperLettersRus? 1: 0) +
   (hasOtherSigns? 1: 0);

  var R = Math.pow(m, password.length);
  var intScore = Math.log(R);

  if (u > 0) {
   strVerdict = 0;
  }
  else
  if (intScore < 5 || password.length < 4) {
   strVerdict = 1;
  }
  else
   if (intScore < 16)
    strVerdict = 2;
   else
    if (intScore < 25)
     strVerdict = 3;
    else
     if (intScore < 35)
      strVerdict = 4;
     else
      if (intScore < 45)
       strVerdict = 5;
      else
       strVerdict = 6;
  document.getElementById('message').innerHTML = "<p>&nbsp;</p>" + passDesc[strVerdict];
  if (strVerdict > 1)
   return true
  else
   return false
 }

 function checkemail() {  e = gid('email');  if (!filterEmail.test(e.value)) {
   e.style.background = '#fcc';
   return false;
  }
  else {
   e.style.background = '#cfc';
   return true;
  }
 }

 function gid(name) {  return document.getElementById(name);
 }

 function checkpassword() {  onPasswordChange();
  p1 = gid('password1');
  p2 = gid('password2');

  if (p1.value != p2.value || p1.value.lenght < 4 || p2.value.length < 4) {   p1.style.background = '#fcc';
   p2.style.background = '#fcc';
   return false;
  }
  else {   p1.style.background = '#cfc';
   p2.style.background = '#cfc';
   return true;
  }
 }

 function checklogin() {  var hasDigits = false;
  var hasLowerLettersRus = false;
  var hasUpperLettersRus = false;
  var hasOtherSingsLogin = false;

  e = gid('nickname');

  val = e.value;

  for(i = 0; i < val.length; i++) {
   hasDigits |= digits.indexOf(val.charAt(i)) >= 0;
   hasLowerLettersRus |= lowerLettersRus.indexOf(val.charAt(i)) >= 0;
   hasUpperLettersRus |= upperLettersRus.indexOf(val.charAt(i)) >= 0;
   hasOtherSingsLogin |= otherSingsLogin.indexOf(val.charAt(i)) >= 0;
  }

  var u =
   (hasLowerLettersRus? 1: 0) +
   (hasUpperLettersRus? 1: 0) +
   (hasOtherSingsLogin? 1: 0);

 if (u > 0 || val.length < 3 || val.length > 20) {   e.style.background = '#fcc';
   return false;
  }
  else {
   e.style.background = '#cfc';
   return true;
  }
 }

 function updatecapcha(e) {  e.src = "/capcha.php?rand=" + Math.random();
 }

 function changecapcha() {  e = gid('capcha');

  if (e.value > 999 && e.value < 9999)	{   e.style.background = '#cfc';
   return true;
  }
  else {   e.style.background = '#fcc';
   return false;
  }
 }

 function checkform(e) {  var u = 0;

  u = (changecapcha() ? 0 : 1) +
  (checklogin() ? 0 : 1) +
  (checkemail() ? 0 : 1) +
  (onPasswordChange() ? 0 : 1) +
  (checkpassword() ? 0 : 1);

  if (u > 0)
   return false;
  else
   return true;
 }
