// ============================================================
//
// Copyright (c) 2000-2003 GE Smallworld. All Rights Reserved.
//
// =============================================================

var hcfRoot= this.top;

function showNormativa(pagina){
	// muestra la noramtiva del plan. Si el elemento seleccionado
	// tiene asociada normativa mostramos esta
	//
	
	if (pagina !='' && pagina != undefined) {
		var enlace = 'http://callejero.jerez.es/manuales/normasPGOU/normas/'+pagina;
	}
	else {
			var enlace = 'http://callejero.jerez.es/manuales/normasPGOU/NormasPGOU.htm';
	}
			
	var winName = 'Normativa Vigente PGOU';

      	var vent=window.open(enlace,'Normativa_PGOU','width=800,height=600,scrollbars = yes'); //,winName,'width=300,height=200');
       	vent.focus();
	//return 'ok';	
}


function showParcelaContent() {
	
	 //  llama al servicio Parcela para mostrar la ficha de la
	 // parcela seleccionada
	 
   	aService="parcela";

	var aStylesheet='content_parcela.xsl';
	
	aService = unescape(aService);
	aStylesheet = unescape(aStylesheet);

	//var cteParcela = "swrefVgeometryVdatasetZgisVcollectionZparcelaVfieldZrotuloVlocalZTrueVkeysZ"
	var st = hcfRoot.GLOBAL_CONTEXT.state;
	var xmldoc= new ActiveXObject("Microsoft.XMLDOM"); 
	xmldoc.async="false"; 
	xmldoc.validateOnParse = true; 
	xmldoc.loadXML(st);
	doc=xmldoc.documentElement;



	 var xmlNodo = xmldoc.selectSingleNode("//collection[@name='select_list']/records");
	 
	var urn_parcela='';
	//var urn_parcela='swrefVrecordVdatasetZgisVcollectionZparcelaVkeysZJ51A5654BQ56428Q33';
	for (var i=0;xmlNodo.childNodes.length;i++) {
		var n=xmlNodo.childNodes[i];
		if (n== null) {
			break;
			}
		var obj1=n.getAttribute('object');
		
		if ((obj1.indexOf('swrefVgeometryVdatasetZgisVcollectionZparcelaVfieldZcontornoVlocalZTrueVkeys') != -1) ||
			(obj1.indexOf('swrefVrecordVdatasetZgisVcollectionZparcelaVkeys') != -1))
	 	{
			urn_parcela = obj1;
			break;
		}
		
  	}
	if (urn_parcela!=''){
   		// setup the request
			var aRequest = '<request>' +
   						'<name>' + aService + '</name>' +
						'<parcela>'+urn_parcela+'</parcela>' +
						'<format>xml</format>' +
   						'</request>';
		hcfRoot.executeRequest(hcfRoot.hcf_content, aRequest, aStylesheet);

	//	BarraProgresoCapa = hcfRoot.xbGetElementById('progressIndicator', hcfRoot.hcf_content);
		
   	//	if (BarraProgresoCapa)
   	//	{
	//BarraProgresoCapaStyle = new hcfRoot.xbStyle(BarraProgresoCapa);
	//BarraProgresoCapaStyle.setVisibility('hidden');
	//
	setActiveFolder('parcela');
	}
	else
  		alert('No se ha seleccionado la parcela');
	
}
	
	
	
