if (document.images) 
{
q = new Image(80,80);
q.src = "g/i/q.gif";
   
for(pi=0;pi<18;pi++){
pi = new Image(80,80);

pi.src = "g/i/" + pi + ".gif";
}
}

/*
/*
 * Title  -> JavaScript Pairs
 * Author -> Paul Gration (Gratz)
 * URL    -> http://www.i-labs.org
 * Email  -> paulg(at)i-labs.org
 */

function Pairs() {

	var board;
	var cellOne = null;
	var cellTwo = null;
	var randomNum;
	var array;
	var store;

	this.Pairs = function(rows, cols) {

		document.write("<table height='480' id=\"board\">");
		document.write("	<tbody></tbody>");
		document.write("</table>");

		board = document.getElementById("board").getElementsByTagName("tbody")[0];
		array = new Array();
		store = new Array();
		for(a = 0; a < 2; a++) {
			for(b = 0; b < ((rows*cols)/2); b++) {
				array.push(b);
			}
		}

		for(i = 0; i < rows; i++) {
			board.appendChild(document.createElement("tr"));
			for(j = 0; j < cols; j++) {
				randomNum = Math.round(Math.random()*(array.length-1));
				store["x" + i + "y" + j] = array[randomNum];
				array.splice(randomNum, 1);
				board.childNodes[i].appendChild(document.createElement("td")).appendChild(document.createElement("a"));
				board.childNodes[i].childNodes[j].setAttribute("class","q");
				board.childNodes[i].childNodes[j].childNodes[0].onclick = this.check;
				//board.childNodes[i].childNodes[j].childNodes[0].onmouseout = this.ClearFounds;
				board.childNodes[i].childNodes[j].childNodes[0].setAttribute("id","x" + i + "y" + j);
				board.childNodes[i].childNodes[j].childNodes[0].setAttribute("href","#");
				board.childNodes[i].childNodes[j].childNodes[0].appendChild(document.createElement("img"));
				board.childNodes[i].childNodes[j].childNodes[0].childNodes[0].setAttribute("src","g/i/q.gif");
				board.childNodes[i].childNodes[j].childNodes[0].childNodes[0].setAttribute("width","80");
				board.childNodes[i].childNodes[j].childNodes[0].childNodes[0].setAttribute("height","80");
				board.childNodes[i].childNodes[j].childNodes[0].childNodes[0].setAttribute("border","0");
			}
		}
	}

//#########################################################################################
//# Click a cell
//#########################################################################################
	this.check = function(event) {

	NoMatch = 0;
		if((cellOne == null) || (cellTwo == null)) {

//		var CurClicks = eval(document.getElementById("clicks").getAttribute("innerHTML"));
		var CurClicks = eval(document.getElementById("clicks").innerHTML);
		//alert (CurClicks);
		if(CurClicks > 0)	{
					document.getElementById("clicks").innerHTML = CurClicks -1;
					if(CurClicks==100){clock();};
					}

				}
				
		if((cellOne != null) && (cellTwo != null)) {
			var cellOneId = cellOne.getAttribute("id");
			var cellTwoId = cellTwo.getAttribute("id");
			

		if(store[cellOneId] == store[cellTwoId]) {

		}
			if(store[cellOneId] != store[cellTwoId]) {
				MyOne = document.getElementById(cellOneId).childNodes[0].getAttribute("src");
				MyClassOne = document.getElementById(cellOneId).childNodes[0].getAttribute("class");
				if(MyClassOne!="found"){document.getElementById(cellOneId).parentNode.replaceChild(cellOne, document.getElementById(cellOneId))};

				MyTwo = document.getElementById(cellTwoId).childNodes[0].getAttribute("src");
				MyClassTwo = document.getElementById(cellTwoId).childNodes[0].getAttribute("class");
				if(MyClassTwo!="found"){document.getElementById(cellTwoId).parentNode.replaceChild(cellTwo, document.getElementById(cellTwoId))};

			}
			cellOne = null;
			cellTwo = null;

		}


		var current = document.createElement(this.tagName);
		current.setAttribute("id", this.getAttribute("id"));
		current.setAttribute("href", "#");
		current.onclick = this.onclick;

		current.appendChild(document.createElement("img"));
		current.childNodes[0].setAttribute("src","g/i/q.gif");
		current.childNodes[0].setAttribute("width","80");
		current.childNodes[0].setAttribute("height","80");
		current.childNodes[0].setAttribute("border","0");


		if(cellOne == null) {
			cellOne = current;
		} else {
			cellTwo = current;
		}


		
		
		this.removeAttribute("href");
		this.onclick = null;

		

		this.childNodes[0].setAttribute("src","g/i/" + store[this.getAttribute("id")] + ".gif");
		this.childNodes[0].setAttribute("width","80");
		this.childNodes[0].setAttribute("height","80");


if((cellOne != null) && (cellTwo != null)) {
	var cellOneId = cellOne.getAttribute("id");
	var cellTwoId = cellTwo.getAttribute("id");

	var StayOne =  store[cellOne.getAttribute("id")];
	var StayTwo =  store[cellTwo.getAttribute("id")];

	var HowClose = CloseMatch(store, cellOneId, cellTwoId);

	if(HowClose==1)
		{
		document.getElementById(cellOneId).childNodes[0].setAttribute("src","g/f/" + StayOne +".gif");
		document.getElementById(cellOneId).childNodes[0].setAttribute("class","found");

		document.getElementById(cellTwoId).childNodes[0].setAttribute("src","g/f/" + StayTwo +".gif");
		document.getElementById(cellTwoId).childNodes[0].setAttribute("class","found");
		}
	
		if(HowClose==10)
		{
		document.getElementById(cellOneId).childNodes[0].setAttribute("src","g/m/" + StayOne +".gif");
		document.getElementById(cellOneId).childNodes[0].setAttribute("class","found");

		document.getElementById(cellTwoId).childNodes[0].setAttribute("src","g/m/" + StayTwo +".gif");
		document.getElementById(cellTwoId).childNodes[0].setAttribute("class","found");
		}

		if(HowClose>0)
		{
		if(IsLastOne()){ShowScore()};
		}
	
}

	}

//	ShowInfo();
}



