
// Copyright 2007, Steve McLaughlin. All rights reserved.
// iPhone-Solitaire.com

var sboard = '';
var sboardstatus = '';
var ssolution = '';
var resume = false;

var totalscore = 0;
var playing = false;

var cards = new Array('A', '2', '3', '4','5','6','7','8','9','10','J','Q','K');
var suits = new Array('C', 'S', 'D', 'H');
var deck;
var play;

var goalpiles;
var stacks;

var selectedcard = null;
var cardback = '&nbsp;';

var cardwidth=32;
var cardheight=42;

var topline = 80; // 74 or 120
var farleft = 10;

var topdeck = topline; // 68

var leftplay = 50;

var vcardspace = 16;
var hcardspace = 12;
var leftgoal = farleft + 3 * (cardwidth + hcardspace);//142;
var topstack = topline + cardheight + vcardspace;

var gamestart;

function unselectcard()
{
	// no card selected now
	if(selectedcard != null)
	{
		selectedcard.style.backgroundColor='';
		selectedcard=null;
	}
}

function randOrd()
{
	return(Math.round(Math.random() ) -0.5 );
}

function flipup(card)
{
	card.style.backgroundColor='#ffffff';
	card.innerHTML = card.myface;
	card.face = 'u';
}

function flipdown(card)
{
	card.style.backgroundColor='#77ccff';
	card.innerHTML = cardback;
	card.face = 'd';
}

function initdeck(resume)
{
	deck = new Array();
	play = new Array();
	goalpiles = new MultiDimensionalArray(4, 0);
	stacks = new MultiDimensionalArray(7, 0);
	
	// Put all 52 cards into the deck (string names)
	for(var s = 0; s<4; s++)
	{
		for(c=0;c<13;c++)
		{
			var suit = suits[s].toString();
			var card = cards[c].toString();
			
		    var cardid = card + "-" + suit;
		    var mycard = document.getElementById('card' + cardid);
			
			// put the suit and the card into custom properties	
			mycard.suit = suit;
			mycard.card = card;
			mycard.cardidx = c;
			
			// set the size of the card
			mycard.style.width = cardwidth.toString() + 'px';
			mycard.style.height = cardheight.toString() + 'px';
			
			// put the proper suitcolor as a custom property, sc
			var suitcolor = 'b';
			if(suit=='D' || suit=='H')
				suitcolor = 'r';
			mycard.sc = suitcolor;
			
			switch(suit)
			{
				case 'D':
					suitg = '&diams;';
					break;
				case 'C':
					suitg = '&clubs;';
					break;
				case 'S':
					suitg = '&spades;';
					break;
				case 'H':
					suitg = '&hearts;';
					break;
			}
						
			// store what should be shown if it is face up
			mycard.myface = '<div class="cn">' + card + ' ' + suitg + '<br>' + suitg + '</div>';
			flipdown(mycard);
			if(!resume)
				deck.push(mycard);
		}
	}
	
	var deckplace = document.getElementById('deckplace');
			
	// set the size of the card
	deckplace.style.top = topdeck.toString() + 'px';
	deckplace.style.left = farleft.toString() + 'px';
	deckplace.style.width = cardwidth.toString() + 'px';
	deckplace.style.height = cardheight.toString() + 'px';
	deckplace.style.visibility = 'visible';
}

function shuffledeck()
{
	// shuffle the deck
	deck.sort(randOrd);
}

function printcards()
{
	for(i=0;i<52;i++)
	{
		document.write(deck[i] + '<br>');
	}
}

function placegoals()
{
	for(var i=0; i<4; i++)
	{
		// set the location for the DIV of this goal pile
		var mygoal = document.getElementById('goal' + i);
		var left = i * (cardwidth + hcardspace) + leftgoal;
		
		mygoal.style.left = left.toString() + 'px';
		mygoal.style.top = topline + 'px';
		mygoal.style.width = cardwidth.toString() + 'px';
		mygoal.style.height = cardheight.toString() + 'px';
		mygoal.style.zIndex = 1;
		mygoal.style.visibility='visible';
		mygoal.pileid = i;
	}
}

function placestacks()
{
	for(var i=0; i<7; i++)
	{
		// set the location for the DIV of this stack pile
		var mystack = document.getElementById('stack' + i);
		var left = i * (cardwidth + hcardspace) + farleft;
		var top = topstack;
		
		mystack.style.left = left.toString() + 'px';
		mystack.style.top = top.toString() + 'px';
		mystack.style.width = cardwidth.toString() + 'px';
		mystack.style.height = cardheight.toString() + 'px';
		mystack.style.zIndex = 1;
		mystack.style.visibility='visible';
		mystack.pileid = i;
	}
}

