/**
 * JavaScript form validation helper script. 
 * 
 * This script contains all the common input form validation functions required.
 * For more information, please see the description on each of the functions.
 */

/* known good characters */
var good = /^[a-zA-Z0-9_@\-\ \'!$%&.,()]*$/;


              
              
/**
 * Validates the form looking for id's labeled "required" and
 * ensureing a value has been enetered.
 * 
 * @param form
 * @return
 */              
function validateRequiredFields(form) {
	var invalid = new Array();
	var count = 0;
	
	/* loop through all the elements in the form */
	for (var i = 0; i < form.elements.length; i ++) {
		var element = form.elements[i];
		
		/* pull the id */
		var id = element.id;
		
		/* look to see if the id contains the text "required" */
		if (id.indexOf("-required") != -1) {
			/* the field is marked required, ensure the value is not null */
			if (element.value == null || element.value == "") {
				/* the value is bad, store the id */
				invalid[count] = id;
				
				/* increment the count */
				count ++;
			}
		}
	}
	
	/* check the invalid length for any fields to report */
	if (invalid.length != 0) {
		/* we have values to report, build up the error message */
		var displayArea = document.getElementById("errorMessageArea");
		if (displayArea != null) {
			displayArea.style.display = "block";
		}

		/* update the form elements 'error' and style */
		for (var j = 0; j < invalid.length; j ++) {
			var error_id = invalid[j] + "-input";		
			var img = document.getElementById(error_id);
			if (img != null) {
				/* display the error */
				img.setAttribute("class", "form-error");
				/* class name for IE, since it doesn't like standards */
				img.setAttribute("className", "form-error");
			}
		}
	
		/* update the text in the message box */
		var messageTxt = document.getElementById("error_message_text");
		messageTxt.innerHTML = "Required Fields Missing.  Please fill in all required fields and try again. ";
	
		return false;
	}
	
	/* form is valid */
	return true;
}
              
/**
 * Validates the given form and it's fields for any known bad characters.  
 * If any "bad" characters are found, they are escaped into "safe" versions of 
 * the bad characters.
 */
function validateFormSafeSubmit(form) {
	/* list to contain the form element ids containing invalid characters */
	var invalid = new Array();
	var count = 0;
	
	/* reset the images */
	reset(form);
	
	/* loop through all the elements in the form */
	for (var i = 0; i < form.elements.length; i ++) {
		var element = form.elements[i];
		
		/* now that all the values have been replaced, verify that we still have a good string */
		if (good.test(element.value) == false) {
			/* add the element id to the list */
			invalid[count] = element.id;
			count ++;
		}
	}
	
	/* check to see if there are any ids to report */
	if (invalid.length != 0) {
		var displayArea = document.getElementById("errorMessageArea");
		if (displayArea != null) {
			displayArea.style.display = "block";
		}
		
		/* update the form elements 'error' and style */
		for (var j = 0; j < invalid.length; j ++) {
			var error_id = invalid[j] + "-input";		
			var img = document.getElementById(error_id);
			if (img != null) {
				/* display the error */
				img.setAttribute("class", "form-error");
				/* class name for IE, since it doesn't like standards */
				img.setAttribute("className", "form-error");
			}
		}
		
		/* update the text in the message box */
		var messageTxt = document.getElementById("error_message_text");
		messageTxt.innerHTML = "Invalid Content.  Valid Characters [a-z, A-Z, 0-9, _, !, $, %, & ., (, )] ";
		
		return false;
	}
	
	/* form is good */
	return true;
}

/**
 * Validates the given form and it's field for known bad characters.  If
 * any "bad" characters are found, the form is not submitted and an error
 * message is displayed.
 */
function validateFormSubmit(form) {
	/* list to contain the form element ids containing invalid characters */
	var invalid = new Array();
	var count = 0;
	
	/* loop through all the elements in the form */
	for (var i = 0; i < form.elements.length; i ++) {
		var element = form.elements[i];
		var value = element.value;
		
		/* ensure that the value contains only good characters */
		if (good.test(value) == false) {
			/* add the field id to the list 'bad' list */
			invalid[count] = element.id;
			count++;
		}
	}
	
	/* check to see if there are any ids to report */
	if (invalid.length != 0) {
		/* update the forms invalid area and return */
		/* TODO: update the form */
		alert("Invalid form fields : " + invalid);
	}
}


/**
 * Helper function that clears the message area and error icons on a form.
 */
function reset(form) {
	var displayArea = document.getElementById("errorMessageArea");
	if (displayArea != null) {
		displayArea.style.display = "none";
	}
	
	/* update the form elements 'error' and style */
	for (var i = 0; i < form.elements.length; i ++) {
		var element = form.elements[i];
		var id = element.id;
		
		/* look for an error image with this id */
		var error_img = id + "-input";
		var img = document.getElementById(error_img);
		if (img != null) {
			/* ensure the image is hidden */
			img.setAttribute("class", "");
		}
	}
}

/**
 * Helper that validates required fields and safe values.
 * 
 * @param form
 * @return
 */
function validateRequiredAndSafeFormSubmit(form) {
	/* call required and safe form submit */
	var ret = validateRequiredFields(form);
	if (ret == true) {
		/* call safe form submit */
		ret = validateFormSafeSubmit(form);
	}
	return ret;
}

/**
 * Helper function that displays the error message block with the error message
 * provided.
 * @param message
 * @return
 */
function displayError(message) {
	/* enable the display area */
	var displayArea = document.getElementById("errorMessageArea");
	if (displayArea != null) {
		displayArea.style.display = "block";
	}
	
	/* update the text in the message box */
	var messageTxt = document.getElementById("error_message_text");
	messageTxt.innerHTML = message;
}