
var known_bad = "[^A-Za-z0-9_ ,.:@/\-]";
var valid_email = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;


// format_currency()
// desc: formats the value of the element provided.
function formatCurrency(element) {
	
	// make sure the value of the element is a number
	if (isNaN(element.value)) {
		// do nothing
		return;
	}
	
	var amount = element.value;
	
	element.value = parseFloat(amount).toFixed(2);
	return;
}


// resetForm
// desc: removes all error messages and clears the message box
function resetForm(form) {
	
	// hide the msg area
	$('#message-area').hide();
	$('#spacer').hide();
	
	// clear the list in the msg area
	$('#msg-list').empty();
	
	// reset the classes on every element
	for (var i = 0; i < form.elements.length; i ++ ) {
		$("#" + form.elements[i].id).removeClass('error');
	}
}


function isNumeric(ele) {
	// make sure the value is a number
	if (isNaN(ele.value)) {
		// item is invalid, reset the form
		$("#" + $ele.id).addClass('error');
		$("#msg-list").append("<li>" + $ele.id + " is Invalid</li>");
		$('#message-area').show();
		$('#spacer').show();
		return false;
	} 
}

function validateEmail(ele) {
	// ensure the field is valid	
	if (!valid_email.test(ele.value)) { 
	
		if ($("#message-area").css("display") != "block") {
			$('#message-area').show();
			$('#spacer').show();
		}
		$("#" + ele.id).addClass('error');
		$("#msg-list").append("<li>" + ele.id + " is Invalid</li>");
	} 
}

// validateFields()
// desc: validates the fields from the form
function validateFields(form) {
	var required = new Array();
	var invalid = new Array();
	
	// loop through the form elements
	for (var i = 0; i < form.elements.length; i ++ ) {
		// check to make sure the form element is not empty
		var ele = form.elements[i];
		if (ele.id.indexOf('required') != -1) {
			if (ele.value == "" || ele.value == undefined) {
				// add the id to the invalid list
				required.push(ele.id);
			}
		}
		
		if (ele.id.indexOf('numeric') != -1) {
			// make sure it's a number
			if (isNaN(ele.value)) {
				invalid.push(ele.id);
			}
		}
		
		// verify that there are no invalid characters
		if ((ele.type == "text" || ele.type == "textarea" || ele.type == "password") && (ele.id.indexOf('ignore') == -1)) {
			if (ele.value.match(known_bad)) {
				// item is invalid
				invalid.push(ele.id);
			}
		}
		
		if (ele.id.indexOf('email') != -1) {
			if (!valid_email.test(ele.value)) { 
				// item is invalid
				invalid.push(ele.id);
			}
		}
	}
	
	if (invalid.length != 0 || required.length != 0) {
		// clear the list in the msg area
		$('#msg-list').empty();
		
		// update the style on the elements that are bad
		for (var i = 0; i < invalid.length; i ++) {
			var id = invalid[i];
			$("#" + id).addClass('error');
			$("#msg-list").append("<li>" + id + " is Invalid</li>");
		}
		
		// update the style on the elements that are bad
		for (var i = 0; i < required.length; i ++) {
			var id = required[i];
			$("#" + id).addClass('error');
			$("#msg-list").append("<li>" + id + " is Required</li>");
		}
		
		// enable the message area
		$('#message-area').show();
		$('#spacer').show();
		return false;
	}
	return true;
}
