var propiedad_seleccionada = 0;
MyDesktop.AdministracionModule = Ext.extend(Ext.app.Module, {
    init : function(){
        this.launcher = {
            text: 'Administracion',
            iconCls: 'administracion',
            handler: function() {
				return false;
			},
            menu: {
                items:[{
                    text: 'Cargar propiedades',
                    iconCls:'bogus',
                    handler : createWindowCargarPropiedades,
                    scope: this
                    },{
                    text: 'Tipos de operaciones ',
                    iconCls:'bogus',
                    handler : createWindowTipoOperacion,
                    scope: this
                    },{
                    text: 'Tipos de propiedades ',
                    iconCls:'bogus',
                    handler : createWindowTipoPropiedad,
                    scope: this
                    },{
                    text: 'Provincias ',
                    iconCls:'bogus',
                    handler : createWindowProvincia,
                    scope: this
                    },{
                    text: 'Localidades ',
                    iconCls:'bogus',
                    handler : createWindowLocalidad,
                    scope: this
                    },{
                    text: 'Partidos ',
                    iconCls:'bogus',
                    handler : createWindowPartido,
                    scope: this
                },{
                    text: 'Monedas ',
                    iconCls:'bogus',
                    handler : createWindowMonedas,
                    scope: this
                }]
            }
        }
    }
 });

