		
		var validation_rules = new Array();
		
		function check_field(evt) {
			// first get the event
			if (!evt) var evt = window.event;
			// now get the target element/form field
			var field;
			if (evt.target) field = evt.target;
			else if (evt.srcElement) field = evt.srcElement;
			if (field.nodeType == 3) // defeat Safari bug
				field = field.parentNode;
			
			var field_type = field.type;
			
			switch(field_type) {
				case "text":
					return check_text_field(field);
				break;
				case "password":
					return check_text_field(field);
				break;
				case "textarea":					
					return check_text_area(field);
				break;
			}	
		}
		
		function validate_form(evt) {
			// first get the event
			if (!evt) var evt = window.event;
			// now get the target form
			var form;
			if (evt.target) form = evt.target;
			else if (evt.srcElement) form = evt.srcElement;
			if (form.nodeType == 3) // defeat Safari bug
				form = form.parentNode;
			
			// the main boolean value for validating the entire form
			var form_valid = true;
			var val_count = 0;
			
			// now loop through all of the forms fields and validate them
			for(i=0; i<form.elements.length; i++){
				var field = form.elements[i];
				var field_type = field.type;				
			
				switch(field_type) {
					case "text":						
						if(!check_text_field(field))
							val_count--;
					break;
					case "hidden":						
						if(!check_hidden_field(field))
							val_count--;
					break;
					case "password":						
						if(!check_text_field(field))
							val_count--;
					break;
					case "textarea":						
						if(!check_text_field(field))
							val_count--;
					break;
				}
			}
			if(val_count < 0)
				form_valid = false;
			else
				form_valid = true;			
			
			if (evt.preventDefault) {
				if (form_valid == false)
		    		evt.preventDefault(); // DOM/Mozilla method to cancel the submission  	
			}else{
				// works in IE
				evt.returnValue = form_valid;
			}
		}
		
		function check_text_field(field) {
			// get the validation rule for this field
			var rule = validation_rules[field.getAttribute("name")];
			
			// if no rule is defined for the field, return true
				if(!rule)
					return true;
						
			// now use the rule regular expression to validate the field
			if (!field.value.match(rule)) {				
				raise_error(validation_rules[field.name+"_msg"]);
				field.style.backgroundColor = 'yellow';
				return false;
			}else{
				if(field.style.className != "readonly")
					field.style.backgroundColor = 'white';
				return true;
			}
		}
		
		function check_hidden_field(field) {
			// get the validation rule for this field
			var rule = validation_rules[field.getAttribute("name")];
			
			var displayField = document.getElementById(field.getAttribute("name")+"_display") 
			
			// if no rule is defined for the field, return true
				if(!rule)
					return true;
						
			// now use the rule regular expression to validate the field
			if (!field.value.match(rule)) {				
				raise_error(validation_rules[field.name+"_msg"]);
				displayField.style.backgroundColor = 'yellow';
				return false;
			}else{
				if(field.style.className != "readonly")
					displayField.style.backgroundColor = 'white';
				return true;
			}
		}
		
		function check_text_area(field) {
			// get the validation rule for this field
			var rule = validation_rules[field.getAttribute("name")];
			
			// now use the rule regular expression to validate the field
			if (!field.value.match(rule)) {				
				raise_error(validation_rules[field.name+"_msg"]);
				field.style.backgroundColor = 'yellow';
				return false;
			}else{
				field.style.backgroundColor = 'white';
				return true;
			}
		}
		
		function raise_error(msg) {
			alert(msg);	
		}
		
		function addFormEvent(obj, type, fn) {			
		  	if (obj.attachEvent) { 
		    	obj['e'+type+fn] = fn; 
		    	obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
		    	obj.attachEvent('on'+type, obj[type+fn]); 
		  	}else{
		    	obj.addEventListener( type, fn, false );
			}
		} 
		function removeFormEvent(obj, type, fn) { 
		  	if (obj.detachEvent) { 
		    	obj.detachEvent('on'+type, obj[type+fn]); 
		    	obj[type+fn] = null;
		  	}else{
		    	obj.removeEventListener( type, fn, false );
		    } 
		} 
		
		
		function addFormListeners() {
			var no_page_forms = document.forms.length;
			
			for(j=0; j<no_page_forms; j++) {
				// add the submit listener per form
				addFormEvent(document.forms[j], "submit", validate_form);				
			}
		}
		
		addOnloadEvent(addFormListeners);