// user clicked the deck, deal the next 3 (or num left) cards
function deal3()
{
	// keep the other cards face up and move them back in line to the left

	var card;
	for(var i = 0; i< play.length; i++)
	{
		card = play[i];
		
		// move it, but keep it FACE UP
		var left = leftplay ;
		var top = topdeck;
			
		card.style.left = left.toString() + 'px';
		card.style.top = top.toString() + 'px';
		card.style.zIndex = i;
		
		flipup(card);
	}
		
		
	// Deal the next 3 off the deck up top
	for(var i= 0; i<3; i++)
	{
		// take a card off the deck and place it onto the play stack of 3
		var mycard = deck.pop();
		if(mycard == null)
			break;
		
		play.push(mycard);
		
		var l = play.length;
		
		// set the location for the DIV of this card
					
		var left = i * (cardwidth/2) + leftplay;
		var top = topdeck + i * 2;
		
		mycard.style.left = left.toString() + 'px';
		mycard.style.top = top.toString() + 'px';
		mycard.style.zIndex = l + i + 1;
		
		// use our custom properties to track which pile this is on
		mycard.pile = 'P';
		mycard.pilenum = 0;
		
		// set the custom face property to up or down

		flipup(mycard);
		mycard.style.visibility='visible';
		
	}
}

function dealgame()
{
	// deal cards onto each of the stacks in order such that 1 on stack 1, 2 on 2, etc. through 7 on stack 7
	// then flip over the top card on each stack
	
	// deal out the stacks across the bottom
	for(var i = 0; i<7; i++)
	{
		for(var j=0;j<i+1;j++)
		{
			// take a card off the deck and place it onto the stack
			var mycard = deck.pop();
			stacks[i].push(mycard);
			
			// set the location for the DIV of this card
						
			var left = i * (cardwidth + hcardspace) + farleft;
			var top = j * 3 + topstack;
			
			mycard.style.left = left.toString() + 'px';
			mycard.style.top = top.toString() + 'px';
			mycard.style.zIndex = j+2;
			
			// use our custom properties to track which pile this is on
			mycard.pile = 'S';
			mycard.pilenum = i;
			
			// set the custom face property to up or down
			if(j == i)
			{
				flipup(mycard);
			}
			else
			{
				flipdown(mycard);
			}
			mycard.style.visibility='visible';
		}
	}
	
	// Deal the first 3 off the deck up top
	for(i= 0; i<3; i++)
	{
		// take a card off the deck and place it onto the play stack of 3
		var mycard = deck.pop();
		play.push(mycard);
		
		// set the location for the DIV of this card
					
		var left = i * (cardwidth/2) + leftplay;
		var top = topdeck + i * 2;
		
		mycard.style.left = left.toString() + 'px';
		mycard.style.top = top.toString() + 'px';
		mycard.style.zIndex = i+1;
		
		// use our custom properties to track which pile this is on
		mycard.pile = 'P';
		mycard.pilenum = 0;
		
		// set the custom face property to up or down

		flipup(mycard);
		
		mycard.style.visibility='visible';

	}
	
	// now set the rest of the deck to face down and back on the deck pile
	for(i=0; i<deck.length; i++)
	{
		var mycard = deck[i];
		
		mycard.style.zIndex = i+1;
		
		// use our custom properties to track which pile this is on
		mycard.pile = 'D';
		mycard.pilenum = 0;
		
		// set the custom face property to up or down
		flipdown(mycard);
		
		var left = farleft;
		var top = topdeck;
		
		mycard.style.left = left.toString() + 'px';
		mycard.style.top = top.toString() + 'px';
			
		mycard.style.visibility='visible';
	}
}

function newgame()
{		
	var obj = document.getElementById('iphone');
	obj.style.visibility = "hidden";
	
	var sgame = readCookie('gamestatus');
	//alert(sgame);
	
	placegoals();
	
	placestacks();
	
	if(sgame != null)	// resumegame
	{
		initdeck(true);
		resumegame(sgame);
	}
	else
	{
		initdeck(false);
		
		shuffledeck();
		
		dealgame();
		
		startTime();
	}
	
	playing = true;
	
	goTime();
	//printcards();
}

