//Upload imagem perfil
var imageLoader = document.getElementById('filePhoto');
imageLoader.addEventListener('change', handleImage, false);
function handleImage(e) {
    var reader = new FileReader();
    reader.onload = function (event) {
        $('.edit-img img').attr('src',event.target.result);
    }
    reader.readAsDataURL(e.target.files[0]);
}

var password = document.getElementById("password")
  , confirm_password = document.getElementById("passwordrepeat");

function validatePassword(){
  if(password.value != confirm_password.value) {
    confirm_password.setCustomValidity("As passwords não correspondem");
  } else {
    confirm_password.setCustomValidity('');
  }
}

// Validate passwords inputs are equal
var password = document.getElementById("password")
,
confirm_password = document.getElementById("passwordrepeat");

function validatePassword(){
  if(password.value != confirm_password.value) {
    confirm_password.setCustomValidity("As passwords não correspondem");
  } else {
    confirm_password.setCustomValidity('');
  }
}
password.onchange = validatePassword;
confirm_password.onkeyup = validatePassword;

//Show password Eye
document.getElementById('olho').addEventListener('mousedown', function() {
  document.getElementById('password').type = 'text';
});
document.getElementById('olho').addEventListener('mouseup', function() {
  document.getElementById('password').type = 'password';
});
document.getElementById('olho').addEventListener('mousemove', function() {
  document.getElementById('password').type = 'password';
});
//Password verification client side
var strength = {
  0: "Péssima ",
  1: "Má ",
  2: "Fraca ",
  3: "Boa ",
  4: "Forte ",
  5: "Forte "
}
var password = document.getElementById('password');
var meter = document.getElementById('password-strength-meter');
var text = document.getElementById('password-strength-text');
password.addEventListener('input', function()
{
  let val = password.value;
  let result = zxcvbn(val);
  if(val.length < 12) {
    if(result.score >= 2){
      result.score = 2;
    }
  }
  let strengthValue = {
    'caps': false,
    'length': false,
    'special': false,
    'numbers': false,
    'small': false
  };
  for(let index=0; index < val.length; index++) {
    let char = val.charCodeAt(index);
    if(!strengthValue.caps && char >= 65 && char <= 90) {
      strengthValue.caps = true;
    } else if(!strengthValue.numbers && char >=48 && char <= 57){
      strengthValue.numbers = true;
    } else if(!strengthValue.small && char >=97 && char <= 122){
      strengthValue.small = true;
    } else if(!strengthValue.numbers && char >=48 && char <= 57){
      strengthValue.numbers = true;
    } else if(!strengthValue.special && (char >=33 && char <= 47) || (char >=58 && char <= 64)) {
      strengthValue.special = true;
    }
  }
  let strengthIndicator = 0;
  for(let metric in strengthValue) {
    if(strengthValue[metric] === true) {
      strengthIndicator++;
    }
  }
  if(result.score > strengthIndicator){
    result.score = strengthIndicator;
  }
  meter.value = result.score;
  // Update the text indicator
  if(val !== "") {
    text.innerHTML = "Força: " + "<strong>" + strength[result.score] + "</strong>";
  } else {
    text.innerHTML = "";
  }
});
