var nameson = 1;
var animateon = 1;
var splashfile = 'splash.jpg'
var setupphase = 1;

if (window!=top){top.location.href=location.href;}

var Projectiles = new Array();
var DBButtons = 0;
var MasterDeckID = 1;
var ruleset = 0;
var gameplaying = 0;
var MouseCard = -1;
var imagepath = 'images/cards/';
var ext = '.png';

var Keys = new Array();
var MX = 0;
var MY = 0;
var TurnTaken = 0;
var heros = 2;
var speed = 1500;
AddKey(52, 15000, 'StartPvPGame(0);','4','Start Player vs Player Game (4)', 'pgo1.png');
AddKey(49, 5000, 'StartGame(0);','1','Start Standard Game (1)', 'go.png');
AddKey(67, 1500, 'if (ToggleWindow(\'zoomscreen\') == 0) {ZoomCard();}','c','Zoom Card (c)', 'shortcuts.png', 2);
AddKey(50, 5000, 'StartGame(1);','2','Easy Advanced Game (2)', 'go2.png');
AddKey(51, 5000, 'StartGame(2);','3','Hard Advanced Game (3)', 'go3.png');
AddKey(53, 5000, 'StartGame(5);','5','Epic Advanced Game (5)', 'go5.png');
AddKey(54, 5000, 'StartGame(6);','6','Hardcore Advanced Game (6)', 'go6.png');
//AddKey(52, 15000, 'StartPvPGame(0);','4','Start Player vs Player Game (4)', 'pgo1.png');

//AddKey(107, 700, 'IncreaseCards();','+','Increase deck size (+)', 'na.png', 0);
//AddKey(109, 700, 'DecreaseCards();','-','Decrease deck size (-)', 'na.png', 0);

//if (ruleset == 1) {
	AddKey(73, 2000, 'if (gameplaying == 1) {UseInterrupt(1);}','i','Interrupt (i)', 'interrupt.png', 1);
//}
AddKey(88, 500, 'ExitGame();','x','Exit Game (x)', 'exit.png', 2);
AddKey(112, 500, 'if (ToggleWindow(\'helpscreen\') == 0) {PlayAction(1018, 0, 1, 1, 0)};','f1','Help (f1)', 'help.png', 2);
//AddKey(65, 500, 'if (ToggleWindow(\'chatscreen\') == 0) {PlayAction(1038, 0, 1, 1, 0)};','a','Chat (a)', 'chat.png', 2);
var MasterTarget = 0;
var Dollars = 0;
var RedrawCards = 0;
var windows = new Array();
var rooms = new Array('Chat','Status','Combat','Private','Development Log');
var roompics = new Array('crew.png','58.png','ap.png','crewx.png','money.png');
var roomtoggle = new Array(1, 1,1, 1, 1);
var chat = new Array();
var ipopupt = 0;
var nameok = 0;
var interacting = 0;
var apopupt = 0;
var Cards = new Array();
var LastClickedItem = -1;
var ButtonTimer = -1;
var ButtonTimerH = -1;
var nametimer = -1;
var appeartimer = -1;
var appearptimer = -1;
var ButtonInterval = 0;
var locations = 5;
var infotimer = -1;
var lastinfocard = -1;

var lastfightlog = '';

var FindCard = 1;
var FontFolder = FontFolder;
var loaded = 0;
var caching = 1;

var dragapproved=false
var initialwidth,initialheight
var dragobj;
var dragoffsetx = 0;
var dragoffsety = 0;
var dragtempx = 0;
var dragtempy = 0;
var lastx = 0;
var lasty = 0;
var justSent = false;
var DeckCount = 0;
var Decks = new Array();
var Cards = new Array();
var BoundXY = 0;
var delayt = 0;
var lockinput = 0;
var docks = new Array();
var dragid = '';

window.onload= function (evt) {
	window.top.xmlobj = window.top.getXmlHttpRequestObject();
	window.top.ajaxget = window.top.getXmlHttpRequestObject();
	ShowLoginBox();
	window.top.PageLoaded = true;
}

