//
//
// Conjunto de funciones que buscan listas de precios en Sidetours, y la colocan
// en el fichero correspondiente, que tenga los elementos "id" adecuados


//
// Función imprescindible que crea un objeto AJAX (válido para los navegadores mas usados)

function nuevoAjax() {
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

var xmlhttp=nuevoAjax();


//
// Funciones que se encargan de procesar las peticiones de listas de precios
//		LoadPrices()
//		ComponerTablaLoadPrices()


// Esta función se encarga de recolectar los precios con estacion = "stationcode"
// con el modo "Current" en la temporada,
// Se encarga también de recolectar todos los precios en todas las temporadas
function LoadPrices(stationcode) {

	// Primera llamada con las estaciones concretas y "Current" 
	cadena = "prices_xmlhttp.php?" +
	"agencycode=ELITE&" +
	"stationcode=" + stationcode + "&" +
	"seasoncode=" + "Current";

	xmlhttp.open("GET",	cadena, false);
	xmlhttp.send(null);
	
	// Ahora se sustituyen todos los precios, a partir del XML
	var xmlDoc=xmlhttp.responseXML.documentElement;
	ProcesarLoadPrices(xmlDoc);

	// Segunda llamada, con todas las estaciones y todas las temporadas
	cadena = "prices_xmlhttp.php?" +
	"agencycode=ELITE&" +
	"stationcode=" + stationcode + "&" +
	"seasoncode=All";

	xmlhttp.open("GET",	cadena, false);
	xmlhttp.send(null);

	// Ahora se sustituyen todos los precios, a partir del XML
	var xmlDoc=xmlhttp.responseXML.documentElement;
	ProcesarLoadPrices(xmlDoc);

} // LoadPrices


function ProcesarLoadPrices(xmlDoc) {
	
	var querystatus = xmlDoc.getElementsByTagName('QueryStatus')[0].childNodes[0].nodeValue;

	if (querystatus == 'Ok') {

		// Se va a hacer una búsqueda en cuatro niveles (Company no se usa de momento):
		//  Station
		//   Season (Low, Mid o High)
		//    Company (no se va a usar de momento)
		//     Group (todos los grupos disponibles)
	
		var isCurrent = xmlDoc.getElementsByTagName("SeasonCode")[0].childNodes[0].nodeValue;

		var stations = xmlDoc.getElementsByTagName("Station");
		for (var oneStation = 0; oneStation < stations.length; oneStation++) {

			var stationcode = stations[oneStation].getElementsByTagName("StationCode")[0].childNodes[0].nodeValue;
			
			var seasons = stations[oneStation].getElementsByTagName("Season");
			for (var oneSeason = 0; oneSeason < seasons.length; oneSeason++) {
	
				var seasonname = seasons[oneSeason].getElementsByTagName("SeasonName")[0].childNodes[0].nodeValue;

				var groups = seasons[oneSeason].getElementsByTagName("Group");
				for (var oneGroup = 0; oneGroup < groups.length; oneGroup++) {
	
					groupcode = groups[oneGroup].getElementsByTagName("GroupCode")[0].childNodes[0].nodeValue;
					currentprice = groups[oneGroup].getElementsByTagName("GroupPrice")[0].childNodes[0].nodeValue;

					if (isCurrent == "Current") {
						showedSeason = "Current";
					} else {
						showedSeason = seasonname;
					}

					// Componemos y sustituimos los precios de la temporada
					tag = 	"station_" + stationcode +
							"_season_" + showedSeason +
							"_group_" + groupcode;
							
					if (currentprice == "Unavailable") {
						currentprice_texto = "N/A";
					} else {
						while(currentprice.length<3)currentprice="0"+currentprice; // Rellenar con ceros hasta tamaño 3
						currentprice_texto = 	currentprice.substring(0, currentprice.length - 2);
						currentprice_cents = currentprice.substring(currentprice.length - 2);
						// Mostrar los centimos solamente si no son cero
						if (currentprice_cents > 0) {
							currentprice_texto = currentprice_texto + ',' + currentprice_cents;
						}
												
					}

					if (document.getElementById(tag) != null) {
						document.getElementById(tag).innerHTML = currentprice_texto + " &euro;";
					}

				} // for Group
				
			} // for Season
		
		} // for Station
			

	} // if QueryStatus == "Ok"
	
} // ComponerTablaLoadPrices


