selects=new Array();

deck=new Array();

currentChoices=new Array();
oldChoices=new Array();

function buildDeck() {
	deck=new Array();
	for (i=0; i<cards.length; i++)
		if (validateCard(i))
			deck[deck.length]=i;
	if (deck.length<10)
		alert("Warning: Fewer than 10 kingdom cards are left with selected options");
	
	fisherYates(deck);
}

//Fisher-Yates shuffle
//Via http://sedition.com/perl/javascript-fy.html
function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}

function populateSelectArray() {
	selects[selects.length]=document.getElementById("k01");
	selects[selects.length]=document.getElementById("k02");
	selects[selects.length]=document.getElementById("k03");
	selects[selects.length]=document.getElementById("k04");
	selects[selects.length]=document.getElementById("k05");
	selects[selects.length]=document.getElementById("k06");
	selects[selects.length]=document.getElementById("k07");
	selects[selects.length]=document.getElementById("k08");
	selects[selects.length]=document.getElementById("k09");
	selects[selects.length]=document.getElementById("k10");
}

function populateOptions() {
	for (i=0; i<cards.length; i++) {
		cardname=cards[i].name +" ("+setAbbreviations[cards[i].set]+")";
		for (j=0; j<selects.length; j++) {
			selects[j].add(new Option(cardname, i), null);
		}
	}
}

function writeCardList() {
	alchemy=document.getElementById("alchemyList");
	base=document.getElementById("baseSetList");
	intrigue=document.getElementById("intrigueList");
	seaside=document.getElementById("seasideList");
	promo=document.getElementById("promoList");
	for (i=0; i<cards.length; i++) {
		if (cards[i].set=="Alchemy")
			alchemy.innerHTML+="<br><input type='checkbox' id='c"+i+"' checked> "+cards[i].name;
		if (cards[i].set=="Base")
			base.innerHTML+="<br><input type='checkbox' id='c"+i+"' checked> "+cards[i].name;
		else if (cards[i].set=="Intrigue")
			intrigue.innerHTML+="<br><input type='checkbox' id='c"+i+"' checked> "+cards[i].name;
		else if (cards[i].set=="Seaside")
			seaside.innerHTML+="<br><input type='checkbox' id='c"+i+"' checked> "+cards[i].name;
		else if (cards[i].set=="Promo")
			promo.innerHTML+="<br><input type='checkbox' id='c"+i+"' checked> "+cards[i].name;
	}
}

function init() {
	populateSelectArray();
	populateOptions();
	writeCardList();
	buildDeck();
	randomizeAll();
}