/* ******************************************************** */
/*           AjaxForm & QuickContact Classes v1.0           */
/*              Created by Tibor Szegedi, 2011.             */
/* ******************************************************** */
/* Free for personal use.                                   */
/* For more information on usage and commercial licencing:  */
/* http://www.creativedesign.hu/tszegedi                    */
/* ******************************************************** */
/* MooTools v1.2 required: http://mootools.net/             */
/* ******************************************************** */

var AjaxForm = new Class({		
	Implements: [Options, Events],
	options: {
		url:			null,
		alertmode:		'all',	// Modes: all, success, error, none
		
		showresponse:	'none', // Modes: all, success, error, none -> puts response text into responsetag if defined
		responsetag:	null,
		
		handlemode:		'none', // Modes: all, success, error, none -> calls the function associated to handler if defined
		handler:		null,
		
		errorhandler:	$empty,	// calls the associated function if an ajax error occurs
		
		handlepriority: ['showalert','responsetag','handler'],
		
		oncomplete:		$empty,
		onsuccess:		$empty,
		onfailure:		$empty
		},        
	
	initialize: function(form,options){
			
		this.setOptions(options);
		
		if (!($defined(form))) return 0;
		if (!($defined($(form)))) return 0;
		
		if ($defined(this.options.url))  $(form).set('action',this.options.url);
		
		$(form).set('send', {
			onSuccess: function(resp) {
				if ($defined(resp)) {
					var data=JSON.decode(resp);
					if ($defined(data.success)) {
						this.options.handlepriority.each(function(el) {
							var fnc='this.__'+el;							
							eval(fnc)(data,this.options);
							}.bind(this));
						} else this.options.errorhandler.run('badresponse');
					} else this.options.errorhandler.run('noresponse');
				this.options.onsuccess.run(resp);
				}.bind(this),
			onFailure: function(resp) {
				this.options.errorhandler.run(resp);
				this.options.onfailure.run(resp);
				}.bind(this),
			onComplete: function(resp) {
				this.options.oncomplete.run(resp);
				}.bind(this)
			});
			
		$(form).addEvent('submit',function(e) {
			e.stop();
			$(this).send();
			return false;
			});
		
		},
		
	__showalert: function(data,opts) {
		if (data.success=='1') {
			if ((opts.alertmode=='all') || (opts.alertmode=='success')) alert(data.text);
			} else {
			if ((opts.alertmode=='all') || (opts.alertmode=='error')) {
				if ($defined(data.errortext)) alert(data.errortext);
			 	 else opts.errorhandler.run('ajaxerror');
				}
			}
		},

	__responsetag: function(data,opts) {
		if (data.success=='1') {
			if ((opts.alertmode=='all') || (opts.alertmode=='success')) {
				if ($defined(opts.responsetag)) $(opts.responsetag).set('html',data.text);
				}
			} else {
			if ((opts.alertmode=='all') || (opts.alertmode=='error')) {
				if ( ($defined(data.errortext)) && ($defined($(opts.responsetag))) ) $(opts.responsetag).set('html',data.errortext);
			 	 else opts.errorhandler.run('ajaxerror');
				}
			}
		},

	__handler: function(data,opts) {
		if (data.success=='1') {
			if ((opts.handlemode=='all') || (opts.handlemode=='success')) {
				if ($defined(opts.handler)) opts.handler.run(data);
				}
			} else {
			if ((opts.handlemode=='all') || (opts.handlemode=='error')) {
				if ($defined(opts.handler)) opts.handler.run(data);
				}
			}		
		}
			
	});
	
	
var QuickContactForm  = new Class({		
	Implements: [Options, Events],	
	
	initialize: function(container,text,options) {
		
		if (!($defined(container))) return false;
		if (!($defined($(container)))) return false;

		this.setOptions(options);
		
		if (!($defined(text))) {
			var text=['Name','E-Mail','Phone','Message','Submit'];
			}

		var classname=this.options.class;
		
		var qcfform = new Element('form').inject($(container));
		if ($defined(classname)) qcfform.addClass(classname);
				
		if (text[0]!=null) {
			new Element('span').set('html',text[0]).inject(qcfform);
			new Element('br').inject(qcfform);
			}
		new Element('input',{'type':'text','name':'name'}).inject(new Element('label').set('html',text[1]+':').inject(qcfform));
		new Element('br').inject(qcfform);
		new Element('input',{'type':'text','name':'email'}).inject(new Element('label').set('html',text[2]+':').inject(qcfform));
		new Element('br').inject(qcfform);
		new Element('input',{'type':'text','name':'phone'}).inject(new Element('label').set('html',text[3]+':').inject(qcfform));
		new Element('br').inject(qcfform);
		new Element('label').set('html',text[4]+':').inject(qcfform);
		new Element('br').inject(qcfform);
		new Element('textarea',{'name':'message'}).inject(qcfform);
		new Element('br').inject(qcfform);
		new Element('input',{'type':'submit','name':'submit','value':text[5]}).inject(qcfform);
		
		clearinputs=function(){	
			qcfform.getElements('input').each(function(el){
				if (el.get('name')!='submit') el.set('value',null);
				});
			qcfform.getElements('textarea')[0].set('html',null);
			}
			
		if (this.options.clearonsuccess) this.options.onsuccess=clearinputs.bind(this);
		if ( (this.options.clearoncomplete) && (!this.options.clearonsuccess) ) this.options.oncomplete=clearinputs.bind(this);		
		
		new AjaxForm(qcfform,this.options);
		
		}
	
	});