function resumegame(sgame)
{
	// parse the serialized game string
	// order = deck, play, goalpiles, stacks 
	
	var s = sgame.split('*');
	
	// process the deck
	var scard = s[0].split('|');
	for(var i = 0; i < scard.length; i++)
	{
		
		// take off the -d or -u indicating which way the face is
		var carddata = scard[i].split('-');
		var cardid = carddata[0] + '-' + carddata[1];
		
		var mycard = document.getElementById('card' + cardid);
		
		if(mycard != null)
		{
			deck.push(mycard);
			
			mycard.style.zIndex = i+1;
			
			// use our custom properties to track which pile this is on
			mycard.pile = 'D';
			mycard.pilenum = 0;
			
			// set the custom face property to up or down
			flipdown(mycard);
			
			var left = farleft;
			var top = topdeck;
			
			mycard.style.left = left.toString() + 'px';
			mycard.style.top = top.toString() + 'px';
				
			mycard.style.visibility='visible';
		}
	}
	
	
	// parse the play stack
	var scard = s[1].split('|');
	for(var i = 0; i < scard.length; i++)
	{
		
		// take off the -d or -u indicating which way the face is
		var carddata = scard[i].split('-');
		var cardid = carddata[0] + '-' + carddata[1];
		var face = carddata[1];
		
		var mycard = document.getElementById('card' + cardid);
		// move it, but keep it FACE UP
		var left = leftplay ;
		var top = topdeck;
			
		if(mycard != null)
		{
			mycard.style.left = left.toString() + 'px';
			mycard.style.top = top.toString() + 'px';
			mycard.style.zIndex = i;
			
			flipup(mycard);
			
			play.push(mycard);
			
			if(i > scard.length - 4)	// move it over
			{
				var j = 3 - (scard.length - i);
				
				// set the location for the DIV of this card
							
				var left = j * (cardwidth/2) + leftplay;
				var top = topdeck + j * 2;
				
				mycard.style.left = left.toString() + 'px';
				mycard.style.top = top.toString() + 'px';
				mycard.style.zIndex = i;
				
				// use our custom properties to track which pile this is on
				mycard.pile = 'P';
				mycard.pilenum = 0;
				
				// set the custom face property to up or down
		
				flipup(mycard);
					
				mycard.style.visibility='visible';
			}
		}
	}
	
	// parse the goalpile stacks
	for(g=0;g<4;g++)
	{
		var scard = s[2+g].split('|');
		
		//alert(s[2+g] + ' ' + scard.length);
		
		for(var i = 0; i < scard.length; i++)
		{
			
			// take off the -d or -u indicating which way the face is
			var carddata = scard[i].split('-');
			var cardid = carddata[0] + '-' + carddata[1];
			var face = carddata[2];
			
			var mycard = document.getElementById('card' + cardid);
			
			if(mycard != null)
			{
				if(face == 'u')
					flipup(mycard);
				else
					flipdown(mycard);
				
				placegoalcard(mycard, g);
				mycard.style.visibility='visible';
			}
			
		}
	}
	
	var top;
	var cardunder;
	
	// parse the stacks
	for(g=0;g<7;g++)
	{
		var scard = s[6+g].split('|');
		
		//alert(s[2+g] + ' ' + scard.length);
		
		for(var i = 0; i < scard.length; i++)
		{
			//alert(scard[i]);
			
			// take off the -d or -u indicating which way the face is
			var carddata = scard[i].split('-');
			var cardid = carddata[0] + '-' + carddata[1];
			var face = carddata[2];
			
			var mycard = document.getElementById('card' + cardid);
			
			var pilenum = g;
			
			if(mycard != null)
			{
				if(face == 'u')
					flipup(mycard);
				else
					flipdown(mycard);
				
				var l =  stacks[pilenum].length
				if(l==0)
					cardunder = null;
				else
					cardunder = stacks[pilenum][l-1];
				
				stacks[pilenum].push(mycard);
				
				// set the location for the DIV of this card
							
				var left = pilenum * (cardwidth + hcardspace) + farleft;
				
				if(cardunder == null)
				{
					top = topstack;
				}
				else
				{
					if(cardunder.face == 'u')
						top = parseInt(cardunder.style.top) + vcardspace ;
					else
						top = parseInt(cardunder.style.top) + 3 ; ;
				}
				
				mycard.pilenum = pilenum;
				mycard.pile = 'S';
				mycard.style.left = left.toString() + 'px';
				mycard.style.top = top.toString() + 'px';
				mycard.style.zIndex = l +2;
	
				mycard.style.visibility='visible';
			}
			
		}
	}
	
	// reset the gamestart so it includes the prior elapsed time
	var priortime = s[13];
	
	if(priortime == null)
		priortime=0;
	
	var now = new Date;
	gamestart = now.getTime() - priortime;
	
}