function catastro() {

	// abre un enlace a la oficina virtual de catastro con la parcela seleccionada
	var st = hcfRoot.GLOBAL_CONTEXT.state;
	var xmldoc= new ActiveXObject("Microsoft.XMLDOM"); 
	xmldoc.async="false"; 
	xmldoc.validateOnParse = true; 
	xmldoc.loadXML(st);
	doc=xmldoc.documentElement;
	var xmlNodo = xmldoc.selectSingleNode("//collection[@name='select_list']/records");
	var cteParcela = 'swrefVrecordVdatasetZgisVcollectionZparcelaVkeys'; //ZJ51A5665EQ60558Q02
	var urn_parcela=''
	for (var i=0;xmlNodo.childNodes.length;i++) {
		var n=xmlNodo.childNodes[i];
		if (n== null) {
			break;
			}
		var obj1=n.getAttribute('object');
		if (obj1.indexOf(cteParcela) != -1)
	 	{
			urn_parcela = obj1;
			var parcela = urn_parcela.slice(-15);
			var parc_cat = parcela.slice(7,12)+parcela.slice(-2)+'Q'+parcela.slice(0,6);
			break;
		}
  	}
  	
	if (urn_parcela!=''){
		var opciones="left=100,top=100,width=250,height=150", i= 0; 

		//var parcela = '6737702QA5663F'
		var winName = 'Ventana_Informativa';
		// enlace a la ficha de datos de la parcela:
		// https://ovc.catastro.meh.es/CYCBienInmueble/OVCListaBienes.aspx?del=53&muni=20&rc1=5449402&rc2=QA5654G&descProv=CADIZ&descMuni=JEREZ@DE@LA@FRONTERA
		//var enlace = 'https://ovc.catastro.meh.es/CYCBienInmueble/OVCListaBienes.aspx?del=53&muni=20&rc1='+
		//		parc_cat.slice(0,7)+ '&rc2='+parc_cat.slice(-7)+'&descProv=CADIZ&descMuni=JEREZ@DE@LA@FRONTERA';
				
		
		var enlace = 'https://www1.sedecatastro.gob.es/CYCBienInmueble/OVCListaBienes.aspx?RC1='+
				parc_cat.slice(0,7)+ '&rc2='+parc_cat.slice(-7)+'&RC3=&RC4=&esBice=&esBice1=&RCBice2=&DenoBice=&del=53&mun=20';
		
		var vent=window.open(enlace,"");
		vent.focus();
		// enlace a la ficha de datos de una unidad de vivienda:
		//https://ovc.catastro.meh.es/CYCBienInmueble/OVCConCiud.aspx?del=53&mun=20&UrbRus=U&RefC=5449402QA5654G0006TD&Apenom=
		//enlace al mapa de catastro con la localización de la parcela
		//enlace ='https://ovc.catastro.meh.es/Cartografia/mapa.aspx?del=53&mun=20&refcat='+parc_cat+'&final=';
		//https://ovc.catastro.meh.es/Cartografia/mapa.aspx?del=53&mun=20&refcat=6737702QA5663F&final=
		//vent=window.open(enlace,""); //,opciones); //,winName,'width=300,height=200');
      	//	vent.focus();
		
	}else
  		alert('No se ha seleccionado la parcela');
	
}


