/**
 * Base form for all form submits.
 */
var form = function()
{	
	// Define this to the request uri.
	this.requestUrl = null;
	// Data to be posted 
	this._data 		= null;
	// Errors.
	this._errors	= false;
	// Reference to the form element.
	this.$_form		= null;

	/**
	 * Abstract validation function, override this to add validation.
	 * @return void
	 */
	this.validate	= 	function()
						{	
						};
	
	this.init 		= 	function()
						{
							var $form 	= this.$_form;
							var $this 	= this;
							
							$form.find(".generic-button.submit .hitbox")
								.click(
									function()
									{
										$form.submit();
									}
								);
				
							$form.submit(
									function(evt)
									{
										evt.stopPropagation();
										$this.onSubmit();
										return false;
									}
								);
						}
	
	/**
	 * Do the AJAX request.
	 * @return void
	 */
	this.doRequest 	=	function()
						{
                            if (this.preRequest != null)
                            {
                                this.preRequest();
                            }

							if (this.requestUrl != null)
							{
								var $this = this;
								if (this._data == null)
								{
									this._data = this.$_form.serialize();
								}

								$.post(this.requestUrl, this._data, function(response)
								{
									$this.onResponse(response);
								}, 'json');

								this._data = null;
							}
							else
							{
								alert ('Missing request url.');
							}
						};
	
	/**
	 * Callback for ajax onResponse request.
	 * @param {Object} response
	 */	
	this.onResponse = 	function(response)
						{
							if (response.status == 'succes')
							{
								this.onSucces(response);
							}
							else
							{
								this.onError(response);
							}
						};
	/**
	 * Callback for succes response.
	 * @param {Object} response
	 */				
	this.onSucces	=	function(response)
						{	
							// clean up form.
							this.$_form.find("input").val('');
						};
						
	/**
	 * Callback for error responses.
	 * @param {Object} response
	 */
	this.onError    = 	function(response)
						{
							this.setErrors(response.messages);
							this.displayErrors();
						};
	/**
	 * Overwrite the error property
	 * @param {Object} errors
	 */
	this.setErrors	= 	function(errors)
						{
							this._errors = errors;
						};
	
	/**
	 * Default function to bind to the form's onsubmit event listener.
	 */	
	this.onSubmit 	=	function()
						{
							this.validate();

							if (this.hasErrors())
							{
								this.displayErrors();
							}
							else
							{
								this.doRequest();
							}
						};	
	
	/**
	 * Has errors?
	 * @return boolean
	 */
	this.hasErrors	=	function()
						{
							return (this._errors !== false);
						}
	
	/**
	 * Add an error message
	 * @param {Object} message
	 * @param {Object} name
	 */
	this.addError	=	function(message, name)
						{
							if (this._errors === false)
							{
								this._errors = {};
							}
							
							if (typeof this._errors[name] == 'undefined')
							{
								this._errors[name] = [];
							}
							var translated = translate(message);
							
							this._errors[name].push(translated);
						};
	
	/**
	 * Display errors, default behavior is alerting the messages.
	 */				
	this.displayErrors	= 	function()
							{
								if (this._errors != false)
								{
									var messages = [];
									for (var element in this._errors)
									{
										for (var message in this._errors[element])
										{
											if (typeof this._errors[element][message] != 'function')
											{
												messages.push(this._errors[element][message]);
											}
										}
									}
									
									alert (messages.join("\n"));
									
									this.clearErrors();
								}
							};
	/**
	 * Erease all errors.
	 */				
	this.clearErrors	= 	function()
							{
								this._errors = false;
							};
};
