function FormControl(FormID, Delegate, ErrorHandler, PassedHandler){
	this.Delegate=Delegate;	
	this.Labels=[];
	
	this.ErrorHandler=ErrorHandler;
	this.PassedHandler=PassedHandler;
	
	var self=this;
	DOMReady(function(){
		self.Form=DOM(FormID);
		req=/\breq\b/;
		for (i=0; i<self.Form.elements.length; i++){
			if (req.test(self.Form.elements[i].className)){
				if (self.Form.elements[i].type=='text'||self.Form.elements[i].type=='password'||self.Form.elements[i].tagName=='textarea'){					
					 AttachEvent(self.Form.elements[i], 'keyup', self.OnInputChanged_Factory(self.Form.elements[i]));;
				}				
				AttachEvent(self.Form.elements[i], 'change', self.OnInputChanged_Factory(self.Form.elements[i]));			
				
			}
			if (self.Form.elements[i].type=='submit'){
				self.Submit=self.Form.elements[i];
				AttachEvent(self.Submit, 'click', function(event){self.SubmitClick(event);});
			}
		}
		self.OnInputChanged(null);
		self.ErrorHandler=ErrorHandler;		
	})
}

FormControl.prototype.SubmitClick= function(event){
	if (this.ErrorInput){
		this.ErrorInput.focus();
		DOM_Stop(event);
	}
}

FormControl.prototype.OnInputChanged_Factory= function(Input){
	var self=this;
	return function(){
		self.OnInputChanged(Input);
	}
}

FormControl.prototype.OnInputChanged= function(Input){

	if (this.checkForm(this.Form, Input)){
		if (this.Submit){
			this.Submit.setAttribute('title','');
			this.Submit.disabled=false;
			if (this.ErrorHandler) this.ErrorHandler(null, null);
		}
	}		
	else{		
		if (this.Submit){
			this.Submit.setAttribute('title',this.ErrorMessage);
			this.Submit.disabled=true;
		}
		if (this.ErrorBox&&this.ErrorBox.htmlFor==Input.id) this.ErrorBox.style.display='none';		
	}
}

FormControl.prototype.OnSubmit= function(event){	
	if (this.checkForm()){
		return true;
	}
	else {
		DOM_Stop(event);
		if (this.ErrorBox){
			this.ErrorBox=this.ErrorMessage;
			if (this.ErrorInput) this.ErrorInput=this.ErrorBox.htmlFor=this.ErrorInput.id;
		}
	}
}

FormControl.prototype.checkForm= function(Input){
	this.ErrorMessage=null;
	this.ErrorInput=null;
	this.Delegate(this.Form, Input);
	return (this.hasError())?false:true;
}

FormControl.prototype.Error= function(Message, Input){
	this.ErrorMessage=Message;
	this.ErrorInput=Input;	
	if (this.ErrorHandler) this.ErrorHandler(Message,Input);
}

FormControl.prototype.safeError= function(Message, Input){
	if (!this.ErrorMessage) this.Error(Message,Input);
	detachClass(Input,'passed');
	if (lbl=this.Labels[Input.id]){
		detachClass(lbl,'passed');
		attachClass(lbl,'error');
		lbl.setAttribute('title',ErrorMessage.StripTags());
	}
}

FormControl.prototype.hasError= function(){
	return (this.ErrorMessage)?true:false;
}

FormControl.prototype.Passed= function(Input){
	if (this.PassedHandler) this.PassedHandler(Input);
}
