/**
 *Declaracion de la ventana Localizar por Coordenadas
 *@author Romanuel Ramon Antunez
**/

/*****************************************************************************/
var ptoG = new Point();
var flag = false;
var flagz = false;
var afterTool;
var vscale;

//var vzoom = [[20],[50],[75],[100],[150],[200]];
/*var storezoom = new Ext.data.SimpleStore({
			fields: [{name: 'state', type: 'int'}],
			data: [[20],[50],[75],[100],[150],[200]],
			autoLoad: true
		});*/	

//storezoom.loadData(vzoom);

var comboZoom = new Ext.form.ComboBox({
	fieldLabel: 'variaci&oacute;n relativa del zoom',
	id: 'combozoom',
	value: 20,
	width : 60,
	name: 'czoom',
	mode:'remote',
	valueField:'id_porciento',
	displayField:'porciento',
	triggerAction: 'all',
	store : new Ext.data.Store({
		autoLoad:true,
		baseParams:{
			accion:'cargarPorcientoZoom',
			opcion:1,
		},
	      	proxy: new Ext.data.HttpProxy({ url:"geoweb/configurar/sConfigurar.php" }),
	     	reader: new Ext.data.JsonReader({root: 'filas', totalProperty:'cant_filas'},[{name:'id_porciento'},{name:'porciento'}])
	}),
	disabled : true
})

var comboZoom1 = new Ext.form.ComboBox({
	fieldLabel: 'variaci&oacute;n relativa del zoom',
	id: 'combozoom1',
	value: 20,
	width : 60,
	name: 'czoom1',
	mode:'remote',
	valueField:'id_porciento',
	displayField:'porciento',
	triggerAction: 'all',
	store : new Ext.data.Store({
		autoLoad:true,
		baseParams:{
			accion:'cargarPorcientoZoom',
			opcion:1,
		},
	      	proxy: new Ext.data.HttpProxy({ url:"geoweb/configurar/sConfigurar.php" }),
	     	reader: new Ext.data.JsonReader({root: 'filas', totalProperty:'cant_filas'},[{name:'id_porciento'},{name:'porciento'}])
	}),
	disabled : true
})

var valueX = new Ext.form.NumberField({
	id: 'valueXG', 
	allowBlank: false,
	blankText: 'Valor Requerido',
	hideLabel:true,
	width:90,
	decimalPrecision: 5,
	//allowBlank: false	
})

var valueY = new Ext.form.NumberField({
	id: 'valueYG', 
	allowBlank: false,
	blankText: 'Valor Requerido',
	hideLabel:true, 
	decimalPrecision: 5,
	width:90	
})

var valueX1 = new Ext.form.NumberField({
	id: 'valueXM',
	allowBlank: false,
	blankText: 'Valor Requerido',
	hidden: true, 
	hideLabel:true,
	decimalPrecision: 5,
	width:90	
})

var valueY1 = new Ext.form.NumberField({
	id: 'valueYM',
	allowBlank: false,
	blankText: 'Valor Requerido', 
	decimalPrecision: 5,
	hidden: true,
	hideLabel:true,
	width:90
})

var valueX2 = new Ext.form.NumberField({
	id: 'valueXS', 
	allowBlank: false,
	blankText: 'Valor Requerido',
	hidden: true,
	hideLabel:true,
	decimalPrecision: 5,
	width:90  
})

var valueY2 = new Ext.form.NumberField({
	id: 'valueYS',
	allowBlank: false,
	blankText: 'Valor Requerido', 
	hidden: true,
	hideLabel:true,
	decimalPrecision: 5,
	width:90 
})

var valueXUTM = new Ext.form.NumberField({ 
			id: 'XUTM', 
			hideLabel:true,
			allowBlank: false,
			blankText: 'Valor Requerido', 
			decimalPrecision: 5,
			width:90
		})

var valueYUTM = new Ext.form.NumberField({ 
			id: 'YUTM', 
			hideLabel:true,
			allowBlank: false,
			blankText: 'Valor Requerido',
			decimalPrecision: 5, 
			width:90
		})

/*var valueH = new Ext.form.TextField({ 
			id: 'HUTM', 
			hideLabel:true, 
			width:90, 
	     })*/


