

candidaturas	=	{
	
	ds			: null
	,started	: 0
	,form_fp	: null
	,bt_next	: null
	,bt_prev	: null
	,bt_save	: null
	,stepers	:[0,1,2,3,4]
	,ctrStep	: 0
	,userExistVal:1
	



	,main:function()
	{
		this.creat_form();
		this.form_fp.render(Ext.get("candidaturas_imod-form"))
		this.creat_bts();
		this.goStep(0);
		this.started	=	1;
		
	}




	,creat_form:function()
	{
		if(this.form_fp)
		{
			this.form_fp.destroy();
		}
			
		this.form_fp = new Ext.FormPanel({
			baseCls: 'x-plain'
			,id: 'form_fp'
			,anchor: '100%'
			,frame: true
			,autoHeight: true
			,labelWidth: 100
			,defaults: {
				allowBlank: true
				,msgTarget: 'side'
			},
			items: [
				candidaturas_s0()
				,candidaturas_s1()
				,candidaturas_s2()
				,candidaturas_s3()
				,candidaturas_s4()
			]
		});
		return this.form_fp
	}
	
	
	
	
	,creat_bts:function()
	{
		var l	=	candidaturas_imod_lang;
		
		this.bt_prev	= new Ext.Button({
			text: l.prev
			, handler: function()
			{
				candidaturas.goStep(candidaturas.ctrStep-1)
			}
		}).render("candidaturas_imod-btns");

		this.bt_next	= new Ext.Button({
			text: l.next
			, handler: function()
			{
				candidaturas.goStep(candidaturas.ctrStep+1)
			}
		}).render("candidaturas_imod-btns");
		
		this.bt_save	= new Ext.Button({
			text: l.save
			, handler: function()
			{
				candidaturas.save()
			}
		}).render("candidaturas_imod-btns");
	}


	,goStep:function(n){
		

		if(this.started && this.ctrStep<n)
		{
			var stepFp	=	Ext.getCmp("step_"+this.ctrStep)
			var els		=	stepFp.items.map
			for(var x in els)
			{
				if(Ext.getCmp(x).getXType()=="panel")
					continue;
				if(!Ext.getCmp(x).isValid())
				{
					return;	
				}
			}	
		}
		
		if(this.ctrStep==1)
		{
			var aluno_email	=	Ext.getCmp("aluno_email").getValue();
			if(aluno_email && candidaturas.userExistVal)
			{
				Ext.MessageBox.alert("erro", "Aparentemente o aluno já se encontra inscrito, para mais esclarecimentos contacte o serviço administrativo")
				return;	
			}
		}
		
		
		for(var k=0; k<this.stepers.length;k++)
		{
			var elFp	=	Ext.getCmp("step_"+this.stepers[k]);
			if(elFp)
			elFp.hide();	
		}
		
		var newFP	=	Ext.getCmp("step_"+this.stepers[n]);
		if(newFP)
		{
			newFP.show();
			//Ext.select("#candidaturas_imod-form").slideIn('t', { duration: 1 });
		}
		this.ctrStep	=	n
		candidaturas.views_bts()
		candidaturas.status_update()
		candidaturas.form_fp.doLayout();
	}

	,views_bts:function()
	{
		if(this.ctrStep==this.stepers[0])
		{
			this.bt_next.show()
			this.bt_prev.hide()
			this.bt_save.hide()
		}
		
		else if(this.ctrStep==this.stepers[this.stepers.length-1])
		{
			this.bt_next.hide()
			this.bt_prev.show()
			this.bt_save.show()
		}
		
		else{
			this.bt_next.show()
			this.bt_prev.show()
			this.bt_save.hide()	
		}
	}

	,status_update:function()
	{
		var status	=	(this.ctrStep+1) + " de " + (this.stepers.length)
		Ext.select("#candidaturas_imod-inner .status").update(status)
	}

	,formLoadRec:function()
	{
		var record	=	candidaturas.selRec
		var fl	=	candidaturas.fields	
		for(k=0;k<fl.length;k++)
		{
			var name	=	fl[k].name;
			var input	=	Ext.getCmp(name)
			if(input){
				
				var xtype	=	input.getXtype
				switch(input.getXType())
				{
					case	"fileuploadfield":
					break;
					case	"combo":
						input.setValue(record.data[name])
					break;
					case	"radiogroup":
					break;
					default:
						input.setValue(record.data[name])
					break;
				}
			}
		}
	}
	
	
	
	,save:function()
	{

		var frm	=	candidaturas.form_fp.getForm();
		
			
		
		if(!frm.isValid()){
			return false;
		}
		var values	=	frm.getValues(false);
		for(var x in values)
		{
			if(x.indexOf("hidden_")==0)
			{
				values[x.slice(7)]	=	values[x];
				delete values[x];
			}
		}
		//console.log(values)
		Ext.MessageBox.wait('A enviar dados ...');
		frm.submit({
			url: "/imod/candidaturas_imod/"
			,params:{
				a		: 	"save"
				,c		: 	"candidaturas_imod_action"
				,data	: 	escape(Ext.util.JSON.encode(values))
			}
			,success: function(fp, o){
				
				Ext.MessageBox.updateProgress(1);
				Ext.MessageBox.hide();
				candidaturas.final_p()
			}
			,failure:function(fp, o)
			{
				//data	=	Ext.util.JSON.decode(response.responseText);
				//msg('erro', data.msg);
			}
		});
	}
	
	,userExist:function(email){
		Ext.Ajax.request(
		{
			url: "/imod/candidaturas_imod/"
			,params:{
				a		: 	"userExist"
				,c		: 	"candidaturas_imod_action"
				,email	: 	email
			}
			,success: function(response, options)
			{
				var data	=	Ext.util.JSON.decode(response.responseText);
				//console.log(data)
				candidaturas.userExistVal = parseInt(data.aluno_id)>0?true:false
			}
			,failure:function(response, options)
			{
				//data	=	Ext.util.JSON.decode(response.responseText);
				//msg('erro', data.msg);
				Ext.MessageBox.alert("erro", "De momento não é possivel estabelecer ligação ao servidor, pf. tente mais tarde")
				candidaturas.userExistVal = true
			}
		});
	}
	
	,final_p:function(data)
	{
		var l	=	candidaturas_imod_lang;
		
		Ext.get("candidaturas_imod-outer").hide();
		candidaturas.form_fp.destroy();
		Ext.get("candidaturas_imod-final-outer").show();
		Ext.get("candidaturas_imod-final-outer").update(l.finalMsg);
		
	}
	

}
