var LANGUAGE_LATIN = 0;
var LANGUAGE_FARSI = 1;
var iType = LANGUAGE_FARSI;
var objSet = null;
var Button = new Array('Farsi Input' , 'English Input')

var FarsiMap = new Array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 
                          25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 1711, 40, 41, 42, 43, 1608, 45, 46,
                          47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 1603, 60, 61, 62, 1567, 
                          64, 1588, 1584, 1586, 1609, 1579, 1576, 1604, 1570, 1607, 1578, 1606, 1605, 1574, 1583, 1582,
                          1581, 1590, 1602, 1587, 1601, 1593, 1585, 1589, 1591, 1594, 1592, 1580, 1688, 1670, 94, 95,
                          1662, 1588, 1584, 1586, 1610, 1579, 1576, 1604, 1575, 1607, 1578, 1606, 1605, 1574, 1583, 
                          1582, 1581, 1590, 1602, 1587, 1601, 1593, 1585, 1589, 1591, 1594, 1592, 123, 124, 125, 126,
                          127, 128, 1588, 1584, 1586, 1609, 1579, 1576, 1604, 1575, 1570, 1607, 1578, 1606, 1605, 1574,
                          1583, 1582, 1581, 1590, 1602, 1587, 1601, 1593, 1585, 1589, 1591, 1594, 1592
                        );

var LatinMap = new Array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
                          26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
                          49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
                          72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
                          95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
                          115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 97, 98, 99, 100, 101,
                          102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
                          120, 121, 122
                        );
var TypedKeys = new Array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
                           25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
                           48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
                           71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
                           94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
                           113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 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, 1570
                         );
function setit(myobj)
{
objSet=myobj;
//alert(myobj.dir);
}

function IndexOf( iKey)
{
  var i, iKeyIndex, l = TypedKeys.length;
  iKeyIndex = -1;
  var bFound = false;
  for( i = 0; ( i < l) && !bFound; i++)
    if( TypedKeys[ i] == iKey)
    {
      bFound = true;
      iKeyIndex = i;
    }
  return iKeyIndex;
}

function onEditKeyDown( a, e)
{
  if( e.keyCode == 123) 
//  if( window.event.shiftKey && window.event.altKey)
    {
    iType = ( iType == LANGUAGE_LATIN) ? LANGUAGE_FARSI : LANGUAGE_LATIN;
    if (document.all.lang)
    {document.all.lang.value=Button[iType];}
    if(objSet)
    {
    objSet.focus();
    if (iType==LANGUAGE_LATIN)
      {objSet.dir='ltr';}
    else
      {objSet.dir='rtl';}
    }
//    change();
//    alert(a.value);
//    alert(iType);
//    alert(e.keyCode);    
    }
  return;
}

function change()
{
    iType = ( iType == LANGUAGE_LATIN) ? LANGUAGE_FARSI : LANGUAGE_LATIN;
    
//    alert(iType);
//    alert(objSet.value);
    if (document.all.lang)
    {document.all.lang.value=Button[iType];}
    if(objSet)
    {
    objSet.focus();
    if (iType==LANGUAGE_LATIN)
      {objSet.dir='ltr';}
    else
      {objSet.dir='rtl';}
    }
}

function onEditKeyPress( a, e)
{
  e = window.event;
  var iResult = e.keyCode;
//  alert(e.keyCode);//
  var CurKey = IndexOf( iResult);
//  alert(CurKey);//
  if( CurKey != -1)
    iResult = ( iType == LANGUAGE_FARSI) ? FarsiMap[ CurKey] : LatinMap[ CurKey];
  e.keyCode = iResult;
  return;
}