fLocalizacionCG = new Ext.form.FormPanel({ 
	labelAlign: 'left', 
	frame:true, 
	items:[{ 
		xtype: 'fieldset', 
		width: 250, 
		autoHeight:true, 
		title: 'Coordenadas',
		items: [{ 
			layout: 'column', 
			items: [{
				style:'margin:0 0 0 5;', 
				columnWidth: '.5', 
				html: 'Longitud' 
			},{ 
				style:'margin:0 0 0 5;', 
				columnWidth: '.5', 
				html: 'Latitud' 
			},{ 
			columnWidth: '.4', 
			layout: 'form', 
			items: [valueX] 
			},{ 
				style:'margin:0 0 0 5;', 
				columnWidth: '.1', 
				html: '&deg' 
			},{ 
				columnWidth: '.4', 
				layout: 'form', 
				items: [valueY] 
			},{
				style:'margin:0 0 0 5;', 
				columnWidth: '.1', 
				html: '&deg' 
			},{ 
			columnWidth: '.4', 
			layout: 'form', 
			items: [valueX1] 
			},{ 
				//hidden: true,
				style:'margin:0 0 0 5;', 
				columnWidth: '.1', 
				html: '<div id="labelMX"> &quot </div>' 
			},{ 
				columnWidth: '.4', 
				layout: 'form', 
				items: [valueY1] 
			},{
				//hidden: true,
				style:'margin:0 0 0 5;', 
				columnWidth: '.1', 
				html: '<div id="labelMY"> &quot </div>' 
			},{ 
			columnWidth: '.4', 
			layout: 'form', 
			items: [valueX2] 
			},{ 
				//hidden: true,
				style:'margin:0 0 0 5;', 
				columnWidth: '.1', 
				html: '<div id="labelSX"> &#34&#34 </div>' 
			},{ 
				columnWidth: '.4', 
				layout: 'form', 
				items: [valueY2] 
			},{ 
				//hidden: true,
				style:'margin:0 0 0 5;', 
				columnWidth: '.1', 
				html: '<div id="labelSY"> &#34&#34 </div>'  
			}] 
		}] 
	},{
	    xtype: 'fieldset', 
		width: 250, 
		autoHeight:true, 
		title: 'Modificar nivel de zoom',	
		items :[{
			layout: 'column', 
			items:[{
				columnWidth: '.6',
				items:[{
					xtype: 'checkbox',
					id: 'modzoom1',
					hideLabel :true,
					boxLabel: 'Modificar vista actual',
					//checked: true,
					listeners: {  check: function(cb, checked) {
						if (checked == true){
							comboZoom.enable();
						}	
						else{
							comboZoom.disable();	
						}
					}}
				}]
			},{
				columnWidth: '.4',
				items:[comboZoom]
			}]	
		}]			  
	}] 
});


fLocalizacionCUTM = new Ext.form.FormPanel({ 
	labelAlign: 'left', 
	frame:true, 
	items:[{ 
		xtype: 'fieldset', 
		width: 250, 
		autoHeight:true, 
		title: 'Coordenadas',
		items: [{ 
			layout: 'column', 
			items: [{
				style:'margin:0 0 0 5;', 
				columnWidth: '.1', 
				html: 'X' 
			},{ 
			columnWidth: '.4', 
			layout: 'form', 
			items: [valueXUTM,/*{
				style:'margin:5 0 0 5;',
				columnWidth: '1', 
				html: 'Huso UTM',
				id: 'hutm',
						
			},{ 
			columnWidth: '.4', 
			layout: 'form', 
			items: [valueH] 
			}*/] 
			},{ 
				style:'margin:0 0 0 5;', 
				columnWidth: '.1', 
				html: 'Y' 
			},{ 
				columnWidth: '.4', 
				layout: 'form', 
				items: [valueYUTM] 
			}] 
		}] 
	},{
	   	xtype: 'fieldset', 
		width: 250, 
		autoHeight:true, 
		title: 'Modificar nivel de zoom',
		items :[{
			layout: 'column', 
			items:[{
				columnWidth: '.6',
				items:[{
					xtype: 'checkbox',
					id: 'modzoom',
					hideLabel :true,
					boxLabel: 'Modificar vista actual',
					listeners: {  check: function(cb, checked) {
						if (checked == true){
							comboZoom1.enable();
						}	
						else{
							comboZoom1.disable();	
						}
					}}
				}]
			},{
				columnWidth: '.4',
				items:[comboZoom1]
			}]	
		}]			  
	}] 
});