function activarTemas() {
	//activa el panel de temas
	// Remove listeners for active panel here
	
	hcfRoot.unregisterContentListener(hcfRoot.hcf_panel);
//hcfRoot.unregisterContentListener(hcfRoot.hcf_content);	
	var aRequest = '<request><name>map</name><format>xml</format></request><request><name>ace</name><command>aces</command><format>xml</format></request>';
	
hcfRoot.executeRequest(top.hcf_panel,aRequest,'panel_themes.xsl');
	
	
}
function activarAjustes() {
	var aRequest = '<request><name>map</name><format>xml</format></request><request><name>ace</name><command>aces</command><format>xml</format></request><request><name>coordinate_systems</name><format>xml</format></request>';
	top.executeRequest(top.hcf_panel,aRequest,'panel_ace.xsl');
}
function activarLocalizarCoord(){
	var aRequest = '<request><name>map</name><format>xml</format></request><request><name>ace</name><command>aces</command><format>xml</format></request><request><name>coordinate_systems</name><format>xml</format></request>';
	top.executeRequest(top.hcf_panel,aRequest,'panel_find_coordinate.xsl');
}
function activarOrto() {
	// activa o desactiva la ortofoto
	
	// si se pasa el parámetro temas, solo se activan estos 
	//var temasActual = hcfRoot.response.registry.swldy_themes.value;
	var st = hcfRoot.GLOBAL_CONTEXT.state;
	var xmldoc= new ActiveXObject("Microsoft.XMLDOM"); 
	xmldoc.async="false"; 
	xmldoc.validateOnParse = true; 
	xmldoc.loadXML(st);
	doc=xmldoc.documentElement;
	var xmlNodo = xmldoc.selectSingleNode("//collection[@name='registry']/records/record[@name='swldy_themes']");
	var temasActual=xmlNodo.getAttribute('value');
	
	//comprueba si esta activa la foto aerea
	if (temasActual != ''){
		if (temasActual.indexOf('2ortofoto') == -1)
			{
				aThemes = temasActual +",2ortofoto";
			}
		else {	
				aThemes = temasActual.replace('2ortofoto','');
			}
		}
	else{
		aThemes = 'Foto Aerea';
		}
		
		

		// Get default Map 
   	// create a new message
   	var aMessage = new hcfRoot.Message();

   	// set up target
  	 aMessage._target.frame = hcfRoot.hcf_content;
  	 
  	 aMessage.registerUrlParam('swldy_themes', aThemes);
  	 // setup request    
	   aRequest = aMessage.registerRequest();
   	aRequest.name = 'map';
   	aRequest.format = 'xml';

   	// setup transform
   	aTransform = aMessage.registerTransform();
   	aTransform.stylesheet = 'content_map.xsl';

   	// submit the request
   	hcfRoot.aSubmitHandler.doMessage(aMessage);
			
}

function activarSS(dia) {
	hcfRoot.diaSanto = dia;
	var aRequest = '<request><name>ace</name><command>aces</command><format>xml</format></request>';
	top.executeRequest(top.hcf_panel,aRequest,'panel_semana_santa.xsl');
		
}
function restoreTrail()
{
	aRequest =	'<request>'+
					'<name>trail</name>'+
					'<command>clear</command>'+
					'<format>xml</format>'+
					'<generate_map>true</generate_map>'+
			'</request>';

	//execute request
	hcfRoot.executeRequest(hcfRoot.hcf_panel, aRequest, 'panel_trail.xsl');
  
}

function writePanelTitle(sPanelTitle) {
//	----------------------------------------------------------------------
//	Function that writes the text for each panel title dynamically
//	Parameters:
//		sPanelTitle: text for the new panel title
//	----------------------------------------------------------------------	
   
 //  if (hcfRoot.navigator.family == 'nn4') {
 //     eval("document.pageTitleText.document.open()");
 //     eval("document.pageTitleText.document.write(sPanelTitle)");
 //     eval("document.pageTitleText.document.close()");
 //  }
 //  else {
 //     var aTitleText = hcfRoot.xbGetElementById('pageTitleText', hcfRoot.header.window);
 //     hcfRoot.xbSetInnerHTML(aTitleText, sPanelTitle); 
 //  }
         
}	


// ---------- Helper Functions to Open Panels -----------------
function openPanel(aPanel, aPanelTitle, aStylesheet, aStylesheetDirectory) {
//	----------------------------------------------------------------------
//	This method is deprecated and has been replaced by loadPage.
//	---------------------------------------------------------------------- 	
		
	loadPage(aPanel, aPanelTitle, aStylesheet, aStylesheetDirectory);		
}


function loadPage(aPanel, aPanelTitle,  aStylesheet, aStylesheetDirectory) {
//	----------------------------------------------------------------------
//	This is a helper method that load a simple HTML page into a panel.
//	A simple page is a page generated by the NULL service.
//
//	Parameters:
//		aPanel: panel - The panel that will host the loaded page.
//		aPanelTitle: string - The title that should be displayed above the panel.
//		aStylesheet: string - The name of the stylesheet to use.
//		aStylesheetDirectory: string - The name of the stylesheet directory to use.
//	---------------------------------------------------------------------- 	

	loadServicePage("null", hcfRoot.undefined, 
				    aPanel, aPanelTitle, 
				    aStylesheet, aStylesheetDirectory);
}


