var ATimer = 0;
var fightstart = 0;
var fightend = 0;
var COTGTrackingID = COTGTrackingID;
var PatchDirty = 0;
var MessageCount = 0;
var widescreen = widescreen;
var rps = 0;
var Scratched = 0;
var ChatC = 0;
var ErrorWindow = null;
var UseShortcuts = 1;
var keyhandle = 0;
var Running = false;
var autopiloton = 0;
var LastError = "";
var PMArray = new Array();
var Messages = new Array();
var LogWindow = null;
var PMObj = new Array();
var windowid = 0;
var LastTrade = 0;
var LastCombatItem = 0;
var GuildID = 0;
var LogCounter = 0;
var PartyID = 0;
var petfightt = 0;
var faiths = new Array('Blank', 'None','White Order', 'Red Covenant', 'Order of the Blade', 'Keeper of Secrets', 'Guardians of the Woods', 'Entertainers Circle');
var story = new Array('None', 'Noble','Peasant', 'Gypsy', 'Explorer', 'Intellect');
var hidetimer = 0;
var Referral = Referral;
var gameversion = gameversion;
var FacebookID = 0;
var Slots = new Array();
var LastNPC = 0;
var fetching = 0;
var appeartimer = 0;
var appearptimer = 0;
var nametimer = 0;
var nameok = 0;
var ButtonTimer2 = 0;
var ButtonTimerEnd  = -1;
var ButtonInterval2 = 0; 
var actiontimeout = -1;
var actiontimeout2 = -1;
var RegionName = '';
var RegionID = 0;
var widgets = 0;
var chatrefresht = 0;
var ChatPrefix = '';
var justSent = 0;
var FindPlayer = 0;
var ajaxget;
var xmlobj;
var ajaxparty;
var Done = -1;
var success = 0;
var fight = new Array();
var gamelive = 1;
fight[1]= 0;
fight[2]= 0;
var refreshChat = 0;
var PlayerCount = 0;
var Players = new Array();
var rPlayers = new Array();
var Powers = new Array();
var chat = new Array()
var Rooms = new Array();
var PageLoaded = false;

if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
	//Using Opera browser so use work around
	setTimeout("StartGame()", 2500);
} else {
	window.onload = function anonymous() {StartGame()};
	window.onunload = function anonymous() {Running = false; TidyWindows()};
}


function StartGame() {
xmlobj = getXmlHttpRequestObject();
ajaxget = getXmlHttpRequestObject();
ajaxparty = getXmlHttpRequestObject();
PageLoaded = true;
if (getObj('ChatBox2p') != null) {
	getObj('ChatBox2p').style.display = 'none';
}
setTimeout('PageStart();', 500);
}

function ChatMenu() {
	SplitPageBox();
	SetMainBox('<div id=ChatWindow style="background-image: URL(images/boxscreen.png); width: 350; height: 260; background-repeat: no-repeat;"></div>');
}

function PlanState(a) {
	var strout = '';
	if (a != 0) {
		strout = '<img src=\'images/circle' + (4 + a) + '.png\' width=12 height=12>'
	} else {
		
		strout = '<img src=\'images/circle0a.png\' width=12 height=12>'
	}
	return strout;
}

function TogglePlan(p) {
	PlayAction(13,p, 0, 0, 0);	
}

function Website(pagex) {
	sendRequest(pagex);
}

function CraftItem(i) {
	PlayAction(14,1, i, 0, 0);	
}

function ASCIIV(text, c, f) {
	var strout = '' + ASCII(text,c, f) + '<img src="images/gui/coin.png" width=12 height=12>';
	return strout;
}

function Craft(s, a, s2) {
	if (a == null) {
		a = 0;
	}
	PlayAction(11,a, s, 0, 0,  URLencode(s2));
}

function Plans(s, a, s2) {
	if (a == null) {
		a = 0;
	}
	PlayAction(12,a, s, 0, 0,  URLencode(s2));
}

function JournalButtons() {
	var strout = '';
	strout = '<table style=\'background-image: URL(images/scene6.png); width: 350px;\'><tr><td>' + Adac('HomeMenu(1);','Stats','','buttonc','stats.png', null) + '</td><td>' + Adac('HomeMenu(2);','Skills','','buttonc','skills.png', null) + '</td><td>' + Adac('HomeMenu(7);','Crafting','','buttonc','crafting.png', null) + '</td><td>' + Adac('PlayAction(1006,0, 1, 1, 0);','Equipment','','buttonc','equipment.png', null) + '</td><td>' + Adac('PlayAction(1001,0, 1, 1, 0);','Items','','buttonc','items.png', null) + '</td><td>' + Adac('HomeMenu(11);','Quests','','buttonc','quests.png', null) + '</td><td>' + Adac('HomeMenu(12);','Story','','buttonc','story.png', null) + '</td></tr></table>';
	return strout;
}

function ToggleChat(x) {
	getObj('ChatBoxp').style.display = (x == 1 ? '' : 'none');
	getObj('ChatBox2p').style.display = (x == 1 ? 'none' : '');
	getObj('LogBoxp').style.display = 'none';
}

function ToggleChatShort(x) {
	getObj('ChatBoxp').style.display = (x == 1 ? '' : 'none');
	getObj('ChatBox2p').style.display = (x == 1 ? 'none' : '');
}

function MenuLoad() {
if (getObj('rcsbutton7') == null) {
	getObj('menubar').innerHTML = '<table id="rcsbuttonbox" cellpadding=1 cellspacing=1 height=24><tr height=24><td id="rcsbutton1" style="width: 70px;">' + Adat('ExploreMenu();','Home','Home') + '</td><td id="rcsbutton2" style="width: 70px;">' + Adat('HomeMenu();','Journal','Journal') + '</td><td id="rcsbutton3" style="width: 70px;">' + Adat('TravelMenu();','Travel','Travel') + '</td><td id="rcsbutton4" style="width: 70px;">' + Adat('FollowerMenu();','Followers','Followers') + '</td><td id="rcsbutton5" style="width: 70px;">' + Adat('CommunityMenu();','Community','Community') + '<form method=\"post\" target=\"new\" action=\"http://www.forlorngames.com/default.asp\" name=\"forumsr\" style=\"margin: 0px;\"><input type=hidden id=accountid name=accountid value=\"" + accountid + "\"><input type=hidden id=gameid name=gameid value=\"3\"></form></td><td id="rcsbutton7" style="width: 70px;">' + Adat('LogoutMenu();','Logout','Logout') + '</td></tr></table>';
	getObj('mappy').style.display = '';
	RefreshMappy();
}
}

function TradeCompleted() {
	LastTrade = 0;
	SetMainBox('<table width=\'350\' cellpadding=1 cellspacing=1 style=\'background-image: URL(images/boxscreen.png); background-position: top left; background-repeat: no-repeat;\'><tr><td><div style=\'height: 280px; overflow: auto;\'>' + ASCII('Trade completed successfully') + '</td></td></tr></table>');
}

function MenuUnload() {
var l = getObj('statbox');
if (l != null) {
	l.innerHTML = '';
	l = null;
}
var l = getObj('ChatButtons');
if (l != null) {
	l.innerHTML = '';
	l = null;
}
if (getObj('rcsbuttonbox') != null) {
	getObj('menubar').innerHTML = '';
	getObj('mappy').style.display = 'none';
}
}

function AddChat(text, chattype, pmid, ccolor) {
		
	if (chattype == null) {
		chattype = 0;
	}
	if (ccolor == null) {
		ccolor = 0;
	}
	if (text.substr(0, 5) != '<div>') {
		text = '<div ' + (ccolor > 1 ? ' style=\'color:' + getAColor(ccolor) + '\'' : '') + '>' + text + '</div>';
	}
	chat[chat.length] = new UpdateChat(text, chattype, pmid);
	if (chattype != 0) {
		//Place a delay on refreshing chat to reduce spammage.
		clearTimeout(refreshChat);
		refreshChat = setTimeout('ShowChat();', 150);
	}
}

function AddLog(text, chattype, pmid, ccolor) {
	getObj('LastLog').innerHTML = '<center style=\'background-image: URL(maskbg.png);\'>' + ASCII(text) + '</center>';
	if (text.indexOf(':void') >= 0) {
		AddChat(ASCII(text,ccolor), 5, null, ccolor);
	} else {
		if (PushIt(ASCII(text,ccolor)) == 0) {
			LogCounter = LogCounter + 1;
			if (LogCounter > 300) {
				LogCounter = 0;
				ClearLog();
			}

			var myElement = document.createElement("DIV");
			myElement.innerHTML = ASCII(text,ccolor) + "";
			myElement.id = "LastLogLine" + LogCounter

			if (getObj("LastLogLine" + (LogCounter - 1)) == null) {
				getObj('LogContents').appendChild(myElement); 
			} else {
				getObj('LogContents').insertBefore(myElement, getObj("LastLogLine" + (LogCounter - 1)));
			}
		}
	}
}

function ClearLog() {
	if (getObj('LogContents') != null) {
		getObj('LogContents').innerHTML = '';
	}
}

function ClearChat() {
	if (getObj('ChatContents') != null) {
		getObj('ChatContents').innerHTML = '';
	}
}

function FaithChanged(faithid, faithname) {
	var cp = Players[1];
	if (cp != null) {	
		cp.faithid = faithid;
		cp.PlayerTitle = faithname;
		cp = null;
	}
	RefreshStatBox();
}

function NPCInteract(p) {
	LastNPC = p;
	PlayAction(6,0, p, 0, 0);
}

function NPCSteal(p, cooldown) {
	if (cooldown == null) {
		cooldown = 6;
	}
	LastNPC =  p;
	NPCAction(3, 3, 1, p);
}

function NPCBeg(p, cooldown) {
	if (cooldown == null) {
		cooldown = 6;
	}
	LastNPC =  p;
	NPCAction(3, 3, 2, p);
}

function UpdateWB(t) {
	getObj('worldbuttons').innerHTML = ASCII(t);
}

function NPCAction(q, mq, a, p) {
	//resource gathering
	if (getObj('NPCButtonBox' + p) != null) {
		getObj('NPCButtonBox' + p).innerHTML = '<table cellpadding=0 cellspacing=0><tr><td>' + ASCII((a == 1 ? 'Theft in progress ... ' : (a == 4 ? 'Digging in progress ... ' : (a == 5 ? 'Deciphering in progress ... ' : (a == 6 ? 'Lockpicking in progress ... ' : (a == 10 ? 'Revealing in progress ... ' : 'Begging in progress ... ')))))) + '</td><td>' + GetBox2(q * 2 + 's remaining', 'bigred', q, mq, 150) + '</td></tr></table>'
		clearTimeout(actiontimeout);
		if (q > 0) {
			actiontimeout = setTimeout('NPCAction(' + (q - 1) + ', ' + mq + ',' + a + ',' + p + ');' + (q == 1 ? 'PlayAction(7, ' + a + ', ' + p + ');' : '') + '', 2000);
		}
	}
}

function ActionTimer(text, js, q, mq) {
	if (getObj('worldbuttons') != null) {
		getObj('worldbuttons').innerHTML = '<table cellpadding=0 cellspacing=0><tr><td>' + ASCII(text) + '</td><td>' + GetBox2((q * 2) + 's remain', 'bigred', q, mq, 150) + '</td></tr></table>'
		clearTimeout(actiontimeout2);
		if (q > 0) {
			actiontimeout2 = setTimeout('ActionTimer(\'' + text + '\',\'' + js + '\',' + (q - 1) + ', ' + mq + ');' + (q == 1 ? js : '') + '', 500);
		}
	}
}


function GetTimer(t) {
	var s = 0;
	var cp =Players[1];
	if (cp != null) {
		s = GetStat(cp, 59)
		if (s != null) {
			t = t - s;
		}
	}
	if (t <= 0) {
		t = 1;
	}
	cp = null;
	return t;
}

function TimedButton(x, y, z, p, t, size) {
	ButtonInterval2 = 0;
	Spacer2('spacer', size);
	if (z == null) {
		z = 1;
	}
	if (p == null) {
		p = 1;
	}
	ButtonTimer2 = setInterval('Spacer2(\'spacer\', ' + size + ');', (t / 2));
	ButtonTimerEnd = setTimeout('clearInterval(ButtonTimer2); PlayAction(' + x + ', ' + y + ',' + z + ', ' + p + ', 1);clearTimeout(ButtonTimerEnd);ButtonTimerEnd = -1;', (t * 5));
}

function CheckTreasure() {
	var strout = '';
	var cp = Players[1];
	if (cp != null) {
		if (cp.Skills[36].SkillValue > 0) {
			strout += '<tr><td colspan=2><table width=350 style="background-image: URL(images/scene6.png); height: 50px; background-repeat: no-repeat;" cellpadding=0 cellspacing=0><tr><td width=340 style="padding: 2px; padding-top: 6px;" valign=bottom>' + ASCII('You notice the ground has been disturbed.') + '</td></tr><tr><td id=NPCButtonBox2>' + Adar('TreasureSeek();','Dig','Dig','button5','normal6.png', 1) + '</td></tr></table></td></tr>';
		}
	}
	return strout;
}

function CheckChest() {
	var strout = '';
	var cp = Players[1];
	if (cp != null) {
		if (cp.Skills[75].SkillValue > 0) {
			strout += '<tr><td colspan=2><table width=350 style="background-image: URL(images/scene6.png); height: 50px; background-repeat: no-repeat;" cellpadding=0 cellspacing=0><tr><td width=340 style="padding: 2px; padding-top: 6px;" valign=bottom>' + ASCII('You discover a locked treasure chest.') + '</td></tr><tr><td id=NPCButtonBox3>' + Adar('ChestOpen();','Pick','Pick','button5','normal6.png', 1) + '</td></tr></table></td></tr>';
		}
	}
	return strout;
}

function CheckDialect() {
	var strout = '';
	var cp = Players[1];
	if (cp != null) {
		if (cp.Skills[74].SkillValue > 0) {
			strout += '<tr><td colspan=2><table width=350 style="background-image: URL(images/scene6.png); height: 50px; background-repeat: no-repeat;" cellpadding=0 cellspacing=0><tr><td width=340 style="padding: 2px; padding-top: 6px;" valign=bottom>' + ASCII('You see an ancient symbol.') + '</td></tr><tr><td id=NPCButtonBox4>' + Adar('Decipher();','Read','Read','button5','normal6.png', 1) + '</td></tr></table></td></tr>';
		}
	}
	return strout;
}

function CheckShadow() {
	var strout = '';
	var cp = Players[1];
	if (cp != null) {
		if (cp.Skills[39].SkillValue > 0) {
			strout += '<tr><td colspan=2><table width=350 style="background-image: URL(images/scene6.png); height: 50px; background-repeat: no-repeat;" cellpadding=0 cellspacing=0><tr><td width=340 style="padding: 2px; padding-top: 6px;" valign=bottom>' + ASCII('You notice an odd shadow on the ground.') + '</td></tr><tr><td id=NPCButtonBox10>' + Adar('ShadowSeek();','Reveal','Reveal','button5','normal6.png', 1) + '</td></tr></table></td></tr>';
		}
	}
	return strout;
}

function ShadowSeek() {
	NPCAction(GetTimer(3), GetTimer(3), 10, 10);
}

function TreasureSeek() {
	NPCAction(GetTimer(3), GetTimer(3), 4, 2);
}

function Decipher() {
	NPCAction(GetTimer(3), GetTimer(3), 5, 4);
}

function ChestOpen() {
	NPCAction(GetTimer(3), GetTimer(3), 6, 3);
}

function Spacer2(divx, size) {
	if (size == null) {
		size = 200;
	}
	if (getObj(divx) == null) {
		clearInterval(ButtonTimer2);
	} else {
		if (ButtonInterval2 >= 10) {
			clearInterval(ButtonTimer2);
			ButtonInterval2 = 10;
		}
		clearInnerHTML(getObj(divx));
		getObj(divx).innerHTML = '' + PercentBoxSmall(size, ButtonInterval2 * 10, 'bigblue', ASCII((ButtonInterval2 * 10) + '%')) + '';	
		ButtonInterval2 = ButtonInterval2 + 1;	
	}
}


function InventoryDirty(i, q) {
	if (getObj('inventoryscreen') != null) {
		var iobj = getObj('i' + i);
		if (iobj != null) {
			iobj.style.display = 'none';
		}
		var iobj = null;
				
	}
}

function AddNPCButtons(p, pn, qc, ab, as, it) {
	var strout = '';
	if (it == 3) {
		if (qc > 0) {
			strout += '' + Adar('LastNPC = ' + p + ';PlayAction(10001,0, ' + p + ', 0, 0);','Take Quests from ' + pn + '',qc + ' Quests','button5','normal6.png', 1);
		}
	} else {
		if (it == 5) {
			strout = strout + Adar('LastNPC = ' + p + ';PlayAction(4000,0, ' + p + ', 0, 0);','Talk to ' + pn + '','Auctions','button5','normal5.png', 1);
		} else if (it == 15) {
			strout += '' + Adar('LastNPC = ' + p + ';PlayAction(3005,0, 0, 0, 0);','Access your storage','Storage','button5','normal6.png', 1);
		} else if (it == 18) {
			strout += '' + Adar('LastNPC = ' + p + ';PlayAction(30051,0, 0, 0, 0);','Access your guild storage','Storage','button5','normal7.png', 1);
		} else if (it == 16) {
			strout += '' + Adar('LastNPC = ' + p + ';PlayAction(5001,0, 0, 0, 0);','Complete Job','Complete Job','button5','normal6.png', 1);
			strout += '' + Adar('LastNPC = ' + p + ';PlayAction(5000,0, 0, 0, 0);','Create Job','Create Job','button5','normal6.png', 1);
			strout += '' + Adar('LastNPC = ' + p + ';PlayAction(5002,0, 0, 0, 0);','Manage your jobs','Manage','button5','normal6.png', 1);
		} else {
			strout = strout + Adar('NPCInteract(' + p + ');','Talk to ' + pn + '',(it == 7 ? 'Buy' : (it == 4 ? 'Train' : (it == 23 ? 'Improve' : 'Talk'))),'button5','normal5.png', 1);
		}
		if (it == 55) {
			strout += '' + Adar('LastNPC = ' + p + ';PlayAction(5001,0, 0, 0, 0);','Complete Job','Complete Job','button5','normal6.png', 1);
		}
		if (it == 7) {
			strout += '' + Adar('LastNPC = ' + p + ';PlayAction(10006,0, ' + p + ', 0, 0);','Sell items to ' + pn + '','Sell','button5','normal6.png', 1);
		}
		if (qc > 0) {
			strout += '' + Adar('LastNPC = ' + p + ';PlayAction(10001,0, ' + p + ', 0, 0);','Take Quests from ' + pn + '',qc + ' Quests','button5','normal6.png', 1);
		}
	}
	var cp = Players[1];
	if (cp.Skills[10].SkillValue > 0 && as != 0) {
		strout += '' + Adar('NPCSteal(' + p + ');','Steal from ' + pn + '','$','sbutton1','snormal2.png', 1)
	}
	if (cp.Skills[11].SkillValue > 0 && ab != 0) {
		strout += '' + Adar('NPCBeg(' + p + ');','Beg from ' + pn + '','<img src="images/gui/coins.png" width=12 height=12>','sbutton1','snormal2.png', 1)
	}
	if (strout == '') {
		strout += '' + ASCII('<i>*' + pn + ' is daydreaming*</i>');
	}
	cp = null;
	return strout;
}

function MoneyChanged(mp) {
	var cp = Players[1];
	if (cp != null) {	
		cp.LegendPieces += mp;
		cp = null;
	}
	RefreshStatBox();
}

function SetSkill(PlayerID, SkillID, SkillValue, SkillMax) {
	AddPS(PlayerID, SkillID, SkillValue, SkillMax, 0);
}

function UpdateChat(text, chattype, pmid) {
this.chat = text;
this.chattype = chattype;
this.pm = pmid;
}

function UpdatePMS(text, pmid) {
this.chat = text;
this.pm = pmid;
}


function ShowChat(c) {
	var x = 0;
	var y = 0;
	var yok = 0;
	var match = 0;
	var match2 = 0;
	if (c == null) {
		c = 1;
	}
	var strout = '';
	var strout2 = '';
	var chatopen = 0;
	var statusopen = 0;
	var matches = 20;
	var pms = new Array();
	if (getObj('ChatContents') != null || getObj('ChatWindow') != null) {
		chatopen = 1;
	}

	if (chatopen == 1) {
		for (x = chat.length - 1; x >= 0; x=x-1) {
			if (chat[x] != null) {
				yok = -1;
				if (chat[x].chattype == 4) {
					if (WindowOpen(chat[x].pm) == 1) {
						if (pms.length > 0) {
							for (y = 0; y < pms.length; y++) {
								if (pms[y].pm == chat[x].pm) {
									yok = y;
								}
							}
						}
						if (yok == -1) {
							var pml = pms.length;
							pms[pml] = new Array();
							pms[pml] = new UpdatePMS(chat[x].chat,chat[x].pm)
							yok = pml;
						} else {
							pms[yok].chat += chat[x].chat
						}
					}
				}

				if (chatopen == 1 && yok == -1) {
					if (match2 < matches) {
						strout2 += chat[x].chat;
						match2 = match2 + 1;
					}
				}
			}
			if (match >= matches && match2 >= matches) {
				break;
			}
		}

		if (pms.length > 0) {
			clearTimeout(rps);
			for (y = 0; y < pms.length; y++) {
				PushMessage(pms[y].pm, pms[y].chat);
			}
			rps = setTimeout("RefreshPlayerSmall();",200);
		}

		if (strout2 != "") {
			var myElement = document.createElement("DIV");
			myElement.innerHTML = strout2 + "";
			myElement.id = "LastChatLine" + ChatC
			if (getObj("LastChatLine" + (ChatC - 1)) == null) {
				getObj('ChatContents').appendChild(myElement); 
			} else {
				getObj('ChatContents').insertBefore(myElement, getObj("LastChatLine" + (ChatC - 1)));
			}
			ChatC = ChatC + 1;
		}
		chat = new Array();

	}
	pms = null;


}

function RandomMap() {
	var x = 0;
	var y = 0;
	var strout = '<table cellpadding=0 cellspacing=0>';
	for (x = 1; x < 16; x++) {
		strout += '<tr>';
		for (y = 1; y < 16; y++) {
			strout += '<td><img src=\'images/tiles/grass1.png\' width=20 height=20></td>';
		}
		strout += '</tr>';
	}
	return strout + '</table>';
}

function ChatSplit(t) {
	var a = t.split(" ");
	var o = '';
	var m = 30;
	if (a.length > 0) {
		var x = 0;
		for (x = 0; x < a.length; x++) {
			m = 30;
			if (a[x].indexOf('http://') != -1) {
				m = 250;
			}
			if (a[x].length > m) {
				o = o + ' *snipped* ';
			} else {
				o = o + a[x] + ' ';
			}	
		}
	}
	return o;
}

function SendCMessage() {
	var r = 0;
	if (getObj('messc').value == '[Type what you wish to say here!]') {
		getObj('messc').value = '';
	}
	if (justSent) {
		AddChat('You are typing too fast.', 2);
	} else {
		var t2 = ChatSplit(getObj('messc').value);
		var t = (t2.indexOf('/') == -1 || t2.indexOf('/') > 0 ? ChatPrefix : '') + t2;
		if (t == '' || t == ' ') {
		} else {
			sendRefresh('play_ajax.asp?TextColor=' + URLencode(getObj('TextColor').value) + '&TextEffect=' + getObj('TextEffect').value + '&Message=' + URLencode2(t));
			getObj('messc').value = '';
			justSent = true;
			r = setTimeout('justSent = false;', 500);
		}
	}

	return false;
}

