function showPaises() {
	limpiarHtml();
	var desplegable_regiones = document.getElementById("selectRegiones");
	var region = desplegable_regiones.options[desplegable_regiones.selectedIndex].value;
	var desplegable_paises = document.getElementById(region);
	desplegable_paises.style.display = "block";
	desplegable_paises.selectedIndex = 0;
	for(var i=1; i<11; i++) {
		if (region != "paises"+i) {
			document.getElementById("paises"+i).style.display = "none";
		}
	}
}
function buscarTarifa(id) {
	limpiarHtml();
	var seleccion_actual = document.getElementById("paises"+id);
	var pais_value = seleccion_actual.options[seleccion_actual.selectedIndex].value;
	var pais_nombre = seleccion_actual.options[seleccion_actual.selectedIndex].text;	
	if ( pais_value != "0" ) {
		var input_pais_ajax = document.getElementById("input_countries");
		input_pais_ajax.value = pais_nombre;
		doAjaxRequest(pais_nombre);
	}
}
function limpiarHtml()
{
	document.getElementById("input_countries").value = "";
	document.getElementById("tar_int_voz").innerHTML = "";
	document.getElementById("tar_int_sms").innerHTML = "";
	document.getElementById("tar_int_est").innerHTML = "";
	document.getElementById("tar_int_imp").innerHTML = "";
	document.getElementById("tar_int_voz_movil").innerHTML = "";
	document.getElementById("tar_int_sms_movil").innerHTML = "";
	document.getElementById("tar_int_est_movil").innerHTML = "";
	document.getElementById("tar_int_imp_movil").innerHTML = "";
}
function actualizarTarifaFromXML(tarifas_string) {
			// Las tarifas están en  infoItem.value separadas por espacios:
			var tarifas_array=tarifas_string.split(" ");
			var tar_int_voz = document.getElementById("tar_int_voz");
			var tar_int_sms = document.getElementById("tar_int_sms");
			var tar_int_est = document.getElementById("tar_int_est");
			var tar_int_imp = document.getElementById("tar_int_imp");

			tar_int_est.innerHTML = "Establecimiento: " + (parseFloat(tarifas_array[0])/100).toFixed(2) + "&euro; ";
			tar_int_est.innerHTML = tar_int_est.innerHTML.replace(".", ",");
			tar_int_voz.innerHTML = "Voz: " + (parseFloat(tarifas_array[1])/100).toFixed(2) + "&euro; ";
			tar_int_voz.innerHTML = tar_int_voz.innerHTML.replace(".", ",");		
			tar_int_sms.innerHTML = "SMS: " + (parseFloat(25)/100).toFixed(2) + "&euro; ";  // Fijo 0.45€
			tar_int_sms.innerHTML = tar_int_sms.innerHTML.replace(".", ",");
			tar_int_imp.innerHTML = "* IVA no incluido";
			
			// Tarifas móvil:
			var tar_int_voz_movil = document.getElementById("tar_int_voz_movil");
			var tar_int_sms_movil = document.getElementById("tar_int_sms_movil");
			var tar_int_est_movil = document.getElementById("tar_int_est_movil");
			var tar_int_imp_movil = document.getElementById("tar_int_imp_movil");			
			
			if ( tarifas_array[3] != null ) {
				tar_int_est_movil.innerHTML = "Establecimiento: " + (parseFloat(tarifas_array[3])/100).toFixed(2) + "&euro; ";
				tar_int_est_movil.innerHTML = tar_int_est_movil.innerHTML.replace(".", ",");
				tar_int_voz_movil.innerHTML = "Voz: " + (parseFloat(tarifas_array[4])/100).toFixed(2) + "&euro; ";
				tar_int_voz_movil.innerHTML = tar_int_voz_movil.innerHTML.replace(".", ",");		
				tar_int_sms_movil.innerHTML = "SMS: " + (parseFloat(25)/100).toFixed(2) + "&euro; ";  // Fijo 0.45€
				tar_int_sms_movil.innerHTML = tar_int_sms_movil.innerHTML.replace(".", ",");			
				tar_int_imp_movil.innerHTML = "* IVA no incluido";
			} else {
				tar_int_est_movil.innerHTML = "";
				tar_int_est_movil.innerHTML = "";
				tar_int_voz_movil.innerHTML = "";
				tar_int_voz_movil.innerHTML = "";
				tar_int_sms_movil.innerHTML = "";
				tar_int_sms_movil.innerHTML = "";
				tar_int_imp_movil.innerHTML = "";
			}
}	
		