var LocalizacionPCG;
var LocalizacionPCUTM;

function fLocalizadorPC(){
	afterTool = document.getElementById('tool').value;
        
	if(mapconfig.coord == 'degree'){
		if(!LocalizacionPCG){
			LocalizacionPCG = new Ext.Window({
					title		: 'Localizaci&oacute;n por Coordenadas',
					renderTo    : document.body,
					layout      : 'fit',
					width       : 280,
					height      : 300,
					closeAction :'hide',
							resizable   :false,
					plain       : true,
							items		: fLocalizacionCG,
							buttons		: [{
					    text     : 'Aceptar',
								handler  : function(){
									LocalizarG();
					    }					
					},{
					    text     : 'Cancelar',
					    handler  : function(){
						LocalizacionPCG.hide();
					    }
					}]

			});
		}
		mainmap.resetMapEventHandlers();	
        	LocalizacionPCG.show();	
        	valueX.reset();
			valueX1.reset();
			valueX2.reset();
			valueY.reset();
			valueY1.reset();
			valueY2.reset();
		switch(mapconfig.fCoord){
			case 0:
				valueX1.setVisible(false);
				valueX1.setValue(0);
				document.getElementById('labelMX').style.display = 'none';
				valueY1.setVisible(false);
				valueY1.setValue(0);
				document.getElementById('labelMY').style.display = 'none';				
				valueX2.setVisible(false);
				valueX2.setValue(0);
				document.getElementById('labelSX').style.display = 'none';
				valueY2.setVisible(false);
				valueY2.setValue(0);
				document.getElementById('labelSY').style.display = 'none';
				break;
			case 1:
				valueX1.setVisible(true);
				document.getElementById('labelMX').style.display = 'block';
				valueY1.setVisible(true);
				document.getElementById('labelMY').style.display = 'block';
				valueX2.setVisible(false);
				valueX2.setValue(0);
				document.getElementById('labelSX').style.display = 'none';
				valueY2.setVisible(false);
				valueY2.setValue(0);
				document.getElementById('labelSY').style.display = 'none';
				break;
			case 2:
				valueX1.setVisible(true);
				document.getElementById('labelMX').style.display = 'block';
				valueY1.setVisible(true);
				document.getElementById('labelMY').style.display = 'block';
				valueX2.setVisible(true);
				document.getElementById('labelSX').style.display = 'block';;
				valueY2.setVisible(true);
				document.getElementById('labelSY').style.display = 'block';
				break;
		}
		
	}else{
		if(!LocalizacionPCUTM){
			LocalizacionPCUTM = new Ext.Window({
					title		: 'Localizaci&oacute;n por Coordenadas',
					renderTo    : document.body,
					layout      : 'fit',
					width       : 280,
					height      : 260,
					closeAction :'hide',
							resizable   :false,
					plain       : true,
							items		: fLocalizacionCUTM,
							buttons		: [{
					    text     : 'Aceptar',
								handler  : function(){
									LocalizarUTM();
					    }					
					},{
					    text     : 'Cancelar',
					    handler  : function(){
						LocalizacionPCUTM.hide();
					    }
					}]

			});
		}
		mainmap.resetMapEventHandlers();	
        	LocalizacionPCUTM.show();
		valueXUTM.reset();
		valueYUTM.reset();
		//valueH.setValue("");		
	}	

       	
	function LocalizarG(){
	var a = false;
		if(fLocalizacionCG.form.isValid())
		{
		a = true;
		}
		if(a)
		{
			valx = new Array();
			valy = new Array();

			switch(mapconfig.fCoord){
				case 0:
					ptoG.x = valueX.getValue(); 
					ptoG.y = valueY.getValue();
					break;
				case 1:
					valx[0] = valueX.getValue();
					valy[0] = valueY.getValue();
					valx[1] = valueX1.getValue();
					valy[1] = valueY1.getValue();
					ptoG.x = GMaGD(valx);
					ptoG.y = GMaGD(valy);
					break;
				case 2:
					valx[0] = valueX.getValue();
					valy[0] = valueY.getValue();
					valx[1] = valueX1.getValue();
					valy[1] = valueY1.getValue();
					valx[2] = valueX2.getValue();
					valy[2] = valueY2.getValue();
					ptoG.x = GMSaGD(valx);
					ptoG.y = GMSaGD(valy);
					break;
			}

			if(!comboZoom.disabled){
				tnzoom = comboZoom.getRawValue();
				nzoom = 100/tnzoom;
				document.getElementById("factorNZoom").value = nzoom;
				document.getElementById("flagNZoom").value = 1;
				LocalizacionPCG.hide();
				RecentrarPuntoNZoom(ptoG.x,ptoG.y);
			}else{
				//document.getElementById("flagNZoom").value = false;
				LocalizacionPCG.hide();
				RecentrarPunto(ptoG.x,ptoG.y);	
			}
		}					
	}

	function LocalizarUTM(){
		if(fLocalizacionCUTM.form.isValid()){
			ptoG.x = valueXUTM.getValue();
			ptoG.y = valueYUTM.getValue();

			if(!comboZoom1.disabled){
				tnzoom = comboZoom.getRawValue();
				nzoom = 100/tnzoom;
				document.getElementById("factorNZoom").value = nzoom;
				document.getElementById("flagNZoom").value = 1;
				LocalizacionPCUTM.hide();
				RecentrarPuntoNZoom(ptoG.x,ptoG.y);
			}else{
				//document.getElementById("flagNZoom").value = false;
				LocalizacionPCUTM.hide();
				RecentrarPunto(ptoG.x,ptoG.y);	
			}					
		}
	}	
};

