//color -> worst
var initColor = new Array(208,16);
//color -> best
var endColor = new Array(16,208);
//
var composante3 = 16;
//messages
var infoSecure = "Niveau de sécurité : "
var secureMsg = new Array("", infoSecure+"Faible", infoSecure+"Faible",infoSecure+"Moyen",infoSecure+"Elevé",infoSecure+"Elevé");
//maximum value
var coefColor = 13;

var valSecure = 0;

function Password_check(obj, id) {
  valSecure = 0;
  //one lower case letter
  if (obj.value.match(/[a-z]/)) {
    valSecure++;
  }                         

  //one upper case letter
  if (obj.value.match(/[A-Z]/)) {
    valSecure++;
  }  

  //one number
  if (obj.value.match(/\d+/)) {
    valSecure++;
  }

  //both upper and lower case
  if (obj.value.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) {
    valSecure+=3;
  }

  //both letters and numbers, almost works because an additional character is required
  if (obj.value.match(/(\d.*\D)|(\D.*\d)/)) {
    valSecure+=3;
  }

  /*
  //three numbers
  if (obj.value.match(/(.*[0-9].*[0-9].*[0-9])/)) {
    valSecure++;
  }

  //one special character
  if (obj.value.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)) {
    valSecure++;
  }

  //letters, numbers, and special characters
  if (obj.value.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)) {
    valSecure++;
  }
  */

  if (obj.value.length ==0) {
    valSecure = 0;
  } else if (obj.value.length <= 4) {
    valSecure=1;
  } else if (obj.value.length <= 7) {
    valSecure=2;
  } else {
    valSecure += 4;
  }

  getBar(valSecure, id);
}

function getBar(refColor, id) {

  var rgbColor = new Array();
  rgbColor = getColor(refColor);

  document.getElementById(id + "_lv").style.backgroundColor = "rgb("+ rgbColor[0] +"," + rgbColor[1] + "," + composante3 + ")";
  document.getElementById(id + "_lv").style.width = Math.round(100*refColor/coefColor) + "%";
  document.getElementById(id + "_lv_txt").innerHTML = secureMsg[Math.ceil(refColor/coefColor*(secureMsg.length-1))];
  document.getElementById(id + "_lv_txt").style.color = "rgb("+ rgbColor[0] +"," + rgbColor[1] + "," + composante3 + ")";
}

function getColor(coef) {

  var diffr = 0;
  var diffg = 0;
  var tabResult = new Array(0,0);
  var coefV1 = 1;
  var coefV2 = 1;

  if (endColor[0]>=initColor[0]) {
    diffr = endColor[0] - initColor[0];
    coefV1 = 1;
  } else {
    diffr = initColor[0] - endColor[0];
    coefV1 = -1;
  }

  if (endColor[1]>=initColor[1]) {
    diffg = endColor[1] - initColor[1];
    coefV2 = 1;
  } else {
    diffg = initColor[1] - endColor[1];
    coefV2 = -1;
  }

  var diffTotal = diffr + diffg;
  var v1 = diffTotal*coef/coefColor;


  if (initColor[0]>endColor[0]) {

    if (v1<=diffr) {
      tabResult[0] = Math.round(initColor[0]);
      tabResult[1] = Math.round(initColor[1]-(v1*coefV1));
    } else {
      tabResult[0] = Math.round(initColor[0]-(v1*coefV2)+(diffr*coefV2));
      tabResult[1] = Math.round(endColor[1]);
    }

  } else {

    if (v1<=diffr) {
      tabResult[0] = Math.round(initColor[0]+(v1*coefV1));
      tabResult[1] = Math.round(initColor[1]);
    } else {
      tabResult[0] = Math.round(endColor[0]);
      tabResult[1] = Math.round(initColor[1]+(v1*coefV2)-(diffr*coefV2));
    }

  }

  return tabResult;
}

function checkemail(name){
 var mail = /^[\w\-]+(\.[\w\-]+)*@[\w\-]+(\.[\w\-]+)*\.[\w\-]{2,}$/; 
 var str = document.getElementById(name).value;
 if( mail.test(str) ){
 	return true;
 } else {
 	return false;
 }
}

function caracteres_speciaux(name){
	var exp = new RegExp("^[a-zA-Z0-9-]+$","gi");
	var str = document.getElementById(name).value;
	if( exp.test(str) ){
		return true;
	} else {
		return false;
	}
}