var butcatrequest = createRequest();
var contentrequest = createRequest();
var itemdetailrequest = createRequest();
var cartrequest = createRequest();
var popuprequest = createRequest();
var searchinterfacerequest = createRequest();
var sessionrequest = createRequest();
var detailallowed = true;
var popupactive = false;
var ajaxurl = "index.php?option=com_outpostajax&format=raw";
var contextid = 9999;
var currentdetailid = null;
var filters;
var buttonfilters;
var persistenterror = false;
var initializing = false;
var sortbuffer = null;

function createRequest()
{
	var req = false;
	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
		if (req.overrideMimeType)
		{
			req.overrideMimeType("text/xml");
		}
	}
	else if (window.ActiveXObject)
	{
		try
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
			}
		}
	}
	
	return req;
}

function initializeLayout(specificitemid)
{
	cleanup();
	
	showPane("init");
	
	document.getElementById("searchheadcaption").innerHTML = "Search";
	document.getElementById("collectionsheadcaption").innerHTML = "Sets";
	document.getElementById("listsheadcaption").innerHTML = "Product list";
	document.getElementById("cartheadcaption").innerHTML = "Shopping cart";
	
	modifyCart(0,"none");
	
	initializing = true;
	
	if (specificitemid != 0)
	{
		persistenterror = true;
		getSpecificItem(specificitemid);
	}
	
	searchinterfacerequest.open("POST",ajaxurl,true);
	searchinterfacerequest.onreadystatechange = processSearchInterface;
	searchinterfacerequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	searchinterfacerequest.send("ajaxmode=searchinterface&contextid=0");
}

function getSpecificItem(specificitemid)
{
	document.getElementById("listsbody").innerHTML = "";
	document.getElementById("filterbody").innerHTML = "";
	
	contentrequest.open("POST",ajaxurl,true);
	contentrequest.onreadystatechange = processContentResponse;
	contentrequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	contentrequest.send("ajaxmode=specificitem&itemid=" + specificitemid);
	
	showPane("lists");
}

function findSomething(currentcontextid)
{
	cleanup();
	
	document.getElementById("listsheadinfo").innerHTML = " - Loading...";
	
	var searchword = document.getElementById("cardsearchfield").value;
	if (searchword.length < 2)
	{
		return;
	}
	var fullsearchnode = document.getElementById("limitsearchbox");
	if (fullsearchnode)
	{
		var fullsearch = fullsearchnode.checked ? "1" : "0";
	}
	else
	{
		var fullsearch = "0";
	}
	
	document.getElementById("listsbody").innerHTML = "";
	document.getElementById("filterbody").innerHTML = "";
	
	contentrequest.open("POST",ajaxurl,true);
	contentrequest.onreadystatechange = processContentResponse;
	contentrequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	contentrequest.send("ajaxmode=search&fullsearch=" + fullsearch + "&contextid=" + currentcontextid + "&keyword=" + searchword);
	
	if (currentcontextid == 9999)
	{
		clearGenericSearch();
	}
	else
	{
		clearSearch();
	}
	
	showPane("lists");
}

function getDetails(rootbuttonid,menubuttonid)
{
	cleanup();
	
	document.getElementById("listsheadinfo").innerHTML = " - Loading...";
	document.getElementById("listsbody").innerHTML = "";
	document.getElementById("filterbody").innerHTML = "";
	
	contentrequest.open("POST",ajaxurl,true);
	contentrequest.onreadystatechange = processContentResponse;
	contentrequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	contentrequest.send("ajaxmode=content&menubuttonid=" + menubuttonid + "&rootbuttonid=" + rootbuttonid);
	
	showPane("lists");
}

function processContentResponse()
{
	if (contentrequest.readyState == 4)
	{
		var displayerror = "";
		document.getElementById("listsheadinfo").innerHTML = "";
		
		if (contentrequest.status == 200)
		{
			try
			{
				var xmlobj = new ActiveXObject("Microsoft.XMLDOM");
				xmlobj.async = false;
				xmlobj.loadXML(contentrequest.responseText);
			}
			catch (error)
			{
				var xmlobj = contentrequest.responseXML;
			}
			var errornode = xmlobj.getElementsByTagName("errorcode")[0];
			if (errornode)
			{
				var err = errornode.firstChild.nodeValue;
				if (err == 0)
				{
					var answerheadcaption = xmlobj.getElementsByTagName("ajaxanswerheadcaption")[0];
					if (answerheadcaption)
					{
						var htmlhead = answerheadcaption.firstChild.nodeValue;
						document.getElementById("listsheadcaption").innerHTML = htmlhead;
					}
					var answerheadinfo = xmlobj.getElementsByTagName("ajaxanswerheadinfo")[0];
					if (answerheadinfo)
					{
						var htmlheadinfo = answerheadinfo.firstChild.nodeValue;
						document.getElementById("listsheadinfo").innerHTML = " (" + htmlheadinfo + ")";
					}
					var answerbody = xmlobj.getElementsByTagName("ajaxanswerbody")[0];
					if (answerbody)
					{
						var htmlbody = answerbody.firstChild.nodeValue;
						document.getElementById("listsbody").innerHTML = htmlbody;
						filters = null;
						filters = new Array();
						sortName(true);
					}
					var filternode = xmlobj.getElementsByTagName("ajaxanswerfilters")[0];
					if (filternode)
					{
						var filtertext = filternode.firstChild.nodeValue;
						document.getElementById("filtercaption").innerHTML = "Filters : ";
						document.getElementById("filterbody").innerHTML = filtertext;
						document.getElementById("listfilters").style.display = "block";
					}
					var alphafilternode = xmlobj.getElementsByTagName("ajaxansweralphabetfilter")[0];
					if (alphafilternode)
					{
						var alphafiltertext = alphafilternode.firstChild.nodeValue;
						document.getElementById("listsheadfilter").innerHTML = alphafiltertext;
					}
				}
				else
				{
					var errortextnode = xmlobj.getElementsByTagName("errormessage")[0];
					if (errortextnode)
					{
						displayerror = errortextnode.firstChild.nodeValue;
					}
					else
					{
						displayerror = "An error has occurred";
					}
				}
			}
		}
		
		if (displayerror != "")
		{
			errorDisplay(displayerror);
		}
	}
}

