/* TODO:
	- make this more automatic
		- make this file have a Validations object with functions that take an input
			- Validations.required
			- Validations.email
			- more?
		- add classes to input validate-XXX
		- do this at end of this file:
			Event.observe(window, 'load', function() {
				$$('form').each(function() {
					// go through inputs
					// look for validate-XXX classes
					// take XXX and use it as a property of Validations
					// add this function as an event handler onsubmit of the forms
				});
			});
		- so <input class="validate-email" /> will call Validations.email when the form is submitted
*/


	function trigger_field_error(field, type, message) {
		field.extend;
		field.addClassName('error');

		// put message in label and add error class
		label = field.next('label');
		if(label)
			label.update(message).addClassName('error');
	}

	function valid_email_address(address){	
		var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
		if(pattern.test(address)) return true;   
		else return false; 
	}







	function validate_comment_form(event) {
		form = event.element();
		error = false;
  		if(!$('comment_author').value) {
			trigger_field_error($('comment_author'), 'blank', 'Name is Required');
  			error = true;
  		}
  		email = $('comment_author_email');
		if(email.value != false && !valid_email_address(email.value)) {
			trigger_field_error(email, 'invalid', 'Invalid Email Address');
  			error = true;
  		}
  		if(!$('comment').value) {
			trigger_field_error($('comment'), 'blank', 'Comment is Required');
	  		error = true;
	  	}

	  	if(error) Event.stop(event); // don't submit
	  	return !error;
	}
	
	
	// add event handlers
	Event.observe(window, 'load', function() {
		if($('comment-form')) $('comment-form').observe('submit', validate_comment_form);
	});