document.onkeydown = function anonymous(event) {VETKEY(event)};

	function setMX(x, y) {
		MX = x;
		MY = y;
	}

	function drag_drop(e){
	if (dragobj) {
		e = e || window.event;
		if (dragapproved){
			dragobj.style.left=dragtempx+e.clientX-dragoffsetx+"px";
			dragobj.style.top=dragtempy+e.clientY-dragoffsety+"px";
			if (dragid != '') {
				SaveWindow(dragid, dragobj.style.left, dragobj.style.top);
			}
		}
		e.cancelBubble = true;
	}
	}

	function initializedrag(e, windowname, context){
		dragid = windowname;
		e = e || window.event;
		dragoffsetx=e.clientX;
		dragoffsety=e.clientY;
		dragobj=context;
		dragtempx=parseInt(dragobj.style.left);
		dragtempy=parseInt(dragobj.style.top);
		dragapproved=true
		document.onmousemove=drag_drop
		document.onmouseup=stopdrag
	}

	function stopdrag(){
		dragapproved=false;
		dragobj = null;
		dragid = '';
		document.onmousemove=null;
		document.onmouseup=null;
	}

	function closeme(windowname){
		getObj(windowname).style.display="none";
	}

	function closepm(w) {
		var oldpm = getObj(w);
		if (oldpm != null) {
			oldpm.parentNode.removeChild(oldpm);
		}
	}

	function ToggleWindow(w) {
		var oldpm = getObj(w);
		if (oldpm != null) {
			oldpm.parentNode.removeChild(oldpm);
			var o = 1;
		} else {
			var o = 0;
		}
		return o;
	}

	function UpdateDock(parentid, onclickx, title) {
		this.parentid = parentid;
		this.onclickx = onclickx;
		this.title = title;
	}

	function RemoveDock(d) {
		if (docks[d] != null) {
			closepm('d' + docks[d].parentid);
			docks[d] = null;
		}
		
	}


	function IncreaseCards() {
		if (setupphase == 1) {
			locations = locations + 1
			if (locations > 15) {
				locations = 15;
			}
			ShowDefaultBar();
			ResizeGame();
		} else {
			alert('It is not possible to increase or decrease your deck size during a game.');
		}
	}

	function DecreaseCards() {
		if (setupphase == 1) {
			locations = locations - 1
			if (locations < 5) {
				locations = 5;
			}
			ShowDefaultBar();
			ResizeGame();
		} else {
			alert('It is not possible to increase or decrease your deck size during a game.');
		}
	}



	function RemoveDock2(d) {
		var x = 0;
		var newd = docks.length;
		for (x = 0; x < docks.length; x++) {
			if (docks[x] != null) {
				if (docks[x].parentid == d) {
					RemoveDock(x);
					break;
				}
			}
		}	
		
	}

	function DockClick(d) {
		if (docks[d] != null) {
			eval(docks[d].onclickx);
			RemoveDock(d);
		}
	}

	function AddDock(w, onclickx, title) {
		var x = 0;
		var newd = docks.length;
		for (x = 0; x < docks.length; x++) {
			if (docks[x] == null) {
				newd = x;
				break;
			
			}
		}	

		docks[newd] = new Array();
		docks[newd] = new UpdateDock(w, onclickx, title);	

		closepm(w);
		var pmWindowx = document.createElement("div");
		pmWindowx.id = 'd' + w;
		pmWindowx.className = "navborderx";
		pmWindowx.style.border = "1px solid black";
		pmWindowx.style.zIndex = 110;
		pmWindowx.onclick = function anonymouse(e) {DockClick(newd)};
		pmWindowx.style.position = "absolute";
		pmWindowx.style.cursor = "pointer";
		pmWindowx.style.left = "0";
		pmWindowx.style.top = 32 + ((newd * 22)) + 2;
		pmWindowx.style.width = "210";
		pmWindowx.style.height = "22";
		pmWindowx.innerHTML = "" + ASCII(title);
		document.body.appendChild(pmWindowx);		
	}

	function SendPMMessage(a) {
		window.top.sendRequest('play_ajax.asp?TextColor=' + URLencode(window.top.getObj('TextColor').value) + '&Type=PM&PM=' + a + '&Message=' + URLencode(getObj('mess' + a).value));
		getObj('mess' + a).value = '';
		return false;
	}

	function minwin(w, a, func, title) {
		var oldpm = getObj(w);
		if (oldpm != null) {
			AddDock(w, func, title);
		}		
	}

	function InfoShow() {
		getObj('infoscreen').className = '';
		getObj('infoscreen').style.zIndex = 1010;
	}

	function InfoHide() {
		if (getObj('ontop').checked == false) {
			getObj('infoscreen').className = 'transparent';
			getObj('infoscreen').style.zIndex = 5;
		}
	}


	function SetupDiv(id, pmWindowx, left, top, width, height, text) {
		pmWindowx.style.position = "absolute";
		pmWindowx.style.cursor = "pointer";
		pmWindowx.style.left = left;
		pmWindowx.style.top = top;
		pmWindowx.style.width = width;
		pmWindowx.style.height = height;
		if (id == 'infoscreen') {
			pmWindowx.style.zIndex = 5;
			pmWindowx.className = 'transparent';
			pmWindowx.onmouseover = function anonymous(e) {InfoShow();}
			pmWindowx.onmouseout = function anonymous(e) {InfoHide();}
		} else if (id == 'infoscreentab') {
			pmWindowx.style.zIndex = 1010;
			pmWindowx.onmouseover = function anonymous(e) {InfoShow();}
		} else {
			pmWindowx.style.zIndex = 1010;
			pmWindowx.onmousedown = function anonymous(e) {	e = e || window.event;initializedrag(e, '' + id, getObj('' + id));}
			pmWindowx.onselectstart = function anonymous(e) {return false};
		}
		pmWindowx.innerHTML =  text;
		AddWindow(id, left, top, pmWindowx);
	}

	function AddWindow(id, left, top, pmx) {
		var x = 0;
		var FindWindow = -1
		for (x = 0; x <= window.top.windows.length; x++) {
			if (window.top.windows[x] != null) {
				if (window.top.windows[x].id == id) {
					pmx.style.left = windows[x].left;
					pmx.style.top = windows[x].top;
					FindWindow = 1;
					break;
				}
			}
		}		

		if (FindWindow = -1) {
			x = window.top.windows.length;
			window.top.windows[x] = new Array();
			window.top.windows[x] = new UpdateWindow(id, left, top);
		}
	}

	function SaveWindow(id, left, top) {
		var x = 0;
		FindWindow = -1
		for (x = 1; x <= window.top.windows.length; x++) {
			if (window.top.windows[x] != null) {
				if (window.top.windows[x].id == id) {
					windows[x].left = left;
					windows[x].top = top;
					break;
				}
			}
		}		
	}

	function UpdateWindow(id, left, top) {
	this.id = id;
	this.left = left;
	this.top = top;
	}


	function HelpScreen2(p) {
		PlayAction(1018,0, 1, 1, 0);
	}

	function Search() {
		var s = getObj('search');
		if (s != null) {
			PlayAction(1018,0, 1, 1, 0, '' + s.value);
		}
	}

	function HelpScreen(p) {
		var cp = window.top.Decks[p];
		var w = 'helpscreen';

		RemoveDock2(w);
		closepm(w);
		
		var pmWindowx = document.createElement("div");
		pmWindowx.id = '' + w;
		SetupDiv(w, pmWindowx, 50, 200, 400, 300, "<table width='100%' cellpadding=0 cellspacing=0 class='popupx'><tr style='height: 22px;' class='popupxtitle'><td class='menul'>&nbsp;&nbsp;&nbsp;</td><td class='title' style='padding-left: 0px;' nowrap width='100%'>" + ASCII("Help", 6) + "</td><td><a title='Close Window' onclick=\"closepm('" + w + "');\"><img src=\"images/cancel.png\" border=0 style=\"cursor: pointer;\"></a><a title='Min/Restore Window' onclick=\"minwin('" + w + "'," + p + ",'HelpScreen2(" + p + ");','Help');\"><img src=\"images/restore.png\" border=0 style=\"cursor: pointer;\"></a></td><td class='menur'>&nbsp;&nbsp;&nbsp;</td></tr><tr height='220'><td colspan=4><div style='height: 370px; overflow: auto; background-position: center center; background-repeat: no-repeat;' id='p" + w + "'></div></td></tr></table>");
		document.body.appendChild(pmWindowx);		
	}

	function SendChat() {
		//PlayAction(2,1, 1, 1, 0, getObj('chatsay').value);
		if (getObj('chatsay') != null) {
			PlayAction(2,1, 1, 1, 0, getObj('chatsay').value);
			getObj('chatsay').value = '';
		}
	}

	function KeyScreen() {
		var w = 'keyscreen';

		RemoveDock2(w);
		closepm(w);
		var pmWindowx = document.createElement("div");
		pmWindowx.id = '' + w;
		//<tr style='height: 22px;' class='popupxtitle'><td class='title' style='padding-left: 0px;' nowrap width='100%'>" + ASCII("Commands") + "</td><td><a title='Min/Restore Window' onclick=\"minwin('" + w + "',1,'KeyScreen();','Commands');\"><img src=\"images/restore.png\" border=0 style=\"cursor: pointer;\"></a></td></tr>
		SetupDiv(w, pmWindowx, 250,25, 450, 47, "<table width='100%' cellpadding=0 cellspacing=0 class='popupx'><tr height='100%'><td id=pkeys colspan=2>" + GetKeys() + "</td></tr></table>");
		document.body.appendChild(pmWindowx);		
	}

	function ShowTreasureGain(t) {
		//alert(t);
		var w = 'treasurescreen';
		RemoveDock2(w);
		closepm(w);
		var pmWindowx = document.createElement("div");
		pmWindowx.id = '' + w;
		window.top.AddChat('You have gained <b>' + t + '</b> treasure pieces.'); 
		
		SetupDiv(w, pmWindowx, 100, 160, 300, 125, "<table width='100%' cellpadding=0 cellspacing=0 class='popupx'><tr style='height: 22px;' class='popupxtitle'><td class='title' style='padding-left: 0px;' nowrap>" + ASCII('Treasure Gained', 6) + "</td><td><a title='Close Window' onclick=\"closepm('" + w + "');\"><img src=\"images/cancel.png\" border=0 style=\"cursor: pointer;\"></a></td></tr><tr height='100'><td colspan=2><div id=ptreasurescreen class='weakercell' style='overflow: auto; height: 100px; background-image: URL(images/treasurebox.gif); background-repeat: no-repeat; padding-left: 100px;'>" + ASCII("You have gained " + t + " treasure pieces", 6) + "</div></td></tr></table>");
		document.body.appendChild(pmWindowx);			
		setTimeout('closepm(\'' + w + '\');', 5000);
	}


	function ZoomCard() {
		var w = 'zoomscreen';

		if (MouseCard >= 0) {
		
			var cp = window.top.Cards[MouseCard];
			if (cp != null) {
				RemoveDock2(w);
				closepm(w);
				var pmWindowx = document.createElement("div");
				pmWindowx.id = '' + w;
				
				SetupDiv(w, pmWindowx, 50, 160, 200, 250, "<table width='100%' cellpadding=0 cellspacing=0 class='popupx'><tr style='height: 22px;' class='popupxtitle'><td class='title' style='padding-left: 0px;' nowrap>" + ASCII(cp.CardName) + "</td><td><a title='Close Window' onclick=\"closepm('" + w + "');\"><img src=\"images/cancel.png\" border=0 style=\"cursor: pointer;\"><a title='Min/Restore Window' onclick=\"minwin('" + w + "',1,'ZoomCard();','" + cp.CardName + "');\"><img src=\"images/restore.png\" border=0 style=\"cursor: pointer;\"></a></td></tr><tr height='100'><td colspan=2><div id=pzoom class='weakercell' style='overflow: auto; height: 250px;'><img src='images/cards/cb" + cp.CardID + ext + "'></div></td></tr></table>");
				document.body.appendChild(pmWindowx);		
			}
		}
	}

	function ShowCardInfo(c) {
		if (getObj('zoomscreen') != null) {
			MouseCard = c;
			lastinfocard = c;
			ZoomCard();
		}
	}

	function CardInfo(c) {
		clearTimeout(infotimer);
		if (lastinfocard != c) {
			infotimer = setTimeout('ShowCardInfo(' + c + ');',1000);
		}
	}

	function InfoScreen() {
		var w = 'infoscreen';
		var strout = "<table width='100%' height='170px' cellpadding=0 cellspacing=0 class='popupx'><tr height='25'><td><table><tr><td><input name=ontop id=ontop type=checkbox value=1 title='Always on Top chat'></td><td>" + ASCII("Say:",6) + "</td><td><input size='34' maxlength='500' name=chatsay id=chatsay value=''><input name=fightlocation id=fightlocation value=1 type=hidden><textarea type=hidden id=fightchat name=fightchat style='display: none;'></textarea></td><td><button onclick='SendChat();'>" + ASCII("Send", 3) + "</button></td><td id=roombox></td></td></tr></table></td></tr><tr height='150px'><td width='100%' style='padding-left: 12px;'><div id=pchat class='weakcell' style='overflow: auto; height: 150px;' onmouseover='InfoShow();'></div></td></tr></table>";
		if (getObj(w) != null) {
			var pmWindowx = getObj(w);
			if (getObj('infoscreentab') == null) {
				SetupDiv(w, pmWindowx, 0, getObj('MainBox').offsetHeight - 190, getObj('MainBox').scrollWidth, 150, strout);
			}
			pmWindowx.style.left = 0;
			pmWindowx.style.top = getObj('MainBox').offsetHeight - 190;
			pmWindowx.style.width = getObj('MainBox').scrollWidth;
			pmWindowx.style.height = 100;

		} else {
			closepm(w);
			var pmWindowx = document.createElement("div");
			pmWindowx.id = '' + w;
			SetupDiv(w, pmWindowx, 0, getObj('MainBox').offsetHeight - 190, getObj('MainBox').scrollWidth, 150, strout);
			document.body.appendChild(pmWindowx);		
		}

		w = 'infoscreentab'
		var strout = "<div class='popupx' style='height: 170px;'>" + ASCII('><br>><br>><br>>', 6) + "</div>";

		if (getObj(w) != null) {
			var pmWindowx = getObj(w);
			SetupDiv(w, pmWindowx, -10, getObj('MainBox').offsetHeight - 185, 15, 170, strout);
			pmWindowx.style.left = 200;
			pmWindowx.style.top = getObj('MainBox').offsetHeight - 185;
			pmWindowx.style.width = 10;
			pmWindowx.style.height = 100;

		} else {
			closepm(w);
			var pmWindowx = document.createElement("div");
			pmWindowx.id = '' + w;
			SetupDiv(w, pmWindowx, 200, getObj('MainBox').offsetHeight - 185, 15, 170, strout);
			document.body.appendChild(pmWindowx);	
		}

		ShowChat();
	}

	function NewAccountScreen() {
		getObj('MainBox').innerHTML = "<div style='width: 400px; height: 400px;' class='boxx_solid' align=center><table cellpadding=0 cepllspacing=0 width='100%' class='popupx' height='100%'><tr style='height: 22px;' class='popupxtitle'><td class='menul'>&nbsp;&nbsp;&nbsp;</td><td class='title' style='padding-left: 0px;' nowrap width='100%'>" + ASCII("New Account Signup", 6) + "</td><td><a title='Cancel' onclick=\"getObj('MainBox').innerHTML = '';\"><img src=\"images/cancel.png\" border=0 style=\"cursor: pointer;\"></a></td><td class='menur'>&nbsp;&nbsp;&nbsp;</td></tr><tr height='100%'><td colspan=4><table width='100%' height='100%'><tr><td colspan=2>Note: Lands of Hope account holders simply log in with your Lands of Hope username/password. All other visitors please sign up for a new account below.</td></tr><tr><td>" + ASCII("Account Name:") + "</td><td><input name='saccountname' id='saccountname' size=15 maxlength=25 value=''></td></tr><tr><td>" + ASCII("Password:") + "</td><td><input type=password name='spassword' id='spassword' size=15 maxlength=25 value=''></td></tr><tr><td>" + ASCII("Confirm Password:") + "</td><td><input type=password name='spassword2' id='spassword2' size=15 maxlength=25 value=''></td></tr><tr><td>" + ASCII("Email:") + "</td><td><input type=text name='semail' id='semail' size=20 maxlength=75 value=''></td></tr><tr><td colspan=2><input type=checkbox id=tos value=1 name=tos>" + ASCII("I agree to the TOS.",6) + "</td></tr><tr><td colspan=2><u>Multiple accounts will be blocked, so please only sign up once!</u></td></tr><tr height='100%'><td colspan=2 valign=top><button onclick='CreateAccount();'>" + ASCII("Create Account", 2); + "</button></td></tr></table></td></tr></table></div>";
	}

	function CreateAccount() {
		if (window.top.Done != 0) {
			if (getObj('tos').checked == false) {
				alert('You must agree to the Terms of Service, please check the relevant box.');
			} else if ( getObj('spassword').value ==  getObj('spassword2').value &&  getObj('spassword').value.length >= 6 &&  getObj('saccountname').value.length >= 6) {
				window.top.sendRequest('load_ajax.asp?A=4000&Value=1&sac=' + getObj('saccountname').value + '&sap=' + getObj('spassword').value + '&semail=' + getObj('semail').value);
			} else if (getObj('saccountname').value.length < 6) {
				alert('Your account name must be 6 characters or longer.');
			} else if (getObj('spassword').value.length < 6) {
				alert('Your password must be 6 characters or longer.');
			} else {
				alert('Please check your password boxes are identical.');
			}
		}		
	}

	function GetKeys() {
		var x = 0;
		var k = null;
		var strout = '';
		for (x = 0; x < 255; x++) {
			k = Keys[x];
			if (k != null) {
				strout += '<div class=divbutton id=divbutton><a href="javascript: void(0);" title="' + k.caption + '" ' + (k.disabled == 0 && ((k.gameonly == 0 || k.gameonly == 2) || (gameplaying != 0 && k.gameonly != 0)) ? 'onclick="ProcessKey(' + x + ');"' : 'style="background-color: RGB(255,100,100);"') + '><div style="width: 40px; height: 40px; background-image: URL(images/' + k.picture + '); background-repeat: no-repeat;">' + ASCII('' + k.tag) + '</div></a></div>';
			}
		}	
		return strout;
	}

	function DB(caption, disabled, picture, tag, scripted, cd) {
		DBButtons = DBButtons + 1;
		var strout = '<div class=divbutton id=divbutton><a href="javascript: void(0);" id="dbbutton' + DBButtons + '" title="' + caption + '" ' + (disabled == 0 ? 'onclick="' + scripted + (cd != 0 ? 'getObj(\'dbbutton' + DBButtons + '\').style.backgroundColor = \'RGB(255, 100 ,100)\'; getObj(\'dbbutton' + DBButtons + '\').onclick = null;' : '') + '"' : 'style="background-color: RGB(255,100,100);"') + '><div style="width: 32px; height: 32px; background-image: URL(images/gui/' + picture + '); background-repeat: no-repeat;">' + ASCII(tag) + '</div></a></div>';
		return strout;
	}


	function closeok() {
		if (getObj("autoclose").checked != 0) {
			closeit();
		}
	}

function GetLanguages() {
	var lang = new Array('us.png', 'gb.png','fr.png');
	var lang2 = new Array('english', 'english','elfish');
	var x = 0;
	var strout = '<div>';
	for (x = 0; x < lang.length; x++) {
		strout += '<img src="images/flags/' + lang[x] + '" title="' + lang2[x] + '" onclick="ChangeLanguage(\'' + lang2[x] + '\');" style="cursor: pointer;">';
	}	
	strout += '</div>'
	document.write(strout);
}

function ChangeLanguage(l) {
	window.location.replace('?lang=' + l);
}

function SetRegion(rn) {
}


function URLencode(sStr) {
	return escape(sStr).replace(/\+/g, '%2B');
}


