// -_-_-_-_-_-_-_-_-_-_-_-_-  FARSI TYPE V1.2  -_-_-_-_-_-_-_-_-_-_-_-_-
//
//  to type farsi in web forms in Internet Explorer, Gecko family (Mozilla/FireFox) and now in Opera.
//  This script was created by k@\/Eh Ahmadi.
//  kaveh[at]ashoob[dot]net.
//
// Original idea is from:      
// who really knows? tell us!
//
// Mozilla solving problem idea is from:
// http://forum.persiantools.com/showthread.php?p=667351
//
// auto Creation idea is from:
// http://ip.webkar.com/forums/index.php?act=ST&f=19&t=99
// with a look at behdad.org/editor and blogfa.com farsi solutions!
//
// ALL CODES HAVE BEEN EXPANDED BY ME!
// if you want to use this code PLEASE send me a note  and please keep this note intact
// (C) 2002-2006,
//
// -_-_-_-_-_-_-_-_-_-_-_-_-  ChangeLog  -_-_-_-_-_-_-_-_-_-_-_-_-
//
// V 1.1
//   - Some little Gecko fixes
// V 1.2
//   - opera full support added
//
// -_-_-_-_-_-_-_-_-_-_-_-_-  Usage Sample  -_-_-_-_-_-_-_-_-_-_-_-_-
//
// include the "farsitype.js" file to your <head> tag:
// <script language="javascript" src="farsitype.js" type="text/javascript"></script>
//
// add "lang" attribute with value "fa" to any <input> or <textarea> tags that you want to have FarsiType!
// <input type="text" name="whatever" lang="fa" /> or <textarea cols="30" rows="7" name="whatever" lang="fa"></textarea>
// (only the lang="fa" is important for FarsiType!)
//
// you can also have an enable/disable checkbox. (it's not necessary)
// <input  type="checkbox" id="disableFarsiType" />
//
// you can use F8 keybord button to change language instead of the change language button.