function URLencode2(sStr) {

	var s = sStr;
	s = s.replace(/</g, '&#60;');
	s = s.replace(/>/g, '&#62;');
	s = s.replace(/\'/g, '&#39;');
	s = escape(s).replace(/\+/g, '%2B');
	return s;
}

function URLencode(sStr) {
	return escape(sStr).replace(/\+/g, '%2B');
}

function AccountScreen(p, v, v2) {
	if (p == null) {
		p = 1;
	}
	if (v == null) {
		v = 0;
	}
	if (v2 == null) {
		v2 = 0;
	}
	getObj('menubar').innerHTML = '';

	setTimeout("sendRequest('load_ajax.asp?A=10&Value=" + v + "&A2=" + v2 + "&P=" + p + "');",500);
	setRefresh();
	ToggleChat(1);

}

function Login() {
	if (getObj('accountname') != null) {
		if (getObj('accountname').value != '' && getObj('accountpass').value != '') {
			sendRequest('load_ajax.asp?A=0&Value=0&A2=0&P=1&A4=' + getObj('accountname').value + '&A3=' + getObj('accountpass').value);
		} else {
			if (FacebookID != 0) {
				sendRequest('load_ajax.asp?A=0&Value=' + FacebookID + '&A2=0&P=1&A4=&A3=');
			}
		}
	}
}

function CloseGameX(x) {
	if (getObj('header2') != null) {
		getObj('header2').style.backgroundImage = 'URL(header.jpg)';
	}
	MenuUnload();
}

function SetRegion(y, rn, rb) {
	if (rb == null) {
		rb = 'kol.jpg';
	}
	if (rn != null) {
		RegionName = rn;
	}
	
	if (getObj('kolback') != null && RegionID != y) {
		RegionID = y;
		getObj('kolback').style.backgroundImage = 'URL(images/regions/' + rb + ')';
		RefreshMappy();
	}
}

function RefreshMappy() {
	getObj('mappy').innerHTML = '<iframe src="map.asp" style="width:160px;height:80px;background: transparent; margin: 0px; padding: 2px;" border=0 scrolling="none" frameborder="0" allowtransparency="true" id="mapframe" name="mapframe"></iframe>';
}

function GoPA(p, actionid, av, av2, av3, av4, av5) {
	if (actionid == null) {
		actionid = 1001;
	}
	if (av == null) {
		av = 1;
	}
	if (av2 == null) {
		av2 = 0;
	}

	if (av3 == null) {
		av3 = '';
	}
	PlayAction(actionid,av, av2, p, 0, av3, av4, av5);
}

function GetMessages () {
	SplitPageBox();
	PlayAction(3001,0, 1, 1, 0);
}


function GetPatches() {
	if (PatchDirty == 0) {
		OpenNewsScreen();
	} else {
		SplitPageBox();
		PatchDirty = 0;
		RefreshPlayerSmall();
		PlayAction(3004,0, 1, 1, 0);
	}
}

function SetFacebookID(fb) {
	FacebookID = fb
}

function GameOptions() {
//	ChooseCOlor();
	PlayAction(5005,0, 0, 0, 0);
}

function InitGame(a) {
	AddShortcuts();
	if (getObj('header2') != null) {
		getObj('header2').style.backgroundImage = 'URL(header2b.jpg)';
	}
	if (getObj('ChatButtons') != null) {
		getObj("ChatButtons").innerHTML = "<form method=\"post\" action=\"\" onsubmit=\"return SendCMessage();\" style=\"margin: 0\"><div style=\"width: 100%;\" align=left><table><tr><td onclick=\"OpenChatTarget();\" title=\"Click to choose the chat destination\" id=SayText style=\"cursor: pointer; width: 50px;\">" + ASCII("Say:") + "</td><td id=messcdiv style=\"width: 220px;\"><input style=\"width: 215px;\" id=\"messc\" name=\"messc\" size=30 maxlength=500 value=\"[Type what you wish to say here!]\" onclick=\"if (getObj('messc').value == '[Type what you wish to say here!]') {getObj('messc').value = '';}\" type=\"text\" tabindex=\"0\"></td><td style=\"width: 185px;\">" + Adar("SendCMessage();","Say","Say","sbutton1","snormal3.png",1) + "" + Adar("GameOptions();","Options","<img src=\'images/gui/tools_32.png\' border=0 width=16 height=16>","sbutton1","snormal2.png",1) + (widescreen == 0 ? "" + Adar("ChatResize(50);","Make chat bigger","<img src=\'images/gui/plus.png\' border=0 width=16 height=16>","sbutton1","snormal3.png", 1) + Adar("ChatResize(-50);","Make chat smaller","<img src=\'images/gui/minus.png\' border=0 width=16 height=16>","sbutton1","snormal3.png", 1) : "") + "</td><td style=\"width: 180px;\">" + Adar("ToggleLog2();","Show Game Log","<img src=\'images/gui/ency.png\' border=0 width=16 height=16>","sbutton1","snormal2.png",1) + Adar("LibraryoftheSages();","Show the Library of the Sages","Library/Help","button5","normal8.png",1) + Adar("ShowFacebookMenu();","Facebook Page","<img src=\"images/facebook.png\" border=0 height=\"16\" width=\"16\">","sbutton1","snormal3.png", 1) + "</td></tr></table></div></form>";

	}
	AccountID = a;
	Running = true;
	setTimeout('PlayAction(9, 0, 1, 0, null, null);',500);
}


function ShowFacebookMenu() {
	AddButton(-3, 'Text','Text','Picture');
	AddSubMenu(-3, 'Profile', '', '', 'window.open("http://www.facebook.com/apps/application.php?id=198093516879");');
	if (FacebookID > 0) {
		AddSubMenu(-3, 'Invite', '', '', 'window.open("fb_ajax.asp?invite=1");');
	}
	ShowSubMenu(-3,'messc',0);
}

function LibraryoftheSages() {
	window.open("library.asp");
}

function ShowCatalog() {
	window.open("http://www.e-junkie.com/regal/tag/27105/27722/tag.php#Champion+of+the+Gods");
}


function ChooseChatColor() {
	var id1 = -3;
	var id3 = 0;
	var id2 = 'SayText'
	AddButton(-3, 'Text','Text','Picture');
	var y = 0;
	for (y = 1; y < 12; y++) {
		AddSubMenu(-3, ASCII('' + getAColor(y), y), '', '', 'SetChatColor(' + y + ');');
	}
	ShowSubMenu(id1,id2,id3);
}

function SetOption(on, ov) {
	PlayAction(5007, 0, 1, 0, null, on, ov);
}

function SetChatTitle() {
	var s = getObj('ChatTitlex').value;
	SetOption('ChatTitle',s);
}

function SetChatColor(y) {
	getObj('TextColor').value = y;
}

function FormEnter(e, f) {
	e = e || window.event;
	if (e == null) {
	} else {
		if (e.keyCode == 13) {
			if (f != null)	 {
				eval(f);
			}
		}
	}
}


function PageStart() {
CloseGameX(1);
//if (getObj('rcsbuttonbox') == null) {
	getObj('menubar').innerHTML = '<table id="rcsbuttonbox" cellpadding=2 cellspacing=2 height=24><tr height=24><td id="rcsbutton0" style="width: 70px;">' + Adat('PageStart();','Home','Home') + '</td><td id="rcsbutton4" style="width: 70px;">' + Adat('ShowCatalog();','Shop for awesome extra content!','Shop') + '</td><td id="rcsbutton1" style="width: 70px;">' + Adat('Website(\'website.asp?p=1\');','About','About') + '</td><td id="rcsbutton2" style="width: 70px;">' + Adat('Screenshots();','Screenshots','Screenshots') + '</td><td id="rcsbutton3" style="width: 70px;">' + Adat('window.location.replace(\'mobile.asp\');','Mobile','Mobile') + '</td><td id="rcsbutton3" style="width: 70px;">' + Adat('Website(\'website.asp?p=3\');','Contact Us','Contact Us') + '</td></tr></table>';
//}
SplitPageBox();
getObj('ChatBox').innerHTML = '<div id=ChatContents style=\'width: ' + (widescreen == 1 ? 280 : 600) + 'px;  text-align: left;\'></div>';
getObj('LogBox').innerHTML = '<div id=LogContents style=\'width: ' + (widescreen == 1 ? 280 : 600) + 'px;  text-align: left;\'></div>';
getObj('headerbar').innerHTML = ASCIIH('About Champion of the Gods');
var l = getObj('leftbox');
if (l != null) {
	l.innerHTML = '<table cellpadding=1 cellspacing=1 style="margin-left: 10px; width: 120; height: 300px; background-repeat: no-repeat; background-image: URL(images/loginscreen.png);"><tr><td align=center>' + ASCIIH('Login', 6) + '</td></tr><tr><td>' + ASCII('Username:', 1) + '</td></tr><tr><td>' + (FacebookID == 0 ? '<input name=accountname id=accountname size=15 maxlength=30>' : ASCII('Facebook') + Adar('FacebookID = 0; PageStart();','Login using a non-facebook account.','Other','button5','normal6.png') + '<input name=accountname id=accountname size=15 maxlength=30 type=hidden value="">') + '</td></tr><tr><td>' + ASCII('Password:', 1) + '</td></tr><tr><td>' + (FacebookID == 0 ? '<input type=password name=accountpass id=accountpass size=15 maxlength=30 onkeypress="FormEnter(event,\'Login();\');">' : ASCII('Not required')) + '</td></tr><tr><td id=loginresponse></td></tr><tr><td align=center>' + Adar('Login();','Login to the game','Login','button5','normal5.png') + (FacebookID == 0 ? ASCII('or') + Adar('Signup();','Signup to the game','Signup','button5','normal6.png') +  ASCII('<b>Interface</b>', 6) + Adar('window.location.replace(\'default.asp?RL=1\');','Open Default/Normal size','Normal','button5','normal7.png') + '' + Adar('FullScreen();','Open Full screen','Fullscreen','button5','normal7.png') + '' + Adar('window.location.replace(\'default.asp?w=1&RL=1\');','Open Wide screen','Widescreen','button5','normal7.png') : '') + Adar('window.location.replace(\'http://www.facebook.com/apps/application.php?id=198093516879\');','Facebook Page','<img src="images/facebook.png" border=0 height="16" width="16">','sbutton1','snormal3.png') + '</td></tr><tr height=\'100%\'><td valign=top><small>' + ASCII('Game v' + gameversion) + '</small></td></tr></table>'

}
var l = getObj('mainbox');
if (l != null) {
	l.innerHTML = '<div style="width: 350px; height: 320px; background-image: URL(images/boxscreen.png); background-repeat: no-repeat; background-position: top left;">&nbsp;&nbsp;&nbsp;' + ASCII('Welcome to <b>Champion of the Gods</b>!') + '<div style="width: 350px; height: 60px; background-image: URL(images/hometab.png); background-repeat: no-repeat; background-position: bottom left; padding-top: 10px;"><div style="padding-left: 65px; padding-top: 5px; padding-bottom: 5px; padding-right: 15px;">' + ASCII('Would you like to know more about the game?') + '<br>' + Adar('ShowMovie();','Introduction','Intro','button5','normal5.png', 1) + Adar('Signup();','Signup to the game','Signup','button5','normal6.png', 1) + '</div></div><div style="width: 350px; height: 55px; background-image: URL(images/hometab.png); background-repeat: no-repeat; background-position: bottom left;"><div style="padding-left: 65px; background-image: URL(images/items/84.png); background-repeat: no-repeat; background-position: 5px 5px; padding-top: 10px; padding-bottom: 5px; padding-right: 15px; height: 50px;" valign=bottom>' + ASCII('Looking for help and answers?') + '<br>' + Adar('window.open(\'library.asp\');','Library of the Sages','Library','button5','normal5.png', 1) + Adar('window.open(\'mailto:support@championofthegods.com\');','Support','Support','button5','normal8.png', 1) + '</div></div>' + getObj('newsbox').innerHTML + '</div>';
}
}

function CloseMovie() {
	getObj('moviebox').innerHTML = '';
}

function ShowMovie(c) {
	getObj('moviebox').style.display = '';
	if (c == null) {
		getObj('moviebox').innerHTML = '<center><div align=center valign=center  style="border: 2px solid black; WIDTH:540;height:310; padding-top: 90px;"><IFRAME SRC="website.asp?p=6" border=0 scrolling="none" frameborder="0" allowtransparency="true" WIDTH="540" height="330" scroll=no></IFRAME></div></center>';
	} else {
		getObj('moviebox').innerHTML = '<center><div align=center valign=center  style="border: 2px solid black; WIDTH:630;height:400; padding-top: 90px;"><IFRAME SRC="website.asp?p=6&c=' + c + '" border=0 scrolling="none" frameborder="0" allowtransparency="true" WIDTH="630" height="400" scroll=no></IFRAME></div></center>';
	}
}

function Screenshots() {
	//Website(\'website.asp?p=2\');
	//window.open('screenshots.asp', 'Screenshots', 'status=no, width=608, height=648,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=no');
	window.open('gallery.asp?a=0', 'Screenshots', 'status=no, width=608, height=648,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=no');
}

function FullScreen() {
	window.open('default.asp?full=1&rl=1' + (widescreen != 0 ? '&w=1' : '') + '', 'COTGGame', 'status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
	window.close();
}

function StoryInfo() {
var l = getObj('leftbox');
if (l != null) {
	l.innerHTML = '<table cellpadding=0 cellspacing=0 style="margin-left: 10px; width: 120; height: 300px; background-repeat: no-repeat; background-image: URL(images/loginscreen.png);"><tr><td><img src="images/story/1.png" width=15 height=15>' + ASCII('Noble', 6) + '</td></tr><tr><td>' + ASCII('Bonuses: <br><b>MIGHT</b>') + '<br>' + ASCII('<b title=\'A small chance of getting an item for free when you try to buy an item from a store.\'>Free Items</b>') + '</td></tr><tr><td><img src="images/story/2.png" width=15 height=15>' + ASCII('Peasant', 6) + '</td></tr><tr><td>' + ASCII('Bonuses: <br><b>LUCK</b>') + '<br>' + ASCII('<b title=\'The ability to beg money from certain NPCs.\'>Begging</b>') + '</td></tr><tr><td><img src="images/story/4.png" width=15 height=15>' + ASCII('Explorer', 6) + '</td></tr><tr><td>' + ASCII('Bonuses: <br><b>HEALTH</b>') + '<br>' + ASCII('<b title=\'The ability to find and dig up buried treasure.\'>Treasure Seeking</b>') + '</td></tr><tr><td><img src="images/story/3.png" width=15 height=15>' + ASCII('Gypsy', 6) + '</td></tr><tr><td>' + ASCII('Bonuses: <br><b>DEXTERITY</b>') + '<br>' + ASCII('<b title=\'A 10% bonus to all money received from questing.\'>Money Bonus</b>') + '</td></tr><tr><td><img src="images/story/5.png" width=15 height=15>' + ASCII('Intellect', 6) + '</td></tr><tr><td>' + ASCII('Bonuses: <br><b>WISDOM</b>') + '<br>' + ASCII('<b title=\'The ability to understand and read ancient scrolls and symbols.\'>Ancient Dialects</b>') + '</td></tr></table>';
}
}

function SupportInfo() {
var l = getObj('leftbox');
if (l != null) {
	l.innerHTML = '<table cellpadding=0 cellspacing=0 style="margin-left: 10px; width: 120; height: 300px; background-repeat: no-repeat; background-image: URL(images/loginscreen.png);"><tr><td align=center>' + ASCIIH('Game Code', 6) + '</td></tr><tr><td valign=bottom height="102" style="background-image: URL(images/buy.png?v=2); background-position: top center; background-repeat: no-repeat; padding-bottom: 3px;"><center>' + Adar('ShowCatalog();','Buy redeemable content as Game Codes','Buy Content','button5','normal2.png') + '</center></td></tr><tr><td>' + ASCII('Code:', 1) + '</td></tr><tr><td><input id=gamecode value="" size=15 maxlength=30></td></tr><tr><td align=center>' + Adar('PlayAction(10, -6, 0, 1, 0, getObj(\'gamecode\').value );','Redeem game code','Redeem','button5','normal5.png') + '</td></tr><tr><td align=center>' + ASCIIH('Support', 6) + '</td></tr><tr><td align=center>' + Adar('window.open(\'library.asp\');','Library of the Sages','Help','button5','normal7.png') + '' + Adar('window.open(\'http://www.forlorngames.com/\');','Forlorn Games - Forum','Forums','button5','normal7.png') + '' + Adar('window.open(\'mailto:support@championofthegods.com\');','Email COTG Support','Contact Us','button5','normal7.png') + '</center></td></tr><tr height="100%"><td></td></tr></table>';
}
}

function Signup(f1, f2, f3, f4, f5) {
var an = '';
var ap = '';
var ae = '';
var ar = Referral;
if (f1 == null) {
	f1 = '';
	f2 = '';
	f3 = '';
	f4 = '';
	f5 = '';
}
if (getObj('accountname') != null) {
	an = getObj('accountname').value;
}
if (getObj('accountpass') != null) {
	ap = getObj('accountpass').value;
}
if (getObj('accountemail') != null) {
	ae = getObj('accountemail').value;
}
if (getObj('referrer') != null) {
	ar = getObj('referrer').value;
}

var l = getObj('leftbox');
if (l != null) {
	l.innerHTML = '';
}
var l = getObj('mainbox');
if (l != null) {
	l.innerHTML = '<div style="width: 350px; background-image: URL(images/boxscreen.png);"><table cellpadding=1 cellspacing=1 style="margin-left: 10px; width: 350; height: 270px;"><tr><td align=center>' + ASCIIH('Signup') + '</td></tr><tr><td width=150>' + ASCII('Username:', 6) + '</td><td><input name=accountname id=accountname size=15 maxlength=30 ' + (f1 == 1 ? ' style="border: 1px solid red;"' : '') + ' value="' + an + '"></td></tr><tr><td>' + ASCII('Password:', 6) + '</td><td><input type=password name=accountpass id=accountpass value="' + ap + '" size=15 maxlength=30 ' + (f2 == 1 ? ' style="border: 1px solid red;"' : '') + '></td></tr><tr><td>' + ASCII('Confirm Password:', 6) + '</td><td><input type=password name=accountpass2 id=accountpass2 size=15 maxlength=30 ' + (f3 == 1 ? ' style="border: 1px solid red;"' : '') + ' onkeypress="FormEnter(event,\'Signup2();\');"></td></tr><tr><td>' + ASCII('Email:', 6) + '</td><td><input name=accountemail id=accountemail value="' + ae + '" size=20 maxlength=50 ' + (f5 == 1 ? ' style="border: 1px solid red;"' : '') + ' onkeypress="FormEnter(event,\'Signup2();\');"></td></tr><tr><td></td><td>' + ASCII('A valid email address. You may wish to add support@championofthegods.com to your safe list.') + '</td></tr><tr><td>' + ASCII('Referred By:', 6) + '</td><td><input name=referrer id=referrer size=20 maxlength=50 value="' + ar + '"></td></tr><tr><td></td><td>' + ASCII('The game account number or Email address of the friend that referred you.') + '</td></tr><tr><td colspan=2 align=center>' + Adar('Signup2();','Create a new game account.','Create','button5','normal5.png', 1) + Adar('PageStart();','Home','Home','button5','normal6.png', 1) + '</td></tr><tr height=\'100%\'><td colspan=2></td></tr></table></div>';
}
}

function Signup2() {
	var fail = 0;
	var f1 = 0;
	var f2 = 0;
	var f3 = 0;
	var f4 = 0;
	var f5 = 0;
	if (getObj('accountname') != null) {
		if (getObj('accountname').value == '') {
			fail = 1;
			f1 = 1;
		}
		if (getObj('accountpass').value == '') {
			fail = 1;
			f2 = 1;
		}
		if (getObj('accountpass2').value == '') {
			fail = 1;
			f3 = 1;

		}
		if (getObj('accountpass').value != getObj('accountpass2').value) {
			fail = 1;
			f4 = 1;

		}
		if (getObj('accountemail').value == '') {
			fail = 1;
			f5 = 1;

		}
		if (fail == 1) {
			Signup(f1, f2, f3, f4, f5);
		} else {
			sendRequest('load_ajax.asp?A=-100&Value=0&A2=' + COTGTrackingID + '&P=1&A4=' + URLencode(getObj('accountname').value) + '&A3=' + URLencode(getObj('accountpass').value) + '&e=' + URLencode(getObj('accountemail').value) + '&r=' + URLencode(getObj('referrer').value));
		}
	}
}


function PageHome() {
MenuLoad();
SplitPageBox();
RefreshStatBox();
}

function GetBoxSmall2(Tag, color, value1, value2, width) {
	if (width == null) {
		width = 300;
	}
	var b = GetPerc(value2, value1)
	var strout = PercentBoxSmall(width, b, color,' ', 3);
	return strout;
}

function GetXP(l) {
	if (l < 0) {
		return 0;
	} else if (l == 0) {
		return 2;
	} else {
		return l * Math.round(l / 2);
	}
}

function RefreshStatBox() {
var l = getObj('statbox');
if (l != null) {
	var cp = Players[1];
	if (cp != null) {
		var sm = cp.Skills[25].SkillMax;
		var sv = cp.Skills[25].SkillValue;
		if (sv > sm) {
			sv = sm;
		}
		var strout = '';
		strout += '<tr><td colspan=6 align=left style=\' padding-left: 20px;\'><div style=\'background-color: black;width: 280px;\' title=\'Exp: ' + addCommas(sv) + '/' + addCommas(sm) + '\'>'  + GetBox3('Lvl: ' + cp.DiscoveryLevel + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' + addCommas(sm - sv) + ' TNL', 'biggreen', sv, sm, 280) + '</div></td></tr>';


		l.innerHTML = '<table cellpadding=1 cellspacing=0 height=60><tr><td width=68 valign=bottom>&nbsp;</td><td style=\'padding-top: 1px;\'><table width=430 cellpadding=0 cellspacing=0><tr><td rowspan=3 align=center><img src=\'images/faith/' + cp.faithid + '.png\' title=\'' + GetFaith(cp.faithid) + ': Click to change/manage your faith!\' style=\'cursor: pointer;\' onclick=\'ChangeFaithScreen();\'></td><td style=\'padding-left: 20px; width: 300;\' colspan=6>' + ASCII(cp.PlayerName, 6) + '</td><td rowspan=3 align=center><img src=\'images/story/' + cp.storyid + '.png\'  title=\'' + GetStory(cp.storyid) + '\'></td></tr>' + strout + '<tr><td width=17 style=\'padding-left: 30px;\'><img src=\'images/gui/hpicon.png\' width=17 height=17 title="Health power (hp)"></td><td width=90>' +  GetBox2('Health', 'bigred', (cp.Skills[1].SkillValue > cp.Skills[1].SkillMax ? cp.Skills[1].SkillMax : cp.Skills[1].SkillValue), cp.Skills[1].SkillMax, 90) + '</td><td width=17 title="Magic power/Power (mp)"><img src=\'images/gui/mpicon.png\' width=17 height=17></td><td style=\'width: 70px;\'>' +  GetBox2('Power', 'bigblue', (cp.Skills[2].SkillValue > cp.Skills[2].SkillMax ? cp.Skills[2].SkillMax : cp.Skills[2].SkillValue), cp.Skills[2].SkillMax, 70) + '</td><td width=17 title="Gold pieces (gp) - Your currency"><img src=\'images/gui/gpicon.png\' width=17 height=17></td><td style=\'width:110px\'>' + ASCIIS('' + addCommas(cp.LegendPieces), 6) + '</td></tr></table></td></tr></table>'
		cp = null;
	} else {
		l.innerHTML = '';
	}
}
}

//--Small

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function GetFaith(f) {
	return faiths[f];
}

function GetStory(s) {
	return story[s];
}

function ChangeFaithScreen() {
	PlayAction(1002, 0, 1, 0, null, null);
}

function GetBox(Tag, color, value1, value2, h) {
	var b = GetPerc(value2, value1);
	var strout = PercentBoxSmall(96, b, color,ASCIIS('' + addCommas(value1) + '/' + addCommas(value2) + ''), h);
	return strout;
}

function GetBox2(Tag, color, value1, value2, w) {
	if (w == null) {
		w = 120;
	}
	var b = GetPerc(value2, value1);
	var strout = PercentBox(w, b, color,ASCIIS((value2 == value1 ? value1 : '' + addCommas(value1) + '/' + addCommas(value2) + '')));
	return strout;
}

function GetBox3(Tag, color, value1, value2, w) {
	if (w == null) {
		w = 120;
	}
	var b = GetPerc(value2, value1);
	var strout = PercentBox(w, b, color,ASCIIS(Tag));
	return strout;
}

function PercentBox(pwidth, PercentValue, Color, caption, xheight) {
	if (xheight == null) {
		xheight =15;
	}
	if (caption == '') {
		caption = PercentValue + '%';
	}
	return '<div style="width: ' + (pwidth + 2) + 'px; height: ' + (xheight + 2) + 'px; border: 1px solid black; position: relative; background-color: black;" align=left><div style="width: ' + Math.round((pwidth / 100) * PercentValue) + 'px; height: ' + xheight + 'px; background-image: URL(images/' + Color + '.png); background-repeat: repeat-x; font-size: 1pt;  position: absolute; left: 0px; top: 0px; padding: 0; margin: 0;"></div><div style=" position: absolute; left: 0px; top: 0px; width:' + pwidth + 'px; height: ' + xheight + 'px;" align=center>' + caption + '</div></div>';
}

function GetPerc(MaxP, CurP) {
	if (CurP <= 0) {
		return 0
	} else {
		if (CurP > MaxP) {
			return 100
		} else {
			return Math.floor((100 / MaxP) * CurP) 
		}
	}

}

function PercentBoxSmall(pwidth, PercentValue, Color, caption, xheight) {
	if (xheight == null) {
		xheight = 10;
	}
	if (caption == '') {
		caption = PercentValue + '%';
	}
	return '<div style="width: ' + (pwidth + 2) + 'px; height: ' + (xheight + 2) + 'px; border: 1px solid black; position: relative; background-color: black;" align=left><div style="width: ' + Math.round((pwidth / 100) * PercentValue) + 'px; height: ' + xheight + 'px; background-image: URL(images/' + Color + '.png); background-repeat: repeat-x; font-size: 1pt;  position: absolute; left: 0px; top: 0px; padding: 0; margin: 0;"></div><div style=" position: absolute; left: 0px; top: 0px; width:' + pwidth + 'px; height: ' + xheight + 'px;" align=center>' + caption + '</div></div>';
}

function HomeMenu(x) {
	if (x == null) {
		x = 1;
	}
	PlayAction(2000, 0, 1, x, null, null);
}

function ExploreMenu() {
	PlayAction(2001, 0, 1, 0, null, null);
}


function ExploreActual(x) {
	if (x == null) {
		x = 1;
	}
	PlayAction(20011, 0, x, 0, null, null);
}

function ExploreActual2(x) {
	SetMainBox('<table width=350 style="background-image: URL(images/scene6.png); height: 50px; background-repeat: no-repeat;" cellpadding=0 cellspacing=0><tr><td width=240 style="font-weight: bold;" valign=top></td></tr><tr><td id=worldbuttons></td></tr></table>');
	ActionTimer('Exploring Area...', 'ExploreActual();', GetTimer(4), GetTimer(4));
}

function RegionTravel(fr, tr) {
	PlayAction(2002, 100, -tr, 0, null, null)
}

function DelayedRegionTravel(fr, tr, timex) {
	getObj('mappy').innerHTML = ASCII('You are travelling to a new location ...', 6)
	setTimeout('RegionTravel(' + fr + ',' + tr + ');', timex);
}


function TravelMenu() {
	ShowTravelMap();
}

function FollowerMenu(x) {
	if (x == null) {
		x = 1;
	}
	PlayAction(2003, 0, 1, 0, null, null);
}

function CommunityMenu() {
//function ShowPlayerMenu(id3) {
	var id1 = -3;
	var id3 = 0;
	var id2 = 'rcsbutton5'
	AddButton(-3, 'Text','Text','Picture');
	AddSubMenu(-3, 'Guilds', '', '', 'OpenGuildScreen();');
	AddSubMenu(-3, 'Party', '', '', 'OpenPartyScreen();');
	AddSubMenu(-3, 'Friends', '', '', 'OpenFriendScreen();');
	AddSubMenu(-3, 'Forums', '', '', 'document.forms[\'forumsr\'].submit();');
	AddSubMenu(-3, 'Ranks', '', '', 'OpenRankScreen();');
	AddSubMenu(-3, 'News', '', '', 'OpenNewsScreen();');
	AddSubMenu(-3, 'Bugs', '', '', 'OpenBugScreen();');
	AddSubMenu(-3, 'Shop', '', '', 'OpenShop();');
	ShowSubMenu(id1,id2,id3);
}

function OpenShop() {
	window.open('http://www.e-junkie.com/regal/');
}

function OpenPartyScreen() {
	setTimeout('PlayAction(10174,0,0, 1, 0);',250);
}

function OpenFriendScreen() {
	setTimeout('PlayAction(1015,0,0, 1, 0);',250);
}

function OpenGuildScreen() {
	setTimeout('PlayAction(10164,0,0, 1, 0);',250);
}

function OpenNewsScreen() {
	setTimeout('PlayAction(10184,0,0, 1, 0);',250);
}

function OpenRankScreen() {
	setTimeout('PlayAction(10204,0,0, 1, 0);',250);
}

function OpenBugScreen() {
	setTimeout('PlayAction(10194,0,0, 1, 0);',250);
}

function AddGuildMember() {
	if (getObj('guildname') == null) {
	} else {
		var f = getObj('guildname').value;
		PlayAction(10164, 0, 3, 1, 0, f);
	}
}

function ChatResize(h) {
	if (Math.round(getObj('ChatBoxp').style.height.replace("px", "")) + h > 355) {
	} else {
		getObj('ChatBoxp').style.height = Math.round(getObj('ChatBoxp').style.height.replace("px", "")) + h;
		getObj('ChatBoxpi').style.height = Math.round(getObj('ChatBoxpi').style.height.replace("px", "")) + h;
		getObj('ChatBox').style.height = Math.round(getObj('ChatBox').style.height.replace("px", "")) + h;
		getObj('LogBoxpi').style.height = Math.round(getObj('LogBoxpi').style.height.replace("px", "")) + h;
		getObj('LogBox').style.height = Math.round(getObj('LogBox').style.height.replace("px", "")) + h;
	}
}

function JoinGuild(g) {
	PlayAction(10163, g, 0, 1, 0, 0);
}

function AcceptProposal(g) {
	PlayAction(1015, g, 6, 1, 0);
}

function JoinParty(g) {
	PlayAction(10173, g, 0, 1, 0, 0);
}


function AddFriend() {
	if (getObj('friendname') == null) {
	} else {
		var f = getObj('friendname').value;
		PlayAction(1015, 0, 3, 1, 0, f);
	}
}

function AddPartyMember() {
	if (getObj('partyname') == null) {
	} else {
		var f = getObj('partyname').value;
		PlayAction(10174, 0, 3, 1, 0, f);
	}
}

function HelpMenu() {
	PlayAction(2005, 0, 1, 0, null, null);
}

function LogoutMenu(x) {
	RegionID = 0;
	ToggleChat(0);
	TidyWindows();
	Chat = new Array();
	ClearChat();
	ClearLog();
	CloseGameX(1);
	if (x == null) {
		SplitPageBox();
	}
	Messages = new Array();
	if (x == null) {
		AccountScreen(1,0,0);
	}
}

function Fight(f) {
	if (f == null) {
		f = 2;
	}
	MenuLoad();

	var cp = Players[1];
	var cp2 = Players[f];
	//'BigPageBox();
	if (cp != null && cp2 != null) {
		FightSetup(f);
	}
}

function FightSetup(f) {
	CancelFight();
	SplitPageBox2();
	SetMainBox('<table><tr><td valign=top id=fightbox>Loading ....</td></tr></table>');
	UpdateFight(1, f);
}

function CancelFight() {
	fighting = 0;
	clearTimeout(fight[1]);
	clearTimeout(fight[2]);
	clearTimeout(petfightt);
}

function AutoFight(f) {
	//Initiate automatic combat
	var cp = Players[1];
	var cp2 = Players[f];
	if (cp != null && cp2 != null) {
		clearTimeout(fightstart);
		if (getObj('fightbox') == null) {
			cp2.Skills[1].SkillValue = cp2.Skills[1].SkillMax;
			cp2.Skills[2].SkillValue = cp2.Skills[2].SkillMax;
			cp2.dead = 0;

			cp.Skills[1].SkillValue = cp.Skills[1].SkillMax;
			cp.Skills[2].SkillValue = cp.Skills[2].SkillMax;
			cp.dead = 0;
			FightSetup(f);
		}

		if (cp.dead != 0 || cp2.dead != 0)  {
			AddLog('' + (cp2.dead != 0 ? cp2.PlayerName : cp.PlayerName) + ' is already dead.');
		} else {
			StartFight(f);
		}
	} else {
		AddLog('Null data delivered! (' + cp + ') ( ' + cp2 + ')');
	}
}

function StartFight(f) {
	fighting = f;
	UpdateFight(1, f);
	var cp = Players[1];
	var cp2 = Players[fighting];
	if (cp != null && cp2 != null) {
		AddLog('<b>' + cp.PlayerName + '</b> vs <b>' + cp2.PlayerName + '</b>');
		if (IsPartyFight(cp) == true || IsPartyFight(cp2) == true) {
			autopiloton = 0;
		} else {
			autopiloton = autopiloton - 1;
		}

		if (autopiloton < 0) {
			autopiloton = 0;
		}
		
		var sv1 = cp.Skills[69].SkillValue;
		var sv2 = cp2.Skills[69].SkillValue;
		if (sv1 < 1.5) {
			sv1 = 1.5;
		}
		if (sv2 < 1.5) {
			sv2 = 1.5;
		}

		clearTimeout(fight[1]);
		clearTimeout(fight[2]);
		clearTimeout(petfightt);

		fight[1] = setTimeout('ContinueFight(1, ' + f +  ');', sv1 * 1000);
		fight[2] = setTimeout('ContinueFight(' + f +  ', 1);', sv2 * 1000);
		petfightt = setTimeout('PetFight(1, ' + f + ');', 3000);
	} else {
		AddLog('Null data delivered! (' + cp + ') ( ' + cp2 + ')');
	}
}

function PetFight(x1, x2) {
	if (rand(2) == 1) {
		RunPetFight(x1, x2);
		RunPetFight(x2, x1);
	} else {
		RunPetFight(x2, x1);
		RunPetFight(x1, x2);
	}
}

function RunPetFight(x1, x2) {
	var y = 0;
	var cp = Players[x1];
	if (cp != null) {
		var cp2 = Players[x2];
		if (cp2 != null) {
			if (cp2.dead == 0 && cp.dead == 0) {
				for (y = 1; y < cp.Pets.length; y++) {
					var p = cp.Pets[y];
					if (p != null) {
						if (p.AI == 1) {
							//Damage pet
							var d = Math.round(p.level * 0.75) + 5;
							cp2.Skills[1].SkillValue = cp2.Skills[1].SkillValue - d;
							cp2.tmpdmg = cp2.tmpdmg + d;
							AddLog('' + p.PetName + ' bypasses ' + cp2.PlayerName + ' armor and deals ' + d + ' damage.');
						} else if (p.AI == 2) {
							//Heal pet
							var d = Math.round(p.level * 0.5) + 5;
							if (d > cp.Skills[1].SkillMax - cp.Skills[1].SkillValue) {
								d = cp.Skills[1].SkillMax - cp.Skills[1].SkillValue;
							}
							cp.Skills[1].SkillValue = cp.Skills[1].SkillValue + d;
							AddLog('' + p.PetName + ' heals ' + cp.PlayerName + ' for ' + d + ' health.');
						} else if (p.AI == 3) {
							//Power pet
							var d = Math.round(p.level * 0.5) + 5;
							if (d > cp.Skills[2].SkillMax - cp.Skills[2].SkillValue) {
								d = cp.Skills[2].SkillMax - cp.Skills[2].SkillValue;
							}
							cp.Skills[2].SkillValue = cp.Skills[2].SkillValue + d;
							AddLog('' + p.PetName + ' regenerates ' + cp.PlayerName + ' power by ' + d + ' points.');
						} else if (p.AI == 4) {
							//Magic damage pet
							var d = Math.round(p.level * 0.75) + 5;
							cp2.Skills[1].SkillValue = cp2.Skills[1].SkillValue - d;
							cp2.tmpdmg = cp2.tmpdmg + d;
							AddLog('' + p.PetName + ' bypasses ' + cp2.PlayerName + ' magic defense and deals ' + d + ' damage.');
						} else if (p.AI == 5) {
							//Physical shield pet
							if (cp.petdefense == -1) {
								cp.petdefense = Math.round(p.level * 0.5);
								AddLog('' + p.PetName + ' moves to defend ' + cp.PlayerName + '.');
							} else if (cp.petdefense  > 0) {
								AddLog('' + p.PetName + ' is defending ' + cp.PlayerName + ' (' + cp.petdefense + ').');
							}
						} else if (p.AI == 6) {
							//Magical shield pet
							if (cp.petmagicdefense == -1) {
								cp.petmagicdefense = Math.round(p.level * 0.5);
								AddLog('' + p.PetName + ' moves to defend ' + cp.PlayerName + '.');
							} else if (cp.petmagicdefense  > 0) {
								AddLog('' + p.PetName + ' is defending ' + cp.PlayerName + ' (' + cp.petmagicdefense + ').');
							}
						} else {
							AddLog('' + p.PetName + ' takes a turn but does nothing.');
						}
						UpdateFightShort(x1, x2, 0);
					}
				}
				if (x1 == 1) {
					clearTimeout(petfightt);
					petfightt = setTimeout('PetFight(' + x1 + ', ' + x2 + ');', 3000);
				}
			} else {
				clearTimeout(petfightt);
			}
			cp2 = null;
		}
		cp = null;
	}

}

function rand(x) {
	return Math.floor(Math.random()*x) + 1;
}

function SkillFormula(s, sv) {
	var strout = '';
	var t = 0;
	if (s == 64) {
		t = Math.round(sv / 2);
		if (t > 65) {
			t = 65;
		}
		strout = 'Crit % = ' + t;
	} else if (s == 62) {
		t = Math.round(sv / 100);
		if (t > 65) {
			t = 65;
		}
		strout = 'Evade % = ' + t;
	} else if (s == 65) {
		t = Math.round(sv / 100);
		if (t > 65) {
			t = 65;
		}
		strout = 'Parry % = ' + t;
	} else if (s == 66) {
		t = Math.round(sv / 10);
		strout = 'Block % = ' + t;
	} else if (s == 8) {
		t = Math.round(sv / 5);
		strout = '-' + t + 'dmg';
	} else if (s == 3) {
		t = Math.round(sv / 10);
		strout = '+' + t + 'dmg';
	} else if (s == 5) {
		t = Math.round(sv / 50);
		strout = '+' + t + 'gp';
		t = Math.round(sv / 100);
		strout = strout + ', +' + t + 'gather';
	} else if (s == 4) {
		t = Math.round(sv / 50);

		strout = '+' + t + '% evade';
	} else if (s == 80) {
		if (sv == 1) {
			strout = 'Fire';
		} else if (sv == 2) {
			strout = 'Freeze';
		} else if (sv == 3) {
			strout = 'Stun';
		} else if (sv == 4) {
			strout = 'Damage';
		} else if (sv == 5) {
			strout = 'Drain/Heal';
		} else if (sv == 6) {
			strout = 'Drain/Power';
		} else if (sv == 7) {
			strout = 'Acid';
		}
	}
	return (strout != '' ? ASCII(strout,2) : '');
}

function ContinueFight(a, b) {
	var cp = Players[a];
	var endfight = 0;

	var cp2 = Players[b];
	if (cp != null && cp2 != null) {
		//62 Evasion
		//63 Magic Bonus
		//64 Critical Increase
		//65 Parry
		//66 Block
		//67 Magic Damage Bonus
		//68 Magic Healing Bonus
		//69 Attack Speed
		//70 Accuracy
		//71 Damage Reduction
		//47 Magic Defense
		//8 Defense
		//7 Damage
		var damage = 0;
		if (cp.Stunned == 1) {
			AddLog('' + cp.PlayerName + ' is stunned.');
		} else {
			AddLog('' + cp.PlayerName + ' attacks ' + cp2.PlayerName + '.');

			if ((cp.i != 1 || autopiloton > 0) && cp.dead == 0) {
				var d = new Date();
				var ar = getPowers(cp);
				for (x = 1; x <= 8; x++) {
					var a = ar[x];
					if (a != null) {
						var t = null;
						try {
							t = cp.Powers[x];
						} catch (e) {
						}
						if ((Math.abs(d.getTime()) - (t == null ? 100 : t)) > Powers[a].cooldown && cp.dead == 0) {
							//Okay to use
							TriggerPower(cp.i, a, x, b);
							break;
						}
					}
				}	
			}

			var crit = Math.round(cp.Skills[64].SkillValue / 2);
			if (crit > 65) {
				crit = 65;
			}

			damage = cp.Skills[7].SkillValue;
		
			if (rand(100) + 1 < crit)  {
				AddPlayerWidget(cp, 'spells/s13.png', 'Critical', 6);
				AddLog('' + cp2.PlayerName + ' took a critical hit.');
				damage = damage * 1.5;
			}
			damage = damage + (cp.Skills[3].SkillValue / 10);

			var evasion = Math.round(cp2.Skills[62].SkillValue / 100) + (cp2.Skills[4].SkillValue / 50);
			if (evasion > 65) {
				evasion = 65;
			}
			if (rand(100) + 1 < evasion)  {
				AddPlayerWidget(cp, 'spells/s13.png', 'Miss', 3);
				AddLog('' + cp2.PlayerName + ' evaded.');
				damage = 0;
			}

			var parry = Math.round(cp2.Skills[65].SkillValue / 100);
			if (parry > 65) {
				parry = 65;
			}
			if (rand(100) + 1 < parry && damage > 0)  {
				AddPlayerWidget(cp2, 'spells/s13.png', 'Parry', 5);
				AddLog('' + cp2.PlayerName + ' parried.');
				damage = 0;
			}
	

			var block = Math.round(cp2.Skills[66].SkillValue / 10);
			if (rand(100) + 1 < block && damage > 0 && cp2.Skills[21].SE != 0)  {
				AddPlayerWidget(cp2, 'spells/s21.png', 'Block', 4);
				AddLog('' + cp2.PlayerName + ' blocked.');
				damage = 0;
			}
		
			if (damage > 0 && cp2.petdefense > 0) {
				var t = cp2.petdefense;
				if (damage > cp2.petdefense) {
					damage = damage - t;
					cp2.petdefense = 0;
				} else {
					t = damage;
					damage = 0;
					cp2.petdefense = cp2.petdefense - t;
				}
				AddLog('' + cp2.PlayerName + '`s physical pet blocked ' + t + ' damage.');
			}

			if (damage > 0)	 {

				var r= Retaliate(cp, cp2);
				damage = Math.round(damage);
	
				if (cp2.Skills[71].SkillValue > 0) {
					var r = cp2.Skills[71].SkillValue;
					if (r > damage) {
						r = damage;
					}

					AddLog('' + cp2.PlayerName + ' reduced damage by ' + r + '.',null,null, 9);
					damage = damage - r;
				}
				var mdamage = 0;
				if (cp.Skills[80].SkillValue > 0) {
					//Special damage from magical enchanted weapons and such.
					var md = cp.Skills[80].SkillValue;
					if (md < 0) {
						md = rand(7);
					}
					if (md == 1) {
						//fire damage
						if (rand(3) == 1) {
							mdamage = Math.round(rand(cp.Skills[7].SkillValue / 5));
					}
					} else if (md == 2) {
						//cold damage [freeze]
						if (rand(3) == 1) {
							DeBuffSkill(cp2.i, 4, 10000, 1);
						}
					} else if (md == 3) {
						//Earth damage [root]
						if (rand(3) == 1) {
							Root(cp2.i, 500);
						}
					} else if (md == 4) {
						//Water damage [damage]
						if (rand(3) == 1) {
							mdamage = Math.round(rand(cp.Skills[7].SkillValue / 5));
						}
					} else if (md == 5) {
						//Heal while you damage [heal]
						if (rand(3) == 1) {
							mdamage = Math.round(rand(cp.Skills[7].SkillValue / 5)) + 1;
							if (mdamage > (cp.Skills[1].SkillMax - cp.Skills[1].SkillValue)) {
								mdamage = (cp.Skills[1].SkillMax - cp.Skills[1].SkillValue);
							}
							if (mdamage > 0) {
								cp.Skills[1].SkillValue = cp.Skills[1].SkillValue + mdamage;
								AddLog('' + cp.PlayerName + '`s drains ' + mdamage + ' health.',null,null, 6);
							}
							mdamage = 0
					}
					} else if (md == 6) {
						//Heal power while you damage [heal]
						if (rand(3) == 1) {
							mdamage = Math.round(rand(cp.Skills[7].SkillValue / 5)) + 1;
							if (mdamage > (cp.Skills[2].SkillMax - cp.Skills[2].SkillValue)) {
								mdamage = (cp.Skills[2].SkillMax - cp.Skills[2].SkillValue);
							}
							if (mdamage > 0) {
								cp.Skills[2].SkillValue = cp.Skills[2].SkillValue + mdamage;
								AddLog('' + cp.PlayerName + '`s drains ' + mdamage + ' power.',null,null, 6);
							}
							mdamage = 0
						}
					} else if (md == 7) {
						//Acid on armor [heal]
						if (rand(3) == 1) {
							mdamage = Math.round(cp2.Skills[8].SkillMax / 7) + 1;
							if (mdamage > cp2.Skills[8].SkillValue) {
								mdamage = cp2.Skills[8].SkillValue;
							}
							if (mdamage > 0) {
								cp2.Skills[8].SkillValue = cp2.Skills[8].SkillValue - mdamage;
								AddLog('' + cp.PlayerName + '`s weakens opponents armor by ' + mdamage + ' pts (' + cp2.Skills[8].SkillValue + ').',null,null, 6);
							}
							mdamage = 0
						}	
					}
				}

				if (cp2.Skills[8].SkillValue > 0) {
					var d = Math.round((cp2.Skills[8].SkillValue / 5) - rand(cp2.Skills[8].SkillValue / 100));
					if (d > damage) {
						d = damage;
					}
					AddLog('' + cp2.PlayerName + '`s armor blocked ' + d + ' damage.', 9);
					damage = damage - d;
				}
				damage = damage + mdamage;
	
				if (damage > 0) {
					AddPlayerWidget(cp2, 'spells/s13.png', '-' + damage, 3);
					
					if (mdamage > 0) {
						AddLog('' + cp2.PlayerName + ' took ' + damage + ' damage from ' + cp.PlayerName  + (md == 1 ? ' and looks a little singed.' : (md == 4 ? ' and looks a little wet.' : '')) + '.',null,null, 6);
					} else {
						AddLog('' + cp2.PlayerName + ' took ' + damage + ' damage from ' + cp.PlayerName  + '.',null,null, 3);
					}
					cp2.Skills[1].SkillValue = cp2.Skills[1].SkillValue - damage;
				}

			}

		}


		if (IsPartyFight(cp2) == true && damage + cp2.tmpdmg > 0 && cp2.dead == 0) {
			PartyAction(7000, cp2.PlayerID, damage + cp2.tmpdmg);
			cp2.tmpdmg = 0;
		//} else if ((cp.Monster == 3 || cp.Monster == 4) && cp.tmpdmg > 0 && cp.dead == 0) {
		//	PartyAction(7000, cp.PlayerID, cp.tmpdmg);
		//	cp.tmpdmg = 0;
		}

		var cpx = 0;
		if (cp2.Skills[1].SkillValue <= 0) {
			//Target dead
			if (cp2.i == 1) {
				endfight = 1;
				cpx = cp.PlayerID;
			} else {
				endfight = 2;
				cpx = cp2.PlayerID;
			}
			cp2.Skills[1].SkillValue = cp2.Skills[1].SkillMax;
			cp2.dead = 1;
		}
		if (cp.Skills[1].SkillValue <= 0) {
			//Fighter dead
			if (cp.i == 1) {
				endfight = 1;
				cpx = cp2.PlayerID;
			} else {
				endfight = 2;
				cpx = cp.PlayerID;
			}

			cp.Skills[1].SkillValue = cp.Skills[1].SkillMax;
			cp.dead = 1;
		}

	}


	UpdateFightShort(a, b, 0);
	if (endfight == 0) {
		var sv1 = cp.Skills[69].SkillValue;
		if (sv1 < 1.5) {
			sv1 = 1.5;
		}
		clearTimeout(fight[(cp.i == 1 ? 1 : 2)]);
		fight[(cp.i == 1 ? 1 : 2)] = setTimeout('ContinueFight(' + cp.i + ', ' + cp2.i +  ');', sv1 * 1000);
	}
	if (endfight != 0) {
		clearTimeout(fight[1]);
		clearTimeout(fight[2]);
		clearTimeout(petfightt);
		//You won the fight
		if (IsPartyFight(cp2) == false || endfight == 1) { //Either if we are in a party fight or if the player fighting dies
			clearTimeout(fightend);
			fightend = setTimeout('PlayAction(5004,' + endfight + ', ' + cpx + ');', 500);
			if (autopiloton > 0 && IsPartyFight(cp2) == false && IsPartyFight(cp) == false) {
				cp.dead = 0;
				cp2.dead = 0;
				AddLog(autopiloton + ' auto fights remaining.',null,null, 11);
				clearTimeout(fightstart);
				fightstart = setTimeout('AutoFight(' + fighting + ');', 4000);
			}
		}
		fighting = 0;
	}
}

function IsPartyFight(cpx) {
	var x = false;
	if (cpx.Monster == 3 || cpx.Monster == 4) {
		x = true;
	}
	return x;
}

function Retaliate(cp, cp2) {

	if (cp2.Skills[89].SkillValue > 0) {
		//Retaliation damage
		var r = cp2.Skills[89].SkillValue;
		if (r >= cp.Skills[1].SkillValue) {
			r = cp.Skills[1].SkillValue - 1;
		}
		cp.Skills[1].SkillValue = cp.Skills[1].SkillValue - r;
		AddLog('' + cp2.PlayerName + ' deals ' + r + ' retaliation damage to ' + cp.PlayerName + '.',null,null, 7);
		cp.tmpdmg = cp.tmpdmg + r;
	}
}
		

function BuffSkill(Target, SkillID, Duration, BaseValue) {
	var t = Players[Target];
	if (t != null) {
		var l = Players[1].DiscoveryLevel;
		var d = Duration + GetCalc(1, l);
		var v = GetCalc(BaseValue, l);
		t.Skills[SkillID].SkillValue = t.Skills[SkillID].SkillValue + v;
		t.Skills[SkillID].SkillMax = t.Skills[SkillID].SkillMax + v;
		AddLog('Buff: ' + getSkillName(SkillID) + ' changed by ' + v + ' for ' + Mil2Sec(d) + '');
		setTimeout('Restore(' + t.PlayerID + ',' + Target + ',' + SkillID + ', -' + v + ');', d);
		t = null;
	}
}


function ReBuffSkill(Target, SkillID, Duration, BaseValue, iteration) {
	if (iteration == null) {
		iteration = 0
	}
	//Recurring buff
	var t = Players[Target];
	if (t != null && fighting != 0) {
		var l = Players[1].DiscoveryLevel;
		var d = Duration + GetCalc(1, l);
		var v = GetCalc(BaseValue, l);
		t.Skills[SkillID].SkillValue = t.Skills[SkillID].SkillValue + v;
		if (t.Skills[SkillID].SkillValue > t.Skills[SkillID].SkillMax) {
			t.Skills[SkillID].SkillValue = t.Skills[SkillID].SkillMax;
		}
		AddLog('Rebuff Buff: ' + getSkillName(SkillID) + ' changed by ' + v + ' for ' + Mil2Sec(d) + ' (' + iteration + ')');
		if (iteration == 0) {
			setTimeout('ReBuffSkill(' + Target + ',' + SkillID + ', ' + Duration + ', ' + BaseValue + ', 1);', d / 4);
			setTimeout('ReBuffSkill(' + Target + ',' + SkillID + ', ' + Duration + ', ' + BaseValue + ', 2);', (d / 4) * 2);
			setTimeout('ReBuffSkill(' + Target + ',' + SkillID + ', ' + Duration + ', ' + BaseValue + ', 3);', (d / 4) * 3);
		}
		t = null;
	}	
}

function XBuffSkill(Target, SkillID, Duration, BaseValue) {
	var l = Players[1].DiscoveryLevel;
	var d = Duration + GetCalc(1, l);
	var v = GetCalc(BaseValue, l);
	return 'Buff: ' + getSkillName(SkillID) + ' by ' + v + ' for ' + Mil2Sec(d) + '';
}

function XReBuffSkill(Target, SkillID, Duration, BaseValue, iteration) {
	if (iteration == null) {
		iteration = 0
	}
	//Recurring buff
	var l = Players[1].DiscoveryLevel;
	var d = Duration + GetCalc(1, l);
	var v = GetCalc(BaseValue, l);
	return 'Repeating Buff: ' + getSkillName(SkillID) + ' changed by ' + v + ' for ' + Mil2Sec(d) + ' * 4';
}


function XReDeBuffSkill(Target, SkillID, Duration, BaseValue, iteration) {
	if (iteration == null) {
		iteration = 0
	}
	//Recurring buff
	var l = Players[1].DiscoveryLevel;
	var d = Duration + GetCalc(1, l);
	var v = GetCalc(BaseValue, l);
	return 'Repeating Debuff: ' + getSkillName(SkillID) + ' changed by -' + v + ' for ' + Mil2Sec(d) + ' * 4';
}

function XDeBuffSkill(Target, SkillID, Duration, BaseValue) {
	var l = Players[1].DiscoveryLevel;
	var d = Duration + GetCalc(1, l);
	var v = GetCalc(BaseValue, l);
	return 'Debuff: ' + getSkillName(SkillID) + ' by -' + v + ' for ' + Mil2Sec(d) + '';
}

function XAddPet(PetID, PetName, Sprite, AI, p, level, ps) {
	var l = Players[1].DiscoveryLevel;
	return 'Summon Lvl ' + l + ' pet to slot ' + ps + '.';
}

function ReDeBuffSkill(Target, SkillID, Duration, BaseValue, iteration) {
	if (iteration == null) {
		iteration = 0
	}
	//Recurring buff
	var t = Players[Target];
	if (t != null && fighting != 0) {
		var l = Players[1].DiscoveryLevel;
		var d = Duration + GetCalc(1, l);
		var v = GetCalc(BaseValue, l);
		t.Skills[SkillID].SkillValue = t.Skills[SkillID].SkillValue - v;
		AddLog('Redebuff DeBuff: ' + getSkillName(SkillID) + ' changed by -' + v + ' for ' + Mil2Sec(d) + ' (' + iteration + ')', null, null, 10);
		if (iteration == 0) {
			setTimeout('ReDeBuffSkill(' + Target + ',' + SkillID + ', ' + Duration + ', ' + BaseValue + ', 1);', d / 4);
			setTimeout('ReDeBuffSkill(' + Target + ',' + SkillID + ', ' + Duration + ', ' + BaseValue + ', 2);', (d / 4) * 2);
			setTimeout('ReDeBuffSkill(' + Target + ',' + SkillID + ', ' + Duration + ', ' + BaseValue + ', 3);', (d / 4) * 3);
		}
		t = null;
	}	
}

function XRoot(Target, Duration) {
	// Root target, but as movement not a feature. Stun/Paralysis instead.
	var l = Players[1].DiscoveryLevel;
	var d = Duration + GetCalc(1, l);
	return 'Stunned for ' + d;
}

function Root(Target, Duration) {
	// Root target, but as movement not a feature. Stun/Paralysis instead.
	var t = Players[Target];
	if (t != null) {
		var l = Players[1].DiscoveryLevel;
		var d = Duration + GetCalc(1, l);
		t.Stunned = 1;
		AddLog('<div>Stunned for ' + Mil2Sec(d) + '');
		setTimeout('Unroot(' + Target + ');', d);
		t = null;
	}	
}

function Unroot(Target, Duration) {
	// UnRoot target
	var t = Players[Target];
	if (t != null) {
		t.Stunned = 0;
		AddLog('Stun expired');
	}
}

function DeBuffSkill(Target, SkillID, Duration, BaseValue) {
	//Negative buff
	var t = Players[Target];
	if (t != null) {
		var l = Players[1].DiscoveryLevel;
		var d = Duration + GetCalc(1, l);
		var v = GetCalc(BaseValue, l);
		t.Skills[SkillID].SkillValue = t.Skills[SkillID].SkillValue - v;
		t.Skills[SkillID].SkillMax = t.Skills[SkillID].SkillMax - v;
		AddLog('Debuff: ' + getSkillName(SkillID) + ' changed by ' + v + ' for ' + Mil2Sec(d) + '', null, null, 10);
		setTimeout('Restore(' + t.PlayerID + ',' + Target + ',' + SkillID + ', ' + v + ');', d);
		t = null;
	}
}

function Mil2Sec(d) {
	return Math.round(d / 1000) + 's'
}

function Restore(p, Target, SkillID, v) {
	var t = Players[Target];
	if (t != null) {
		if (t.PlayerID == p) {
			AddLog('Restore: ' + getSkillName(SkillID) + ' restored by ' + v + 'pts on ' + t.PlayerName + '');
			t.Skills[SkillID].SkillValue = t.Skills[SkillID].SkillValue + v;
			t.Skills[SkillID].SkillMax = t.Skills[SkillID].SkillMax + v;
		}
	}	
}

function GetCalc(d, l) {
	var l2 = (l / 10) + 1;
	return Math.round(d * l2);
}


function UpdateGroupFight(MonsterID, damage, who) {
	if (Players.length > 0) {
		for (x = 1; x <= Players.length; x++) {
			var cp2 = Players[x];
			if (cp2 != null) {
				if (cp2.PlayerID == MonsterID) {
					AddPlayerWidget(cp2, 'spells/s13.png', '-' + damage, 7);
					AddLog('' + cp2.PlayerName + ' took ' + damage + ' damage from ' + who + '.');
					cp2.Skills[1].SkillValue = cp2.Skills[1].SkillValue - damage;
					if (fighting != x) {
						UpdateFight(1, x);
					}
				}
				cp2 = null;
			}
		}	
	}
}

function IsFighting(f) {
	var y = false;
	if (Players.length > 0) {
		for (x = 1; x <= Players.length; x++) {
			var cp2 = Players[x];
			if (cp2 != null) {
				if (cp2.PlayerID == f) {
					y = true;
					cp2 = null;
					break;
				}
				cp2 = null;
			}
		}	
	}
	return y;
}

function AddPlayerWidget(p, image, text, c) {
	//Speech
	if (p != null && getObj('fightx' + p.i) != null) {
		if (p.dead == 0) {
			var objn = 'fightx' + p.i
			var x = findPos(getObj(objn))[0] + 40;
			var y = findPos(getObj(objn))[1];

			var t =  y + rand(20);
			var l =  x + rand(30);

			var w2 = document.createElement("div");
			w2.id = 'widgetx' + widgets;
			w2.style.position = "absolute";
			w2.style.left = l;
			w2.style.top = t;
			w2.style.width = 46;
			w2.style.height = 40;
			w2.style.zIndex = (1000 + 1);
			w2.innerHTML = '<img src=\'images/' + image + '\' width=16 height=16>' + ASCII(text,c, 'b' ) + '<br>'
			document.body.appendChild(w2);
			w2 = null;
			setTimeout('HideWidget(' + p.i + ',' + widgets + ');', 1500);
			setTimeout('MoveWidget(' + widgets + ',' + (t - 5) + ');', 250);
			widgets = widgets + 1;
		}
	}
}

function MoveWidget(w, t) {
	var js = getObj('widgetx' + w);
	if (js != null) {
		js.style.top = t;
		setTimeout('MoveWidget(' + w + ',' + (t - 5) + ');', 250);
	}
	js = null;
}

function HideWidget(pid, w) {
	var js = getObj('widgetx' + w);
	if (js != null) {
		js.parentNode.removeChild(js);
	}
	js = null;
}



function UpdateFight(a, f) {
	var cp = Players[a];
	var cp2 = Players[f];
	if (cp != null && cp2 != null && getObj('fightbox') != null) {
		var strout = '<table cellpadding=0 cellspacing=0><tr>';
		strout += '<td>' + getFightInterface(cp) + '</td>';
		strout += '<td>' + getFightInterface(cp2)  + '</td>';
		getFightOthers(f);
		getObj('fightbox').innerHTML = strout + '</tr><tr><td id=worldbuttons colspan=2></td></tr></table>';
		cp = null;
		cp2 = null;

	}
}

function UpdateFightShort(a, f, p) {
	var cp = Players[a];
	var cp2 = Players[f];
	if (cp != null && cp2 != null) {
		setFightInterface(cp, p);
		setFightInterface(cp2, 0);
		getFightOthers(f);
		cp = null;
		cp2 = null;
	}
}

function getFightOthers(f) {
	var x = 0;
	if (getObj('accountpass') == null) {
		var strout = '<table height=250 id=fightscreen>';
		for (x = 0; x <= Players.length; x++) {
			var p = Players[x];
			if (p != null) {
				if (p.Monster != 0) {
					strout += '<tr><td style="background-image: URL(/images/tag.png); width: 130px; cursor: pointer;" onclick="FightSetup(' + x + ');AutoFight(' + x + ');"><table cellpadding=0 cellspacing=0><tr><td width=100>' + ASCII(p.PlayerName) + '<br>' + (p.dead != 0 ? ASCII('DEAD') : ASCII('Level: ' + p.DiscoveryLevel)) + '</td><td><img src=\'images/races/' + p.portrait + '\' width=30 height=30</td></tr></table></td></tr>';
				}
				p = null;
			}
		}	
		strout += '<tr height="100%"><td colspan=2 valign=bottom align=center>' + Adar('ActionTimer(\'Fleeing fight...\', \'autopiloton = 0; CancelFight();PageHome();ExploreActual();\', 2, 2);','Flee Fight','Flee','button5','normal6.png', 1) + '</td></tr>';
		strout += '</table>';
		if (getObj('leftbox2') != null) {
			getObj('leftbox2').innerHTML = strout;
		}
	}
	return strout;
}

function EscapeFight() {
	//Escape / flee
}


function Shop() {
	MenuLoad();
	BigPageBox();
	SetMainBox('<table><tr><td align=center style="background-image: URL(player1.gif); background-position: center center; background-repeat: no-repeat;"><img src="portrait.png" width=100 height=100></td><td>' + ASCII('Shop Screen ... NPC Interaction') +'</td><tr></table>');
}

function SplitPageBox() {
	getObj('sidebox').style.display = '';
	getObj('sidebox').innerHTML =  DrawBoxT(0,0, 150, 360, 'leftbox', '');
	getObj('pagebox').innerHTML = DrawBoxT(0,0, 400, 360, 'mainbox', ''); 
	RefreshPlayerSmall();
}

function SplitPageBox2() {
	getObj('sidebox').style.display = '';
	getObj('sidebox').innerHTML =  DrawBoxT(0,0, 150, 360, 'leftbox2', '');
	getObj('pagebox').innerHTML = DrawBoxT(0,0, 400, 360, 'mainbox', ''); 
	//RefreshPlayerSmall();
}

function BigPageBox() {
	getObj('sidebox').style.display = 'none';
	getObj('pagebox').innerHTML = DrawBoxT(0,0, 570, 360, 'mainbox', ''); 
}

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else {
	        try{ return new ActiveXObject("MSXML3.XMLHTTP") }catch(e){}
	        try{ return new ActiveXObject("MSXML2.XMLHTTP.3.0") }catch(e){}
	        try{ return new ActiveXObject("Msxml2.XMLHTTP") }catch(e){}
	        try{ return new ActiveXObject("Microsoft.XMLHTTP") }catch(e){}
	}
}

function ISNULL(x, y) {
	if (x == null) {
		x = y;
	}
	return x;
}

function GetAuctionList(s, v) {
	PlayAction(v,0,0,1,0,'' + s, ISNULL(getObj('al1').value,0), ISNULL(getObj('al2').value,0));
}

function OpenAuctionMenuW(c, v) {
	AddButton(-3, 'Text','Text','Picture');
	AddSubMenu(-3, 'BOWS', '', '', 'GetAuctionList(23, ' + v + ');');
	AddSubMenu(-3, 'CROSSBOWS', '', '', 'GetAuctionList(24, ' + v + ');');
	AddSubMenu(-3, 'DAGGERS', '', '', 'GetAuctionList(22, ' + v + ');');
	AddSubMenu(-3, 'FISTS', '', '', 'GetAuctionList(30, ' + v + ');');
	AddSubMenu(-3, 'INSTRUMENTS', '', '', 'GetAuctionList(29, ' + v + ');');
	AddSubMenu(-3, 'AXES', '', '', 'GetAuctionList(16, ' + v + ');');
	AddSubMenu(-3, 'MACES', '', '', 'GetAuctionList(18, ' + v + ');');
	AddSubMenu(-3, 'SWORDS', '', '', 'GetAuctionList(14, ' + v + ');');
	AddSubMenu(-3, 'POLEARMS', '', '', 'GetAuctionList(20, ' + v + ');');
	AddSubMenu(-3, 'SHIELDS', '', '', 'GetAuctionList(21, ' + v + ');');
	AddSubMenu(-3, 'STAVES', '', '', 'GetAuctionList(13, ' + v + ');');
	AddSubMenu(-3, 'THROWN', '', '', 'GetAuctionList(32, ' + v + ');');
	AddSubMenu(-3, 'LARGE AXES', '', '', 'GetAuctionList(17, ' + v + ');');
	AddSubMenu(-3, 'LARGE SWORDS', '', '', 'GetAuctionList(15, ' + v + ');');
	ShowSubMenu(-3,'aucfil',0);
}

function OpenAuctionMenuR(c, v) {
	AddButton(-3, 'Text','Text','Picture');
	AddSubMenu(-3, 'Fish', '', '', 'GetAuctionList(-16, ' + v + ');');
	AddSubMenu(-3, 'Ore', '', '', 'GetAuctionList(-17, ' + v + ');');
	AddSubMenu(-3, 'Wood', '', '', 'GetAuctionList(-18, ' + v + ');');
	AddSubMenu(-3, 'Plant', '', '', 'GetAuctionList(-19, ' + v + ');');
	AddSubMenu(-3, 'Fruit', '', '', 'GetAuctionList(-20, ' + v + ');');
	ShowSubMenu(-3,'aucfil',0);
}

function FindPlan(skillid, lrange, lrange2) {
	if (lrange2 == null) {
		lrange2 = (Math.round(lrange) + 20)
	}
	var sn = '';
	if (skillid == 28) {
		sn = 'WEAPONSMITHING';
	} else if (skillid == 61) {
		sn = 'ENCHANTING';
	} else if (skillid == 46) {
		sn = 'JEWELRYCRAFTING';
	} else if (skillid == 45) {
		sn = 'ARMORCRAFTING';
	} else if (skillid == 50) {
		sn = 'TRAPS';
	} else if (skillid == 33) {
		sn = 'ALCHEMY';
	}
	window.open('PlanSearch.asp?4=' + lrange + '&5=' + lrange2 + '&6=' + sn,'library','height=255,width=250,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');
}


function UploadScreen() {
	window.open('services/Uploader.asp','Upload','height=415,width=420,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');
}

function PlanFilterMenu(skillid, lrange, lrange2) {
	AddButton(-3, 'Text','Text','Picture');
	var ox = 50;
	var r = 200;
	if (skillid == 28) {
		r = 100;
		ox = 20;
	} else if (skillid == 45) {
		ox = 10;
		r = 50;
	}
	AddSubMenu(-3, 'Level 0-' + ox, '', '', 'FilterPlans(0, ' + skillid + ', ' + ox + ');');
	var x = 0;
	lrange = Math.round(lrange);

	for (x = (lrange - r); x <= (lrange + r); x=x+ox) {
		if (x > (5000 - ox) || x < 0) {
		} else {
			AddSubMenu(-3, '' + x + '-' + (x + ox), '', '', 'FilterPlans(' + x + ', ' + skillid + ',' + (x + ox) +  ');');
		}
	}	
	AddSubMenu(-3, 'Other', '', '', 'PlanFilterMenu2(' + skillid + ', ' + lrange + ', ' + lrange2 + ');');
	ShowSubMenu(-3,'InvxPages',0);
}

function PlanFilterMenu2(skillid, lrange, lrange2) {
	lrange = Math.round(lrange);
	if (lrange2 == null) {
		lrange2 = lrange + 20;
	}
	getObj('moviebox').style.display = '';
	getObj('moviebox').innerHTML = '<center><div align=center valign=center  style="WIDTH:320;height:184; padding-top: 120px;"><table style="border: 2px solid black; background-image: URL(maskBG.png);"><tr><td>' + ASCII('Level range:') +'</td><td><input name="lr1" id="lr1" onkeypress="return fxkp(event);" size=5 maxlength=5 value="' + lrange + '"></td><td>-</td><td><input name="lr2" id="lr2" onkeypress="return fxkp(event);" size=5 maxlength=5 value="' + (lrange2) + '"></td><td colspan=2>' + Adar('getObj(\'moviebox\').style.display = \'none\';FilterPlans(getObj(\'lr1\').value,' + skillid + ',getObj(\'lr2\').value);','Set the level range','Set','sbutton1','snormal3.png', 1) + '</td><td colspan-2>' + Adar('getObj(\'moviebox\').style.display = \'none\';','Close','Close','sbutton1','snormal1.png', 1) + '</td></tr></table></div></center>';
}

function FilterPlans(lrange, skillid, lrange2) {
	if (lrange2 == null) {
		lrange2 = lrange + 20;
	}
	PlayAction(12,0,skillid,1,0,'' + lrange, lrange2, 0);
}

function OpenAuctionMenuC(c, v) {
	AddButton(-3, 'Text','Text','Picture');
	AddSubMenu(-3, 'CLOTHING', '', '', 'GetAuctionList(53, ' + v + ');');
	AddSubMenu(-3, 'LIGHT ARMOR', '', '', 'GetAuctionList(54, ' + v + ');');
	AddSubMenu(-3, 'MEDIUM ARMOR', '', '', 'GetAuctionList(55, ' + v + ');');
	AddSubMenu(-3, 'HEAVY ARMOR', '', '', 'GetAuctionList(56, ' + v + ');');
	ShowSubMenu(-3,'aucfil',0);
}

function PlayAction(a, av, av2, p, delay, av3, av4, av5) {
	if (Done != 0) {
		if (a == 1001) {
			//Inventory
			//disable box
			var i = 0;
			i = getObj('Inventory');
			if (i != null) {
				i.innerHTML = ASCII('Performing action ... please wait!', 8);
			}
			i = null;
		}
		RemoveTiny('messagebox');
		clearTimeout(actiontimeout);
		clearTimeout(actiontimeout2);
		clearInterval(ButtonTimer2);
		clearInterval(ButtonTimerEnd);
		if (a != 7000 && a != 7004) {
			CancelFight();
		}

		//if (delay > 0) {
		//	clearTimeout(delayt);
		//	delayt = setTimeout("sendRequest('load_ajax.asp?A=" + a + "&Value=" + av + "&A2=" + av2 + "&P=" + p + "&A4=" + av4 + "&A3=" + av3 + "');",350);
		//} else {
			sendRequest('load_ajax.asp?A=' + a + '&Value=' + av + '&A2=' + av2 + '&P=' + p + '&A4=' + av4 + '&A5=' + av5 + '&A3=' + av3);
		//}
	}
}

function PartyAction(a, av, av2, p, delay, av3, av4, av5) {
	sendParty('load_ajax.asp?A=' + a + '&Value=' + av + '&A2=' + av2 + '&P=' + p + '&A4=' + av4 + '&A5=' + av5 + '&A3=' + av3);
}


function getPowers(cp) {
	var x = 0;
	var strout = new Array();
	l = 1;
	for (x = 0; x <= Powers.length; x++) {
		var p = Powers[x];
		if (p != null) {
			//
			if (p.faith == cp.faithid || (cp.Monster != 0 && p.DefenseSkill != 8)) {
				if (HasSkill(cp, p.SkillID) > 0) {
					strout[l] = new Array();
					strout[l] = x;
					l = l + 1;
				}
			}
			p = null;
		}
	}	
	return strout;
}

function getMonsterColor(m) {
	var x = 1;
	if (m == 2) {
		x = 3;
	} else if (m == 3) {
		x = 6;
	} else if (m == 4) {
		x = 11;
	}
	return x;
}

function getFightInterface(cp) {
	var strout = '';
	if (cp != null) {
		var ar = getPowers(cp);
		var mc = getMonsterColor(cp.Monster);
		strout = '<div id=fightx' + cp.i + ' style="width: 180px; height: 290px; background-image: URL(images/fight' + (cp.i == 1 ? 1 : 2) + '.png); background-repeat: no-repeat; background-position: top center;"><center>' + ASCII(cp.PlayerName, mc) + '<br>' + ASCII('Level: ' + cp.DiscoveryLevel) + ' ' + (cp.Monster == 3 ? ASCII('[Party]', mc) : (cp.Monster == 4 ? ASCII('[Raid Boss]', mc) : '')) + '</center><div style="padding-left: 10px;" id="fightstats' + cp.i + '">' + GetBox2('Health', 'bigred', (cp.Skills[1].SkillValue > cp.Skills[1].SkillMax ? cp.Skills[1].SkillMax : cp.Skills[1].SkillValue), cp.Skills[1].SkillMax, 150) + GetBoxSmall2('Power', 'bigblue', (cp.Skills[2].SkillValue > cp.Skills[2].SkillMax ? cp.Skills[2].SkillMax : cp.Skills[2].SkillValue), cp.Skills[2].SkillMax, 150) + '</div><br><div style=\'background-image: URL(images/races/' + cp.portrait + '); background-repeat: no-repeat; background-position: 40 40;\'><table cellpadding=0 cellspacing=1 style=\'width: 180px; background-image: URL(images/fightcircle' + getFightCircle(cp) + '.png); background-repeat: no-repeat;\'>';
		strout += '<tr height=40><td></td><td align=center id="power1-' + cp.i + '">' + getPower(cp, ar, 1) + '</td><td align=center id="power2-' + cp.i + '">' + getPower(cp, ar, 2) + '</td><td></td></tr>';
		strout += '<tr height=100><td width=40><font id="power3-' + cp.i + '">' + getPower(cp, ar, 3) + '</font><br><font id="power4-' + cp.i + '">' + getPower(cp, ar, 4) + '</font></td><td colspan=2 width=100 valign=center>' + (cp.dead == 0 ? '&nbsp;'  : '<center>' + ASCII('Dead', 3) + '</center>') + '</td><td width=40><font id="power5-' + cp.i + '">' + getPower(cp, ar, 5) + '</font><br><font id="power6-' + cp.i + '">' + getPower(cp, ar, 6) + '</font></td></tr>';
		strout += '<tr height=40><td></td><td align=center id="power7-' + cp.i + '">' + getPower(cp, ar, 7) + '</td><td align=center id="power8-' + cp.i + '">' + getPower(cp, ar, 8) + '</td><td id="power9-' + cp.i + '">' + getCombatItem(cp) + '</td></tr>';
		strout += '<tr><td colspan=4 id="fightpets' + cp.i + '"><table cellpadding=0 cellspacing=1><tr>'
		var y = 0;
		for (y = 1; y < cp.Pets.length; y++) {
			var p = cp.Pets[y];
			if (p != null) {
				strout += '<td title="' + p.PetName + ', Level ' + p.level  + '" style="width: 40px; height: 40px; padding: 1px; background-image: URL(images/fightsquare.png); background-repeat: no-repeat;"><img src="images/races/' + p.Sprite + '" width=36 height=36></td>';
				p = null;
			}
		}	
		strout += '</tr></table></td></tr></table></div></div>';
	}
	return strout;
}

function getFightCircle(cp) {
	//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	var r = '';
	if (cp.Skills != null) {
		var s = cp.Skills[80];
		if (s != null) {
			if (s.SkillValue > 7 || s.SkillValue <= 0) {
				r = '';
			} else {
				r = '' + s.SkillValue;
			}
		}
	}
	return r;
}

function getCombatItem(cp) {
	var strout = '';
	if (cp.Skills[77].SkillValue > 0 || cp.Skills[78].SkillValue > 0) {
		if (fighting <= 0) {
		} else {
			var d = new Date();
			if ((Math.abs(d.getTime()) - LastCombatItem) > 5000 || LastCombatItem == 0) {
				//AddLog('' + (Math.abs(d.getTime())) + ' ' + LastCombatItem);
				strout = Adaca('UseCombatItem(' + cp.i + ', ' + fighting + ');', 'Use Combat Item','1', 'buttonc','circle6b.png', null, 'spells/s44.png','GetCombatItemInfo();');
			} else {
				strout = Adaca('', 'Combat item on cooldown.','1', 'buttonc','circle0b.png', null, 'spells/s44.png','GetCombatItemInfo();');
			}
		}
	} else {
		//No food stuff
		//strout = Adaca('', 'You have no combat item equipped.','1', 'buttonc','circle0a.png', null, 'spells/s44.png','You can Equip an item of food or a salve or poltice to the combat item slot.');
	}
	return strout;
}

function UseCombatItem(x, f) {
	var cp = Players[x];
	var e = 0;
	e = cp.Skills[77].SkillValue;
	if (e > 0) {
		if (e > cp.Skills[1].SkillMax - cp.Skills[1].SkillValue) {
			e = cp.Skills[1].SkillMax - cp.Skills[1].SkillValue;
		}
		cp.Skills[1].SkillValue = cp.Skills[1].SkillValue + e;
		AddLog('You use a combat item and regain ' + e + ' health.')
	}
	e = cp.Skills[78].SkillValue
	if (e > 0) {
		if (e > cp.Skills[2].SkillMax - cp.Skills[2].SkillValue) {
			e = cp.Skills[2].SkillMax - cp.Skills[2].SkillValue;
		}
		cp.Skills[2].SkillValue = cp.Skills[2].SkillValue + e;
		AddLog('You use a combat item and regain ' + e + ' power.')
	}
	var d = new Date();
	LastCombatItem = Math.abs(d.getTime());
	UpdateFightShort(x, f, 9);
}

function GetCombatItemInfo() {
	var cp = Players[1];
	if (cp != null) {
		LoadFetch('<table><tr><td>' + ASCII('Combat Item', 6) + '</td></tr><tr><td>' + ASCII((cp.Skills[77].SkillValue > 0 ? 'Heals: ' + cp.Skills[77].SkillValue : '') + (cp.Skills[78].SkillValue > 0 ? 'Power: ' + cp.Skills[78].SkillValue : ''), 1) + '</td></tr><tr><td>' + ASCII('Cooldown: 10s') + '</td></tr></table>', 1, 1, 1, null, 9000);
	}
}

function setFightInterface(cp, s) {
	if (s == null) {
		s = 0;
	}
	var strout = '';
	if (cp != null) {
		var ar = getPowers(cp);

		if (s > 0 && s <= 9) {
			//Just update one power
			var p = getObj('power' + s + '-' + cp.i);
			if (p != null) {
				if (s == 9) {
					p.innerHTML = '' + getCombatItem(cp);
				} else {
					p.innerHTML = '' + getPower(cp, ar, s);
				}
				p = null;
			}
		} else {
			var y = 0;
			for (y = 1; y <= 8; y++) {
				var p = getObj('power' + y + '-' + cp.i);
				if (p != null) {
					p.innerHTML = '' + getPower(cp, ar, y);
					p = null;
				}
			}	

			var p = getObj('power9-' + cp.i);
			if (p != null) {
				p.innerHTML = '' + getCombatItem(cp);
				p = null;
			}
		}

		if (getObj('fightstats' + cp.i) != null) {
			getObj('fightstats' + cp.i).innerHTML = GetBox2('Health', 'bigred', (cp.Skills[1].SkillValue > cp.Skills[1].SkillMax ? cp.Skills[1].SkillMax : cp.Skills[1].SkillValue), cp.Skills[1].SkillMax, 150) + GetBoxSmall2('Power', 'bigblue', (cp.Skills[2].SkillValue > cp.Skills[2].SkillMax ? cp.Skills[2].SkillMax : cp.Skills[2].SkillValue), cp.Skills[2].SkillMax, 150);
		}
	
		strout = ''
		if (getObj('fightpets' + cp.i) != null) {
			strout += '<table cellpadding=0 cellspacing=1><tr>';
			for (y = 1; y < cp.Pets.length; y++) {
				var p = cp.Pets[y];
				if (p != null) {
					strout += '<td title="' + p.PetName + ', Level ' + p.level  + '" style="width: 40px; height: 40px; padding: 1px; background-image: URL(images/fightsquare.png); background-repeat: no-repeat;"><img src="images/races/' + p.Sprite + '" width=36 height=36></td>';
					p = null;
				}
			}	
			strout += '</tr></table>';
			getObj('fightpets' + cp.i).innerHTML = strout;
		}
	}
	return strout;
}


function getPaperdoll(y) {
	var strout = '';
	var cp = Players[1];
	if (cp != null) {
		strout = '<table style="width: 350px; height: 230px;"><tr><td width=180><div style=\'background-image: URL(images/races/' + cp.portrait + '); background-repeat: no-repeat; background-position: 40 40;\'><table cellpadding=0 cellspacing=1 style=\'width: 180px; background-image: URL(images/equipcircle3.png); background-repeat: no-repeat;\'>';
		strout += '<tr height=40><td></td><td align=center id=Loc1>' + getESlot(cp, 1) + '</td><td align=center id=Loc4>' + getESlot(cp, 4) + '</td><td></td></tr>';
		strout += '<tr height=100><td width=40>' + getESlot(cp, 5) + '<br>' + getESlot(cp, 7) + '</td><td colspan=2 width=100 valign=center align=center>&nbsp;</td><td width=40>' + getESlot(cp, 9) + '<br>' + getESlot(cp, 10) + '</td></tr>';
		strout += '<tr height=40><td></td><td align=center id=Loc11>' + getESlot(cp, 11) + '</td><td align=center id=Loc12>' + getESlot(cp, 12) + '</td><td>' + getESlot(cp, 17) + '</td></tr>';
		strout += '<tr height=40><td id=Loc2>' + getESlot(cp, 2) + '</td><td align=center id=Loc3>' + getESlot(cp, 3) + '</td><td align=center id=Loc6>' + getESlot(cp, 6) + '</td><td id=Loc8>' + getESlot(cp, 8) + '</td></tr>';
		strout += '<tr height=40><td id=Loc13>' + getESlot(cp, 13) + '</td><td align=center id=Loc14>' + getESlot(cp, 14) + '</td><td align=center id=Loc15>' + getESlot(cp, 15) + '</td><td id=Loc16>' + getESlot(cp, 16) + '</td></tr>';
		strout += '</table></div></td><td valign=top></td></tr></table>';
		cp = null;
	}
	return strout;
}

function getPaperdoll2(y) {
	getObj('equips').innerHTML = getPaperdoll(y);
}

function getESlot(cp, x) {
	var strout = '';
	var t = cp.Slots[x];
	if (t != null) {
		if (t.PlayerItemID == 0) {
			strout = Adaca('', '' + t.LocationName + '','', 'buttonc','fightsquare2.png', null, '../images/items/l' + t.PictureID);
		} else {
			strout = Adaca('Unequip(' + t.PlayerItemID + ',' + x + ');', '' + t.ItemName + ' - ' + t.LocationName + '','', 'buttonc','fightsquare2.png', null, '../images/items/' + t.PictureID, 'Fetch(' + x + ',' + t.PlayerItemID + ', 1);','FetchOff();');
		}
	} else {
		strout = '<img src=\'images/fightsquare.png\' title=\'Nothing equipped\'>';
	}
	return strout;
}

function ClearESlots() {
	Players[1].Slots = new Array();
}

function AdEdSlot(PlayerItemID, ItemID, ItemName, LocationName, LocationID) {
	var x = LocationID;
	Players[1].Slots[x] = new Array();
	Players[1].Slots[x] = new UpdateESlot(PlayerItemID, ItemID, ItemName, LocationName, LocationID);

}

function UpdateESlot(PlayerItemID, ItemID, ItemName, LocationName, LocationID) {
this.PictureID = ItemID + '.png';
this.PlayerItemID = PlayerItemID;
this.ItemID = ItemID;
this.ItemName = ItemName;
this.LocationName = LocationName;
this.LocationID = LocationID;
}

function getAccountCharacters(y, tp) {
	var strout = '';
	if (tp == null) {
		tp = 1;
	}
	var ar = Slots[y];
	if (ar != null) {
		strout = '<table style="width: 350px; height: 230px;"><tr><td width=180><center><b>' + ASCII(ar.SlotName) + '</b></center>' + Adar('AccountScreen(1,' + ar.ID + ',0);','Play ' + ar.SlotName, 'Play','button5','normal5.png', 1) + '' + Adar('AccountScreen(0, -2,' + ar.ID + ');','Delete ' + ar.SlotName, '<img src="images/gui/delete.png" width=16 border=0 height=16>','sbutton1','snormal1.png', 1) + '<br><div style=\'background-image: URL(images/races/' + ar.PictureID + '); background-repeat: no-repeat; background-position: 40 40; border: 1px dotted black; width: 180px; height: 230px;\' align=left><table align=left cellpadding=0 cellspacing=1 style=\'width: 180px; background-image: URL(images/fightcircle.png); background-repeat: no-repeat;\'>';
		strout += '<tr height=40><td>' + (tp > 1 ? Adar('AccountPage(1);','Page 1', 'Pg 1', 'sbutton1','snormal1.png') : '') + '</td><td align=center>' + getSlot(1) + '</td><td align=center>' + getSlot(2) + '</td><td>' + (tp > 1 ? Adar('AccountPage(2);','Page 2', 'Pg 2', 'sbutton1','snormal1.png') : '') + '</td></tr>';
		strout += '<tr height=100><td width=40>' + getSlot(3) + '<br>' + getSlot(4) + '</td><td colspan=2 width=100 valign=center align=center onclick=\'AccountScreen(1,' + ar.ID + ',0);\' title=\'Click to play as ' + ar.SlotName + '\' style=\'cursor: pointer;\'>&nbsp;</td><td width=40>' + getSlot(5) + '<br>' + getSlot(6) + '</td></tr>';
		strout += '<tr height=40><td>' + (tp > 2 ? Adar('AccountPage(3);','Page 3', 'Pg 3', 'sbutton1','snormal1.png') : '') + '</td><td align=center>' + getSlot(7) + '</td><td align=center>' + getSlot(8) + '</td><td></td></tr>';
		strout += '</table></div></td><td valign=top width=100><table><tr><td>' + ASCII('Level: ' + ar.Level) + '</td></tr><tr><td>' + ASCII('Story:') + '</td></tr><tr><td><img align=center title=\'' + GetStory(ar.story) + '\' src=\'images/story/' + ar.story + '.png\'></td></tr><tr><td>' + ASCII('Faith:') + '</td></tr><tr><td><img align=center title=\'' + GetFaith(ar.faith) + '\' src=\'images/faith/' + ar.faith + '.png\'></td></tr></table></td></tr></table>';
	} else {
		strout = '' + ASCII('Welcome to Champion of the Gods, you currently do not have any characters. It is time you began your journey, are you ready?<br>') + Adar('AccountScreen(1,-1,0);','Yes, give me hours of role playing goodness!', 'Yes','sbutton1','snormal3.png', 1) + Adar('YouSaidWhat();','No, the responsibility is too much!', 'No','sbutton1','snormal1.png', 1);
	}
	return strout;
}

function AccountPage(p) {
	//xxxxx
	AccountScreen(p,0,0);
}

function YouSaidWhat() {
	getObj('chars').innerHTML = ASCII('Alas your journey has not even begun and you are already leaving.<br><br>The gods look forward to seeing you again soon when you are ready to take on the challenge and be one of their champions!<br><br>Are you sure you wish to leave?<br>') + Adar('AccountScreen(0,0,0);','You may have another chance, if you change your mind!', 'No','sbutton1','snormal1.png', 1) +  Adar('window.location.replace(\'http://www.google.com\');','Sends you off to fight the demons of the Internet!', 'I am sure','button5','normal5.png', 1);
}

function getAccountCharacters2(y) {
	getObj('chars').innerHTML = getAccountCharacters(y);
}

function getSlot(x) {
	var strout = '';
	var t = Slots[x];
	if (t != null) {
		strout = Adaca('getAccountCharacters2(' + x + ');', '' + t.SlotName + ', Level ' + t.Level + ', ' + GetStory(t.story) + ', ' + GetFaith(t.faith) + '','' + x + '', 'buttonc','fightsquare2.png', null, '../images/races/' + t.PictureID);
	} else {
		strout = '<img src=\'images/fightsquare.png\' title=\'Empty slot - Click to make new Character\' style=\'cursor: pointer;\' onclick=\'AccountScreen(1,-1,0);\'>';
	}
	return strout;
}

function ClearSlots() {
	Slots = new Array();
}

function AddSlot(PlayerID, PlayerName, Level, PictureID, f, s) {
	var x = Slots.length;
	if (x <= 0) {
		x = 1;
	}
	Slots[x] = new Array();
	Slots[x] = new UpdateSlot(PlayerID, PlayerName, Level, PictureID, x, f, s);

}

function UpdateSlot(PlayerID, PlayerName, Level, PictureID, x, f, s) {
this.PictureID = PictureID;
this.SlotName = PlayerName;
this.Level = Level;
this.x = x;
this.ID = PlayerID;
this.faith = f;
this.story = s;
}

function TriggerPower2(slot) {
	var c = 1;
	if (fighting <= 0) {
	} else {
		//In a fight
		var cp = Players[c];
		if (cp != null) {
			var ar = getPowers(cp);	
		
			var a = ar[slot];
			if (a != null) {
				TriggerPower(c, a, slot, fighting)
			}
			ar = null;
		}
		cp = null;
	}
}

function TriggerPower(c, px, slot, f) {
	if (fighting <= 0) {
		AutoFight(2);
		f = -1;
	}
	var p = Powers[px];
	var fail = 0;
	if (p != null && f > 0) {
		var d = new Date();
		var cp = Players[c];
		if (cp != null) {
			var pc = 0;
			pc = GetCalcP(p.Power, cp.DiscoveryLevel);
			if (pc > cp.Skills[2].SkillValue) {
				fail = 1;
			}
		}

		//if ((Math.abs(d.getTime()) - (t == null ? 100 : t)) > Powers[a].cooldown && cp.dead == 0)

		if (fail == 0) {
			var t = cp.Powers[slot];
			if ((Math.abs(d.getTime()) - (t == null ? 100 : t)) > p.cooldown && cp.dead == 0) {
				cp.Powers[slot] = Math.abs(d.getTime());
				if (getObj('power' + slot + '-' + c) != null) {
					getObj('power' + slot + '-' + c).innerHTML = Adaca('', '' + p.PowerName + '','' + px + '', 'buttonc','circle0a.png', null, 'spells/s' + p.SkillID + 'b.png','GetPowerInfo(' + px + ');');
				}
				var cp2 = Players[f];
				if (cp2 != null) {
					AddPlayerWidget(cp, 'spells/s' + p.SkillID + 'b.png', '' + p.PowerName, 4);
					if (p.Damage < 0) {
						var d = Math.abs(p.Damage);
						d = GetCalc(d, cp.DiscoveryLevel);
						if (p.DefenseSkill == 8) {
							d = d - Math.round(cp2.Skills[8].SkillValue / 10); //Damage minus armor
						} else {
							d = d + cp.Skills[67].SkillValue;
						}
						if (d < 0) {
							d = 0;
						}
						if (d > 0 && cp2.petmagicdefense > 0) {
							var t = cp2.petmagicdefense;
							if (d > cp2.petmagicdefense) {
								d = d - t;
								cp2.petmagicdefense = 0;
							} else {
								t = d;
								d = 0;
								cp2.petmagicdefense = cp2.petmagicdefense - t;
							}
							AddLog('' + cp2.PlayerName + '`s magical pet blocked ' + t + ' d.');
						}
						cp2.Skills[1].SkillValue = cp2.Skills[1].SkillValue - d;
						cp2.tmpdmg = cp2.tmpdmg + d;
						AddLog('' + p.PowerName + ' executed for ' + d + ' dmg!', 4);
						var r = Retaliate(cp, cp2);
					} else if (p.Damage > 0 && (p.startjs == '' || p.startjs == ' ')) {
						var d = Math.abs(p.Damage);
						d = GetCalc(d, cp.DiscoveryLevel);
						d = d + cp.Skills[68].SkillValue;
						cp.Skills[1].SkillValue = cp.Skills[1].SkillValue + d;
						AddLog('' + p.PowerName + ' cast for ' + d + ' dmg healed!', 4);
					} else {
						//Buff or Debuff
						AddLog('' + p.PowerName + ' triggered!', 4);
						if (p.startjs != '' && p.startjs != ' ') {
							//text = text.replace(/\{M\}/g, cp.Mother);
		
							var s = p.startjs.replace("\{P\}", f);
							s = s.replace("\{C\}", c);
							s = s.replace("\{I\}", cp.PlayerID);
	
							s = s.replace("\{L\}", cp.DiscoveryLevel);
							eval(s);
						}
					}
					cp.Skills[2].SkillValue = cp.Skills[2].SkillValue - pc;
					UpdateFightShort(c, f, slot);
				}
			} else {
				AddLog('' + p.PowerName + ' is on cooldown.');
			}
			cp = null;
		} else {
			AddLog('' + p.PowerName + ' requires ' + pc);
		}
		p = null;

	}
}

function GetCalc(d, l) {
	var l2 = (l / 10) + 1;
	return Math.round(d * l2);
}

function GetCalcP(d, l) {
	var l2 = (l / 15) + 1;
	return Math.round(d * l2);
}

function getPower(cp, ar, x) {
	var a = ar[x];
	var strout = '';
	if (a != null && cp != null) {
		var t = null;
		try {
			t = cp.Powers[x];
		} catch (e) {
		}
		var d = new Date();
		var p = Powers[a];
		if ((Math.abs(d.getTime()) - (t == null ? 100 : t)) > p.cooldown && cp.dead == 0 && cp.i == 1) {
			if (cp.Stunned == 1) {
				strout = Adac('', 'STUNNED - ' +p.PowerName + '','' + x + '', 'buttonc','circle' +p.faith + 'b.png', null, 'spells/s' +p.SkillID + 'b.png');
			} else {
				strout = Adaca('TriggerPower(1, ' + a + ', ' + x + ', ' + fighting + ');', '' +p.PowerName + '','' + x + '', 'buttonc','circle' +p.faith + 'b.png', null, 'spells/s' +p.SkillID + 'b.png','GetPowerInfo(' + a + ');');
			}
		} else {
			strout = Adaca('', '' +p.PowerName + '','' + x + '', 'buttonc','circle0a.png', null, 'spells/s' +p.SkillID + 'b.png','GetPowerInfo(' + a + ');');
		}
	} else {
		strout = '<img src=\'images/circle0a.png\' title=\'No Power\'>';
	}
	return strout;
}

function TrackPage(URL) {

        try {
		if (window.pageTracker != null) {
			pageTracker._trackPageview("/" + URL);
		}
	} catch(e) {
	}	
}

function GetPowerInfo(a) {
	var p = Powers[a];
	if (p != null) {
		var cp = Players[1];
		if (cp != null) {
			LoadFetch('<table><tr><td>' + ASCII(p.PowerName,6) + '</td></tr><tr><td>' + ASCII(p.Damage < 0 ? 'Damages for ' + GetCalc(Math.abs(p.Damage), cp.DiscoveryLevel) : (p.Damage > 0 && (p.startjs == '' || p.startjs == ' ') ? 'Heals for ' + GetCalc(Math.abs(p.Damage), cp.DiscoveryLevel) : (p.startjs != '' ? GetJS(p.startjs) : 'Other Effect'))) + '</td></tr><tr><td>' + ASCII('Power: ' + GetCalcP(p.Power, cp.DiscoveryLevel)) + '</td></tr><tr><td>' + ASCII('Cooldown: ' + (p.cooldown / 1000) + 's') + '</td></tr></table>', 1, 1, 1, null, 9000);
			cp = null;
		}
		p = null;
	}
}

function GetJS(startjs) {
	var s = startjs.replace("\{P\}", 1);
	s = s.replace("\{C\}", 1);
	s = s.replace("\{I\}", 1);
	var cp = Players[1];
	if (cp != null) {
		s = s.replace("\{L\}", cp.DiscoveryLevel);
	} else {
		s = s.replace("\{L\}", 1);
	}
	cp = null;
	s = 'X' + s.replace(';', '');
	return eval(s);
}

function AddPlayer(PlayerID, PlayerName, LP, PictureID, MapX, MapY, NPC, Monster, storyid, respawn, dl, title, emotion, portrait, faithid, petdata, sex, mother, father) {
	var x = 0;
	var match = 0;
	if (PlayerCount > 0 && FindPlayer >= 1) {
		for (x = 1; x <= PlayerCount; x++) {
			if (Players[x] != null) {
				if (Players[x].PlayerID == PlayerID) {
					match = x;
					break;
				}
			} else if (FindPlayer == 2) {
				match = x;
				break;
			}
		}	
	}

	if (match == 0) {
		PlayerCount = PlayerCount + 1;
		if (Players[PlayerCount] == null) {
			
		}
		x = PlayerCount;
	}

	if (x > 0) {
		Players[x] = new UpdatePlayer(PlayerID, PlayerName, LP, PictureID, MapX, MapY, NPC, Monster, storyid, x, respawn, 0, dl, title, emotion, portrait, faithid, petdata, sex, mother, father);
		if (petdata == null) {
			petdata = '';
		}
		if (petdata != '') {
			eval(petdata.replace("\{P\}", x));
		}
	}
}


function AddPet(PetID, PetName, Sprite, AI, p, level, ps) {
	var z = 0;
	var match = -1;
	for (z = 1; z <= PlayerCount; z++) {
		var cp = Players[z];
		if (cp != null) {
			if (cp.PlayerID == p) {
				match = z;
				break;
			}
		}
	}	

	if (match != -1) {
		if (ps == null) {
			ps = 1;
		}

		RemovePet(cp,ps);

		var pc = ps;
		cp.Pets[pc] = new Array();
		cp.Pets[pc] = new UpdatePet(PetID, PetName, Sprite, AI, level, match);
	}
	cp = null;
}

function RemovePets(x) {
	if (x == null) {
		x = 1;
	}
	var cp = Players[x];
	if (cp != null) {
		cp.petdata = '';
		cp.Pets = new Array();
	}
	cp = null;
}

function RemovePet(p, pi) {
	var x = 0;
	var match = -1;
	if (pi == null) {
		pi = 1;
	}
	if (p != null) {
		PurgePet(p, pi);
	}
}

function PurgePet(cp, pi) {
	if (cp != null) {
		var y = 0;
		var strout = '' + cp.petdata;
		var splits = strout.split(";");
		strout = '';
		for (y = 0; y < splits.length; y++) {
			if ((y + 1) == pi) {
			} else {
				if (splits[y] != '') {
					strout += splits[y] + ';';
				}
			}
		}
		cp.petdata = strout;
		if (cp.Pets[pi] != null) {
			cp.Pets[pi]  = null;
		}
	}
}

function UpdatePet(petID, PetName, Sprite, AI, level, p) {
	this.PetName = PetName;
	this.Sprite = Sprite;
	this.AI = AI;
	this.combattimer = 0;
	this.level = level;
	this.PetID = petID;
}

function UpdatePlayer(PlayerID, PlayerName, LP, PictureID, MapX, MapY, NPC, Monster, storyid, i, respawn, rage, dl, title, emotion, portrait, faithid, petdata, sex, mother, father) {
if (title == null) {
	title = '';
}
if (dl == null) {
	dl = 1;
}

if (portrait == null) {
	portrait = 'player1.gif';
}
this.Slots = new Array();
this.portrait = portrait;
this.PlayerID = PlayerID;
this.PlayerName = PlayerName;
this.PictureID = PictureID;
this.MapX = MapX;
this.MapY = MapY;
this.storyid = storyid;
this.tmpdmg = 0;
this.Frame = 1;
this.Stunned = 0;
this.Sex = sex;
this.petdefense = -1;
this.petmagicdefense = -1;
this.Mother = mother;
this.Father = father;
this.subframe = 0;
this.PlayerTitle = title;
this.DiscoveryLevel = dl;
this.changed = 1;
this.StartX = MapX;
this.StartY = MapY;
this.quests = 0;
this.redrawing = 0;
this.LegendPieces = LP;
this.NPC = NPC;
this.sizemod = 1;
this.Pets = new Array();
this.petdata = petdata;
this.Monster = Monster;
this.animated = 1;
this.subx = 0;
if (faithid == null) {
	faithid = 0;
}
this.Powers = new Array(9);
this.faithid = faithid;
this.suby = 0;
if (NPC == 0 && Monster == 0) {
	this.AI = 0;
} else {
	if (NPC != 0) {
		this.animated = (NPC == 4 ? 1 : 0);
		this.StartFrame = 2;
		this.Frame = 2;
		this.AI = NPC;
	} else {
		this.AI = Monster;
	}
}
this.respawn = respawn;
this.StartRespawn = respawn;
this.dead = 0;
this.fighting = 0;
this.fighttimer = -1;
this.following = 0;
this.Skills = new Array();
this.i = i;
this.destx = 0;
this.desty = 0;
this.rage = 0;
}

function APS(PlayerID, SkillID, SkillValue, SkillMax, SE, en) {
	AddPS(PlayerID, SkillID, SkillValue, SkillMax, SE, en);
}

function AddPS(PlayerID, SkillID, SkillValue, SkillMax, SE, en) {
	if (SE == null) {
		SE = 0;	
	}

	if (en == null) {
		en = 0;	
	}
	var x = 0;
	for (x = 1; x <= PlayerCount; x++) {
		var cp = Players[x];
		if (cp != null) {
			if (cp.PlayerID == PlayerID) {
				cp.Skills[SkillID] = new Array();
				cp.Skills[SkillID] = new UpdatePlayerSkill(PlayerID, SkillID, SkillValue, SkillMax, SE, en);
				break;
			}
		}
		cp = null;
	}		
}

function DrawBox(l, t, w, h, idx, contents) {
	return '<DIV STYLE="width: ' + w + '; height: ' + h + ';"><table width=100% height=100% align=center cellpadding=0 cellspacing=0 style="background-image: URL(wintile.jpg);"><tr height=15><td style="background-image: URL(wintop.jpg); background-repeat: repeat-x; " width=100%><table width=100% height=100% cellpadding=0 cellspacing=0><tr><td style="width: 15px; background-image: URL(wintopl.jpg); background-repeat: repeat-y; "></td><td width=* style="font-size: 1pt; ">&nbsp;</td><td style="width: 15px; background-image: URL(wintopr.jpg); background-repeat: repeat-y;"></td></tr></table></td></tr>	<tr height=100%><td><table width=100% height=100% cellpadding=0 cellspacing=0><tr><td style="width: 15px; background-image: URL(winleft.jpg); background-repeat: repeat-y; "></td><td width=* id=' + idx + '>' +  contents + '</td><td style="width: 15px; background-image: URL(winright.jpg); background-repeat: repeat-y;"></td></tr></table></td></tr>	<tr height=15><td style="background-image: URL(winbottom.jpg); background-repeat: repeat-x; " width=100%><table width=100% height=100% cellpadding=0 cellspacing=0><tr><td style="width: 15px; background-image: URL(winbottoml.jpg); background-repeat: repeat-y; "></td><td width=* style="font-size: 1pt; ">&nbsp;</td><td style="width: 15px; background-image: URL(winbottomr.jpg); background-repeat: repeat-y;"></td></tr></table></td></tr></table></DIV>'
}

function DrawBoxT(l, t, w, h, idx, contents) {
	return '<DIV STYLE="width: ' + w + '; height: ' + h + '; position: relative;"><table width=100% height=100% align=center cellpadding=0 cellspacing=0 style="background-image: URL(wintile.jpg);"><tr height=15><td style="background-image: URL(wintop.jpg); background-repeat: repeat-x; " width=100%><table width=100% height=100% cellpadding=0 cellspacing=0><tr><td style="width: 15px; background-image: URL(wintopl.jpg); background-repeat: repeat-y; "></td><td width=* style="font-size: 1pt; ">&nbsp;</td><td style="width: 15px; background-image: URL(wintopr.jpg); background-repeat: repeat-y;"></td></tr></table></td></tr>	<tr height=100%><td><table width=100% height=100% cellpadding=0 cellspacing=0><tr><td style="width: 15px; background-image: URL(winleft.jpg); background-repeat: repeat-y; "></td><td width=* id=' + idx + 'b>&nbsp;</td><td style="width: 15px; background-image: URL(winright.jpg); background-repeat: repeat-y;"></td></tr></table></td></tr>	<tr height=15><td style="background-image: URL(winbottom.jpg); background-repeat: repeat-x; " width=100%><table width=100% height=100% cellpadding=0 cellspacing=0><tr><td style="width: 15px; background-image: URL(winbottoml.jpg); background-repeat: repeat-y; "></td><td width=* style="font-size: 1pt; ">&nbsp;</td><td style="width: 15px; background-image: URL(winbottomr.jpg); background-repeat: repeat-y;"></td></tr></table></td></tr></table></DIV><div style="position: absolute; top: 20; left: 20;" id=' + idx + '>' +  contents + '</div>'
}

function DrawBoxT2(l, t, w, h, idx, contents) {
	return '<DIV STYLE="width: ' + w + '; height: ' + h + '; position: relative;" class=transbox><table width=100% height=100% align=center cellpadding=0 cellspacing=0 style="background-image: URL(wintile.jpg);"><tr height=15><td style="background-image: URL(wintop.jpg); background-repeat: repeat-x; " width=100%><table width=100% height=100% cellpadding=0 cellspacing=0><tr><td style="width: 15px; background-image: URL(wintopl.jpg); background-repeat: repeat-y; "></td><td width=* style="font-size: 1pt; ">&nbsp;</td><td style="width: 15px; background-image: URL(wintopr.jpg); background-repeat: repeat-y;"></td></tr></table></td></tr>	<tr height=100%><td><table width=100% height=100% cellpadding=0 cellspacing=0><tr><td style="width: 15px; background-image: URL(winleft.jpg); background-repeat: repeat-y; "></td><td width=* id=' + idx + ' valign=top>' + contents + '&nbsp;</td><td style="width: 15px; background-image: URL(winright.jpg); background-repeat: repeat-y;"></td></tr></table></td></tr>	<tr height=15><td style="background-image: URL(winbottom.jpg); background-repeat: repeat-x; " width=100%><table width=100% height=100% cellpadding=0 cellspacing=0><tr><td style="width: 15px; background-image: URL(winbottoml.jpg); background-repeat: repeat-y; "></td><td width=* style="font-size: 1pt; ">&nbsp;</td><td style="width: 15px; background-image: URL(winbottomr.jpg); background-repeat: repeat-y;"></td></tr></table></td></tr></table></DIV>'

}

function UpdatePlayerSkill(PlayerID, SkillID, SkillValue, SkillMax, SE, en) {
this.PlayerID = PlayerID;
this.SkillID = SkillID;
this.SkillValue = SkillValue;
this.SkillMax = SkillMax;
this.SE = SE;
this.en = en;
}

function AddPlayerSmall(PlayerID, PlayerName, PictureID) {
	var x = 0;
	var match = 0;
	if (rPlayers.length > 0) {
		for (x = 1; x <= rPlayers.length; x++) {
			if (rPlayers[x] != null) {
				if (rPlayers[x].PlayerID == PlayerID) {
					match = x;
					break;
				}
			}
		}	
	}

	if (match == 0) {
		match = rPlayers.length + 1;
		if (rPlayers[match] == null) {
			rPlayers[match] = new Array();
		}
	}


	if (match > 0) {
		rPlayers[match] = new UpdatePlayerSmall(PlayerID, PlayerName, PictureID);
	}	
	RefreshPlayerSmall();
}

function ShowPlayerMenu(id3) {
	var id1 = -3;
	var id2 = 'Chatter' + id3;
	AddButton(-3, 'Text','Text','Picture');
	AddSubMenu(-3, 'PM', '', '', 'Chatx(\'/pm ' + rPlayers[id3].PlayerName + '\');');
	AddSubMenu(-3, 'Message', '', '', 'OpenMessage(' + id3 + ');');
	if (PartyID > 0) {
		AddSubMenu(-3, 'Invite [Group]', '', '', 'PlayAction(10174, 0, 3, 1, 0,\'' + rPlayers[id3].PlayerName + '\');');
	}
	//if (GuildID > 0) {
	//	AddSubMenu(-3, 'Invite [Guild]', '', '', 'GuildInvite(' + id3 + ',\'' + rPlayers[id3].PlayerName + '\');');
	//}
	AddSubMenu(-3, 'Profile', '', '', 'Chatx(\'{player:' + rPlayers[id3].PlayerName + '}\');');
	AddSubMenu(-3, 'Trade', '', '', 'Chatx(\'/trade ' + rPlayers[id3].PlayerName + '\');');
	ShowSubMenu(id1,id2,id3);
}

function Chatx(e) {
	getObj('messc').value = '' + e;
	SendCMessage();
}

function chatMenu(a2, nick) {
}

function SaveNewMessage() {
	var messageto = URLencode(getObj('messageto').value);
	var messagesub = URLencode(getObj('messagesub').value);
	var messagetext =  tinyMCE.activeEditor.getContent();
	RemoveTiny('messagebox');

	var strout = '';
	strout = '<div style=\'width: 350px; height: 270px; background-image: URL(images/boxscreen.png); background-repeat: no-repeat;\'><table style="width: 340px;"><tr><td>' + ASCII('To:') + '</td><td>' + ASCII(messageto, 6) + '</td></tr><tr><td>' + ASCII('Subject:') + '</td><td>' + ASCII(messagesub, 6) + '</td></tr><tr><td colspan=2 style="color: white;" class="glowb"><div style="height: 150px; overflow: auto;">' + messagetext + '</div></td></tr><tr><td colspan=2>' + Adar('setTimeout(\'OpenMessage2();\',750);','New','New','button5','normal5.png', 1) + '</td></tr></table>';
	SetMainBox('' + strout);

	setTimeout("sendRequest('load_ajax.asp?A=3003&mto=" + messageto + "&msub=" + messagesub + "&mtxt=" + URLencode(messagetext) + "');",500);
}

function Update(gv) {
	if (gv != gameversion && gv > 0) {
		var m = getObj('moviebox');
		if (m != null) {
			getObj('moviebox').style.display = '';
			getObj('mappy').style.display = 'none';
			getObj('moviebox').innerHTML = '<center><div align=center valign=center style="margin-top: 80px; border: 2px solid black; WIDTH:350;height:250; background-image: URL(images/woodenpanel.jpg); background-repeat: no-repeat; background-position: top left;">' + ASCII('<h3>A patch for your game is now availble and will be auto-applied in a few seconds. Update from v' + gameversion + ' to v' + gv + '.</h3>') + '</div></center>';
		} else {
			AddLog('A patch for your game is now availble and will be auto-applied in 6 seconds. Update from v' + gameversion + ' to v' + gv + '.');
		}
		gameversion = gv;
		setTimeout('LogoutMenu(1);window.location.replace(\'default.asp?rl=1' + (widescreen != 0 ? '&w=1' : '') + '&' + (new Date()).getTime() + '\')', 6000);
	}

}

function AddMessage(id,who) {
	var pc = Messages.length + 1;
	Messages[pc] = new Array();
	Messages[pc] = new UpdatePMW(id, who);
	RefreshPlayerSmall();
}

function RemMes(z) {
	Messages[z] = null;
	RefreshPlayerSmall();
		
}

function ReadMessage(i) {
	PlayAction(3001, 1, i, 1, 0);
}

function OpenMessage(id) {
	OpenMessage2(rPlayers[id].PlayerName);
}

function SaveNewBug() {
	var messagesub = URLencode(getObj('bugtitle').value);
	var mbh = URLencode('' + navigator.appName + ' v' + navigator.appVersion);
	var messagetext =  URLencode(tinyMCE.activeEditor.getContent());
	RemoveTiny('messagebox');
	setTimeout("sendRequest('load_ajax.asp?A=10194&msub=" + messagesub + "&mbh=" + mbh  + "&mtxt=" + messagetext + "');",500);
}

function NewBug() {

	var strout = '';
	strout = '<div style=\'width: 350px; height: 270px; background-image: URL(images/boxscreen.png); background-repeat: no-repeat;\'><table style="width: 340px;"><tr><td>' + ASCII('Title:') + '</td><td><input name=bugtitle id=bugtitle value=\'\'></td></tr><tr><td colspan=2><textarea name=messagebox rows=12 cols=55 id=messagebox style="width: 320px;"></textarea></td></tr><tr><td colspan=2>' + Adar('SaveNewBug();','Report','Report','button5','normal5.png', 1) + '</td></tr></table>';
	SetMainBox('' + strout);
	AddTiny('messagebox');
}

function SaveNewNews() {
	var messagesub = URLencode(getObj('newstitle').value);
	var messagetext =  URLencode(tinyMCE.activeEditor.getContent());
	RemoveTiny('messagebox');
	setTimeout("sendRequest('load_ajax.asp?A=10184&msub=" + messagesub + "&mtxt=" + messagetext + "');",500);
}

function NewNews() {

	var strout = '';
	strout = '<div style=\'width: 350px; height: 270px; background-image: URL(images/boxscreen.png); background-repeat: no-repeat;\'><table style="width: 340px;"><tr><td>' + ASCII('Title:') + '</td><td><input name=newstitle id=newstitle value=\'\'></td></tr><tr><td colspan=2><textarea name=messagebox rows=12 cols=55 id=messagebox style="width: 320px;"></textarea></td></tr><tr><td colspan=2>' + Adar('SaveNewNews();','Add News','Add','button5','normal5.png', 1) + '</td></tr></table>';
	SetMainBox('' + strout);
	AddTiny('messagebox');
}

function OpenMessage2(idn) {
	var strout = '';
	strout = '<div style=\'width: 350px; height: 270px; background-image: URL(images/boxscreen.png); background-repeat: no-repeat;\'><table style="width: 340px;"><tr><td>' + ASCII('To:') + '</td><td><input name=messageto id=messageto value=\'' + (idn == null ? '' : idn) + '\'></td></tr><tr><td>' + ASCII('Subject:') + '</td><td><input name=messagesub id=messagesub value=\'\'></td></tr><tr><td colspan=2><textarea name=messagebox rows=12 cols=55 id=messagebox style="width: 320px;"></textarea></td></tr><tr><td colspan=2>' + Adar('SaveNewMessage();','Send','Send','button5','normal5.png', 1) + '</td></tr></table>';
	SetMainBox('' + strout);
	AddTiny('messagebox');
}


	function RemoveTiny(t) {
		try{
			var elm = tinyMCE.getInstanceById(t);
			if (elm) {
			try{
				tinyMCE.execCommand('mceRemoveControl', false, t);
			}catch(e){}
			}
			elm = null;
		}catch(e){}
	}

	function AddTiny(t) {
		RemoveTiny(t);
		tinyMCE.execCommand('mceAddControl', false, t);
	}

function GroupInvite(id) {
}

function GuildInvite(id) {
}

function Profile(id) {
}

function Trade(id) {
}

function RefreshPlayerSmall() {
	var x = 0;
	if (getObj('accountpass') == null) {
		var match = 0;
		var strout = '<table>';
		if (rPlayers.length > 0) {
			strout += '<tr><td colspan=2 style="background-image: URL(/images/top.png);"><b>' + ASCII('Players', 6) + '</b></td></tr>';
			for (x = 1; x <= rPlayers.length; x++) {
				if (rPlayers[x] != null) {
					strout += '<tr><td><img src=\'images/races/' + rPlayers[x].PictureID + '\' width=20 height=20></td><td id=Chatter' + x + ' style="background-image: URL(/images/tag.png); cursor: pointer; width: 110px; overflow: hidden;" title="' + rPlayers[x].PlayerName + '" onclick="ShowPlayerMenu(' + x + ');"><div style="height: 20px; width: 110px; overflow: hidden;">' + ASCII(rPlayers[x].PlayerName) + '</div></td></tr>';
				}
			}	
		}

		var z = 0;
		strout += '<tr><td colspan=2 style="background-image: URL(/images/top.png);"><b>' + ASCII('Private', 6) + '</b></td></tr>';
		if (PMArray.length > 0) {
			for (z = 1; z <= PMArray.length; z++) {
				var p = PMArray[z];
				if (p != null) {
					strout += '<tr><td>' + Adar('RemPM(' + z + ');','Remove this PM entry','x','sbutton1','snormal1.png') + '</td><td id=PMw' + z + ' style="background-image: URL(/images/tag.png); cursor: pointer; width: 110px; overflow: hidden;" title="PM - ' + p.who + '" onclick="openPM(' + p.id + ',\'' + p.who + '\');"><div style="height: 20px; width: 110px; overflow: hidden;">' + ASCII(p.who, (p.isDirty != 0 ? 3 : 1 )) + '</div></td></tr>';
				}
			}
		}

		strout += '<tr><td colspan=2 style="background-image: URL(/images/top.png);"><b>' + ASCII('<a href="javascript:void(0);" onclick="GetMessages();" style="cursor: pointer; color: gold;" title="Click to read your messages, ' + MessageCount + ' unread.">Inbox (' + MessageCount + ')</a>', 6) + '</b></td></tr>';
		if (Messages.length > 0) {
			for (z = 1; z <= Messages.length; z++) {
				var p = Messages[z];
				if (p != null) {
					strout += '<tr><td>' + Adar('RemMes(' + z + ');','Remove this entry','x','sbutton1','snormal1.png') + '</td><td id=PMm' + z + ' style="background-image: URL(/images/tag.png); cursor: pointer; width: 110px; overflow: hidden;" title="Message - ' + Messages[z].who + '" onclick="RemMes(' + z + '); ReadMessage(' + Messages[z].id + ',\'' + Messages[z].who + '\');"><div style="height: 20px; width: 110px; overflow: hidden;">' + ASCII(Messages[z].who, 6) + '</div></td></tr>';
				}
			}	
		}

		strout += '<tr><td colspan=2 style="background-image: URL(/images/top.png);"><b><a href="javascript:void(0);" onclick="GetPatches();" style="cursor: pointer; color: gold;" title="Click to read recent changes to the game.">' + (PatchDirty != 0 ? ASCII('Patch Notes', null,null,1) : ASCII('News', 6)) + '</a></b></td></tr>';

		strout += '</table>';
		if (getObj('leftbox') != null) {
			getObj('leftbox').innerHTML = strout;
		}
	}
}

function UpdatePlayerSmall(PlayerID, PlayerName, PictureID) {
this.PlayerID = PlayerID;
this.PlayerName = PlayerName;
this.PictureID = PictureID;
}

function RemovePlayerSmall(PlayerID) {
	var x = 0;
	var match = 0;
	if (rPlayers.length > 0) {
		for (x = 1; x <= rPlayers.length; x++) {
			if (rPlayers[x] != null) {
				if (rPlayers[x].PlayerID == PlayerID) {
					rPlayers[x] = null;
					break;
				}
			}
		}	
	}
	if (getObj('leftbox')!= null) {
		RefreshPlayerSmall();
	}
}

function ClearPlayers() {
	var x = 0;
	PlayerCount = 0;
	Players = new Array();
}

function Gather(x, q, mq) {

	//resource gathering
	if (getObj('resourcebar') != null) {
		getObj('resourcebar').innerHTML = GetBox2(q + ' remaining', 'bigblue', q, mq, 90)
		clearTimeout(actiontimeout);
		if (q > 0) {
			actiontimeout = setTimeout('Gather(' +  x + ',' + (q - 1) + ', ' + mq + ');' + (q == 1 ? 'PlayAction(5006, ' + x + ', ' + mq + ');' : '') + '', (GetTimer(2) * 1000));
		}
	}
}

function SetMainBox(t) {
        try {
		RemoveTiny('messagebox');
	} catch(e) {
	}
	if (getObj('mainbox').innerHTML != null) {
		getObj('mainbox').innerHTML = t;
	}
}

function sendRequest(doc){
	if (xmlobj != null) {

	        try {
			if (xmlobj.readyState != 0) {
				xmlobj.abort(); 
			}
		} catch(e) {
		}			

		Done = 0;
		xmlobj.open('GET','' + doc + (doc.match(/\?/) == null ? "?" : "&") + (new Date()).getTime(),true);
		xmlobj.setRequestHeader("Cache-Control", "no-cache");
		xmlobj.setRequestHeader("Pragma", "no-cache");
		xmlobj.onreadystatechange = function(){
 
			if (xmlobj.readyState == 4 && xmlobj.status == 200) {

 
				if (xmlobj.responseText){
					var rt = xmlobj.responseText;
				  	try {
						var r = eval(rt);
					} catch(e) {
						newerror(e, rt, doc);
					}
					TrackPage('ajax.asp');

					success = 0;
				}
				Done = 1;
			} else if (xmlobj.readyState == 4 && xmlobj.status != 200) {
				Done = -1;
				if (xmlobj.status == 0) {
				} else {
					alert(xmlobj.status + ' ' + xmlobj.statusText + ' ' + xmlobj.responseText);
				}
			}
		};


		xmlobj.send(null);
	} else {
		//ajaxerror(null, null, 'Routine 1');
		Done = -1;
	}
}

function sendParty(doc){
	if (ajaxparty != null) {

	        try {
			if (ajaxparty.readyState != 0) {
				ajaxparty.abort(); 
			}
		} catch(e) {
		}			

		ajaxparty.open('GET','' + doc + (doc.match(/\?/) == null ? "?" : "&") + (new Date()).getTime(),true);
		ajaxparty.setRequestHeader("Cache-Control", "no-cache");
		ajaxparty.setRequestHeader("Pragma", "no-cache");
		ajaxparty.onreadystatechange = function(){
 
			if (ajaxparty.readyState == 4 && ajaxparty.status == 200) {

 
				if (ajaxparty.responseText){
					var rt = ajaxparty.responseText;
				  	try {
						var r = eval(rt);
					} catch(e) {
						newerror(e, rt, doc);
					}
					TrackPage('ajax.asp');
				}
			} else if (ajaxparty.readyState == 4 && ajaxparty.status != 200) {
				if (ajaxparty.status == 0) {
				} else {
					alert(ajaxparty.status + ' ' + ajaxparty.statusText + ' ' + ajaxparty.responseText);
				}
			}
		};


		ajaxparty.send(null);
	} else {
	}
}


function sendRefresh(doc){
	if (ajaxget != null) {

	        try {
			if (ajaxget.readyState != 0) {
				ajaxget.abort(); 
			}
		} catch(e) {
		}
		DisableRefresh();
		ajaxget.open('GET','' + doc + (doc.match(/\?/) == null ? "?" : "&") + (new Date()).getTime(),true);
		ajaxget.setRequestHeader("Cache-Control", "no-cache");
		ajaxget.setRequestHeader("Pragma", "no-cache");
		ajaxget.onreadystatechange = function(){
 
			if (ajaxget.readyState == 4 && ajaxget.status == 200) {
				if (ajaxget.responseText){
					var rt = ajaxget.responseText;
				        try {
						var r = eval(rt);
					} catch(e) {
						newerror(e, rt, doc);
					}
				}
			} else if (ajaxget.readyState == 4 && ajaxget.status != 200) {
				if (ajaxget.status == 0) {
				} else {
					ajaxerror(ajaxget.status, ajaxget.statusText + ' ' + ajaxget.responseText, 'Routine 2b ');
				}
			}
		};
		ajaxget.send(null);
		setRefresh();
	} else {
		ajaxerror(null, null, 'Routine 2');
	}
}

function setRefresh() {
	DisableRefresh();
	chatrefresht  = setTimeout('sendRefresh(\'play_ajax.asp\');', 5000);
}	

function DisableRefresh() {
	if (chatrefresht != 0) {
		clearTimeout(chatrefresht);
	}
}	

function getAColor(c) {
	var AC = '';
	if (c <= 1) {
		AC = 'white';
	} else {
		if (c == 2) {
			AC = '#00FF00';
		} else if (c == 3) {
			AC = '#FF0000';
		} else if (c == 4) {
			AC = 'yellow';
		} else if (c == 5) {
			AC = 'cyan';
		} else if (c == 6) {
			AC = 'gold';
		} else if (c == 7) {
			AC = '#CC66CC';
		} else if (c == 8) {
			AC = '#66FF99';
		} else if (c == 9) {
			AC = '#CC6600';
		} else if (c == 10) {
			AC = '#FF9900';
		} else if (c == 11) {
			AC = '#CCFF00';
		} else if (c == 12) {
			AC = '#9966FF';
		} else if (c == 13) {
			AC = '#FF80FF';
		} else if (c == 14) {
			AC = '#2BBFA6';
		} else if (c == 15) {
			AC = '#BE542C';
		}
	}
	return AC;
}

function getAColor2(c) {
	var AC = '';
	if (c <= 1) {
		AC = 'RGB(156, 140, 103)';
	} else {
		AC = 'RGB(153, 77, 46)';
	}
	return AC;
}

function ASCII(text, c, f, r) {

	return ASCII2(text, c, f, r) ;
}

function Choose(x) {
	PlayAction(2007, 0,0,0,0)
}

function PickFaith(x) {
	PlayAction(2007, 1,x,0,0)
}

function PickFaith2(x, y) {
	if (y == null) {
		y = 2008;
	}
	PlayAction(y, 1,x,0,0)
}

function ASCII2(text, c, f, r, fw) {
	if (c == null) {
		c = 1;
	}
	if (r == null) {
		r = 0;
	}
	var strout = '';
	var x = 0;
	var p = 2;

	if (f == null) {
		f = '2';
	}

	//

	var cp = Players[1];
	if (cp != null) {
		try {
			text = text.replace(/\{M\}/g, cp.Mother);
			text = text.replace(/\{F\}/g, cp.Father);
		} catch (e) {
		}
		cp = null;
	}

	if (r == 0) {
		var c2 = getAColor(c);
		//class="word" 
		strout = '<span class="glow' + f + '" style="' + (fw != null ? 'font-size: ' + fw + 'pt;' : '') + 'color: ' + c2 + ';">';
		strout += text;
		strout += '</span>';
	} else {
		if (text != null) {
			if (text.replace != null) {
				text = text.replace(/<br>/gi,"|");
			}
		}
		strout = '<span class="glow' + f + '">';
		for (x = 0; x < text.length; x++) {
			if (text.charAt(x) == "|") {
				strout += '<br>';
			} else if (text.charAt(x) == " ") {
				strout += ' ';
			} else {

				strout += '<span style="' + (fw != null ? 'font-size: ' + fw + 'pt;' : '') + 'color: ' + getAColor((r == -1 ? (rand(6) + 1) : p)) + '; padding-right: 1px;">' + text.charAt(x) + '</span>'
				p = p + 1;
				if (p > 15) {	
					p = 2;
				}
			}
		}	
		strout += '</span>';
	}
	return strout;
}

function ASCIIS(text, c, f, r) {
	if (c == null) {
		c = 1;
	}
	if (r == null) {
		r = 0;
	}
	var strout = '';
	var x = 0;
	var p = 2;

	if (f == null) {
		f = '2';
	}

	if (r == 0) {
		var c2 = getAColor(c);
		//class="word" 
		strout = '<span class="glow' + f + '" style="font-size: 7pt; color: ' + c2 + ';">';
		strout += text;
		strout += '</span>';
	} else {
		if (text != null) {
			if (text.replace != null) {
				text = text.replace(/<br>/gi,"|");
			}
		}
		strout = '<span class="glow' + f + '">';
		for (x = 0; x < text.length; x++) {
			if (text.charAt(x) == "|") {
				strout += '<br>';
			} else if (text.charAt(x) == " ") {
				strout += ' ';
			} else {

				strout += '<span style="font-size: 7pt; color: ' + getAColor((r == -1 ? (rand(6) + 1) : p)) + '; padding-right: 1px;">' + text.charAt(x) + '</span>'
				p = p + 1;
				if (p > 15) {	
					p = 2;
				}
			}
		}	
		strout += '</span>';
	}
	return strout;
}

function ASCIIH(text, c, f, r) {
	if (c == null) {
		c = 1;
	}
	if (r == null) {
		r = 0;
	}
	var strout = '';
	var x = 0;
	var p = 2;

	if (f == null) {
		f = '2';
	}

	if (r == 0) {
		strout = '<span style="font-size: 10pt; font-weight: bold; color: gold;">';
		strout += text;
		strout += '</span>';
	} else {
		if (text != null) {
			if (text.replace != null) {
				text = text.replace(/<br>/gi,"|");
			}
		}
		strout = '<span>';
		for (x = 0; x < text.length; x++) {
			if (text.charAt(x) == "|") {
				strout += '<br>';
			} else if (text.charAt(x) == " ") {
				strout += ' ';
			} else {

				strout += '<span style="font-size: 10pt; font-weight: bold; color: gold; padding-right: 1px;">' + text.charAt(x) + '</span>'
				p = p + 1;
				if (p > 15) {	
					p = 2;
				}
			}
		}	
		strout += '</span>';
	}
	return strout;
}


function Adar(Actions, Titles, Names, classx, imgx, fl) {
if (classx == null) {
	classx = "button3";
	imgx = "normal4.png";
}
if (imgx == null) {
	imgx = "normal5.png";
}
imgx = "background-image: url(/" + imgx + ");" + (fl != null ? 'float: left;' : '')
return "<a class=\"" + classx + "\" style=\"" + imgx + "\" href=\"javascript:void(0);\"  onclick=\"var tmpaok = 0; if (window.checkATimer) {tmpaok = checkATimer(); if (tmpaok != 0) {setATimer();}} else {tmpaok = 1;} if (tmpaok != 0) {" + Actions + ";}\" title=\"" + Titles + "\"><center>" + ASCII2(Names) + "</center></a>";
}

function checkATimer() {
	var rt = false;
	var nt = new Date().getTime();
	if (ATimer + 700 < nt) {
		rt = true;
	} else {
		rt = false;
		AddLog('You are clicking too fast. 700ms delay in effect!');
	}
	return rt;
}

function setATimer() {
	var nt = new Date().getTime();
	ATimer = nt;
}

function Adac(Actions, Titles, Names, classx, imgx, fl, img2) {
if (classx == null) {
	classx = "buttonc";
	imgx = "circle1b.png";
}
if (imgx == null) {
	imgx = "circle1b.png";
}
if (img2 == null) {
	img2 = '';
}
imgx = "background-image: url(/images/" + imgx + ");" + (fl != null ? 'float: left;' : '')
return "<a class=\"" + classx + "\" style=\"" + imgx + "\" href=\"javascript:void(0);\"  onclick=\"" + Actions + ";\" title=\"" + Titles + "\">" + (img2 != "" ? "<div style=\"width: 40; height: 40; background-position: 5 5; " + (Actions != "" ? "cursor: pointer;" : "") + " background-image: url(/images/" + img2 + "); background-repeat: no-repeat;\">" + ASCII2(Names) + "</div>" : "<center>" + ASCII2(Names) + "</center>") + "</a>";
}

function Adaca(Actions, Titles, Names, classx, imgx, fl, img2, mover, mout) {
if (classx == null) {
	classx = "buttonc";
	imgx = "circle1b.png";
}
if (imgx == null) {
	imgx = "circle1b.png";
}
if (img2 == null) {
	img2 = '';
}
imgx = "background-image: url(/images/" + imgx + ");" + (fl != null ? 'float: left;' : '')
return "<a class=\"" + classx + "\" style=\"" + imgx + "\" href=\"javascript:void(0);\" " + (mover != null ? "onmouseover=\"" + mover + "\"" : "") + " " + (mout != null ? "onmouseout=\"" + mout + "\"" : "") + "  onclick=\"" + Actions + ";\" title=\"" + Titles + "\"><img style=\"width: 30; height: 30; margin: 5px; " + (Actions != "" ? "cursor: pointer;" : "") + "\" border=0 src=\"/images/" + img2 + "\"></a>";
}

function Adat(Actions, Titles, Names, classx, imgx, fl) {
if (classx == null) {
	classx = "buttont";
	imgx = "tab.png";
}
if (imgx == null) {
	imgx = "tab.png";
}
imgx = "background-image: url(/images/" + imgx + ");" + (fl != null ? 'float: left;' : '')
return "<a class=\"" + classx + "\" style=\"" + imgx + "\" href=\"javascript:void(0);\"  onclick=\"" + Actions + ";\" title=\"" + Titles + "\"><center>" + ASCII2(Names) + "</center></a>";
}


function fxn(stuff) {
  var str = '';
  var s;

	for (s = 0; s < stuff.value.length; s++) {
		if (stuff.value.charCodeAt(s) >= 48 && stuff.value.charCodeAt(s) <= 57) {
			str = str + '' + stuff.value.charAt(s);
		}
	}
	stuff.value = str;
	if (stuff.value == '' || stuff.value == null) {
		stuff.value = 0;
	}
	if (stuff.value.length > 1 && stuff.value.charCodeAt(0) == 48)  {
		stuff.value = stuff.value.substr(1,stuff.value.length - 1)
	}
}

function getObj(objn) {
	if (document.getElementsByName(objn).length <= 1) {
		return document.getElementById(objn);
	} else {
		return document.getElementsByName(objn);
	}
}


function GetStat(p, x) {
	var out = 0;

	if (p.Skills[x] != null) {
		out = p.Skills[x].SkillValue;
	}
	return out;
}


function fxkp(e) {
	e = e || window.event;	
	if (e != null) {
		var f = e.keyCode;
		if (e.which != null && f == 0) {
			f = e.which;
		}

		if ((f < 45 || f > 57) && f != 8 && f != 9 && f != 13) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

function ShowSubMenuF(id1, id2, id3, active, ftype) {
	if (ftype == null) {
		ftype = 0;
	}
	AddButton(-3, 'Text','Text','Picture');
	if (ftype == 0) {
		if (active > 0) {
			AddSubMenu(-3, 'Stay', '', '', 'PlayAction(2003,2, ' + id3 + ', 0, 0);');
		} else {
			AddSubMenu(-3, 'Follow', '', '', 'PlayAction(2003,1, ' + id3 + ', 0, 0);');
		}
	} else {
		AddSubMenu(-3, 'Interact', '', '', 'PlayAction(2003,10, ' + id3 + ', 0, 0);');
	}
	ShowSubMenu(id1,id2,id3);
}

function ShowSubMenuSell(id1, id2, id3, npc, p, q, v) {
	AddButton(-3, 'Text','Text','Picture');
	AddSubMenu(-3, 'Sell' + (q > 1 ? ' All' : ''), '', '', 'PlayAction(10006,{0}, ' + npc + ', ' + p + ', 0);');
	if (q > 1) {
		AddSubMenu(-3, 'Sell 1', '', '', 'PlayAction(10006,{0}, ' + npc + ', ' + p + ', 0,null, 1);');
	}
	if (q > 5) {
		AddSubMenu(-3, 'Sell 5', '', '', 'PlayAction(10006,{0}, ' + npc + ', ' + p + ', 0, null, 5);');
	}

	if (q > 10) {
		AddSubMenu(-3, 'Sell 10', '', '', 'PlayAction(10006,{0}, ' + npc + ', ' + p + ', 0, null, 10);');
	}
	if (q > 50) {
		AddSubMenu(-3, 'Sell 50', '', '', 'PlayAction(10006,{0}, ' + npc + ', ' + p + ', 0, null, 50);');
	}

	ShowSubMenu(id1,id2,id3);
}


function ShowSubMenuI(p, v, id1, id2, id3, v1, v2, f, q, av3, r, il, s, it, level) {
	AddButton(-3, 'Text','Text','Picture');
	if (il > 0 && v1 == 0) {
		var eok = 1;
		if (s > 0) {
			var cp = Players[1];
			if (GetStat(cp, s) <= 0) {
				eok = 0;
			}
			cp = null;
		}
		if (eok > 0 && level <= Players[1].DiscoveryLevel) {
			AddSubMenu(-3, 'Equip', '', '', 'PlayAction(1001,14, {0}, ' + p + ', 0,\'' + av3 + '\');');
			if (il == 11 && (s != 13 && s != 15 && s !=  17 && s !=  19 && s !=  20 && s !=  29) && level >= 50) {
				AddSubMenu(-3, 'Offhand', '', '', 'PlayAction(1001,-14, {0}, ' + p + ', 0,\'' + av3 + '\');');
			}
		}
	} else {
		AddSubMenu(-3, 'Interact', '', '', 'PlayAction(1001,3, {0}, ' + p + ', 0,\'' + av3 + '\');');
	}
	AddSubMenu(-3, 'Drop', '', '', 'ShowSubMenuID(' + p + ', ' + v + ', ' + id1 + ', \'' + id2 + '\', ' + id3 + ', \'' + av3 + '\');');
	var fb = (32 & f);
	if (fb == 32) {
		AddSubMenu(-3, 'Unrune', '', '', 'PlayAction(1001,10, {0}, ' + p + ', 0,\'' + av3 + '\');');
	}
	if (LastTrade > 0 && ((v2 == 0 && v1 == 0) || (v1 == 0 && v2 != -1)) && it != 30) {
		AddSubMenu(-3, '&nbsp;&nbsp;Trade [Window]', '', '', 'QuickTrade({0});');
		AddSubMenu(-3, '&nbsp;&nbsp;Trade [Multi-item]', '', '', 'AddToTrade({0});');
	}
	if (v1 == 0 && v2 == 0 && it != 30) {
		AddSubMenu(-3, 'Auction', '', '', 'PlayAction(1001,5, {0}, ' + p + ', 0,\'' + av3 + '\');');
	} else if (v2 == 0 && it != 30) {
		AddSubMenu(-3, 'Take Off Auction', '', '', 'PlayAction(1001,5, {0}, ' + p + ', 0,\'' + av3 + '\');');
	}
	if (v2 == 0 && v1 == 0) {
		AddSubMenu(-3, 'Protect', '', '', 'PlayAction(1001,7, {0}, ' + p + ', 0,\'' + av3 + '\');');
	} else if (v2 == -1) {
	} else if (v1 == 0) {
		AddSubMenu(-3, 'Unprotect', '', '', 'PlayAction(1001,7, {0}, ' + p + ', 0,\'' + av3 + '\');');
	}

	if (it != 30) {
		if (Players[1].DiscoveryLevel >= 50) {
			AddSubMenu(-3, 'Store', '', '', 'PlayAction(1001,6, {0}, ' + p + ', 0,\'' + av3 + '\');');
		}
		if (RegionID == 223) {
			AddSubMenu(-3, 'Guild Store', '', '', 'PlayAction(1001,-6, {0}, ' + p + ', 0,\'' + av3 + '\');');
		}
	}
	
	if (q > 1) {
		AddSubMenu(-3, 'Split', '', '', 'PlayAction(1001,11, {0}, ' + p + ', 0,\'' + av3 + '\');');
	}
	AddSubMenu(-3, 'Stack', '', '', 'PlayAction(1001,12, {0}, ' + p + ', 0,\'' + av3 + '\');');

	AddSubMenu(-3, 'Send to Chat', '', '', 'LinkTo("pitem:", getObj("Inv' + v + '").title);');
	if (it >= 16 && it <= 20) {
		AddSubMenu(-3, 'Craft List', '', '', 'LinkTo("ingredient:",getObj("Inv' + v + '").title);');
	}

	ShowSubMenu(id1,id2,id3);
}

function TradeScreen2(p) {
	LastTrade = Math.abs(p);
	setTimeout('PlayAction(1060,0,0, 1, 0,' + p + ');',250);
}

function AddToTrade(p) {
	if (LastTrade > 0) {
		PlayAction(10601,1,p, 1, 0,LastTrade);
	} else {
		AddLog('Trade not active.');
	}
}
function QuickTrade(p) {
	if (LastTrade > 0) {
		PlayAction(1060,1,p, 1, 0,LastTrade);
	} else {
		AddLog('Trade not active.');
	}
}

function GetCraftingPlans(l, f, l1, l2) {
	if (l1 == null) {
		l1 = 0;
	}
	if (l2 == null) {
		l2 = 0;
	}
	PlayAction(1012,0, l, 1, 0, f, l1, l2);
}

function Unequip(i, slot) {
	FetchOff();
	getObj('equips').innerHTML = ASCII('<table style="width: 350px; height: 230px;"><tr height="100%"><td width=180>' + ASCII('Unequipping item please wait...') + '</td></tr></table>');
	//var e = getObj('Loc' + slot);
	//if (e != null) {
	//	clearInnerHTML(e);
	//	e.parentNode.removeChild(e);
	//}
	//e = null;
	//Do it on a timer
	setTimeout('PlayAction(1006,3, ' + i + ', 1, 0);', 350);
}

function clearInnerHTML(obj, purgex) {
	if (obj != null) {
		if (purgex == null) {
			purgex = 0;
		}
		while(obj.firstChild) {
			obj.removeChild(obj.firstChild)
		};
		obj = null;
	}
}

function Consume(i, s) {
	PlayAction(10091,i, s, 1, 0);
}


function InventoryFilter(av) {
	PlayAction(1001,0, 0, 1, 0,av);
}

function ItoS(s) {
	var out = 'border: 1px solid transparent;';
	if (s > 0) {
		var p = Players[1];
		if (p != null) {
			if (p.Skills[s] != null) {
				if (p.Skills[s].SkillValue <= 0) {
					out += 'border-bottom: 1px dotted #ff6666;'
				}
			} else {
				out += 'border-bottom: 1px dotted #ff6666;'
			}
		}
		p = null;
	}
	return out;
}

function ItoL(l) {
	var out = '';
	if (l > 0) {
		var p = Players[1];
		if (p != null) {
			if (p.DiscoveryLevel < l) {
				out = 'l'
			}
		}
		p = null;
	}
	return out;
}


function HasSkill(p, s) {
	var out = 0;
	if (s > 0) {
		if (p != null) {
			if (p.Skills[s] != null) {
				if ((p.Skills[s].SE > 0 && s <= 32) || (p.Skills[s].en > 0 && (s > 32 || s == 19))) {
					out = 1;
				}
			}
		}
		p = null;
	}
	return out;
}

function ItoS2(s, av, p) {
	if (p == null) {
		p = 0;
	}
	var out = '';
	out = ItoS(s);
	if (av > 0) {
		out += 'background-image:URL(images/invbacks.png); background-repeat: no-repeat;';
	} else if (p == 1 || p == -1) {
		out += 'background-image:URL(images/invbackp.png); background-repeat: no-repeat;';
	}
	return out;
}

function getSkillName(x) {
	var skillz = new Array('Unknown', 'Health','Power','Might','Dexterity','Luck','Wisdom','Damage','Defense');
	var strout = '';
	if (skillz[x] == null) {
		strout = skillz[0];
	} else {
		strout = skillz[x];
	}
	return strout;
}

function AlterSkill2(p, x, y, my, dd, ds, mn) {
	var out = 0;
	var cp = Players[p];
	if (cp != null) {
	var s = cp.Skills[x];
	if (s != null) {
		if (y > 0 || my > 0) {
			if (x == 1 || x == 2) {
				s.SkillMax = s.SkillMax + my;
				if (y > (s.SkillMax - s.SkillValue)) {
					y = s.SkillMax - s.SkillValue;
				}
				s.SkillValue = s.SkillValue + y;
			} else {
				s.SkillMax = s.SkillMax + my;
				s.SkillValue = s.SkillValue + y;
			}
			if (cp.dead == 0) {
				if (y == 0 && my != 0) {
					//AddChat(getMsg('fight.change','Max ' + getSkillName(x),my,cp.PlayerName), 3);
				} else {
					//AddChat(getMsg('fight.change',getSkillName(x),y,cp.PlayerName), 3);
				}
			}
		} else if (y < 0 || my < 0) {
			s.SkillMax = s.SkillMax + my;
			s.SkillValue = s.SkillValue + y;
			if (cp.dead == 0) {
				if (y == 0 && my != 0) {
					//AddChat(getMsg('fight.change','Max ' + getSkillName(x),my,cp.PlayerName), 3);
				} else {
					//AddChat(getMsg('fight.change',getSkillName(x),y,cp.PlayerName), 3);
				}
			}
		}

		//if (x == 1 && cp.dead == 0) {
		//	 if (cp.Skills[1].SkillValue <= 0) {
		//		KillPlayer(cp);
		//	}
		//}
		if (p == 1) {
			RefreshStatBox;
		}
	}
	}
	s = null;
	cp = null;
}

function AlterSkill(p, x, y, my, dd, ds, mn) {
	var out = 0;
	if (mn == null) {
		if (Players[dd] != null) {
			mn = Players[dd].PlayerName;
		} else {
			mn = '';
		}
	}
	var bywho = ' by ' + mn;
	var cp = Players[p];
	if (cp != null) {
	var s = cp.Skills[x];
	if (s != null) {
		if (dd != null) {
			if (x == 1 && y < 0) {
				var d = Math.abs(y);
				if (ds != null) {
					if (cp.Skills[ds] != null) {
						d = Math.abs(d) - Math.abs(cp.Skills[ds].SkillValue);
						if (d < 0) {
							d = 0;
						}
					}
				}
				s.SkillValue = s.SkillValue - d;
				s.SkillMax = s.SkillMax - my;

				 if (s.SkillValue <= 0) {
				//	AddChat(getMsg('fight.die1',d,cp.PlayerName,bywho), 3);
				} else {
				//	AddPlayerWidget(cp, 'damage.png', '-' + d, 3);
				//	AddChat(getMsg('fight.damage',d,cp.PlayerName,bywho), 3);
				}
			} else {
				if (y > 0 || my > 0) {
					s.SkillMax = s.SkillMax + my;
					if (y > (s.SkillMax - s.SkillValue)) {
						y = s.SkillMax - s.SkillValue;
					}
					s.SkillValue = s.SkillValue + y;
					if (cp.dead == 0) {
						if (y == 0 && my != 0) {
						//	AddChat(getMsg('fight.change','Max ' + getSkillName(x),my,cp.PlayerName), 3);
						} else {
						//	AddChat(getMsg('fight.change',getSkillName(x),y,cp.PlayerName), 3);
						}
					}
				}
			}
		} else if (y > 0 || my > 0) {
			if (x == 1 || x == 2) {
				s.SkillMax = s.SkillMax + my;
				if (y > (s.SkillMax - s.SkillValue)) {
					y = s.SkillMax - s.SkillValue;
				}
				s.SkillValue = s.SkillValue + y;
			} else {
				s.SkillMax = s.SkillMax + my;
				s.SkillValue = s.SkillValue + y;
			}
			if (cp.dead == 0) {
				if (y == 0 && my != 0) {
					//AddChat(getMsg('fight.change','Max ' + getSkillName(x),my,cp.PlayerName), 3);
				} else {
					//AddChat(getMsg('fight.change',getSkillName(x),y,cp.PlayerName), 3);
				}
			}
		} else if (y < 0 || my < 0) {
			var d = Math.abs(y);
			if (x == 1 && ds != null) {
				if (cp.Skills[ds] != null) {
					d = Math.abs(d) - Math.abs(cp.Skills[ds].SkillValue);
					if (d < 0) {
						d = 0;
					}
				}
				s.SkillValue = s.SkillValue - d;
				s.SkillMax = s.SkillMax - my;
				y = -d;
			} else {
				s.SkillMax = s.SkillMax + my;
				s.SkillValue = s.SkillValue + y;
			}
			if (cp.dead == 0) {
				//AddChat(getMsg('fight.change',getSkillName(x),y,cp.PlayerName), 3);
			}
		}
		if (p == 1) {
			RefreshStatBox;
		}
	}
	}
	cp = null;
	s = null;
}

function FetchAction(a, av, av2, p, delay, av3) {
	sendRequest('load_ajax.asp?A=' + a + '&Value=' + av + '&A2=' + av2 + '&P=' + p + '&A3=' + av3);
}

function ChatFetch(fetchtype, fetchname, p) {
	if (p == null) {
		p = 0;
	}
	sendRequest('load_ajax.asp?A=1999&Value=' +p  + '&A2=0&P=1&A3=' + fetchname + '&ft=' + fetchtype);
}

function LoadFetch(fetchtext, v, PlayerItemID, index, divx, timerx) {
	var objn = divx + v;
	var ox = 140;
	var oy = 20;
	if (getObj(objn) == null) {
		objn = 'mappy';
		ox = -50;
		oy = 100;
	}
	if (timerx == null) {
		timerx = 5000;
	}
	var l = findPos(getObj(objn))[0] + ox;
	var l2 = findPos(getObj(objn))[1] + oy;
	getObj('infobox').innerHTML = fetchtext;
	getObj('infobox').style.left = l;
	getObj('infobox').style.top = l2;
	getObj('infobox').style.padding = 5;
	getObj('infobox').style.display = '';
	clearTimeout(hidetimer);
	hidetimer = setTimeout('getObj(\'infobox\').style.display = \'none\';', timerx);
}



function SpendStatPoint(s, v) {
	if (v == null) {
		v = 1;
	}
	PlayAction(2000,s, v, 1, 0);
}

function Fetch(v, PlayerItemID, ft, divx) {
	clearTimeout(fetching);
	fetching = setTimeout("FetchAction(" + (99 + ft) + ", " + PlayerItemID + ", " + v + ", 1, 1);", 1000);
}

function ShowSubMenuID(p, v, id1, id2, id3, av3) {
	HideSubMenu(-3);
	AddButton(-3, 'Text','Text','Picture');
	AddSubMenu(-3, 'Yes', '', '', 'PlayAction(1001,4, {0}, ' + p + ', 0,\'' + av3 + '\');');
	AddSubMenu(-3, 'No', '', '', 'HideSubMenu(-3);');
	ShowSubMenu(id1,id2,id3);
}

function FetchOff() {
	clearTimeout(fetching);
}

function findPos(obj){
var posX = obj.offsetLeft;var posY = obj.offsetTop;
while(obj.offsetParent){
posX+=obj.offsetParent.offsetLeft;
posY+=obj.offsetParent.offsetTop;
if(obj==document.getElementsByTagName('body')[0]){break}
else{obj=obj.offsetParent;}
}
return [posX,posY]
}

function UpdatePower(PowerID, PowerName, Damage, Power, SkillID, SkillValue, ws, ds, Animation, startjs, endjs, js, cooldown, chance, faith) {
	this.PowerID = PowerID;
	this.PowerName = PowerName;
	this.Damage = Damage;
	this.Power = Power;
	this.SkillID = SkillID;
	this.SkillValue = SkillValue;
	this.WeaponSkill = ws;
	this.DefenseSkill = ds;
	this.startjs = startjs;
	this.endjs = endjs;
	this.js = js;
	this.Animation = Animation;
	this.cooldown = cooldown;
	this.chance = chance;
	this.faith = faith;
}

function APW(PowerID, PowerName, Damage, Power, SkillID, SkillValue, ws, ds, Animation, startjs, endjs, js, cooldown, chance, faith) {
	AddPower(PowerID, PowerName, Damage, Power, SkillID, SkillValue, ws, ds, Animation, startjs, endjs, js, cooldown, chance, faith);
}

function AddPower(PowerID, PowerName, Damage, Power, SkillID, SkillValue, ws, ds, Animation, startjs, endjs, js, cooldown, chance, faith) {
	Powers[PowerID] = new Array();
	Powers[PowerID] = new UpdatePower(PowerID, PowerName, Damage, Power, SkillID, SkillValue, ws, ds, Animation, startjs, endjs, js, cooldown, chance, faith);
}

function ClearPowers() {
	Powers = new Array();
}

function ShowTravelMap() {
	getObj('headerbar').innerHTML = ASCIIH('Travel');
	SetMainBox('<iframe src="load_ajax.asp?A=2002" style="width:360px;height:320px;background: transparent; margin: 0px; padding: 0px;" border=0 scrolling="none" frameborder="0" allowtransparency="true" id="mapframe" name="mapframe"></iframe>');

}


function PickCraftedItem(s, a, v) {
	if (a == null) {
		a = 10095;
	}
	if (v == null) {
		v = 0;
	}

	sendRequest("load_ajax.asp?A=" + a + "&Value=" + v + "&A2=" + s + "&", "", 1, 0, "Choose Crafted Item", "", "", 300, 200);
}

function PickShortCutItem(s) {
	sendRequest("load_ajax.asp?A=10090&A2=" + s + "&", "", 1, 0, "Choose Item", "", "", 300, 200);
}

function PickPlayer(s) {
	sendRequest("load_ajax.asp?A=10097&A2=" + s + "&", "", 1, 0, "Choose Player", "", "", 300, 300);
}


function PickItem(s, p, a3) {
	sendRequest("load_ajax.asp?A=10092&A2=" + s + "&P=" + p + "&A3=" + a3, "", 1, 0, "Choose Item", "", "", 300, 200);
}

function NewCharacterScreen(dn) {
	if (dn == null) {
		dn = '';
	}
	nameok = 0;
	SetMainBox('<div style=\'height: 300px; width: 350px; background-image: URL(images/boxscreen.png);\'><table width=345><tr><td>' + ASCII('Name:') + '</td><td><table cellpadding=0 cellspacing=0><tr><td><input name=storyid id=storyid size=22 maxlength=22 type=hidden value=\'0\'><input name=title id=title size=40 maxlength=40 type=hidden value=\'Adventurer\'><input name=playername id=playername size=30 maxlength=30 onkeydown="NameCheck(250);" onkeypress="return isAlphaKey(event)" value="' + dn + '" onblur="this.value=extractAlpha(this.value)"></td><td>' + Adar('NamePicker();','Random name','?','sbutton1','snormal3.png', 1)  + '</td></tr></table></td></tr><tr><td>' + ASCII('Gender:') + '</td><td><input type=radio checked name=sex value=\'M\' onclick=\'CheckAppearance();\'>' + ASCII('Male') + '<input type=radio name=sex value=\'F\' onclick=\'CheckAppearance();\'>' + ASCII('Female') + '</td></tr><tr height=60><td valign=top><input name=sprite id=sprite value=\'\' type=hidden></td><td id=appearance name=appearance width=290></td></tr><tr><td colspan=2>' + ASCII('Background story:') + '</td></tr><tr><td colspan=2>' + ASCII('Note: This is not the same as "Class" you can pick your class at the end of a short introduction area.') + '</td></tr><tr height=60><td valign=top id=story colspan=2 align=center></td></tr><tr><td colspan=2 id=results name=results></td></tr><tr><td></td><td id=buttons>' + Adar('SubmitNewCharacter();','Begin your Journey','Begin','button5','normal5.png',1) + Adar('AccountScreen(1,0,0);','Back', 'Back','button5','normal8.png', 1) + '</td></tr></table></div>');
}

function EditGuildScreen(g, dn, gw, gc, gs, ghtml) {
	if (dn == null) {
		dn = '';
	}
	SetMainBox('<div style=\'height: 300px; width: 350px; background-image: URL(images/boxscreen.png);\'><table width=345><tr><td colspan=2 id=TalkPages></td></tr><tr><td>' + ASCII('Guild Name:') + '</td><td><input name=guildid id=guildid type=hidden value=\'' + g + '\'><input name=guildname id=guildname size=30 maxlength=30 onkeypress="return isAlphaKey(event)" value="' + dn + '" onblur="this.value=extractAlpha(this.value)"></td></tr><tr><td>' + ASCII('Guild Website:') + '</td><td><input name=guildwebsite id=guildwebsite size=30 maxlength=150 value="' + gw + '"></td></tr><tr><td>' + ASCII('Guild Color:') + '</td><td><input name=guildcolor id=guildcolor type="hidden" size=30 maxlength=150 value="' + gc + '"></td></tr><tr><td colspan=2>' + ASCII('Description:') + '</td><td></td></tr><tr><td colspan=2><textarea name=messagebox rows=8 cols=55 id=messagebox style="width: 320px;">' + ghtml + '</textarea></td></tr><tr><td colspan=2 id=results name=results></td></tr><tr><td></td><td id=buttons>' + Adar('EditSaveGuild();','Save Guild Changes','Save','button5','normal5.png',1) + Adar('GuildUploadScreen(' + g + ');','Upload Guild Logo','Upload','button5','normal7.png',1) + '</td></tr></table></div>');
	AddTiny('messagebox');
}

function GuildUploadScreen(g) {
	window.open('services/GuildUploader.asp?guildid=' + g,'Upload','height=415,width=420,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');
}

function EditSaveGuild() {
	var gn = URLencode(getObj('guildname').value);
	var gid =getObj("guildid").value;
	var gw = URLencode(getObj('guildwebsite').value);
	var gc = URLencode(getObj('guildcolor').value);
	var messagetext =  URLencode(tinyMCE.activeEditor.getContent());
	RemoveTiny('messagebox');
	SetMainBox('<div style=\'height: 300px; width: 350px; background-image: URL(images/boxscreen.png);\'><table width=345><tr><td colspan=2>' + ASCII('Saving Changes ....') + '</td></tr></table></div>');
	setTimeout("sendRequest('load_ajax.asp?A=10189&gc=" + gc + "&g=" + gid + "&gw=" + gw + "&gn=" + gn + "&ghtml=" + messagetext + "');",500);
}


function NamePicker() {
	clearTimeout(nametimer);
	nameok = 0;
	if (getObj('sex') != null) {
		if (getObj('sex').value != '') {
			appeartimer = setTimeout('if (getObj(\'sex\') != null) {PlayAction(24, 0, getSex(), 1, 0);}', 400);
		}
	}
}

function isAlphaKey(e) {
var k;
document.all ? k = e.keyCode : k = e.which;
var o = ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 ||  k == 46 || k == 16);
return o;
}

function extractAlpha(str) {
return str.replace(/[^a-z ]/gi,"");
}

function SubmitNewCharacter() {
	var fail = 0;
	var failwhat = '';
	if (getObj('playername') != null) {
		if (nameok == 0 || getObj('playername').value == '') {
			failwhat = 'Your name has not passed our check.'
			fail = 1;
		}
	} else {
		failwhat = 'You need to enter a name.'
		fail = 1;
	}

	if (getSex() == '') {
		fail = 1;
	}

	getObj('appearance').style.border = "";
	if (getObj('sprite') != null) {
		if (getObj('sprite').value == '') {
			getObj('appearance').style.border = "1px solid red";
			failwhat = failwhat + '<br>Please choose an Avatar.'
			fail = 1;
		}
	} else {
		failwhat = failwhat + '<br>Please choose an Avatar.'
		fail = 1;
	}

	getObj('story').style.border = "";
	if (getObj('storyid') != null) {
		if (getObj('storyid').value == '' || '' + getObj('storyid').value == '0') {
			getObj('story').style.border = "1px solid red";
			failwhat = failwhat + '<br>Please choose a background story.'
			fail = 1;
		}
	} else {
		failwhat = failwhat + '<br>Please choose a background story.'
		fail = 1;
	}

	if (fail > 0) {
		getObj('results').innerHTML = '' + ASCII(failwhat); //'BAD';
	} else {
		PlayAction(23, 0, '' + getObj('playername').value + '|' + getObj('sprite').value + '|' + getObj('storyid').value + '|' + getSex() + '|' + getObj('title').value , 1, 0);
		SetMainBox('');
	}
}

function NameCheck(d) {
	clearTimeout(nametimer);
	nameok = 0;
	if (getObj('playername') != null) {
		getObj('playername').style.color = '#6666FF';
		if (getObj('playername').value != '') {
			nametimer = setTimeout('if (getObj(\'playername\') != null) {PlayAction(21, 0, getObj(\'playername\').value, 1, 0);}', d);
		}
	}
}


function CheckAppearance() {
	clearTimeout(appeartimer);
	clearTimeout(appearptimer);
	getObj('sprite').value = '';
	if (getObj('sex') != null) {
		if (getObj('sex').value != '') {
			appeartimer = setTimeout('if (getObj(\'sex\') != null) {PlayAction(22, 0, getSex(), 1, 0);}', 400);
		}
	}
}

function getSex() {
	var i = 0;
	var s = '';
	for (i = 0; i < getObj('sex').length; i++)  {
		if (getObj('sex')[i].checked == true) {
			s = getObj('sex')[i].value;
			break;
		}
	}
	return s;
}

function SaveAccount() {
	var pwd = getObj('pwd').value;
	var pwd2 = getObj('pwd2').value;
	var email = getObj('email').value;
	var email2 = getObj('email2').value;
	var ref = getObj('referrer').value;
	var ce = getObj("canemail").checked;
	var cd = getObj("candelete").checked

	if (pwd != pwd2 || pwd == '') {
		getObj('results').innerHTML = '' + ASCII('Check your password is not empty and both password boxes match.', 3);
	} else if (email != email2 || email == '') {
		getObj('results').innerHTML = '' + ASCII('Check your email address is not empty and both email boxes match', 3);
	} else {
		setTimeout("sendRequest('load_ajax.asp?A=1020&pwd=" + URLencode(pwd) + "&ce=" + (ce == true ? 1 : 0)  + "&cd=" + (cd == true ? 1 : 0)  + "&e=" + URLencode(email) + "&r=" + URLencode(ref) + "');",500);
	}

}

function ScratchCard(y) {
	Scratched = 0;
	if (y == null) {
		var scratch = new Array("images/faith/2.png","images/faith/3.png","images/faith/4.png","images/faith/5.png","images/faith/6.png","images/faith/7.png");
		var l = 6;
	} else {
		var scratch = new Array("images/story/1.png","images/story/2.png","images/story/3.png","images/story/4.png","images/story/5.png");
		var l = 5;
	}
	var scratchr = new Array(6);
	var i = 0;
	for (i = 1; i <= 6; i++)  {
		scratchr[i] = rand(l) - 1; // + 1;
	}

	return '<div><table cellspacing=0 cellpadding=0 style="width: 290px; height: 241px; background-image: URL(images/scratchcard.jpg);"><tr height=52><td></td><tr><td style="padding-left: 22px; padding-right: 22px;" valign=top><table cellpadding=0 cellspacing=9 height="144"><tr height=69><td width=71 id=scratch1 align=center valign=center><img src="images/scratchsquare.png" onclick="ChangeScratch(1,\'' + scratch[scratchr[1]] + '\',' + scratchr[1] + ');"></td><td width=71 align=center valign=center id=scratch2><img src="images/scratchsquare.png" onclick="ChangeScratch(2,\'' + scratch[scratchr[2]] + '\',' + scratchr[2] + ');"></td><td width=71 align=center valign=center id=scratch3><img src="images/scratchsquare.png" onclick="ChangeScratch(3,\'' + scratch[scratchr[3]] + '\',' + scratchr[3] + ');"></td></tr><tr height=69><td width=71 align=center valign=center id=scratch4><img src="images/scratchsquare.png" onclick="ChangeScratch(4,\'' + scratch[scratchr[4]] + '\',' + scratchr[4] + ');"></td><td width=71 align=center valign=center id=scratch5><img src="images/scratchsquare.png" onclick="ChangeScratch(5,\'' + scratch[scratchr[5]] + '\',' + scratchr[5] + ');"></td><td width=71 align=center valign=center id=scratch6><img src="images/scratchsquare.png" onclick="ChangeScratch(6,\'' + scratch[scratchr[6]] + '\',' + scratchr[6] + ');"></td></tr></table></td></tr></tr></table></div>';
}

function ChangeScratch(x, i, mc) {
	Scratched = Scratched + 1;
	getObj('scratch' + x).innerHTML = '<img src="' + i + '">';
	if (Scratched == 6) {
		var scratch = new Array("images/faith/2.png","images/faith/3.png","images/faith/4.png","images/faith/5.png","images/faith/6.png","images/faith/7.png");
		var b = 0;
		for (b = 1; b <= 6; b++)  {
			var winner = 0;
			mc = 0;
			var s = getObj('scratch' + b);
			if (s != null) {
				var f = 0;
				for (f = 1; f <= 6; f++)  {
					if (s.innerHTML == getObj('scratch' + f).innerHTML) {
						//We have a match
						winner = winner + 1;
						var g = 0;
						for (g = 1; g <= 6; g++)  {
							if (s.innerHTML.indexOf(scratch[g]) != -1) {
								//We have a match
								//alert(s.innerHTML + ' ' + g + ' ' + scratch[g]);
								mc = (g);
								break;
							}
						}
					}
				}
			}
			if (winner >= 3) {
			
				setTimeout('PlayAction(7, 8,' + mc + ', ' + winner + ');', 500);
				s = null;
				break;
			}
			s = null;
		}
	
	}
}

function openPM(a, who) {

	var w = '';
	if (AccountID < a) {
		w = '' + AccountID + 'p' + a;
	} else {
		w = '' + a + 'p' + AccountID;
	}

	var z = 0;
	var fn = -1;
	var match = -1;
	for (z = 1; z <= PMArray.length; z++) {
		var p = PMArray[z];
		if (p != null) {
			if ('' + p.id == '' + a) {
				match = z;
				break;
			}
		} else {
			if (fn == -1) {
				fn = z;
			}
		}
	}	
	if (match != -1) {
		try {
			PMObj[match].document.title = PMObj[match].document.title;
		} catch (e) {
			fn = match;
			match = -1;
		}

	}
	if (match == -1) {
		var pc = (fn != -1 ? fn : PMArray.length + 1);
		if (pc > 5) {
			AddLog('You may only have 5 pm windows open at a time. You might consider closing a PM window.');
		} else {
			PMArray[pc] = new Array();
			PMArray[pc] = new UpdatePMW(a, who);
			PMObj[pc] = window.open("PrivateMessage.htm" +  "?" + (new Date()).getTime() + "","" + w,"toolbar=no,status=0,location=1,scrollbars=0,width=220,height=257,left=200,top=0");
			setTimeout("InitWindow(" + pc + ", '" + who + "'," + a + ");", 1250);
		}
	}
	try {
		PMArray[match].isDirty = 0;
		PMObj[match].focus();
	} catch (e) {
	}
	RefreshPlayerSmall();
	p = null;

}

function InitWindow(pc, who, a) {
	if (PMObj[pc] != null) {
		PMObj[pc].document.title = who;
		PMObj[pc].PMTarget = a;
	}
}

function PushMessage(a, t) {
	var z = 0;
	var match = -1;
	for (z = 1; z <= PMArray.length; z++) {
		var p = PMArray[z];
		if (p != null) {
			if ('' + p.id == '' + a) {
				match = z;
				break;
			}
		}
	}	
	if (match != -1) {
		if (PMObj[match] != null) {
			try {
				if (PMObj[match].PMTarget == 0) {
					InitWindow(match, p.who, p.id);
				}
				PMArray[match].isDirty = 1;
				PMObj[match].PushIt(t);
			} catch (e) {
				CloseWindow(match);
			}
		}		
	}

}

function RemPM(z) {
	CloseWindow(z)
	RefreshPlayerSmall();
}


function WindowOpen(a) {
	var out = 0;
	var z = 0;
	var match = -1;
	for (z = 1; z <= PMArray.length; z++) {
		var p = PMArray[z];
		if (p != null) {
			if ('' + p.id == '' + a) {
				match = z;
				break;
			}
		}
	}	

	if (match != -1) {
		var p = PMArray[match];
		if (p != null) {
			try {
				if (PMObj[match] != null) {
					if (PMObj[match].PMTarget != 0) {
						out = 1;
					}
				}
			} catch (e) {
				out = 0;
				CloseWindow(match);
			}
		}
	}
	return out;
}

function CloseWindow(x) {
	if (PMObj[x] != null) {
		PMObj[x].close()
	}
	try {
		PMObj[x] = null;
	} catch (e) {
	}
	try {
		PMArray[x] = null;
	} catch (e) {
	}
}

function CloseAll() {
	var z = 0;
	for (z = 1; z <= PMArray.length; z++) {
		PMObj[z].close();
		try {
			PMObj[z] = null;
		} catch (e) {
		}
		
	}	
	PMArray = new Array();
	PMObj = new Array();
}

function UpdatePMW(pmid, who) {
this.id = pmid;
this.who = who;
this.isDirty = 0;
}


function ToggleLog(x) {
	getObj('ChatBoxp').style.display = 'none';
	getObj('ChatBox2p').style.display = (x == 1 ? 'none' : '');
	getObj('LogBoxp').style.display = (x == 1 ? '' : 'none')
}

function ToggleLogShort(x) {
	getObj('LogBoxp').style.display = (x == 1 ? '' : 'none')
}

function ToggleLog2() {
	//Open log window
	if (LogWindow != null) {
		try {
			LogWindow.close();
		} catch (e) {
		}
		LogWindow = null;
	} else {
		LogWindow = window.open("Log.htm","Log","toolbar=no,status=0,resizable=1,scrollbars=1,width=250,height=257,left=200,top=0");
	}
	ShowChat();
}

function PushIt(t) {
	var aok = 0;
	var aok2 = 0;
	try {
		aok2 = (LogWindow != null ? 1 : 0)
	} catch (e) {
		aok2 = 0;
	}

	if (aok2 != 0) {
		try {
			aok = 1;
			LogWindow.PushIt("" + t + "");
		} catch (e) {
			aok = 0;
		}
	} else {
		LogWindow = null;
	}
	return aok;
}

function TidyWindows() {
	RemoveShortcuts();

	try {
		CloseAll();
	} catch (e) {
	}

	try {
		if (LogWindow != null) {
			LogWindow.close();
		}
	} catch (e) {
	}
	LogWindow = null;

	try {
		if (ErrorWindow != null) {
			ErrorWindow.close();
		}
	} catch (e) {
	}
	ErrorWindow = null;
}

function AddShortcuts() {
	if (UseShortcuts != 0) {
		shortcut.add("Ctrl+Alt+J",function() {HandleKey("HomeMenu();")});
		shortcut.add("Ctrl+Alt+H",function() {HandleKey("ExploreMenu();")});
		shortcut.add("Ctrl+Alt+T",function() {HandleKey("TravelMenu();")});
		shortcut.add("Ctrl+Alt+F",function() {HandleKey("FollowerMenu();")});
		shortcut.add("Ctrl+Alt+E",function() {HandleKey("ExploreActual2();")});
		shortcut.add("Ctrl+Alt+I",function() {HandleKey("PlayAction(1001,0, 1, 1, 0);")});
		shortcut.add("Alt+1",function() {HandleKey("TriggerPower2(1);")});
		shortcut.add("Alt+2",function() {HandleKey("TriggerPower2(2);")});
		shortcut.add("Alt+3",function() {HandleKey("TriggerPower2(3);")});
		shortcut.add("Alt+4",function() {HandleKey("TriggerPower2(4);")});
		shortcut.add("Alt+5",function() {HandleKey("TriggerPower2(5);")});
		shortcut.add("Alt+6",function() {HandleKey("TriggerPower2(6);")});
		shortcut.add("Alt+7",function() {HandleKey("TriggerPower2(7);")});
		shortcut.add("Alt+8",function() {HandleKey("TriggerPower2(8);")});
	}
}

function HandleKey(x) {
	clearTimeout(keyhandle);
	//alert(x);
	keyhandle = setTimeout(x, 500);
}

function RemoveShortcuts() {
	if (UseShortcuts != 0) {
		shortcut.remove("Ctrl+Alt+J");
		shortcut.remove("Ctrl+Alt+H");
		shortcut.remove("Ctrl+Alt+T");
		shortcut.remove("Ctrl+Alt+F");
		shortcut.remove("Ctrl+Alt+E");
		var z = 0;
		for (z = 1; z <= 8; z++) {
			shortcut.remove("Alt+" + z);
		}	

	}
}

function newerror(err, responsetext, u) {
	var txt = '';
	try {
		if (ErrorWindow != null) {
			try {
				ErrorWindow.close();
			} catch (e) {
			}
			ErrorWindow = null;
		}
		ErrorWindow = window.open("Log.htm","Error","toolbar=no,status=0,resizable=1,scrollbars=0,width=380,height=260,left=200,top=0");
		LastError ="<div>" + ASCII("We are sorry you have experienced an error to have us investigate and fix the problem please report the error using the button below.<br>") + "<div style='width: 360px; height:260px;'><form method=post id=errorform name=errorform action='error.asp'><input type=hidden id=oldurl name=oldurl value='" + u + "'><textarea rows=20 cols=80 style='display:none;' id=errortext name=errortext>" + responsetext + "</textarea><br>Comments:<br><center><textarea rows=10 cols=60 id=errorcomment name=errorcomment>Name: " + err.name + " (" + err.message + ", " + err.number + ")\n\nError description: " + err.description + "</textarea></center><br>" + Adar("document.errorform.submit();","Report to Support","Report","button5","normal5.png") + "</form></div></div>";
		setTimeout("ErrorShow();", 1100);
	} catch(e){
		txt="Name: " + err.name + " (" + err.message + ", " + err.number + ")\n\nError description: " + err.description + "\n\n" + responsetext;
		alert(txt)

	}

}

function ErrorShow() {
	var aok2 = 0;
	try {
		aok2 = (ErrorWindow != null ? 1 : 0)
	} catch (e) {
		aok2 = 0;
	}
		if (aok2 != 0) {
		try {
			aok = 1;
			ErrorWindow.getObj("ChatWindow").innerHTML = "";
			ErrorWindow.PushIt(LastError);
		} catch (e) {
		}
	} else {
		ErrorWindow = null;
	}
}

function SetStoryID(s, sn) {
	var z = 0;
	for (z = 1; z <= 5; z++) {
		var obj = getObj('story' + z);
		if (obj != null) {
			if ('' + z == '' + s) {
				obj.innerHTML = '<img src="images/gui/plus.png" width=20 height=20 class="transparent" align=center valign=center>';
				obj.className = '';
			} else {
				obj.innerHTML = '';
				obj.className = 'transparent';
			}
		}
	}

	getObj("storyid").value = s
	getObj("title").value = sn;
}

function SetAppearance(s, i) {
	var z = 0;
	for (z = 1; z <= 30; z++) {
		var obj = getObj('pic' + z);
		if (obj != null) {
			if ('' + z == '' + i) {
				obj.className = '';
			} else {
				obj.className = 'transparent';
			}
		}
	}

	if (getObj('sprite') != null) {
	//	getObj('spritep').innerHTML = "<img src='images/races/" + s + "' width=50 height=50>";

		getObj('sprite').value = s;
	}	
}


function MiniGame() {
	window.open('minigame.htm', 'MiniGames', 'status=no, width=658, height=658,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=no');
}

function AutoPilot(x) {
	if (x == null) {
		x = 5;
	}
	if (autopiloton > 0) {
		autopiloton = 0;
		AddLog('Auto pilot turned off.');
	} else {
		autopiloton = x;
		AddLog('Auto pilot turned on.');
	}
	
}

function SetMessageCount(m) {
	if (m != MessageCount) {
		MessageCount = m;
		RefreshPlayerSmall();
	}
}


function OpenChatTarget() {

	AddButton(-3, 'Text','Text','Picture');
	AddSubMenu(-3, 'Default', '', '', 'SetChatTarget(\'\',\'Say:\');');
	//AddSubMenu(-3, 'Local', '', '', 'SetChatTarget(\'/l\',\'Local:\');');
	if (GuildID > 0) {
		AddSubMenu(-3, 'Guild', '', '', 'SetChatTarget(\'/g\',\'Guild:\');');
	}
	if (PartyID > 0) {
		AddSubMenu(-3, 'Party', '', '', 'SetChatTarget(\'/p\',\'Party:\');');
	}
	var x = 0;
	for (x = 1; x <= PlayerCount; x++) {
		var cp = Players[x];
		if (cp != null && x != 1) {
			
			if (cp.Monster == 0) {
				if (cp.NPC == 0) {
				} else {
					AddSubMenu(-3, '' + cp.PlayerName, '', '', 'SetChatTarget(\'/s ' + cp.PlayerName + '|\',\'PM:\');');
				}
			}
		}
	}

	var z = 0;
	if (PMArray.length > 0) {
		for (z = 1; z <= PMArray.length; z++) {
			var p = PMArray[z];
			if (p != null) {
				AddSubMenu(-3, '' + p.who, '', '', 'SetChatTarget(\'/s ' + p.who + '|\',\'PM:\');');
			}
		}
	}

	var z = 0;
	if (Rooms.length > 0) {
		for (z = 1; z <= Rooms.length; z++) {
			var p = Rooms[z];
			if (p != null) {
				AddSubMenu(-3, '' + p.PlayerName, '', '', 'SetChatTarget(\'/' + p.PlayerID + '\',\'Room ' + p.PlayerID + ':\');');
			}
		}
	}

	ShowSubMenu(-3,'SayText',0);
}

function SetChatTarget(t, st) {
	ChatPrefix = (t != '' ? '' + t + ' ' : t);
	if (getObj('SayText') != null) {
		getObj('SayText').innerHTML = '' + ASCII('' + st);
	}
}

function JoinRoom(r, rn) {
	var x = 0;
	var match = 0;
	if (Rooms.length > 0) {
		for (x = 1; x <= Rooms.length; x++) {
			if (Rooms[x] != null) {
				if (Rooms[x].PlayerID == r) {
					match = x;
					break;
				}
			}
		}	
	}

	if (match == 0) {
		match = Rooms.length + 1;
		if (Rooms[match] == null) {
			Rooms[match] = new Array();
		}
	}


	if (match > 0) {
		Rooms[match] = new UpdatePlayerSmall(r, rn, '');
	}	
}

function LeaveRoom(r, rn) {
	if (Rooms.length > 0) {
		for (x = 1; x <= Rooms.length; x++) {
			if (Rooms[x] != null) {
				if (Rooms[x].PlayerID == r) {
					Rooms[x] = null;
					break;
				}
			}
		}	
	}	
}

function Dictate(plid) {
	var o = getObj('note' + plid);
	if (o != null) {
		
		if (o.textContent) {
			var t = o.textContent;
		} else {
			var t = o.innerText;
		}
		if (t.indexOf('notebox') >= 0) {
			t = '';
		}
		if (t == '[Note]') {
			t = '';
		}
		o.onclick = null;
		o.innerHTML = '<div title="Edit/Add a note, hit Enter to save"><input value="' + t + '" id="notebox' + plid + '" name="notebox' + plid + '" size=20 maxlength=200 onkeypress="FormEnter(event,\'DictateSave(' + plid + ');\');"></div>';
	}
	
}

function DictateSave(plid) {
	if (getObj('notebox' + plid) != null) {
		var t = getObj('notebox' + plid).value;
		if (t.indexOf('notebox') >= 0) {
			t = '';
		}
		PlayAction(333, 1, plid, 1, 1, URLencode(t));
	}
}

function LinkTo(x, y) {
	if (getObj('messc').value == '[Type what you wish to say here!]') {
		getObj('messc').value = '';
	}
	getObj('messc').value = getObj('messc').value + ' {' + x + y + '}';
	getObj("messc").focus();
}

function KillGuild() {
	getObj('moviebox').style.display = '';
	getObj('moviebox').innerHTML = '<center><div align=center valign=center  style="WIDTH:320;height:184; padding-top: 120px;"><table style="border: 2px solid black; background-image: URL(maskBG.png);"><tr><td colspan=2>' + ASCII('Are you 150% sure?') +'</td><td colspan=2>' + Adar('getObj(\'moviebox\').style.display = \'none\';PlayAction(10164,0,  666, 1, 0);','Yes delete the guild','Yes','sbutton1','snormal3.png', 1) + '</td><td>' + Adar('getObj(\'moviebox\').style.display = \'none\';','No','No','sbutton1','snormal1.png', 1) + '</td></tr></table></div></center>';
}