function SendLogin() {
	var r = 0;
	if (justSent) {
		AddChat(getMsg('error.fast2'), 2);
	} else {
		sendRequest('load_ajax.asp?username=' + URLencode(getObj('username').value) + '&password=' + URLencode(getObj('password').value));
		justSent = true;
		r = setTimeout('justSent = false;', 500);
	}

	return false;
}

function rand2(x) {
	return Math.floor(Math.random()*x);
}

function rand(x) {
	return Math.floor(Math.random()*x) + 1;
}

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);
return o;
}

function extractAlpha(str) {
return str.replace(/[^a-z ]/gi,"");
}

function CloseGame() {
	closepm('keyscreen');
	closepm('zoomscreen');
	closepm('treasurescreen');
	closepm('helpscreen');
	getObj('MainBox').onclick = null;

	DisableRefresh();
}

function ShowLoginBox() {
	CloseGame();
	Dollars = 0;
	InfoScreen();
	if (getObj('RightBox').innerHTML.indexOf('gamelogin') ==-1) {
		getObj('MainBox').onclick = null;
		getObj('MainBox').innerHTML = '';
		getObj('MainBox').offsetParent.style.backgroundImage = "URL('images/table" + rand(3) + ".jpg')";
		getObj('RightBox').innerHTML = '<div style="width: 200; height: 100%;" align=center><img align=center src="images/logowide.png"><div style="width: 200px; height: 200px;" align=center valign=middle><form method="post" name="gamelogin" action="" onsubmit="return SendLogin();" style="margin: 0"><table style="width: 200px;"><tr><td style="width: 200px;">' + window.top.ASCII(getMsg('login.txt')) + '</td></tr><tr><td id=LoginResponse></td></tr><tr><td><table cellpadding=2 cellspacing=0 width=198><tr><td width="88">' + ASCII('Username:') + '</td><td width="110px"><input name="username" id="username" size=12 maxlength=30 value="" type="text" tabindex="0"></td></tr><tr><td width="88">' + ASCII('Password:') + '</td><td width="110px"><input name="password"  id="password" size=12 maxlength=30 value="" type="password" tabindex="0"></td></tr><tr><td colspan=2><button type="button" onclick="javascript:SendLogin();" tabindex="0">' + window.top.ASCII(getMsg('login.button'), 6) + '</button><button type="button" onclick="NewAccountScreen();" tabindex="0">' + window.top.ASCII(getMsg('login.signup'), 2) + '</button></td></table></td></tr></table></form></div></div>'
	}
}

function ShowBanBox() {
	alert(2);
}

function ManualRoll(v) {
	return Math.floor(Math.random()*v) + 1;
}

function UpdateWD(wd1) {
	window.top.getObj('currentwd').innerHTML = '' + wd1;
}

