/**
 * eBCL Form Object - Mootooled Form for online orders
 * 
 * requires Mootools v1.1
 *
 * @version		0.1
 * @author		Corneanu Ciprian Sandu <csciprian [at] yahoo [dot] com>
 * @copyright	Authors
 */

FormOrderOnline = new Class({

	order: 'photoupload-details',
	
	upload: 'photoupload',
	
	persoana: {
		fizica: 'p-fizica',
		juridica: 'p-juridica'
	},
	
	orderKey: null,

	initialize: function(order) {
		this.order = $(this.order);
		this.upload = $(this.upload);
		this.persoana.fizica = $(this.persoana.fizica);
		this.persoana.juridica = $(this.persoana.juridica);
		
		this.order.reset();
		this.persoana.juridica.setStyle('display', 'none');

		$$(this.order['persoana']).each(function(el) {
			el.addEvent('click', function(el){
				this.tooglePerson(el);
			}.bind(this, el));
		}.bind(this));
		
		this.order.addEvent('submit', function(e){
			new Event(e).stop();
			this.order.send({
				onComplete: function(rsp) {
					this.parseResp(rsp);
				}.bind(this)
			});
		}.bind(this));
	},
	
	tooglePerson: function(el) {
		if(el.value == 'fizica') {
			this.persoana.juridica.setStyle('display', 'none');
			this.persoana.fizica.setStyle('display', 'block');
		} else {
			this.persoana.juridica.setStyle('display', 'block');
			this.persoana.fizica.setStyle('display', 'none');
		};
	},

	parseResp: function(rsp) {
		var m = rsp.substr(0, 6).toLowerCase();
		
		if ( m == 'eroare' )
		{
			alert(rsp);
		}
		else if ( m == 'succes' ) 
		{
			this.upload.setProperty('action', this.upload.getProperty('action')+'/'+rsp.substr(9));

			new FancyUpload($E('.fileupload-photo'), {
				swf: 'http://www.copyprint.ro/files/uploader/Swiff.Uploader.swf',
				container: $('upload-box')
			});

			this.order.setStyle('display', 'none');
			this.upload.setStyle('display', 'block');

			var winScroller = new Fx.Scroll(window);
			winScroller.toElement(this.upload);
		}
	}
});