/*function ValidarFormat(val){
	if(IsNumeric(val)){
		return true
	}else{
		return false;
	}
}*/

/*function ErrorFormat(){
	Ext.MessageBox.show({
		 title: 'Error Localizaci&oacute;n',
		 msg: 'Formato incorrecto para las coordenadas',
		 buttons: Ext.MessageBox.OK,
		 icon: Ext.MessageBox.ERROR
	});
}*/

function ErrorRango(){
	Ext.MessageBox.show({
		 title: 'Error Localizaci&oacute;n',
		 msg: 'Coordenas imposible de alcanzar',
		 buttons: Ext.MessageBox.OK,
		 icon: Ext.MessageBox.ERROR
	});
}
			
/*function CambiarZoom(){
	document.getElementById("recenter_scale").value = parseInt(vscale);
	setCustomScale("select");			
}*/
function RecentrarPuntoNZoom(x,y){
    flag = true;
    myform.selection_coords.value = x + "," + y+ ";" + x + "," + y;
    myform.selection_type.value = "rectangle";
    storeFeatures();
    document.getElementById("tool").value = 	"zoomin";
    CartoWeb.trigger("Location.Zoom", "doSubmit()");
}

function RecentrarPunto(x,y){
			flag = true;    			
			myform.selection_coords.value = x + "," + y;
    			myform.selection_type.value = "point";
    			storeFeatures();
    			document.getElementById("tool").value = "pan";
    			CartoWeb.trigger('Location.Pan', "doSubmit()",{source: 'map'});			
}	

function UbicarPunto(x,y){
	if((x >= mainmap.extent.xmin) && (x <= mainmap.extent.xmax) && (y >= mainmap.extent.ymin) && (y <= mainmap.extent.ymax)){
		if(document){
			adisplay = document.getElementById('map_eventPad')._display;
			ex = geo2Pix(x, mainmap.extent.xmin, mainmap.extent.xmax, 0, adisplay._width) - 20;
			ey = geo2Pix(y, mainmap.extent.ymax, mainmap.extent.ymin,0, adisplay._height) - 32;
					
			 if(!document.getElementById("tachuela"))
			{
		   	      var tachuela = document.createElement("div");
		   	      tachuela.setAttribute("id", "tachuela");
		   	      tachuela.setAttribute("style","position: absolute; z-index: 2");
		   	      var imgTachuela =  document.createElement("img");
		   	      imgTachuela.setAttribute("src","images/azul/icon-azul/marker-blue.png");
		   	      tachuela.appendChild(imgTachuela);
		   	      document.getElementById("map_rootLayer").appendChild(tachuela);
		    	      
			  }
			  else
				xShow("tachuela");

			  xMoveTo("tachuela", ex ,ey);
		}
	}else{
		ErrorRango();
	}	
		
};





