var AjaxForm = Class.create(Eventable, {
	/*
	* fired Events
	*  - requestStart
	*  - requestFinished (OOK aangeroepen als de request faalt)
	*  - requestFailed
	*  - requestSuccess
	*  - responseError memo: responseText
	*  - processError memo: exception
	*  - response memo : response
	*/
						
	form : null,

	initialize : function(formID)
	{
		this.form = $(formID);
		
		this.firebugConnect();
		
		this.convertSubmits();
	},
	
	firebugConnect : function()
	{
		if(typeof(console) == "undefined" || !console.error) return;
		
		this.observe("responseError", function(event)
		{
			text = event.memo.responseText;
			
			if(text) text = text.replace(/(<([^>]+)>)/ig,"");
			else text = "[empty string]"; 
			console.error("Error in response:\n\n"+text);
		});
		
		this.observe("processError", function(event)
		{
			console.error(event.memo.exception);
		});
		
		this.observe("requestFailed", function(event)
		{
			console.error("AjaxForm error", event.object);
		});
	},
	
	convertSubmits : function()
	{
		while(submitButton = this.form.down("input[type=submit]"))	
		{
			// Een keer raden in welke browser het niet werkte om gewoon
			// submitButton.type = "button";
			// te gebruiken...
			
			button = new Element("input", {"type":"button", "value" : submitButton.value});
			
			button.observe("click", this.send.bindAsEventListener(this));
			
			/*
			* why not submitButton.replace?
			* --> http://prototypejs.org/api/element/replace
			*/
			Element.replace(submitButton, button);
		}
	},
		
	send : function(event)
	{	
		event.stop();
		
		// tinyForms triggeren
		if(typeof(tinyMCE) != "undefined" && typeof(tinyMCE.triggerSave) != "undefined")
		{
			tinyMCE.triggerSave();
		}
		
		requestOptions = {
			onSuccess : this.handleResponse.bindAsEventListener(this),
			onFailure : this.handleFailure.bindAsEventListener(this)
		}
		
		this.form.request(requestOptions);
		
		this.form.disable();
		
		this.fire("requestStart");
	},
	
	handleResponse : function(transport)
	{
		try
		{
			response = transport.responseText.evalJSON();
			
			try
			{
				this.processResponse(response);
			}
			catch(e)
			{
				this.fire("processError", {exception : e});
			}
			
		}
		catch(e)
		{
			this.fire("responseError", {responseText : transport.responseText});
		}
		
		this.form.enable();
		this.fire("requestFinished");
		this.fire("requestSuccess");
	},
	
	processResponse : function(response)
	{
		this.fire("response", {response : response});
		
		if(response["alert"])
		{
			alert(response["alert"]);
		}
		
		if(response["js"])
		{
			eval(response["js"]);
		}
		
		if(response["reload"])
		{
			this.reset()
			location.reload();
			return;
		}
		
		if(response["reset"])
		{
			this.reset();
		}
	},
	
	reset : function()
	{
		this.form.reset();
		
		if(typeof(tinyMCE) != "undefined"){
			if(typeof(tinyMCE.execCommand) != "undefined"){
				tinyMCE.execCommand('mceSetContent', false,"");
			}
		}
		
		this.form.enable();
	},

	handleFailure : function()
	{	
		this.fire("requestFinished");
		this.fire("requestFailed");
	}
});