function selectedstuff(obj) {
    var curleft = 0;
	
    if (obj.offsetParent) {
        while (1) {
		if (obj.id.substr(0, 4) == 'mess' || obj.id == 'chatsay') {
			curleft = 1;
			break;
		}
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    }
    return curleft;
}

function VETKEY(e) {
	var chatselected = 0;
	e = e || window.event;
	if (e == null) {
	} else {
		var ae = document.activeElement;
		if (ae != null) {
			chatselected = selectedstuff(ae);
		}
		if (e.keyCode == 27) {
			e.returnValue = false;
			e.cancelBubble = true;
			if (window.top.getObj('MainBox') != null) {
				window.top.getObj('MainBox').focus();
			}
			HideSubMenu();
			
		} else if (e.keyCode == 113) {
			if (window.top.getObj('message') != null) {
				window.top.getObj('message').focus();
			}
		} else if (chatselected == 0) {
			if (window.top.getObj('pkeys') != null) {
				window.top.ProcessKey(e.keyCode);
			}
		} else if (chatselected != 0) {
			if (e.keyCode == 13) {
				//Chat enter
				SendChat();
			}
		}

	}
}

function ProcessKey(x) {
	var k = window.top.Keys[x];
	if (k != null) {
		if (k.disabled == 0 && k.timeout > 0) {
			k.disabled = 1;
			eval(k.exec);
			setTimeout('ResetKey(' + x + ');', k.timeout);
			if (window.top.getObj('pkeys') != null) {
				window.top.getObj('pkeys').innerHTML = GetKeys();
			}
		}
	}
}

function ResetKey(x) {
	var k = window.top.Keys[x];
	if (k != null) {
		k.disabled = 0;
		if (window.top.getObj('pkeys') != null) {
			window.top.getObj('pkeys').innerHTML = GetKeys();
		}
	}
}

function SendCommand(x) {
	alert(x);
//	window.top.AddChat(x, 2);
}

function GoPA(p, actionid, av, av2) {
	if (actionid == null) {
		actionid = 1001;
	}
	if (av == null) {
		av = 1;
	}
	if (av2 == null) {
		av2 = 0;
	}
	PlayAction(actionid,av, av2, p, 0,'');
}

function PlayAction(a, av, av2, p, delay, av3) {
	if (window.top.Done != 0) {
		if (delay > 0) {
			if (delayt != 0) {
				clearTimeout(delayt);
			}
			delayt = setTimeout("sendRequest('load_ajax.asp?A=" + a + "&Value=" + av + "&A2=" + av2 + "&P=" + p + "&A3=" + av3 + "');",250);
		} else {
			sendRequest('load_ajax.asp?A=' + a + '&Value=' + av + '&A2=' + av2 + '&P=' + p + '&A3=' + av3);
		}
	}
}


function AccountScreen(p, v, v2) {
	if (p == null) {
		p = 1;
	}
	if (v == null) {
		v = 0;
	}
	if (v2 == null) {
		v2 = 0;
	}
	CloseGame();
	ShowWait();
	getObj('MainBox').innerHTML = '';
	getObj('MainBox').onclick = null;
	if (v > 0) {
		getObj('RightBox').innerHTML = ASCII(getMsg('login.loading'), 2);
	} else {
		getObj('RightBox').innerHTML = '';
	}

	setTimeout("sendRequest('load_ajax.asp?A=10&Value=" + v + "&A2=" + v2 + "&P=" + p + "');",500);
}

function UpdateDeck(DeckID, DeckName) {
this.DeckID = DeckID;
this.DeckName = DeckName;
this.Cards = new Array();
this.HeroID = -1;
this.LocationID = 0;
}

function AddDeck(DeckID, DeckName) {
	var x = 0;
	DeckCount = DeckCount + 1;
	if (window.top.Decks[DeckCount] == null) {
		window.top.Decks[DeckCount] = new Array();
	}
	x = DeckCount;

	if (x > 0) {
		window.top.Decks[x] = new UpdateDeck(DeckID, DeckName);
	}
}

function StartPvPGame(r) {
	locations = 5;
	heros = 2;
	PlayAction(2000, 0, 0, 1, 0, '');
}

function InitPvPGame(r) {
	locations = 5;
	heros = 2;
	PlayAction(2001, 1, r, 1, 0, '');
}

function StartGame(r) {
	var x = 0;
	var placed = 0;
	var dp = null;
	if (r == 5) {
		heros = 2;
		locations = 8;
	} else if (r == 6) {
		heros = 3;
		locations = 15;
	} else {
		heros = 2;
		locations = 5;
	}
	if (window.top.Decks[1].Cards.length > 0) {
		for (x = 0; x < window.top.Decks[1].Cards.length; x++) {
			dp = window.top.Decks[1].Cards[x];
			if (dp != null) {
				if (dp.LocationID > 0 && dp.LocationID <= locations) {
					placed = placed + 1;
				}
			}
		}
	} else {
		alert('You appear to not have any cards in your deck. Try refreshing the game window to see if this fixes the problem.');
	}
	if (placed >= (locations * 2)) {
		ruleset = r;
		gameplaying = 1;
		setupphase = 0;
		SetName(r);
		var strout = '<table width=\'100%\' cellpadding=1 cellspacing=1><tr style=\'height: 22px;\' class=\'popupxtitle\'><td class=\'title\' style=\'padding-left: 0px;\' nowrap width=\'100%\'>' + ASCII('Basic Instructions') + '</td></tr><tr><td><div style=\'height: 340px; overflow: auto;\'>' + (ruleset == 0 ? 'To win a Trump game your Hero cards highest stat is compared to the same stat on the Monster at the current location.<br><br>If it is higher you win and advance to the next location.' : 'To win a Battle game your Hero cards attack and the Monsters defense are used to figure out how much damage your hero will deal. Your Hero card will lose health when hit.<br><br>If your Hero card kills the Monster card you can advance.') + '<br><br>Remember you can use interrupt cards at any time using the i key.</div></td></tr></table>';
		getObj('RightBox').innerHTML = strout;

		AISetup();
	} else {
		ResizeGame();
		alert('You can not start a game until you have placed all of your location cards (green ones) and your monster cards (red ones).')
	}
}

function StartPvPGameNow(r) {
	var x = 0;
	var placed = 0;
	var dp = null;

	if (window.top.Decks[1].Cards.length > 0) {
		for (x = 0; x < window.top.Decks[1].Cards.length; x++) {
			dp = window.top.Decks[1].Cards[x];
			if (dp != null) {
				if (dp.LocationID > 0 && dp.LocationID <= locations) {
					placed = placed + 1;
				}
			}
		}
	} else {
		alert('You appear to not have any cards in your deck. Try refreshing the game window to see if this fixes the problem.');
	}

	if (placed >= (locations * 2)) {
		ruleset = 3;
		gameplaying = 1;
		setupphase = 0;
		SetName(3);
		var strout = '<table width=\'100%\' cellpadding=1 cellspacing=1><tr style=\'height: 22px;\' class=\'popupxtitle\'><td class=\'title\' style=\'padding-left: 0px;\' nowrap width=\'100%\'>' + ASCII('Basic Instructions') + '</td></tr><tr><td><div style=\'height: 340px; overflow: auto;\'>To win a PvP Battle game your Hero cards must get to the end before the player you are playing does.<br><br>Remember you can use interrupt cards at any time using the i key.</div></td></tr></table>';
		getObj('RightBox').innerHTML = strout;
		QuickSetup();
	} else {
		alert('You can not start a game until you have placed all of your location cards (green ones) and your monster cards (red ones).')
	}
}

function QuickSetup() {
	var x = 0;
	var a = null;
	if (window.top.Decks[1].Cards.length <= 0) {
		alert('Please reload the game, as your deck cards could not be found.');
	} else {
		for (x = 0; x < window.top.Decks[1].Cards.length; x++) {
			a = window.top.Decks[1].Cards[x];
			if (a != null) {
				a.Stats[1] = 0;
				a.Stats[2] = 0;
				a.Stats[3] = 0;
				a.Stats[4] = 0;
				a.Used = 0;
				a.IStats[1] = 0;
				a.IStats[2] = 0;
				a.IStats[3] = 0;
				a.IStats[4] = 0;
			}
		}
	}

	for (x = 0; x < 170; x++) {
		a = window.top.Cards[x];
		if (a != null) {
			a.Picked = 0;
		}
	}

	if (window.top.Decks[1].HeroID == -1) {
		window.top.Decks[1].HeroID = FindHeroCard(1, window.top.Decks[1].HeroID)
	}
	window.top.Decks[1].LocationID = 0;
	window.top.Decks[2].LocationID = 0;

	if (window.top.Decks[2].HeroID == -1) {
		window.top.Decks[2].HeroID = FindHeroCard(2, window.top.Decks[2].HeroID)
	} else {
		
	}

	setupphase = 0;
	MasterDeckID = 1;
	ResizeGame();
}

function AISetup() {
	var x = 0;
	var a = null;
	if (window.top.Decks[1].Cards.length <= 0) {
		alert('Please reload the game, as your deck cards could not be found.');
	} else {
		for (x = 0; x < window.top.Decks[1].Cards.length; x++) {
			a = window.top.Decks[1].Cards[x];
			if (a != null) {
				a.Stats[1] = 0;
				a.Stats[2] = 0;
				a.Stats[3] = 0;
				a.Stats[4] = 0;
				a.Used = 0;
				a.IStats[1] = 0;
				a.IStats[2] = 0;
				a.IStats[3] = 0;
				a.IStats[4] = 0;
			}
		}
	}


	for (x = 0; x < 170; x++) {
		a = window.top.Cards[x];
		if (a != null) {
			a.Picked = 0;
		}
	}

	if (window.top.Decks[2] != null) {
		window.top.Decks[2] = new Array();
		window.top.Decks[2].Cards = new Array();
		DeckCount = DeckCount - 1;
	}
	AddChat('Setting up Deck using ' + window.top.Cards.length + ' cards!', 4);
	AddDeck(2, 'NPC Deck');
	AddCards(2, locations, 1);
	AddCards(2, 2, heros);
	AddCards(2, locations + 5, 3);
	AddCards(2, 6, 4);
	AddCards(2, 6, 5);

	if (window.top.Decks[1].HeroID == -1) {
		window.top.Decks[1].HeroID = FindHeroCard(1, window.top.Decks[1].HeroID)
	}
	window.top.Decks[1].LocationID = 0;
	window.top.Decks[2].LocationID = 0;

	SetupDeck(2)

	if (window.top.Decks[2].HeroID == -1) {
		window.top.Decks[2].HeroID = FindHeroCard(2, window.top.Decks[2].HeroID)
	}

	setupphase = 0;
	MasterDeckID = 1;
	ResizeGame();
}

function EnterLocation(l, d) {
	//Move to a new location.
	//var d = 1;
	if (l = (window.top.Decks[d].LocationID + 1)) {
		if (AttackCard(l, d) == 1) {
			window.top.Decks[d].LocationID = window.top.Decks[d].LocationID + 1;
			if (window.top.Decks[d].HeroID != -1) {
				var a = window.top.Decks[d].Cards[window.top.Decks[d].HeroID];
				if (ruleset != 2 && ruleset != 5 && ruleset != 6) {
					a.Stats[1] = 0;
					a.Stats[2] = 0;
					a.Stats[3] = 0;
					a.Stats[4] = 0;
				}
				a.IStats[1] = 0;
				a.IStats[2] = 0;
				a.IStats[3] = 0;
				a.IStats[4] = 0;
			}
		}
		if (window.top.Decks[d].LocationID >= locations) {
			closepm('keyscreen');
			closepm('zoomscreen');
			if (d == 1) {
				PlayAction(1007, -2, ruleset, 1, 0);
			} else {
				PlayAction(1007, -1, ruleset, 1, 0);
			}
		} else {
			if (d == 1 && window.top.Decks[1].HeroID == -1)  {
				//You are dead
				closepm('keyscreen');
				closepm('zoomscreen');
				PlayAction(1007, -1, ruleset, 1, 0);	
			}
			if (d == 1 && window.top.Decks[2].HeroID != -1 && ruleset != 3) {
				//MasterDeckID = 2
				EnterLocation(window.top.Decks[2].LocationID + 1, 2);			
			} else {
				ResizeGame();
			}
		}
	}
}

function ExitGame() {
	closepm('keyscreen');
	closepm('zoomscreen');
	closepm('treasurescreen');

	ResetGame();
	//closegame();
	PlayAction(10, 0, 0, 1, 0);	
}

function ResetGame(a) {
	gameplaying = 0;
	DeckCount = 0;
	setupphase = 1;
	MasterDeckID = 1;
	window.top.Decks = new Array();
	
}

function PickandUseInterrupt(d, s) {
	var x = 0;
	var y = 0;
	var dp = null;
	var strout = '';
	var ct = 4;
	//alert(window.top.Decks[d].Cards.length);
	for (x = 0; x < window.top.Decks[d].Cards.length; x++) {
		dp = window.top.Decks[d].Cards[x];
		if (dp != null) {	
			if (dp.CardTypeID == ct && dp.Used == 0) {
				var c = window.top.Cards[dp.CardID];
				if (c.Stats[s] != 0) {
					PickCardI(x, d);
					break;
				}
			}
		}
	}
}

function AttackCard(l, d) {
	var x = 0;
	var lc = 0;
	var mc = 0;
	var won = 0;

	var d2 =  (d == 2 ? 1 : 2);

	for (x = 0; x < window.top.Decks[d2].Cards.length; x++) {
		cp = window.top.Decks[d2].Cards[x];
		if (cp != null) {
			cp.Used == 0;
			if (cp.LocationID == l && cp.CardTypeID == 1 && lc == 0) {
				lc = x;
			}
			if (cp.LocationID == l && cp.CardTypeID == 3 && mc == 0) {
				mc = x;
			}
		}
	}
	var HeroCard = window.top.Decks[d].HeroID;
	//alert(HeroCard + ' ' + d);
	if (HeroCard == -1) {
		AddFightChat('<div>All Hero Cards are Dead!</div>' + d,2)
		//StartGame();
	} else {
	var pc = window.top.Decks[d].Cards[HeroCard];
	var pch = GetCardStat(pc.CardID, 1) + pc.IStats[1];
	var pcm = GetCardStat(pc.CardID, 2) + pc.IStats[2];
	var pca = GetCardStat(pc.CardID, 3) + pc.IStats[3];
	var pcd = GetCardStat(pc.CardID, 4) + pc.IStats[4];

	var ach = 0;
	var acm = 0;
	var aca = 0;
	var acd = 0;
	if (pc.AugmentID > 0) {
		var ac = window.top.Cards[pc.AugmentID];
		var ach = GetCardStat(ac.CardID, 1);
		var acm = GetCardStat(ac.CardID, 2);
		var aca = GetCardStat(ac.CardID, 3);
		var acd = GetCardStat(ac.CardID, 4);
	}

	var tc = window.top.Decks[d2].Cards[lc];
	var tch = GetCardStat(tc.CardID, 1);
	var tcm = GetCardStat(tc.CardID, 2);
	var tca = GetCardStat(tc.CardID, 3);
	var tcd = GetCardStat(tc.CardID, 4);

	pch = pch + tch + ach;
	pcm = pcm + tcm + acm;
	pca = pca + tca + aca;
	pcd = pcd + tcd + acd;

	var mc = window.top.Decks[d2].Cards[mc];
	var mch = GetCardStat(mc.CardID, 1);
	var mcm = GetCardStat(mc.CardID, 2);
	var mca = GetCardStat(mc.CardID, 3);
	var mcd = GetCardStat(mc.CardID, 4);

	if ((ruleset == 0 || ruleset == 1 || ruleset == 2 || ruleset == 5 || ruleset == 6) && d == 2) {
		if (rand(3) == 1 && tc.CardID != 112 && tc.CardID != 113) {
			if (tca < 0) {
				//find an attack boost card
				PickandUseInterrupt(d, 3);
			} else if (tcd < 0) {
				//find a defense boost card
				PickandUseInterrupt(d, 4);
			} else if (pch <  GetCardStat(pc.CardID, 1)) {
				PickandUseInterrupt(d, 1);
			}
		}
	
	}

	if (ruleset == 0) {
		var looper = 0;
		for (x = 0; x < 1; x++) {
			var hs = GetHighStats(pch, pcm, pca, pcd);
			AddFightChat('<div><font style="color: ' + (d == 1 ? '#66ff66' : '#ff6666') + '">' + hs + ' ' + pc.CardName + ' (HP: ' + pch + ' MP: ' + pcm + ' AP: ' + pca + ' DP: ' + pcd + ') fights ' + mc.CardName + ' (HP: ' + mch + ' MP: ' + mcm + ' AP: ' + mca + ' DP: ' + mcd + ') at ' + tc.CardName + '</font></div>', 2);
			if (hs == 1) {
				if (pch > mch) {
					//Hero kills monster
					won = 1;
				} else if (pch == mch) {
					pch = 0;
					x = -1;
				}
			} else if (hs == 2) {
				if (pcm > mcm) {
					//Hero kills monster
					won = 1;
				} else if (pcm == mcm) {
					pcm = 0;
					x = -1;
				}
			} else if (hs == 3) {
				if (pca > mca) {
					//Hero kills monster
					won = 1;
				} else if (pca == mca) {
					pca = 0;
					x = -1;
				}
			} else if (hs == 4) {
				if (pcd > mcd) {
					//Hero kills monster
					won = 1;
				} else if (pcd == mcd) {
					pcd = 0;
					x = -1;
				}
			}
			looper = looper + 1;
			if (looper > 5) {
				won = -1;
				break;
			}
		}
	} else {
		//Card combat
		won = -1;

		if (pca > (pcm/2)) {
			var dx = (pca - rand2(mcd));
			if (dx < 0) {
				dx = 0;
			}
			AddFightChat('<div><font style="color: ' + (d == 1 ? '#66ff66' : '#ff6666') + '">' + pc.CardName + ' damaged ' + mc.CardName + ' for ' + dx + 'pts.</font></div>', 2);
			if (dx > 0) {
				mc.Stats[1]= mc.Stats[1] - dx;
				mch = (mch + mc.Stats[1]);
			}
		} else {
			dx = (rand(pcm) - rand2(mcd));
			if (dx < 0) {
				dx = 0;
			}
			AddFightChat('<div><font style="color: ' + (d == 1 ? '#66ff66' : '#ff6666') + '">' + pc.CardName + ' magically damaged ' + mc.CardName + ' for ' + dx + 'pts.</font></div>', 2);
			if (dx > 0) {
				mc.Stats[1]= mc.Stats[1] - dx;
				mch = (mch + mc.Stats[1]);
			}
		}
		if (mch <= 0) {
			//One hit kill
			won = 1;
		}		
	

		if (won != 1) {

			if (mca > (mcm / 2)) {
			//Monster vs You
				var dx = (mca - rand2(pcd));
				if (dx < 0) {
					dx = 0;
				}
				AddFightChat('<div><font style="color: ' + (d == 1 ? '#66ff66' : '#ff6666') + '">' +  mc.CardName + ' damaged ' + pc.CardName + ' for ' + dx + 'pts.</font></div>', 2);

				if (dx > 0) {
					pc.Stats[1]= pc.Stats[1] - dx;
					pch = (pch + pc.Stats[1]);
				}
			} else {
	
				dx = (rand(mcm) - rand2(pcd));
				if (dx < 0) {
					dx = 0;
				}
				AddFightChat('<div><font style="color: ' + (d == 1 ? '#66ff66' : '#ff6666') + '">' + mc.CardName + ' magically damaged ' + pc.CardName + ' for ' + dx + 'pts.</font></div>', 2);
				if (dx > 0) {
					pc.Stats[1]= pc.Stats[1] - dx;
					pch = (pch + pc.Stats[1]);
				}
			}
			if (pch <= 0) {
				//One hit kill
				won = 0;
			}
		}
	}

	if (won == 0) {
		//HeroCard died
		AddFightChat('<div>' + pc.CardName + ' was defeated!</div>', 2);
		pc.Used = 1;
		window.top.Decks[d].HeroID = FindHeroCard(d, window.top.Decks[d].HeroID)
		HeroCard = window.top.Decks[d].HeroID;
	} else if (won == 1) {
		AddFightChat('<div>' + pc.CardName + ' defeated ' + mc.CardName + '!</div>', 2);
	} else if (won == -1 && ruleset == 0) {
		AddFightChat('<div>' + pc.CardName + ' drew with ' + mc.CardName + '!</div>', 2);
	}
	if (HeroCard == -1) {
		AddFightChat('<div>All Hero cards are dead!</div>', 2);
	}
	}
	return won;
}


function FindHeroCard(d, oldh) {
	var x = 0;
	var h = -1;
	var sh = 1;
	for (x = 0; x < window.top.Decks[d].Cards.length; x++) {
		var a = window.top.Decks[d].Cards[x];
		if (a != null) {
			if (a.CardTypeID == 2) {
				sh = sh + 1;
			}
			if (a.CardTypeID == 2 && a.Used == 0) {
				h = x;
				break;
			}
			if (sh >= (heros + 1)) {
				break;
			}
		}
	}
	return h;

}

function GetHighStats(h, m, a, d) {
	var c = new Array(h, m, a, d);
	var v = c[0];
	var v2 = 0;
	var v3 = 1;
	for (x = 0; x < 4; x++) {
		v2 = c[x];
		if (v2 > v) {
			v = v2;
			v3 = x + 1;
		}
	}
	return v3;
}

function GetHighStat(cid) {
	var x = 0;
	var c = window.top.Cards[cid];
	var v = GetCardStat(cid, 1);
	var v2 = 0;
	var v3 = 1;
	for (x = 2; x < 5; x++) {
		v2 = GetCardStat(cid, x);
		if (v > v2) {
			v = v2;
			v3 = x;
		}
	}
	return v3;
}

function GetCardStat(cid, stat) {
	var v = 0;
	if (window.top.Cards[cid] == null) {
	} else {
		if (window.top.Cards[cid].Stats[stat] != null) {
			v = window.top.Cards[cid].Stats[stat].StatValue;
		}
	}
	return v;
}

function TakeTurn(l, d) {
	if (getObj('TurnButton') == null) {
		TurnTaken = 0;
	} else {
		if (TurnTaken == 0) {
			TurnTaken = (ruleset == 3 ? 11 : 2);
			EnterLocation(l, d);
			TurnButtonTimer();
		} else {
		}
	}
}

function TurnButtonTimer() {
	if (getObj('TurnButton') != null) {
		TurnTaken = TurnTaken - 1;
		if (TurnTaken <= 0) {
			TurnTaken = 0;
			getObj('TurnButton').innerHTML = ASCII('Take Turn', 6);
		} else {
			getObj('TurnButton').innerHTML = ASCII('Wait ' + TurnTaken + 's', 3);
			setTimeout('TurnButtonTimer();', 1000);
		}
	} else {
		TurnTaken = 0;
	}
}

function DrawLocationCard(d, l) {
	var strout = '';
	var mw = getObj('MainBox').scrollWidth;
	var cw = 200;
	var ch = 250;
	var cs = locations;
	var cz = ch / 3;
	var l2= 2;

	if (l > (window.top.Decks[(d == 1 ? 2 : 1)].LocationID + 1)) {
		strout += '<div id=layer' + l + ' style="position: absolute; left: ' + (((cw + 5) * l2) - cw) + ';top: ' + (1 * cz) + '; border: 1px dashed red; width: ' + cw + 'px; height: ' + ch + ';"><img src="images/cards/card1.gif" width="' + cw + '" height="' + ch + '"></div>';
	} else {
		for (x = 0; x < window.top.Decks[d].Cards.length; x++) {
			cp = window.top.Decks[d].Cards[x];
			if (cp != null) {
				if (cp.LocationID == l && cp.CardTypeID == 1) {
					strout += '<div onmouseover="MouseCard = ' + cp.CardID + '; CardInfo(' + cp.CardID + ');" id=layer' + l + ' style="position: absolute; left: ' + (((cw + 5) * l2) - cw) + ';top: ' + ((1 * cz)) + '; border: 1px dashed red; width: ' + cw + 'px; height: ' + ch + ';"><img src="' + imagepath + (window.top.Decks[(d == 1 ? 2 : 1)].LocationID >= l ? 'defeated1.png' : 'cb' + cp.CardID + ext) + '" width="' + cw + '" height="' + ch + '"></div>';
					strout += '<div style="position: absolute; left: ' + (((cw + 5) * l2) - cw) + ';top: ' + (((1 * cz) + (ch - 15))) + '; width: ' + cw + 'px; height: 30; z-index: 500; cursor: pointer;"><center><button id=TurnButton onclick="TakeTurn(' + l + ', ' + (d == 1 ? 2 : 1) + ');">' + (TurnTaken == 0 ? ASCII('Take Turn', 6) : ASCII('Wait ' + TurnTaken + 's', 3)) + '</button></center></div>';
				}
				if (cp.LocationID == l && cp.CardTypeID == 3) {
					strout += '<div onmouseover="MouseCard = ' + cp.CardID + ';  CardInfo(' + cp.CardID + ');" id=mlayer' + l + ' style="position: absolute; left: ' + (((cw + 5) * l2) - (cw/ 2)) + ';top: ' + ((1 * cz) + ch) + '; border: 1px dashed red; width: ' + cw + 'px; height: ' + ch + '; cursor: pointer;"><img src="' + imagepath + 'cb' + cp.CardID + ext + '" width="' + cw + '" height="' + ch + '"></div>';
					strout += GetCardStats(cp, cw, l, ch, cz, cs);
					break;
				}
			}
		}
	}

	return strout;

}

function GetCardStats(cp, cw, l, ch, cz, cs) {
	var c = window.top.Cards[cp.CardID];
	var strout = '';
	if (c != null) {
		var v1 = (c.Stats[1] ? c.Stats[1].StatValue : 0);
		var v2 = (c.Stats[2] ? c.Stats[2].StatValue : 0);
		var v3 = (c.Stats[3] ? c.Stats[3].StatValue : 0);
		var v4 = (c.Stats[4] ? c.Stats[4].StatValue : 0);
		var cw1 = 50;
		var ch1 = 99;

		var cw3 = 20;

		var l2 = 2;

		if (cw > 100) {
			strout = '<div id=slayer' + l + ' style="font-weight: bold; position: absolute; left: ' + ((((cw + 5) * l2) - (cw / 2)) + (130)) + ';top: ' + ((1 * cz) + (ch + 31)) + '; width: ' + cw1 + 'px; height: ' + ch1 + '; background-Color: black; cursor: pointer; z-Index: 10;"><img src="images/hp.png" width="' + cw3 + '" height="' + cw3 + '"><font style="color: #FF6666;">' + (v1 + cp.Stats[1]) + '</font><br><img src="images/mp.png" width="' + cw3 + '" height="' + cw3 + '"><font style="color: #6666FF;">' + (v2 + cp.Stats[2]) + '</font><br><img src="images/ap.png" width="' + cw3 + '" height="' + cw3 + '"><font style="color: #666666;">' + (v3 + cp.Stats[3]) + '</font><br><img src="images/dp.png" width="' + cw3 + '" height="' + cw3 + '"><font style="color: #666666;">' + (v4 + cp.Stats[4]) + '</font></div>';
		}
	}
	return strout;
}

function DrawHeroCard(d) {
	var strout = '';
	var mw = getObj('MainBox').scrollWidth;
	var cw = 200;
	var ch = 250;
	var cs = locations;
	var cz = ch / 3;
	var l2 = 2;
	cp = window.top.Decks[d].Cards[window.top.Decks[d].HeroID];
	if (cp != null) {
		var l = window.top.Decks[d].LocationID;
		//' + (cz + ((cp.CardTypeID * cz) - cz)) + '
		strout += '<div onmouseover="MouseCard = ' + cp.CardID + ';  CardInfo(' + cp.CardID + ');" id=hero style="position: absolute; left: ' + (((cw + 5) * l2) - (cw * 1.5)) + ';top: ' + ((1 * cz) + ch) + '; border: 1px dashed gold; width: ' + cw + 'px; height: ' + ch + '; cursor: pointer; z-Index: 100;"><img src="' + imagepath + 'cb' + cp.CardID + ext + '" width="' + cw + '" height="' + ch + '"></div>';
		strout += GetHeroCardStats(cp, cw, l, ch, cz, cs, cp.CardTypeID, d);
		if (cp.AugmentID > 0) {
			strout += '<div onmouseover="MouseCard = ' + cp.AugmentID + ';  CardInfo(' + cp.AugmentID + ');" id=heroa style="position: absolute; left: ' + (((cw + 5) * l2) - ((cw * 1.5) + (cw/2))) + ';top: ' + ((1 * cz) + ((ch * 1.5))) + '; border: 1px dashed brown; width: ' + (cw / 2) + 'px; height: ' + (ch / 2) + '; cursor: pointer; z-Index: 100;"><img src="' + imagepath + 'cb' + cp.AugmentID + ext + '" width="' + (cw / 2) + '" height="' + (ch / 2) + '"></div>';
		}

	}

	return strout;

}

function GetHeroCardStats(cp, cw, l, ch, cz, cs, ct, d) {
	var d2 = (d == 1 ? 2 : 1);
	var lc = -1;
	var x = 0;
	for (x = 0; x < window.top.Decks[d2].Cards.length; x++) {
		tc = window.top.Decks[d2].Cards[x];
		if (tc != null) {
			if (tc.LocationID == (l + 1) && tc.CardTypeID == 1 && lc == -1) {
				lc = x;
				break;
			}
		}
	}

	if (lc >= 0) {
		var tch = GetCardStat(tc.CardID, 1);
		var tcm = GetCardStat(tc.CardID, 2);
		var tca = GetCardStat(tc.CardID, 3);
		var tcd = GetCardStat(tc.CardID, 4);
	} else {
		var tch = 0;
		var tcm = 0;
		var tca = 0;
		var tcd = 0;
	}

	var c = window.top.Cards[cp.CardID];
	var strout = '';
	if (c != null) {
		var a1 = 0;
		var a2 = 0;
		var a3 = 0;
		var a4 = 0;
		if (cp.AugmentID > 0) {
			var a = window.top.Cards[cp.AugmentID];
			if (a != null) {
				var a1 = (a.Stats[1] ? a.Stats[1].StatValue : 0);
				var a2 = (a.Stats[2] ? a.Stats[2].StatValue : 0);
				var a3 = (a.Stats[3] ? a.Stats[3].StatValue : 0);
				var a4 = (a.Stats[4] ? a.Stats[4].StatValue : 0);
			}
		}

		var v1 = ((c.Stats[1] ? c.Stats[1].StatValue : 0) + cp.IStats[1]) + a1;
		var v2 = ((c.Stats[2] ? c.Stats[2].StatValue : 0) + cp.IStats[2]) + a2;
		var v3 = ((c.Stats[3] ? c.Stats[3].StatValue : 0) + cp.IStats[3]) + a3;
		var v4 = ((c.Stats[4] ? c.Stats[4].StatValue : 0) + cp.IStats[4]) + a4;
		var cw1 = 50;
		var ch1 = 99;
		var l2 = 2;
		var cw3 = 20;

		if (cw > 100) {
			strout = '<div id=slayer' + l + ' style="font-weight: bold; position: absolute; left: ' + ((((cw + 5) * l2) - (cw * 1.5)) + 130) + ';top: ' + ((1 * cz) + (ch + 31)) + '; width: ' + cw1 + 'px; height: ' + ch1 + '; background-Color: black; cursor: pointer; z-index: 100;"><img src="images/hp.png" width="' + cw3 + '" height="' + cw3 + '"><font style="color: #FF6666;">' + (tch + v1 + cp.Stats[1]) + '</font><br><img src="images/mp.png" width="' + cw3 + '" height="' + cw3 + '"><font style="color: #6666FF;">' + (tcm + v2 + cp.Stats[2]) + '</font><br><img src="images/ap.png" width="' + cw3 + '" height="' + cw3 + '"><font style="color: #666666;">' + (tca + v3 + cp.Stats[3]) + '</font><br><img src="images/dp.png" width="' + cw3 + '" height="' + cw3 + '"><font style="color: #666666;">' + (tcd + v4 + cp.Stats[4]) + '</font></div>';
		}
	}
	return strout;
}

function GetHighStatR(cid) {
	var x = 0;
	var c = window.top.Cards[cid];
	var v = GetCardStat(cid, 1);
	var v2 = 0;
	var v3 = 1;
	for (x = 2; x < 5; x++) {
		v2 = GetCardStat(cid, x);
		if (v2 < v) {
			v = v2;
			v3 = x;
		}
	}
	return v3;
}

function GetHighStatR(cid) {
	var x = 0;
	var c = window.top.Cards[cid];
	var v = GetCardStat(cid, 1);
	var v2 = 0;
	var v3 = 1;
	for (x = 2; x < 5; x++) {
		v2 = GetCardStat(cid, x);
		if (v2 < v) {
			v = v2;
			v3 = x;
		}
	}
	return v3;
}

function SetupDeck(DeckID) {

	var x = 0;
	var cc = 0;
	var breaker = 0;
	var limit = 0;

	limit = window.top.Decks[DeckID].Cards.length - 1;

	AddChat('Setting up Deck ' + DeckID + '!', 4);
	
	var y1 = 0;
	for (x = 0; x <= limit; x++) {
		var a = window.top.Decks[DeckID].Cards[x];
		if (a != null) {		
			if (a.CardTypeID == 1 && a.LocationID == 0 && (rand(2) == 1)) {
				cc = cc + 1;
				a.LocationID = cc;
				y1 = GetHighStatR(a.CardID);
				for (y = 0; y < window.top.Decks[DeckID].Cards.length; y++) {
					var b = window.top.Decks[DeckID].Cards[y];
					if (b != null) {
						if (b.CardTypeID == 3 && b.LocationID == 0) {
							var y2 = GetCardStat(b.CardID, y1);
							if (y2 >=  4 && rand(4) == 1) {
								b.LocationID = cc;
								break;	
							}
						}
					}
					if (b.LocationID == 0 && y >= (limit))  {
						y = -1;
					}
				}

			}
			if (cc >= locations) {
				break;
			}

		}
		if (cc < locations && breaker < 55 && x >= (limit)) {
			breaker = breaker + 1;
			x = -1;
		}
	}
}

function AddCards(DeckID, Cards, CardType) {

	var x = 0;
	var cc = 0;
	var tmplimit = 150;
	var breaker = 0;
	for (x = 0; x < tmplimit; x++) {
		var a = window.top.Cards[x];
		if (a != null) {
			if (a.CardTypeID == CardType && rand(5) == 2 && a.Picked == 0) {
				cc = cc + 1;
				a.Picked = 1;
				AddCard(DeckID, a.CardID, a.CardName, a.CardTypeID, 0, 0);
			}
			if (cc >= Cards) {
				break;
			}
		}
		if (cc < Cards && breaker < 20 && x >= (tmplimit - 1)) {
			breaker = breaker + 1;
			x = -1;
		}
	}	
}

function CardInDeck(DeckID, CardID) {
	var x = 0;
	var cc = 0;
	for (x = 0; x < window.top.Decks[DeckID].Cards.length; x++) {
		var a = window.top.Decks[DeckID].Cards[x];
		if (a != null) {
			if (a.CardID == CardID) {
				cc = 1;
				break;
			}
		}
	}	
	return cc;
}

function AddKey(keyCode, timeout, exec, tag, caption, picture, gameonly) {
	window.top.Keys[keyCode] = new Array();
	if (gameonly == null) {
		gameonly = 0
	}
	window.top.Keys[keyCode] = new UpdateKey(keyCode, timeout, exec, tag, caption, picture, gameonly)
}

function UpdateKey(keyCode, timeout, exec, tag, caption, picture, gameonly) {
	this.keyCode = keyCode;
	this.timeout = timeout;
	this.exec = exec;
	this.tag = tag;
	this.picture = picture;
	this.caption = caption;
	this.disabled = 0;
	this.gameonly = gameonly;
}

function ACB(CardID, CardName, CardTypeID, PowerCost) {
	window.top.Cards[CardID] = new Array();
	window.top.Cards[CardID] = new UpdateBCard(CardID, CardName, CardTypeID, PowerCost);
}

function ACBS(CardID, StatName, StatID, StatValue) {
	window.top.Cards[CardID].Stats[StatID] = new Array();
	window.top.Cards[CardID].Stats[StatID] = new UpdateStat(CardID, StatName, StatID, StatValue);
}

function UpdateStat(CardID, StatName, StatID, StatValue) {
this.CardID = CardID;
this.StatName = StatName;
this.StatID = StatID;
this.StatValue = StatValue;
}

function UpdateBCard(CardID, CardName, CardTypeID, PowerCost) {
this.CardID = CardID;
this.CardName = CardName;
this.CardTypeID = CardTypeID;
this.Stats = new Array();
this.Picked = 0;
this.PowerCost = PowerCost;
}


function AddCard(DeckID, CardID, CardName, CardTypeID, LocationID, AugmentID, Used) {
	var x = 0;
	if (Used == null) {
		Used = 0;
	}
	for (x = 1; x <= DeckCount; x++) {
		if (window.top.Decks[x] != null) {
			if (window.top.Decks[x].DeckID == DeckID) {
				var l = window.top.Decks[x].Cards.length;
				window.top.Decks[x].Cards[l] = new Array();
				window.top.Decks[x].Cards[l] = new UpdateCard(DeckID, CardID, CardName, x, CardTypeID, LocationID, AugmentID, Used);
				break;
			}
		}
	}		
}

function UpdateCard(DeckID, CardID, CardName, d, CardTypeID, LocationID, AugmentID, Used) {
this.DeckID = DeckID;
this.CardID = CardID;
this.CardName = CardName;
this.d = d;
this.CardTypeID = CardTypeID;
this.LocationID = LocationID;
this.AugmentID = AugmentID;
this.Used = Used;
//Temp array to store augmented card stats
this.Stats = new Array(0,0,0,0,0);
this.IStats = new Array(0,0,0,0,0);
}

function ShowPlayerBox(i) {
}

function DollarClick() {
	//Clicked on dollars now do stuff
	popupwindow("dollars.asp?", "", 1, 0, "Legend Dollars - Management", "", "", 500, 400);
}

function GameReturn(returnVal, pb) {
	if (returnVal != null) {
		if (returnVal > 0) {
		}
	}
}

function ShowPlayerBoxShort(i) {
	if (i == null) {
 		i = 1;
	}
	if (getObj('statbar') != null) {
		getObj('statbar').innerHTML = getHnP(i);
	}
}

function getHnP(i) {
	if (i == null) {
 		i = 1;
	}
	var strout = '';

	return strout;
}

function jsfix(str, v1, v2) {
	var x = str.replace(/X1/gi, v1);
	x = x.replace("\{P\}", v2);
	return x;
}

function GetBox(Tag, color, value1, value2) {
	var b = GetPerc(value2, value1)
	var strout = PercentBoxSmall(100, b, color,ASCII(value1 + '/' + value2));
	return strout;
}

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, height) {
	if (height == null) {
		height = 10;
	}
	if (caption == '') {
		caption = PercentValue + '%';
	}
	return '<div style="width: ' + pwidth + 'px; height: ' + height + 'px; position: relative; background: URL(images/black.gif) repeat-x"><div style="width: ' + ((pwidth / 100) * PercentValue) + 'px;height: ' + height + 'px; position: static; background: URL(images/' + Color + '.gif) repeat-x"><div class=perc style=" position: absolute; width:' + pwidth + 'px"><center>' + caption + '</center></div></div></div>';
}

