var winBuscarAsociado;
var who ='';

function number_format( number, decimals, dec_point, thousands_sep ){
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
function minimizeWin(win){
	if(win.minimized){
		win.setWidth(win.restoreWidth);
		win.expand(true);
		win.minimized = false;	
	}
	else{
		win.collapse(true);
		win.setWidth(win.minimizeWidth);
		win.minimized = true;
	}	
}
var dsBusquedaAsociadoConsultar= new Ext.data.JsonStore({
	url: 'php/mysqljsongrid.php',
    method: 'POST',
    totalProperty: 'totalRows',
    root: 'data',
    fields: [{
        name: 'id_asociado', type: 'string' }, {
		name: 'clave',	  	 type: 'string' }, {	
        name: 'nombre',	  	 type: 'string' }, {
		name: 'completo', 	 type: 'string' }, {
		name: 'otro',	  	 type: 'string' }, {
		name: 'domicilio',	 type: 'string' }, {	    
		name: 'ciudad',	  	 type: 'string' }, {
		name: 'estado',	  	 type: 'string' }, {
		name: 'cve_estado',	 type: 'string' }, {
		name: 'id_ciudad',	 type: 'string' }, {
		name: 'tel',	  	 type: 'string' }, {
		name: 'tel2',	  	 type: 'string' }, {	
		name: 'ife',	  	 type: 'string' }, {
		name: 'licencia',	 type: 'string' }, {
    }]
});
/////////////////////////////////////////////modelo de columnas
var cmBusquedaAsociadoConsultar = new Ext.grid.ColumnModel([{
	header: "Clave",
    width: 100,
    sortable: true,
    dataIndex: 'clave'	
},{
    header: "Asociado",
    width: 205,
    sortable: true,
    dataIndex: 'completo'	
},{
 	header: "Ciudad",
    width: 130,
    sortable: true,
    dataIndex: 'ciudad'
},{
    header: "Estado",
    width: 130,
    sortable: true,
    dataIndex: 'estado'
},{
    header: "Teléfono",
    width: 70,
    sortable: true,
    dataIndex: 'tel'
},{
    header: "IFE",
    width: 90,
    sortable: true,
    dataIndex: 'ife'
},{
    header: "Licencia",
    width: 90,
    sortable: true,
    dataIndex: 'licencia'
},{
    header: "Otro",
    width: 90,
    sortable: true,
    dataIndex: 'otro'
}]);
cmBusquedaAsociadoConsultar.defaultSortable = true;
///////////////////////////////////////////////////////////////////////////////////////////////grid de Directorio
var gridBusquedaAsociadoConsultar = new Ext.grid.GridPanel({
    ds: dsBusquedaAsociadoConsultar,
    enableColLock: false,
    cm: cmBusquedaAsociadoConsultar,
    loadMask: true,
	frame: true,
    sm: new Ext.grid.RowSelectionModel({
        singleSelect: true
    }),
    width: 320,
    height: 200,
	y:125,
	x:4,
    border: true,
    listeners: {
        render: function(g){
            g.getSelectionModel().selectRow(0);			
	    },
		click: function(e){
			var record = this.getSelectionModel().getSelected();
			if (!record) {return;}
				frmBusquedaAsociado.getForm().loadRecord(record);
				if (who == 'inv') {
					busqueda_asc_invitado = record.get('id_asociado');
					txtInvito_est.setValue(record.get('clave')); //////    establecimiento -invito
					verificarInvito();
					Ext.getCmp('btnGuardarEstablecimiento').focus();
				}else if (who =='') {
					busqueda_asc = record.get('id_asociado');
					txtAsociado_est.setValue(record.get('clave')); 
					verificarInvito();///    establecimiento
					Ext.getCmp('txtComision_est').focus();
					txtAsociado_RC.setValue(record.get('clave')); ////          registro de compra
					txtTicket_RC.focus(true,200);					
					txtBuscar_ascHCM.setValue(record.get('clave')); /////// en historial de consumo
					txtInvitoAso.setValue(record.get('clave')); ////// en asociados	
					Ext.getCmp('btnAgregarAsociado').focus();
					txtBuscar_ascCB.setValue(record.get('clave')); /////// en Comisiones x Bimestre						
					txtDomicilio_att.focus();
					txtFechaIni_HCM.focus(true,200);
					txtEstado_CB.focus(true, 300);
				}
				else if(who=='asoFuera'){
					busqueda_asc = record.get('id_asociado');
					txtInvitoAso1.setValue(record.get('clave'));   ///// en asociado de afuera
				} 
				else if(who=='representante'){
					busqueda_asc = record.get('id_asociado');
					txtAsociado_att.setValue(record.get('completo')); /////// en Atencion a clientes
					txtDomicilio_att.setValue(record.get('domicilio'));
					txtEstado_att.setValue(record.get('cve_estado'));
					txtCiudad_att.setValue(record.get('id_ciudad'));
					txtEstado_att.setRawValue(record.get('estado'));
					txtCiudad_att.setRawValue(record.get('ciudad'));
					txtTel_att.setValue(record.get('tel'));
					txtCel_att.setValue(record.get('tel2'));
					txtDomicilio_att.focus(true,200);
				}
				winBuscarAsociado.hide();				
		},
		dblclick: function(e){
			 this.fireEvent('click');
		},
		keypress: function(e){
			if (e.getKey() == 13) {
				this.fireEvent('click');
			}
		}	
		 
    }
});
//////////////////////////////////////////////////////////////////////////////////////////LISTA DE LABEL
var lblBuscarNombre_asc = new Ext.form.Label({
	text: 'Nombre:',
	x: 15,
	y: 30
});
var lblBuscarID_asc = new Ext.form.Label({
	text: 'Identificación:',
	x: 15,
	y: 60
});
var lblBuscarClave_asc = new Ext.form.Label({
	text: 'Clave:',
	x: 15,
	y: 90
});
var frameAsoci_busqueda = new Ext.form.FieldSet({
	width: 320,
	height:115,
	title: 'Búsqueda',
	x: 2,
	y: 2
});
/////////////////////////////////////////////////////////////////////////////////////////////LISTA DE TEXT
var txtBuscar_asc = new Ext.form.TextField({
	hideLabel: true,
	width: 200,
	x: 90,
	y: 25,
	maskRe:/[A-Za-zñÑ0-9_ÁáéíóúÉÍÓÚ\x20\x08\x09]/,
	enableKeyEvents: true,
	selectOnFocus: true,
	listeners: {
		keyup: function(t,e){
			if(e.getKey()==13){
				dsBusquedaAsociadoConsultar.removeAll();
				dsBusquedaAsociadoConsultar.reload({
					params: {
						query: 'call spCatAsociadoMostrar("'+txtBuscar_asc.getValue()+'","'+txtBuscarID_asc.getValue()+'","'+txtBuscarClaveLarga_asc.getValue()+'");'
					}});
					}
			}
		}
});
var txtBuscarID_asc = new Ext.form.TextField({
	hideLabel: true,
	width: 200,
	x: 90,
	y: 55,
	enableKeyEvents: true,
	selectOnFocus: true,
	listeners: {
		keyup: function(t,e){
			if(e.getKey()==13){
				dsBusquedaAsociadoConsultar.removeAll();
				dsBusquedaAsociadoConsultar.reload({
					params: {
						query: 'call spCatAsociadoMostrar("'+txtBuscar_asc.getValue()+'","'+txtBuscarID_asc.getValue()+'","'+txtBuscarClaveLarga_asc.getValue()+'");'
					}});
					}
				}
			}
});
var txtBuscarClaveLarga_asc = new Ext.form.TextField({
	hideLabel: true,
	width: 200,
	x: 90,
	y: 85,
	enableKeyEvents: true,
	selectOnFocus: true,
	listeners: {
		keyup: function(t,e){
			if(e.getKey() == 40) {
				if(dsBusquedaAsociadoConsultar.getCount()> 0){
					gridBusquedaAsociadoConsultar.getSelectionModel().selectRow(0);
					gridBusquedaAsociadoConsultar.getView().focusRow(0);	
				}	
			
			}
			if(e.getKey() == 40 || e.getKey()==13){
				dsBusquedaAsociadoConsultar.removeAll();
				dsBusquedaAsociadoConsultar.reload({
					params: {
						query: 'call spCatAsociadoMostrar("'+txtBuscar_asc.getValue()+'","'+txtBuscarID_asc.getValue()+'","'+txtBuscarClaveLarga_asc.getValue()+'");'
					}});
				}
			}
		}
});
///////////////////////////////////////////////////////////////////////////////////////////////FORMA
var frmBusquedaAsociado = new Ext.FormPanel({
	layout: 'absolute',
	frame: true,
	items: [frameAsoci_busqueda,lblBuscarNombre_asc,txtBuscar_asc,lblBuscarID_asc,txtBuscarID_asc,lblBuscarClave_asc,txtBuscarClaveLarga_asc,gridBusquedaAsociadoConsultar]
});
//Ventana
function buscarAsociado(qien){
	who = qien;
	if (!winBuscarAsociado) {
		winBuscarAsociado = new Ext.Window({
			layout: 'fit',
			title: 'Búsqueda de Asociado',
			resizable: false,
			minimizable: true,
			modal: true,
			width: 355,
			height: 380,
			closeAction: 'hide',
			plain: true,
			constrain:true,
			shadow:true,
			items: [frmBusquedaAsociado]
			
		});		
		winBuscarAsociado.minimizeWidth = 200;
		winBuscarAsociado.restoreWidth = 355;
		winBuscarAsociado.minimized = false;
		winBuscarAsociado.on({'minimize': minimizeWin});			
	}
	frmBusquedaAsociado.getForm().reset();
	dsBusquedaAsociadoConsultar.removeAll();
	dsBusquedaAsociadoConsultar.reload({
		params: {
			 query: 'call spCatAsociadoMostrar("","","");'
			}
	});	
	winBuscarAsociado.show();
	if(winBuscarAsociado.minimized){
		winBuscarAsociado.setWidth(winBuscarAsociado.restoreWidth);	
	}
	else{
		winBuscarAsociado.setWidth(winBuscarAsociado.restoreWidth);
		winBuscarAsociado.setHeight(380);
		winBuscarAsociado.collapse(false);
		winBuscarAsociado.expand(false);
	}
	winBuscarAsociado.minimized = false;	
	txtBuscar_asc.focus(true,500);
}