function getCats(butid)
{
	var magiclegal = "Magic the Gathering is TM and copyright Wizards of the Coast, Inc, a subsidiary of Hasbro, Inc. All rights reserved.";
	var wowlegal = "Warcraft, World of Warcraft and Blizzard Entertainment are trademarks and/or registered trademarks of Blizzard Entertainment, Inc.";
	var yugiohlegal = "Yu-Gi-Oh! is a trademark of Kazuki Takahashi, Shueisha, Konami Japan, etc. ©1996-2010. All rights reserved. ";
	var boardlegal = "Welcome to Outpost";
	
	var legaltekst = "";
	
	document.getElementById("collectionsbody").innerHTML = "";
	document.getElementById("searchbody").innerHTML = "";
	
	switch (butid)
	{
		case 1:
			legaltekst = magiclegal;
			break;
		case 2:
			legaltekst = wowlegal;
			break;
		case 3:
			legaltekst = yugiohlegal;
			break;
		case 4:
			legaltekst = magiclegal;
			break;
		case 5:
			legaltekst = boardlegal;
			break;
	}
	
	contextid = butid;
	
	document.getElementById("legal").innerHTML = legaltekst;
	
	butcatrequest.open("POST",ajaxurl,true);
	butcatrequest.onreadystatechange = processButCat;
	butcatrequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	butcatrequest.send("ajaxmode=settable&contextid=" + butid);
	
	searchinterfacerequest.open("POST",ajaxurl,true);
	searchinterfacerequest.onreadystatechange = processSearchInterface;
	searchinterfacerequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	searchinterfacerequest.send("ajaxmode=searchinterface&contextid=" + butid);
	
	showPane("collections");
}

function processButCat()
{
	if (butcatrequest.readyState == 4)
	{
		var displayerror = "";
		
		if (butcatrequest.status == 200)
		{
			try
			{
				var xmlobj = new ActiveXObject("Microsoft.XMLDOM");
				xmlobj.async = false;
				xmlobj.loadXML(butcatrequest.responseText);
			}
			catch (error)
			{
				var xmlobj = butcatrequest.responseXML;
				
			}
			var errornode = xmlobj.getElementsByTagName("errorcode");
			if (errornode)
			{
				var err = errornode[0].firstChild.nodeValue;
				if (err == 0)
				{
					var answerheadcaption = xmlobj.getElementsByTagName("ajaxanswerheadcaption")[0];
					if (answerheadcaption)
					{
						var headhtml =  answerheadcaption.firstChild.nodeValue;
						document.getElementById("collectionsheadcaption").innerHTML = headhtml;
					}
					var answerheadinfo = xmlobj.getElementsByTagName("ajaxanswerheadinfo")[0];
					if (answerheadinfo)
					{
						var headhtmlinfo = answerheadinfo.firstChild.nodeValue;
						document.getElementById("collectionsheadinfo").innerHTML = " (" + headhtmlinfo + ")";
					}
					var answerbody = xmlobj.getElementsByTagName("ajaxanswerbody")[0];
					if (answerbody)
					{
						var bodyhtml = answerbody.firstChild.nodeValue;
						document.getElementById("collectionsbody").innerHTML = bodyhtml;
					}
					var answerfilters = xmlobj.getElementsByTagName("ajaxansweralphafilter")[0];
					if (answerfilters)
					{
						var filtertext = answerfilters.firstChild.nodeValue;
						document.getElementById("collectionsheadfilter").innerHTML = filtertext;
						buttonfilters = null;
						buttonfilters = new Array();
					}
				}
				else
				{
					var errortextnode = xmlobj.getElementsByTagName("errormessage")[0];
					if (errortextnode)
					{
						displayerror = errortextnode.firstChild.nodeValue;
					}
					else
					{
						displayerror = "An error has occurred";
					}
				}
			}
		}
		
		if (displayerror != "")
		{
			errorDisplay(displayerror);
		}
	}
}

