// password.js file


// Begin Password Validator JavaScript //
// This last (window.onload) line in the file
// may need to be repeated in the calling file, because of popup blockers.

function initKeyPress(){ // detect keypress and fire for detected object...

	passText = document.getElementById('password').onkeyup = checkPass;
	passTextConfirm = document.getElementById('password_confirm').onkeyup = checkPass;
	document.getElementsByName('username')[0].onkeyup = checkPass;
	document.getElementsByName('first_name')[0].onkeyup = checkPass;
	document.getElementsByName('last_name')[0].onkeyup = checkPass;
}

function checkPass(){  //calls the rule validator functions...
	
	userName  = document.getElementsByName('username')[0].value;
	firstName = document.getElementsByName('first_name')[0].value;
	lastName  = document.getElementsByName('last_name')[0].value;
	
	//what needs to be checked?...
	if (document.getElementById('minLength') != null) checkPassLength(); 	// minimum length 
	if (document.getElementById('minAlphaChar') != null) checkAlphaChar(); 	// alphabet characters 
	if (document.getElementById('minCapsChar') != null) checkCapsChar(); 	// alphabet mixed-case characters
	if (document.getElementById('minNumChar') != null) checkNumChar(); 	// numeric characters
	if (document.getElementById('minPunctChar') != null) checkPunctChar();	// punctuation characters
	if (document.getElementById('noUserText') != null) checkNoUserText(userName, firstName, lastName);	// no user name text 
	confirmPass();              //confirm that the password and confirm password fields match;
	if (document.getElementById('noReuse') != null) checkNoReuse();	// no password reuse rule
}

function checkPassLength(){
	theMinLength = document.getElementById('minLengthValue').value;  //determine the minimum length value...
	rule = document.getElementById('minLength');
	passLength = document.getElementById('password').value.length;
	if (passLength >= theMinLength)
	    rule.className = 'ok' 
	else
	    rule.className = 'unchecked';
}
function checkAlphaChar(){
	ruleLI = document.getElementById('minAlphaChar');
	// does not seem to match non-ASCII Unicode letters
	// also superfluously allows underscore.
	passText = document.getElementById('password').value;
	rule = /\w/i;
	result = passText.match(rule);
	ruleLI.className = (result != null && passText.length >= 1) ? 'ok' : 'unchecked';
}
function checkNumChar(){
	ruleLI = document.getElementById('minNumChar');
	passText = document.getElementById('password').value;
	rule = /\d/i;
	result = passText.match(rule);
	ruleLI.className = (result != null && passText.length >= 1) ? 'ok' : 'unchecked';
}
function checkPunctChar(){
	ruleLI = document.getElementById('minPunctChar');
	// there is not a punctuation check, unless we enumerate.
	// this counts non-ASCII Unicode letters as punctuation
	passText = document.getElementById('password').value;
	rule = /\W/i;
	result = passText.match(rule);
	ruleLI.className = (result != null && passText.length >= 1) ? 'ok' : 'unchecked';
}
function checkNoUserText(userName, firstName, lastName){	
	passText = document.getElementById('password').value;
	reverse_passText = reverseString(passText);
	
	ruleLI = document.getElementById('noUserText'); 

	if (compareText(userName, passText, reverse_passText) ||
	    compareText(lastName, passText, reverse_passText) ||
	    compareText(firstName, passText, reverse_passText)) {
	    ruleLI.className = 'unchecked';
	} else {
	    ruleLI.className = 'ok';
        }
}

// if pattern is empty, return false; 
// if either target is empty, return true;
// if otherwise return true if target contains the pattern (case insensitive)
function compareText(pattern, target1, target2) {
     if (pattern == "") return false;
      
     if (target1.length = 0 || target2.length == 0) return true;
     var patternLC = pattern.toLowerCase();
     return (target1.toLowerCase().indexOf(patternLC) != -1) || 
	    (target2.toLowerCase().indexOf(patternLC) != -1);
}

function checkNoReuse(){
	ruleLI = document.getElementById('noReuse'); 
	passText = document.getElementById('password').value;
	oldPass = document.getElementById('old_password').value;
	ruleLI.className = (passText != oldPass && passText.length >= 1) ? 'ok' : 'unchecked';
}
function checkCapsChar(){
	ruleLI = document.getElementById('minCapsChar');
	passText = document.getElementById('password').value;
	rule = /[A-Z]/g;
	result = passText.match(rule);
	ruleLI.className = (result != null && passText.length >= 1) ? 'ok' : 'unchecked';
}
function confirmPass(){
	ruleLI = document.getElementById('cfrmMatch');
	passText = document.getElementById('password').value;
	passTextConfirm = document.getElementById('password_confirm').value;
	ruleLI.className = (passText == passTextConfirm && passText.length >= 1) ? 'ok' : 'unchecked';
}

// Reverse string

function reverseString(string) {
  var inp = string;
  var outp = "";
  for (i = 0; i <= inp.length; i++) {
    outp = inp.charAt (i) + outp;
  }
  return outp;
} 

window.onload = initKeyPress;  //capture all form field keypresses when window loads...

// End JavaScript Validator Script //
