﻿/*
 * Clase para gestionar la informacion geografica en formularios.
 */
function WBEGeoInfo()
{
	this.ajax = new WBE_AjaxClass();// Clase Ajax para lanzar los eventos
	this.default_country = "ES"; 	// Aun no se usa

  // Rellena las provincias en un combo, y recibe el nombre del campo de donde extrae el valor a seleccionar
	this.changeGEOElement = function  (oCurrentElement, sParentValue, sSelectedValue) 
	{	
		var oTargetElement = this.GetTargetElem(oCurrentElement);
		if (oTargetElement == null) return;	// No hay destino

		//Tipo de target
		var sTargetDataType = oTargetElement.getAttribute('data');
		var sChildSelected;
		switch (sTargetDataType) {
			case "country":
					// Recupera los paises y los rellena en el target
					sChildSelected = this.fillCountries(oTargetElement, sSelectedValue)
				break;
			case "province":
					// Recupera los paises y los rellena en el target
					sChildSelected = this.fillProvinces(oTargetElement, sParentValue, sSelectedValue)
				break;
			case "city":
					sChildSelected = this.fillCities(oTargetElement, sParentValue, sSelectedValue)
				break;
		}

		// Recursividad, procesa el hijo para resolver dependencias.
		if (sTargetDataType!=null) {
			this.changeGEOElement(oTargetElement, sChildSelected, null); 
		}
	}

	this.fillCountries = function(oElem, sSelected) {
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		this.clearSelect(oElem);
		var oXmlData = oAjax.throwEventXML("geo_get_countries");
		if (oXmlData.tagName == 'countries') {
			return this.fillDataIntoSelect(oElem, oXmlData, sSelected);
		}
	}

	this.fillProvinces = function(oElem, sCountry, sSelected) {
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		this.clearSelect(oElem);
		var s_country = (sCountry==null || sCountry== '') ? this.default_country = 'ES' : sCountry;
		oAjax.addPostParameter('_geo_country_id', s_country);
		var oXmlData = oAjax.throwEventXML("geo_get_provinces");
		if (oXmlData.tagName == 'provinces') {
			return this.fillDataIntoSelect(oElem, oXmlData, sSelected);
		}
	}

	this.fillCities = function(oElem, sProvinceID, sSelected) {
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		this.clearSelect(oElem);
		if (sProvinceID==null) return null;	// Es obligatoria la provincia
		oAjax.addPostParameter('_geo_province_id', sProvinceID);
		var oXmlData = oAjax.throwEventXML("geo_get_cities");
		if (oXmlData.tagName == 'cities') {
			return this.fillDataIntoSelect(oElem, oXmlData, sSelected);
		}
	}
	
	/* ------------------------------------------------------------------------------------------------------------ */
	/* ----------------------------------------- Funciones internas ----------------------------------------- */
	/* ------------------------------------------------------------------------------------------------------------ */
	// Recupera el objeto target que modificará
	this.GetTargetElem = function(oCurrentElem) {
		var sTarget = oCurrentElem.getAttribute('target');
		if (sTarget!=null && sTarget!='') {
			var oElem = document.getElementById(sTarget);
			return oElem;
		}
		return null;
	}

	// Rellena el combo  
	this.clearSelect = function (oComboElem) {
		// Limpia el combo
		if (!oComboElem.options) return;
		for(var aux=oComboElem.options.length-1; aux>=0; aux--) oComboElem.options[aux] = null;
	}

	// Rellena el combo  
	this.fillDataIntoSelect = function (oComboElem, data, val_selected)
	{
		var value_selected = val_selected.toString();
		var opt;
		var current_selected;
		// Rellena el combo
		for(var i=0;i<data.childNodes.length;i++) {
			var oNode = data.childNodes[i]; // oNode puede ser tanto una provincia como una poblacion / ciudad ya que la estructura del xml es la misma
			var sDesc = oNode.childNodes[1].firstChild.data;
			var sValue = oNode.childNodes[0].firstChild.data;
			var sDescription = sDesc.toString();
			opt = new Option(sDesc, sValue);
			if (value_selected!=null && (value_selected==sValue || sDescription.toLowerCase() == value_selected.toLowerCase())) {
				current_selected = sValue;
				opt.selected = true;
			}
			oComboElem.options[oComboElem.length] = opt;
		}
		if (current_selected==null && oComboElem.options.length>0) {
			current_selected = oComboElem.options[0].value;
		}
		return current_selected;
	}
	
} // fin clase