function processSearchInterface()
{
	if (searchinterfacerequest.readyState == 4)
	{
		var displayerror = "";
		
		if (searchinterfacerequest.status == 200)
		{
			try
			{
				var xmlobj = new ActiveXObject("Microsoft.XMLDOM");
				xmlobj.async = false;
				xmlobj.loadXML(searchinterfacerequest.responseText);
			}
			catch (error)
			{
				var xmlobj = searchinterfacerequest.responseXML;
				
			}
			var errornode = xmlobj.getElementsByTagName("errorcode");
			if (errornode)
			{
				var err = errornode[0].firstChild.nodeValue;
				if (err == 0)
				{
					var answerheadcaption = xmlobj.getElementsByTagName("ajaxanswerheadcaption")[0];
					if (answerheadcaption)
					{
						var headhtml =  answerheadcaption.firstChild.nodeValue;
						document.getElementById("searchheadcaption").innerHTML = headhtml;
					}
					var answerheadinfo = xmlobj.getElementsByTagName("ajaxanswerheadinfo")[0];
					if (answerheadinfo)
					{
						var headinfohtml = answerheadinfo.firstChild.nodeValue;
						document.getElementById("searchheadinfo").innerHTML = headinfohtml;
					}
					var answerbody = xmlobj.getElementsByTagName("ajaxanswerbody")[0];
					if (answerbody)
					{
						var bodyhtml = answerbody.firstChild.nodeValue;
						document.getElementById("searchbody").innerHTML = bodyhtml;
					}
				}
				else
				{
					var errortextnode = xmlobj.getElementsByTagName("errormessage")[0];
					if (errortextnode)
					{
						displayerror = errortextnode.firstChild.nodeValue;
					}
					else
					{
						displayerror = "An error has occurred";
					}
				}
			}
		}
		
		if (displayerror != "")
		{
			errorDisplay(displayerror);
		}
	}
}

function cartImageChange(element,baseimage)
{
	element.src = "images/outpostimages/" + baseimage + ".png";
}

function addToCart(itemid)
{
	modifyCart(itemid,"create");
}
function deleteItem(itemid)
{
	modifyCart(itemid,"delete");
}

//modifiers
//"plus" = increment number of items
//"min" = decrement number of items
//"create" = add item to card
//"delete" = delete item from cart
//"none" = no modifications, only reloads cart
function modifyCart(itemid,modifier)
{
	if (modifier == "create")
	{
		var aantal = document.getElementById("productamount" + itemid).value;
		if (!aantal)
		{
			aantal = 0;
		}
	}
	else
	{
		var aantal = 1;
	}
	
	cartrequest.open("POST",ajaxurl,true);
	cartrequest.onreadystatechange = processCart;
	cartrequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	cartrequest.send("ajaxmode=cart&itemid=" + itemid + "&modifier=" + modifier + "&aantal=" + aantal);
}

function processCart()
{
	if (cartrequest.readyState == 4)
	{
		var displayerror = "";
		
		if (cartrequest.status == 200)
		{
			try
			{
				var xmlobj = new ActiveXObject("Microsoft.XMLDOM");
				xmlobj.async = false;
				xmlobj.loadXML(cartrequest.responseText);
			}
			catch (error)
			{
				var xmlobj = cartrequest.responseXML;
				
			}
			
			var errornode = xmlobj.getElementsByTagName("errorcode")[0];
			if (errornode)
			{
				var err = errornode.firstChild.nodeValue;
				if (err == 0)
				{
					var answertab = xmlobj.getElementsByTagName("ajaxanswertabinfo")[0];
					if (answertab)
					{
						var tabinfotext = answertab.firstChild.nodeValue;
						document.getElementById("tabcart").innerHTML = "Cart" + tabinfotext;
					}
					var answerheadcaption = xmlobj.getElementsByTagName("ajaxanswerheadcaption")[0];
					if (answerheadcaption)
					{
						var htmlhead =  answerheadcaption.firstChild.nodeValue;
						document.getElementById("cartheadcaption").innerHTML = htmlhead;
					}
					var answerheadinfo = xmlobj.getElementsByTagName("ajaxanswerheadinfo")[0];
					if (answerheadinfo)
					{
						var htmlheadinfo = answerheadinfo.firstChild.nodeValue;
						document.getElementById("cartheadinfo").innerHTML = htmlheadinfo;
					}
					var answerbody = xmlobj.getElementsByTagName("ajaxanswerbody")[0];
					if (answerbody)
					{
						var htmlbody = answerbody.firstChild.nodeValue;
						document.getElementById("cartbody").innerHTML = htmlbody;
					}
					
					if (initializing)
					{
						initializing = false;
						if (htmlbody != "")
						{
							showPane('cart');
						}
					}
				}
				else
				{
					var errortextnode = xmlobj.getElementsByTagName("errormessage")[0];
					if (errortextnode)
					{
						displayerror = errortextnode.firstChild.nodeValue;
					}
					else
					{
						displayerror = "An error has occurred";
					}
				}
			}
			else
			{
				displayerror = "The server response was inappropriate";
			}
			
			var notificationnode = xmlobj.getElementsByTagName("notification")[0];
			if (notificationnode)
			{
				notification = notificationnode.firstChild.nodeValue;
			}
		}
		
		if (displayerror != "")
		{
			errorDisplay(displayerror);
		}
	}
}

function showPopUp(itemid)
{
	if (currentdetailid == itemid)
	{
		return;
	}
	popupactive = true;
	
	popuprequest.open("POST",ajaxurl,true);
	popuprequest.onreadystatechange = displayPopUp;
	popuprequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	popuprequest.send("ajaxmode=popup&itemid=" + itemid);
}

