function startmenu()
{
        document.getElementById('a-long').style.display = "none";
}

function menufunc(menuId)
{
        if(document.getElementById(menuId).style.display == "none")
        {
                document.getElementById(menuId).style.display = "block";
        }
        else if(document.getElementById(menuId).style.display == "block")
        {
               document.getElementById(menuId).style.display = "none"; 
        }
}


function Hide(item)
{
	obj = document.getElementById(item);
	obj.style.display = "none";
}

function Show(item)
{
	obj = document.getElementById(item);
	obj.style.display = "";
	obj.style.display = "block";
}


function cl(Name) {
	if (Name.defaultValue==Name.value) Name.value = '';
	}
function cl(Email1) {
	if (Email1.defaultValue==Email1.value) Email1.value = '';
	}
function popup(mylink, windowname) {
	if (! window.focus)return true;
		var href;
	if (typeof(mylink) == 'string')
		href=mylink;
	else
		href=mylink.href;
	window.open(href, windowname, 'width=200,height=100,scrollbars=no');
	return false;
	}

var started = false;
var timeoutid;
var tries = 0;
var gamepics = new Array();
gamepics[0] = new Image();
gamepics[1] = new Image();
gamepics[2] = new Image();
gamepics[3] = new Image();
gamepics[4] = new Image();
gamepics[5] = new Image();
gamepics[6] = new Image();
gamepics[7] = new Image();
gamepics[8] = new Image();
gamepics[9] = new Image();
gamepics[10] = new Image();
gamepics[11] = new Image();
gamepics[12] = new Image();
gamepics[13] = new Image();
gamepics[14] = new Image();
gamepics[15] = new Image();
gamepics[16] = new Image();
gamepics[17] = new Image();
gamepics[18] = new Image();
gamepics[19] = new Image();

var imgback = new Image();
imgback.src = "images/card-back.gif";
var imgdone = new Image();
imgdone.src = "images/card-matched.gif";
var hold = false;

var current_selected = -1;
var keys = new Array ();
var dones = new Array ( false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false );
var starttime;

function notinarray( num ) {
	for ( n = 0; n < picnums.length; n++ ) {
		if ( picnums[n] == num ) {
			return false;
		}
	}
	return true;
}

var picnums = new Array()
var statuses = new Array( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
function pageinit() {
	//get the nine image numbers, unique from 1 to 56
	while ( picnums.length < 10 ) {
		r_num = parseInt((Math.random()*55)+1)
		if ( notinarray( r_num ) ) {
			picnums[picnums.length] = r_num;
		}
	}
	//alert( "picnums = " + picnums );
	//get the image order & assign the images & key values
	for ( i = 0; i < 20; i++ ) {
		flag = true;
		while ( flag ) {
			r_num = parseInt(Math.random()*10);
			if ( r_num == 10 ) {
				r_num = 9;
			}
			if ( statuses[r_num] == 0 ) {
				//alert( "setting-a " + i );
				gamepics[i].src = "images/cards/" + picnums[r_num] + "A.gif";
				keys[i] = picnums[r_num];
				statuses[r_num] = 1;
				flag = false;
			} else if ( statuses[r_num] == 1 ) {
				//alert( "setting-b " + i );
				gamepics[i].src = "images/cards/" + picnums[r_num] + "B.gif";
				keys[i] = -picnums[r_num];
				statuses[r_num] = 2;
				flag = false;
			}
		}
	}
	//alert(keys);
	//show all the cards for the preview
	for ( i = 0; i < 20; i++ ) {
		document.images["gamepic" + i].src = gamepics[i].src;
	}
	setTimeout( "startGame();", 8000 );

	return;
}

function startGame() {
	for ( i = 0; i < 20; i++ ) {
		document.images["gamepic" + i].src = imgback.src;
	}
	started = true;
	starttime = new Date()
	return;
}

function imgclick( index ) {
	// if we are holding for a delay, exit now
	if ( hold ) {
		return;
	}
	// if the game is not running, then exit
	if ( !started ) {
		return;
	}
	// do not bother if it has been solved.
	if ( dones[index] ) {
		return;
	}
	// if no other card is currently selected, then display this card and set the current card to this one.
	if ( current_selected == -1 ) {
		document.images["gamepic" + index].src = gamepics[index].src;
		current_selected = index;
		tries++;
		return;
	}
	// if the card that is clicked is the currently selected one, do nothing
	if ( current_selected == index ) {
		return;
	}
	// if the new card matches the selected one, mark them both as done and set the backgounds to the done backing
	if ( Math.abs(keys[current_selected]) == Math.abs(keys[index]) ) {
		hold = true;
		document.images["gamepic" + index].src = gamepics[index].src;
		timeoutid = setTimeout( "document.images[\"gamepic" + index + "\"].src = imgdone.src;document.images[\"gamepic" + current_selected + "\"].src = imgdone.src;dones[" + current_selected + "] = true;dones[" + index + "] = true;current_selected = -1;hold = false;checkDone();", 2000 );
		return;
	}
	// if the new card does not match the selected one, then reset both card backs, reset the current card, and return.
	if ( Math.abs(keys[current_selected]) != Math.abs(keys[index]) ) {
		hold = true;
		document.images["gamepic" + index].src = gamepics[index].src;
		timeoutid = setTimeout( "document.images[\"gamepic" + index + "\"].src = imgback.src;document.images[\"gamepic" + current_selected + "\"].src = imgback.src;hold = false;", 1000 );
		//setTimeout( "document.images[\"gamepic" + current_selected + "\"].src = imgback.src;", 1000 );
		current_selected = -1;
		return;
	}
	return;
}

function checkDone() {
	for ( i = 0; i < dones.length; i++ ) {
		if ( !dones[i] ) {
			return;
		}
	}
	num_matches = dones.length/2;
	percentage = Math.floor(num_matches/tries*100);
	started = false;
	donetime = new Date();
	diff = donetime.getTime() - starttime.getTime();
	gametime = Math.floor( diff/(1000 * 60) ) + " minutes, " + Math.floor((diff/1000) % 60 ) + " seconds"
	alert( "You finished in " + gametime + ".\nYou made " + tries + " tries.\nYour accuracy was " + percentage + "%." );
	return;
}

function validateForm() {
	if ( document.forms["loginform"].elements["txtLoginID"].value == "" ||
		 document.forms["loginform"].elements["txtPassword"].value == "" ) {
		 alert ( "You must enter a Login Name and Password to proceed." );
		 return false;
	}
	return true;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
