/**
* @author Laurent Dinclaux <laurent.dinclaux@gmail.com>
* @since  Monday Jul 18 2006
*/

if (Object.isUndefined(Gaiak)) { var Gaiak = { }; };
if (Object.isUndefined(Gaiak.Httpr)) { Gaiak.Httpr = { }; }
 
Gaiak.Httpr.JsonForm = Class.create( {
	
  	initialize: function (form) {
		
		var e = Prototype.emptyFunction;
		
		this.form 			= $(form);
		this.onSuccess      = e;
		this.onComplete     = e;
		this.onBeforeSubmit = e;
		this.onError 	= e;

		this.resDiv = null;
		this.statDiv = null;
		
		this.form.observe('submit',  this.submit.bindAsEventListener(this));

	},

	init: function () {
		this.form.observe('submit',  this.submit.bindAsEventListener(this));
	},

	/**
	* Call this one to submit your form
	*/
	submit : function (event) {

		
		this.makeHelpers();
		
		// hide result div
		this.resultDiv.hide();
		

		// xhr_status
		this.statusDiv.update('Chargement ...');
		this.statusDiv.show();
		
		this.statusDiv.scrollTo();
		
		this.onBeforeSubmit();
		// scroll top
		// scroll(0,this.scrollTo);

		// build query string from form
		var data = this.form.serialize() + '&HTTPR=1';

		// disable form
		try {this.form.disable();} catch(e) {}
		
		//clear errors
		//this.clear_form_errors(this.form);

		// get form method
		var formMethod = this.form.method;
		var method = formMethod.toLowerCase();

		this.clear_form_errors();
			
		var myAjax = new Ajax.Request( this.form.action,
										{
											method: method,
											parameters: data,
											onComplete: this.handler.bind(this)
											
										});
		event.stop();
	},

	// XHR handler
	handler : function(xhr, response) {
		
		// case bad status code get returned, such as 404
		if( typeof(xhr) == 'number' ) {

			this.statusDiv.update("Error: " + xhr);
			this.statusDiv.show();

		}
		else {
		 	try {
				var response = xhr.responseText.evalJSON(true);
			}
			catch (ex) {
				alert(xhr.responseText);
				this.statusDiv.update('Erreur. Réessayez.');
				this.form.enable();
			}
			/**
			 * Response is JSON
			 */
			 if( response ) {
	
				/**
				 * status repport errors occured
				 */
				if( response.status != 1 ) {
	
					this.show_form_errors (response.errors);
					this.statusDiv.update('Chargé.');
					this.form.enable();
					this.statusDiv.hide();
					
					this.onError.bind(this);
					
	
				}
	
				//  no error, redirect to user list
				else {
					this.statusDiv.update('Traitement en cours. Patientez.');
					this.onSuccess().bind(this);
	
				}
	
	
			}
			
			this.onComplete().bind(this);
		}

	},

	// show form errors
	show_form_errors : function (errors) {
		
		// display errors using DOM
		var errt 	= new Element('H1').update('Désolé, il y a des erreurs');
		var domerr 	= new Element('UL');
		
		errors.each(

			function(error) {
				/* create error list */
				var container 	= new Element('LI').update(error.message);
				domerr.appendChild(container);

				/* set fields class names */
				var fields = error.fields;
				var fields_on_error = fields.split(',');
				
				fields_on_error.each( function(fieldID) {
											  
					if(field = $(fieldID)) field.addClassName('errorField');
				});
			}
		);

		// show errors
		this.resultDiv.update(errt).appendChild(domerr);
		this.resultDiv.show();
		this.statusDiv.update('Chargé.');
	},
	

	clear_form_errors : function (form) {

		// clear previous error messages
		this.resultDiv.hide().update('');

		// clear classname from error fields
		if( this.form.elements ) {
			for(var i=0; i<this.form.elements.length; i++) {
				/* set fields class names */
				var field = $(this.form.elements[i]);
				field.removeClassName('errorField');
				
			}
		}
		return this;
	},
	
	makeHelpers: function() {
		
		if( !this.statusDiv ) {
			this.statusDiv = new Element('DIV', {className: 'FormStatus', style: "display:none"});
			this.form.insert( { top: this.statusDiv } );
		}
		
		if( !this.resultDiv ) {
			this.resultDiv = new Element('DIV', { className: 'FormResult', style: "display:none"});
			this.form.insert( { top: this.resultDiv } );
		}
	},
	
	redirect : function (url) {
	
		document.location = url;
		return this; 
	
	},
	
	reload : function () {
		window.location.reload();
		return this; 
	},
	
	onBeforeSubmitEvent : function( funct ) {
		this.onBeforeSubmit=funct;
	}
})