/* 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) {
	
	// Hide any previous feedback messages (from the appwide feedback handler (i.e., app_feedback.cfm)
	if (document.getElementById("AppFeedbackSpan") != null)
		document.getElementById("AppFeedbackSpan").style.display = "none"
	
	validateThisFormField('UserNameClr','Required')
	validateThisFormField('PswrdClr','Required')
	
	// Determine whether or not to display Error box above submit button
	if (ValidationErrorCounter > 0) {
		colorSwitcher("FormValidationMessage")
		document.getElementById("FormValidationMessage").style.display = "block"
		document.getElementById("Login_btn").disabled               = false
		document.getElementById("Login_btn").style.fontWeight       = "normal"
		document.getElementById("Login_btn").style.color            = "black"
		document.getElementById("Login_btn").value                  = "Login"
		ValidationErrorCounter = 0
		return false
	} else {
		document.getElementById("FormValidationMessage").style.display = "none"
		document.getElementById("Login_btn").disabled               = true
		document.getElementById("Login_btn").style.fontWeight       = "bold"
		document.getElementById("Login_btn").style.color            = "green"
		document.getElementById("Login_btn").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 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
}
/* Toggle the international characters */
function toggleDiv(thisDivID){
	if(document.getElementById(thisDivID).style.display == "none"){
		document.getElementById(thisDivID).style.display = "block";
	}
	else{
		document.getElementById(thisDivID).style.display = "none";
	}
}
function insertIntChar(thisFieldName, thisCharacter){
	fieldReference = document.getElementById(thisFieldName);
	fieldReference.value = fieldReference.value + thisCharacter;
	fieldReference.focus();
}