function newgamebutton()
{
	eraseCookie('gamestatus');
	newgame();
}

// save the game in progress to a few cookies
function savegame()
{
	
	var sdeck = "";
	
	// serialize the deck
	for(i=0; i<deck.length; i++)
	{
		var mycard = deck[i];
		
		sdeck = sdeck + mycard.card + "-" + mycard.suit + '-' + mycard.face;
		
		if(i < deck.length-1)
			sdeck = sdeck + '|';
	}
	sdeck = sdeck + "*";
	
	// The play pile
	for(i=0; i<play.length; i++)
	{
		var mycard = play[i];
		
		sdeck = sdeck + mycard.card + "-" + mycard.suit + '-' + mycard.face;
		if(i < play.length-1)
			sdeck = sdeck + '|';
	}
	sdeck = sdeck + "*";
	
	// the goals
	for(j=0; j<4; j++) {
		for(i=0; i < goalpiles[j].length; i++)
		{
			var mycard = goalpiles[j][i];
			
			sdeck = sdeck + mycard.card + "-" + mycard.suit + '-' + mycard.face;
			if(i < goalpiles[j].length-1)
				sdeck = sdeck + '|';
		}
		sdeck = sdeck + "*";
	}
	
	// the stacks
	for(j=0; j<7; j++) {
		for(i=0; i < stacks[j].length; i++)
		{
			var mycard = stacks[j][i];
			
			sdeck = sdeck + mycard.card + "-" + mycard.suit + '-' + mycard.face;
			if(i < stacks[j].length-1)
				sdeck = sdeck + '|';
		}
		sdeck = sdeck + "*";
	}
	
	// save the amount of time elapsed
	var now = new Date;
	var elapsed = now.getTime() - gamestart;
	
	sdeck = sdeck + elapsed + "*";
	
	createCookie('gamestatus',sdeck,365);
}

// Handle a click on an empty stack space. Only if a King is being placed here is it legit
function stackclick(pile)
{
	if(selectedcard == null)
		return;
		
	// check the first face-up card, and then each other face up card to see if we can move it
	var card;
	var tmp = new Array();
	
	if(selectedcard.pile == 'P')
	{
		if(selectedcard.card == 'K')	// place it
		{
			// move the selected card
			// pop it off stack
			var selcard = popcard(selectedcard);
			
			placeemptystackcard(selcard, pile.pileid);
		}
		// no card selected now
		selectedcard.style.backgroundColor='';
		selectedcard=null;
		return;
	}
	
	var n = selectedcard.pilenum;
	var arr = stacks[n];
	l = arr.length;
	for(i = l-1; i>=0; i--)
	{
		card = arr[i];
		if(card.face == 'd')
			break;
			
		if(card.card == 'K')
		{
			// move this card and each of the ones face up on top of it (below on board)
			
			for(var j=i; j<l; j++)
			{
				// move the selected card
				// pop it off stack
				var selcard = popcard(card);
				
				// store it temporarily on the stack
				tmp.push(selcard);
			}
			
			// now, move all the cards from the temp stack to destination
			card = tmp.pop();
			placeemptystackcard(card, pile.pileid);
			card = tmp.pop();
			
			while(card != null)
			{
				placestackcard(card, pile.pileid);
				card = tmp.pop();
			}
			break;
		}
	}
	
	// no card selected now
	selectedcard.style.backgroundColor='';
	selectedcard=null;		
}



// Handle a click on the empty deck space. This should cause the deck to reset and deal the next 3 if they are there
function deckclick(pile)
{
	var i;
	
	// no card selected now
	if(selectedcard != null)
	{
		selectedcard.style.backgroundColor='';
		selectedcard=null;
	}
		
	// move the cards from the play stack to the deck stack
	var card = play.pop();
	i=1;
	while(card != null )
	{
		deck.push(card);
		// turn it over and move it to the deck location
		var left = farleft ;
		var top = topdeck;
			
		card.style.left = left.toString() + 'px';
		card.style.top = top.toString() + 'px';
		card.pile = 'D';
		flipdown(card);
		card.style.zIndex = i;
		
		card = play.pop();
		
		i++;
	}

}

