


window.onload = getFile;

	// only gets file once, on window load
	// prepares global variable xsteps once
	// thereafter (as click on steps) no more HTTP request
	// instead just accesses prepared global variable


// global variables

var xhr = false;		// the XMLHttpRequest object

var xsteps;			

	// the parsed XML as a collection of steps
	// xsteps = xml.getElementsByTagName("continuerun");




function msgStatus ( string )		// normal text
{
 document.getElementById("msgs").innerHTML = string;
}


function msgError ( string )		// maybe not error, but at least highlighted
{
 var s = "<font style='background-color:#ffff66'>" + string + "</font>";

 document.getElementById("msgs").innerHTML = s;
}






function getFile() 
{
 	  msgError  ("Downloading and parsing XML. Please wait." );
	makeRequest("test.ajax.wwm.xml");		 
	return false;					// don't load new web page
}




function makeRequest(url) 
{
	if (window.XMLHttpRequest) 	// if supported by browser
	{
		xhr = new XMLHttpRequest();
	}
	else 
	{
		if (window.ActiveXObject) 			// IE up to version 6 
		{
			try 
			{
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}

	if (xhr) 
	{
		xhr.onreadystatechange = gotFile;
			// this fn is called when xhr is ready

		xhr.open("GET", url, true);
			// GET url of file on the same server as this

		// no caching:
		xhr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");
		xhr.setRequestHeader( "Cache-Control", "no-cache" );
		xhr.setRequestHeader( "Cache-Control", "must-revalidate" );
		xhr.setRequestHeader( "Cache-Control", "no-store" );
		xhr.setRequestHeader( "Pragma", "no-cache" );
		xhr.setRequestHeader( "Expires", "0" );
	 
		xhr.send(null);
	}
	else 
	{
		msgError  ( "Error: Could not create XMLHttpRequest." );
	}
}




function gotFile() 
{
 if ( xhr.readyState == 4 ) 	// xhr finished
 {
  if ( xhr.status == 200 ) 	// HTTP code 200
  {



// Javascript debug dump functions work on arrays but not complex objects

// 	var testArray = {'true' : true, 'false' : false, num : 3};   
//	alert( dump( testArray ));   
//	print_r ( testArray, true );

//	alert( "[" + dump( xhr ) + "]" );  
//	print_r ( xhr, true );

	

	if ( xhr.responseXML.xml )			// IE
	{
	  parseit ( xhr.responseXML );	 	// structured XML object
	  // msgStatus  ( "... Done. " );
	  msgStatus ( "&nbsp;" );			// Clear the box of any messages
	}


	else if ( xhr.responseText )			// other browsers
	{		
//	  alert ( xhr.responseText );	  	// the XML text, not a structured XML object

	var xmlobject = (new DOMParser()).parseFromString( xhr.responseText, "text/xml");

	  parseit ( xmlobject  );	
	  msgStatus ( "&nbsp;" );		 
	}


	else														 
	 msgError ( "Error: No XML returned. "  );
  }
  else 
   msgError ( "Error: XMLHttpRequest failed. Status: " + xhr.status );
 }
}



function displayStep ( i )
{
 	slist = xsteps[i].getElementsByTagName("score");		// will only be one of them

 if ( slist[0].text )
  s = slist[0].text;			// IE
 else
  s = slist[0].textContent;		// other browsers

 	document.getElementById("score").innerHTML = s;


   	xlist = xsteps[i].getElementsByTagName("state"); 	 

 if ( xlist[0].text )
  x = xlist[0].text;			 
 else
  x = xlist[0].textContent;		 
	 
 	document.getElementById("state").innerHTML = x;


   	alist = xsteps[i].getElementsByTagName("action"); 	 
	a = alist[0].attributes.getNamedItem("value").value;		 
 	document.getElementById("action").innerHTML = a;

   	tlist = xsteps[i].getElementsByTagName("timestep"); 	 
	t = tlist[0].attributes.getNamedItem("value").value;		 
 	document.getElementById("timestep").innerHTML = t;
}



function parseit ( xml )
{
 xsteps = xml.getElementsByTagName("continuerun");		// set up global variable

 var s = "";
 for ( var i=0; i < xsteps.length; i++ )		
 // each step i is a link you can click to call displayStep(i)
 {
  s = s + "<a class=reverseAnchor onClick='displayStep(" + i + ");' style='cursor: pointer; cursor: hand;'>" + i + "</a> &nbsp; ";
 }
 document.getElementById("timesteplist").innerHTML = s;

 displayStep ( xsteps.length - 1 );
 //	displayStep ( 0 );
}