function loadServicePage(aService, parameters,
					     aPanel, aPanelTitle, 
				         aStylesheet, aStylesheetDirectory) {
//	----------------------------------------------------------------------
//	This is a helper method that loads an HTML page that represents
//  a service into a panel.
//
//	Parameters:
//		aService: string - The name of the service to call.
//		parameters: parameters object - Contains a list of parameter objects that specify
//				name-value pairs that should be sent to the server.
//		aPanel: panel - The panel that will host the loaded page.
//		aPanelTitle: string - The title that should be displayed above the panel.
//		aStylesheet: string - The name of the stylesheet to use.
//		aStylesheetDirectory: string - The name of the stylesheet directory to use.
//	---------------------------------------------------------------------- 	

	// create a new message 		
	
	var aMessage = new hcfRoot.Message();
	
    // set up target
    aMessage._target.frame = aPanel;
	
	

	// setup the request
	aRequest = aMessage.registerRequest();
	
	aRequest.name = aService;
	aRequest.format = 'xml';

	// always assume the format is xml
	aRequest.format = 'xml';

	// load the parameters
	if (parameters != hcfRoot.undefined) {
		aRequest._params.registerParameters(parameters);
	}
	
	// Create the transform
	aTransform = aMessage.registerTransform();
	aTransform.stylesheet = aStylesheet;
	
	if (aStylesheetDirectory != hcfRoot.undefined) {
		aTransform.stylesheetDirectoryName = aStylesheetDirectory;
	}
			
	// submit the message
	hcfRoot.aSubmitHandler.doMessage(aMessage);

	// write panel title
	if (aPanelTitle != hcfRoot.undefined) {
		writePanelTitle(aPanelTitle);
	}
	
	
}

function showDefaultMap() {
//	----------------------------------------------------------------------
//	Executes a request to hcf_content to load the default map view.  Note
// that this method ASSUMES that there is a view called default view,
// the default world has a universe of 0 and world of 0, and the default
// ace is named Default.
//
//	Parameters: none
//
// Results: none;
//	---------------------------------------------------------------------- 

	// create a new message
	var aMessage = new hcfRoot.Message();

    // set up target
    aMessage._target.frame = hcfRoot.hcf_content;

	// ASSUMPTION - use default view
	aMessage.registerUrlParam('swldy_view',hcfRoot.DEFAULT_VIEW);

	// Use default world as configured in globals.js
	aMessage.registerUrlParam('swldy_world', hcfRoot.DEFAULT_WORLD);
	
	// Use default ACE as configured in globals.js
	//aMessage.registerUrlParam('swldy_ace', hcfRoot.DEFAULT_ACE);
	
	// ASSUMPTION - use default ace
	// aMessage._url._params.registerParameter('swldy_ace', 'Default');

	// setup request    
	aRequest = aMessage.registerRequest();
	aRequest.name = 'map';
	aRequest.format = 'xml';

	// setup transform
	aTransform = aMessage.registerTransform();
	aTransform.stylesheet = 'content_map.xsl';

	// submit the request
	hcfRoot.aSubmitHandler.doMessage(aMessage);
}

function openAboutWindow() {
	// create a new message
	var aMessage = new hcfRoot.Message();

    // set up target
    aMessage._target.frame = hcfRoot.hcf_panel;
    
    // indicate that we want this window to be a pop-up 
    // with the following properties
   	aMessage._target.openWindow = true;
    aMessage._target.windowName = 'server_info';
   	aMessage._target.windowWidth = 300;
   	aMessage._target.windowHeight = 420;

	aRequest = aMessage.registerRequest();
	aRequest.name = 'server_info';
	aRequest.format = 'xml';
	aRequest.registerParam('feature_count','100');

	aTransform = aMessage.registerTransform();
	aTransform.stylesheet = 'panel_server_info.xsl';

	hcfRoot.aSubmitHandler.doMessage(aMessage);

}

