/*
 * Needs:
 *	jQuery 1.2.6 or higher
 *	jqXMLUtils.js
 *	jqSOAPClient.js
 *
 *	This is used to populate the cities list tab on the Spend home page using an AJAX call to a webservice method
*/

$(document).ready(function() {


	//Send it
	SOAPClient.Proxy="/webservice/ZoneMapWebService/ZoneMapService";

	//Create a new top level xml element for web:GetCitiesInZone
	var soapBody = new SOAPObject("web:GetZonesAndCities"); 

	//Create a new SOAP request
	var sr = new SOAPRequest("GetZonesAndCities", soapBody); 
	//Add namespaces as per wsdl to match the elements above:  web and web1
	sr.addNamespace("web","http://uk/co/airmiles/webservice");
	sr.addNamespace("web1","http://airmiles.co.uk/webservice");	        

	SOAPClient.SendRequest(sr, processResponse, processError); //Send request and assign a callback        			
});
 
function processResponse(soapEnvelope) {

	var zones = soapEnvelope.Body[0].GetZonesAndCitiesResponse[0].zones_and_cities_results[0].zones[0].zone;
	var cities = new Array();
	var citiesCombined = new Array();

	var output = "";
	var divOpen = "<div>";
	var divClose = "</div>";
	var imageTag = "";
	var heading = "<h2>";
	var cityLinks = "";
	var zonesArray = new Array();
	jQuery.each(zones, function(i, zone) {
		if(zone["@id"] != "0") {
			imageTag += '<img src="' + zone["@image_url"] + '" alt="Zone ' + i + ' Image" />';
			heading += "Zone " + zone["@id"] + ":&nbsp; <span>" + zone["@mileage"] + "</span> <em>miles</em></h2>";
			output += imageTag + divOpen + heading;
			imageTag = "";
			heading = "<h2>";
		}
		
		
		if(zone["@id"] != "0") {
			
		}
		
		//Get the list of cities from the envelope
		if(zone["@id"] === "0" && zone["@id"] != "1") {
			citiesCombined = soapEnvelope.Body[0].GetZonesAndCitiesResponse[0].zones_and_cities_results[0].zones[0].zone[i].cities[0].city;
			var cities1 = soapEnvelope.Body[0].GetZonesAndCitiesResponse[0].zones_and_cities_results[0].zones[0].zone[1].cities[0].city;
			var index = 0;
			var endPoint = cities1.length;
			while (index < endPoint) {
				citiesCombined.push(cities1[index]);
				index ++;
			}

			//sort the combined cities alphabetically by name
			citiesCombined.sort(function(city1, city2){
				var name1 = city1["@name"];
				var name2 = city2["@name"];
				return ((name1 < name2) ? -1 : ((name1 > name2) ? 1 : 0));
			});

			//construct the link from each city object
			jQuery.each(citiesCombined, function(j, city) {
				cityLinks += '<a href="';
				cityLinks += city["@flight_search_url"] + '">';
				if(citiesCombined.length != j + 1) {
					cityLinks += city["@name"] + "</a>, ";
				}else {
					cityLinks += city["@name"] + "</a>";
				}
			});
		}

		if(zone["@id"] != "0" && zone["@id"] != "1") {
			cities = soapEnvelope.Body[0].GetZonesAndCitiesResponse[0].zones_and_cities_results[0].zones[0].zone[i].cities[0].city;
		}

		//construct the link from each city object
		jQuery.each(cities, function(j, city) {
			cityLinks += '<a href="';
			cityLinks += city["@flight_search_url"] + '">';
			if(cities.length != j + 1) {
				cityLinks += city["@name"] + "</a>, ";
			}else {
				cityLinks += city["@name"] + "</a>";
			}
		});
		
		if(zone["@id"] != "0") {
			output += cityLinks + divClose;
			cityLinks = "";
			$("#cities-list-error").remove();
			$("#spend-cit ul").append('<li class="clearfix">' + output + "</li>");
			//clear the output, otherwise it will be repeated on the next run through!
			output = "";
		}
		
	});
	
}

var cititesTabErrorCount = 0;

function processError(textStatus) {
    cititesTabErrorCount++;
	if(cititesTabErrorCount == 1) {
	  $("#spend-cit ul").append('<div id="cities-list-error">The webservice is down, please try again later</div>');
	}
}