<!--
// Farsi keyboard map based on standard Windows 2000 keyboard

// Copyright by Grummelchen 2004
// Version 1.01
// For details contact me: grummel@grummelchen.de

var ButtonFarsi   = "  Farsi keyboard  ";
var ButtonEnglish = "English keyboard";

var FocusTextArea ="";
var FocusForm = "";
var LoadPage = 1;

var ArrayFarsi = [
//	BLANK	!	"	#	$	%	&	'	(	)
	0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x06AF, 0x0029, 0x0028,
//	*	+	,	-	.	/	0	1	2	3
	0x002A, 0x002B, 0x0648, 0x002D, 0x002E, 0x002F, 0x06F0, 0x06F1, 0x06F2, 0x06F3,
//	4	5	6	7	8	9	:	;	<	=
	0x06F4, 0x06F5, 0x06F6, 0x06F7, 0x06F8, 0x06F9, 0x003A, 0x06A9, 0x003C, 0x003D,
//	>	?	@	A	B	C	D	E	F	G
	0x003E, 0x061F, 0x0040, 0x064E, 0x0625, 0x0698, 0x0650, 0x064D, 0x0651, 0x0652,
//	H	I	J	K	L	M	N	O	P	Q
	0x0622, 0x005B, 0x0640, 0x00AB, 0x00BB, 0x0621, 0x0623, 0x005D, 0x005C, 0x064B,
//	R	S	T	U	V	W	X	Y	Z	[
	0x0020, 0x064F, 0x060C, 0x002C, 0x0624, 0x064C, 0x064A, 0x061B, 0x0629, 0x062C,
//	\	]	^	_	`	a	b	c	d	e
	0x067E, 0x0686, 0x005E, 0x005F, 0x00F7, 0x0634, 0x0630, 0x0632, 0x06CC, 0x062B,
//	f	g	h	i	j	k	l	m	n	o
	0x0628, 0x0644, 0x0627, 0x0647, 0x062A, 0x0646, 0x0645, 0x0626, 0x062F, 0x062E,
//	p	q	r	s	t	u	v	w	x	y
	0x062D, 0x0636, 0x0642, 0x0633, 0x0641, 0x0639, 0x0631, 0x0635, 0x0637, 0x063A,
//	z	{	|	}	~
	0x0638, 0x007B, 0x007C, 0x007D, 0x00D7
];

var BothLangKeys = [
        0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0028,
	0x0029, 0x002A, 0x002B, 0x002D, 0x002E, 0x002F,
	0x003A, 0x003C, 0x003D, 0x003E,
	0x0040,
	0x005E, 0x005F,
	0x007B, 0x007C, 0x007D
];


var R_String = '';
var bCorrectBrowser = false;

function SwitchLang()
{
	if (false == bCorrectBrowser) {
		return;
	}
	if (ButtonFarsi == FocusForm.LangButton.value) {
		FocusForm.LangButton.value = ButtonEnglish;
	}
	else {
		FocusForm.LangButton.value = ButtonFarsi;
	}
}



// change the Farsi