function displayPopUp()
{
	if (popuprequest.readyState == 4)
	{
		if (popuprequest.status == 200)
		{
			if (popupactive)
			{
				try
				{
					var xmlobj = new ActiveXObject("Microsoft.XMLDOM");
					xmlobj.async = false;
					xmlobj.loadXML(popuprequest.responseText);
				}
				catch (error)
				{
					var xmlobj = popuprequest.responseXML;
					
				}
				
				var errornode = xmlobj.getElementsByTagName("errorcode")[0];
				if (errornode)
				{
					var err = errornode.firstChild.nodeValue;
					if (err == 0)
					{
						var answerbody =  xmlobj.getElementsByTagName("ajaxanswerbody")[0];
						if (answerbody)
						{
							var htmlbody = answerbody.firstChild.nodeValue;
							overlib(htmlbody,FGCOLOR,'#D6D4AE',HAUTO,VAUTO,BGCOLOR,'#000000');
						}
					}
				}
			}
		}
	}
}

function removePopUp()
{
	popupactive = false;
	nd();
}

function findPosX(element)
{
	var curleft = 0;
	while (element.offsetParent)
	{
		curleft += element.offsetLeft;
		element = element.offsetParent;
	}
	if (curleft == 0)
	{
		curleft += element.x;
	}
	
	return curleft;
}

function findPosY(element)
{
	var curtop = 0;
	while (element.offsetParent)
	{
		curtop += element.offsetTop;
		element = element.offsetParent;
	}
	if (curtop == 0)
	{
		curtop += element.y
	}
	
	return curtop;
}

function errorDisplay(errortext)
{
	var attachpoint = document.getElementById("carttop");
	var x = 28;
	var y = attachpoint.offsetTop + attachpoint.offsetHeight;
	var errordetails = document.getElementById("errordetails");
	document.getElementById("errordetailview").innerHTML = errortext;
	errordetails.style.position = "absolute";
	errordetails.style.width = "668px";
	errordetails.style.left = x + "px";
	errordetails.style.top = y + "px";
	errordetails.style.display = "block";
}

function removeItemDetails()
{
	currentdetailid = null;
	document.getElementById("itemdetails").style.display = "none";
	document.getElementById("itemdetailview").innerHTML = "";
}

function removeErrorDetails()
{
	document.getElementById("errordetails").style.display = "none";
	document.getElementById("errordetailview").innerHTML = "";
}

function cleanup()
{
	removeItemDetails();
	if (!persistenterror)
	{
		removeErrorDetails();
	}
	else
	{
		persistenterror = true;
	}
}

function notLoggedIn()
{
	toggleLoginOn();
	errorDisplay("You are not logged in, you must be logged in to checkout. Please feel free to register.");
}

/*Display detail from cart item in proper position*/
function getCartItemDetails(cardid)
{
	cleanup();
	
	if (!detailallowed)
	{
		return;
	}
	
	currentdetailid = cardid;
	
	removePopUp();
	
	itemdetailrequest.open("POST",ajaxurl,true);
	itemdetailrequest.onreadystatechange = processCartItemDetails;
	itemdetailrequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	itemdetailrequest.send("ajaxmode=itemdetail&cardid=" + cardid);
}

function processCartItemDetails()
{
	if (itemdetailrequest.readyState == 4)
	{
		if (itemdetailrequest.status == 200)
		{
			try
			{
				var xmlobj = new ActiveXObject("Microsoft.XMLDOM");
				xmlobj.async = false;
				xmlobj.loadXML(itemdetailrequest.responseText);
			}
			catch (error)
			{
				var xmlobj = itemdetailrequest.responseXML;
			}
			
			var errornode = xmlobj.getElementsByTagName("errorcode")[0];
			if (errornode)
			{
				var err = errornode.firstChild.nodeValue;
				if (err == 0)
				{
					var cardinformationbuffer = null;
					var cardid = null;
					var answerbody = xmlobj.getElementsByTagName("ajaxanswerbody")[0];
					if (answerbody)
					{
						cardinformationbuffer =  answerbody.firstChild.nodeValue;
					}
					var answercardid = xmlobj.getElementsByTagName("ajaxanswercardid")[0];
					if (answercardid)
					{
						cardid = answercardid.firstChild.nodeValue;
					}
					if (cardid && cardinformationbuffer)
					{
						cartDilate(cardid,cardinformationbuffer);
					}
				}
			}
		}
	}
}

function cartDilate(cardid,cardinformationbuffer)
{
	var attachpoint = document.getElementById("cartdetailhyperlink" + cardid);
	var x = 28;
	var y = attachpoint.offsetTop + attachpoint.offsetHeight + 3;
	var itemdetails = document.getElementById("itemdetails");
	document.getElementById("itemdetailview").innerHTML = cardinformationbuffer;
	itemdetails.style.position = "absolute";
	itemdetails.style.width = "668px";
	itemdetails.style.left = x + "px";
	itemdetails.style.top = y + "px";
	itemdetails.style.display = "block";
}

