var req;
var theXML;

function loadXMLDoc(url){
	new Ajax(url, {method: 'get', onComplete: showResponse}).request()
	return false;
};

function showResponse(request){
  if (document.getElementById("mapimage")) {
  	// code for IE
  	if (window.ActiveXObject){
  		var xmlobject=new ActiveXObject("Microsoft.XMLDOM");
  		
  		xmlobject.async="false";
  		xmlobject.loadXML(request);
  	}
  	// code for Mozilla, Firefox, Opera, etc.
  	else{
  		// convert string to XML object
  		var xmlobject = (new DOMParser()).parseFromString(request, "text/xml");
  	}
  
  	//here's where you put in what to do with the XML doc
  	maxNum = xmlobject.getElementsByTagName("instance").length; // how many instances in that xml doc can we pick from? Create a maximum.
  	number = getCookie("whichInstance"); //pull the last number the user accessed from the user's cookie
  	if (number >= maxNum || number == null) { //if they've reached the last one in the xml doc, or they don't have a cookie
  		number = 1; //reset the number to 1
  	} else {
  		number++; //or go to the next number
  	}
  	instance = xmlobject.getElementsByTagName("instance")[number-1]; // zero in on the instance that corresponds to that number, minus one because of the funny way javascript picks array items starting from 0.
  	document.getElementById("mapimage").style.background = "url(graphics/splash/"+instance.firstChild.nodeValue+".jpg) no-repeat"; // get that map image
  	//document.getElementById("thismap").href = "graphics/thumbs/" + instance.firstChild.nodeValue + ".html"; // get the link to the map of that instance
  
  	setCookie("whichInstance",number,"Sat, 12 Dec 2099 11:59:59 UTC"); // reset a new cookie with the new
  }	
};

function setCookie(name, value, expires, path, domain, secure) {
	cookieText = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie= cookieText;
}
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null; 
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}