function showMapContent(entorno) {
//	----------------------------------------------------------------------
//	Executes a request to load the map content type into hcf_content with
//	a basic map request.
//	Parameters:
//		None
//	---------------------------------------------------------------------- 




if (entorno == null){
	entorno=hcfRoot.DEFAULT_ACE;
	}
switch (entorno) {
	     case hcfRoot.DEFAULT_ACE:
			hcfRoot.registerParam('swldy_ace', hcfRoot.DEFAULT_ACE);
			hcfRoot.registerParam('swldy_drapp', hcfRoot.DEFAULT_DRAPP);
			hcfRoot.registerParam('swldy_themes', '0transporte,0alojamiento,2callejero,2portales,2RASTER,vacio');
			hcfRoot.registerContentListener('map', hcfRoot.updateThemesPanel, top.hcf_panel);
			break;
		case 'catastro_sias':
			hcfRoot.registerParam('swldy_ace', 'catastroweb_sias');
			hcfRoot.registerParam('swldy_drapp', 'Default');
			hcfRoot.registerParam('swldy_themes', '2administrativo,2catastro_urbano,2parcelario_aparente,2mobiliario,2raster,vacio');
		hcfRoot.registerContentListener('catastro', hcfRoot.updateThemesPanel, top.hcf_panel);
			break;
				break;
		case 'catastroweb_sias':
			hcfRoot.registerParam('swldy_ace', 'catastroweb_sias');
			hcfRoot.registerParam('swldy_drapp', 'Default');
			hcfRoot.registerParam('swldy_themes', '2administrativo,2catastro_urbano,2parcelario_aparente,2mobiliario,2raster,vacio');
		hcfRoot.registerContentListener('catastro', hcfRoot.updateThemesPanel, top.hcf_panel);
			break;
		case 'urbanismoweb_sias':
			hcfRoot.registerParam('swldy_ace', 'urbanismoweb_sias');
			hcfRoot.registerParam('swldy_drapp', 'urbanismo');
			hcfRoot.registerParam('swldy_themes', '0defecto,2cartografia,2raster,vacio');
hcfRoot.registerContentListener('urbanismo', hcfRoot.updateThemesPanel, top.hcf_panel);
			break;
		case 'turismo_sias':
			hcfRoot.registerParam('swldy_ace', 'turismo_sias');
			hcfRoot.registerParam('swldy_drapp', 'turismo');
	
			//hcfRoot.registerParam('swldy_themes', '0transporte,0visita_cultural,0alojamiento,2cartografia_urbana,2callejero,2portales,2RASTER,Estado');

			break;
	}
	
	//set up map request

	aRequest = '<request>'+
						'<name>map</name>'+
						'<format>xml</format>'+
				'</request>';
				
	var aStylesheet = 'content_map.xsl';				

	//now execute request
	hcfRoot.executeRequest(hcfRoot.hcf_content, aRequest, aStylesheet);
	
}