function getItemDetails(cardid)
{
	cleanup();
	
	if (!detailallowed)
	{
		return;
	}
	
	currentdetailid = cardid;
	
	removePopUp();
	
	itemdetailrequest.open("POST",ajaxurl,true);
	itemdetailrequest.onreadystatechange = processItemDetails;
	itemdetailrequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	itemdetailrequest.send("ajaxmode=itemdetail&cardid=" + cardid);
}

function processItemDetails()
{
	if (itemdetailrequest.readyState == 4)
	{
		if (itemdetailrequest.status == 200)
		{
			try
			{
				var xmlobj = new ActiveXObject("Microsoft.XMLDOM");
				xmlobj.async = false;
				xmlobj.loadXML(itemdetailrequest.responseText);
			}
			catch (error)
			{
				var xmlobj = itemdetailrequest.responseXML;
			}
			
			var errornode = xmlobj.getElementsByTagName("errorcode")[0];
			if (errornode)
			{
				var err = errornode.firstChild.nodeValue;
				if (err == 0)
				{
					var cardinformationbuffer = null;
					var cardid = null;
					var answerbody = xmlobj.getElementsByTagName("ajaxanswerbody")[0];
					if (answerbody)
					{
						cardinformationbuffer =  answerbody.firstChild.nodeValue;
					}
					var answercardid = xmlobj.getElementsByTagName("ajaxanswercardid")[0];
					if (answercardid)
					{
						cardid = answercardid.firstChild.nodeValue;
					}
					if (cardid && cardinformationbuffer)
					{
						dilate(cardid,cardinformationbuffer);
					}
				}
			}
		}
	}
}

function dilate(cardid,cardinformationbuffer)
{
	var attachpoint = document.getElementById(cardid);
	var x = 28;
	var y = attachpoint.offsetTop + attachpoint.offsetHeight;
	var itemdetails = document.getElementById("itemdetails");
	document.getElementById("itemdetailview").innerHTML = cardinformationbuffer;
	itemdetails.style.position = "absolute";
	itemdetails.style.width = "668px";
	itemdetails.style.left = x + "px";
	itemdetails.style.top = y + "px";
	itemdetails.style.display = "block";
}

function increaseItem(itemid)
{
	var plusbut = document.getElementById("plusbut" + itemid);
	if (plusbut)
	{
		plusbut.disabled = true;
		plusbut.style.backgroundImage = "url('images/outpostimages/plusimagehover.png')";
	}
	var minbut = document.getElementById("minbut" + itemid);
	if (minbut)
	{
		minbut.disabled = true;
	}
	modifyCart(itemid,"plus");
}

function reduceItem(itemid)
{
	var plusbut = document.getElementById("plusbut" + itemid);
	if (plusbut)
	{
		plusbut.disabled = true;
	}
	var minbut = document.getElementById("minbut" + itemid);
	if (minbut)
	{
		minbut.disabled = true;
		minbut.style.backgroundImage = "url('images/outpostimages/minimagehover.png')";
	}
	modifyCart(itemid,"min");
}

function clearSearch()
{
	var searchfield = document.getElementById("cardsearchfield");
	if (searchfield)
	{
		searchfield.value = "";
	}
	
	opUncheck("limitsearchbox");
}

function clearGenericSearch()
{
	var searchfield = document.getElementById("cardsearchfield");
	if (searchfield)
	{
		searchfield.value = "";
	}
	
	opCheck("limitsearchbox");
}

/*
MAGIC SEARCH FUNCTIONS
*/
function buttonpop(text)
{
	overlib(text,WIDTH,80,FGCOLOR,"#FFFFAA",BGCOLOR,"#000000");
} 