function ASCII(text, c, f) {
	if (c == null) {
		c = 1;
	}
	var AsciiOn = 1;
	var strout = '';
	var x = 0;
	if (text != null) {
		if (text.replace != null) {
			text = text.replace(/<br>/gi,"|");
		}
	}

	if (f == null) {
		f = FontFolder;
	}

	if (AsciiOn == 0) {
		strout = text;
	} else {
		for (x = 0; x < text.length; x++) {
			if (text.charAt(x) == "|") {
				strout += '<br>';
			} else if (text.charAt(x) == " ") {
				strout += '&nbsp; ';
			} else {
				strout += '<img border=0 src="images/fonts/' + f + '/c' + text.charCodeAt(x) + '_' + c + '.png">';
			}
		}	
	}
	return strout;
}


function ClearPlayers() {
	DeckCount = 0;
	window.top.Decks = new Array();
}

function ClearDeckCards(d) {
	var x = 0;
	for (x = 1; x <= DeckCount; x++) {
		if (window.top.Decks[x] != null) {
			if (window.top.Decks[x].DeckID == d) {
				window.top.Decks[x].Cards = new Array;
			}
		}
	}	
}


function toProperCase(s)
{
  return s.toLowerCase().replace(/^(.)|\s(.)/g, 
          function($1) { return $1.toUpperCase(); });
}