function replaceLatinChar(texto) 
{
	// Acentos y ñ, mayusculas y minusculas
    texto = texto.replace(/%c3%a1/gi,'Á');
    texto = texto.replace(/%C3%81/gi,'Á');
    texto = texto.replace(/%c3%a9/gi,'É');
    texto = texto.replace(/%C3%89/gi,'É');
    texto = texto.replace(/%c3%ad/gi,'Í');
    texto = texto.replace(/%C3%8D/gi,'Í');
    texto = texto.replace(/%c3%b3/gi,'Ó');
    texto = texto.replace(/%C3%93/gi,'Ó');
    texto = texto.replace(/%C3%BA/gi,'Ú');	
    texto = texto.replace(/%C3%9A/gi,'Ú');
	texto = texto.replace(/%C3%A4/gi,'Ä');
	texto = texto.replace(/%C3%84/gi,'Ä');
	texto = texto.replace(/%C3%AB/gi,'ë');
	texto = texto.replace(/%C3%8B/gi,'Ë');
	texto = texto.replace(/%C3%AF/gi,'Ï');
	texto = texto.replace(/%C3%8F/gi,'Ï');
	texto = texto.replace(/%C3%B6/gi,'Ö');
	texto = texto.replace(/%C3%96/gi,'Ö');
	texto = texto.replace(/%C3%BC/gi,'Ü');
	texto = texto.replace(/%C3%9C/gi,'Ü');
	texto = texto.replace(/%C3%B1/gi,'Ñ');
	texto = texto.replace(/%C3%91/gi,'Ñ');
	// Otros caracteres "raros"
	texto = texto.replace(/%C3%80/gi,'À');
	texto = texto.replace(/%C3%82/gi,'Â');
	texto = texto.replace(/%C3%83/gi,'Ã');
	texto = texto.replace(/%C3%84/gi,'Ä');
	texto = texto.replace(/%C3%85/gi,'Å');
	texto = texto.replace(/%C3%86/gi,'Æ');
	texto = texto.replace(/%C3%88/gi,'È');
	texto = texto.replace(/%C3%8A/gi,'Ê');
	texto = texto.replace(/%C3%8B/gi,'Ë');
	texto = texto.replace(/%C3%8C/gi,'Ì');
	texto = texto.replace(/%C3%8E/gi,'Î');
	texto = texto.replace(/%C3%8F/gi,'Ï');
	texto = texto.replace(/%C3%92/gi,'Ò');
	texto = texto.replace(/%C3%94/gi,'Ô');
	texto = texto.replace(/%C3%96/gi,'Ö');
	texto = texto.replace(/%C3%99/gi,'Ù');
	texto = texto.replace(/%C3%9B/gi,'Û');
	texto = texto.replace(/%C3%9C/gi,'Ü');
	texto = texto.replace(/%C3%9F/gi,'ß');
	texto = texto.replace(/%C3%A0/gi,'À');
	texto = texto.replace(/%C3%A2/gi,'â');
	
	return texto;
}	
// According to RFC 3986, only characters from a set of reserved and a set
// of unreserved characters are allowed in a URL:
var unreserved = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~";
var reserved = "!*'();:@&=+$,/?%#[]";
var allowed = unreserved + reserved;
var hexchars = "0123456789ABCDEFabcdef";
// --------------------------------- Encoding -------------------------------

// This function returns a percent sign followed by two hexadecimal digits.
// Input is a decimal value not greater than 255.
function gethex(decimal) {
  return "%" + hexchars.charAt(decimal >> 4) + hexchars.charAt(decimal & 0xF);
  }