function findMagic()
{
	cleanup();
	
	document.getElementById("listsheadinfo").innerHTML = " - Loading ...";
	
	var searchword = document.getElementById("cardsearchfield").value;
	
	var fullsearch = (document.getElementById("limitsearchbox").checked) ? "1" : "0";
	
	var common = (document.getElementById("commonbox").checked) ? "1" : "0";
	var uncommon = (document.getElementById("uncommonbox").checked) ? "1" : "0";
	var rare = (document.getElementById("rarebox").checked) ? "1" : "0";
	var mythic = (document.getElementById("mythicbox").checked) ? "1" : "0";
	
	var iswhite = (document.getElementById("orangebox").checked) ? "1" : "0";
	var isblue = (document.getElementById("bluebox").checked) ? "1" : "0";
	var isblack = (document.getElementById("blackbox").checked) ? "1" : "0";
	var isred = (document.getElementById("redbox").checked) ? "1" : "0";
	var isgreen = (document.getElementById("greenbox").checked) ? "1" : "0";
	var isnocol = (document.getElementById("nocolbox").checked) ? "1" : "0";
	
	var island = (document.getElementById("landbox").checked) ? "1" : "0";
	var iscreature = (document.getElementById("creaturebox").checked) ? "1" : "0";
	var issorcery = (document.getElementById("sorcerybox").checked) ? "1" : "0";
	var isinstant = (document.getElementById("instantbox").checked) ? "1" : "0";
	var isenchantment = (document.getElementById("enchantbox").checked) ? "1" : "0";
	var isartifact = (document.getElementById("artifactbox").checked) ? "1" : "0";
	var isplaneswalker = (document.getElementById("planeswalkerbox").checked) ? "1" : "0";
	
	var iswithfoils = (document.getElementById("foilonbox").checked) ? "1" : "0";
	var iswithnonfoils = (document.getElementById("nonfoilonbox").checked) ? "1" : "0";
	
	var setselector = document.getElementById("setselection");
	var setid = setselector.options[setselector.selectedIndex].value;
	
	var cmcselector = document.getElementById("relationsign");
	var cmcrel = cmcselector.options[cmcselector.selectedIndex].value;
	
	var sortselector = document.getElementById("sortingcriterium");
	var sortkey = sortselector.options[sortselector.selectedIndex].value;
	
	var supertype = document.getElementById("cardsupertype").value;
	
	var subtype = document.getElementById("cardsubtype").value;
	
	var artist = document.getElementById("cardartist").value;
	
	var cmcval = document.getElementById("relationnumber").value;
	if (!cmcval)
	{
		cmcval = 0;
	}
	
	var sendstring = "ajaxmode=search&contextid=1&keyword=" + searchword + "&sortkey=" + sortkey + "&cw=" + iswhite + "&cu=" + isblue + "&cg=" + isgreen + "&cr=" + isred;
	sendstring += "&cb=" + isblack + "&cnocol=" + isnocol + "&tland=" + island + "&tcreature=" + iscreature + "&tsorcery=" + issorcery + "&tinstant=" + isinstant + "&tenchantment=";
	sendstring += isenchantment + "&tartifact=" + isartifact + "&tplaneswalker=" + isplaneswalker + "&common=" + common + "&uncommon=" + uncommon + "&rare=" + rare + "&mythic=" + mythic;
	sendstring += "&setid=" + setid + "&cmcrel=" + cmcrel + "&cmcval=" + cmcval + "&supertype=" + supertype + "&subtype=" + subtype + "&artist=" + artist;
	sendstring += "&foilon=" + iswithfoils + "&nonfoilon=" + iswithnonfoils + "&fullsearch=" + fullsearch;
	
	clearMagicSearch();
	
	document.getElementById("listsbody").innerHTML = "";
	document.getElementById("filterbody").innerHTML = "";
	
	contentrequest.open("POST",ajaxurl,true);
	contentrequest.onreadystatechange = processContentResponse;
	contentrequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	contentrequest.send(sendstring);
	

	showPane("lists");
}

function resetRarities()
{
	opCheck("commonbox");
	opCheck("uncommonbox");
	opCheck("rarebox");
	opCheck("mythicbox");
}

function disableRarities()
{
	opUncheck("commonbox");
	opUncheck("uncommonbox");
	opUncheck("rarebox");
	opUncheck("mythicbox");
}

function resetColors()
{
	opCheck("orangebox");
	opCheck("bluebox");
	opCheck("blackbox");
	opCheck("redbox");
	opCheck("greenbox");
	opCheck("nocolbox");
}

function disableColors()
{
	opUncheck("orangebox");
	opUncheck("bluebox");
	opUncheck("blackbox");
	opUncheck("redbox");
	opUncheck("greenbox");
	opUncheck("nocolbox");
}

function resetTypes()
{
	opCheck("landbox");
	opCheck("creaturebox");
	opCheck("sorcerybox");
	opCheck("instantbox");
	opCheck("enchantbox");
	opCheck("artifactbox");
	opCheck("planeswalkerbox");
}

function disableTypes()
{
	opUncheck("landbox");
	opUncheck("creaturebox");
	opUncheck("sorcerybox");
	opUncheck("instantbox");
	opUncheck("enchantbox");
	opUncheck("artifactbox");
	opUncheck("planeswalkerbox");
}

function clearMagicSearch()
{
	document.getElementById("cardsearchfield").value = "";
	document.getElementById("cardsupertype").value = "";
	document.getElementById("cardsubtype").value = "";
	document.getElementById("cardartist").value = "";
	document.getElementById("relationnumber").value = "";
	
	document.getElementById("foilonbox").checked = true;
	document.getElementById("nonfoilonbox").checked = true;
	
	resetRarities();
	resetColors();
	resetTypes();
	
	opUncheck("limitsearchbox");
	opCheck("foilonbox");
	opCheck("nonfoilonbox");
	
	var setselector = document.getElementById("setselection");
	setselector.selectedIndex = 0;
	
	var cmcselector = document.getElementById("relationsign");
	cmcselector.selectedIndex = 0;
	
	var sortselector = document.getElementById("sortingcriterium");
	sortselector.selectedIndex = 0;
}

function exclusiveColors(color)
{
	disableColors();
	opCheck(color);
}

function exclusiveTypes(type)
{
	disableTypes();
	opCheck(type);
}

function exclusiveRarities(rarity)
{
	disableRarities();
	opCheck(rarity);
}

function checkMagicEnter(e)
{
	if (e && e.which)
	{
		var code = e.which;
	}
	else
	{
		e = event;
		var code = e.keyCode;
	}
	if (code == 13)
	{
		findMagic();
	}
}

function checkSearchEnter(e)
{
	if (e && e.which)
	{
		var code = e.which;
	}
	else
	{
		e = event;
		var code = e.keyCode;
	}
	if (code == 13)
	{
		findSomething(contextid);
	}
}

