jQuery(function($){
	
	if ($("#territorio049_filter_table").length > 0){
		$("#territorio049_filter_table").rup_combo({
			source: "/ab03bRERAWar/ab03bNora/comboProvincia",
			sourceParam : {label:"desc"+$.rup_utils.capitalizedLang(), value:"code", style:"css"},
			width: 300,
			rowStriping:true,
			inputText:true,
			ordered:true,
			blank:""
		});
		
		$("#municipio049_filter_table").rup_combo({
			source: "/ab03bRERAWar/ab03bNora/comboMunicipio049",
			sourceParam:{
				label:"ds_o",
				value:"id"
			},
			ordered:true,
			blank: "",
			style:"dropdown",
			width:300,
			parent:["territorio049_filter_table"]
		});
	} else {
		$("#municipio049_filter_table").rup_combo({
			source: "/ab03bRERAWar/ab03bNora/comboMunicipio049?territorio049=" + $("#territorio049_filter_table_hidden").val(),
			sourceParam:{
				label:"ds_o",
				value:"id"
			},
			ordered:true,
			blank: "",
			style:"dropdown",
			width:300
		});
	}
	
	if ($("#territorio049_detail_table").length > 0){
		$("#territorio049_detail_table").rup_combo({
			source: "/ab03bRERAWar/ab03bNora/comboProvincia",
			sourceParam:{
				label:"ds_o",
				value:"id"
			},
			ordered:true,
			style:"dropdown",
			width:300, 
			blank:""
		});
		
		$("#municipio049_detail_table").rup_combo({
			source: "/ab03bRERAWar/ab03bNora/comboMunicipio049",
			sourceParam:{
				label:"ds_o",
				value:"id"
			},
			ordered:true,
			blank: "",
			style:"dropdown",
			width:300,
			parent:["territorio049_detail_table"]
		});
	} else {
		$("#municipio049_detail_table").rup_combo({
			source: "/ab03bRERAWar/ab03bNora/comboMunicipio049?territorio049=" + $("#territorio049_detail_table_hidden").val(),
			sourceParam:{
				label:"ds_o",
				value:"id"
			},
			ordered:true,
			blank: "",
			style:"dropdown",
			width:300
		});
	}
	
	$('#id048Org_detail_table').rup_combo({
		source: "/ab03bRERAWar/ab03b48t00/comboOrganismos",
		sourceParam:{
			label:"ds_o",
			value:"id"
		},
		ordered:true,
		blank: "",
		style:"dropdown",
		width:200
	});
	
	$('#id048Org_filter_table').rup_combo({
		source: "/ab03bRERAWar/ab03b48t00/comboOrganismos",
		sourceParam:{
			label:"ds_o",
			value:"id"
		},
		ordered:true,
		blank: "",
		style:"dropdown",
		width:200
	});
	
	$('#regManual049').rup_combo({
		source : [
				   {label: $.rup.i18n.app.mantenimientos.todas, value:""},
				   {label: $.rup.i18n.app.mantenimientos.manual, value:"1"},
				   {label: $.rup.i18n.app.mantenimientos.automatico, value:"0"}					   
				],
		width: 200,
		rowStriping : true,
		inputText:true,
		ordered:true
	});
	
	if(esUsuarioModificacion){
		$("#MantDelegaciones").rup_table({
			url: "../ab03b49v00",
			colNames: [
				$.rup.i18n.app.mantenimientos.id,
				$.rup.i18n.app.mantenimientos.organismo,
				$.rup.i18n.app.mantenimientos.descripcion,
				$.rup.i18n.app.mantenimientos.calle,
				$.rup.i18n.app.mantenimientos.codPostal,
				$.rup.i18n.app.mantenimientos.municipio,
				$.rup.i18n.app.mantenimientos.territorio,
				$.rup.i18n.app.mantenimientos.telefono,
				$.rup.i18n.app.mantenimientos.fax,
				$.rup.i18n.app.mantenimientos.email,
				$.rup.i18n.app.mantenimientos.registroManual
			],
			colModel: [
				{ 	name: "id049", 
				 	label: "id049",
					align: "center", 
					width: 88, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "desc" + $.rup_utils.capitalizedLang() + "048", 
				 	label: "desc" + $.rup_utils.capitalizedLang() + "048",
					align: "", 
					width: 200, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "desc" + $.rup_utils.capitalizedLang() + "049", 
				 	label: "desc" + $.rup_utils.capitalizedLang() + "049",
					align: "", 
					width: 180, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "calle049", 
				 	label: "calle049",
					align: "", 
					width: 150, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "codPostal049", 
				 	label: "codPostal049",
					align: "center", 
					width: 55, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "munDo", 
				 	label: "munDo",
					align: "", 
					width: 150, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "territorioDo", 
				 	label: "territorioDo",
					align: "", 
					width: 158, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "telefono049", 
				 	label: "telefono049",
					align: "", 
					width: 100, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "fax049", 
					label: "fax049",
					align: "", 
					width: 100, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{	name: "email049",
					label: "email049",
					align: "",
					width: 120,
					editable: true,
					fixed: false,
					hidden: false,
					resizable: true,
					sortable: true
				},
				{ 	name: "regManual049", 
					label: "regManual049",
					align: "center",
					edittype: "checkbox",
					formatter: "checkbox",
					editoptions: {
						value: "1:0"
					},
					width: 108, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				}
	        ],
	
	        model:"Ab03b49v00",
	        usePlugins:[
				"formEdit",
	        	"feedback",
				"toolbar",
	        	"contextMenu",
	        	"fluid",
	        	"filter",
	         	],
			primaryKey: "id049",
			sortname: "id049",
			sortorder: "asc",
			loadOnStartUp: true,
			feedback:{
				config:{
					block: false
				}
			},
	        formEdit:{
	        	detailForm: "#MantDelegaciones_detail_div",
				fillDataMethod: "clientSide",
	         	validate:{ 
	    			rules:{
	    				"id048Org": {
	    					required: true
	    				},
	    				"descEs049":{
							required: true
	    				},
	    				"descEu049":{
							required: true
	    				},
	    				"calle049":{
							required: true
	    				},
	    				"numero049":{
							required: true
	    				},
	    				"codPostal049":{
							required: true,
							digits: true
	    				},
	    				"territorio049":{
							required: true
	    				},
	    				"municipio049":{
							required: true
	    				},
	    				"telefono049":{
							required: true,
							digits: true
	    				},
	    				"fax049":{
							required: true,
							digits: true
	    				},
	    				"email049": {
	    					email: true
	    				}    
	    			}
	    		},
	    		addEditOptions:{
	    			reloadAfterSubmit:true
	    		}
	        },
	        toolbar: {
	        	showOperations: {
	        		'add': false,
	        		'clone': false,
	        		'delete': false
	        	}
	        },
	        contextMenu: {
	        	items: {
	        		'add': { disabled: true },
	        		'clone': { disabled: true },
	        		'delete': { disabled: true }
	        	}
	        }
		});
	}else{
		$("#MantDelegaciones").rup_table({
			url: "../ab03b49v00",
			colNames: [
				$.rup.i18n.app.mantenimientos.id,
				$.rup.i18n.app.mantenimientos.organismo,
				$.rup.i18n.app.mantenimientos.descripcion,
				$.rup.i18n.app.mantenimientos.calle,
				$.rup.i18n.app.mantenimientos.codPostal,
				$.rup.i18n.app.mantenimientos.municipio,
				$.rup.i18n.app.mantenimientos.territorio,
				$.rup.i18n.app.mantenimientos.telefono,
				$.rup.i18n.app.mantenimientos.fax,
				$.rup.i18n.app.mantenimientos.email,
				$.rup.i18n.app.mantenimientos.registroManual
			],
			colModel: [
				{ 	name: "id049", 
				 	label: "id049",
					align: "center", 
					width: 88, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "desc" + $.rup_utils.capitalizedLang() + "048", 
				 	label: "desc" + $.rup_utils.capitalizedLang() + "048",
					align: "", 
					width: 200, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "desc" + $.rup_utils.capitalizedLang() + "049", 
				 	label: "desc" + $.rup_utils.capitalizedLang() + "049",
					align: "", 
					width: 180, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "calle049", 
				 	label: "calle049",
					align: "", 
					width: 150, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "codPostal049", 
				 	label: "codPostal049",
					align: "center", 
					width: 55, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "munDo", 
				 	label: "munDo",
					align: "", 
					width: 150, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "territorioDo", 
				 	label: "territorioDo",
					align: "", 
					width: 158, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "telefono049", 
				 	label: "telefono049",
					align: "", 
					width: 100, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{ 	name: "fax049", 
					label: "fax049",
					align: "", 
					width: 100, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				},
				{	name: "email049",
					label: "email049",
					align: "",
					width: 120,
					editable: true,
					fixed: false,
					hidden: false,
					resizable: true,
					sortable: true
				},
				{ 	name: "regManual049", 
					label: "regManual049",
					align: "center",
					edittype: "checkbox",
					formatter: "checkbox",
					editoptions: {
						value: "1:0"
					},
					width: 108, 
					editable: true, 
					fixed: false, 
					hidden: false, 
					resizable: true, 
					sortable: true
				}
	        ],
	
	        model:"Ab03b49v00",
	        usePlugins:[
	        	"feedback",
	        	"fluid",
	        	"filter",
	         	],
			primaryKey: "id049",
			sortname: "id049",
			sortorder: "asc",
			loadOnStartUp: true,
			feedback:{
				config:{
					block: false
				}
			}
		});
	}
	
	$("#MantDelegaciones_filter_form").rup_validate({
		feedback:$("#MantDelegaciones_feedback"),
		liveCheckingErrors: false,
		rules:{
			"id049": { digits: true },
			"numero049":{digits: true},
			"piso049":{digits: true},
			"codPostal049":{digits: true},
			"telefono049":{digits: true},
			"fax049":{digits: true},
			"email049": {email: true}
		}
	});	
	
	
	$("#MantDelegaciones").on("rupTable_beforeFilter",
		function(){
			if(ab03bUtils.form.validate($("#MantDelegaciones_filter_form"))){	
				return true;
			} else {
				return false;
			}
		}
	);	

});

//Funcion limpiar
$('#MantDelegaciones_filter_cleanLink').click(function(event){
	resetFiltro($("#MantDelegaciones_filter_form"));
	
	//se borran los mensajes de errores que se muestran en el feedback
	$("#MantDelegaciones_filter_form").validate().submitted = {};
	//se cierra el feedback
	$("#MantDelegaciones_feedback").rup_feedback("close");
	
	$("#MantDelegaciones").rup_table('filter'); 
	
	// se quitan las imagenes que marcan el error
	$(".rup-maint_validateIcon", $("#MantDelegaciones_filter_form")).remove();
});