﻿function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();

}

var langFarsi=1;

function changeLang() {
    if (langFarsi == 0) {
    langFarsi = 1;
    return true;
  }
  else {
    langFarsi = 0;
    return true;
  }
}

function FKeyDown (txtFrm){
 var key = window.event.keyCode;
 if (key == 145){
    if (langFarsi == 0) {
      langFarsi = 1;
      return true;
    }
    else {
      langFarsi = 0;
      return true;
    }

}

}
function FKeyPress(txtFrm) {
   var key = window.event.keyCode;
  if (key == 13) { window.event.keyCode = 13; return true; }

   if (langFarsi == 1) { // If Farsi
     if (key == 0x0020 && window.event.shiftKey) // Shift-space -> ZWNJ
       window.event.keyCode = 0x200C;
     else
     {
		if (key>31 && key<128)
		{
			if (window.event)
					window.event.keyCode=' !"#$%،گ)(×+و-./0123456789:ك,=.؟@ِذ}ىُىلآ÷ـ،/’د×؛َءٍف‘{ًْإ~جژچ^_پشذزيثبلاهتنمئدخحضقسفعرصطغظ<|>()ّ'.charCodeAt(key-32);
			else if (e)
					e.which=' !"#$%،گ)(×+و-./0123456789:ك,=.؟@ِذ}ىُىلآ÷ـ،/’د×؛َءٍف‘{ًْإ~جژچ^_پشذزيثبلاهتنمئدخحضقسفعرصطغظ<|>)(ّ'.charCodeAt(key-32);
		   }
	  }
     //if (farsikey[key - 0x0020] == 92) {
      //  window.event.keyCode = 0x0698;
     //}
     //if (farsikey[key - 0x0020] == 8205) {
     //   window.event.keyCode = 0x067E;
    // }
   }
   return true;
}

function change(obj){
if (lang==0)
lang=1
else
lang=0;
obj.focus();
}


/* ---------------------------------------------------------------------- *\
  Function    : switchLang
  Description : Switchesh Language between Wnglish and Farsi
  Usage       : editor_event()
\* ---------------------------------------------------------------------- */
function switchLang()
{
	langFarsi=!langFarsi;
	if (document.all.farsi.length>0)
		for (i=0;i<document.all.farsi.length;i++)
			langFarsi?document.all.farsi[i].src=document.all.farsi[i].src.replace("en.gif","fa.gif"):document.all.farsi[i].src=document.all.farsi[i].src.replace("fa.gif","en.gif");
	else
			langFarsi?document.all.farsi.src=document.all.farsi.src.replace("en.gif","fa.gif"):document.all.farsi.src=document.all.farsi.src.replace("fa.gif","en.gif");
}

/* ---------------------------------------------------------------------- *\
  Function    : FarsiKeyboard
  Description : Changes the keycodes to Farsi if it is in Farsi mode
  Usage       : editor_event(editEvent)
  Arguments   : editEvent - null: it is not a editarea
							else: the editEvent of the edit area
\* ---------------------------------------------------------------------- */

function FarsiKeyboard(editEvent)
{
	if (editEvent==null)
		editEvent = window.event;
	key=editEvent.keyCode;
	if (langFarsi)
		if (key >= 0x0020 && key < 0x00FF && editEvent.type == 'keypress')
		{
			if (editEvent.shiftKey && key==32 ){	//catch nimspace
				editEvent.keyCode='‌'.charCodeAt(0);
				return;
			}
			editEvent.keyCode=' !"#$%،گ)(×+و-./0123456789:ك,=.؟@ِذ}ىُىلآ÷ـ،/’د×؛َءٍف‘{ًْإ~جژچ^_پشذزيثبلاهتنمئدخحضقسفعرصطغظ<|>ّ'.charCodeAt(key-32);
		}

}