function opCheckUncheck(elementid)
{
	var element = document.getElementById(elementid);
	element.checked = !element.checked;
	(element.checked) ? document.getElementById(elementid + "span").style.backgroundPosition = "0px -12px" : document.getElementById(elementid + "span").style.backgroundPosition = "0px 0px";
}

function opCheck(elementid)
{
	document.getElementById(elementid).checked = true;
	document.getElementById(elementid + "span").style.backgroundPosition = "0px -12px";
}

function opUncheck(elementid)
{
	document.getElementById(elementid).checked = false;
	document.getElementById(elementid + "span").style.backgroundPosition = "0px 0px";
}
/*
END OF MAGIC SEARCH FUNCTIONS
*/
function resetFilters()
{
	for (var j in filters)
	{
		var filterline = document.getElementById(j + "filter");
		if (filterline)
		{
			for (k=0;k<filterline.childNodes.length;k++)
			{
				if (k == 0)
				{
					filterline.childNodes[k].style.fontWeight = "bold";
				}
				else
				{
					filterline.childNodes[k].style.fontWeight = "normal";
				}
			}
		}
	}
	
	var mainlist = document.getElementById("innerlist");
	if (mainlist)
	{
		var oddrow = true;
		for (i=0;i<mainlist.childNodes.length;i++)
		{
			var currentline = mainlist.childNodes[i];
			currentline.style.display = "block";
			if (oddrow)
			{
				currentline.className = "oddrow";
				oddrow = false;
			}
			else
			{
				currentline.className = "evenrow";
				oddrow = true;
			}
		}
	}
}

function narrowSearchAbit(selector,choice)
{
	filters[selector] = choice;
	
	var filterline = document.getElementById(selector + "filter");
	if (filterline)
	{
		for (j=0;j<filterline.childNodes.length;j++)
		{
			filterline.childNodes[j].style.fontWeight = "normal";
		}
		var mememe = document.getElementById("filter" + selector + choice);
		if (mememe)
		{
			mememe.style.fontWeight = "bold";
		}
	}
	
	var mainthing = document.getElementById("innerlist");
	if (mainthing)
	{
		var oddrow = true;
		for (i=0;i<mainthing.childNodes.length;i++)
		{
			var currentthing = mainthing.childNodes[i];
			var display = true;
			for (attribute in filters)
			{
				var choice = filters[attribute];
				if (choice != "all")
				{
					if (currentthing.getAttribute(attribute) != choice)
					{
						display = false;
					}
				}
			}
			
			if (display)
			{
				currentthing.style.display = "block";
				if (oddrow)
				{
					currentthing.className = "oddrow";
					oddrow = false;
				}
				else
				{
					currentthing.className = "evenrow";
					oddrow = true;
				}
			}
			else
			{
				currentthing.style.display = "none";
			}
		}
	}
}

function narrowButtonsAbit(selector,choice)
{
	buttonfilters[selector] = choice;
	
	var filterline = document.getElementById(selector + "filter");
	if (filterline)
	{
		for (j=0;j<filterline.childNodes.length;j++)
		{
			filterline.childNodes[j].style.fontWeight = "normal";
		}
		var mememe = document.getElementById("filter" + selector + choice);
		if (mememe)
		{
			mememe.style.fontWeight = "bold";
		}
	}
	
	var mainthing = document.getElementById("collectionsbody");
	if (mainthing)
	{
		for (i=0;i<mainthing.childNodes.length;i++)
		{
			var childthing = mainthing.childNodes[i];
			for (k=0;k<childthing.childNodes.length;k++)
			{
				var currentthing = childthing.childNodes[k].firstChild;
				if (currentthing.getAttribute)
				{
					var display = true;
					for (attribute in buttonfilters)
					{
						var choice = buttonfilters[attribute];
						if (choice != "all")
						{
							if (currentthing.getAttribute(attribute) != choice)
							{
								display = false;
							}
						}
					}
					
					if (display)
					{
						currentthing.parentNode.setAttribute("class","setbutton");
					}
					else
					{
						currentthing.parentNode.setAttribute("class","greyedoutbutton");
					}
				}
			}
		}
	}
}

function showPane(paneid)
{
	cleanup();
	
	if (paneid == "catalog")
	{
		document.getElementById("legal").innerHTML = "";
	}
	
	if (paneid == "init")
	{
		paneid = "catalog";
	}
	
	if (paneid == "collections")
	{
		if (!contextid)
		{
			return false;
		}
	}
	
	document.getElementById("tabcatalog").style.fontWeight = "";
	document.getElementById("tabcollections").style.fontWeight = "";
	document.getElementById("tablists").style.fontWeight = "";
	document.getElementById("tabsearch").style.fontWeight = "";
	document.getElementById("tabcart").style.fontWeight = "";
	
	var activetab = document.getElementById("tab" + paneid);
	if (activetab)
	{
		activetab.style.fontWeight = "bold";
	}
	
	document.getElementById("catalog").style.display = "none";
	document.getElementById("collections").style.display = "none";
	document.getElementById("lists").style.display = "none";
	document.getElementById("search").style.display = "none";
	document.getElementById("cart").style.display = "none";
	
	var activepane = document.getElementById(paneid);
	if (activepane)
	{
		activepane.style.display = "block";
	}
	
	if (paneid == "search")
	{
		var searchstart = document.getElementById("cardsearchfield");
		if (searchstart)
		{
			searchstart.focus();
		}
	}
}

