function getValue(s) {
	index=s.selectedIndex;
	return s.options[index].value;
}


function show(id) {
	thisOne=document.getElementById(id);
	value=getValue(thisOne);
	if (value!=-1) {
		html=cards[value].toHTML();
		html=html.replace(/\[VP\]/g, '<img src="images/shield.png" title="VP" alt="VP">');
		html=html.replace(/\[treasure\]/g, '<img src="images/coin.png" title="treasure" alt="treasure">');
		html=html.replace(/\[potion\]/g, '<img src="images/potion.png" title="potion" alt="potion">');
		document.getElementById('display').innerHTML=html
		
	}
}

function clearDisplay() {
	document.getElementById('display').innerHTML="";
}

function validateCard(index) {
	c=cards[index]
	
	valid=true;
	
	if (c.set=="Alchemy" && !document.getElementById("Alchemy").checked)
		valid=false;
	else if (c.set=="Base" && !document.getElementById("Base").checked)
		valid=false;
	else if (c.set=="Intrigue" && !document.getElementById("Intrigue").checked)
		valid=false;
	else if (c.set=="Seaside" && !document.getElementById("Seaside").checked)
		valid=false;
	else if (c.set=="Promo" && !document.getElementById("Promo").checked)
		valid=false;
	else if (!document.getElementById('c'+index).checked)
		valid=false;
	else if (document.getElementById('reqDifferent').checked && contains(oldChoices, index))
		valid=false;
	else if (c.is("Attack") && document.getElementById("noAttack").checked)
		valid=false;
	else if (c.is("Curse") && document.getElementById("noCurse").checked)
		valid=false;
	
	return valid;
}

function hasA(att) {
	for (i=0; i<selects.length; i++) {
		index=getValue(selects[i])
		if (cards[index].is(att))
			return true;
	}
	return false;
}

function getA(att) {
	for (i=0; i<deck.length; i++)
		if (cards[deck[i]].is(att))
			return i;
	return -1;
}

function reqAttackClicked() {
	document.getElementById("reqReaction").disabled=!document.getElementById("reqAttack").checked;
	if (document.getElementById("reqAttack").checked)
		document.getElementById("noAttack").checked=false;
}

function noAttackClicked() {
	if (document.getElementById("noAttack").checked) {
		document.getElementById("reqAttack").checked=false;
		document.getElementById("reqReaction").disabled=true;
	}
}

function clearDifferencesList() {
	document.getElementById('differences').innerHTML="";
}

function initializeDifferencesList() {
	clearDifferencesList();
	d=document.getElementById('differences');
	d.innerHTML="<center>Differences from last set</center>";
	d.innerHTML+="<div id='keepList'><b>Keep</b></div>";
	d.innerHTML+="<div id='addList'><b>Add</b></div>";
	d.innerHTML+="<div id='removeList'><b>Remove</b></div>";
	d.innerHTML+="<span class='clearDifferencesLink'><a href='javascript:clearDifferencesList()'>X</a></span>";
}

function populateDifferencesList() {
	if (oldChoices.length>0 && !document.getElementById('reqDifferent').checked) {
		initializeDifferencesList();
		a=document.getElementById('addList');
		k=document.getElementById('keepList');
		r=document.getElementById('removeList');
		addTotal=0;
		keepTotal=0;
		removeTotal=0;
		for (i=0; i<oldChoices.length; i++) {
			c=oldChoices[i]
			if (contains(currentChoices, c)) {
				k.innerHTML+="<br><span class='keep'>"+cards[c].nameWithSet+"</span>";
				keepTotal++;
			}
			else {
				r.innerHTML+="<br><span class='remove'>-"+cards[c].nameWithSet+"</span>";
				removeTotal++;
			}
		}
		for (i=0; i<currentChoices.length; i++) {
			c=currentChoices[i]
			if (!contains(oldChoices, c)) {
				a.innerHTML+="<br><span class='add'>+"+cards[c].nameWithSet+"</span>";
				addTotal++;
			}
		}
		if (addTotal==0)
			a.style.display="none";
		if (keepTotal==0)
			k.style.display="none";
		if (removeTotal==0)
			r.style.display="none";
	} else
		document.getElementById('differences').innerHTML="";
}

function contains(a, obj) {
	for (var i = 0; i < a.length; i++)
		if (a[i] === obj)
			return true;
	return false;
}

function setCurrentChoices() {
	currentChoices=new Array();
	for (i=0; i<selects.length; i++) {
		if (selects[i].selectedIndex!=0)
			currentChoices[currentChoices.length]=selects[i].selectedIndex-1;
	}
	sortCurrentChoices()
}

function sortCurrentChoices() {
	currentChoices.sort(choiceSort);
}
function choiceSort(a, b) {
	return a-b;
}

function showTip(id) {
	document.getElementById(id).style.display="block";
}
function hideTip(id) {
	document.getElementById(id).style.display="none";
}