function DrawDeck() {
	var strout = '';

	strout += '' + DrawCards(MasterDeckID) + '';

	getObj('MainBox').onclick = function anonymous(event) {MainBoxClick(event)}
	getObj('MainBox').onkeydown = function anonymous(event) {VETKEY(event)}

	return strout;
}

function MainBoxClick(e) {

}

function SetLocation(l) {
	var d = window.top.Decks[2];
	if (d != null) {
		window.top.Decks[2].LocationID = l;
		getObj('Progress').innerHTML = GetLocations();
	}
}

function DrawCards(d) {
	var x = 0;
	var y = 0;
	var dp = null;
	var placed = 0;
	var strout = '';
	if (setupphase == 1) {

		for (x = 1; x < (locations + 1); x++) {
			strout += DrawCardLayer(x);
		}

		for (x = 0; x < window.top.Decks[d].Cards.length; x++) {
			dp = window.top.Decks[d].Cards[x];
			if (dp != null) {
				strout += DrawCard(d, x);
			}
		}

		if (getObj('dkeyscreen') == null) {
			KeyScreen();
		}
			
	} else {
		getObj('fightlocation').value = window.top.Decks[d].LocationID;
		
		strout += '<div id=Progress>' + GetLocations() + '</div>';
		strout += DrawLocationCard2((d == 1 ? 2 : 1), window.top.Decks[d].LocationID + 1);
		strout += DrawLocationCard((d == 1 ? 2 : 1), window.top.Decks[d].LocationID + 1);
		strout += DrawHeroCard(d);
	}
	
	return strout;
}

function GetLocations() {
	var x = 0;
	var strout = ASCII('Progress: ', 6);
	for (x = 0; x < locations; x++) {
		if (x < window.top.Decks[1].LocationID) {
			strout += '<img src="images/location.png" width=20 height=20 title="' + GetLocationName(2, x + 1) + '">';
		} else {
			strout += '<img src="images/locationnd.png" width=20 height=20 title="' + GetLocationName(2, x + 1) + '">';
		}
	}
	strout += ASCII(' vs ', 2, 'mingb');

	for (x = 0; x < locations; x++) {
		if (x < window.top.Decks[2].LocationID) {
			strout += '<img src="images/location2.png" width=20 height=20 title="' + GetLocationName(1, x + 1) + '">';
		} else {
			strout += '<img src="images/location2nd.png" width=20 height=20 title="' + GetLocationName(1, x + 1) + '">';
		}
	}

	return strout; 
	//'Progress: <b>' + window.top.Decks[1].LocationID + '</b> locations completed vs <b>' + window.top.Decks[2].LocationID + '</b> locations completed'
}

function GetLocationName(d, l) {
	var x = 0;
	var strout = '';
	for (x = 0; x < window.top.Decks[d].Cards.length; x++) {
		var cp = window.top.Decks[d].Cards[x];
		if (cp != null) {
			if (cp.LocationID == l && cp.CardTypeID == 1) {
				strout = cp.CardName;
				break;
			}
		}
	}

	return strout;

}


function HandleAI() {

}

function DrawLocationCard2(d, l) {
	var y = 0;
	var x = 0;
	var mw = getObj('MainBox').scrollWidth;
	var cw = 200;
	var ch = 250;
	var cs = locations;
	var cz = ch / 3;
	var l2= 2;
	var cp = null;
	var strout = '';
	for (x = 0; x < window.top.Decks[d].Cards.length; x++) {
		cp = window.top.Decks[d].Cards[x];
		if (cp != null) {
			if (cp.LocationID != l && cp.CardTypeID == 1) {
				y = cp.LocationID;
				if (y < l) {	
					//Already done
					strout += '<div onmouseover="MouseCard = ' + cp.CardID + ';  CardInfo(' + cp.CardID + ');" style="position: absolute; left: ' + 10 + ';top: ' + ((1 * cz) + (y * 30)) + '; border: 1px dashed green; width: ' + cw + 'px; height: 30; cursor: pointer; background-image: URL(' + imagepath + 'cb' + cp.CardID + ext + ')"></div>';
				} else if (y > l) {
					strout += '<div onmouseover="MouseCard = ' + cp.CardID + ';  CardInfo(' + cp.CardID + ');" style="position: absolute; left: ' + 410 + ';top: ' + ((1 * cz) + (y * 30)) + '; border: 1px dashed red; width: ' + cw + 'px; height: 30; cursor: pointer; background-image: URL(' + imagepath + 'cb' + cp.CardID + ext + ')"></div>';
				}
			}
		}
	}
	return strout;
}

