//ajax object creation function
function createRequestObject() {
   var req;
   try {
		req=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			req=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (oc) {
			req=null;
		}
	}
	if(!req && typeof XMLHttpRequest != "undefined")
		req = new XMLHttpRequest();
	if (!req)
		alert("Could not create connection object.");
   return req;
}

var activeSkill=1;
var itemRequest = createRequestObject();
var mouseX;
var mouseY;
var helpMessages=new Array(
	"This is a test of the help system.  Had this been real help, it'd be a lot more useful",
	"Your hometown is where you can sleep, and replenish your supply of water, which is vital to surviving in the vaste wastelands.",
	"The town hall is where you can buy and sell land from the town, and provide money and resources for the town's growth.",
	"The hotel is where you sleep, and replenish water.  Both are free early on, but soon they cost a daily amount.",
	"The bank stores your money, where it's safe from theives that scour the wastelands looking for undefended people.",
	"Accuracy measures how often you hit a target, the base value being 40%",
	"Melee Weapons is how much damage you do to a target, the total being x/5.",
	"Unarmed Fighting is how much damage you do to a target, the total being x/5.",
	"Repair/Build is how effective you are at repairing and constructing buildings.  A higher value equals better repairs, and cheaper buildings.",
	"Item Crafting is how skilled you are at combining items.  Certain combinations require a minimum skill level.",
	"Salesman is how good you are at bartering.  A high salesman level will buy and sell to NPCs for cheaper.",
	"First Aid is an actived skill.  Higher values will heal more HP, the total being x/5+5.",
	"Search is how good you are at finding items hidden in the wastelands.  Higher skills will result in better items found more often."
	)

function selectSkill(obj)
{
	if(activeSkill!=1)
	{
		activeSkill.className="";
	}
	if(activeSkill===obj)
	{
		obj.className="";
		activeSkill=1;
		document.skills.activeSkill.value=0;
		return;
	}
	obj.className="active";
	activeSkill=obj;
	switch(activeSkill.innerHTML)
	{
		case "First Aid":
			document.skills.activeSkill.value=1;
			break;
		case "Search":
			document.skills.activeSkill.value=2;
			break;
		default:
			document.skills.activeSkill.value=0;
			break;
	}
}

//Used to prevent forms on the AJAX frames from refreshing the page
function fake() {
	return false;
}

function help(event, onWhat, custom)
{
	var hoverDiv=document.getElementById("hover");
	switch(onWhat)
	{
		case "custom":
			hoverDiv.innerHTML=custom;
			break;
		case "search":
			hoverDiv.innerHTML=helpMessages[12];
			break;
		case "firstaid":
			hoverDiv.innerHTML=helpMessages[11];
			break;
		case "salesman":
			hoverDiv.innerHTML=helpMessages[10];
			break;
		case "itemCraft":
			hoverDiv.innerHTML=helpMessages[9];
			break;
		case "itemRepair":
			hoverDiv.innerHTML=helpMessages[8];
			break;
		case "unarmed":
			hoverDiv.innerHTML=helpMessages[7];
			break;
		case "melee":
			hoverDiv.innerHTML=helpMessages[6];
			break;
		case "accuracy":
			hoverDiv.innerHTML=helpMessages[5];
			break;
		case "bank":
			hoverDiv.innerHTML=helpMessages[4];
			break;
		case "hotel":
			hoverDiv.innerHTML=helpMessages[3];
			break;
		case "town_hall":
			hoverDiv.innerHTML=helpMessages[2];
			break;
		case "hometown":
			hoverDiv.innerHTML=helpMessages[1];
			break;
		case "test":
		default:
			hoverDiv.innerHTML=helpMessages[0];
			break;
	}
	//alert(event.clientX-100);
	hoverDiv.style.left = (event.clientX-100) + 'px';
	hoverDiv.style.top = (event.clientY+20) + 'px';
	//hoverDiv.style.width = hoverDiv.innerHTML.length*10;
	hoverDiv.style.display = 'inline';
	hoverDiv.style.zIndex = '99';
}

function closeDiv(id)
{
	hoverDiv=document.getElementById(id);
	hoverDiv.style.left = '0px';
	hoverDiv.style.top = '0px';
	hoverDiv.style.display = 'none';
}

/*
Functions for changing the item status panel
*/
function changeItem(){
    if (itemRequest.readyState == 4){
        statusDiv = document.getElementById('item_info');
        if (itemRequest.status == 200){
			statusDiv.style.left = mouseX-100 + 'px';
			statusDiv.style.top = mouseY+20 + 'px';
			statusDiv.style.display = 'inline';
			statusDiv.style.zIndex = '99';
            statusDiv.innerHTML = itemRequest.responseText;
        } else {
            statusDiv.innerHTML = "Error: Status "+itemRequest.status;
        }
    }
}

//show item function
function show_item(event, item)
{
	url = document.location.href;
	xend = url.lastIndexOf("/") + 1;
	base_url = url.substring(0, xend) + "showItem.php?id=";
	mouseX=event.clientX;
	mouseY=event.clientY;
	if (window.ActiveXObject) {
		itemRequest = createRequestObject();
	}
	new_url = base_url + item;
	itemRequest.onreadystatechange = changeItem;
	itemRequest.open("GET", new_url, true);
	itemRequest.send(null);
}

function close_item() {
	document.getElementById('item_info').style.visibility = "hidden";
	document.getElementById('item_info').innerHTML = "";
}