  var mapRefresherXmlHttp = "";
//
// You will have to call this function AFTER the
// whole page has loaded
//
function updateMap()
{
	mapRefreshTimer = "";
	if(mapRefresherXmlHttp == "")
	{
		debug("Requesting mapUpdate");
		var mapID = document.getElementById("mapID").value;
		var url = mapInfoUrl + "" + mapID;
		debug("AJAX:" + url);
		mapRefresherXmlHttp=GetXmlHttpObject();
		mapRefresherXmlHttp.onreadystatechange=updateMapComplete;
		mapRefresherXmlHttp.open("GET",url,true)
		mapRefresherXmlHttp.send(null);
		mapRefreshTimer = "Loading...";
	}
	else
	{
		debug("Map is updating, update denied!");
	}
	
}


function updateMapComplete()
{
	if (mapRefresherXmlHttp.readyState==4)
	{ 
		debug("Map Update Recieved");
		fullText  = mapRefresherXmlHttp.responseText;
		var data1 = fullText.split("::");

		
		//
		// get the fields
		//
		fieldData = data1[0];
		var fields = fieldData.split(",");
		
		for(i=1;i<data1.length;i++)
		{
			//alert(data1[i]);
			var data = data1[i].split(",");
			unit = new Array();
			for(j=0;j<fields.length;j++)
			{
				unit[fields[j]] = data[j];
				//debug(fields[j] + ":" + data[j] + ":" + unit[fields[j]]);
			}
		
			if(unit["unitID"] > 0)
			{
				//alert(unit["unitID"]);
				unitID = unit["unitID"];
				var objUnit = document.getElementById("unit_" + unit["unitID"]);
				
				if(objUnit != null)
				{
					debug("Unit Found: " + unitID);
				
					if(objUnit.getAttribute("tileX") > boardWidth ||
						objUnit.getAttribute("tileY") > boardHeight
					)
					{
						debug("Error unit is off map " + boardWidth + "," + boardHeight);
						
						
					}
					
					if(objUnit.getAttribute("tileX") != unit["x"] &&
						objUnit.getAttribute("tileY") != unit["y"]
						&& unitID != selectedUnitID)
					{
						moveUnit(unitID, unit["x"] , unit["y"]);
						
					}
					life = unit["life"];
					
					if(objUnit.getAttribute("life") < life ||
						objUnit.getAttribute("life") > life)
					{
						//alert(life);
						diff = parseInt(objUnit.getAttribute("life")) - life;
						//showUnitMsg(unitID, "-" + diff);		
						objUnit.setAttribute("life",life);
						
						if(life < 0)
						{
							removeUnit(unitID);
						}
					}
					
				}
				else
				{
					if(unitID > 0 && unit["life"] > 0)
					{
						debug("Adding Unit: " + unitID);
						addUnitFromArrays(fields, data);
						//addUnitFromStrings(fieldData, data1[i]);
					}
				}
			}
			
		}
		debug("Map Update Complete");
		
		mapRefresherXmlHttp = "";
	}

}