function UseInterrupt(d) {
	var x = 0;
	var y = 0;
	var dp = null;
	var strout = '';
	var ct = 4;
	var d2 = (d == 1 ? 2 : 1);
	var lc = 0;
	l = window.top.Decks[d].LocationID + 1;
	for (x = 0; x < window.top.Decks[d2].Cards.length; x++) {
		var cp = window.top.Decks[d2].Cards[x];
		if (cp != null) {
			if (cp.LocationID == l && cp.CardTypeID == 1 && lc == 0) {
				lc = cp.CardID;
				break;
			}
		}
	}

	if (lc == 112 || lc == 113) {
		//Blocks interrupt
		window.top.AddChat('<div style="color: red;">The <u>' + cp.CardName + '</u> location does not allow for interrupt cards to be used.</div>');
	} else {
		for (x = 0; x < window.top.Decks[d].Cards.length; x++) {
			dp = window.top.Decks[d].Cards[x];
			if (dp != null) {	
				if (dp.CardTypeID == ct && dp.Used == 0) {
					var c = window.top.Cards[dp.CardID];
					var aok = 1;
					if (c.PowerCost > 0) {
						var HeroCard = window.top.Decks[d].HeroID;
						if (HeroCard == -1) {
							aok = 0;
						} else {
							var h = window.top.Decks[d].Cards[HeroCard];
							if (h == null) {
								aok = 0;
							} else {
								var h2 = window.top.Cards[h.CardID];
								if (h2.Stats[2].StatValue < c.PowerCost) {
									aok = 0;
								}
							}
						}
					}
					if (aok == 1) {
						strout += '<div style="float: left; width: 200; height: 250; cursor: pointer;"><img onclick="PickCardI(' + x + ', 1);" src="' + imagepath + 'cb' + dp.CardID + ext + '"></div>';
					}
				}
			}
		}
	}
	getObj('RightBox').innerHTML = '<div style="overflow: auto; width: 200px; height: 600px;">' + strout + '</div>';
}


function PickCardLocation(l, ct) {
	var x = 0;
	var y = 0;
	var dp = null;
	var strout = '';
	strout += '<div onclick="PickCard(' + l + ',-1);" style="float: left; width: 200; height: 25; cursor: pointer;">' + window.top.ASCII('Remove', 4) + '</div>';

	for (x = 0; x < window.top.Decks[MasterDeckID].Cards.length; x++) {
		dp = window.top.Decks[MasterDeckID].Cards[x];
		if (dp != null) {	
			if (dp.CardTypeID == ct && dp.LocationID == 0) {
				strout += '<div style="float: left; width: 200; height: 250; cursor: pointer;"><img onclick="PickCard(' + l + ',' + dp.CardID + ');" src="' + imagepath + 'cb' + dp.CardID + ext + '"></div>';
			}
		}
	}
	getObj('RightBox').innerHTML = '<div style="overflow: auto; width: 200px; height: 600px;">' + strout + '</div>';
}

function PickArtifactCardLocation(l, ct, x2) {
	var x = 0;
	var y = 0;
	var dp = null;
	var Artifacts = new Array();
	var AC = 0;
	var strout = '';
	strout += '<div onclick="PickCardA(' + l + ',-1);" style="float: left; width: 200; height: 25; cursor: pointer;">' + window.top.ASCII('Remove', 4) + '</div>';
	for (x = 0; x < window.top.Decks[MasterDeckID].Cards.length; x++) {
		dp = window.top.Decks[MasterDeckID].Cards[x];
		if (dp != null) {	
			if (dp.AugmentID > 0) {
				//This is an artifact card
				Artifacts[AC] = new Array();
				Artifacts[AC] = dp.AugmentID;
				AC = AC + 1;		
			}
		}
	}
	//alert(AC);
	x = 0;
	for (x = 0; x < window.top.Decks[MasterDeckID].Cards.length; x++) {
		
		dp = window.top.Decks[MasterDeckID].Cards[x];
		if (dp != null) {	
			if (dp.CardTypeID == ct && dp.CardID != x2) {
				var Match = 1;
				for (y = 0; y < AC; y++) {
					if (Artifacts[y] == dp.CardID) {
						Match = 0;
						break;
					}
				}
				if (Match == 1) {
					strout += '<div style="float: left; width: 200; height: 250; cursor: pointer;"><img onclick="PickCardA(' + l + ',' + dp.CardID + ');" src="' + imagepath + 'cb' + dp.CardID + ext + '"></div>';
				}
			}
		}
	}
	getObj('RightBox').innerHTML = '<div style="overflow: auto; width: 200px; height: 600px;">' + strout + '</div>';
}

function PickCard(l, c) {

	PlayAction(1005, l, c, 1, 0, 0);
}

function PickCardI(c, d) {
	var aok = 0;
	if (gameplaying == 1) {
		//Use an interrupt card.
		var dp = window.top.Decks[d].Cards[c];
		if (dp != null) {
			if (dp.Used == 0) {
				var h = window.top.Decks[d].HeroID;
				if (dp.CardID == 79 || dp.CardID == 131) {
					//rez
					var x = 0;
					for (x = 0; x < window.top.Decks[d].Cards.length; x++) {
						var hp = window.top.Decks[d].Cards[x];
						if (hp != null) {	
							if (hp.CardTypeID == 2 && hp.Used != 0) {
								if (h == -1) {
									window.top.Decks[d].HeroID = x;
								}
								if (dp.CardID == 79) {
									hp.Stats[1] = -(window.top.Cards[hp.CardID].Stats[1].StatValue - 1);
								} else {
									hp.Stats[1] = 0;
								}
								hp.Stats[2] = 0;
								hp.Stats[3] = 0;
								hp.Stats[4] = 0;
								hp.IStats[1] = 0;
								hp.IStats[2] = 0;
								hp.IStats[3] = 0;
								hp.IStats[4] = 0;
								AddChat(hp.CardName + ' has been returned to you.', 2);
								hp.Used = 0;
								aok = 1;
								break;
							}
						}
					}					
				} else {
					var v1 = GetCardStat(dp.CardID, 1);
					var v2 = GetCardStat(dp.CardID, 2);
					var v3 = GetCardStat(dp.CardID, 3);
					var v4 = GetCardStat(dp.CardID, 4);
					//alert(v1 + ' ' + v2 + ' ' + v3 + ' ' + v4);
					if (h != -1) {
						var hc = window.top.Decks[d].Cards[h];
						if (hc.Stats[1] < 0 && v1 > 0) {
							aok = 1;
							hc.Stats[1] = hc.Stats[1] + v1;
						}
						if (hc.Stats[2] < 0 && v2 > 0) {
							aok = 1;
							hc.Stats[2] = hc.Stats[2] + v2;
						}
						if (v3 > 0) {
							aok = 1;
							hc.IStats[3] = hc.IStats[3] + v3;
						}
						if (v4 > 0) {
							aok = 1;
							hc.IStats[4] = hc.IStats[4] + v4;
						}

					}
				}
				if (aok > 0) {
					var pc = window.top.Cards[dp.CardID].PowerCost;
					if (pc > 0 && hc != null) {
						hc.Stats[2] = hc.Stats[2] - pc;
					}
					AddFightChat('<div><font style="color: #ff66ff;">Interrupt used: ' + dp.CardName + '</font></div>', 2);
					dp.Used = 1;
					if (d == 1) {
						ResizeGame();
						getObj('RightBox').innerHTML = '';
					}
				} else {
					AddFightChat('<div><font style="color: #ff66ff;">Interrupt used: ' + dp.CardName + ' [no effect]</font></div>', 2);
				}
			}
		}

		//if (dp.CardID == )		
	}
}

function PickCardA(l, c) {
	getObj('RightBox').innerHTML = '';
	PlayAction(1006, l, c, 1, 0, 0);
}

function DrawCardLayer(x) {
	var strout = '';
	var mw = getObj('MainBox').scrollWidth;
	var cw = 200;
	var ch = 250;
	var cs = locations;
	var cz = ch / 3;

	strout = '<div onclick="PickCardLocation(' + x + ', 1);" id=layer' + x + ' style="position: absolute; left: 10;top: ' + ((50) + (x * 30)) + '; border: 1px dashed green; width: ' + cw + 'px; height: 30; cursor: pointer; z-Index: 10; background-image: URL(images/cards/card1.gif);">' + ASCII('Location ' + x + ' Empty',1, 'mingb') + '</div>';
	strout = strout  + '<div onclick="PickCardLocation(' + x + ', 3);" id=layer' + x + ' style="position: absolute; left: 215;top: ' + ((50) + (x * 30)) + '; border: 1px dashed red; width: ' + cw + 'px; height: 30; cursor: pointer;z-Index: 10; background-image: URL(images/cards/card3.gif);">' + ASCII('Monster ' + x + ' Empty',1, 'mingb') + '</div>';
	return strout;

}


function MoveCardToTop(c) {
	PlayAction(1007, -4, c, 1, 0, 0);
}


function MoveCardToBottom(c) {
	PlayAction(1007, -5, c, 1, 0, 0);
}