function showQueryContent() {
//	----------------------------------------------------------------------
//	Checks to see if a query was executed and if so then executes the last
//	query service type and loads the query content type into hcf_content
//	Parameters:
//		None
//	---------------------------------------------------------------------- 
	var last_query = hcfRoot.LAST_QUERY_SERVICE;
	if ((last_query == '') || (last_query == 'feature_info')) {
		alert(hcfRoot.NO_QUERY_ERROR_MESSAGE);
		return;
	}

	if (last_query == 'locator') {
		var aMessage = new hcfRoot.Message();

		// set up target
		aMessage._target.frame = hcfRoot.hcf_content;

		aRequest = aMessage.registerRequest();
		aRequest.name = last_query;
		aRequest.registerParam('swldy_write_dd','true');
		aRequest.format = 'xml';
		aRequest.registerParam('feature_count',hcfRoot.FEATURE_COUNT);
		aRequest.registerParam('swldy_write_xsd_namespace','xml');
		aTransform = aMessage.registerTransform();

		var aStylesheet;

		aRequest.registerParam('swldy_locator_id',hcfRoot.CURRENT_LOCATOR);
		var aStep = hcfRoot.LOCATOR_STEP;
		if ((aStep != null) && (aStep != '')) {
			 aRequest.registerParam('swldy_locator_step',aStep);
		}

		aUrn = hcfRoot.LOCATOR_URN;
		if ((aUrn != null) && (aUrn != '')) {
			aRequest.registerParam('swldy_object',aUrn);
		}

		var Refinements = hcfRoot.LOCATOR_REFINEMENT;
		if ((Refinements != null) && (Refinements != '')) {
			aRequest.registerParam('swldy_refinements',Refinements);
		}

		aRequest.registerParam('swldy_command','one_step');
		var feature_start = hcfRoot.LAST_FEATURE_START;
		if (feature_start != null) {
			aRequest.registerParam('feature_start',feature_start);
		}

		aTransform.stylesheet = 'content_locators.xsl';

		//now execute request
		hcfRoot.aSubmitHandler.doMessage(aMessage);
	}
	else
	{
		 //set up query content request
		var aRequest =  '<request>'+
				'<name>' + hcfRoot.LAST_QUERY_SERVICE + '</name>'+
				'<swldy_write_dd>true</swldy_write_dd>'+
				'<format>xml</format>'+
				'<feature_count>' + hcfRoot.FEATURE_COUNT + '</feature_count>'+
				'<swldy_write_additional_fields>false</swldy_write_additional_fields>'+
				'<swldy_write_xsd_namespace>xml</swldy_write_xsd_namespace>'+
				'</request>';
				
		var aStylesheet = 'content_query.xsl';

		//now execute request
		hcfRoot.executeRequest(hcfRoot.hcf_content, aRequest, aStylesheet);
	}
}


function showPlotContent() {
//	----------------------------------------------------------------------
//	Loads the plot panel with a plot info request. The plot panel is setup
//	to execute a request on hcf_content for a default plot preview. This is
//	done because it is impossible to show a plot preview without first calling
//	plot info to get the plot template information.
//	Parameters:
//		None
//	---------------------------------------------------------------------- 

	//set up plot request
	var aRequest =  '<request>'+
					'<name>plot</name>'+
					'<swldy_command>info</swldy_command>'+
					'<format>xml</format>'+
					'</request>';
		
	//set up ace request
	aRequest = aRequest +  
				'<request>'+
					'<name>ace</name>'+
					'<command>aces</command>'+
					'<format>xml</format>'+
				'</request>';
	
	//set up map request
	aRequest = aRequest +  
				'<request>'+
						'<name>map</name>'+
						'<format>xml</format>'+
				'</request>';
				
	var aStylesheet = 'panel_plot_wizard.xsl';	
	
	//now execute request
	hcfRoot.executeRequest(hcfRoot.hcf_panel, aRequest, aStylesheet);
	writePanelTitle(hcfRoot.PLOT_PREVIEW_TITLE);
}
function toggleXMLFlag( element ){
    var normal = "Output Raw XML: Off";
    var xml = "Output Raw XML: On";

    hcfRoot.ShowXML=!hcfRoot.ShowXML;
    hcfRoot.xbDEBUG.dump('Changed display raw XML value to: '+hcfRoot.ShowXML);
    
    var a = (element.document.getElementById("XMLOUTPUT"));
    var textNode = a.childNodes[0];
    
    //hcfRoot.xbDEBUG.dump("txt: "+textNode.data)
    
    if( hcfRoot.ShowXML == true ){
        textNode.data = xml;
    }
    else{
        textNode.data = normal;
    }
}

