var neteditAjaxForm = new Class({
	Implements: Options,
	
	options: {
		divId:'div-ajax',
		actionsSave:[],
		actionsLoad:[],
		stepsName:[],
		
		onSave: null,	/* IMPORTANTE - funzione da chiamare prima di salvare */
    	onSaveComplete: null,	/* IMPORTANTE - funzione da chiamare quando ha finito un salvataggio */
    	onLoad: null,	/* IMPORTANTE - funzione da chiamare prima di caricare */
    	onLoadComplete: null,	/* IMPORTANTE - funzione da chiamare quando ha finito il caricamento */
    	prevButtonId: 'prev-button',
    	primaryKey: 'id',
    	initId: 0,
    	appendUrl: ''
    },

    initialize: function(options){
 
    	this.setOptions(options);
    	this.currentStep = 0;
    	this.id = this.options.initId;
    	this.load();
    	this.delLastAction = false;
    },
    
    addEventsForm: function(){
    	var forms = $$("#"+this.options.divId+" form");
    	if(forms){
	    	var form = forms[0];
	    	if(form != undefined){
		    	form.addEvent('submit', function(e){
					 return this.save(e);
				}.bind(this));
	    	}
    	}
    },
    
    addEventsPrevButton: function(){
    	var button = $(this.options.prevButtonId);
    	if(button != undefined){
	    	button.addEvent('click', function(e){
				 this.prevStep(e);
				 return false;
			}.bind(this));
    	}    	
    },
     
    save: function(e){
    	//alert('SAVE: this.currentStep:'+this.currentStep+'\n'+this.options.actionsLoad[this.currentStep]);
    
    	var event = new Event(e);
    	var form = $(event.target);
    	
    	if(this.options.onSave){
	    	this.options.onSave(this.currentStep);
	    }
	    
	    if(this.options.actionsSave[this.currentStep] != undefined){
		    var actionSave = this.options.actionsSave[this.currentStep]+this.options.appendUrl+'#step'+this.currentStep;
	    	form.send(actionSave.replace(/\&amp;/g,'&'));
		    var request = form.get('send');
		   	request.onSuccess = function(txt, xml){
				try{
				
					// controlla la risposta di php
					var resp = eval('(' + txt + ')');
					// controlla se esiste una variabile id
					if(resp.id != undefined){
						this.id = resp.id;
					}
					
					// controlla se l'azione precedente era da eliminare
					if(this.delLastAction){
						this.delLastAction = false;
						this.options.actionsSave.pop();
						this.options.actionsLoad.pop();
						this.currentStep--;
						if(resp.addActionLoad != undefined || resp.addActionSave != undefined){
							// controlla se la prossima azione load deve essere aggiunta
							if(resp.addActionLoad != undefined ) this.options.actionsLoad.push(resp.addActionLoad);
							// controlla se la prossima azione seve deve essere aggiunta
							if(resp.addActionSave != undefined ) this.options.actionsSave.push(resp.addActionSave);
							this.delLastAction = true;
							this.currentStep++;
						}
					}else{
						this.currentStep++;
						if(resp.addActionLoad != undefined || resp.addActionSave != undefined){
							// controlla se la prossima azione load deve essere aggiunta
							if(resp.addActionLoad != undefined ) this.options.actionsLoad.push(resp.addActionLoad);
							// controlla se la prossima azione seve deve essere aggiunta
							if(resp.addActionSave != undefined ) this.options.actionsSave.push(resp.addActionSave);
							this.delLastAction = true;
						}
					}
					
					if(this.options.onSaveComplete){
				    	this.options.onSaveComplete(this.currentStep, resp);
				    }
					
				    
					this.load();
				}catch(e){
					alert(e);
				}
		   	}.bind(this);
		   	request.onRequest = function() {
				netedit.ajax.showLoading(this.options.divId);
			}.bind(this);
		   	return false;
	    }else{
	    	return true;
	    }
    },
    
    load: function(){
    	//alert('LOAD: this.currentStep:'+this.currentStep+'\n'+this.options.actionsLoad[this.currentStep]);
    
    	if(this.options.onLoad){
	    	this.options.onLoad(this.currentStep);
	    }
	    if(this.options.actionsLoad[this.currentStep] != undefined){
		    var actionLoad = this.options.actionsLoad[this.currentStep]+'&'+this.options.primaryKey+'='+this.id+this.options.appendUrl+'#step'+this.currentStep;
		    
	    	netedit.ajax.getData( 
				actionLoad.replace(/\&amp;/g,'&'),
				{
					method: 'get',
					onComplete: function(txt){
						try{
							var div = $(this.options.divId);
							if(div){
								div.set('html', txt);
							}
							this.addEventsForm();
							this.addEventsPrevButton();
							
							if(this.options.onLoadComplete){
						    	this.options.onLoadComplete(this.currentStep);
						    }
						}catch(e){
							alert('Errore: LOAD:\n'+e);
						}
						
					}.bind(this),
					onRequest : function() {
						netedit.ajax.showLoading(this.options.divId);
					}.bind(this)
				}
			);
	    }
    },
    
    prevStep: function(e){
    	var event = new Event(e);
    	var button = $(event.target);
    
    	button.set('disabled', true);
    	if(this.currentStep>0){
	    	
	    	if(this.delLastAction){
				this.delLastAction = false;
				this.options.actionsSave.pop();
				this.options.actionsLoad.pop();
			}
	    	this.currentStep--;
	    	this.load();
    	}
    },
});

/*
-----------------------------
---------- ESEMPIO ----------
-----------------------------

<script>
window.addEvent("domready", function(){
	var myNeteditAjaxForm = new neteditAjaxForm(
		{
			divId:'quiz-container',
			actionsSave:['<?=$this->urlAJAX(array('controller'=>'quiz','action'=>'formSave0'))?>','<?=$this->urlAJAX(array('controller'=>'quiz','action'=>'formSave1'))?>'],
			actionsLoad:['<?=$this->urlAJAX(array('controller'=>'quiz','action'=>'formLoad0'))?>','<?=$this->urlAJAX(array('controller'=>'quiz','action'=>'formLoad1'))?>','<?=$this->urlAJAX(array('controller'=>'quiz','action'=>'formQuestion'))?>'],
			primaryKey: 'quiz[id]',
			initId: 1,
			prevButtonId: 'prev-button',
			appendUrl: '&mod_quiz[id]=<?=$this->data['config']['id']?>'
		}
	);
});
</script>


<div id="quiz-container">
</div>
*/
