/* acct_forgot_login_form.js */

/*  Validate form  */
var ValidationErrorCounter = 0 // Make this a global var so can be seen inside of all JS functions.

function validateForm(ThisForm) {

	if (document.getElementById("AppFeedbackSpan") != null)
		document.getElementById("AppFeedbackSpan").style.display = "none"

	validateThisFormField('LastName','Required')
		
	FurtherTimeValidation = validateThisFormField('Email','Required')
	
	// Email
	if (FurtherTimeValidation == true ) {
		var ThisEmail = document.getElementById('Email').value
		
		if( ThisEmail.length == 0 ) {
			readyToGo = false;
			document.getElementById('err_Email').innerHTML = 'Required';
			ValidationErrorCounter = ValidationErrorCounter + 1
		} else if ( checkMailSyntax(ThisEmail) != true ) {
			readyToGo = false;
			document.getElementById('err_Email').innerHTML = 'E-mail does not appear to be valid';
			ValidationErrorCounter = ValidationErrorCounter + 1		
		} else {
			document.getElementById('err_Email').innerHTML = ""
		}
	}
	
	// Determine whether or not to display Error box above submit button
	if (ValidationErrorCounter > 0) {
		colorSwitcher("FormValidationMessage")
		document.getElementById("FormValidationMessage").style.display = "block"
		document.getElementById("Retrieve_Button").disabled               = false
		document.getElementById("Retrieve_Button").style.fontWeight       = "normal"
		document.getElementById("Retrieve_Button").style.color            = "black"
		document.getElementById("Retrieve_Button").value                  = "Retrieve"
		ValidationErrorCounter = 0
 		return false
	} else {
		document.getElementById("FormValidationMessage").style.display = "none"
		document.getElementById("Retrieve_Button").disabled               = true
		document.getElementById("Retrieve_Button").style.fontWeight       = "bold"
		document.getElementById("Retrieve_Button").style.color            = "green"
		document.getElementById("Retrieve_Button").value                  = "Please wait... "
		return true
	}
}

/* Validate form -- Helper function */
function validateThisFormField(ThisFormFieldName, ThisMessage) {
	//alert(ThisFormFieldName)
	ThisFormFieldName = '' + ThisFormFieldName // fixes it for FireFox
	if (document.getElementById(ThisFormFieldName).value.length == 0){
		validateDisplayHelper(ThisFormFieldName,ThisMessage,1)
		ThisResult = false
	} else {
		validateDisplayHelper(ThisFormFieldName,'',0)
		ThisResult = true
	}
	return ThisResult
}

/* Validate form -- Helper function */
function validateDisplayHelper(ThisFormFieldName, ThisMessage, ThisValue) {
	//alert(ThisFormFieldName)
	ThisFormFieldName = '' + ThisFormFieldName // fixes it for FireFox
	ThisMessage       = '' + ThisMessage       // fixes it for FireFox
	document.getElementById(ThisFormFieldName).value = Trim(document.getElementById(ThisFormFieldName).value) // Trim() is three JS functions
	document.getElementById("err_" + ThisFormFieldName).innerHTML = ThisMessage
	ValidationErrorCounter = ValidationErrorCounter + ThisValue
}

/* Validate form -- Helper function */
function validateRadioButtons(ThisRadioButtonGroupName) {
	var ValidationErrorCounter = false
	var ThisRadioGroup = document.getElementsByName(ThisRadioButtonGroupName)
	for (i=0; i<ThisRadioGroup.length; i++) {
		if (ThisRadioGroup[i].checked == true) {
			ValidationErrorCounter = true
		}
	}
	if (ValidationErrorCounter == false) {
		return false
	}
	return true
}

/* Validate form -- Helper function */
function checkMailSyntax(mail){
	if (window.RegExp) { // error check using regular expressions
		var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
		var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,20}|[0-9]{1,3})(\]?)$/; // valid email format
		if (!reg1.test(mail) && reg2.test(mail)) { // if syntax is valid do nothing
			return true;
		}else{
			return false;				
		}
	}
}

/* Validate form -- Helper function */
function LTrim(str) { 
	for (var k=0; k<str.length && str.charAt(k)<=" " ; k++) ;
	return str.substring(k,str.length);
}
function RTrim(str) {
	for (var j=str.length-1; j>=0 && str.charAt(j)<=" " ; j--) ;
	return str.substring(0,j+1);
}
function Trim(str) {
	return LTrim(RTrim(str));
}

/* Validate form -- Helper function */
var ThisColor = "#FFF8EE"

function colorSwitcher(ThisDivID) {
	if ( ThisColor == "#FFF8EE" || ThisColor == "" ) {
		ThisColor = "#FFE8D8" // tangerine
	} else {
		ThisColor = "#FFF8EE" // off-pink
	}
	document.getElementById(ThisDivID).style.backgroundColor = ThisColor
}