//#########################################################################################
//# Show info
//#########################################################################################

function ShowInfo(){
var imgs = document.getElementsByTagName('img');
var k=0;
	for (var i=0;i<imgs.length;i++)
	{

		var q = imgs[i].src.substring(imgs[i].src.lastIndexOf('/'));
alert(q);
		if(q=="/q.gif"){k++;}else{}
	}
if(k==36){		
	document.getElementById("help").innerHTML = "<i>A woman with </i>";
	}
}



//#########################################################################################
//# Check For End
//#########################################################################################

function IsLastOne(){
var imgs = document.getElementsByTagName('img');
var LastOne = true;

	for (var i=0;i<imgs.length;i++)
	{

		var q = imgs[i].src.substring(imgs[i].src.lastIndexOf('/'));
		if(q=="/q.gif"){LastOne = false;}
	}
if(LastOne){document.getElementById("help").innerHTML =""};

return LastOne;
}


//#########################################################################################
//# Compare images
//#########################################################################################
function CloseMatch(store, cellOneId , cellTwoId ){
a = store[cellOneId];
b = store[cellTwoId];
MyHelp = "";
MyScore = 0;
s = ""

if(a==b)	{
		MyScore = 10;
		MyHelp = "A perfect match";
		s = "s"
		}else{
		

			if(a>0&&a<8){
				if(b>0&&b<8)	{
					MyScore = 1;
					MyHelp = "A woman with blonde hair, and somebody like her";
						}
			};

			if(a>7&&a<13)	{
				if(b>7&&b<13)	{	
					MyScore = 1;
					MyHelp = "A woman with black hair, and somebody like her";
						}
					}

			if(a>12&&b>12)	{
					MyScore = 1;
					MyHelp = "A woman with brown hair, and somebody like her";
					};

		}
	if(MyScore>0)	{
			document.getElementById("score").innerHTML = eval(document.getElementById("score").innerHTML)+MyScore;
			document.getElementById("help").innerHTML = "<i>" + MyHelp + ": " + MyScore + " point" + s + "</i>"
			}
			else{
			
			}
return MyScore;
}

//#########################################################################################
//# Show Score
//#########################################################################################
function ShowScore(){
var MyName = "AAA";
var TotalScore = "100";
fScore = eval(document.getElementById("score").getAttribute("InnerText"));
cScore = eval(document.getElementById("clicks").getAttribute("InnerText"));
tScore = eval(document.getElementById("timer").getAttribute("InnerText"));
TotalScore = eval(fScore + cScore + tScore);
document.getElementById("help").innerHTML = "<i> g a m e &nbsp; o v e r</i>"
document.getElementById("total").innerHTML = fScore + cScore + tScore;

if(document.getElementById("s10")!=null){
if(TotalScore>=eval(document.getElementById("s10").getAttribute("InnerText"))){
	if(TotalScore>=eval(document.getElementById("s1").getAttribute("InnerText"))){MyWinner = "<br>and email address<br><input type=text name='e' size=20>";}else{MyWinner=""};
document.getElementById("ResultText").innerHTML = "You made it onto the high score table.<br><br>Enter your name<br><input type='text' name='n' value='AAAAAAAAAA' maxlength='10' size='15'>" + MyWinner + "<br><input type='submit' id='SubStart' value='SUBMIT'>";

}else{
document.getElementById("ResultText").innerHTML = "Sorry, you didn't make it onto the high score table.<br><br><br><br><br><input type='submit' id='SubStart' value='RESTART'>";
}
}

document.forms[0].s.value = TotalScore;

//document.all.ResultText.style.visibility= document.all.ResultText.style.visibility=='hidden' ? '' : 'hidden';
}

//#########################################################################################
//# Get hair colour
//#########################################################################################
function GetColour(a){


			if(a==0)	{
				MyColour = "red";
					};
			

			if(a>0&&a<8)	{
				MyColour = "blonde";
					};

			if(a>7&&a<13)	{
				MyColour = "black";
					}

			if(a>12)	{
				MyColour = "brown";
					};

return MyColour;
}





//#########################################################################################
//# Count Down Time Bonus
//#########################################################################################
function clock() {
//if (!document.layers && !document.all) return;
var digital = new Date();
var hours = digital.getHours();
var minutes = digital.getMinutes();
var seconds = digital.getSeconds();
var amOrPm = "AM";
if (hours > 11) amOrPm = "PM";
if (hours > 12) hours = hours - 12;
if (hours == 0) hours = 12;
if (minutes <= 9) minutes = "0" + minutes;
if (seconds <= 9) seconds = "0" + seconds;
dispTime =  seconds;
if (document.layers) {
document.layers.timer.document.write(dispTime);
document.layers.timer.document.close();
}
else
//if (document.all)
if(eval(document.getElementById("timer").innerHTML)>0&&document.getElementById("total").innerHTML==""){
document.getElementById("timer").innerHTML = eval(document.getElementById("timer").innerHTML) - 2;
}
setTimeout("clock()", 2000);
}