function formSubmit(target) {
    return true;
	
    target = $(target);
	if(!isUndefinedOrNull(target.onsubmit)) {
	    if(!target.onsubmit()) return false;
    }
	return target.submit();
}


function ValidatorContainer() {
	this.validators = [];
}

ValidatorContainer.prototype = {
	'initialize' : function(target) {
		log('hookForms', target);
		this.add = bind(this.add, this);
		map(this.add, target.getElementsByTagName('label'));
		target.onsubmit = bind(this.onsubmit, this);
	},

	'add' : function(target) {
		if(!isUndefinedOrNull(target.getAttribute('valid:cond'))) {
			var v = new Validator;
			v.initialize(target);
			this.validators.push(v);
		}
	},

	'onsubmit' : function() {
		log('onsubmit');
		var allValid = true;
		for(var i in this.validators) {
			var r = this.validators[i].validate();
			if(!r) allValid = false;
		}
		return allValid;
	}
		
}		

	


function Validator() {
	this.control = null;
	this.label = null;
	this.message = null;
	this.oldLabel = null;
	this.errorDiv = null;
}

Validator.prototype = { 
	'initialize' : function(target) {
		log('new Validator', target);
		target = $(target);
		this.validate = bind(this.validate, this);
		this.label = target;
		this.cond = target.getAttribute('valid:cond');
		this.message = (isUndefinedOrNull(target.getAttribute('valid:msg'))) ? "Please enter a value" : target.getAttribute('valid:msg');
		this.control = $(this.label.htmlFor);
		this.errorDiv = DIV({'class':'errorDiv'}, '');
		this.control.parentNode.insertBefore(this.errorDiv, this.control);
		//.appendChildNodes(this.label, this.errorDiv);
	},

	'showError' : function() {
		if(isUndefinedOrNull(this.oldLabel)) this.oldLabel = this.label.innerHTML;
		this.errorDiv.innerHTML = this.message;
		addElementClass(this.label, 'error');
	},

	'clearError' : function() {
		if(this.oldLabel != null) {
			removeElementClass(this.label, 'error');
		    swapDOM(this.errorDiv, false);
		    this.errorDiv = DIV({'class':'errorDiv'});
			this.control.parentNode.insertBefore(this.errorDiv, this.control);
		}
	},

	'validate' : function() {
		
		if(this.cond == 'notEmpty') {
			if(!strip(this.control.value)) {
				this.showError(); return false;
			} else {
				this.clearError();
			}
			
		} else if (this.cond == 'checked') {
			if(!this.control.checked) {
				this.showError(); return false;
			} else {
				this.clearError();
			}
		} else {
			log('UNKNOWN VALIDATOR', this.control.value);
		}

		return true;
	}
}


function hookForms() {
	forEach(getElementsByTagAndClassName('form', 'validForm'), function(f) { var i = new ValidatorContainer; i.initialize(f); });
	return true;
}

addLoadEvent(hookForms);
	
		
		
