<!--
var selfRef = null;
function getrand(min, max)
{
  if (min == max) {return min;}
  return min + Math.floor(Math.random()*(max-min+1));
}

function shuffleArray(a)
{
    for (var m = 0; m < a.length; m++)
    {
	t1 = getrand(0,a.length-1);t2 = getrand(0,a.length-1);
	if (t1 != t2) {tt = a[t1];a[t1]=a[t2];a[t2]=tt;}
    }
}

function gameOver(score)
{
    var str = '<div style="height:36px;float:right;"><div style="float:left;padding-top:10px;;">Game Over&nbsp;</div><div id="lbtn"></div><div id="mbtn" onClick="submitfrm('+score+');">OK</div><div id="rbtn"></div></div>';
    var f = document.getElementById("helpboard"); if (f != null) {f.innerHTML = str;}
}

function submitfrm(score)
{
    var f = document.getElementById("memorybrd");
    if (f != null) 
    {
        f.innerHTML = "";
        var str = "Congratulations!!<br>Your score is " + score + "%";
        var o = f.appendChild(document.createElement('div'));
        o.innerHTML='<input type="hidden" name="helppage" value="memoryhelp.html" />';
        var o = f.appendChild(document.createElement('div'));
        o.innerHTML='<input type="hidden" name="gamepage" value="memory.html" />';
        o = f.appendChild(document.createElement('div'));
        o.innerHTML='<input type="hidden" name="msg" value="' + str + '" />';
    }
        
    document.memory_frm.submit();
}

function showHelpText(str)
{
    var o = document.getElementById("helpboard");
    if (o != null) {o.innerHTML = str;}
}


function FruitsMemory()
{
	this.isReady=false;
	this.sqArr = new Array(16);
	this.dspArr = new Array(8);
	this.pair_id = 0;
	this.opc = 10;
	this.imgobj1 = null;
	this.imgobj2 = null;
	this.usrsel = -1;
	this.score=0;
	this.mmImgs = new Array(10);
	
	selfRef = this;



	this.initScr = function ()
	{
	    this.preLoadImgs();
	    showHelpText("memorize the fruits & positions.");
	    var str = "";
	    for (var i = 0; i < 4; i++)
	    {
		for (var j = 0; j < 4; j++)
		{
		    var idx = i*4 + j;
		    str += '<div id="frtSquare"><div class="frtFlash" id="s' + idx + '" onClick="fm.checkClick(' + idx + ');"></div></div>';
		}        
	    }

	    var o = document.getElementById("memorybrd"); if (o != null) {o.innerHTML = str;}
	    this.initGame();
	}


	this.showImage = function (id, imgno)
	{
	    var obj = document.getElementById("s" + id);
	    if (obj != null) {obj.innerHTML = '<img src="' + this.mmImgs[imgno].src  + '" />';}
	}

	this.gameOver = function ()
	{
	    for (var k = 0; k < 17; k++) 
	    {
		if (this.sqArr[k] > 0 && this.sqArr[k] < 10) {return false;}
	    }

	    return true;
	}

	this.checkClick = function (n)
	{
	    if (!this.isReady) {return;}
	    if (this.usrsel >= 0) 
	    {
		var obj = document.getElementById("s" + this.usrsel);
		if (obj != null) {obj.style.backgroundColor = "#ffffff";}
	    }
	    if (this.sqArr[n] == 0) {return;}
	    if (this.sqArr[n] < 10)
	    {
		if (this.usrsel == -1) {showHelpText("Select a fruit to match");}
		else
		{
		    if (this.sqArr[n]+10 == this.sqArr[this.usrsel])
		    {
			this.score++;
			this.showImage(n, this.sqArr[n]);this.sqArr[n] = 0;this.sqArr[this.usrsel] = 0;
		    }
		    else
		    {
			this.showImage(n, 0);this.sqArr[n] = 0;
		    }
		}
		this.usrsel = -1;
		if (this.gameOver()) 
		{
	            for (var i = 0; i < this.sqArr.length; i++) {o = document.getElementById("s" + i);if (o != null) {o.onclick=null;}}
		    gameOver(Math.floor(100*this.score/8+0.5));
		}
		return;
	    }
	    var obj = document.getElementById("s" + n);
	    if (obj != null) {obj.style.backgroundColor="#ff4500";}
	    this.usrsel = n;
	    showHelpText("Find the fruit pairs.");
	}



	this.getImageIndex = function (imgid, plc)
	{
	    var k = 0;
	    for (var ii = 0; ii < 16; ii++)
	    {
		if (this.sqArr[ii] == imgid)
		{
		    k++;
		    if (k == plc) {return ii;}
		}
	    }
	    return 0;
	}



	this.show8 = function ()
	{
	    for (i = 0; i < 8; i++)
	    {
		id = this.getImageIndex(this.dspArr[i], 1);
		if (id >= 0 && id <= 16) 
		{
		    this.sqArr[id] += 10;
		    var obj = document.getElementById("s" + id); if (obj != null) {obj.innerHTML = '<img src="' + this.mmImgs[this.dspArr[i]].src + '" />';}
		}
	    }
	    this.isReady = true;
	    showHelpText("Find the fruit pairs.");
	}


	this.showPair = function ()
	{
	    if (this.pair_id > 7) {this.show8();return;}
	    id1 = this.getImageIndex(this.dspArr[this.pair_id], 1);
	    id2 = this.getImageIndex(this.dspArr[this.pair_id], 2);
	    this.opc = 10;
	    this.imgobj1 = document.getElementById("s" + id1); if (this.imgobj1 != null) {this.imgobj1.innerHTML = '<img src="' + this.mmImgs[this.dspArr[this.pair_id]].src + '" />';}
	    this.imgobj2 = document.getElementById("s" + id2); if (this.imgobj2 != null) {this.imgobj2.innerHTML = '<img src="' + this.mmImgs[this.dspArr[this.pair_id]].src + '" />';}
	    setTimeout(this.showSlow, 100);
	}

	this.initGame = function ()
	{
	    for (i = 0; i < 8; i++)  {var frtid=i+1;this.sqArr[i] = frtid;this.sqArr[i+8] = frtid;this.dspArr[i] = frtid;}
	    shuffleArray(this.sqArr);
	    shuffleArray(this.dspArr);

	    this.pair_id = 0;
	    this.showPair();
	}

	this.preLoadImgs = function ()
	{
	    for (var i = 0; i < 9; i++) { 
		this.mmImgs[i] = new Image(); 
		this.mmImgs[i].src = "images/memory/frt_" + i + ".png"; 
	    } 
	}
	
	this.showSlow = function ()
	{
	    if (selfRef.opc < 101)
	    {
		if (selfRef.imgobj1 != null)
		{
		    selfRef.imgobj1.style.opacity = (selfRef.opc > 99 ? 0.99 : selfRef.opc/100);
		    selfRef.imgobj1.style.filter='alpha(opacity='+ selfRef.opc + ')';
		}

		if (selfRef.imgobj2 != null)
		{
		    selfRef.imgobj2.style.opacity = (selfRef.opc > 99 ? 0.99 : selfRef.opc/100);
		    selfRef.imgobj2.style.filter='alpha(opacity='+ selfRef.opc + ')';
		}
		selfRef.opc += 5;
		setTimeout(selfRef.showSlow,100);
	    }
	    else
	    {
		if (selfRef.imgobj1 != null) {selfRef.imgobj1.innerHTML = "";}
		if (selfRef.imgobj2 != null) {selfRef.imgobj2.innerHTML = "";}
		selfRef.pair_id++;
		selfRef.showPair();
	    }
	}	
}

