
	// javascript to download all the written answers from theyworkforyou.com

	// constants
	var LINK_URL = "http://www.theyworkforyou.com";
	var TOTAL_TO_DISPLAY = 4;
	var LOADING_MESSAGE = "<p>Downloading information from <b>theyworkforyou.com</b> ...</p>";
	var ERROR_MESSAGE = "<p><b>Error: unable to connect</b></p><p>Unfortunately. we were unable to download any information from <b>theyworkforyou.com</b>.  Please try this page again later.</p>";
	var RESULTS_START = "<p><b>Don Foster's most recent appearances in Parliament</b></p>";
	var RESULTS_END   = "<p><b>For more information</b><br>See more of Don Foster's appearances in Parliament at <a href=\"http://www.theyworkforyou.com/mp/don_foster/bath\" target=\"_blank\">theyworkforyou.com</a>.</p>";

	// variables
	var theHTML = "";
	
	var count = 0;
	
	// executable code	
	$(document).ready(function() {
		// state we are loading
		$("#theyworkforyoudata").append(LOADING_MESSAGE);
		
		$.ajax({
		  // lets pull the data off the local
		  url: "theyworkforyou.php",
		  dataType: "xml",
		  timeout: 10000,
		  cache: false,
		  error: function() { 
		  	$("#theyworkforyoudata").html(ERROR_MESSAGE);
		   },
		  success: function(xml) { 
		  	count = 1;
			
		  	$(xml).find('match').each(function() { 
				//count++;
				
				if (count <= TOTAL_TO_DISPLAY)
				{
					// title
					var title = $(this).find("parent body").text();
					
					if (title != "")
					{
						count++;
						// main url
						var url = $(this).find("listurl").eq(0).text();
						// main text
						var theBody = $(this).find("body").eq(0).text();
						var theDate = $(this).find("hdate").text();
						
						// compile it all up
						theHTML += '<p><a href="' + LINK_URL + url + '" target="_blank">' + title + ' (' + theDate + ')</a></p>';
						theHTML += "<p>" + theBody + "</p>";
					}
				}
			});
			
			
			theHTML = RESULTS_START + theHTML + RESULTS_END;
			
			$("#theyworkforyoudata").html(theHTML);
		   }
		});
	});



