var cards = new Array(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8);
var matched = new Array(16);
var clicked = 0;
var matchCount = 0;
var moveCount = 1;
var firstCard;
var pair;

function shuffle(deck) {
	var i=deck.length; //get the deck length
	if(i===0) {
		return false;
	}
	for(var x=0; x<i; x++) {
		var j=Math.floor(Math.random()*(x+1));
		var temp=deck[x];
		deck[x]=deck[j];
		deck[j]=temp;
	}
}

shuffle(cards); //call the shuffle function

function turnOver(e){
	//image swaping code here
	if(matchCount<8 & !matched[e.name]){
	document.form1.moves.value=moveCount++;
		if(clicked<1){
			//first move
			firstCard=e;
			pair=cards[firstCard.name];
			e.src=document.images["load"+pair].src;
			clicked++;
		} else {
			//second move
			e.src=document.images["load"+cards[e.name]].src;
			if(pair!==cards[e.name]){
				//no match
				alert("That's not a match, try again!");
				firstCard.src=document.images["load0"].src;
				e.src=document.images["load0"].src;
			} else {
				matched[firstCard.name]=true;
				matched[e.name]=true;
				matchCount++;

				if(matchCount>=8) {
					alert("YAY, you won!!");
				}
			}
		clicked=0;
		}
	} else if(matchCount>=8) {
		alert("YAY, you won!!");
	}
}