function SwitchKeys()
{
	var icount = 0;
	var ikey = window.event.keyCode;

	if (false == bCorrectBrowser) {
		return true;
	}

	// Is key at both keyboards at the the same place? = > Leave
        while (icount < BothLangKeys.length) {
        	if (BothLangKeys[icount] == ikey) {
        		return true;
        	}
        	icount++;

        }

	if (ButtonFarsi == FocusForm.LangButton.value) {
		// Farsi language

		// Shift && BLANK => special character
		if (ikey == 0x0020 && window.event.shiftKey) {
			window.event.keyCode = 0x200C;
		}
		// keys exists at both keboards, but at farsi combined with shift only
		else if (((ikey == 0x002C) || (ikey == 0x005B) ||
			  (ikey == 0x005C) || (ikey == 0x005D)) &&
			 window.event.shiftKey) {
			return true;
		}
		// special character 'R'
		else if (ikey == 0x0052) {
			var strinsert = String.fromCharCode(0x631,0x64A,0x627,0x644);
			if (FocusTextArea.createTextRange &&
			    FocusTextArea.caretPos) {
				var caretPos = FocusTextArea.caretPos;
				caretPos.text =
					caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? strinsert + ' ' : strinsert;
			}
			else {
				FocusTextArea.value  = strinsert;
			}
			return false;
		}
		// translate all other keys by list
		else if ((ikey >= 0x020) && (ikey <= 0x07E)) {
				window.event.keyCode = ArrayFarsi[ikey - 0x0020];
		}

	}
	else {
		// English language

		// keys exists at both keboards, but at english not combined with shift
		if (((ikey == 0x002C) || (ikey == 0x005B) ||
			  (ikey == 0x005C) || (ikey == 0x005D)) &&
			 !window.event.shiftKey) {
			return true;
		}
		// uppercase R is concatination of four farsi letters.
		else if ((R_String == '') && (ikey == 0x644) && window.event.shiftKey) {
			R_String = 'g';
			return true;
		}
		else if ((R_String == 'g') && (ikey == 0x627)) {
			R_String = 'gh';
			return true;
		}
		else if ((R_String == 'gh') && (ikey == 0x64A)) {
			R_String = 'ghX';
			return true;
		}
		else if ((R_String == 'ghX') && (ikey == 0x631)) {
			window.event.keyCode = 0x0052;
			return true;
		}

		// find farsi key in list.
		// If found, it is an farsi character => translate, otherwise it is an english one
		icount = 0;
		while (icount < ArrayFarsi.length) {
			if (window.event.keyCode == ArrayFarsi[icount]) {
				window.event.keyCode = icount + 0x0020;
				return true;
			}
			icount++;
		}
	}
	return true;
}

function storeCaret ()
{
	if (false == bCorrectBrowser) {
		return;
	}
	if (FocusTextArea.createTextRange) {
		FocusTextArea.caretPos = document.selection.createRange().duplicate();
	}
}

function LeaveKey()
{
	// searchs the whole sting for special characters e.g small i.
	var cbefore = '';
	var cafter = '';
	var strText = FocusTextArea.value;
	var icount = 0;
	var bChanged = false;

	if (false == bCorrectBrowser) {
		return true;
	}

	if (ButtonFarsi == FocusForm.LangButton.value) {
		while (icount < strText.length) {
			if ((0x06CC == strText.charCodeAt(icount)) ||
			    (0x064A == strText.charCodeAt(icount))) {
				cbefore = strText.charCodeAt(icount+1);
				if (((cbefore > 0x0620) && (cbefore < 0x064B)) || ((cbefore > 0x0670) && (cbefore < 0x06D6))) {
					if (strText.substr(icount,1) == String.fromCharCode(0x06CC)) {
						bChanged = true;
						strText = strText.substr(0, icount) +
						        String.fromCharCode(0x064A) +
					        	strText.substr(icount+1,strText.length);
					}
				}
				else {
					if (strText.substr(icount,1) == String.fromCharCode(0x064A)) {
						bChanged = true;
						strText = strText.substr(0, icount) +
						        String.fromCharCode(0x06CC) +
					        	strText.substr(icount+1,strText.length);
					}
				}
			}
			icount++;
		}
	}

	if (true == bChanged) {
		FocusTextArea.value = strText;
	}

	R_String = '';

	storeCaret();
}

function GetName(Text,Form)
{
	FocusTextArea = Text;
	FocusForm = Form;
	if (LoadPage == 1) {
		if (navigator.appName != "Microsoft Internet Explorer") {
			FocusForm.LangButton.value = "Button not supported";
			FocusForm.LangButton.style.visibility = "hidden";
			alert("Only Internet Explorer is supported at this site!");
		}
		else {
			bCorrectBrowser = true;
			SwitchLang();
		}
		LoadPage = 0;
	}
}
//-->