function pic_is(imgname)
{
	if (!(document.images)) return "non compatibile browser";

	// eval("fn=window.document.playfield.p"+imgname+".src")
	fn=document.images["p"+imgname].src

	l=fn.length

	filename=fn.substring(l-5,l)

	return filename
}


function display_piece(piec)
{
	eval("window.document.playfield.p"+piec+".src= Img"+field_contains_piece[piec]+".src  ")
}

function hide_piece(piec)
{
	eval("window.document.playfield.p"+piec+".src= Img0.src ")
}

function hide()
{
	UcantMove=false
	hide_piece(first)
	hide_piece(second)
	second=0
	first=0
}

function clickon(field)
{
	if (UcantMove) return;
	if (!(pic_is(field)=="0.gif")) return;
	display_piece(field)
	if (first==0 || field==first)
 	{
		first=field;
		return;
	}
	UcantMove=true
	second=field
	moves++
	if(field_contains_piece[first]!=field_contains_piece[second]) 
	{
		setTimeout("hide()",400)
		return;
	}

	first=0
	second=0
	foundnumberofpairs++

	/// You can change the result display here or even can change the action

	if (foundnumberofpairs==numberofpairs){
		alert("CONGRATS!!! You have found all the matching pairs with "+moves+" tries");
		window.document.playfield.tries.value=moves;
		window.document.playfield.submit();
	}
	UcantMove=false;
}

function randomise()
{
	for (n=1;n<(tfieldsize+1);n++)
	{
		tmp=Math.floor(Math.random()*tfieldsize+1)
		swaptemp=field_contains_piece[tmp]
 		field_contains_piece[tmp]=field_contains_piece[n]
 		field_contains_piece[n]=swaptemp
	}

}


function initgame()
{
	foundnumberofpairs=0; //reset in init
	first=0
	second=0
	moves=0

	for (n=1;n<(tfieldsize+1);n++)
	{
		field_contains_piece[n]=Math.floor(n/2+0.5);
		hide_piece(n);
	}
	randomise()
	UcantMove=false
}