// feel free to copy

function hex2Dec(strHex){
	return parseInt(strHex, 16);
	};

var arrU2B = new Array(80); // maps Unicode (in decimal) to Buckwalter char
var arrUG2B = new Array(130); // maps Unicode Glyph (in decimal) to Buckwalter char(s)
var arrB2U = new Array(127);// maps 7-bit ASCII to decimal Unicode code for 
                            // corresponding Arabic char
                            
	//first Arabic Unicode character is 1569 (0x621)
	//last  Arabic Unicode character is 1649 (0x671), so that's 80 chars
	
var cOffset = 1569; // so arrU2B[0] represents arrU2B[1569]
var cOffsetG = 65152; // so arrUG2B[0] represents arrGU2B[65152]

function map(strHex,strBuckChar){
	// setup both mapping arrays
	arrU2B[hex2Dec(strHex) - cOffset] = strBuckChar;
	arrB2U[strBuckChar.charCodeAt(0)] = hex2Dec(strHex);
	};	
	
function map2(lngDecimal,strBuckChars){
	// setup one way glyph -> Buckwalter mapping array
	arrUG2B[lngDecimal - cOffsetG] = strBuckChars;
	};	
	
// this is the actual map	
	map('621','\'');
	map('622','|');
	map('623','>');
	map('624','&');
	map('625','<');
	map('626','}');
	map('627','A');
	map('628','b');
	map('629','p');
	map('62A','t');
	map('62B','v');
	map('62C','j');
	map('62D','H');
	map('62E','x');
	map('62F','d');
	map('630','*');
	map('631','r');
	map('632','z');
	map('633','s');
	map('634','$');
	map('635','S');
	map('636','D');
	map('637','T');
	map('638','Z');
	map('639','E');
	map('63A','g');
	map('640','_');
	map('641','f');
	map('642','q');
	map('643','k');
	map('644','l');
	map('645','m');
	map('646','n');
	map('647','h');
	map('648','w');
	map('649','Y');
	map('64A','y');
	map('64B','F');
	map('64C','N');
	map('64D','K');
	map('64E','a');
	map('64F','u');
	map('650','i');
	map('651','~');
	map('652','o');
	map('670','`');
	map('671','{');

	// unicode glyph mappings 
	// http://www.wildboar.net/multilingual/middleeastern/arabic/language/unicode/unicode2.html
	map2(65152,'\'');
	map2(65153,'|');
	map2(65154,'|');
	map2(65155,'>');
	map2(65156,'>');
	map2(65157,'&');
	map2(65158,'&');
	map2(65159,'<');
	map2(65150,'<');
	map2(65161,'}');
	map2(65162,'}');
	map2(65163,'}');
	map2(65164,'}');
	map2(65165,'A');
	map2(65166,'A');
	map2(65167,'b');
	map2(65168,'b');
	map2(65169,'b');
	map2(65170,'b');
	map2(65171,'p');
	map2(65172,'p');
	map2(65173,'t');
	map2(65174,'t');
	map2(65175,'t');
	map2(65176,'t');
	map2(65177,'v');
	map2(65178,'v');
	map2(65179,'v');
	map2(65180,'v');
	map2(65181,'j');
	map2(65182,'j');
	map2(65183,'j');
	map2(65184,'j');
	map2(65185,'H');
	map2(65186,'H');
	map2(65187,'H');
	map2(65188,'H');
	map2(65189,'x');
	map2(65190,'x');
	map2(65191,'x');
	map2(65192,'x');
	map2(65193,'d');
	map2(65194,'d');
	map2(65195,'*');
	map2(65196,'*');
	map2(65197,'r');
	map2(65198,'r');
	map2(65199,'z');
	map2(65200,'z');
	map2(65201,'s');
	map2(65202,'s');
	map2(65203,'s');
	map2(65204,'s');
	map2(65205,'$');
	map2(65206,'$');
	map2(65207,'$');
	map2(65208,'$');
	map2(65209,'S');
	map2(65210,'S');
	map2(65211,'S');
	map2(65212,'S');
	map2(65213,'D');
	map2(65214,'D');
	map2(65215,'D');
	map2(65216,'D');
	map2(65217,'T');
	map2(65218,'T');
	map2(65219,'T');
	map2(65220,'T');
	map2(65221,'Z');
	map2(65222,'Z');
	map2(65223,'Z');
	map2(65224,'Z');
	map2(65225,'E');
	map2(65226,'E');
	map2(65227,'E');
	map2(65228,'E');
	map2(65229,'g');
	map2(65230,'g');
	map2(65231,'g');
	map2(65232,'g');
	map2(65233,'f');
	map2(65234,'f');
	map2(65235,'f');
	map2(65236,'f');
	map2(65237,'q');
	map2(65238,'q');
	map2(65239,'q');
	map2(65240,'q');
	map2(65241,'k');
	map2(65242,'k');
	map2(65243,'k');
	map2(65244,'k');
	map2(65245,'l');
	map2(65246,'l');
	map2(65247,'l');
	map2(65248,'l');
	map2(65249,'m');
	map2(65250,'m');
	map2(65251,'m');
	map2(65252,'m');
	map2(65253,'n');
	map2(65254,'n');
	map2(65255,'n');
	map2(65256,'n');
	map2(65257,'h');
	map2(65258,'h');
	map2(65259,'h');
	map2(65260,'h');
	map2(65261,'w');
	map2(65262,'w');
	map2(65263,'Y');
	map2(65264,'Y');
	map2(65265,'y');
	map2(65266,'y');
	map2(65267,'y');
	map2(65268,'y');
	map2(65269,'l|');
	map2(65270,'l|');
	map2(65271,'l>');
	map2(65272,'l>');
	map2(65273,'l<');
	map2(65274,'l<');
	map2(65275,'lA');
	map2(65276,'lA');
	
	
