
var cl = new Array("cl0","cl1","cl2","bound3","bound4","bound1","bound2","num","numtable");
var mouseButton = 0;
var curClass = "";
var isModified = new Boolean(false);
var cellWidth = 10;
var cellHeight = 12;
document.writeln("<img src=3x3_1.gif height='0' width='0'>");
function paletteMouseDown()
{
	mouseButton = window.event.button;
	if (mouseButton < 1 || mouseButton > 2) return;

	var cell = window.event.srcElement;
	if (mouseButton == 2)
	{
		if (cell.style.backgroundImage == "none" || cell.style.backgroundImage == "")
			cell.style.backgroundImage = "url(3x3_1.gif)";
		else
			cell.style.backgroundImage = "none";
	}

	window.event.returnValue = false;
	window.event.cancelBubble = true;
}

function mouseDown()
{
	mouseButton = window.event.button;
	if (mouseButton < 1 || mouseButton > 2) return;

	var cell = window.event.srcElement;
	if (mouseButton == 1)
	{
	  if (cell.className == cl[1])
			cell.className = cl[0];
		else
			cell.className = cl[1];
	}
	else
	if (mouseButton == 2)
	{
		if (cell.className == cl[2])
			cell.className = cl[0];
		else
			cell.className = cl[2];
	}
	curClass = cell.className;
	isModified = true;
	window.event.returnValue = false;
	window.event.cancelBubble = true;
}

function mouseOver()
{
	if (curClass != "")
		window.event.srcElement.className = curClass;
	window.event.returnValue = false;
	window.event.cancelBubble = true;
}

function mouseUp()
{
	mouseButton = 0;
	curClass = "";
	window.event.returnValue = false;
	window.event.cancelBubble = true;
}

function f1()
{
	window.event.returnValue = false;
	window.event.cancelBubble = true;
}

function storeCookie(cid)
{
	var cur=-1;
	var len=0;
	var type=0;
	cookie="";
	for(j=0;j<net.rows.length;j++)
		for(i=0;i<net.rows[j].cells.length;i++)
		{
			if (net.rows[j].cells[i].className == cl[0]) type = 0;
			else
			if (net.rows[j].cells[i].className == cl[1]) type = 1;
			else
			if (net.rows[j].cells[i].className == cl[2]) type = 2;
			else continue;
			if (cur < 0) cur = type;
			if (cur == type) len++
			else
			{
				cookie = cookie + cur + len + ",";
				cur = type;
				len = 1;
			}
		}
	cookie = cookie + cur + len + ",";
	setCookie(cid, cookie);
	isModified = false;
}

function clearNet()
{
	for(j=0;j<net.rows.length;j++)
		for(i=0;i<net.rows[j].cells.length;i++)
			if (net.rows[j].cells[i].className == cl[1] ||
			    net.rows[j].cells[i].className == cl[2])
				net.rows[j].cells[i].className = cl[0];
	for(j=0;j<topNum.rows.length;j++)
		for(i=0;i<topNum.rows[j].cells.length;i++)
			topNum.rows[j].cells[i].style.backgroundImage = "none";
	for(j=0;j<leftNum.rows.length;j++)
		for(i=0;i<leftNum.rows[j].cells.length;i++)
			leftNum.rows[j].cells[i].style.backgroundImage = "none";
	isModified = true;
}

function drawTopNum(num1,num2)
{
	var widthNet = (cellWidth + 1) * num1[0].length + Math.ceil(num1[0].length / 5 - 1) + 1;
	var heightNet = cellHeight * num1.length;
	document.write("<table id=topNum class="+cl[8]+" cellspacing=0 cellpadding=0 width="+widthNet+" height="+heightNet+">");
	for(j=0;j<num1.length;j++)
	{
		document.write("<tr>");
		for(i=0;i<num1[j].length;i++)
		{
			if (i%5 == 0 && i>0)
				document.write("<td class="+cl[6]+"><img src=/images/1x1.gif width=1 height=1></td>");
			if (num1[j][i]==0)
				document.write("<td class="+cl[7]+">&nbsp</td>");
			else
				document.write("<td class="+cl[7]+"	title='"+num1[j][i]+"' onmousedown='paletteMouseDown()'>"+num1[j][i]+"</td>");
		}
		document.write("</tr>");
	}
	document.write("</table>");
}

