BLCMS.Form=new Class({
	Implements: [Events, Options],
	
	inputs: null,

	options: {
		id:			null,
		sent:		false,
		ajax:		null,		
		sendvars:	{},

		autosubmit:	true,
		validator:	null,
		
		debug:		false
	},
	
	initialize: function(fid, options) {
		this.setOptions(options);
		this.form=$(fid);
		this.form.store('form', this);
		this.inputs=[];
		
		this.options.id=fid;
		
		new Element('input', {type: 'hidden', name: '___blcmsforms___', 'value': fid}).inject(this.form, 'top');
		
		this.form.addEvent('submit', function() {
			if (!this._submited) return this._formSubmit();
		}.bind(this));
	},
	
	addInput: function(input) {
		this.inputs.push(input);
	},
	
	_formSubmit: function(name) {
		var inval=this.form.getElement("input[name='___blcmsforms___']");
		inval.set('value', this.options.id + (name!=null ? ":" + name : ''));
		
		this._submited=true;
		this._name=name;
		
		if (this.options.autosubmit) return this.submit();
	},
	
	submit: function() {
		this.inputs.each(function(el) { el.fireEvent('submit', this._name); }.bind(this));

		this.fireEvent('submit', this._name);
		
		if (this.options.ajax) {
			var This=this;
			
			var submits=this.form.getElements('input[type=\'submit\']');
			
			submits.each(function(el) { el.disabled=true; });
			
			var vars=String.parseQueryString(this.form.toQueryString().trim());
			vars['___blcmsajax___']=this.options.ajax;

			vars=Object.merge(vars, this.options.sendvars);
			
			var req=new BLCMS.Request({
				url: this.form.get("action"),
				vars: vars,
				update: $(this.options.ajax),
				
				onComplete: function() {
					submits.each(function(el) { el.disabled=false; });
				},
			
				onError: function(e) {
					var err='Wystąpił problem techniczny.\nProsimy o kontakt z pomocą techniczną.';
					
					if (this.options.debug) err+='\n\n'+e.result;
					
					alert(err);
					
					submits.each(function(el) { el.disabled=false; });
				}.bind(this)
			});
			
			req.addEvent('success', function() { this.fireEvent('ajaxUpdate'); }.bind(this));
			
			req.send();
			
			return false;
		}

		return true;
	}
});

BLCMS.Form.Input=new Class({
	Implements: [Events],

	type: null,
	el: null,
	hint: null,
	value: null,
	name: null,
	
	initialize: function(params) {
		this.type=params.type;
		this.el=$(params.id);
		this.hint=params.hint;
		this.value=params.value;
		this.name=params.name;
		
		if (this.el) {
			this.initHint();
			
			switch(this.type) {
			case 'multi-text': new BLCMS.Form.Multi(this, params); break;
			}
		}
	},
	
	initHint: function() {
		switch(this.type) {
		case 'password':
		case 'float':
		case 'textarea':
		case 'text':
			var hp=function(t) {
				var val=this.el.get('value');
				
				if (t==0 && val==this.hint) {
					this.el.set('value', '');
					this.el.removeClass('hint');
				}
				
				if (t==1 && val=='') {
					this.el.set('value', this.hint);
					this.el.addClass('hint');
				}
			}.bind(this);

			this.el.addEvent('validate', function(errors) {
				var val=this.el.get('value');

				if (val==this.hint) errors.push(formcheckLanguage['required']);
			}.bind(this));
			
			this.el.set('value', this.value || this.hint);

			if (this.hint) {
				this.el.addEvent('focus', hp.bind(this, 0));
				this.el.addEvent('blur', hp.bind(this, 1));
				this.el.set('title', this.hint);
				
				if (this.value==null) this.el.addClass('hint');
			}
			
			break;
		}
	}
});

BLCMS.Form.Multi=new Class({
	input: null,
	newinput: null,
	
	initialize: function(input, params) {
		this.input=input;
		
		if (input.value) {
			input.value.each(function(v) {
				this.addInput(v);
			}, this);
		}
		
		this.addNewInput();
	},
	
	addNewInput: function(fade) {
		if (this.newinput) {
			this.newinput.removeClass('new');
		}
		
		this.newinput=this.addInput(null, fade);
		this.newinput.addClass('new');
		if (this.input.hint) this.newinput.addClass('hint');
		this.newinput.set('value', this.input.hint);
	},
	
	addInput: function(value, fade) {
		var li=new Element('li');
		
		var iname='form['+this.input.name+']';
		
		var input=new Element('input', {
			'value':	value,
			'type':		'text',
			'name':		iname
		});
		
		input.inject(li);
		
		input.addEvent('keypress', function() {
			var value=input.get('value');
			
			if (input==this.newinput) {
				if (!value) {
					this.addNewInput(true);
				}
			}
		}.bind(this));
		
		input.addEvent('focus', function() {
			if (input==this.newinput) {
				input.set('value', null);
				
				if (this.input.hint) input.removeClass('hint');
			}
		}.bind(this));
		
		input.addEvent('blur', function() {
			var value=input.get('value');
			
			if (input!=this.newinput && !value) {
				input.get('tween').addEvent('complete', function() {
					li.destroy();
				});
				
				input.fade(0);
			}
			
			if (this.input.hint && this.newinput==input) {
				this.newinput.set('value', this.input.hint);
				input.addClass('hint');
			}
		}.bind(this));
		
		if (fade) input.fade('hide');
		li.inject(this.input.el);
		if (fade) input.fade(1);
		
		return input;
	}
});
