/* 

	ls_form_validation: ver 0.1
	Experimental form validation script
	If form has validation="true" then script fires
	Iterates through the input fields (only input for the time being)
	and checks to see what kind of validation needs to be done
	
*/

	var vc = [];
	var vc_email_check = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,4})$/;

	window.addEvent('domready',function(){
	
		// Start parsing all forms of the document
		$$('form').each(function(f){
	
			// If form has been set to "validate"
			if (f.getProperty('validate')) {
				
				var form_name = f.getProperty('name');
				vc[form_name] = new Array();
				// Start iterating through this form, 
				// and store references to the objects that need to be validated
				f.getElements('input').each(function(e){
					if (e.getProperty('validate')) {
						
						// Store a reference to this element
						vc[form_name].push(e)
						
						// This we'll be validating, we also need to add a handler for the focus event 
						// in order to clear of the error message
						e.addEvent('focus',function(){ 
							var id = e.id;
							if ($(id+'_error')) {
								$(id+'_error').remove();
							}
						});
						
					}
				});
				
				// Add the on submit event handler to the form
				f.addEvent('submit',function(e) {
					
					var e = new Event(e);
					e.stop();
					
					// Start the validation process
					var form_name = this.getProperty('name');
					
					// Initially assume we will not find any errors
					var proceed = true;
					var msg = "";
					
					// Iterate through all elements of this form
					// that we know have the "validation" property set.
					vc[form_name].each(function(elem) {
						
						// Get the various properties of this element
						var validation = elem.getProperty('validate');
						var validation_text = elem.getProperty('validationtext');
						var id = elem.getProperty('id');
						var value = elem.getValue().trim();
						// If custom validation text has been supplied
						if (!validation_text) {
							validation_text = "Element: "+elem.id+" is empty!";
						}
						
						// NON EMPTY VALIDATION
						if (validation == "not_empty" && value == "") {
							proceed = false;
							// Create the error span (only if it does not exist
							if (!$(id+'_error')) {
								temp = new Element('div',{'id': id+'_error','class':'red'});
								temp.setHTML(validation_text);
								temp.injectBefore(elem);
							}
						}
						
						// EMAIL VALIDATION
						if (validation == "email" && !elem.getValue().test(vc_email_check)) {
							proceed = false;
							// Create the error span (only if it does not exist
							if (!$(id+'_error')) {
								temp = new Element('div',{'id': id+'_error','class':'red'});
								temp.setHTML(validation_text);
								temp.injectBefore(elem);
							}
						}
						
					});
					
					if (proceed) {
						this.submit();	
					}
					
									
				});
							
				// Add an event to the reset handler to clear all error messages
				f.addEvent('reset',function(e) {
					this.getElements('div').each(function(d){
						if (d.getProperty('id').contains('_error')) {
							d.remove();
						}
					});
				});
				
			}
			
			
		});
	
	});