// insertAdjacentHTML(), insertAdjacentText() and insertAdjacentElement() for Netscape 6/Mozilla by Thor Larholm me@jscript.dk
if(typeof HTMLElement!="undefined" && ! HTMLElement.prototype.insertAdjacentElement) {
	HTMLElement.prototype.insertAdjacentElement = function (where,parsedNode) {
		switch (where) {
			case 'beforeBegin':
				this.parentNode.insertBefore(parsedNode,this)
				break;
			case 'afterBegin':
				this.insertBefore(parsedNode,this.firstChild);
				break;
			case 'beforeEnd':
				this.appendChild(parsedNode);
				break;
			case 'afterEnd':
				if (this.nextSibling)
					this.parentNode.insertBefore(parsedNode,this.nextSibling);
				else
					this.parentNode.appendChild(parsedNode);
				break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr) {
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}

	HTMLElement.prototype.insertAdjacentText = function (where,txtStr) {
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
}
		
var FarsiType = 
{
	// Farsi keyboard map based on Iran Popular Keyboard Layout
	farsiKey : [
		32    , 33    , 34    , 35    , 36    , 37    , 1548  , 1711  ,
		41    , 40    , 215   , 43    , 1608  , 45    , 46    , 47    ,
		48    , 49    , 50    , 51    , 52    , 53    , 54    , 55    ,
		56    , 57    , 58    , 1705  , 44    , 61    , 46    , 1567  ,
		64    , 1616  , 1584  , 125   , 1609  , 1615  , 1609  , 1604  ,
		1570  , 247   , 1600  , 1548  , 47    , 8217  , 1583  , 215   ,
		1563  , 1614  , 1569  , 1613  , 1601  , 8216  , 123   , 1611  ,
		1618  , 1573  , 126   , 1580  , 1688  , 1670  , 94    , 95    ,
		1662  , 1588  , 1584  , 1586  , 1740  , 1579  , 1576  , 1604  ,
		1575  , 1607  , 1578  , 1606  , 1605  , 1574  , 1583  , 1582  ,
		1581  , 1590  , 1602  , 1587  , 1601  , 1593  , 1585  , 1589  ,
		1591  , 1594  , 1592  , 60    , 124   , 62    , 1617
	],
	Type : true,
	counter : 0
}

FarsiType.enable_disable = function(Dis) {
	var invis, obj;
	
	if (!Dis.checked)  {
		FarsiType.Type = true;
		invis = 'visible';
	}
	else {
		FarsiType.Type = false;
		invis = 'hidden';
	}

	for (var i=1; i<= FarsiType.counter; i++) {
		obj = document.getElementById('FarsiType_button_' + i);
		obj.style.visibility = invis;
	}
}

FarsiType.init = function() {

	var Inputs = document.getElementsByTagName('INPUT');
	for (var i=0; i<Inputs.length; i++) {
		if (Inputs[i].type.toLowerCase() == 'text' && Inputs[i].lang.toLowerCase() == 'fa') {
			FarsiType.counter++;
			new FarsiType.KeyObject(Inputs[i], FarsiType.counter);
		}
	}

	var Areas = document.getElementsByTagName('TEXTAREA');
	for (var i=0; i<Areas.length; i++) {
		if (Areas[i].lang.toLowerCase() == 'fa') {
			FarsiType.counter++;
			new FarsiType.KeyObject(Areas[i], FarsiType.counter);
		}
	}
	
	var Dis = document.getElementById('disableFarsiType')
	if (Dis != null) {
		FarsiType.enable_disable (Dis);
		Dis.onclick = new Function( "FarsiType.enable_disable (this);" )
	}
}

FarsiType.KeyObject = function(z,x) {

	z.insertAdjacentHTML("afterEnd", "<input type='text' style='display: none;' id=FarsiType_button_"+x+" value='FA' />");
	z.bottelm = document.getElementById ('FarsiType_button_' + x);
	
	z.farsi = true;
	z.style.textAlign = "right";
	z.style.direction = "rtl";
	z.bottelm.title = 'Change lang to english';

	setSelectionRange = function(input, selectionStart, selectionEnd) {
		input.focus()
		input.setSelectionRange(selectionStart, selectionEnd)
	}

	isChangeLang = function(e) {
		if (e == null) e = window.event;
		var key = e.keyCode ? e.keyCode : e.charCode;
		if (key == 119) ChangeLang();
	}
	
	ChangeLang = function() {
		if (z.farsi) {
			z.style.textAlign = "left";
			z.style.direction = "ltr";
			z.farsi = false;
			z.bottelm.value = "EN";
			z.bottelm.title = 'Change lang to persian'
		}
		else {
			z.style.textAlign = "right";
			z.style.direction = "rtl";
			z.farsi = true;
			z.bottelm.value = "FA";
			z.bottelm.title = 'Change lang to english'
		}
		z.focus();
	}
	
	Convert = function(e) {

		if (FarsiType.Type) {

			if (e == null) e = window.event;
			eElement = (e.srcElement) ? e.srcElement : e.originalTarget;
			
			var key = e.keyCode ? e.keyCode : e.charCode;
			if (navigator.userAgent.toLowerCase().indexOf('opera')>-1) key = e.which;
			
			if ( (e.charCode != null) && (e.charCode != key) )	return;
			if (e.ctrlKey || e.altKey || e.metaKey || key == 13 || key == 27 || key == 8) return;

			//check windows lang
			if (key>128){
				alert("Please change your windows language to English");
				return false;
			}

			// if Farsi
			if (z.farsi && key > 31 && key < 128) {

				//check CpasLock
				if ( (key >= 65 && key <= 90) && !e.shiftKey ) {
					alert("Caps Lock is On. To prevent entering farsi incorrectly, you should press Caps Lock to turn it off.");
					return false;
				}
				else if ( (key >= 97 && key <= 122 ) && e.shiftKey ) {
					alert("Caps Lock is On. To prevent entering farsi incorrectly, you should press Caps Lock to turn it off.");
					return false;
				}

				// Shift-space -> ZWNJ
				if (key == 32 && e.shiftKey)
					key = 8204;
				else
					key = FarsiType.farsiKey[key-32];

				// to farsi
				try {
					// IE
					e.keyCode = key
				}
				catch(error) {
					try {
						// Gecko before
						e.initKeyEvent("keypress", true, true, document.defaultView, false, false, true, false, 0, key, eElement);
					}
					catch(error) {
						try {
							// Gecko & Opera now
							var nScrollTop = eElement.scrollTop;
							var nScrollLeft = eElement.scrollLeft;
							var nScrollWidth = eElement.scrollWidth;

							replaceString = String.fromCharCode(key);

							var selectionStart = eElement.selectionStart;
							var selectionEnd = eElement.selectionEnd;
							eElement.value = eElement.value.substring(0, selectionStart) + replaceString + eElement.value.substring(selectionEnd);
							setSelectionRange(eElement, selectionStart + replaceString.length, selectionStart + replaceString.length);

							var nW = eElement.scrollWidth - nScrollWidth;
							if (eElement.scrollTop == 0) { eElement.scrollTop = nScrollTop }

							e.preventDefault()
						}
						catch(error) {
							// else no farsi type!
							alert('Sorry! no FarsiType support')
							FarsiType.Type = false;

							var Dis = document.getElementById('disableFarsiType')
							if (Dis != null) {
								Dis.disabled = true;
								Dis.checked = true;
							}

							for (var i=1; i<= FarsiType.counter; i++) {
								document.getElementById('FarsiType_button_' + i).style.visibility = 'hidden';
							}
							return false;
						}
					}
				}
			}
		}
		return true;
	}

	z.bottelm.onmouseup = ChangeLang;
	z.onkeydown = isChangeLang;
	z.onkeypress = Convert;
}

if (window.attachEvent) {
	window.attachEvent('onload', FarsiType.init)
}
else if (window.addEventListener) {
	window.addEventListener('load', FarsiType.init, false)
}