function orderfoil(a,b)
{
	var returnvalue = 0;
	
	if (a.getAttribute("elname") == b.getAttribute("elname"))
	{
		if (a.getAttribute("foil") == "NF")
		{
			returnvalue = -1;
		}
		else
		{
			returnvalue = 1;
		}
	}
	
	return returnvalue;
}

function sortNameData(a,b)
{
	var propa = a.getAttribute("elname");
	var propb = b.getAttribute("elname");
	
	var returnvalue = 0;
	
	if (propa < propb)
	{
		returnvalue = -1;
	}
	if (propa > propb)
	{
		returnvalue = 1;
	}
	
	return returnvalue;
}

function sortNameDownData(a,b)
{
	var propa = a.getAttribute("elname");
	var propb = b.getAttribute("elname");
	
	var returnvalue = 0;
	
	if (propa > propb)
	{
		returnvalue = -1;
	}
	if (propa < propb)
	{
		returnvalue = 1;
	}
	
	return returnvalue;
}

function sortName(up)
{
	sortbuffer = new Array();
	
	var rootelement = document.getElementById("innerlist");
	if (rootelement)
	{
		for (i=0;i<rootelement.childNodes.length;i++)
		{
			var element = rootelement.childNodes[i];
			sortbuffer[i] = element;
		}
		
		if (up)
		{
			sortbuffer.sort(sortNameData);
			sortbuffer.sort(orderfoil);
		}
		else
		{
			sortbuffer.sort(sortNameDownData);
			sortbuffer.sort(orderfoil);
		}
		var oddrow = true;
		
		for (x in sortbuffer)
		{
			if (sortbuffer[x].style.display != "none")
			{
				if (oddrow)
				{
					sortbuffer[x].setAttribute("class","oddrow");
					oddrow = false;
				}
				else
				{
					sortbuffer[x].setAttribute("class","evenrow");
					oddrow = true;
				}
			}
			
			rootelement.appendChild(sortbuffer[x]);
		}
	}
}

function sortStockData(a,b)
{
	var propa = parseInt(a.getAttribute("stock"));
	var propb = parseInt(b.getAttribute("stock"));
	
	var returnvalue = 0;
	
	if (propa < propb)
	{
		returnvalue = -1;
	}
	else if (propa > propb)
	{
		returnvalue = 1;
	}
	
	return returnvalue;
}

function sortStockDownData(a,b)
{
	var propa = parseInt(a.getAttribute("stock"));
	var propb = parseInt(b.getAttribute("stock"));
	
	var returnvalue = 0;
	
	if (propa > propb)
	{
		returnvalue = -1;
	}
	else if (propa < propb)
	{
		returnvalue = 1;
	}
	
	return returnvalue;
}

function sortStock(up)
{
	sortbuffer = new Array();
	
	var rootelement = document.getElementById("innerlist");
	if (rootelement)
	{
		for (i=0;i<rootelement.childNodes.length;i++)
		{
			var element = rootelement.childNodes[i];
			sortbuffer[i] = element;
		}
		
		if (up)
		{
			sortbuffer.sort(sortStockData);
		}
		else
		{
			sortbuffer.sort(sortStockDownData);
		}
		var oddrow = true;
		
		for (x in sortbuffer)
		{
			if (sortbuffer[x].style.display != "none")
			{
				if (oddrow)
				{
					sortbuffer[x].setAttribute("class","oddrow");
					oddrow = false;
				}
				else
				{
					sortbuffer[x].setAttribute("class","evenrow");
					oddrow = true;
				}
			}
			
			rootelement.appendChild(sortbuffer[x]);
		}
	}
}

function sortPriceData(a,b)
{
	var propa = parseFloat(a.getAttribute("price"));
	var propb = parseFloat(b.getAttribute("price"));
	
	var returnvalue = 0;
	
	if (propa < propb)
	{
		returnvalue = -1;
	}
	else if (propa > propb)
	{
		returnvalue = 1;
	}
	
	return returnvalue;
}

function sortPriceDownData(a,b)
{
	var propa = parseFloat(a.getAttribute("price"));
	var propb = parseFloat(b.getAttribute("price"));
	
	var returnvalue = 0;
	
	if (propa > propb)
	{
		returnvalue = -1;
	}
	else if (propa < propb)
	{
		returnvalue = 1;
	}
	
	return returnvalue;
}

function sortPrice(up)
{
	sortbuffer = new Array();
	
	var rootelement = document.getElementById("innerlist");
	if (rootelement)
	{
		for (i=0;i<rootelement.childNodes.length;i++)
		{
			var element = rootelement.childNodes[i];
			sortbuffer[i] = element;
		}
		
		if (up)
		{
			sortbuffer.sort(sortPriceData);
		}
		else
		{
			sortbuffer.sort(sortPriceDownData);
		}
		var oddrow = true;
		
		for (x in sortbuffer)
		{
			if (sortbuffer[x].style.display != "none")
			{
				if (oddrow)
				{
					sortbuffer[x].setAttribute("class","oddrow");
					oddrow = false;
				}
				else
				{
					sortbuffer[x].setAttribute("class","evenrow");
					oddrow = true;
				}
			}
			
			rootelement.appendChild(sortbuffer[x]);
		}
	}
}