// convert Arabic to Buckwalter
function a2B(lngCode){
	if (lngCode > 1568 && lngCode < 1650)
		{
		// Arabic char, so use mapping
		return arrU2B[lngCode - cOffset]
		}
	else
		{ 
			if (lngCode < 255)
				{
					return String.fromCharCode(lngCode); 
				}
			else
				{
					if (lngCode > 65151 && lngCode < 65277)
					{
					// Arabic glyph char, so use second mapping
					return arrUG2B[lngCode - cOffsetG]
					}
				else
	  			{
	  				return '[' + lngCode + ']';
	  			};
		  	}
		};
	};
	
// convert Buckwalter to Arabic
function b2A(lngCode){
	if (lngCode > 32 && lngCode != 44 )
		{
		// use mapping
		// may need to do something with numerals here...
		return String.fromCharCode(arrB2U[lngCode]); 
		}
	else
		{ 
		// don't do anything
		return String.fromCharCode(lngCode);
		};
	};

function fncArabic2Buckwalter(frm){
	// simple string loop, calling a2B
	strArabic = frm.elements['elmUnicodeArabic'].value;
	strCodes = '';
	for (intI = 0; intI < strArabic.length; intI++)
		{		
		strCodes = strCodes + a2B(strArabic.charCodeAt(intI));
		};
	frm.elements['elmBuckwalterArabic'].value = strCodes;
	};
	
function fncBuckwalter2Arabic(frm){
	// simple string loop, calling b2A
	strCodes = frm.elements['elmBuckwalterArabic'].value;
	strArabic = '';
	for (intI = 0; intI < strCodes.length; intI++)
		{		
		strArabic = strArabic + b2A(strCodes.charCodeAt(intI));
		};
	frm.elements['elmUnicodeArabic'].value = strArabic;
	};

function fncB2A(strCodes){
	// simple string loop, calling b2A
	strArabic = '';
	for (intI = 0; intI < strCodes.length; intI++)
		{		
		strArabic = strArabic + b2A(strCodes.charCodeAt(intI));
		
		};
	return strCodes.replace('<','&lt;') + ' --- ' + '<sp' + 'an st' + 'yle="font-size:200%">' + strArabic + '</sp' + 'an>';
	};

function fncshowA(strCodes){
	// simple string loop, calling b2A
	strArabic = '';
	if (strCodes == '')
		{ return '[not found]' };
	for (intI = 0; intI < strCodes.length; intI++)
		{		
		strArabic += b2A(strCodes.charCodeAt(intI));
		
		};
	return '<sp' + 'an class="fs" dir="rtl">' + strArabic + '</sp' + 'an>';
	};

function fncB2An(strCodes){
	// simple string loop, calling b2A
	strNoVowelCodes = strCodes.replace(/[aiuo]/g,'');
	strArabic = '';
	for (intI = 0; intI < strNoVowelCodes.length; intI++)
		{		
		strArabic = strArabic + b2A(strNoVowelCodes.charCodeAt(intI));
		
		};
	return '<sp' + 'an sty' + 'le="font-size:200%">' + strArabic + '</sp' + 'an>  (' + strCodes.replace('<','&lt;') + ') '  ;
	};


