// Password checker

// This is the main password checking function.
function checkPassword(formName,passwordFieldName) {
 pw = window.document.forms[formName].elements[passwordFieldName].value
 if(checkPasswordLength(pw))
  if(checkPasswordCharsAllowed(pw))
   if(checkMinPasswordChars(pw))
    //window.document.forms[formName].submit();
    return true;
}

// Check to make sure the password is at least minChars characters long.
function checkPasswordLength(pw) {
 if(pw.length<minChars) {
  alert("La longitud de su password es menor a "+minChars+" caracteres.\n\rDebe elegir una que tenga al menos "+minChars+" caracteres de longitud.")
  return false
 }
 return true
}

// Check to make sure that all of the characters in the password are allowed.
function checkPasswordCharsAllowed(pw) {
 for(var i=0;i<pw.length;++i) {
  var ch = pw.charAt(i);
  if((isAlpha(ch) && !lettersAllowed)) {
   alert("Su password contiene letras.\n\rLas letras no estan permitidas dentro de las passwords.")
   return false
  }else if(isNumber(ch) && !numbersAllowed) {
   alert("Su password contiene numeros.\n\rLos numeros no estan permitidos dentro de las passwords.")
   return false
  }else if(isSpecial(ch) && !specialAllowed) {
   alert("Su password contiene caracters especiales.\n\rLos caracteres especiales no estan permitidos.")
   return false
  }else if(!isAlpha(ch) && !isNumber(ch) && !isSpecial(ch)) {
   alert("Su password contiene caracteres no imprimibles.\n\rLos caracters imprimibles no estan permitidos dentro de las passwords.")
   return false
  }
 }
 return true
}

// Check to make sure the password has the required number of alphabetic, numeric, and
// special characters.
function checkMinPasswordChars(pw) {
 var alpha = 0
 var numeric = 0
 var special = 0
 for(var i=0;i<pw.length;++i) {
  var ch = pw.charAt(i)
  if(isAlpha(ch)) ++alpha
  else if(isNumber(ch)) ++numeric
  else if(isSpecial(ch)) ++special
 }
 var errMsg = "Su password no contiene el minimo numero "
 if(alpha < minLetters) {
  errMsg += "(" + minLetters + ") "
  errMsg += "de caracteres alfabeticos."
  alert(errMsg)
  return false
 }else if(numeric < minNumbers) {
  errMsg += "(" + minNumbers + ") "
  errMsg += "de caracteres numericos."
  alert(errMsg)
  return false
 }else if(special < minSpecial) {
  errMsg += "(" + minSpecial + ") "
  errMsg += "de caracteres especiales."
  alert(errMsg)
  return false
 } 
 return true
}
 
// Functions used for character identification.
function isAlpha(ch) {
  if(ch >= "a" && ch <= "z") return true
  if(ch >= "A" && ch <= "Z") return true
  return false
}

function isNumber(ch) {
  if(ch >= "0" && ch <= "9") return true
  return false
}

function isSpecial(ch) {
 var special = new Array("!","\"","#","$","%","&","'","(",")","*","+",",","-",".","/",
 ":",";","<","=",">","?","@","[","\\","]","^","_","`","{","|","}","~")
 for(var i=0;i<special.length;++i)
  if(ch == special[i]) return true
 return false
}