function DrawCard(d, c) {
	var strout = '';
	var cp = window.top.Decks[d].Cards[c];
	var mw = getObj('MainBox').scrollWidth;
	var cw = 200;
	var ch = 250;
	var cs = locations;
	var cz = ch / 3;
	var ox = 0;
	var oy = 0;

	if (cp != null) {
		var x = cp.LocationID;
		if (x != 0 && x <= locations) {
			strout = '<div onmouseover="MouseCard = ' + cp.CardID + '; CardInfo(' + cp.CardID + ');" title="' + (cp.CardTypeID == 1 ? 'Location: ' : 'Monster') + cp.CardName + '" id=layer' + x + ' style="position: absolute; left: ' + (cp.CardTypeID == 3 ? 215 : 10) + ';top: ' + ((50) + (x * 30)) + '; border: 1px dashed gold; width: ' + cw + 'px; height: 30; cursor: pointer; z-Index: 10; background-image: URL(' + imagepath + 'cb' + cp.CardID + ext + ');" onclick="PickCardLocation(' + x + ', ' + cp.CardTypeID + ');"></div>';
		} else if (x > locations) {
			
		} else {
			ox = 0
			oy = 0
			if (cp.CardTypeID == 2) {
				if (locations > 5) {
					ox = 420
					oy = -ch
				}
				var cl = FindCardPosition(d, cp.CardTypeID, cp.CardID);
				strout = '<div onmouseover="MouseCard = ' + cp.CardID + '; CardInfo(' + cp.CardID + ');" title="Hero Card: ' + cp.CardName + '" id=d-' + d + 'card' + c + ' style="position: absolute; left: ' + (ox + ((cw + 5) * cl) - cw) + ';top: ' + (oy + ((1 * ch) + 5)) + ';border: 1px dashed gold; z-Index: 10;"><img src="' + imagepath + 'cb' + cp.CardID + ext + '" width="' + cw + '" height="' + ch + '"></div>';
				if (cl > 1) {
					//Up order
					strout += '<div onmouseover="MouseCard = ' + cp.CardID + '; CardInfo(' + cp.CardID + ');" id=d-' + d + 'card' + c + ' style="position: absolute; left: ' + (ox + ((cw + 5) * cl) - cw) + ';top: ' + (oy + ((1 * ch) + 5)) + ';border: 1px dashed gold; z-Index: 100; cursor: pointer;"><button  onclick="MoveCardToTop(' + cp.CardID + ');" title="Move up order!">' + ASCII('<<', 6) + '</button></div>';
				}
				if (cl < 3) {
					//Down order
					strout += '<div onmouseover="MouseCard = ' + cp.CardID + '; CardInfo(' + cp.CardID + ');" id=d-' + d + 'card' + c + ' style="position: absolute; left: ' + (ox + ((cw + 5) * cl) - cw) + ';top: ' + (oy + ((1 * ch) + 35)) + ';border: 1px dashed gold; z-Index: 100; cursor: pointer;"><button  onclick="MoveCardToBottom(' + cp.CardID + ');" title="Move down order!">' + ASCII('>>', 6) + '</button></div>';
				}
				if (cp.AugmentID > 0) {
					strout += '<div onmouseover="MouseCard = ' + cp.AugmentID + '; CardInfo(' + cp.AugmentID + ');" id=d-' + d + 'a' + cp.AugmentID + ' style="cursor: pointer; position: absolute; left: ' + (ox + ((cw + 5) * cl) - (cw * 0.75)) + ';top: ' + (oy + ((1 * ch) + (ch))) + ';border: 1px dashed brown; z-Index: 10;width: 100px; background-image: URL(' + imagepath + 'cb' + cp.AugmentID + ext + '); background-position: -21 -33; height: 100px;" onclick="PickArtifactCardLocation(' + cp.CardID + ', 5,' + cp.AugmentID + ');"></div>';
				} else {
					strout += '<div id=d-' + d + 'a' + cp.AugmentID + ' title="Click here, and choose an Artifact card!" style="cursor: pointer; position: absolute; left: ' + (ox + ((cw + 5) * cl) - (cw * 0.75)) + ';top: ' + (oy + ((1 * ch) + (ch))) + ';border: 1px dashed brown; z-Index: 10;background-image: URL(images/cards/card5.gif); background-position: -21 -33; width: 100px; height: 100px;" onclick="PickArtifactCardLocation(' + cp.CardID + ', 5,0);">' + ASCII('Choose<br>Artifact') + '</div>';
				}
			} else if (cp.CardTypeID == 4) {
				//strout = '<div onmouseover="MouseCard = ' + cp.CardID + '; CardInfo(' + cp.CardID + ');" id=d-' + d + 'card' + c + ' style="position: absolute; left: ' + ((cw * 3) + rand(cw)) + ';top: ' + (((2 * ch))) + ';border: 1px dashed gold; z-Index: 10;"><img src="' + imagepath + 'cb' + cp.CardID + ext + '" width="' + cw + '" height="' + ch + '"></div>';
			} else if (setupphase == 0) {
				strout = '<div onmouseover="MouseCard = ' + cp.CardID + '; CardInfo(' + cp.CardID + ');" id=d-' + d + 'card' + c + ' style="position: absolute; left: ' + ((((c % 6) * cw))) + ';top: ' + ((((c / 6) * cw))) + '; z-Index: 10;"><img src="' + imagepath + 'cb' + cp.CardID + ext + '" width="' + cw + '" height="' + ch + '"></div>';
			}
		}
	}
	return strout;

}

function FindCardPosition(d, ct, c) {
	var x = 0;
	var y = 0;
	var dp = null;
	var counter = 0;

	for (x = 0; x < window.top.Decks[d].Cards.length; x++) {
		dp = window.top.Decks[d].Cards[x];
		if (dp != null) {	
			if (dp.CardTypeID == ct) {
				counter = counter + 1
				if (dp.CardID == c) {
					break;
				}
			}
		}
	}
	return counter;
}

function RedrawPlayer(c) {


}

function GetBoxSmall(Tag, color, value1, value2) {
	var b = GetPerc(value2, value1)
	var strout = PercentBoxSmall(50, b, color,' ', 3);
	return strout;
}

function ShowMapItems(m, mx, my) {

}

function ShowDefaultBar() {
	var strout = '';
	strout = '<table width=\'100%\' cellpadding=1 cellspacing=1><tr style=\'height: 22px;\' class=\'popupxtitle\'><td class=\'title\' style=\'padding-left: 0px;\' nowrap width=\'100%\'>' + ASCII('Forlorn Card Game') + '</td></tr><tr><td><div style=\'height: 340px; overflow: auto;\'>To begin your game, you must have configured a full deck of ' + locations + ' locations, ' + locations + ' monsters.<br><br>Once that is complete you will be able to press the s key/s command button to start a Trumps game or the t key/t command button to start a Battle game.</div></td></tr></table>';
	getObj('RightBox').innerHTML = strout;
	getObj('MainBox').focus();
}

function popupwindow(url, message, pb, defaultvalue, title, icon, style, pwwidth, pwheight) {
	if (title == undefined) {
		title = '';
	}
	if (icon == undefined) {
		icon = '';
	}
	if (style == undefined) {
		style = '';
	}
	if (pwwidth == undefined) {
		pwwidth = 350
	}
	if (pwheight == undefined) {
		pwheight = 250
	}

	window.top.showPopWin(url + "message=" + message + "&defaultvalue=" + defaultvalue + "&title=" + title + "&icon=" + icon + "&style=" + style, pwwidth, pwheight, GameReturn, null, title, pb);
}

function PromptReturn(returnVal, postback) {
	var v = LastClickedItem;
	if (returnVal != null) {
		if (returnVal != false) {
			if (postback == 1) {
				sendRequest('load_ajax.asp?A=5000&P' + ShipPage);
			}
		}
	}
}


function confirm(message, pb, title, icon, c1, c2) {
	if (title == undefined) {
		title = '';
	}
	if (icon == undefined) {
		icon = '';
	}
	if (c1 == undefined) {
		c1 = 'OK';
	}
	if (c2 == undefined) {
		c2 = 'Cancel';
	}
	window.top.showPopWin("confirm.asp?message=" + message + "&title=" + title + "&c1=" + c1 + "&c2=" + c2 + "&icon=" + icon, 350, 130, PromptReturn, null, title, pb);
}

function buygp() {

	var message = '';
	var pb = 1;
	var defaultvalue = 25000;
	var title = 'Buy Game Points';
	var icon = '';
	var style = '';

	window.top.showPopWin("buypopup.asp?message=" + message + "&defaultvalue=" + defaultvalue + "&title=" + title + "&icon=" + icon + "&style=" + style, 400, 420, PromptReturn, null, title, pb);
}     

function prompt(message, pb, defaultvalue, title, icon, style) {
	if (title == undefined) {
		title = '';
	}
	if (icon == undefined) {
		icon = '';
	}
	if (style == undefined) {
		style = '';
	}

	window.top.showPopWin("prompt.asp?message=" + message + "&defaultvalue=" + defaultvalue + "&title=" + title + "&icon=" + icon + "&style=" + style, 350, 130, PromptReturn, null, title, pb);
}     

function GetMoney(moneyin, xx) {
	return (xx == null ? '<img src=\'images/gold.png\' title=\'' + moneyin + ' Credits\'>' : '') + (moneyin == null ? '' : '' + Math.abs(moneyin) + '<b>cr</b>')
}

function ClearChat() {
	chat = new Array();
}

function AddFightChat(text, chattype, pmid) {
	if (ruleset == 3) {
		//Need to send this fight information to the other player ... do this by setting a hidden field. Oh naughty
		if (getObj('fightchat') != null) {
			getObj('fightchat').value = '' + text +  getObj('fightchat').value ;
		}
		
	}
	AddChat(text, chattype, pmid);
}

function AddChat(text, chattype, pmid) {
	if (chattype == null) {
		chattype = 0;
	}
	if (text.substr(0, 5) != '<div>') {
		text = '<div>' + text + '</div>';
	}
	chat[chat.length] = new UpdateChat(text, chattype, pmid);
	if (chattype != 0) {
		ShowChat();
	}
}


function MakeBox(t) {
	var s = t;
	s = '<div id="boxx_solid">' + t + '</div>';
	return s;
}

function SetChat(text) {
	chat = new Array();
}

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;
	if (c == null) {
		c = 1;
	}
	var strout = '';
	var pms = new Array();
	for (x = chat.length - 1; x >= 0; x=x-1) {
		if (roomtoggle[chat[x].chattype] > 0 || c == 0) {
			if (chat[x].chattype == 3) {
				yok = -1;
				if (getObj('p' + chat[x].pm) != null) {
					if (pms.length > 0) {
						for (y = 0; y < pms.length; y++) {
							if (pms[y].pm == chat[x].pm) {
								yok = y;
							}
						}
					}
					if (yok == -1) {
						pms[pms.length] = new Array();
						pms[pms.length] = new UpdatePMS(chat[x].chat,chat[x].pm)
					} else {
						pms[yok].chat += chat[x].chat
					}
				}
			}
			strout += chat[x].chat;
			match = match + 1;
		}
		if (match >= 15) {
			break;
		}
	}

	if (pms.length > 0) {
		for (y = 0; y < pms.length; y++) {
			if (getObj('p' + pms[y].pm) != null) {
				getObj('p' + pms[y].pm).innerHTML = pms[y].chat;
			}
		}
	}

	if (getObj('pchat') != null) {
		getObj('pchat').innerHTML = '<div style="width: 100%; height: 100%;">' + (strout == '' ? ASCII('Move your mouse over this box or the tab to the left so you may see it more clearly.', 5) : strout) + '</div>';
	}
	if (getObj('roombox') != null) {
		getObj('roombox').innerHTML = GetRooms();
	}

	//getObj('chatboxx').innerHTML = strout;
}

function GetRooms() {
	var strout = '';
	var x = 0;
	for (x = 0; x < rooms.length; x++) {
		strout += '<div title="' + rooms[x] + '" style="margin: 1px; float: left; width: 18px; height: 18px; background-image: URL(images/' + roompics[x] + '); background-repeat: no-repeat;' + (roomtoggle[x] == 0 ? 'background-color: RGB(255, 128, 128);' : 'background-color: RGB(128, 255, 128);') + ' cursor: pointer;" onclick="ToggleRoom(\'' + x + '\');"></div>';
	}
	
	return strout;
}

function ToggleRoom(r) {
	if (roomtoggle[r] > 0) {
		roomtoggle[r] = 0;
	} else {
		roomtoggle[r] = 1;
	}
	ShowChat();
}


function SwitchRegion(r) {
	SetName();
	getObj('MainBox').innerHTML = DrawDeck(r);
}

function SetName() {
	if (setupphase == 1) {
		document.title = 'FCCG - Game Setup';
		//KeyScreen();
	} else {
		document.title = 'FCCG - ' + (ruleset == 0 ? 'Trumps' : (ruleset == 3 ? 'PvP Battle' : (ruleset == 2 ? 'Hard Battle' : (ruleset == 5 ? 'Epic' : (ruleset == 6 ? 'Hardcore Battle' : 'Battle'))))) + ' Game In Progress';
	}
}

function ResizeGame() {
	InfoScreen();
	clearTimeout(RedrawCards);
	if (window.top.Decks != null) {
		if (window.top.Decks.length > 0) {
			setTimeout("getObj('MainBox').innerHTML = DrawDeck(' + MasterDeckID + ');", 150);
			
		}
	}
	return true;
}

function formatCurrency(num) {
var sign = '';
var i = 0;
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
num = Math.floor(num/100).toString();

for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '' + num);
}

function CacheImage(imgpath) {
	if (caching == 1) {
		var image1 = new Image();
		image1.src = imgpath;
	}
}

function Spacer(divx) {
	if (window.top.getObj(divx) == null) {
		clearInterval(ButtonTimer);
	} else {
		window.top.getObj(divx).innerHTML = '' + PercentBoxSmall(200, ButtonInterval * 10, 'green', ASCII((ButtonInterval * 10) + '%')) + '';	
		ButtonInterval = ButtonInterval + 1;	
	}
}

function AcceptBattle(a) {
	PlayAction(2001, 2, a, 1, 0, '');
}

function RefuseBattle(a) {
	PlayAction(2001, 3, a, 1, 0, '');
}

function CacheCards() {
	var powers = new Array();
	var js = getObj('cardfile');
	if (js != null) {
		js.parentNode.removeChild(js);
	}

	var headID = document.getElementsByTagName("head")[0];
	var oScript = document.createElement("script");
	oScript.type = 'text/javascript';
	oScript.src = 'js/cards.js';
	oScript.id = 'cardfile';
	headID.appendChild(oScript);
}