function createWindowTipoOperacion(){
    var desktop = MyDesktop.getDesktop();
	var win = desktop.getWindow('wAbmTipoOperacion');
	if(!win){
        var cm = new Ext.grid.ColumnModel([{
               id:'nombre',
               header: "Nombre",
               dataIndex: 'tipoOperacionNombre',
               sortable: true,
               width: 220
            }
        ]);
        var store = new Ext.data.JsonStore({
                url: "index.php/propiedades/ListadoTipoOperacion",
                root: 'tipo-operacion',
                fields: ['tipoOperacionId', 'tipoOperacionNombre'],
                sortInfo: {field: "tipoOperacionNombre", direction: "DESC"},
                autoLoad: true
            });
            var grid = new Ext.grid.EditorGridPanel({
                id: 'abm-tipo-operacion-grid',
                store: store,
                cm: cm,
                width:600,
                height:300,
                loadMask: true,
                autoExpandColumn:'nombre',
                viewConfig:{
                   sortDescText: 'Orden descendente',
                   sortAscText:'Orden ascendente',
                   columnsText: 'Columnas'
                },
                frame:true,
                tbar: [{
                    text: 'Nueva operación',
                    iconCls:'nuevo',
                    handler : function(){
                        Ext.MessageBox.prompt('Nombre', 'Ingrese la nueva operación:', function(btn, text){
                            if (btn == 'ok'){
                                if(text == ''){
                                    Ext.MessageBox.alert('Modificar','El nombre no puede ser vacio');
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'index.php/propiedades/altaTipoOperacion',
                                    success: 	function(respuesta){
                                                   if(respuesta.responseText=='true'){
                                                        store.reload();
                                                   }else{
                                                       Ext.MessageBox.alert('Nuevo',respuesta.responseText);
                                                   }
                                                },
                                    params: "nombre="+text
                                    });
                            }
                        });
                    }
                },{
                    text: 'Editar',
                    iconCls:'editar',
                    handler : function(){
                        var cell = Ext.getCmp('abm-tipo-operacion-grid').getSelectionModel().getSelectedCell();
                        if(cell==null){
                            Ext.MessageBox.alert('Modificar','Seleccione el tipo de operación a modificar');
                            return;
                        }
                        var id = Ext.getCmp('abm-tipo-operacion-grid').getSelectionModel().selection.record.data.tipoOperacionId;
                        if(id == -1){
                            Ext.MessageBox.alert('Modificar','No puede mofificar la opcion Indistinto');
                            return;
                        }
                        Ext.MessageBox.prompt('Nombre', 'Ingrese el nuevo nombre:', function(btn, text){
                            if (btn == 'ok'){
                                if(text == ''){
                                    Ext.MessageBox.alert('Modificar','El nombre no puede ser vacio');
                                    return;
                                }
                                if(text == 'Indistinto'){
                                    Ext.MessageBox.alert('Modificar','No puede reemplazar la opcion Indistinto');
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'index.php/propiedades/modificarTipoOperacion',
                                    success: 	function(respuesta){
                                                   if(respuesta.responseText=='true'){
                                                        store.reload();
                                                   }else{
                                                       Ext.MessageBox.alert('Nuevo',respuesta.responseText);
                                                   }
                                                },
                                    params: {
                                        nombre:text,
                                        id:id
                                    }
                                });
                            }
                        });
                    }
                },{
                    text: 'Eliminar operación',
                    iconCls:'borrar',
                    handler : function(){
                        Ext.MessageBox.show({
                           title: 'Eliminar',
                           msg: 'Esta seguro que desea eliminar el tipo de operación seleccionado?',
                           width:300,
                           buttons: Ext.Msg.YESNO,
                           buttonText: Ext.MessageBox.buttonText.yes = "Si",
                           fn: showResultText
                       });
                        function showResultText(btn, text){
                            if(btn == 'no'){
                                return;
                            }else{
                                var cell = Ext.getCmp('abm-tipo-operacion-grid').getSelectionModel().getSelectedCell();
                                if(cell==null){
                                    Ext.MessageBox.alert('Eliminar','Seleccione el tipo de operación a eliminar');
                                    return;
                                }
                                var id = Ext.getCmp('abm-tipo-operacion-grid').getSelectionModel().selection.record.data.tipoOperacionId;
                                if(id == -1){
                                    Ext.MessageBox.alert('Eliminar','No puede eliminar la opcion Indistinto');
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'index.php/propiedades/eliminarTipoOperacion',
                                    success: 	function(respuesta){
                                                   if(respuesta.responseText=='true'){
                                                        store.reload();
                                                   }else{
                                                       Ext.MessageBox.alert('Eliminar',respuesta.responseText);
                                                   }
                                                },
                                    params: {
                                        id:id
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            win = desktop.createWindow({
                id: 'wAbmTipoOperacion',
                xtype: 'window',
                layout: 'fit',
                title: 'Tipos de operaciones',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                width:400,
                height:400,
                items: grid
           });
        }
        win.show();
}

function createWindowTipoPropiedad(){
    var desktop = MyDesktop.getDesktop();
	var win = desktop.getWindow('wAbmTipoPropiedad');
	if(!win){
        var cm = new Ext.grid.ColumnModel([{
               id:'nombre',
               header: "Nombre",
               dataIndex: 'tipoPropiedadNombre',
               sortable: true,
               width: 220
            }
        ]);
        var store = new Ext.data.JsonStore({
                url: "index.php/propiedades/ListadoTipoPropiedades",
                root: 'tipo-propiedad',
                fields: ['tipoPropiedadId', 'tipoPropiedadNombre'],
                sortInfo: {field: "tipoPropiedadNombre", direction: "DESC"},
                autoLoad: true
            });
            var grid = new Ext.grid.EditorGridPanel({
                id: 'abm-tipo-propiedad-grid',
                store: store,
                cm: cm,
                width:600,
                height:300,
                loadMask: true,
                autoExpandColumn:'nombre',
                viewConfig:{
                   sortDescText: 'Orden descendente',
                   sortAscText:'Orden ascendente',
                   columnsText: 'Columnas'
                },
                frame:true,
                tbar: [{
                    text: 'Nuevo tipo',
                    iconCls:'nuevo',
                    handler : function(){
                        Ext.MessageBox.prompt('Nombre', 'Ingrese el nuevo tipo de propiedad:', function(btn, text){
                            if (btn == 'ok'){
                                if(text == ''){
                                    Ext.MessageBox.alert('Modificar','El nombre no puede ser vacio');
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'index.php/propiedades/altaTipoPropiedad',
                                    success: 	function(respuesta){
                                                   if(respuesta.responseText=='true'){
                                                        store.reload();
                                                   }else{
                                                       Ext.MessageBox.alert('Nuevo',respuesta.responseText);
                                                   }
                                                },
                                    params: "nombre="+text
                                    });
                            }
                        });
                    }
                },{
                    text: 'Editar',
                    iconCls:'editar',
                    handler : function(){
                        var cell = Ext.getCmp('abm-tipo-propiedad-grid').getSelectionModel().getSelectedCell();
                        if(cell==null){
                            Ext.MessageBox.alert('Modificar','Seleccione el tipo de propiedad a modificar');
                            return;
                        }
                        var id = Ext.getCmp('abm-tipo-propiedad-grid').getSelectionModel().selection.record.data.tipoPropiedadId;
                        if(id == -1){
                            Ext.MessageBox.alert('Modificar','No puede mofificar la opcion Indistinto');
                            return;
                        }
                        Ext.MessageBox.prompt('Nombre', 'Ingrese el nuevo nombre:', function(btn, text){
                            if (btn == 'ok'){
                                if(text == ''){
                                    Ext.MessageBox.alert('Modificar','El nombre no puede ser vacio');
                                    return;
                                }
                                if(text == 'Indistinto'){
                                    Ext.MessageBox.alert('Modificar','No puede reemplazar la opcion Indistinto');
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'index.php/propiedades/modificarTipoPropiedad',
                                    success: 	function(respuesta){
                                                   if(respuesta.responseText=='true'){
                                                        store.reload();
                                                   }else{
                                                       Ext.MessageBox.alert('Nuevo',respuesta.responseText);
                                                   }
                                                },
                                    params: {
                                        nombre:text,
                                        id:id
                                    }
                                });
                            }
                        });
                    }
                },{
                    text: 'Eliminar tipo',
                    iconCls:'borrar',
                    handler : function(){
                        Ext.MessageBox.show({
                           title: 'Eliminar',
                           msg: 'Esta seguro que desea eliminar el tipo de propiedad seleccionado?',
                           width:300,
                           buttons: Ext.Msg.YESNO,
                           buttonText: Ext.MessageBox.buttonText.yes = "Si",
                           fn: showResultText
                       });
                        function showResultText(btn, text){
                            if(btn == 'no'){
                                return;
                            }else{
                                var cell = Ext.getCmp('abm-tipo-propiedad-grid').getSelectionModel().getSelectedCell();
                                if(cell==null){
                                    Ext.MessageBox.alert('Eliminar','Seleccione el tipo de propiedad a eliminar');
                                    return;
                                }
                                var id = Ext.getCmp('abm-tipo-propiedad-grid').getSelectionModel().selection.record.data.tipoPropiedadId;
                                if(id == -1){
                                    Ext.MessageBox.alert('Eliminar','No puede eliminar la opcion Indistinto');
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'index.php/propiedades/eliminarTipoPropiedad',
                                    success: 	function(respuesta){
                                                   if(respuesta.responseText=='true'){
                                                        store.reload();
                                                   }else{
                                                       Ext.MessageBox.alert('Eliminar',respuesta.responseText);
                                                   }
                                                },
                                    params: {
                                        id:id
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            win = desktop.createWindow({
                id: 'wAbmTipoPropiedad',
                xtype: 'window',
                layout: 'fit',
                title: 'Tipos de propiedades',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                width:400,
                height:400,
                items: grid
           });
        }
        win.show();
}

function createWindowProvincia(){
    var desktop = MyDesktop.getDesktop();
	var win = desktop.getWindow('wAbmProvincia');
	if(!win){
        var cm = new Ext.grid.ColumnModel([{
               id:'nombre',
               header: "Nombre",
               dataIndex: 'provinciaNombre',
               sortable: true,
               width: 220
            }
        ]);
        var store = new Ext.data.JsonStore({
                url: "index.php/propiedades/ListadoProvinciaSinIndistinto",
                root: 'provincia',
                fields: ['provinciaId', 'provinciaNombre'],
                sortInfo: {field: "provinciaNombre", direction: "DESC"},
                autoLoad: true
            });
            var grid = new Ext.grid.EditorGridPanel({
                id: 'abm-provincia-grid',
                store: store,
                cm: cm,
                width:600,
                height:300,
                loadMask: true,
                autoExpandColumn:'nombre',
                viewConfig:{
                   sortDescText: 'Orden descendente',
                   sortAscText:'Orden ascendente',
                   columnsText: 'Columnas'
                },
                frame:true,
                tbar: [{
                    text: 'Nueva provincia',
                    iconCls:'nuevo',
                    handler : function(){
                        Ext.MessageBox.prompt('Nombre', 'Ingrese el nombre de la provincia:', function(btn, text){
                            if (btn == 'ok'){
                                if(text == ''){
                                    Ext.MessageBox.alert('Modificar','El nombre no puede ser vacio');
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'index.php/propiedades/altaProvincia',
                                    success: 	function(respuesta){
                                                   if(respuesta.responseText=='true'){
                                                        store.reload();
                                                   }else{
                                                       Ext.MessageBox.alert('Nuevo',respuesta.responseText);
                                                   }
                                                },
                                    params: "nombre="+text
                                    });
                            }
                        });
                    }
                },{
                    text: 'Editar',
                    iconCls:'editar',
                    handler : function(){
                        var cell = Ext.getCmp('abm-provincia-grid').getSelectionModel().getSelectedCell();
                        if(cell==null){
                            Ext.MessageBox.alert('Modificar','Seleccione la provincia modificar');
                            return;
                        }
                        var id = Ext.getCmp('abm-provincia-grid').getSelectionModel().selection.record.data.provinciaId;
                        if(id == -1){
                            Ext.MessageBox.alert('Modificar','No puede mofificar la opcion Indistinto');
                            return;
                        }
                        Ext.MessageBox.prompt('Nombre', 'Ingrese el nuevo nombre:', function(btn, text){
                            if (btn == 'ok'){
                                if(text == ''){
                                    Ext.MessageBox.alert('Modificar','El nombre no puede ser vacio');
                                    return;
                                }
                                if(text == 'Indistinto'){
                                    Ext.MessageBox.alert('Modificar','No puede reemplazar la opcion Indistinto');
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'index.php/propiedades/modificarProvincia',
                                    success: 	function(respuesta){
                                                   if(respuesta.responseText=='true'){
                                                        store.reload();
                                                   }else{
                                                       Ext.MessageBox.alert('Nuevo',respuesta.responseText);
                                                   }
                                                },
                                    params: {
                                        nombre:text,
                                        id:id
                                    }
                                });
                            }
                        });
                    }
                },{
                    text: 'Eliminar provincia',
                    iconCls:'borrar',
                    handler : function(){
                        Ext.MessageBox.show({
                           title: 'Eliminar',
                           msg: 'Esta seguro que desea eliminar la provincia seleccionada?',
                           width:300,
                           buttons: Ext.Msg.YESNO,
                           buttonText: Ext.MessageBox.buttonText.yes = "Si",
                           fn: showResultText
                       });
                        function showResultText(btn, text){
                            if(btn == 'no'){
                                return;
                            }else{
                                var cell = Ext.getCmp('abm-provincia-grid').getSelectionModel().getSelectedCell();
                                if(cell==null){
                                    Ext.MessageBox.alert('Eliminar','Seleccione la provincia a eliminar');
                                    return;
                                }
                                var id = Ext.getCmp('abm-provincia-grid').getSelectionModel().selection.record.data.provinciaId;
                                if(id == -1){
                                    Ext.MessageBox.alert('Eliminar','No puede eliminar la opcion Indistinto');
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'index.php/propiedades/eliminarProvincia',
                                    success: 	function(respuesta){
                                                   if(respuesta.responseText=='true'){
                                                        store.reload();
                                                   }else{
                                                       Ext.MessageBox.alert('Eliminar',respuesta.responseText);
                                                   }
                                                },
                                    params: {
                                        id:id
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            win = desktop.createWindow({
                id: 'wAbmProvincia',
                xtype: 'window',
                layout: 'fit',
                title: 'Provincias',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                width:400,
                height:400,
                items: grid
           });
        }
        win.show();
}

function createWindowLocalidad(){
    var desktop = MyDesktop.getDesktop();
	var win = desktop.getWindow('wAbmLocalidad');
	if(!win){
        var cm = new Ext.grid.ColumnModel([{
                id:'nombreLocalidad',
               header: "Localidad",
               dataIndex: 'localidadNombre',
               sortable: true,
               width: 220
            },{
               id:'nombreProvincia',
               header: "Provincia",
               dataIndex: 'provinciaNombre',
               sortable: true,
               width: 220
            }
        ]);
        var store = new Ext.data.JsonStore({
                url: "index.php/propiedades/ListadoProvinciaLocalidad",
                root: 'localidad',
                fields: ['localidadId', 'provinciaNombre','provinciaId','localidadNombre'],
                sortInfo: {field: "localidadNombre", direction: "DESC"},
                autoLoad: true
            });
            var grid = new Ext.grid.GridPanel({
                id: 'abm-localidad-grid',
                store: store,
                cm: cm,
                width:600,
                height:300,
                loadMask: true,
                autoExpandColumn:'nombreLocalidad',
                multiSelect: false,
                 singleSelect: true,
                 sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
                viewConfig:{
                   forceFit: true,
                   sortDescText: 'Orden descendente',
                   sortAscText:'Orden ascendente',
                   columnsText: 'Columnas'
                },
                frame:true,
                tbar: [{
                    text: 'Nueva localidad',
                    iconCls:'nuevo',
                    handler : function(){
                        var desktop = MyDesktop.getDesktop();
                        var win = desktop.getWindow('editar-localidad-win');
                        if(win){
                            Ext.MessageBox.alert('Nuevo','Ya hay una ventana abierta de edición');
                        }
                        createWindowsEditarLocalidad(-1);
                    }
                },{
                    text: 'Editar',
                    iconCls:'editar',
                    handler : function(){
                        var desktop = MyDesktop.getDesktop();
                        var win = desktop.getWindow('editar-localidad-win');
                        if(win){
                            Ext.MessageBox.alert('Nuevo','Ya hay una ventana abierta de edición');
                        }
                        if(grid.getSelectionModel().getSelected()==undefined){
                            Ext.MessageBox.alert('Editar','Seleccione la localidad a editar');
                            return;
                        }
                        createWindowsEditarLocalidad(grid.getSelectionModel().getSelected().get('localidadId'),grid.getSelectionModel().getSelected().get('provinciaId'),
                            grid.getSelectionModel().getSelected().get('localidadNombre'));
                    }
                },{
                    text: 'Eliminar localidad',
                    iconCls:'borrar',
                    handler : function(){
                        Ext.MessageBox.show({
                           title: 'Eliminar',
                           msg: 'Esta seguro que desea eliminar la localidad seleccionada?',
                           width:300,
                           buttons: Ext.Msg.YESNO,
                           buttonText: Ext.MessageBox.buttonText.yes = "Si",
                           fn: showResultText
                       });
                        function showResultText(btn, text){
                            if(btn == 'no'){
                                return;
                            }else{
                                if(grid.getSelectionModel().getSelected()==undefined){
                                    Ext.MessageBox.alert('Eliminar','Seleccione la localidad a eliminar');
                                    return;
                                }
                                var id = grid.getSelectionModel().getSelected().get('localidadId');
                                if(id == -1){
                                    Ext.MessageBox.alert('Eliminar','No puede eliminar la opcion Indistinto');
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'index.php/propiedades/eliminarLocalidad',
                                    success: 	function(respuesta){
                                                   if(respuesta.responseText=='true'){
                                                        store.reload();
                                                   }else{
                                                       Ext.MessageBox.alert('Eliminar',respuesta.responseText);
                                                   }
                                                },
                                    params: {
                                        id:id
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            win = desktop.createWindow({
                id: 'wAbmLocalidad',
                xtype: 'window',
                layout: 'fit',
                title: 'Localidades',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                width:400,
                height:400,
                items: grid
           });
        }
        win.show();
}

function createWindowPartido(){
    var desktop = MyDesktop.getDesktop();
	var win = desktop.getWindow('wAbmPartido');
	if(!win){
        var cm = new Ext.grid.ColumnModel([{
               id:'nombrePartidoPartido',
               header: "Partido",
               dataIndex: 'partidoNombre',
               sortable: true,
               width: 220
            },{
                id:'nombreLocalidadPartido',
               header: "Localidad",
               dataIndex: 'localidadNombre',
               sortable: true,
               width: 220
            },{
                id:'nombreProvinciaPartido',
               header: "Provincia",
               dataIndex: 'provinciaNombre',
               sortable: true,
               width: 220
            }
        ]);
        var store = new Ext.data.JsonStore({
                url: "index.php/propiedades/ListadoProvinciaLocalidadPartido",
                root: 'partido',
                fields: ['partidoId', 'provinciaNombre','provinciaId','localidadNombre','partidoNombre','localidadId'],
                sortInfo: {field: "partidoNombre", direction: "DESC"},
                autoLoad: true
            });
            var grid = new Ext.grid.GridPanel({
                id: 'abm-partido-grid',
                store: store,
                cm: cm,
                width:600,
                height:300,
                loadMask: true,
                autoExpandColumn:'partidoNombre',
                multiSelect: false,
                 singleSelect: true,
                 sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
                viewConfig:{
                   forceFit: true,
                   sortDescText: 'Orden descendente',
                   sortAscText:'Orden ascendente',
                   columnsText: 'Columnas'
                },
                frame:true,
                tbar: [{
                    text: 'Nuevo Partido',
                    iconCls:'nuevo',
                    handler : function(){
                        var desktop = MyDesktop.getDesktop();
                        var win = desktop.getWindow('editar-partido-win');
                        if(win){
                            Ext.MessageBox.alert('Nuevo','Ya hay una ventana abierta de edición de partidos');
                        }
                        createWindowsEditarPartido(-1);
                    }
                },{
                    text: 'Editar',
                    iconCls:'editar',
                    handler : function(){
                        var desktop = MyDesktop.getDesktop();
                        var win = desktop.getWindow('editar-partido-win');
                        if(win){
                            Ext.MessageBox.alert('Nuevo','Ya hay una ventana abierta de edición de partidos');
                        }
                        if(grid.getSelectionModel().getSelected()==undefined){
                            Ext.MessageBox.alert('Editar','Seleccione el partido a editar');
                            return;
                        }
                        var id = grid.getSelectionModel().getSelected().get('partidoId');
                        var provinciaId = grid.getSelectionModel().getSelected().get('provinciaId');
                        var localidadId = grid.getSelectionModel().getSelected().get('localidadId');
                        var nombre = grid.getSelectionModel().getSelected().get('partidoNombre');
                        createWindowsEditarPartido(id,provinciaId,localidadId,nombre);
                    }
                },{
                    text: 'Eliminar partido',
                    iconCls:'borrar',
                    handler : function(){
                        Ext.MessageBox.show({
                           title: 'Eliminar',
                           msg: 'Esta seguro que desea eliminar el partido seleccionado?',
                           width:300,
                           buttons: Ext.Msg.YESNO,
                           buttonText: Ext.MessageBox.buttonText.yes = "Si",
                           fn: showResultText
                       });
                        function showResultText(btn, text){
                            if(btn == 'no'){
                                return;
                            }else{
                                if(grid.getSelectionModel().getSelected()==undefined){
                                    Ext.MessageBox.alert('Eliminar','Seleccione el partido a eliminar');
                                    return;
                                }
                                var id = grid.getSelectionModel().getSelected().get('partidoId');
                                if(id == -1){
                                    Ext.MessageBox.alert('Eliminar','No puede eliminar la opcion Indistinto');
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'index.php/propiedades/eliminarPartido',
                                    success: 	function(respuesta){
                                                   if(respuesta.responseText=='true'){
                                                        store.reload();
                                                   }else{
                                                       Ext.MessageBox.alert('Eliminar',respuesta.responseText);
                                                   }
                                                },
                                    params: {
                                        id:id
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            win = desktop.createWindow({
                id: 'wAbmPartido',
                xtype: 'window',
                layout: 'fit',
                title: 'Partidos',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                width:400,
                height:400,
                items: grid
           });
        }
        win.show();
}

function createWindowMonedas(){
    var desktop = MyDesktop.getDesktop();
	var win = desktop.getWindow('wAbmMonedas');
	if(!win){
        var cm = new Ext.grid.ColumnModel([{
                id:'tipoMonedaNombre',
               header: "Moneda",
               dataIndex: 'tipoMonedaNombre',
               sortable: true,
               width: 220
            },{
               id:'tipoMonedaNombre',
               header: "Valor",
               dataIndex: 'tipoMonedaValor',
               sortable: true,
               width: 220
            }
        ]);
        var store = new Ext.data.JsonStore({
                url: "index.php/propiedades/ListadoTipoMoneda",
                root: 'tipo-moneda',
                fields: ['tipoMonedaId', 'tipoMonedaNombre','tipoMonedaValor'],
                sortInfo: {field: "tipoMonedaNombre", direction: "DESC"},
                autoLoad: true
            });
            var grid = new Ext.grid.GridPanel({
                id: 'abm-monedas-grid',
                store: store,
                cm: cm,
                width:600,
                height:300,
                loadMask: true,
                autoExpandColumn:'tipoMonedaNombre',
                multiSelect: false,
                 singleSelect: true,
                 sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
                viewConfig:{
                   forceFit: true,
                   sortDescText: 'Orden descendente',
                   sortAscText:'Orden ascendente',
                   columnsText: 'Columnas'
                },
                frame:true,
                tbar: [{
                    text: 'Nueva moneda',
                    iconCls:'nuevo',
                    handler : function(){
                        var desktop = MyDesktop.getDesktop();
                        var win = desktop.getWindow('editar-moneda-win');
                        if(win){
                            Ext.MessageBox.alert('Nuevo','Ya hay una ventana abierta de edición');
                        }
                        createWindowsEditarMoneda(-1);
                    }
                },{
                    text: 'Editar',
                    iconCls:'editar',
                    handler : function(){
                        var desktop = MyDesktop.getDesktop();
                        var win = desktop.getWindow('editar-moneda-win');
                        if(win){
                            Ext.MessageBox.alert('Nuevo','Ya hay una ventana abierta de edición');
                        }
                        if(grid.getSelectionModel().getSelected()==undefined){
                            Ext.MessageBox.alert('Editar','Seleccione la moneda a editar');
                            return;
                        }
                        createWindowsEditarMoneda(grid.getSelectionModel().getSelected().get('tipoMonedaId'),grid.getSelectionModel().getSelected().get('tipoMonedaNombre'),
                            grid.getSelectionModel().getSelected().get('tipoMonedaValor'));
                    }
                },{
                    text: 'Eliminar moneda',
                    iconCls:'borrar',
                    handler : function(){
                        Ext.MessageBox.show({
                           title: 'Eliminar',
                           msg: 'Esta seguro que desea eliminar la moneda seleccionada?',
                           width:300,
                           buttons: Ext.Msg.YESNO,
                           buttonText: Ext.MessageBox.buttonText.yes = "Si",
                           fn: showResultText
                       });
                        function showResultText(btn, text){
                            if(btn == 'no'){
                                return;
                            }else{
                                if(grid.getSelectionModel().getSelected()==undefined){
                                    Ext.MessageBox.alert('Eliminar','Seleccione la moneda a eliminar');
                                    return;
                                }
                                var id = grid.getSelectionModel().getSelected().get('tipoMonedaId');
                                if(id == -1){
                                    Ext.MessageBox.alert('Eliminar','No puede eliminar la opcion Indistinto');
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'index.php/propiedades/eliminarMoneda',
                                    success: 	function(respuesta){
                                                   if(respuesta.responseText=='true'){
                                                        store.reload();
                                                   }else{
                                                       Ext.MessageBox.alert('Eliminar',respuesta.responseText);
                                                   }
                                                },
                                    params: {
                                        id:id
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            win = desktop.createWindow({
                id: 'wAbmMoneda',
                xtype: 'window',
                layout: 'fit',
                title: 'Monedas',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                width:400,
                height:400,
                items: grid
           });
        }
        win.show();
}

function createWindowCargarPropiedades(){
var desktop = this.app.getDesktop();
        var win = desktop.getWindow('propiedades-win');
        if(!win){
            var storePropiedades = new Ext.data.JsonStore({
                root: 'propiedades',
                totalProperty: 'total',
                idProperty: 'threadid',
                remoteSort: true,
                fields: ['id', 'tipo_propiedad', 'tipo_operacion', 'partido', 'localidad', 'provincia', 'ambientes', 'descripcion',
                    'posx', 'posy', 'mostrar', 'precio', 'ubicacion', 'provinciaId','localidadId','partidoId','tipoOperacionId','tipoPropiedadId','monedaId','valor'],
                proxy: new Ext.data.ScriptTagProxy({
                        url: "index.php/propiedades/list"
                }),
                listeners:{ beforeload:function(sto,opt){
                        if(opt.params.tipoPropiedadFiltro==undefined){
                            Ext.getCmp('tipo-propiedad').reset();
                        }
                        if(opt.params.tipoOperacionFiltro==undefined){
                            Ext.getCmp('tipo-operacion').reset();
                        }
                        if(opt.params.provinciaFiltro==undefined){
                            Ext.getCmp('tipo-provincia').reset();
                        }
                        if(opt.params.partidoFiltro==undefined){
                            Ext.getCmp('tipo-partido').reset();
                        }
                        if(opt.params.localidadFiltro==undefined){
                            Ext.getCmp('tipo-localidad').reset();
                        }
                }}
            });
            var pagingBar = new Ext.PagingToolbar({
                pageSize: 25,
                store: storePropiedades,
                displayInfo: true,
                width: 500,
                displayMsg: 'Propiedades {0} - {1} de {2}',
                firstText: 'Primer página',
                lastText: ' Última página',
                nextText: 'Próxima página',
                prevText: 'Página anterior',
                refreshText: 'Reiniciar busqueda',
                emptyMsg: "No hay propiedades para mostrar",
                items:[
                    '-', {
                    iconCls:'accordion',
                    text: 'Ver fotos',
                    listeners:{click:function(obj, e){
                        var elemento = Ext.getCmp('propiedades-grid').getSelectionModel().getSelected();
                        if (elemento == undefined){
                            Ext.MessageBox.alert('Seleccion','Seleccione alguna propiedad');
                            return;
                        }
                        propiedad_seleccionada = elemento.data.id;
                        mostrarFotos();
                    }}
                }]
            });
        var formulario = new Ext.form.FormPanel({
                id: 'propiedades-form-panel',
                baseCls: 'x-plain',
                region    : 'center',
                listeners:{  resize:function(panel, width,  height){
                        Ext.getCmp('propiedades-grid').setWidth(width);
                        Ext.getCmp('propiedades-grid').setHeight(300);
                        Ext.getCmp('detalles-area').setWidth(width);
                }},
                items: [
                    new Ext.grid.GridPanel({
                    id: 'propiedades-grid',
                    loadMask: true,
                    store: storePropiedades,
                    columns: [
                              {header: 'Tipo propiedad',  sortable: true, dataIndex: 'tipo_propiedad'},
                              {header: 'Tipo operacion', sortable: true, dataIndex: 'tipo_operacion'},
                              {header: 'Partido', sortable: true, dataIndex: 'partido'},
                              {header: 'Localidad', sortable: true, dataIndex: 'localidad'},
                              {header: 'Provincia', sortable: true, dataIndex: 'provincia'},
                              {header: 'Ubicación', sortable: true, dataIndex: 'ubicacion'},
                              {header: 'Ambientes', sortable: true, dataIndex: 'ambientes'},
                              {header: 'Precio', sortable: true, dataIndex: 'precio'}
                    ],
                    viewConfig:{
                       sortDescText: 'Orden descendente',
                       sortAscText:'Orden ascendente',
                       columnsText: 'Columnas'
                    },
                    tbar: [{
                        text: 'Nueva propiedad',
                        iconCls:'nuevo',
                        handler : function(){
                            var desktop = MyDesktop.getDesktop();
                            var win = desktop.getWindow('editar-propiedad-win');
                            if(win){
                                Ext.MessageBox.alert('Nuevo','Ya hay una ventana abierta de edición de propiedades');
                            }
                            createWindowsEditarPropiedad(-1);
                        }
                    },{
                    text: 'Editar',
                    iconCls:'editar',
                    handler : function(){
                        var desktop = MyDesktop.getDesktop();
                        var win = desktop.getWindow('editar-propiedad-win');
                        if(win){
                            Ext.MessageBox.alert('Nuevo','Ya hay una ventana abierta de edición de propiedades');
                        }
                        if(Ext.getCmp('propiedades-grid').getSelectionModel().getSelected()==undefined){
                            Ext.MessageBox.alert('Editar','Seleccione la propiedad a editar');
                            return;
                        }
                        var id = Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('id');
                        var registro = new Array();
                        registro['provinciaId']=Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('provinciaId');
                        registro['localidadId']=Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('localidadId');
                        registro['partidoId']=Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('partidoId');
                        registro['tipoPropiedadId']=Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('tipoPropiedadId');
                        registro['tipoOperacionId']=Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('tipoOperacionId');
                        registro['ubicacion']=Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('ubicacion');
                        registro['ambientes']=Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('ambientes');
                        registro['descripcion']=Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('descripcion');
                        registro['mostrar']=Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('mostrar');
                        registro['monedaId']=Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('monedaId');
                        registro['valor']=Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('valor');
                       registro['posx']=Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('posx');
                       registro['posy']=Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('posy');
                        createWindowsEditarPropiedad(id,registro);
                    }
                },{
                    text: 'Eliminar propiedad',
                    iconCls:'borrar',
                    handler : function(){
                        Ext.MessageBox.show({
                           title: 'Eliminar',
                           msg: 'Esta seguro que desea eliminar la propiedad seleccionada?',
                           width:300,
                           buttons: Ext.Msg.YESNO,
                           buttonText: Ext.MessageBox.buttonText.yes = "Si",
                           fn: showResultText
                       });
                        function showResultText(btn, text){
                            if(btn == 'no'){
                                return;
                            }else{
                                if(Ext.getCmp('propiedades-grid').getSelectionModel().getSelected()==undefined){
                                    Ext.MessageBox.alert('Eliminar','Seleccione la propiedad  a eliminar');
                                    return;
                                }
                                var id = Ext.getCmp('propiedades-grid').getSelectionModel().getSelected().get('id');
                                if(id == -1){
                                    Ext.MessageBox.alert('Eliminar','No puede eliminar la opcion Indistinto');
                                    return;
                                }
                                Ext.Ajax.request({
                                    url: 'index.php/propiedades/eliminarPropiedad',
                                    success: 	function(respuesta){
                                                   if(respuesta.responseText=='true'){
                                                        Ext.getCmp('propiedades-grid').getStore().reload();
                                                   }else{
                                                       Ext.MessageBox.alert('Eliminar',respuesta.responseText);
                                                   }
                                                },
                                    params: {
                                        id:id
                                    }
                                });
                            }
                        }
                    }
                }],
                    multiSelect: false,
                    title:'Lista de propiedades',
                    bbar: pagingBar,
                    height: 400,
                    listeners:{cellclick:function(grid, rowIndex, columnIndex, e){
                        var descripcion = grid.getStore().getAt(rowIndex).get('descripcion');
                        Ext.getCmp('detalles-area').setValue(descripcion);
                    }}
                })
            ,
                new Ext.Panel({
                    id: 'panel-detalle',
                    autoWidth: true,
                    autoHeight: true,
                    height: 200,
                    items: [{
                            id: 'detalles-area',
                            xtype: 'textarea',
                            disabled:true,
                            emptyText: 'Seleccione una propiedad para ver los detalles'
                        }]
                })
            ]
            });
            var buscar = new Ext.FormPanel({
                    title       : 'Buscador',
                    region      : 'west',
                    split       : true,
                    width       : 240,
                    labelWidth: 65,
                    collapsible : true,
                    collapsed:true,
                    defaultType: 'combo',
                    bodyStyle:'padding:5px;',
                    items: [{
                        name: 'tipo-propiedad',
                        id: 'tipo-propiedad',
                        displayField:'tipoPropiedadNombre',
                        mode: 'remote',
                        triggerAction: 'all',
                        width: 150,
                        emptyText:'Seleccione una opción...',
                        selectOnFocus:true,
                        editable : false,
                        fieldLabel: 'Tipo de Propiedad',
                        store: new Ext.data.JsonStore({
                                url: 'index.php/propiedades/ListadoTipoPropiedades',
                                root: 'tipo-propiedad',
                                fields: ['tipoPropiedadId', 'tipoPropiedadNombre'],
                                autoLoad: true
                            }),
                        listeners:{select:function(combo, record,  e){
                                storePropiedades.load({
                                    params:{
                                        start:0,
                                        limit:25,
                                        tipoPropiedadFiltro:Ext.getCmp('tipo-propiedad').getValue(),
                                        tipoOperacionFiltro:Ext.getCmp('tipo-operacion').getValue(),
                                        provinciaFiltro:Ext.getCmp('tipo-provincia').getValue(),
                                        partidoFiltro:Ext.getCmp('tipo-partido').getValue(),
                                        localidadFiltro:Ext.getCmp('tipo-localidad').getValue()
                                        //tipoPropiedadFiltro:record.get('tipoPropiedadNombre')
                                    }
                                });
                                Ext.getCmp('detalles-area').setValue('');
                        }}
                    },{
                        id: 'tipo-operacion',
                        name: 'tipo-operacion',
                        displayField:'tipoOperacionNombre',
                        mode: 'remote',
                        triggerAction: 'all',
                        width: 150,
                        emptyText:'Seleccione una opción...',
                        selectOnFocus:true,
                        editable : false,
                        fieldLabel: 'Tipo de Operación',
                        store: new Ext.data.JsonStore({
                                url: 'index.php/propiedades/ListadoTipoOperacion',
                                root: 'tipo-operacion',
                                fields: ['tipoOperacionId', 'tipoOperacionNombre'],
                                autoLoad: true
                            }),
                        listeners:{select:function(combo, record,  e){
                                storePropiedades.load({
                                    params:{
                                        start:0,
                                        limit:25,
                                        tipoPropiedadFiltro:Ext.getCmp('tipo-propiedad').getValue(),
                                        tipoOperacionFiltro:Ext.getCmp('tipo-operacion').getValue(),
                                        provinciaFiltro:Ext.getCmp('tipo-provincia').getValue(),
                                        partidoFiltro:Ext.getCmp('tipo-partido').getValue(),
                                        localidadFiltro:Ext.getCmp('tipo-localidad').getValue()
                                    }
                                });
                                Ext.getCmp('detalles-area').setValue('');
                        }}
                    },{
                        id: 'tipo-provincia',
                        name: 'tipo-provincia',
                        displayField:'provinciaNombre',
                        mode: 'remote',
                        triggerAction: 'all',
                        width: 150,
                        emptyText:'Seleccione una opción...',
                        selectOnFocus:true,
                        editable : false,
                        fieldLabel: 'Provincia',
                        store: new Ext.data.JsonStore({
                                url: 'index.php/propiedades/ListadoProvincia',
                                root: 'provincia',
                                fields: ['provinciaId', 'provinciaNombre'],
                                autoLoad: true
                            }),
                        listeners:{select:function(combo, record,  e){
                                storePropiedades.load({
                                    params:{
                                        start:0,
                                        limit:25,
                                        tipoPropiedadFiltro:Ext.getCmp('tipo-propiedad').getValue(),
                                        tipoOperacionFiltro:Ext.getCmp('tipo-operacion').getValue(),
                                        provinciaFiltro:Ext.getCmp('tipo-provincia').getValue(),
                                        partidoFiltro:Ext.getCmp('tipo-partido').getValue(),
                                        localidadFiltro:Ext.getCmp('tipo-localidad').getValue()
                                    }
                                });
                                Ext.getCmp('detalles-area').setValue('');
                        }}
                    },{
                        id: 'tipo-localidad',
                        name: 'tipo-localidad',
                        displayField:'localidadNombre',
                        mode: 'remote',
                        triggerAction: 'all',
                        width: 150,
                        emptyText:'Seleccione una opción...',
                        selectOnFocus:true,
                        editable : false,
                        fieldLabel: 'Localidad',
                        store: new Ext.data.JsonStore({
                                url: 'index.php/propiedades/ListadoLocalidad',
                                root: 'tipo-localidad',
                                fields: ['localidadId', 'localidadNombre'],
                                autoLoad: true
                            }),
                        listeners:{select:function(combo, record,  e){
                                storePropiedades.load({
                                    params:{
                                        start:0,
                                        limit:25,
                                        tipoPropiedadFiltro:Ext.getCmp('tipo-propiedad').getValue(),
                                        tipoOperacionFiltro:Ext.getCmp('tipo-operacion').getValue(),
                                        provinciaFiltro:Ext.getCmp('tipo-provincia').getValue(),
                                        partidoFiltro:Ext.getCmp('tipo-partido').getValue(),
                                        localidadFiltro:Ext.getCmp('tipo-localidad').getValue()
                                    }
                                });
                                Ext.getCmp('detalles-area').setValue('');
                        }}
                    },{
                        id: 'tipo-partido',
                        name: 'tipo-partido',
                        displayField:'partidoNombre',
                        mode: 'remote',
                        triggerAction: 'all',
                        width: 150,
                        emptyText:'Seleccione una opción...',
                        selectOnFocus:true,
                        editable : false,
                        fieldLabel: 'Partido',
                        store: new Ext.data.JsonStore({
                                url: 'index.php/propiedades/ListadoPartido',
                                root: 'tipo-partido',
                                fields: ['partidoId', 'partidoNombre'],
                                autoLoad: true
                            }),
                        listeners:{select:function(combo, record,  e){
                                storePropiedades.load({
                                    params:{
                                        start:0,
                                        limit:25,
                                        tipoPropiedadFiltro:Ext.getCmp('tipo-propiedad').getValue(),
                                        tipoOperacionFiltro:Ext.getCmp('tipo-operacion').getValue(),
                                        provinciaFiltro:Ext.getCmp('tipo-provincia').getValue(),
                                        partidoFiltro:Ext.getCmp('tipo-partido').getValue(),
                                        localidadFiltro:Ext.getCmp('tipo-localidad').getValue()
                                    }
                                });
                                Ext.getCmp('detalles-area').setValue('');
                        }}
                    }]
                });
            win = desktop.createWindow({
                id: 'propiedades-win',
                title:'Propiedades',
                labelWidth: 75, // label settings here cascade unless overridden
                frame:true,
                bodyStyle: 'padding:5px 5px 0',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                width: 970,
                height: 500,
                plain    : true,
                layout   : 'border',
                items:  [buscar,formulario]
            });
            storePropiedades.setDefaultSort('tipo_propiedad', 'ASC');
            storePropiedades.load({params:{start:0, limit:25}});
        }
        Ext.getCmp('detalles-area').setHeight(170);
        win.show();
 }

function createWindowsEditarMoneda(id,nombre,valor){
        var titulo = -1;
        if(id==-1){
            titulo = 'Nueva moneda';
        }else{
            titulo = 'Editar moneda';
        }
        var desktop = MyDesktop.getDesktop();
        var win = desktop.getWindow('editar-moneda-win');
        if(!win){
            win = desktop.createWindow({
                id: 'editar-moneda-win',
                title: titulo,
                width:300,
                height:150,
                iconCls: 'user',
                shim:false,
                animCollapse:false,
                maximizable: false,
                constrainHeader:true,
                items: new Ext.form.FormPanel({
                    id: 'editar-moneda-form-panel',
                    baseCls: 'x-plain',
                    labelWidth:80,
                    bodyStyle:'padding:5px;',
                    items: [{
                                id: 'text-nombre-moneda',
                                fieldLabel: 'Nombre',
                                xtype: 'textfield',
                                name: 'nombre',
                                 width: 150,
                                 blankText: 'Por favor ingrese la moneda',
                                 allowBlank:false
                            },{
                                id: 'text-valor-moneda',
                                fieldLabel: 'Valor',
                                xtype: 'textfield',
                                name: 'valor',
                                 width: 150,
                                 blankText: 'Por favor ingrese el valor',
                                 vtype: 'float',
                                 allowBlank:false
                            }],
                            buttons: [{
                                text: 'Guardar',
                                type: 'submit',
                                handler: function(){
                                    var fp = Ext.getCmp('editar-moneda-form-panel');
                                    if(fp.getForm().isValid()){
                                        fp.getForm().submit({
                                            url: 'index.php/propiedades/modificarMoneda',
                                            waitMsg: 'Guardando...',
                                            params: "id=" + id,
                                            success: function(fp, o){
                                                if(o.result.Guardar=='true'){
                                                    if(Ext.getCmp('abm-monedas-grid')!=undefined){
                                                        Ext.getCmp('abm-monedas-grid').getStore().reload()
                                                    }
                                                    win.close();
                                                }else{
                                                    Ext.MessageBox.alert('Guardar', o.result.Guardar);
                                                }
                                            }
                                        });
                                    }
                                }
                            },{
                                text: 'Cancel',
                                handler: function(){
                                    win.close();
                                }
                            }]
                }),
                layout: 'fit'
            });
        }
        win.show();
        Ext.getCmp('text-nombre-moneda').setValue(nombre);
        Ext.getCmp('text-valor-moneda').setValue(valor);
}
function createWindowsEditarLocalidad(id,provinciaId,nombre){
        var titulo = -1;
        if(id==-1){
            titulo = 'Nueva localidad';
        }else{
            titulo = 'Editar localidad';
        }
        var desktop = MyDesktop.getDesktop();
        var win = desktop.getWindow('editar-localidad-win');
        if(!win){
            win = desktop.createWindow({
                id: 'editar-localidad-win',
                title: titulo,
                width:300,
                height:150,
                iconCls: 'user',
                shim:false,
                animCollapse:false,
                maximizable: false,
                constrainHeader:true,
                items: new Ext.form.FormPanel({
                    id: 'editar-localidad-form-panel',
                    baseCls: 'x-plain',
                    labelWidth:80,
                    bodyStyle:'padding:5px;',
                    items: [{
                                id: 'combo-form-localidad',
                                fieldLabel: 'Provincia',
                                displayField:'provinciaNombre',
                                valueField: 'provinciaId',
                                xtype: 'combo',
                                name: 'provincia',
                                triggerAction: 'all',
                                width: 150,
                                emptyText:'Seleccione una opción...',
                                selectOnFocus:true,
                                editable : false,
                                blankText: 'Seleccione una provincia',
                                allowBlank:false,
                                store: new Ext.data.JsonStore({
                                    url: 'index.php/propiedades/ListadoProvinciaSinIndistinto',
                                    root: 'provincia',
                                    fields: ['provinciaId', 'provinciaNombre'],
                                    autoLoad: true,
                                    listeners:{datachanged:function(combo, record,  e){
                                            Ext.getCmp('combo-form-localidad').setValue(provinciaId);
                                            Ext.getCmp('text-nombre-localidad').setValue(nombre);
                                    }}
                                })
                            },{
                                id: 'text-nombre-localidad',
                                fieldLabel: 'Nombre',
                                xtype: 'textfield',
                                name: 'nombre',
                                 width: 150,
                                 blankText: 'Por favor ingrese la localidad',
                                 allowBlank:false
                            }],
                            buttons: [{
                                text: 'Guardar',
                                type: 'submit',
                                handler: function(){
                                    var fp = Ext.getCmp('editar-localidad-form-panel');
                                    if(fp.getForm().isValid()){
                                        fp.getForm().submit({
                                            url: 'index.php/propiedades/modificarLocalidad',
                                            waitMsg: 'Guardando...',
                                            params: "id=" + id + "&provinciaId=" + Ext.getCmp('combo-form-localidad').getValue(),
                                            success: function(fp, o){
                                                if(o.result.Guardar=='true'){
                                                    if(Ext.getCmp('abm-localidad-grid')!=undefined){
                                                        Ext.getCmp('abm-localidad-grid').getStore().reload()
                                                    }
                                                    win.close();
                                                }else{
                                                    Ext.MessageBox.alert('Guardar', o.result.Guardar);
                                                }
                                            }
                                        });
                                    }
                                }
                            },{
                                text: 'Cancel',
                                handler: function(){
                                    win.close();
                                }
                            }]
                }),
                layout: 'fit'
            });
        }
        win.show();
}

function provinciaIdParam(){
    if(Ext.getCmp('combo-form-provincia-partido')!=undefined){
        return Ext.getCmp('combo-form-provincia-partido').getValue();
    }else{
        return -1;
    }
}
function createWindowsEditarPartido(id,provinciaId,localidadId,nombre){
        var titulo = -1;
        if(id==-1){
            titulo = 'Nuevo partido';
        }else{
            titulo = 'Editar partido';
            cargar = true;
        }
        var desktop = MyDesktop.getDesktop();
        var win = desktop.getWindow('editar-partido-win');
        if(!win){
            win = desktop.createWindow({
                id: 'editar-partido-win',
                title: titulo,
                width:300,
                height:200,
                iconCls: 'user',
                shim:false,
                animCollapse:false,
                maximizable: false,
                constrainHeader:true,
                items: new Ext.form.FormPanel({
                    id: 'editar-partido-form-panel',
                    baseCls: 'x-plain',
                    labelWidth:80,
                    bodyStyle:'padding:5px;',
                    items: [{
                                id: 'combo-form-provincia-partido',
                                fieldLabel: 'Provincia',
                                displayField:'provinciaNombre',
                                valueField: 'provinciaId',
                                xtype: 'combo',
                                name: 'provincia',
                                triggerAction: 'all',
                                width: 150,
                                emptyText:'Seleccione una opción...',
                                selectOnFocus:true,
                                editable : false,
                                blankText: 'Seleccione una provincia',
                                allowBlank:false,
                                listeners:{ select:function(combo, record,  e){
                                        Ext.getCmp('combo-form-localidad-partido').reset();
                                        Ext.getCmp('combo-form-localidad-partido').getStore().reload({
                                        params:{
                                            provinciaId: provinciaIdParam()
                                        }
                                    });
                                }},
                                store: new Ext.data.JsonStore({
                                    url: 'index.php/propiedades/ListadoProvinciaSinIndistinto',
                                    root: 'provincia',
                                    fields: ['provinciaId', 'provinciaNombre'],
                                    autoLoad: true,
                                    listeners:{datachanged:function(combo, record,  e){
                                            Ext.getCmp('combo-form-provincia-partido').setValue(provinciaId);
                                            Ext.getCmp('combo-form-localidad-partido').getStore().load({
                                                params:{
                                                    provinciaId: provinciaId
                                                }
                                            });
                                            //Ext.getCmp('combo-form-localidad-partido').setValue(localidadId);
                                    }}
                                })
                            },{
                                id: 'combo-form-localidad-partido',
                                fieldLabel: 'Localidad',
                                displayField:'localidadNombre',
                                valueField: 'localidadId',
                                xtype: 'combo',
                                name: 'localidad',
                                triggerAction: 'all',
                                width: 150,
                                emptyText:'Seleccione una opción...',
                                selectOnFocus:true,
                                editable : false,
                                blankText: 'Seleccione una localidad',
                                allowBlank:false,
                                mode:'local',
                                store: new Ext.data.JsonStore({
                                    proxy: new Ext.data.ScriptTagProxy({
                                            url: 'index.php/propiedades/ListadoLocalidadDeProvincia'
                                    }),
                                    root: 'tipo-localidad',
                                    fields: ['localidadId', 'localidadNombre'],
                                    //autoLoad: true,
                                    listeners:{ datachanged:function(combo, record,  e){
                                            if(id!=-1 && cargar){
                                                setTimeout(function(){Ext.getCmp('combo-form-localidad-partido').setValue(localidadId)}, 100);
                                                cargar = false;
                                            }
                                    }}
                                })
                            },{
                                id: 'text-nombre-partido',
                                fieldLabel: 'Nombre',
                                xtype: 'textfield',
                                name: 'nombre',
                                 width: 150,
                                 blankText: 'Por favor ingrese el partido',
                                 allowBlank:false
                            }],
                            buttons: [{
                                text: 'Guardar',
                                type: 'submit',
                                handler: function(){
                                    var fp = Ext.getCmp('editar-partido-form-panel');
                                    if(fp.getForm().isValid()){
                                        fp.getForm().submit({
                                            url: 'index.php/propiedades/modificarPartido',
                                            waitMsg: 'Guardando...',
                                            params: "id=" + id + "&provinciaId=" + Ext.getCmp('combo-form-provincia-partido').getValue() +
                                                    "&localidadId=" +Ext.getCmp('combo-form-localidad-partido').getValue() ,
                                            success: function(fp, o){
                                                if(o.result.Guardar=='true'){
                                                    if(Ext.getCmp('abm-partido-grid')!=undefined){
                                                        Ext.getCmp('abm-partido-grid').getStore().reload()
                                                    }
                                                    win.close();
                                                }else{
                                                    Ext.MessageBox.alert('Guardar', o.result.Guardar);
                                                }
                                            }
                                        });
                                    }
                                }
                            },{
                                text: 'Cancel',
                                handler: function(){
                                    win.close();
                                }
                            }]
                }),
                layout: 'fit'
            });
        }
        Ext.getCmp('text-nombre-partido').setValue(nombre);
        win.show();
}

function createWindowsEditarPropiedad(id,registro){
        var titulo = '';
        if(id==-1){
            titulo = 'Nueva propiedad';
        }else{
            titulo = 'Editar propiedad';
            cargarLocalidad = true;
            cargarPartido = true;
        }
        var desktop = MyDesktop.getDesktop();
        var win = desktop.getWindow('editar-propiedad-win');
        if(!win){
            win = desktop.createWindow({
                id: 'editar-propiedad-win',
                title: titulo,
                width:800,
                height:530,
                minWidth: 800,
                x:50,
                y:50,
                iconCls: 'user',
                shim:false,
                animCollapse:false,
                maximizable: false,
                constrainHeader:true,
                items: new Ext.form.FormPanel({
                    id: 'editar-propiedad-form-panel',
                    baseCls: 'x-plain',
                    labelWidth:80,
                    width:380,
                    bodyStyle:'padding:5px;',
                    layout:"column",
                    items:[{
                                    layout:"column",
                                    items:[{
                                        width:380,
                                        xtype:"panel",
                                         layout:"form",
                                        title:"Datos",
                                        items:[{
                                                                id: 'combo-form-provincia-propiedad',
                                                                fieldLabel: 'Provincia',
                                                                displayField:'provinciaNombre',
                                                                valueField: 'provinciaId',
                                                                xtype: 'combo',
                                                                name: 'provincia',
                                                                triggerAction: 'all',
                                                                width: 250,
                                                                frame:true,
                                                                emptyText:'Seleccione una opción...',
                                                                selectOnFocus:true,
                                                                editable : false,
                                                                blankText: 'Seleccione una provincia',
                                                                allowBlank:false,
                                                                listeners:{ select:function(combo, record,  e){
                                                                        Ext.getCmp('combo-form-localidad-propiedad').reset();
                                                                        Ext.getCmp('combo-form-partido-propiedad').reset();
                                                                        Ext.getCmp('combo-form-localidad-propiedad').getStore().reload({
                                                                            params:{
                                                                                provinciaId: combo.getValue()
                                                                            }
                                                                        });
                                                                        Ext.getCmp('combo-form-partido-propiedad').getStore().reload({
                                                                            params:{
                                                                                localidadId: -1
                                                                            }
                                                                        });
                                                                }},
                                                                store: new Ext.data.JsonStore({
                                                                    url: 'index.php/propiedades/ListadoProvinciaSinIndistinto',
                                                                    root: 'provincia',
                                                                    fields: ['provinciaId', 'provinciaNombre'],
                                                                    autoLoad: true,
                                                                    listeners:{datachanged:function(combo, record,  e){
                                                                            var c = Ext.getCmp('combo-form-provincia-propiedad').getValue();
                                                                            if(registro != undefined){
                                                                                c = registro['provinciaId'];
                                                                            }
                                                                            Ext.getCmp('combo-form-localidad-propiedad').getStore().reload({
                                                                                params:{
                                                                                    provinciaId: c
                                                                                }
                                                                            });
                                                                            if(id!=-1){
                                                                                setTimeout(function(){Ext.getCmp('combo-form-provincia-propiedad').setValue(registro['provinciaId'])}, 100);
                                                                            }
                                                                    }}
                                                                })
                                                            },{
                                                                id: 'combo-form-localidad-propiedad',
                                                                fieldLabel: 'Localidad',
                                                                displayField:'localidadNombre',
                                                                valueField: 'localidadId',
                                                                xtype: 'combo',
                                                                name: 'localidad',
                                                                triggerAction: 'all',
                                                                width: 250,
                                                                emptyText:'Seleccione una opción...',
                                                                selectOnFocus:true,
                                                                editable : false,
                                                                blankText: 'Seleccione una localidad',
                                                                allowBlank:false,
                                                                mode:'local',
                                                                listeners:{ select:function(combo, record,  e){
                                                                        Ext.getCmp('combo-form-partido-propiedad').reset();
                                                                        Ext.getCmp('combo-form-partido-propiedad').getStore().reload({
                                                                            params:{
                                                                                localidadId: combo.getValue()
                                                                            }
                                                                        });
                                                                }},
                                                                store: new Ext.data.JsonStore({
                                                                    proxy: new Ext.data.ScriptTagProxy({
                                                                            url: 'index.php/propiedades/ListadoLocalidadDeProvincia'
                                                                    }),
                                                                    root: 'tipo-localidad',
                                                                    fields: ['localidadId', 'localidadNombre'],
                                                                    //autoLoad: true,
                                                                    listeners:{datachanged:function(combo, record,  e){
                                                                            var c = Ext.getCmp('combo-form-localidad-propiedad').getValue();
                                                                            if(registro != undefined){
                                                                                c = registro['localidadId'];
                                                                            }
                                                                           Ext.getCmp('combo-form-partido-propiedad').getStore().reload({
                                                                                params:{
                                                                                    localidadId: c
                                                                                }
                                                                            });
                                                                            if(id!=-1 && cargarLocalidad){
                                                                                setTimeout(function(){Ext.getCmp('combo-form-localidad-propiedad').setValue(registro['localidadId'])}, 100);
                                                                                cargarLocalidad = false;
                                                                            }
                                                                    }}
                                                                })
                                                            },{
                                                                id: 'combo-form-partido-propiedad',
                                                                fieldLabel: 'Partido',
                                                                displayField:'partidoNombre',
                                                                valueField: 'partidoId',
                                                                xtype: 'combo',
                                                                name: 'partido',
                                                                triggerAction: 'all',
                                                                width: 250,
                                                                emptyText:'Seleccione una opción...',
                                                                selectOnFocus:true,
                                                                editable : false,
                                                                blankText: 'Seleccione un partido',
                                                                allowBlank:false,
                                                                mode:'local',
                                                                store: new Ext.data.JsonStore({
                                                                    url: 'index.php/propiedades/ListadoPartidoDeLocalidad',
                                                                    root: 'tipo-partido',
                                                                    fields: ['partidoId', 'partidoNombre'],
                                                                    //autoLoad: true,
                                                                    listeners:{datachanged:function(combo, record,  e){
                                                                            if(id!=-1 && cargarPartido){
                                                                                setTimeout(function(){Ext.getCmp('combo-form-partido-propiedad').setValue(registro['partidoId'])}, 100);
                                                                                cargarPartido=false;
                                                                            }
                                                                    }}
                                                                })
                                                            },{
                                                                id: 'combo-form-tipo-propiedad-propiedad',
                                                                fieldLabel: 'Tipo propiedad',
                                                                displayField:'tipoPropiedadNombre',
                                                                valueField: 'tipoPropiedadId',
                                                                xtype: 'combo',
                                                                name: 'tipo-propiedad',
                                                                triggerAction: 'all',
                                                                width: 250,
                                                                emptyText:'Seleccione una opción...',
                                                                selectOnFocus:true,
                                                                editable : false,
                                                                blankText: 'Seleccione un tipo de propiedad',
                                                                allowBlank:false,
                                                                //mode:'local',
                                                                store: new Ext.data.JsonStore({
                                                                    url: 'index.php/propiedades/ListadoTipoPropiedadesSinIndistinto',
                                                                    root: 'tipo-propiedad',
                                                                    fields: ['tipoPropiedadId', 'tipoPropiedadNombre'],
                                                                    autoLoad: true,
                                                                    listeners:{datachanged:function(combo, record,  e){
                                                                        if(id!=-1){
                                                                                setTimeout(function(){Ext.getCmp('combo-form-tipo-propiedad-propiedad').setValue(registro['tipoPropiedadId'])}, 100);
                                                                        }
                                                                    }}
                                                                })
                                                            },{
                                                                id: 'combo-form-tipo-operacion-propiedad',
                                                                fieldLabel: 'Tipo operación',
                                                                displayField:'tipoOperacionNombre',
                                                                valueField: 'tipoOperacionId',
                                                                xtype: 'combo',
                                                                name: 'tipo-operacion',
                                                                triggerAction: 'all',
                                                                width: 250,
                                                                emptyText:'Seleccione una opción...',
                                                                selectOnFocus:true,
                                                                editable : false,
                                                                blankText: 'Seleccione un tipo de operación',
                                                                allowBlank:false,
                                                                store: new Ext.data.JsonStore({
                                                                    url: 'index.php/propiedades/ListadoTipoOperacionSinIndistinto',
                                                                    root: 'tipo-operacion',
                                                                    fields: ['tipoOperacionId', 'tipoOperacionNombre'],
                                                                    autoLoad: true,
                                                                    listeners:{datachanged:function(combo, record,  e){
                                                                        if(id!=-1){
                                                                                setTimeout(function(){Ext.getCmp('combo-form-tipo-operacion-propiedad').setValue(registro['tipoOperacionId'])}, 100);
                                                                        }
                                                                    }}
                                                                })
                                                            },{
                                                                    id: 'text-ubicacion-propiedad',
                                                                    fieldLabel: 'Ubicación',
                                                                    xtype: 'textfield',
                                                                    name: 'ubicacion',
                                                                    emptyText: 'Por favor ingrese la dirección',
                                                                    width: 250
                                                            },{
                                                                id:'text-ambientes-propiedad',
                                                                fieldLabel: 'Ambientes',
                                                                width: 250,
                                                                xtype: 'textfield',
                                                                vtype: 'numeric',
                                                                name: 'ambientes'
                                                            },{
                                                                    id:'text-descripcion-propiedad',
                                                                    fieldLabel: 'Descripción',
                                                                    xtype: 'textarea',
                                                                    name: 'descripcion',
                                                                    width: 250,
                                                                    blankText: 'Por favor ingrese la descripcion',
                                                                    allowBlank:false
                                                                },{
                                                                    id: 'chek-mostrar-propiedad',
                                                                    fieldLabel: 'Mostrar',
                                                                    xtype: 'checkbox',
                                                                    name: 'mostrar',
                                                                    checked: true
                                                                },{
                                                                     id: 'combo-form-tipo-moneda-propiedad',
                                                                    fieldLabel: 'Moneda',
                                                                    displayField:'tipoMonedaNombre',
                                                                    valueField: 'tipoMonedaId',
                                                                    xtype: 'combo',
                                                                    name: 'tipo-moneda',
                                                                    triggerAction: 'all',
                                                                    width: 250,
                                                                    emptyText:'Seleccione una moneda...',
                                                                    selectOnFocus:true,
                                                                    editable : false,
                                                                    store: new Ext.data.JsonStore({
                                                                        url: 'index.php/propiedades/ListadoTipoMoneda',
                                                                        root: 'tipo-moneda',
                                                                        fields: ['tipoMonedaId', 'tipoMonedaNombre'],
                                                                        autoLoad: true,
                                                                        listeners:{datachanged:function(combo, record,  e){
                                                                            if(id!=-1){
                                                                                setTimeout(function(){Ext.getCmp('combo-form-tipo-moneda-propiedad').setValue(registro['monedaId'])}, 100);
                                                                        }
                                                                        }}
                                                                    })
                                                            },{
                                                                    id: 'text-precio-propiedad',
                                                                    fieldLabel: 'Precio',
                                                                    xtype: 'textfield',
                                                                    name: 'precio',
                                                                    vtype: 'float',
                                                                    width: 250
                                                            },{
                                                                    id: 'coordenadas-editar-propiedad',
                                                                    fieldLabel: 'Coordenadas',
                                                                    xtype: 'textfield',
                                                                    name: 'coordenadas',
                                                                    disabled: true,
                                                                    width: 250
                                                   }]
                                             },{
                                                    title:"Mapa",
                                                    width:380,
                                                    height:440,
                                                    layout:"form",
                                                    items:[{
                                                                id:'map_canvas',
                                                                xtype: 'gmappanel',
                                                                region: 'center',
                                                                zoomLevel: 14,
                                                                gmapType: 'map',
                                                                mapConfOpts: ['enableScrollWheelZoom','enableDoubleClickZoom','enableDragging'],
                                                                mapControls: ['GSmallMapControl','GMapTypeControl','NonExistantControl']
                                                    }]
                                                  }]
                                                  }]
                }),
                layout: 'fit',
                listeners:{ close:function(elemento){
                    var desktop = MyDesktop.getDesktop();
                    var winMapa = desktop.getWindow('alta-propiedades-win');
                    if(winMapa){
                        winMapa.close();
                    }
                }},
                buttons: [{
                                text: 'Guardar',
                                type: 'submit',
                                handler: function(){
                                    var fp = Ext.getCmp('editar-propiedad-form-panel');
                                    if (Ext.getCmp('text-precio-propiedad').getValue() != '' ){
                                            if(Ext.getCmp('combo-form-tipo-moneda-propiedad').getValue()==''){
                                                Ext.MessageBox.alert('Guardar', 'Si selecciona el valor de la propiedad debe ingresar el tipo de moneda');
                                                return;
                                            }
                                        }

                                    if(fp.getForm().isValid()){
                                        fp.getForm().submit({
                                            url: 'index.php/propiedades/altaPropiedad',
                                            waitMsg: 'Guardando...',
                                            params: "id=" + id+ "&partidoId=" + Ext.getCmp('combo-form-partido-propiedad').getValue() +
                                                    "&tipoOperacionId=" +Ext.getCmp('combo-form-tipo-operacion-propiedad').getValue() +
                                                    "&tipoPropiedadId=" +Ext.getCmp('combo-form-tipo-propiedad-propiedad').getValue() +
                                                    '&tipoMonedaId=' + Ext.getCmp('combo-form-tipo-moneda-propiedad').getValue() +
                                                    '&coordenadas='+Ext.getCmp('coordenadas-editar-propiedad').getValue(),
                                            success: function(fp, o){
                                                if(o.result.Guardar=='true'){
                                                    Ext.getCmp('propiedades-grid').getStore().reload()
                                                    win.close();
                                                }else{
                                                    Ext.MessageBox.alert('Guardar', o.result.Guardar);
                                                }
                                            }
                                        });
                                    }
                                }
                            },{
                                text: 'Cancel',
                                handler: function(){
                                    win.close();
                                }
                            }]
            });
        }
        if(id!=-1){
            Ext.getCmp('text-ubicacion-propiedad').setValue(registro['ubicacion']);
            Ext.getCmp('text-ambientes-propiedad').setValue(registro['ambientes']);
            Ext.getCmp('text-descripcion-propiedad').setValue(registro['descripcion']);
            Ext.getCmp('chek-mostrar-propiedad').setValue(registro['mostrar']);
            Ext.getCmp('text-precio-propiedad').setValue(registro['valor']);
            Ext.getCmp('coordenadas-editar-propiedad').setValue(registro['posx'] + ';' + registro['posy']);
        }
        if(id!=-1){
            var posy = registro['posy'];
            var posx = registro['posx'];
       }else{
           var posy = '-34.692368115514604';
           var posx = '-58.376455307006836';
       }
        var center = new GLatLng(posy, posx);
        var marker = new GMarker(center, {draggable: true});
        Ext.getCmp('map_canvas').getMap().setCenter(center,14);
        Ext.getCmp('map_canvas').getMap().addOverlay(marker);
        GEvent.addListener(marker, "dragend", function() {
            Ext.getCmp('coordenadas-editar-propiedad').setValue(marker.getLatLng().x +';'+ marker.getLatLng().y);
        });
        Ext.getCmp('coordenadas-editar-propiedad').setValue(marker.getLatLng().x +';'+ marker.getLatLng().y);
        win.show();
}


function mostrarFotos(){
    chooser = new ImageChooser({
    			url:'index.php/propiedades/getImages?id='+propiedad_seleccionada,
    			width:575,
                shim:false,
                animCollapse:false,
                maximizable: false,
                constrainHeader:true,
    			height:350
    		});
            chooser.show();
}
function agregarFotos(){
    var desktop = MyDesktop.getDesktop();
    var win = desktop.getWindow('agregar-fotos-form');
    if(!win){
          dialog = new Ext.ux.UploadDialog.Dialog({
              id:'agregar-fotos-form',
              title: 'Fotos',
              modal:true,
              url: 'index.php/propiedades/setImages?id='+propiedad_seleccionada,
              reset_on_hide: false,
              allow_close_on_upload: true,
              permitted_extensions: ['jpg', 'jpeg', 'gif','png']
        });
        dialog.show();
        dialog.on('uploadcomplete', cargaCompleta);
    }
}

function cargaCompleta(){
    Ext.MessageBox.alert('Subir foto','Finalizó la carga de fotos.');
    mostrarFotos();
}