function drawLeftNum(num1,num2)
{
	var widthNet = (cellWidth + 1) * num2.length;
	var heightNet = cellHeight * num2[0].length + Math.ceil(num2[0].length / 5 - 1) + 1;
	document.write("<table id=leftNum class="+cl[8]+" cellspacing=0 cellpadding=0 width="+widthNet+" height="+heightNet+">");
	for(j=0;j<num2[0].length;j++)
	{
		if (j%5 == 0 && j>0)
			document.write("<tr><td class="+cl[5]+"><img src=/images/1x1.gif width=1 height=1></td></tr>");
		document.write("<tr>");
		for(i=0;i<num2.length;i++)
			if (num2[i][j]==0)
				document.write("<td class="+cl[7]+">&nbsp</td>");
			else
				document.write("<td class="+cl[7]+" title='"+num2[i][j]+"' onmousedown='paletteMouseDown()'>"+num2[i][j]+"</td>");
		document.write("</tr>");
	}
	document.write("</table>");
}

function getNextBlock(cookie,i)
{
	var j = cookie.indexOf(",",i);
	if (j > 0) return cookie.substr(i, j-i);
	else return "";
}

function getType(block)
{
	if (block == "") return -1;
	return block.substr(0, 1);
}

function getLength(block)
{
	if (block == "") return -1;
	return block.substr(1);
}

function drawNet(num1,num2,cid)
{
	var cookie=getCookie(cid);
//	var cookie=getCookie(document.location);
	var cur=0,block,curType=-1,curLen;
	if (cookie != "")
	{
		block=getNextBlock(cookie,cur);
		curType=getType(block);
		curLen=getLength(block);
		cur+=block.length+1;
	}

	document.write("<table id=net cellspacing=0 cellpadding=0 class="+cl[4]+">");
	for(j=0;j<num2[0].length;j++)
	{
		if (j%5 == 0 && j>0)
			document.write("<tr class="+cl[4]+"></tr>");
		document.write("<tr>");
		for(i=0;i<num1[0].length;i++)
		{ 
			if (curType >= 0 && curLen <= 0)
			{
				block=getNextBlock(cookie,cur);
				curType=getType(block);
				curLen=getLength(block);
				cur+=block.length+1;
			}
			if (i%5 == 0 && i>0)
				document.write("<td class="+cl[3]+" height=2 width=1><img src=/images/1x1.gif width=1 height=1></td>");
			var c1 = cl[0];
			if (curType>=0)
			{
				if (curType == 0) c1 = cl[0];
				if (curType == 1) c1 = cl[1];
				if (curType == 2) c1 = cl[2];
			}
			document.write("<td id=id"+i+"_"+j+" class="+c1+" onmouseup='mouseUp()' onmousedown='mouseDown()' onmouseover='mouseOver()' height=12 width=10>X</td>");
			--curLen;
		}
		document.write("</tr>");
	}
	document.write("</table>");
}

function drawCrossword(num1,num2,cid)
{
	document.write("<table style='border: 1px #98AAB1 solid;' cellspacing='0' cellpadding='0' align='center' class='alltext' oncontextmenu='f1()' onselectstart='f1()'>");
	document.write("<tr><td id='indicator' align='center' style='background:#FAEBD7;' oncontextmenu='f1()'>&nbsp</td><td>");
	drawTopNum(num1,num2);
	document.write("</td></tr><tr><td>");
	drawLeftNum(num1,num2);
	document.write("</td><td>");
	drawNet(num1,num2,cid);
	document.write("</td></tr></table>");
}