function goalclick(pile)
{
	// allow this move if selected card is an ACE
	if(selectedcard != null)
	{
		if(selectedcard.card == 'A')	// place it
		{
			// move the selected card
			// pop it off stack
			var selcard = popcard(selectedcard);
			
			placegoalcard(selcard, pile.pileid);
		}
		
		// no card selected now
		selectedcard.style.backgroundColor='';
		selectedcard=null;
	}
}

function cardclick(card)
{
	
//	if(selectedcard != null)
	//	selectedcard.style.backgroundColor='';
				
	if(selectedcard == null)
	{
		// only highlight the card if it is on the top of a stack, goalpile, or show PILE
		// get the correct array
	
		var arr;
		switch(card.pile)
		{
			case 'S': // stacks
				arr=stacks[card.pilenum];
				break;
			case 'G': // goalpiles
				arr=goalpiles[card.pilenum];
				break;
			case 'P': // play
				arr=play;
				break;
			case 'D': // deck, deal out more cards
				arr=deck;
				deal3();
				return;
				break;
		}
		
		// see if it's the top on the stack
		if(arr[arr.length-1].id == card.id)
		{				
			card.style.backgroundColor='#ffff00';
			
			// set this as the selected card
			selectedcard = card;
			
		}
	}
	else	// a card is selected, clicking a second card... move it if legal
	{
		var arr;
		switch(card.pile)
		{
			case 'S': // stacks
				arr=stacks[card.pilenum];
				break;
			case 'G': // goalpiles
				arr=goalpiles[card.pilenum];
				break;
			case 'P': // play
				arr=play;
				break;
			case 'D': // deck
				arr=deck;
				break;
		}
		
		// see if it's the top on the stack for placement
		if(arr[arr.length-1].id == card.id)
		{
			// place it if legal
			switch(card.pile)
			{
				case 'S': // stacks only if top card is opposite suit, and next higher number
				
					// we need to check each card that is face up and then move the whole face up stack
					
					checkcardandstack(card,selectedcard);
					break;
				case 'G': // goalpiles
					// be sure that the card is either an Ace being placed on blank spot or same suit, next higher number
					if(isValidGoalMove(card, selectedcard))
					{
						// move the selected card
						// pop it off stack
						var selcard = popcard(selectedcard);
						
						placegoalcard(selcard, card.pilenum);
					}
					
					break;
				case 'P': // play
					arr=play;
					break;
				case 'D': // deck
					arr=deck;
					break;
			}
			
		
		}
		
		// no card selected now
		selectedcard.style.backgroundColor='';
		selectedcard=null;
		
	}
}

function checkcardandstack(targetcard, movingcard)
{
	
	// check the first face-up card, and then each other face up card to see if we can move it
	var card;
	var tmp = new Array();
	
	if(movingcard.pile == 'P' || movingcard.pile == 'G')
	{
		if(isOppositeColor(targetcard, movingcard) && isNextHigher(targetcard,movingcard))
		{
			// move the selected card
			// pop it off stack
			var selcard = popcard(movingcard);
			
			placestackcard(selcard, targetcard.pilenum);
								
		}
		return;
	}
	
	var n = movingcard.pilenum;
	var arr = stacks[n];
	l = arr.length;
	for(i = l-1; i>=0; i--)
	{
		card = arr[i];
		if(card.face == 'd')
			break;
		if(isOppositeColor(targetcard, card) && isNextHigher(targetcard,card))
		{
			// move this card and each of the ones face up on top of it (below on board)
			
			for(var j=i; j<l; j++)
			{
				// move the selected card
				// pop it off stack
				var selcard = popcard(card);
				
				// store it temporarily on the stack
				tmp.push(selcard);
			}
			
			// now, move all the cards from the temp stack to destination
			card = tmp.pop();
			while(card != null)
			{
				placestackcard(card, targetcard.pilenum);
				card = tmp.pop();
			}
			break;
		}
	}
	
					
}

// place the card on a bottom pile, on a blank spot (only KING)
function placeemptystackcard(card, pilenum)
{
	var l =  stacks[pilenum].length
	//var cardunder = stacks[pilenum][l-1];
	
	stacks[pilenum].push(card);
	
	// set the location for the DIV of this card
				
	var left = pilenum * (cardwidth + hcardspace) + farleft;
	
	var top = topstack ;
	
	card.pilenum = pilenum;
	card.pile = 'S';
	card.style.left = left.toString() + 'px';
	card.style.top = top.toString() + 'px';
	card.style.zIndex = 2;
}