function encode(entrada) {
  // Some variables:
  var decoded = entrada;
  var encoded = "";

  // ---------------- If UTF-8 character encoding was chosen: ----------------
  for (var i = 0; i < decoded.length; i++ ) {
      var ch = decoded.charAt(i);
      // Check if character is an unreserved character:
      if (unreserved.indexOf(ch) != -1) {
        encoded = encoded + ch;
      } else {

        // The position in the Unicode table tells us how many bytes are needed.
        // Note that if we talk about first, second, etc. in the following, we are
        // counting from left to right:
        //
        //   Position in   |  Bytes needed   | Binary representation
        //  Unicode table  |   for UTF-8     |       of UTF-8
        // ----------------------------------------------------------
        //     0 -     127 |    1 byte       | 0XXX.XXXX
        //   128 -    2047 |    2 bytes      | 110X.XXXX 10XX.XXXX
        //  2048 -   65535 |    3 bytes      | 1110.XXXX 10XX.XXXX 10XX.XXXX
        // 65536 - 2097151 |    4 bytes      | 1111.0XXX 10XX.XXXX 10XX.XXXX 10XX.XXXX

        var charcode = decoded.charCodeAt(i);

        // Position 0 - 127 is equal to percent-encoding with an ASCII character encoding:
        if (charcode < 128) {
          encoded = encoded + gethex(charcode);
        }

        // Position 128 - 2047: two bytes for UTF-8 character encoding.
        if (charcode > 127 && charcode < 2048) {
          // First UTF byte: Mask the first five bits of charcode with binary 110X.XXXX:
          encoded = encoded + gethex((charcode >> 6) | 0xC0);
          // Second UTF byte: Get last six bits of charcode and mask them with binary 10XX.XXXX:
          encoded = encoded + gethex((charcode & 0x3F) | 0x80);
        }

        // Position 2048 - 65535: three bytes for UTF-8 character encoding.
        if (charcode > 2047 && charcode < 65536) {
          // First UTF byte: Mask the first four bits of charcode with binary 1110.XXXX:
          encoded = encoded + gethex((charcode >> 12) | 0xE0);
          // Second UTF byte: Get the next six bits of charcode and mask them binary 10XX.XXXX:
          encoded = encoded + gethex(((charcode >> 6) & 0x3F) | 0x80);
          // Third UTF byte: Get the last six bits of charcode and mask them binary 10XX.XXXX:
          encoded = encoded + gethex((charcode & 0x3F) | 0x80);
        }

        // Position 65536 - : four bytes for UTF-8 character encoding.
        if (charcode > 65535) {
          // First UTF byte: Mask the first three bits of charcode with binary 1111.0XXX:
          encoded = encoded + gethex((charcode >> 18) | 0xF0);
          // Second UTF byte: Get the next six bits of charcode and mask them binary 10XX.XXXX:
          encoded = encoded + gethex(((charcode >> 12) & 0x3F) | 0x80);
          // Third UTF byte: Get the last six bits of charcode and mask them binary 10XX.XXXX:
          encoded = encoded + gethex(((charcode >> 6) & 0x3F) | 0x80);
          // Fourth UTF byte: Get the last six bits of charcode and mask them binary 10XX.XXXX:
          encoded = encoded + gethex((charcode & 0x3F) | 0x80);
        }

      }

  }  // end of for ...
  return encoded;
}
function doAjaxRequest(input)
{
	var pointer = this;
	// create ajax request
	var url = "php/wk_tarifas/tarifas.php?input=" + encode(input);
	if (!url)
		return false;
	
	var onSuccessFunc = function (req) { setSuggestions(req, input) };
	var onErrorFunc = function (status) { alert("AJAX error: "+status); };

	var myAjax = new _b.Ajax();
	makeAjaxRequest( url, "get", onSuccessFunc, onErrorFunc );
}
	
function setSuggestions(req, input)
{
	var xml = req.responseXML;
	// traverse xml
	if (xml) {
		var results = xml.getElementsByTagName('results')[0].childNodes;
		for (var i=0;i<results.length;i++)
		{
			if (results[i].hasChildNodes())
				actualizarTarifaFromXML(results[i].getAttribute('info'));
		}
	}
}

function makeAjaxRequest(url, meth, onComp, onErr)
{
	if (meth != "POST")
		meth = "GET";
	
	this.onComplete = onComp;
	this.onError = onErr;
	var pointer = this;
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest)
	{
		this.req = new XMLHttpRequest();
		this.req.onreadystatechange = function () { pointer.processReqChange() };
		this.req.open("GET", url, true); //
		this.req.send(null);
	// branch for IE/Windows ActiveX version
	}
	else if (window.ActiveXObject)
	{
		this.req = new ActiveXObject("Microsoft.XMLHTTP");
		if (this.req)
		{
			this.req.onreadystatechange = function () { pointer.processReqChange() };
			this.req.open(meth, url, true);
			this.req.send();
		}
	}
}
function processReqChange()
{
	// only if req shows "loaded"
	if (this.req.readyState == 4) {
		// only if "OK"
		if (this.req.status == 200)
		{
			this.onComplete( this.req );
		} else {
			this.onError( this.req.status );
		}
	}
}