/*
* validate.js
* checks and validates the inputs from a given form
* error checking is set by using classnames like 'required' or 'email'
*/

function validate(pForm, pUsingSubmit)
{
	//alert(pForm);
	var errors = '';
	var email_errors = '';
	var inputs = $ES('input', pForm);
	var email = /^.*@.*\..*$/;
    var usingSubmit = pUsingSubmit;

	//loop through the array of form inputs and check for errors
	for (var i = 0; i < inputs.length; i++)
	{
		var input = inputs[i];
		
		
		//required
		if(input.hasClass('required') && input.value == '')
		{
			errors += make_readable(input.name) + '\n';
		}
		
		//required checkbox
		if(input.hasClass('checkbox') && input.checked != 1)
		{
			errors += make_readable(input.name) + '\n';
		}
		
		//email
		if(input.hasClass('email') && !email.test(input.value))
		{
			email_errors +=  make_readable(input.name) + ', must be a valid email address\n';
		}
		
		//other options can go here
		
	}
	
	errors += email_errors;
	
	//report errors if there are any, or just submit the form
	if(errors == '')
	{
		if(!usingSubmit) pForm.submit();
	}
	else
	{
		alert('Please complete the following fields:\n\n' + errors);
		return false;
	}
}

function make_readable(str)
{
	return str.replace("_", " ", "gi").capitalize();
}