// place the card on a bottom pile
function placestackcard(card, pilenum)
{
	var l =  stacks[pilenum].length
	var cardunder = stacks[pilenum][l-1];
	
	stacks[pilenum].push(card);
	
	// set the location for the DIV of this card
				
	var left = pilenum * (cardwidth + hcardspace) + farleft;
	
	if(cardunder == null)
		var top = topstack;
	else
		var top = parseInt(cardunder.style.top) + vcardspace ;
	
	card.pilenum = pilenum;
	card.pile = 'S';
	card.style.left = left.toString() + 'px';
	card.style.top = top.toString() + 'px';
	card.style.zIndex = l +2;
}

// place the card on a goal pile
function placegoalcard(card, pilenum)
{
	var l =  goalpiles[pilenum].length
	
	goalpiles[pilenum].push(card);
	
	// set the location for the DIV of this card
	var left = pilenum * (cardwidth + hcardspace) + leftgoal;
		
	var top = topline ;
	
	card.pilenum = pilenum;
	card.pile = 'G';
	card.style.left = left.toString() + 'px';
	card.style.top = top.toString() + 'px';
	card.style.zIndex = l + 2;
	
	// see if we won the game
	if(is_won())
	{
		eraseCookie('gamestatus');
		alert('Congratulations!\nYou Won!');
	}
}

function popcard(card)
{
	var arr;
	var mycard;
	var cardbelow = null;
	switch(card.pile)
	{
		case 'S': // stacks
			arr=stacks[card.pilenum];
			break;
		case 'G': // goalpiles
			arr=goalpiles[card.pilenum];
			break;
		case 'P': // play
			arr=play;
			break;
		case 'D': // deck
			arr=deck;
			break;
	}
	mycard = arr.pop();
	
	// flip card below, if stacks[]
	if(card.pile == 'S')
	{
		// flip the card below, if any
		if(arr.length-1 >=0)
		{
			// get the card below
			cardbelow = arr[arr.length-1];
			// flip it up
			flipup(cardbelow);
		}
	}
	return(mycard);
}

function isOppositeColor(card1, card2)
{
	if(card1.sc != card2.sc)
		return true;
	else
		return false;
}

function isNextHigher(card1, card2)
{
	// card1 should be higher than card2 by exactly 1
	if(card1.cardidx - card2.cardidx == 1)
		return true;
	else
		return false;
}

// same suit, next higher
function isValidGoalMove(card1, card2)
{
	if(card1.suit != card2.suit)
		return false;
	if(card2.cardidx - card1.cardidx == 1)
		return true;
	else
		return false;
}

function MultiDimensionalArray(iRows, iCols)
{
	var i;
	var j;
	var a = new Array(iRows);
	for (i=0; i < iRows; i++)
	{
		a[i] = new Array(iCols);
		for (j=0; j < iCols; j++)
		{
			a[i][j] = "";
		}
	}
	return a;
}


function is_won()
{
	for(var g = 0; g < 4; g++)
	{
		if(goalpiles[g].length <13)
			return false;
	}
	playing = false;
	return true;
}





function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function showhelp()
{
	var obj = document.getElementById('help');
	obj.style.visibility = "visible";
}

function hidehelp()
{
	var obj = document.getElementById('iphone');
	obj.style.visibility = "hidden";
	
	var obj = document.getElementById('help');
	obj.style.visibility = "hidden";
}

function showresume()
{
	var obj = document.getElementById('resume');
	obj.style.visibility = "visible";
}

function hideresume()
{
	var obj = document.getElementById('resume');
	obj.style.visibility = "hidden";
	
}

function startTime()
{
	// number of milliseconds since Jan 1, 1970
	var now = new Date;
	gamestart = now.getTime();
}

function goTime()
{
	var now = new Date;
	var elapsed = now.getTime() - gamestart;
	
	s = Math.round(elapsed + 1000) / 1000;
	d = Math.round(s / 86400);
	s %= 86400;
	h = Math.floor(s / 3600);
	s %= 3600;
	m = Math.floor(s / 60);
	s %= 60;
	s = Math.round(s);
	
	// add a zero in front of numbers < 10
	m=checkTime(m);
	s=checkTime(s);
	var c = "";
	if(h > 0)
		c += h + ":";
	
	c += m + ":" + s;
	document.getElementById('timer').innerHTML = c;
	if(playing)
		t=setTimeout('goTime()',500);
}

function checkTime(i)
{
	if(i<10)
	{
		i="0" + i;
	}
	return i;
}