function numsort(a, b){return (a - b);}

function NumberMemory(memoryNum)
{
	this.isReady=false;
	this.sqArr = new Array(25);
	this.dspArr = new Array();
	this.opc = 10;
	this.score=0;
	this.dspnum = 0;
	this.numobj = null;
	this.clickNum = 0;
	
	selfRef = this;

	this.initScr = function ()
	{
	    for (var i = 0; i < 25; i++)
	    {
	        var x = 0; var done = true;
	        do
	        {
	            x = getrand(1,99);done = true;
	            for (var j = 0; j < i; j++) {if (x == this.sqArr[j]) {done = false;break;}}
	        }while (!done);
	        this.sqArr[i] = x;
	    }
	    while (this.dspArr.pop()) {;}
	    for (var i = 0; i < memoryNum; i++) {this.dspArr.push(this.sqArr[i]);}
	    this.sqArr.sort(numsort);
	    this.showFlash();
	    this.dspNum = 0;
	    this.clickNum = 0;
	    showHelpText("Memorize the numbers.");
	    this.showNumber();
	}

	
	this.showFlash = function()
	{
	    var str = '<div id="ss" class="numFlash"></div>';
	    var o = document.getElementById("memorybrd"); if (o != null) {o.innerHTML = str;}
	}
	
	this.showBoard = function()
	{
	    var str = "";
	    for (var i = 0; i < 5; i++)
	    {
	        for (var j = 0; j < 5; j++)
	        {
		    var idx = i*5 + j;
		    str += '<div id="numSquare"><div class="numDiv" id="s' + idx + '" onClick="fm.checkClick(' + idx + ');">' + this.sqArr[idx] + '</div></div>';
	        }
	    }
	    var o = document.getElementById("memorybrd"); if (o != null) {o.innerHTML = str;}
	    showHelpText("Find the memorized numbers.");
	}
		
	
	this.showNumber = function()
	{
	    if (this.dspnum >= this.dspArr.length) {this.showBoard();return;}
	    this.opc = 10;
	    this.numobj = document.getElementById("ss");
	    if (this.numobj != null) 
	    {
	        this.numobj.style.opacity = 0.1;
	        this.numobj.style.filter='alpha(opacity=10)';
	        this.numobj.innerHTML = this.dspArr[this.dspnum];
	    }
  	    setTimeout(this.fadeIn,100);
	}
	
	this.fadeIn = function ()
	{
	    if (selfRef.opc < 101)
	    {
		if (selfRef.numobj != null)
		{
		    selfRef.numobj.style.opacity = (selfRef.opc > 99 ? 0.99 : selfRef.opc/100);
		    selfRef.numobj.style.filter='alpha(opacity='+ selfRef.opc + ')';
		}

		selfRef.opc += 4;
		setTimeout(selfRef.fadeIn,100);
	    }
	    else
	    {
		if (selfRef.numobj != null) {selfRef.numobj.innerHTML = "";}
		selfRef.dspnum++;
		selfRef.showNumber();	
	    }
	}	


	this.checkClick = function (n)
	{
	    this.clickNum++;
	    var clr = "#ff4500";
	    for (var i = 0; i < this.dspArr.length; i++)
	    {
	        if (this.sqArr[n] == this.dspArr[i]) {this.score++;clr = "#00ff7f";break;}
	    }

	    var o = document.getElementById("s" + n);
	    if (o != null) {o.style.backgroundColor = clr;o.onclick=null;o.cursor="default";}
	    if (this.clickNum >= this.dspArr.length) 
	    {
	        for (var i = 0; i < this.sqArr.length; i++) {o = document.getElementById("s" + i);if (o != null) {o.onclick=null;}}
	        gameOver(Math.floor(100*this.score/this.dspArr.length+0.5));
	    }
	 }
	 
}

//-->
