
var imgList;
var imgBuffer;
var adIndex;

function loadXML(szUrl) 
{

   // This function accepts a file name and returns an XML document.
   // Note we're doing no error checking.

   if (window.ActiveXObject){
      // Since IE has so much trouble with responseXML
      // we'll load the XML with the XMLDOM activeX object
      var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      // Handle ready state changes ( ignore them until readyState = 4 )
      xmlDoc.onreadystatechange= function() { if (xmlDoc.readyState!=4) return false; }
      // This is a synchronous call!   The script will stall until the document
      // has been loaded.
      xmlDoc.async="false";
      xmlDoc.load(szUrl);
      return xmlDoc;
   }
  
   // Initialize the AJAX object.
   var AJAX=new XMLHttpRequest();  

   // Handle ready state changes ( ignore them until readyState = 4 )
   AJAX.onreadystatechange= function() { if (AJAX.readyState!=4) return false; }

   // we're passing false so this is a syncronous request.
   // The script will stall until the document has been loaded.
   AJAX.open("GET", szUrl, false);
   AJAX.send(null);

   return AJAX.responseXML;
}  // loadXML


function loadAds( szUrl )
{
	try
	{
		adIndex = 0;
		
		var doc = loadXML( szUrl );
		
		imgList = new Array();
		imgList = doc.getElementsByTagName("ad");
		
		imgBuffer = new Array();
		
		
		//
		// Pre-load the images into an array
		//
		var x;
		for( x=0; x < imgList.length; x++ )
		{
			imgBuffer[x] = new Image();
			imgBuffer[x].src = "./ads/" + imgList[x].childNodes[0].nodeValue;
		}
				
	}
	catch( err )
	{
		alert( "adShuffle.js loadAds: " + err.message );
	}

} // loadAds 

function getNextAd()
{
	
	try
	{
		
		adIndex++;
		if ( adIndex >= imgList.length )
		{
			adIndex = 0;
		}
		
		// return the image object
		// the url of the object is in the .src member
		return imgBuffer[adIndex];
	}
	catch(err)
	{
		alert( "adShuffle.js getNextAd: " +  err.message)
	}

} // getNextAd