//<!-- 
//****************
// File:     /js/functions.js
// Version:  2.1.3.3
// Date:     11/02/2007
// Last edited by:   Andrew Cohen
// Comments: generic functions
// Notes: --
//****************

//* coToLet
//* Description: converts co-ordinate to letter
//* input 1: Co-ordinate
//* output: Letter
function coToLet(inCo)
{
	if (inCo > 8)
	{
		outLet = String.fromCharCode(inCo+97);
	}
	if (inCo <= 8)
	{
		outLet = String.fromCharCode(inCo+96);
	}
	return (outLet);
}

//* letToCo
//* Description: converts letter to co-ordinate
//* input 1: Letter
//* output: Co-ordinate
function letToCo(inLet)
{
	if (inLet.charCodeAt(0) > 105)
	{
		outCo=inLet.charCodeAt(0)-97;
	}
	else
	{
		outCo=inLet.charCodeAt(0)-96;
	}
	return (outCo);
}

//* buildRefs
//* Description: builds up state references previously held in hidden form inputs ref[coord] and var[coord], such as "black","white","empty"
//* Notes:
//* Required Declarations:requires board images to have an id of their co-ordinate
//* Global Variables/functions:  boardSize, coToLet

function buildRefs()
{
	for (x = 1; x <= boardSize; x++) 
	{
		for (y = 1; y <= boardSize; y++) {
			if (document.getElementById(coToLet(x)+(y)).src.search(/empty/) > -1)
			{
				self["var"+coToLet(x)+(y)] = "empty";
			}
			else if (document.getElementById(coToLet(x)+(y)).src.search(/black/) > -1)
			{
				self["var"+coToLet(x)+(y)] = "black";
			}
			else if (document.getElementById(coToLet(x)+(y)).src.search(/white/) > -1)
			{
				self["var"+coToLet(x)+(y)] = "white";
			}
			
			if (document.getElementById("dead"+coToLet(x)+(y)))
			{
				if (document.getElementById("dead"+coToLet(x)+(y)).value == "1")
				{
					self["var"+coToLet(x)+(y)] = (self["var"+coToLet(x)+(y)]+"_dead");
				}
			}

		}
	}
	self["var--"] = "empty";
	ready = 1;
}

//* swapColour
//* Description: switches colours
//* input 1: colour
//* output: other colour
function swapColour(colour)
{
	if (colour == "white")	//gets other colour 
	{
		oppColour = "black";
	}
	else
	{
		oppColour = "white";
	}
	return (oppColour);
}

//* formToggleAll
//* Description: checks all the boxes in a given form
function formToggleAll(inputFormName)
{
	window.isAlTicked=!window.isAlTicked;
	window.isSystemTicked=false;
	if(document.getElementById('toggleSystemInput')) {
		document.getElementById('toggleSystemInput').checked = false;
	}
	inputForm = document.getElementById(inputFormName);
	for(var i=0;inputForm.elements[i];i++)
	{
	if (inputForm.elements[i].type=='checkbox')
		{
			if(inputForm.elements[i].id != 'toggleSystemInput') {
				inputForm.elements[i].checked=window.isAlTicked;
			}
		}
	}
}
//* formToggleSystems
//* Description: checks all the boxes in a given form
function formToggleSystems(inputFormName)
{
	window.isAlTicked=false;
	window.isSystemTicked=!window.isSystemTicked;
	if(document.getElementById('toggleAllInput')) {
		document.getElementById('toggleAllInput').checked = false;
	}
	inputForm = document.getElementById(inputFormName);
	for(var i=0;inputForm.elements[i];i++)
	{
		if (inputForm.elements[i].type=='checkbox')
		{
			if(inputForm.elements[i].id != 'toggleAllInput') {
				inputForm.elements[i].checked=window.isSystemTicked && (inputForm.elements[i].id == 'systemMessage' || inputForm.elements[i].id == 'toggleSystemInput');
			}
		}
	}
}

//* toggleDisplayThing
//* Description: hides/shows given ID thing 
//option: 1 = block
//option: 2 = inline
function toggleDisplayThing(inputObject,inputPrefix,option)
{
	inObj = document.getElementById(inputObject).style;
	inObjLink = document.getElementById("link"+inputObject);
	if (inObj.display == 'none')
	{
		if (option == 1)
		{
			inObj.display = 'block';
		}
		else if (option == 2)
		{
			inObj.display = 'inline';
		}
		inObjLink.innerHTML = 'Hide '+inputPrefix;
	}
	else 
	{
		inObj.display = 'none';
		inObjLink.innerHTML = 'Show '+inputPrefix;
	}
}

function helpBox(e,title,desc)
{
    document.getElementById('helpbox').innerHTML = "<table class='dhpopup' border='0'><tr><td class='dhpopuptitle'>" + title + "</td></tr><tr><td class='dhpopuptext'>" + desc + "</td></tr></table>";

	if (IE)
	{
		coordX = event.clientX + document.body.scrollLeft;
		coordY = event.clientY + document.body.scrollTop;
	}
	else
	{
		coordX = e.pageX;
		coordY = e.pageY;
	}
	
     var t = document.getElementById('helpbox').style;
     t.left = coordX+15+"px";
     t.top = coordY+15+"px";
     t.visibility = 'visible';
}

function clearHelpBox()
{
     document.getElementById('helpbox').style.visibility = 'hidden';
}

function getLeft(l)
{
  if (l.offsetParent) return (l.offsetLeft + getLeft(l.offsetParent));
  else return (l.offsetLeft);
}
function getTop(l)
{
  if (l.offsetParent) return (l.offsetTop + getTop(l.offsetParent));
  else return (l.offsetTop);
}

function fillBuddyText(username)
{
	document.getElementById('recipient').value = username;
	document.getElementById('buddyList').style.visibility = 'hidden';
}

function toggleBList()
{
	bl = document.getElementById('buddyList');	//buddy list
	if (bl.style.visibility == 'visible')
	{
		bl.style.visibility = 'hidden';
	}
	else
	{
		blb = document.getElementById('buddyListButton'); //buddy list button
		bl.style.top = getTop(blb) + blb.offsetHeight + "px";
		bl.style.left = getLeft(blb) + "px";
		bl.style.visibility = 'visible';
	}
}

function fillAssignText(username)
{
	document.getElementById('assignUserName').value = username;
	document.getElementById('assignList').style.visibility = 'hidden';
}
function toggleAssignList()
{
	bl = document.getElementById('assignList');
	if (bl.style.visibility == 'visible')
	{
		bl.style.visibility = 'hidden';
	}
	else
	{
		blb = document.getElementById('assignListButton');
		bl.style.top = getTop(blb) + blb.offsetHeight + "px";
		bl.style.left = getLeft(blb) + "px";
		bl.style.visibility = 'visible';
	}
}

function toggleAddGameComments()
{
	inObj = document.getElementById('addBoardComments').style;
	if (inObj.display == 'none')
	{	inObj.display = 'inline';
		document.getElementById('linkAddBoardComments').innerHTML = "Don't Add Comment";}
	else 
	{	inObj.display = 'none';
		document.getElementById('linkAddBoardComments').innerHTML = 'Add Comment';}
}

// -->