// File: readXML.js

// Start function when DOM has completely loaded
$(document).ready(function(){

	// Open the students.xml file
	$.get("ACI.xml",{},function(xml){

		// Build an HTML string
		myHTMLOutput = '<tr><td colspan=\"3\" style=\"border-bottom: 1px solid #000;\" >&nbsp;</td></tr>';
    startTimeFinal = '';

		// Run the function for each student tag in the XML file
		$('Row',xml).each(function(i) {
			RIC = $(this).find("RIC").text();
      if (RIC == '.JALSH') RIC = 'JSE All Share Index';
      if (RIC == 'XAU=') RIC = 'Gold';
      if (RIC == 'ZAR=') RIC = 'USD/ZAR';
      if (RIC == 'ZAR153=') RIC = 'R153 Bond';
      if (RIC == 'EURZAR=R') RIC = 'EUR/ZAR';
      if (RIC == 'GBPZAR=R') RIC = 'GBP/ZAR';
			bidPrice = $(this).find("Bid_Price").text();
      if (bidPrice == '') bidPrice = '&nbsp;';
			askPrice = $(this).find("Ask_Price").text();
      if (askPrice == '') askPrice = '&nbsp;';
			lastPrice = $(this).find("Last_Price").text();
      if (lastPrice == '') lastPrice = '&nbsp;';
      startTime = $(this).find("StartTime").text();
      if (startTime != '' ) startTimeFinal = startTime;

      if (RIC != '') {
			// Build row HTML data and store in string
			mydata = BuildStudentHTML(RIC,bidPrice,askPrice,lastPrice);
			myHTMLOutput = myHTMLOutput + mydata;
      }
		});
		myHTMLOutput = myHTMLOutput + '<tr><td colspan=\"2\"><small>As of: <strong>'+startTimeFinal+'</strong></small></td></tr>';

		// Update the DIV called Content Area with the HTML string
		$("#xmlTable").append(myHTMLOutput);
	});
});



 function BuildStudentHTML(RIC,bidPrice,askPrice,lastPrice){


	// Build HTML string and return
	output = '';
	output += '<tr>';
	output += '<td style=\"border-bottom: 1px solid #000; border-right: 1px solid #000; border-left: 1px solid #000;\" >'+ RIC +'</td>';
	output += '<td style=\"border-bottom: 1px solid #000; border-right: 1px solid #000;\" >'+ bidPrice +'</td>';
  if (RIC == 'JSE All Share Index')
  	  output += '<td style=\"border-bottom: 1px solid #000; border-right: 1px solid #000; \" >'+ lastPrice +'</td>';
  else
	    output += '<td style=\"border-bottom: 1px solid #000; border-right: 1px solid #000; \" >'+ askPrice +'</td>';
	output += '</tr>';
	return output;
  }
