/*
 * Ext JS Library 2.2.1
 * Copyright(c) 2006-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */


Ext.onReady(function(){

    Ext.QuickTips.init();
    
    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';
Ext.Ajax.request({ 
			url : 'autoriza/encuestas/librerias/consultas.php',  
			timeout:120000,
			method:"POST",
			params: { 
				tipo: 1				
			},  
			success: function(objServerResponse){
			var objTxt = objServerResponse.responseText;
			var dataObj = Ext.util.JSON.decode(objTxt);
			var obs=dataObj.encusta;
			var check = new Array();
			var total=obs.length;
			var count=dataObj.total;
			var IdEnc=dataObj.encusta[0].IdEncuest;
		if(count<=1)
		{
			for(var i=0;i<obs.length;i++)
			{
				if(i==0)
				{
					var id={
							  xtype: 'hidden',
							  name: 'Id',
							  value : dataObj.data[0]
						   };
					check.push(id);
					var idencus= {
									xtype: 'hidden',
									name: 'Idencusta',
									value : dataObj.encusta[0].IdEncuest
							     };
					check.push(idencus);
				}
				var radios ={
				  xtype: 'radiogroup',
				  id:'califica_'+i,
				  fieldLabel: dataObj.encusta[i].DescripEncuesta,
				  items: [
				   {boxLabel: '1', name: 'rb'+i, inputValue: 1},
				   {boxLabel: '2', name: 'rb'+i, inputValue: 2},
				   {boxLabel: '3', name: 'rb'+i, inputValue: 3},
				   {boxLabel: '4', name: 'rb'+i, inputValue: 4},
				   {boxLabel: '5', name: 'rb'+i, inputValue: 5}
				  ],
				  blankText:'Esta pregunta es obligatoria',
				   allowBlank:false
				}	
				check.push(radios);
			}
			
			var fp = new Ext.FormPanel({
				url:'autoriza/encuestas/librerias/insert.php',
				id:'form',
				frame:true,
				title:dataObj.data[1],
				labelWidth: 350,
				width: 600,
				renderTo:'form-ct',
				bodyStyle: 'padding:0 10px 0;',
				items:check,
				buttons: [{
				  text: 'Aceptar',
				  disabled:false,
				  style: {"margin-top": "10px"},
				  handler: function(){
					  fp.form.items.each(function(f){
					  if(!f.isValid(true)){valid = false;}
					  else{valid = true;}
					  });
				  if(valid==false)
					  {
						  Ext.Msg.show({
							  title:'Mensaje',
		  					  msg: 'Selecione una calificaci&oacute;n por cada pregunta',
							  buttons: Ext.Msg.OK,                   
							  animEl: document.body,                   
							  icon: Ext.MessageBox.INFO
						  });			
					  }
					  else
					  {
						  fp.form.submit({
							  waitMsg:'Ejecutando consulta...',
							  failure: function(form, action) {
						  //		alert(form)
								  Ext.MessageBox.alert('Acceso denegado', action.result.msj);
							  },
							  success: function(form, action) {
								   Ext.MessageBox.alert('', 'Gracias por diligenciar la encuesta.');
								   fp.getForm().reset();


							  }
						  });
					  }
				  }
				},{
					text: 'Cancel',
					 style: {"margin-top": "10px"},
					  handler: function(){
						  fp.getForm().reset();
						  }
				}]

			});
			
			var win = new Ext.Window({
			width:820,
				maximizable:true,
				height:550,
				iconCls: 'accordion',
				animCollapse:false,
				constrainHeader:true,
				layout:'form',
				border:false,
				layoutConfig: {
					animate:false
				},
				items:[fp]
			});
			
			var panelExcel = new Ext.Panel({
				renderTo:'graf',
				frame:true,
				width:600,
				autoHeight:true,
				layout:'fit',
				title: 'Resultados encuestas',
				autoScroll: true,
				buttons: [{
				  text: 'Gr&aacute;fica',
				  iconCls: 'informe',
				  disabled:false,
				  handler: function(){
					var winw = new Ext.Window({
						title:'Informe de la encuesta',
						layout:'fit',
						width:600,
						height:300,
						closeAction:'hide',
						plain: true,
		                items: new Ext.TabPanel({
							autoTabs:true,
							activeTab:0,
							deferredRender:false,
							border:false,
							html: "<iframe src='autoriza/encuestas/librerias/graf.php?Id="+IdEnc+"'  frameBorder='0' width='100%' height='100%' />asasas </iframe>"
                	})
            		});
        	        winw.show();
    				 
				  }
				}]
				/*html: "<iframe src='librerias/graf.php?Id="+IdEnc+"'  frameBorder='0' width='100%' height='100%' />asasas </iframe>"*/
			});
			
			
		}
		else
		{
			 Ext.MessageBox.alert('Alerta', 'Hay mas de una encuesta publicada por favor informe al administrador del sistema');

		}
	}
});
});

