﻿var lang = 1;	// 1: Farsi, 0: English

// Farsi keyboard map based on ISIRI-2901

var farsikey = [
   0x0020, 0x0021, 0x061B, 0x066B, 0x00A4, 0x066A, 0x060C, 0x06AF,
   0x0029, 0x0028, 0x002A, 0x002B, 0x0648, 0x002D, 0x002E, 0x002F,
   0x06F0, 0x06F1, 0x06F2, 0x06F3, 0x06F4, 0x06F5, 0x06F6, 0x06F7,
   0x06F8, 0x06F9, 0x003A, 0x06A9, 0x003E, 0x003D, 0x003C, 0x061F,
   0x066C, 0x0624, 0x200C, 0x0698, 0x064A, 0x064D, 0x0625, 0x0623,
   0x0622, 0x0651, 0x0629, 0x00BB, 0x00AB, 0x0621, 0x004E, 0x005D,
   0x005B, 0x0652, 0x064B, 0x0626, 0x064F, 0x064E, 0x0056, 0x064C,
   0x0058, 0x0650, 0x0643, 0x062C, 0x005C, 0x0686, 0x00D7, 0x0640,
   0x200D, 0x0634, 0x0630, 0x0632, 0x06CC, 0x062B, 0x0628, 0x0644,
   0x0627, 0x0647, 0x062A, 0x0646, 0x0645, 0x067E, 0x062F, 0x062E,
   0x062D, 0x0636, 0x0642, 0x0633, 0x0641, 0x0639, 0x0631, 0x0635,
   0x0637, 0x063A, 0x0638, 0x007D, 0x007C, 0x007B, 0x007E
];

// on Alt+Shift, switch language
function FKeyDown()
{
   if (window.event.shiftKey && window.event.altKey) { 
      if (lang == 0) {
         lang = 1;
         window.defaultStatus = "Farsi Mode";
      }
      else {
         lang = 0;
         window.defaultStatus = "English Mode";
      }
      return false;
   }
   return true;
}

// change the Farsi
function FKeyPress()
{
   var key = window.event.keyCode;
   // Avoid processing if control or higher than ASCII (i.e., in Arabic Windows)
   if (key < 0x0020 || key >= 0x00FF)
      return;
   if (lang == 1) { //If Farsi
      if (key == 0x0020 && window.event.shiftKey) // Shift-space -> ZWNJ
         window.event.keyCode = 0x200C;
      else
         window.event.keyCode = farsikey[key - 0x0020];
   }
   return true;
}

function utf4(s) {
	var i, j;
	var tab = "0123456789ABCDEF";

	ret = new String();
	temp = new String();
	for (i = 0; i < s.length; ++i) {
		code = s.charCodeAt(i);
       temp = '';
		for (j = 0; j < 4; ++j) {
			rem = code % 16;
			temp += tab.charAt(rem);
			code = code / 16;
		}
		ret += temp.charAt(3)+temp.charAt(2)+temp.charAt(1)+temp.charAt(0);
	}
	return ret;	
}

function utf4rev(s) {
	var i, j;
	var tab = "0123456789ABCDEF";

	ret = new String();
	temp = new String();
	for (i = 0; i < s.length; ++i) {
		code = s.charCodeAt(i);
		temp = '';
		for (j = 0; j < 4; ++j) {
			rem = code % 16;
			temp += tab.charAt(rem);
			code = code / 16;
		}
		ret += temp.charAt(3)+temp.charAt(2)+temp.charAt(1)+temp.charAt(0);
	}
	return ret;	
}

function convertNumber(str) {
	i = 0;
	while (i < str.length) {
		if ((str.charAt(i) == '&') && (i+1 < str.length) && (str.charAt(i+1) == "#")) {
			i +=6;
		} else  if (str.charAt(i) == '0'){
			str = str.substring(0,i)+"&#1632"+str.substring(i+1);
			i +=6;
		} else  if (str.charAt(i) == '1'){
			str = str.substring(0,i)+"&#1633"+str.substring(i+1);
			i +=6;
		} else  if (str.charAt(i) == '2'){
			str = str.substring(0,i)+"&#1634"+str.substring(i+1);
			i +=6;
		} else  if (str.charAt(i) == '3'){
			str = str.substring(0,i)+"&#1635"+str.substring(i+1);
			i +=6;
		} else  if (str.charAt(i) == '4'){
			str = str.substring(0,i)+"&#1636"+str.substring(i+1);
			i +=6;
		} else  if (str.charAt(i) == '5'){
			str = str.substring(0,i)+"&#1637"+str.substring(i+1);
			i +=6;
		} else  if (str.charAt(i) == '6'){
			str = str.substring(0,i)+"&#1638"+str.substring(i+1);
			i +=6;
		} else  if (str.charAt(i) == '7'){
			str = str.substring(0,i)+"&#1639"+str.substring(i+1);
			i +=6;
		} else  if (str.charAt(i) == '8'){
			str = str.substring(0,i)+"&#1640"+str.substring(i+1);
			i +=6;
		} else  if (str.charAt(i) == '9'){
			str = str.substring(0,i)+"&#1641"+str.substring(i+1);
			i +=6;
		}else{
			i++;
		}
	}
	return str;
}

function IsCharDigit()
{
    var key 
	key = window.event.keyCode
    
	if ( key >=48 && key<=57 )
	   window.event.returnValue= true
	else
	   window.event.returnValue= false
}

function letterOnly(event)
{
    var keyCode = event.keyCode;

        if (keyCode==0)
          keyCode = event.which;
        if (keyCode==64 || keyCode==45 || keyCode==46 || keyCode==47 ||  (keyCode>=96 && keyCode<=123))
          return true;
        return false;
}

