
//------------------------------------------------------
//
// This file handles functions that are specific to
// the system.
//
// CHANGES TO THIS FILE WILL NOT ALLOW YOU TO
// RECIEVE UPGRADES AND/OR HOT FIXES
//
//------------------------------------------------------


/************************************************
Sound Off
************************************************/
soundOn = true;
aiRefreshRate = 20;

/************************************************
AJAX Handlers
************************************************/
var xmlHttpDivID = ""
var xmlHttp = ""
 
function GetXmlHttpObject(handler)
{ 
    var objXMLHttp=null
    if (window.XMLHttpRequest)
    {
        objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    return objXMLHttp;
}


/*****************************************************************************************

	Animated Ads

*****************************************************************************************/

adTopPos = 450;
function floatOver()
{
	var d = document.getElementById("adSpace");
	adTopPos-=3;
	d.style.top = adTopPos;
	if(adTopPos >  -200)
	{
		setTimeout("floatOver()",100);
	}
	else
	{
		adTopPos = 450;
		setTimeout("floatOver()",60000);
		
	}
	
}



/*****************************************************************************************

	LOBBY FUNCTIONS

*****************************************************************************************/

//	Refreshing Lobby -------------------------------------------------------------
function refreshLobby()
{
	refreshChat();	
	setTimeout("refreshLobby()",10000);
	
}



//	Chat -------------------------------------------------------------

function refreshChat()
{
	//alert("Refreshing Chat");
	chatXmlHttp=GetXmlHttpObject();
	
	if (chatXmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	url = 'widgets/divChat.php';
	//document.write(url);
	chatXmlHttp.onreadystatechange=refreshChat_Complete;
	chatXmlHttp.open("GET",url,true)
	chatXmlHttp.send(null);
}


function refreshChat_Complete()
{
	if (chatXmlHttp.readyState==4)
	{ 
		fullText  = chatXmlHttp.responseText;
		document.getElementById("chat").innerHTML = fullText;
	}
}


//	AI -------------------------------------------------------------

function runAI()
{
	//alert("Refreshing Chat");
	aiXmlHttp=GetXmlHttpObject();
	
	if (aiXmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	url = 'actions/aiScript.php';
	//document.write(url);
	aiXmlHttp.onreadystatechange=runAI_Complete;
	aiXmlHttp.open("GET",url,true)
	aiXmlHttp.send(null);
}


function runAI_Complete()
{
	if (aiXmlHttp.readyState==4)
	{ 
		//fullText  = aiXmlHttp.responseText;
		//document.getElementById("chat").innerHTML = fullText;
		
	}
}
/*****************************************************************************************

	WORLD FUNCTIONS

*****************************************************************************************/

function overTile(x, y)
{
	unitID = getUnitAt(x, y);
	
	if(unitID > 0)
	{
		overUnit(unitID, x, y);
	}
	
	tile = document.getElementById("tile_" + x + "_" + y + "_mouseover");	
	tile.style.visibility = "visible";	
}

function offTile(x, y)
{
	tile2 = document.getElementById("tile_" + x + "_" + y + "_mouseover");	
	tile2.style.visibility = "hidden";
}

function openTravel()
{
	showPanel("travel");
}

function tileClicked(x, y)
{
	//alert(x + "," + y);
	unitID = getUnitAt(x, y);
	
	if(unitID > 0)
	{
		objUnit = getUnitDiv(unitID);
		if(objUnit.getAttribute("isOwner")=="Y")
		{
			friendlyUnitClicked(objUnit, x, y);
		}
		else
		{
			enemyUnitClicked(objUnit, x, y);
		}
	}
	else
	{
		objectID = getObjectAt(x, y);
		if(objectID > 0)
		{
			if(inBaseContactWithFriendly(x, y))
			{
				t = document.getElementById("obj_" + objectID);
				functionName = t.getAttribute("functionName");

				//alert(functionName);
				if(functionName != "")
				{
					setTimeout(functionName,100);
				}
			}
		}
		else
		{
			t = document.getElementById("tile_" + x + "_" + y + "_move");
			if(t.getAttribute("canMoveTo") == 'Y')
			{
				moveTileClicked(x, y);
			}
		}
	}
}


lastDisplayUnitID =0;
function setDisplay(unitID)
{
	if(unitID != lastDisplayUnitID)
	{
		displayXmlHttp=GetXmlHttpObject();
	
		if (displayXmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		} 

		url = 'widgets/displayUnit.php?inGame=Y&unitID=' + unitID;
		//document.write(url);
		displayXmlHttp.onreadystatechange=setDisplay_Complete;
		displayXmlHttp.open("GET",url,true)
		displayXmlHttp.send(null);
		lastDisplayUnitID = unitID;
	}
}

function setDisplay_Complete()
{
	if (displayXmlHttp.readyState==4)
	{ 
		displayText  = displayXmlHttp.responseText;
		
		document.getElementById("unitDisplay").innerHTML = displayText;
	}
}

function getUnitDiv(unitID)
{
	return document.getElementById("unit_" + unitID);
}


function inBaseContact(unitID, x, y)
{
	
	//alert(x + "," + y + ":" + getEnemyUnitAt(6,3));
	//alert("1: " + getEnemyUnitAt(x, y+1));
	//alert("2: " + getEnemyUnitAt(x, y+1));
	//alert("3: " + getEnemyUnitAt(x, y-1));
	//alert("4: " + getEnemyUnitAt(x+1, y));
	//alert("5: " + getEnemyUnitAt(x-1, y));
	//alert("6: " + getEnemyUnitAt(x+1, y+1));
	//alert("7: " + getEnemyUnitAt(x+1, y-1));
	//alert("8: " + getEnemyUnitAt(x-1, y-1));
	//alert("9: " + getEnemyUnitAt(x-1, y+1));
	x = parseInt(x);
	y = parseInt(y);
	
	if(	getEnemyUnitAt(x, y+1) > 0 ||
		getEnemyUnitAt(x, y-1) > 0 ||
		getEnemyUnitAt(x+1, y) > 0 ||
		getEnemyUnitAt(x-1, y) > 0 ||
		getEnemyUnitAt(x+1, y+1) > 0 ||
		getEnemyUnitAt(x+1, y-1) > 0 ||
		getEnemyUnitAt(x-1, y-1) > 0 ||
		getEnemyUnitAt(x-1, y+1) > 0 )	
	{
		
		return true;
	}
	
	
	return false;
}

function inBaseContactWithFriendly(x, y)
{
	x = parseInt(x);
	y = parseInt(y);
	
	if(	getFriendlyUnitAt(x, y+1) > 0 ||
		getFriendlyUnitAt(x, y-1) > 0 ||
		getFriendlyUnitAt(x+1, y) > 0 ||
		getFriendlyUnitAt(x-1, y) > 0 ||
		getFriendlyUnitAt(x+1, y+1) > 0 ||
		getFriendlyUnitAt(x+1, y-1) > 0 ||
		getFriendlyUnitAt(x-1, y-1) > 0 ||
		getFriendlyUnitAt(x-1, y+1) > 0 )	
	{
		
		return true;
	}
	
	
	return false;
}



/***************************************************************************
*
*                   Message Window
*
***************************************************************************/

function openMsgWindow(msg)
{
	div = document.getElementById("msgWindow");
	div.innerHTML = msg;
	div.style.visibility = "visible";
	div.style.top = "150px";
}


function closeMsgWindow()
{
	div = document.getElementById("msgWindow");
	div.innerHTML = "";
	div.style.visibility = "hidden";
	div.style.top = "-1000px";
}




/*****************************************************************************************

	Animated Walk

*****************************************************************************************/

var animateCount = 0;
function startAnimateUnit(unitID, x, y)
{
	var unit = document.getElementById("unit_" + unitID);
	
	if(unit != null)
	{
		
		if(unit.getAttribute('finalX') != x || unit.getAttribute('finalY') != y)
		{
			unit.setAttribute('finalX',x);
			unit.setAttribute('finalY',y);
			unit.setAttribute('nextX',unit.getAttribute('tileX'));
			unit.setAttribute('nextY',unit.getAttribute('tileY'));
			animateCount = 0;
			pickNextDest(unit);
		}
	}
	
}

function pickNextDest(objUnit)
{
	finalX = parseInt(objUnit.getAttribute('finalX'));
	finalY = parseInt(objUnit.getAttribute('finalY'));

	currX = parseInt(objUnit.getAttribute('nextX'));
	currY = parseInt(objUnit.getAttribute('nextY'));

	nextX = currX;
	nextY = currY;
	
	if((finalX != currX || finalY != currY) && animateCount < 50)
	{
		if(finalX != currX)
		{
			if(finalX > currX)
			{
				nextX = nextX + 1;
			}
			if(finalX < currX)
			{
				nextX = nextX - 1;
			}
		}
		else
		{
			if(finalY > currY)
			{
				nextY = nextY + 1;
			}
			if(finalY < currY)
			{
				nextY = nextY - 1;
			}
		}

		var tile = document.getElementById("tile_" + nextX + "_" + nextY);
		objUnit.setAttribute('nextX',nextX);
		objUnit.setAttribute('nextY',nextY);
		objUnit.setAttribute('currX',currX);
		objUnit.setAttribute('currY',currY);
		objUnit.setAttribute('destPointX',getLeft(tile));
		objUnit.setAttribute('destPointY',getTop(tile));
		//alert("set: " + nextX + "," + nextY);	
		animateUnit(objUnit.getAttribute("unitID"));
	}
	else
	{
		//alert("At Dest");

		var tile = document.getElementById("tile_" + finalX + "_" + finalY);
		objUnit.style.left = getLeft(tile);
		objUnit.style.top = getTop(tile) - 55;
		objUnit.setAttribute('tileX',finalX);
		objUnit.setAttribute('tileY',finalY);
		
		doneMoving(objUnit.getAttribute("unitID"));
	}
}


function animateUnit(unitID)
{
	//alert(unitID);
	animateCount++;
	
	var unit = document.getElementById("unit_" + unitID);
	
	nextX = parseInt(unit.getAttribute('nextX'));
	nextY = parseInt(unit.getAttribute('nextY'));
	
	currX = parseInt(unit.getAttribute('currX'));
	currY = parseInt(unit.getAttribute('currY'));
	//alert("move: " + currX + "," + currY + " to " + nextX + "," + nextY);
	
	if(nextX == currX && nextY < currY)
	{
		//alert("here1");
		unit.style.left = getLeft(unit) + 4;
		unit.style.top = getTop(unit) + 2;
	}

	if(nextX == currX && nextY > currY)
	{
		//alert("here2");
		unit.style.left = getLeft(unit) - 4;
		unit.style.top = getTop(unit) - 2;
	}

	if(nextX < currX && nextY == currY)
	{
		//alert("here3");
		unit.style.left = getLeft(unit) - 4;
		unit.style.top = getTop(unit) + 2;
	}

	if(nextX > currX && nextY == currY)
	{
		//alert("here4");
		unit.style.left = getLeft(unit) + 4;
		unit.style.top = getTop(unit) - 2;
	}
	
	diffX = Math.abs(getLeft(unit) - unit.getAttribute('destPointX'));
	diffY = Math.abs(getTop(unit) - unit.getAttribute('destPointY'));
	
	//showDamage(unitID, Math.round(diffX/21) + "," + Math.round(diffY/45));
	
	//if(Math.round(diffX/21) <= 0 && Math.round(diffY/45) <= 1)
	if(Math.round((diffX+5)/21) <= 0 && Math.round(diffY/45) <= 1)
	{
		pickNextDest(unit);
	}
	else
	{
		setTimeout("animateUnit(" + unitID + ")",100);
	}

	
}

function setWaitTime(unitID, t)
{
	var unit = document.getElementById("unit_" + unitID);
	unit.setAttribute("waitTime",t);
	var counter = document.getElementById("unitWait_" + unitID);
	counter.innerHTML = t;
	counter.style.visibility = "visible"
	counter.style.top = getTop(unit) + 25 ;
	counter.style.left = getLeft(unit) + 15 ;
	
	if(t > 0)
	{
		unit.style.opacity = .5;
		unit.style.filter = 'alpha(opacity=50)';
	}
	else
	{
		unit.style.opacity = 1;
		unit.style.filter = 'alpha(opacity=100)';
	}
}

function showWaitTime(unitID)
{
	//var counter = document.getElementById("unitWait_" + unitID);
	//var unit = document.getElementById("unit_" + unitID);
	//counter.style.top = getTop(unit) + 45 ;
	//counter.style.left = getLeft(unit) + 15 ;
	//counter.style.visibility = "visible";
	//var unit = document.getElementById("unit_" + unitID);
	//unit.style.opacity = 1;
	//unit.style.filter = 'alpha(opacity=100)';
}

function hideWaitTime(unitID)
{
	var counter = document.getElementById("unitWait_" + unitID);	
	if(counter != null)
	{
		counter.style.visibility = "hidden";
	}
}


/***************************************************************************
*
*                   COMMAND HANDLER
*
***************************************************************************/

var refreshRate = 2;	//in seconds
var pauseTime = 2;	//in seconds

function getNextCommand()
{
	//
	//Called every 5 - 10 seconds keep it short and sweet!
	//
	commandXmlHttp=GetXmlHttpObject();
	
	if (commandXmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	url = 'actions/getNextCommand.php?action=nextCommand';
	///document.write(url);
	commandXmlHttp.onreadystatechange=getNextCommand_Complete;
	commandXmlHttp.open("GET",url,true)
	commandXmlHttp.send(null) 	
	
}

function getNextCommand_Complete() 
{ 
    if (commandXmlHttp.readyState==4)
    { 
        fullText  = commandXmlHttp.responseText;
	//alert("nextCommand: " + fullText);
		
	var data = fullText.split(",");
	
	commandID = data[0];
	command = data[1];
	userID = data[2];
	sentBy = data[3];
	sentByName = data[4];
	turnID = data[5];
	unitID = data[6];
	x = data[7];
	y = data[8];
	damage = data[9];
	life = data[10];
	waitTime = data[11];
	loserID = data[12];
	soundToPlay = data[13];
	popUpMsg = data[14];
	showImage = data[15];
	
	
	if(commandID > 0)
	{
		performCommand(command, fullText);
	}

	updateUnits();

	if(commandID > 0)
	{		
		markAsProcessed(commandID);
	}
	else
	{
		setTimeout("getNextCommand()", (refreshRate * 1000));	
	}	

	//
	//if we got unit information, let's update it.
	//
	if(unitID > 0)
	{
		var unit = document.getElementById("unit_" + unitID);
		if(unit != null)
		{
			unit.life = life;
			unit.waitTime = waitTime;
			//alert(unitID + " wait Time" + waitTime);
			setWaitTime(unitID, waitTime);
		}
	}


    }
}








function markAsProcessed(commandID)
{
	//alert("here");
	markXmlHttp=GetXmlHttpObject();
	
	if (markXmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	url = 'actions/getNextCommand.php?action=markProcessed&processedCommandID=' + commandID;
	//document.write(url);
	markXmlHttp.onreadystatechange=markAsProcessed_Complete;
	markXmlHttp.open("GET",url,true)
	markXmlHttp.send(null) 	
}


function markAsProcessed_Complete() 
{ 
    if (markXmlHttp.readyState==4)
    { 
    	//alert(markXmlHttp.responseText);
	setTimeout("getNextCommand()", (refreshRate* 1000));
    }
}

/***************************************************************************
*
*                   UTILITY FUNCTIONS
*
***************************************************************************/
function pageWidth() 
{
	return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 

function pageHeight() 
{
	return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
} 
function posLeft() 
{
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}
function posTop() 
{
	return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
} 
function posRight() 
{
	return posLeft()+pageWidth();} function posBottom() {return posTop()+pageHeight();
}
                    
                    
                    
function getTop(obj)
{
	if(obj)
	{	
		return parseInt(obj.style.top.replace("px",""));
	}
	
	return 0;
}

function getLeft(obj)
{
	if(obj)
	{
		return parseInt(obj.style.left.replace("px",""));
	}
		
	return 0;
}





/***************************************************************************
*
*                   Target
*
***************************************************************************/
function canTarget(objUnit)
{
	tile = document.getElementById("tile_" + objUnit.getAttribute('tileX') + "_" + objUnit.getAttribute('tileY') + "_target");
	
	if(tile.getAttribute("canTarget")=='Y')
	{
		return true;
	}
	
	return false;
}


function attemptToTarget(attacker, defender)
{
	//alert("attemptToTarget");
	attXmlHttp=GetXmlHttpObject();
	
	if (attXmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	a = document.getElementById("unit_" + attacker);
	actionID = a.getAttribute("actionID");
	a.setAttribute("canTarget","N");
	//a.style.opacity = .5;
	//a.style.filter = 'alpha(opacity=50)';
	url = 'actions/performAttack.php?action=target&attacker=' + attacker;
	url+= '&defender=' + defender;
	url+= '&actionID=' + actionID;
	//document.write(url);
	//alert(url);
	attXmlHttp.onreadystatechange=attemptToTarget_Complete;
	attXmlHttp.open("GET",url,true)
	attXmlHttp.send(null) 	
}




function attemptToTarget_Complete()
{
	if (attXmlHttp.readyState==4)
	{ 
		;//future use
	}
}



/***************************************************************************
*
*                   Animation
*
***************************************************************************/
var fadeAnimationCounter = 0;
function showUnitAnimation(unitID, imagePath)
{
	var animation = document.getElementById("animation");
	var unit = document.getElementById("unit_" + unitID);
	
	animation.style.top = getTop(unit);
	animation.style.left = getLeft(unit);
	animation.src=imagePath;

	fadeAnimationCounter = 0;
	fadeAnimation();
}

function fadeAnimation()
{		
	if(fadeAnimationCounter < 8)
	{
		//alert(fadeDamageCounter);
		animTimer = setTimeout("fadeAnimation()",500);
	}	
	else
	{
		var animation = document.getElementById("animation");
		animation.style.top = -100;
		animation.style.left = -100;
	}
}


/*****************************************************************************************

	Damage

*****************************************************************************************/
var fadeUnitMsgCounter = 0;
function showUnitMsg(unitID, msg)
{
	var d = document.getElementById("unitMsg");
	d.innerHTML = msg;
	
	var unit = document.getElementById("unit_" + unitID);
	d.style.top = getTop(unit) + 10;//+80;
	d.style.left = getLeft(unit) + 25;//+245;
	//alert("Unit msg:" + unitID + " : " + msg);
	fadeUnitMsgCounter = 0;
	fadeUnitMsg();
}



function fadeUnitMsg()
{
	var d = document.getElementById("unitMsg");
	d.style.top = getTop(d)-2;
	d.style.left = getLeft(d);
	fadeUnitMsgCounter++;
	
	if(fadeUnitMsgCounter < 15)
	{
		setTimeout("fadeUnitMsg()",100);
	}	
	else
	{
	
		var animation = document.getElementById("animation");
		d.style.top = -1000;
		d.style.left = -1000;
		animation.style.top = -1000;
		animation.style.left = -1000;
	}
}



/***************************************************************************
*
*                   Update Units
*
***************************************************************************/

function updateUnits()
{
	updXmlHttp=GetXmlHttpObject();
	
	if (updXmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	//alert("updating units");
	url = 'actions/updateUnits.php?action=updateUnits';
	
	updXmlHttp.onreadystatechange=updateUnits_Complete;
	updXmlHttp.open("GET",url,true)
	updXmlHttp.send(null) 	
}

function updateUnits_Complete() 
{ 
    if (updXmlHttp.readyState==4)
    { 
        fullText  = updXmlHttp.responseText;
        
	var data1 = fullText.split("::");
	
	for(i=0;i<data1.length;i++)
	{
		
		var data2 = data1[i].split(",");
		unitID 		= data2[0];
		life 		= data2[1];
		movement 	= data2[2];
		range 		= data2[3];
		str 		= data2[4];
		def		= data2[5];
		unitName	= data2[6];
		waitTime	= data2[7];
		imgName		= data2[8];
	
		if(unitID > 0)
		{
			//alert(unitID);
			var objUnit = getUnitDiv(unitID);
			if(objUnit != null)
			{
				objUnit.setAttribute('life',life);
				if(waitTime > 0)
				{
					setWaitTime(unitID, waitTime);
					showWaitTime(unitID);
				}
				else
				{
					setWaitTime(unitID, 0);
					hideWaitTime(unitID);
				}
			}
		}
	}
    }
}




/***************************************************************************
*
*                   Kill Unit
*
***************************************************************************/
function killUnit(unitID)
{
	var unit = document.getElementById("unit_" + unitID);
	//alert(x + ',' + y);
	if(unit != null)
	{
		unit.setAttribute('tileX',-1);
		unit.setAttribute('tileY',-1);
		unit.style.top = "-1000px";
		unit.style.left = "0px";
	
		if(selectedUnitID == unitID)
		{
			selectedUnitID =0;
		}
	}
}

function setRange(unitID, range)
{
	var unit = document.getElementById("unit_" + unitID);
	unit.setAttribute("range",range);
}


function showUnitMenu()
{
	
	if(selectedUnitID > 0)
	{
		mnuXmlHttp=GetXmlHttpObject();
	
		if (mnuXmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		} 
		url = 'widgets/divUnitMenu.php?unitID=' + selectedUnitID;
		mnuXmlHttp.onreadystatechange=showUnitMenu_Complete;
		mnuXmlHttp.open("GET",url,true);
		mnuXmlHttp.send(null);
	
		var t = document.getElementById("unitMenu");
		t.style.visibility = "visible";
		var unit = document.getElementById("unit_" + selectedUnitID);
		t.style.top = getTop(unit) + 60;
		t.style.left = getLeft(unit) + 55;
	}
}


function showUnitMenu_Complete() 
{ 
    if (mnuXmlHttp.readyState==4)
    { 
    	var t = document.getElementById("unitMenu");
        t.innerHTML = mnuXmlHttp.responseText;
    }
}

function hideUnitMenu()
{
	var t = document.getElementById("unitMenu");
	t.style.visibility = "hidden";
}

function closeUnitMenu()
{
	hideUnitMenu();
}

var lastTmpUnit = -1;
var newUnitIDs = "";
function deployNewUnit(unitID, x, y)
{
	
	lastTmpUnit++;
	
	var tmpUnitWait = document.getElementById("tmpUnitWait_" + lastTmpUnit);
	tmpUnitWait.setAttribute("ID","unitWait_" + unitID);
	tmpUnitWait.id = "unitWait_" + unitID;
	tmpUnitWait.style.visibility = "hidden";
	
	var tmpUnitImg = document.getElementById("tmpImg_" + lastTmpUnit);
	tmpUnitImg.setAttribute("ID","unitImg_" + unitID);
	
	var tile = document.getElementById("tile_" + x + "_" + y);
	
	var tmpUnit = document.getElementById("tmpUnit_" + lastTmpUnit);
	tmpUnit.setAttribute("ID","unit_" + unitID);
	tmpUnit.id = "unit_" + unitID;
	tmpUnit.style.left = getLeft(tile);
	tmpUnit.style.top = getTop(tile) - 55;
	tmpUnit.setAttribute('tileX',x);
	tmpUnit.setAttribute('tileY',y);
	
	deployXmlHttp=GetXmlHttpObject();
	
	if (deployXmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	//alert("updating units");
	url = 'actions/updateUnits.php?action=updateUnits&unitID=' + unitID;
	//document.write(url);
	//alert(url);
	deployXmlHttp.onreadystatechange=deployNewUnit_Complete;
	deployXmlHttp.open("GET",url,true);
	deployXmlHttp.send(null);
	
	
}

function deployNewUnit_Complete() 
{ 
    if (deployXmlHttp.readyState==4)
    { 
        fullText  = deployXmlHttp.responseText;
        
	var data1 = fullText.split("::");
	//alert(fullText);
	var data = data1[0].split(",");
	
	unitID 		= data[0];
	life 		= data[1];
	movement 	= data[2];
	range 		= data[3];
	str 		= data[4];
	def		= data[5];
	unitName	= data[6];
	waitTime	= data[7];
	imgName		= data[8];

	if(lastTmpUnit==0)
	{
		newUnitIDs =unitID;	
	}
	else
	{
		newUnitIDs+="," + unitID;	
	}
	//alert("UnitID:" + newUnitIDs);
	var objUnit = document.getElementById("unit_" + unitID);
	objUnit.setAttribute("isOwner","N");
	objUnit.setAttribute("movement",movement);
	objUnit.setAttribute("range" , range);
	objUnit.setAttribute("waitTime","0");
	objUnit.setAttribute("life", life);
	objUnit.setAttribute("str", str);
	objUnit.setAttribute("def", def);
	objUnit.setAttribute("unitName",unitName);
	objUnit.setAttribute("unitID", unitID);
	objUnit.setAttribute("hasSpecials", "N");
	objUnit.setAttribute("canMove","Y");
	objUnit.setAttribute("canTarget", "Y");
	imgHtml = "<img";
	imgHtml+= " src='images/unitImgs/1_" + imgName + ".gif' ";
	imgHtml+= " id='unitImg_" + unitID + "'";
	imgHtml+= " border=0 ";
	imgHtml+= ">";
	objUnit.innerHTML = imgHtml;
	
    }
}



function forceSpawn()
{
	//alert("Refreshing Chat");
	spawnXmlHttp=GetXmlHttpObject();
	
	if (spawnXmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	url = 'actions/spawn.php?clearWorld=Y';
	//document.write(url);
	spawnXmlHttp.open("GET",url,true)
	spawnXmlHttp.send(null);
}



function sayIt()
{
	//alert("here");
	sayXmlHttp=GetXmlHttpObject();
	
	if (sayXmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	url = 'actions/generalActions.php?action=sayIt&msg=' + document.getElementById("chatMsg").value;
	//url+= '&roomID=' + document.getElementById("worldID").value;
	//document.write(url);
	sayXmlHttp.onreadystatechange=sayIt_Complete;
	sayXmlHttp.open("GET",url,true)
	sayXmlHttp.send(null);
	document.getElementById("chatMsg").value = "";	
}

function sayIt_Complete()
{
	if (sayXmlHttp.readyState==4)
	{ 
		fullText  = sayXmlHttp.responseText;
		refreshChat();
	}
}

function nextTutorial(add)
{
	//alert("here");
	tutXmlHttp=GetXmlHttpObject();
	
	if (tutXmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	url = 'actions/generalActions.php?action=nextTutorial&add=' + add;
	//document.write(url);
	tutXmlHttp.onreadystatechange=nextTutorial_Complete;
	tutXmlHttp.open("GET",url,true)
	tutXmlHttp.send(null);
	
}

function nextTutorial_Complete()
{
	if (tutXmlHttp.readyState==4)
	{ 
		fullText  = tutXmlHttp.responseText;
		var t = document.getElementById("tutorial");
		t.innerHTML = fullText;
		//	t.style.visibility = "visible";
	}
}


function openTutorial()
{
	//nextTutorial(0);
	var t = document.getElementById("tutorial");
	t.style.visibility = "visible";
	
}


function closeTutorial()
{
		var t = document.getElementById("tutorial");
		t.style.visibility = "hidden";

}

function openPopupAd()
{
	var t = document.getElementById("popupAds");
	t.style.visibility = "visible";
}

function closePopupAd()
{
	var t = document.getElementById("popupAds");
	t.style.visibility = "hidden";
	
}

function openUpgrades()
{
	if(selectedUnitID > 0)
	{
		openUpgradesForUnit(selectedUnitID);
		closeUserPanel();
	}
}
function openUpgradesForUnit(unitID)
{
	if(unitID > 0)
	{
		upgXmlHttp=GetXmlHttpObject();

		if (upgXmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		} 

		url = "widgets/displayUpgradeList.php?unitID=" + unitID;
		//document.write(url);
		upgXmlHttp.onreadystatechange=openUpgrades_Complete;
		upgXmlHttp.open("GET",url,true)
		upgXmlHttp.send(null);
		setDisplay(unitID);
		closeUserPanel();
	}
}

function openUpgrades_Complete()
{
	if (upgXmlHttp.readyState==4)
	{ 
		fullText  = upgXmlHttp.responseText;
		var t = document.getElementById("upgrades");
		t.innerHTML = fullText;
		showPanel("upgradesWrapper");
	}
}


function closeUpgrades()
{
	var t = document.getElementById("upgrades");
	t.style.visibility = "hidden";
	
}

function showPanel(divID)
{
	var t = document.getElementById(divID);
	t.style.visibility = "visible";

}

function hidePanel(divID)
{
	var t = document.getElementById(divID);
	t.style.visibility = "hidden";
}


function openPanelByTitle(title)
{
	if(selectedUnitID > 0)
	{
		panelXmlHttp=GetXmlHttpObject();

		url = "widgets/divUserPanel.php?panelTitle=" + title;
		url+= "&unitID=" + selectedUnitID;
		//document.write(url);
		panelXmlHttp.onreadystatechange=openUserPanel_Complete;
		panelXmlHttp.open("GET",url,true)
		panelXmlHttp.send(null)
	}
}


function openGoal(goalID)
{
	panelXmlHttp=GetXmlHttpObject();

	url = "widgets/divUserPanel.php?goalID=" + goalID;
	panelXmlHttp.onreadystatechange=openUserPanel_Complete;
	panelXmlHttp.open("GET",url,true)
	panelXmlHttp.send(null);
	
}

function openUserPanel_Complete()
{
	if (panelXmlHttp.readyState==4)
	{ 
		fullText  = panelXmlHttp.responseText;
		var t = document.getElementById("userPanel");
		t.innerHTML = fullText;
		showPanel("userPanel");
	}
}


function fadeObject(obj)
{
	obj.style.opacity = .5;
	obj.style.filter = 'alpha(opacity=50)';
}

function fadeUnit(unitID)
{
	if(unitID > 0)
	{
		var objUnit = document.getElementById("unit_" + unitID);
		fadeObject(objUnit);
	}	
}

lastDisplayActionID = 0;
function displayAction(actionID)
{
	//alert(actionID);
	if(lastDisplayActionID > 0)
	{
		//document.getElementById("actionDisplay_" + lastDisplayActionID).style.visibility="hidden";
	}
	
	if(actionID > 0)
	{
		document.getElementById("actionDisplay_" + actionID).style.visibility="visible";
		lastDisplayActionID = actionID;
	}
	else
	{
		document.getElementById("actionDisplay_" + lastDisplayActionID).style.visibility="hidden";
	}
}

function displayCompleted()
{
	panelXmlHttp=GetXmlHttpObject();

	url = "widgets/divWorldCleared.php";
	//alert(url);
	panelXmlHttp.onreadystatechange=displayCompleted_Complete;
	panelXmlHttp.open("GET",url,true)
	panelXmlHttp.send(null);
	
}

function displayCompleted_Complete()
{
	if (panelXmlHttp.readyState==4)
	{ 
		fullText  = panelXmlHttp.responseText;
		openMsgWindow(panelXmlHttp.responseText);		
	}
}


