var gFontAlphabet;
var gFontDigits;
var gFontSymbols;
var gFont;
var gString;
var gToggle;

function InitFarsi()
{
	gFontAlphabet = new Array(
		1570, 1575, 1574, 1571, 1576, 1662, 1578, 1579, 1580, 1670, 1581, 1582, 1583, 1584, 1585, 1586, 1688, 1587,
		1588, 1589, 1590, 1591, 1592, 1593, 1594, 1601, 1602, 1705, 1711, 1604, 1605, 1606, 1608, 1607, 1740, 1600);

	gFontDigits = new Array(
		1776, 1777, 1778, 1779, 1780, 1636, 1781, 1782, 1638, 1783, 1784, 1785);

	gFontSymbols = new Array(
		1523, 1524, 1548, 1563, 1567, 1642, 1643, 46, 58);

	gFont = gFontAlphabet.concat(gFontDigits, gFontSymbols);

	gString = new String("");

	gToggle = 0;
}

function UpdateCursor()
{
	obj = document.getElementById('FarsiText');

	obj.innerHTML = gString;

	if (gToggle)
	{
		obj.innerHTML += "_";
	}
	gToggle ^= 1;

	setTimeout("UpdateCursor()", 250);
}

function WriteStringHTML()
{
	obj = document.getElementById('StringHTML');

	obj.innerHTML = gString;
}

function AddSpace()
{
	gString += "&nbsp";
	WriteStringHTML();
}

function AddNewline()
{
	gString += "&#10";
	WriteStringHTML();
}

function BackSpace()
{
	var i;

	i = gString.lastIndexOf("&");
	if (i != -1)
	{
		gString = gString.slice(0, i);
	}
	WriteStringHTML();
}

function ClearString()
{
	gString = gString.slice(0, 0);
	WriteStringHTML();
}

function AddChar(c)
{
	gString += "&#" + c;
	WriteStringHTML();
}

function WriteTableChar(c)
{
	document.writeln("<td align=center>");
	document.writeln("<font size=8>");
	document.writeln("<div id=" + c + " style=\"color='000000'\">");
	document.writeln("<a onclick=\"AddChar(" + gFont[c] + ")\">");
	document.writeln("<p onmousedown=\"this.style.color='00ff00'\" onmouseup=\"this.style.color='ff0000'\" onmouseover=\"this.style.color='ff0000'\" onmouseout=\"this.style.color='000000'\">");

	document.writeln("&#" + gFont[c]);

	document.writeln("</p>");
	document.writeln("</a>");
	document.writeln("</div>");
	document.writeln("</font>");
	document.writeln("</td>");
}

function WriteTableSpecial(str, func)
{
	document.writeln("<td align=center>");
	document.writeln("<font size=6>");
	document.writeln("<div id=" + str + " style=\"color='000000'\">");
	document.writeln("<a onclick=" + func + ">");
	document.writeln("<p onmousedown=\"this.style.color='00ff00'\" onmouseup=\"this.style.color='ff0000'\" onmouseover=\"this.style.color='ff0000'\" onmouseout=\"this.style.color='000000'\">");

	document.writeln(str);

	document.writeln("</p>");
	document.writeln("</a>");
	document.writeln("</div>");
	document.writeln("</font>");
	document.writeln("</td>");
}

function ShowKeyboard()
{
	var i, c;

	// alphabet
	document.writeln("<table border=1 align=center bgcolor=ffffe0>");
	document.writeln("<tr>");
	for (i = 0; i < 18; i++)
	{
		WriteTableChar(18 - 1 - i);
	}
	document.writeln("</tr><tr>");
	for (i = 0; i < 18; i++)
	{
		WriteTableChar(gFontAlphabet.length - 1 - i);
	}
	document.writeln("<tr>");
	document.writeln("</table>");

	c = gFontAlphabet.length;

	// digits
	document.writeln("<table border=1 align=center bgcolor=ffffe0>");
	document.writeln("<tr>");
	for (i = 0; i < gFontDigits.length; i++, c++)
	{
		WriteTableChar(c);
	}
	document.writeln("</tr>");
	document.writeln("</table>");

	// symbols
	document.writeln("<table border=1 align=center bgcolor=ffffe0>");
	document.writeln("<tr>");
	for (i = 0; i < gFontSymbols.length; i++, c++)
	{
		WriteTableChar(c);
	}
	document.writeln("</tr>");
	document.writeln("</table>");

	// specials
	document.writeln("<table border=1 align=center bgcolor=ffffe0>");
	document.writeln("<tr>");
	WriteTableSpecial("clear", "ClearString()");
//	WriteTableSpecial("enter", "AddNewline()");
	WriteTableSpecial("space", "AddSpace()");
	WriteTableSpecial("back", "BackSpace()");
	document.writeln("</tr>");
	document.writeln("</table>");
}
