// JavaScript Document

/*
 * GLOBAL REGULAR EXPRESSIONS USED FOR VALIDATION
 */
//Verifies that there are only 3 digits
var TWODIGITSREGEXP = /^\d{2}$/;

//Verifies that there are only 3 digits
var THREEDIGITSREGEXP = /^\d{3}$/;

//Verifies that there are only 3 digits
var FOURDIGITSREGEXP = /^\d{4}$/;

//Verifies a SSN to be in the format XXX-XX-XXXX or XXXXXXXXX
var SSNREGEXP = /^(\d{3})-?\d{2}-?\d{4}$/;

//Verifies a Phone number to be in the format XXX-XXXX or XXXXXXX
var PHONEREGEXP = /^(\d{3})-?\d{4}$/;

//Invalid Phone number(s)
var INVALIDPHONEREGEXP = /^(5{3})-?(12){2}$/;

//Alpha Numeric character and '-' '.' ' '
var NAMEREGEXP = /^[a-zA-Z0-9-\.\s]*\'?[a-zA-Z0-9-\.\s]*$/;

//Alpha Numeric character and '-' '.' ' ' '#' '_'
var ANSWERREGEXP = /^[a-zA-Z0-9-\.\s\_\#]*$/;

//Alpha Numeric character and '-' '.' '#' '_' '@'
var USERIDREGEXP = /^[a-zA-Z0-9-\.\_\#\@]{6,50}$/;

//Changed password requirements
//var PASSWORDREGEXP = /^[a-zA-Z0-9-\.\_\#\@]{6,10}$/;
//var PASSWORDREGEXP = /((?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?!.*[.,_,#,@])).{8,35}/;
var PASSWORDREGEXP = /(?=.*[\d])(?=.*[A-Z])(?=.*[a-z])^[\w\d!@#_.]{8,35}$/;

//Email
var EMAILREGEXP = /^[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{1,6}$/;

//Numberic only
var NUMERICREGEXP = /^\d+$/;





/* Test function */
function validateWL(theForm){
	var errColor = "#FFE18F";
	var msgArray = new Array();
	var i = 0;
	var result = true;
	
	
	/*test first name */
	if (NAMEREGEXP.test(theForm.fname.value) && trimAll(theForm.fname.value) != '') {
		theForm.fname.style.backgroundColor = '';
	}else if(trimAll(theForm.fname.value) == '') {
		theForm.fname.style.backgroundColor = errColor;
		msgArray[i++] = "<li>First name is a required field</li>";
	} else {
		theForm.fname.style.backgroundColor = errColor;
		msgArray[i++] = "<li>Sorry, only letters (a-z), numbers (0-9), spaces, and periods (.) are allowed for your first name.</li>";
	}

	/*test last name */
	if (NAMEREGEXP.test(theForm.lname.value) && trimAll(theForm.lname.value) != '') {
		theForm.lname.style.backgroundColor = '';
	}else if(trimAll(theForm.lname.value) == '') {
		theForm.lname.style.backgroundColor = errColor;
		msgArray[i++] = "<li>Last name is a required field</li>";
	} else {
		theForm.lname.style.backgroundColor = errColor;
		msgArray[i++] = "<li>Sorry, only letters (a-z), numbers (0-9), spaces, and periods (.) are allowed for your last name.</li>";
	}

	/* Validate SSN */
	var numDashes = theForm.ssn.value.split('-').length - 1;
	if (SSNREGEXP.test(theForm.ssn.value) && numDashes != 1) {
		theForm.ssn.style.backgroundColor = '';
	}
	else if (trimAll(theForm.ssn.value) == '') {
		theForm.ssn.style.backgroundColor = errColor;
		msgArray[i++] = "<li>It appears the SSN you entered is invalid.</li>";
	}
	else {
		theForm.ssn.style.backgroundColor = errColor;
		msgArray[i++] = "<li>Please enter your SSN using 9 digits or in the format: xxx-xx-xxxx.</li>";
	}

	/* test area code */
	if (THREEDIGITSREGEXP.test(theForm.phonecode.value)) {
		theForm.phonecode.style.backgroundColor = '';
	} else if(trimAll(theForm.phonecode.value).length < 3) {
		theForm.phonecode.style.backgroundColor = errColor;
		msgArray[i++] = "<li>Your area code must have 3 digits.</li>";
	} else {
		theForm.phonecode.style.backgroundColor = errColor;
		msgArray[i++] = "<li>The area code you entered contains invalid characters.</li>";
	}
	
	/* test phone */
	if(INVALIDPHONEREGEXP.test(theForm.phone.value)) {
		theForm.phone.style.backgroundColor = errColor;
		msgArray[i++] = "<li>555-1212 is not a valid phone number.</li>";
	} else if (PHONEREGEXP.test(theForm.phone.value)) {
		theForm.phone.style.backgroundColor = '';
		/*
		 * If the phone number is valid then
		 * If the phone number is not in the form xxx-xxxx then put it in the form xxx-xxxx.
		 */
		if (theForm.phone.value.indexOf('-') < 0){
			theForm.phone.value = theForm.phone.value.substring(0,3) + '-' + theForm.phone.value.substring(3,7);
		}
	} else {
		theForm.phone.style.backgroundColor = errColor;
		msgArray[i++] = "<li>Please enter your phone number in this format: xxx-xxxx or xxxxxxx.</li>";
	}
	
	/* test email */
	if (EMAILREGEXP.test(theForm.email.value)) {
		theForm.email.style.backgroundColor = '';
	} else if(trimAll(theForm.email.value) == '') {
		theForm.email.style.backgroundColor = '';
	} else {
		theForm.email.style.backgroundColor = errColor;
		msgArray[i++] = "<li>Please enter a valid e-mail address.</li>";
	}
	
	/* test user id */
	if (USERIDREGEXP.test(theForm.uid.value)) {
		theForm.uid.style.backgroundColor = '';
	} else if(trimAll(theForm.uid.value).length < 6) {
		theForm.uid.style.backgroundColor = errColor;
		msgArray[i++] = "<li>Your User ID should be between 6 and 50 characters.</li>";
	} else {
		theForm.uid.style.backgroundColor = errColor;
		msgArray[i++] = "<li>Your User ID should not contain spaces, forward slashes, backward slashes, or other special characters.</li>";
	}

	/* Test passwords are the same */
	if (theForm.password.value != theForm.password2.value) {
		theForm.password.style.backgroundColor = errColor;
		theForm.password2.style.backgroundColor = errColor;
		msgArray[i++] = "<li>The password you entered does not match the verify password.</li>";
	} else {

		/* test password for all requirements */
		if (PASSWORDREGEXP.test(theForm.password.value)) {
			theForm.password.style.backgroundColor = '';
			theForm.password2.style.backgroundColor = '';
		
		} else {
			theForm.password.style.backgroundColor = errColor;
			theForm.password2.style.backgroundColor = errColor;
			msgArray[i++] = "<li>Your password must be at least 8 characters that include: " +
					  "<p>1 uppercase and 1 lowercase letter and 1 number.</p>" + "</li>"; 
		}
	}
	
	/* test selQuest */
	if (NUMERICREGEXP.test(theForm.selQuest.value)) {
		theForm.selQuest.style.backgroundColor = '';
	} else {
		theForm.selQuest.style.backgroundColor = errColor;
		msgArray[i++] = "<li>Please select a security question.</li>";
	}
	
	/* test answer */
	if (ANSWERREGEXP.test(theForm.answer.value) && trimAll(theForm.answer.value) != '') {
		theForm.answer.style.backgroundColor = '';
	} else {
		theForm.answer.style.backgroundColor = errColor;
		msgArray[i++] = "<li>Please enter your answer to the security question, only letters (a-z), numbers (0-9), spaces, and periods (.) are allowed.</li>";
	}

	
	if (msgArray.length > 0) {
		for (j=0; j<msgArray.length; j++) {
			msg = msg + msgArray[j];
		}
		result = false;
	} else {
		result = true; 
	}


	return result;

}

function validate_reset_session (theForm) {
	var errColor = "#FFE18F";
	var msgArray = new Array();
	var showMsg = false;
	var i = 0;
	var result = true;
	var dob_month_flag = true;
	var dob_day_flag = true;
	var dob_year_flag = true;

	//test user name
	if (trimAll(theForm.username.value) != ''){
		theForm.username.style.backgroundColor = '';
	} else {
		theForm.username.style.backgroundColor = errColor;
		msgArray[i++] = "User name is a required field";
	}

	//test password
	if (trimAll(theForm.pass.value) != ''){
		theForm.pass.style.backgroundColor = '';
	} else {
		theForm.pass.style.backgroundColor = errColor;
		msgArray[i++] = "Password is a required field";
	}
	
	//test first name 
	if (NAMEREGEXP.test(theForm.fname.value) && trimAll(theForm.fname.value) != '') {
		theForm.fname.style.backgroundColor = '';
	}else if(trimAll(theForm.fname.value) == '') {
		theForm.fname.style.backgroundColor = errColor;
		msgArray[i++] = "First name is a required field";
	} else {
		theForm.fname.style.backgroundColor = errColor;
		msgArray[i++] = "Sorry, only letters (a-z), numbers (0-9), spaces, and periods (.) are allowed for your first name.";
	}

	//test last name 
	if (NAMEREGEXP.test(theForm.lname.value) && trimAll(theForm.lname.value) != '') {
		theForm.lname.style.backgroundColor = '';
	}else if(trimAll(theForm.lname.value) == '') {
		theForm.lname.style.backgroundColor = errColor;
		msgArray[i++] = "Last name is a required field";
	} else {
		theForm.lname.style.backgroundColor = errColor;
		msgArray[i++] = "Sorry, only letters (a-z), numbers (0-9), spaces, and periods (.) are allowed for your last name.";
	}

	// Validate SSN
	var numDashes = theForm.ssn.value.split('-').length - 1;
	if (SSNREGEXP.test(theForm.ssn.value) && numDashes != 1) {
		theForm.ssn.style.backgroundColor = '';
	}
	else if (trimAll(theForm.ssn.value) == '') {
		theForm.ssn.style.backgroundColor = errColor;
		msgArray[i++] = "It appears the SSN you entered is invalid.";
	}
	else {
		theForm.ssn.style.backgroundColor = errColor;
		msgArray[i++] = "Please enter your SSN using 9 digits or in the format: xxx-xx-xxxx.";
	}
	
	// Validate DOB 
	if (TWODIGITSREGEXP.test(theForm.month.value) && trimAll(theForm.month.value) != '') {
		theForm.month.style.backgroundColor = '';
	}else if(trimAll(theForm.month.value) == '') {
		theForm.month.style.backgroundColor = errColor;
		dob_month_flag=false;
		msgArray[i++] = "Please select your birth month.";
	} else {
		theForm.month.style.backgroundColor = errColor;
		dob_month_flag=false;
		msgArray[i++] = "Please select your birth month.";
	}
	if (TWODIGITSREGEXP.test(theForm.day.value) && trimAll(theForm.day.value) != '') {
		theForm.day.style.backgroundColor = '';
	}else if(trimAll(theForm.day.value) == '') {
		theForm.day.style.backgroundColor = errColor;
		dob_day_flag=false;
		msgArray[i++] = "Please select your birth day.";
	} else {
		theForm.day.style.backgroundColor = errColor;
		dob_day_flag=false;
		msgArray[i++] = "Please select your birth day.";
	}
	if (FOURDIGITSREGEXP.test(theForm.year.value) && trimAll(theForm.year.value) != '') {
		theForm.year.style.backgroundColor = '';
	}else if(trimAll(theForm.year.value) == '') {
		theForm.year.style.backgroundColor = errColor;
		dob_year_flag=false;
		msgArray[i++] = "Please enter your birth year.";
	} else {
		theForm.year.style.backgroundColor = errColor;
		dob_year_flag=false;
		msgArray[i++] = "Please enter your birth year.";
	}
	
	if(dob_month_flag && dob_day_flag && dob_year_flag) {
		//set ISO DOB
		theForm.dob.value = theForm.year.value + '-' + theForm.month.value + '-' + theForm.day.value;
	}
	
	if(msgArray.length > 0){
		showMsg = true;
		result= false;
		msg_Builder(msgArray, showMsg, "msg");
	}
	// Hide messages
	else{
		//clears the messages.
		msg_Builder(msgArray, showMsg, "msg");
	}

	return result;

}

//building the message array 
function msg_Builder(msgArray, showMsg, msgDivName){
	var objDiv = document.getElementById(msgDivName);

	if(showMsg){
		strInnerHTML = "<div id='msg'><h4>Errors:</h4><ul>";
		//place each message inside an html li tag
		for(i=0; i<msgArray.length; i++){
			strInnerHTML = strInnerHTML + "<li class='err'>" + msgArray[i] + "</li>";
		}
		strInnerHTML = strInnerHTML + "</ul></div>";
		objDiv.innerHTML = strInnerHTML;
		objDiv.style.display = 'block';
	}
	else{
		objDiv.style.display = 'none';
	}
}//end of functions

function trimAll( strValue ) {
	var objRegExp = /^(\s*)$/;
	//check for all spaces
	if(objRegExp.test(strValue)){
	   strValue = strValue.replace(objRegExp, '');
	   if( strValue.length == 0)
		  return strValue;
	}

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
	   //remove leading and trailing whitespace characters
	   strValue = strValue.replace(objRegExp, '$2');
	}
  return strValue;
}
