﻿// JScript File

// Functions in this file are used to convert Unicode input to Akhar font.
// As XmlHttpRequest object used for AJAX functionality does not allow Unicode strings to be sent as request,
// it allows only response back in Unicode.
//
//  Most of the functionality of this file is based on Conversion.cs file in this project.
//


// Array to hold Akhar font alphabets.
var akharUnicode = new Array(64);

// Calls the function to initialize 'akharUnicode' array.
InitializeAkharUnicode();

// Function to initialize 'akharUnicode' array to hold Akhar font values and corresponding Unicodes.
function InitializeAkharUnicode()
{
    // Declares second dimension of 'akharUnicode' array.
    for(i = 0; i < 64; i++)
    {
        akharUnicode[i] = new Array(2);    
    }

    //for special character combinations
    akharUnicode[0][0] = "N";
    akharUnicode[0][1] = "2562";
    akharUnicode[1][0] = "Ew";
    akharUnicode[1][1] = "2566";
    akharUnicode[2][0] = "EY";
    akharUnicode[2][1] = "2576";
    akharUnicode[3][0] = "EO";
    akharUnicode[3][1] = "2580";
    akharUnicode[4][0] = "E";
    akharUnicode[4][1] = "2565";
    akharUnicode[5][0] = "ie";
    akharUnicode[5][1] = "2567";
    akharUnicode[6][0] = "eI";
    akharUnicode[6][1] = "2568";
    akharUnicode[7][0] = "ey";
    akharUnicode[7][1] = "2575";
    akharUnicode[8][0] = "au";
    akharUnicode[8][1] = "2569";
    akharUnicode[9][0] = "aU";
    akharUnicode[9][1] = "2570";
    akharUnicode[10][0] = "A";
    akharUnicode[10][1] = "2579";

    akharUnicode[11][0] = "k";
    akharUnicode[11][1] = "2581";
    akharUnicode[12][0] = "K";
    akharUnicode[12][1] = "2582";
    akharUnicode[13][0] = "g";
    akharUnicode[13][1] = "2583";
    akharUnicode[14][0] = "G";
    akharUnicode[14][1] = "2584";
    akharUnicode[15][0] = "Z";
    akharUnicode[15][1] = "2585";
    akharUnicode[16][0] = "c";
    akharUnicode[16][1] = "2586";
    akharUnicode[17][0] = "C";
    akharUnicode[17][1] = "2587";
    akharUnicode[18][0] = "j";
    akharUnicode[18][1] = "2588";
    akharUnicode[19][0] = "J";
    akharUnicode[19][1] = "2589";
    akharUnicode[20][0] = "z";
    akharUnicode[20][1] = "2590";
    akharUnicode[21][0] = "t";
    akharUnicode[21][1] = "2591";
    akharUnicode[22][0] = "T";
    akharUnicode[22][1] = "2592";
    akharUnicode[23][0] = "f";
    akharUnicode[23][1] = "2593";
    akharUnicode[24][0] = "F";
    akharUnicode[24][1] = "2594";
    akharUnicode[25][0] = "x";
    akharUnicode[25][1] = "2595";
    akharUnicode[26][0] = "q";
    akharUnicode[26][1] = "2596";
    akharUnicode[27][0] = "Q";
    akharUnicode[27][1] = "2597";
    akharUnicode[28][0] = "d";
    akharUnicode[28][1] = "2598";
    akharUnicode[29][0] = "D";
    akharUnicode[29][1] = "2599";
    akharUnicode[30][0] = "n";
    akharUnicode[30][1] = "2600";
    akharUnicode[31][0] = "p";
    akharUnicode[31][1] = "2602";
    akharUnicode[32][0] = "P";
    akharUnicode[32][1] = "2603";
    akharUnicode[33][0] = "b";
    akharUnicode[33][1] = "2604";
    akharUnicode[34][0] = "B";
    akharUnicode[34][1] = "2605";
    akharUnicode[35][0] = "m";
    akharUnicode[35][1] = "2606";
    akharUnicode[36][0] = "X";
    akharUnicode[36][1] = "2607";

    akharUnicode[37][0] = "r";
    akharUnicode[37][1] = "2608";
    akharUnicode[38][0] = "l";
    akharUnicode[38][1] = "2610";
    akharUnicode[39][0] = "v";
    akharUnicode[39][1] = "2613";
    akharUnicode[40][0] = "s";
    akharUnicode[40][1] = "2616";
    akharUnicode[41][0] = "h";
    akharUnicode[41][1] = "2617";

    akharUnicode[42][0] = "L";
    akharUnicode[42][1] = "2620";
    akharUnicode[43][0] = "w";
    akharUnicode[43][1] = "2622";
    akharUnicode[44][0] = "i";
    akharUnicode[44][1] = "2623";
    akharUnicode[45][0] = "I";
    akharUnicode[45][1] = "2624";
    akharUnicode[46][0] = "u";
    akharUnicode[46][1] = "2625";
    akharUnicode[47][0] = "U";
    akharUnicode[47][1] = "2626";
    akharUnicode[48][0] = "y";
    akharUnicode[48][1] = "2631";
    akharUnicode[49][0] = "Y";
    akharUnicode[49][1] = "2632";
    akharUnicode[50][0] = "o";
    akharUnicode[50][1] = "2635";
    akharUnicode[51][0] = "O";
    akharUnicode[51][1] = "2636";

    akharUnicode[52][0] = "V";
    akharUnicode[52][1] = "2593,2620";

    akharUnicode[53][0] = "M";
    akharUnicode[53][1] = "2672";
    akharUnicode[54][0] = "S";
    akharUnicode[54][1] = "2673";

    akharUnicode[55][0] = "e";
    akharUnicode[55][1] = "2674";
    akharUnicode[56][0] = "a";
    akharUnicode[56][1] = "2675";

    //for EK ONKAR
    akharUnicode[57][0] = "161";
    akharUnicode[57][1] = "2676";

    akharUnicode[58][0] = "R";
    akharUnicode[58][1] = "2637,2608";
    akharUnicode[59][0] = "H";
    akharUnicode[59][1] = "2637,2617";
    akharUnicode[60][0] = "W";
    akharUnicode[60][1] = "2637,2613";

    // For sentence endings i.e. single danda(full stop) and double danda.
    akharUnicode[61][0] = ".";
    akharUnicode[61][1] = "2404";
    akharUnicode[62][0] = "171";
    akharUnicode[62][1] = "2405";

    // For 'dot' i.e. '.', the one used after abbreviations or as in eg. or etc.
    akharUnicode[63][0] = "|";
    akharUnicode[63][1] = ".";
}

// Function to normalize unicode text.
function NormalizeUnicode(text)
{
    // Replaces one char words with 'bindi' with equivalent two chars.
    text = text.replace(String.fromCharCode(2611),String.fromCharCode(2610) + String.fromCharCode(2620));
    text = text.replace(String.fromCharCode(2614),String.fromCharCode(2616) + String.fromCharCode(2620));
    text = text.replace(String.fromCharCode(2649),String.fromCharCode(2582) + String.fromCharCode(2620));
    text = text.replace(String.fromCharCode(2650),String.fromCharCode(2583) + String.fromCharCode(2620));
    text = text.replace(String.fromCharCode(2651),String.fromCharCode(2588) + String.fromCharCode(2620));
    text = text.replace(String.fromCharCode(2654),String.fromCharCode(2603) + String.fromCharCode(2620));

    // For 'RARA' i.e. char 'V' in Akhar font, replaces one char form with two char form.
    text = text.replace(String.fromCharCode(2652), String.fromCharCode(2593) + String.fromCharCode(2620));

    // Returns  normalized text.
    return text;
}

// Function to convert unicode text into Akhar font.
function UnicodeToAkhar(word)
{
    // Declares variable to hold 'Akhar' version of 'word'.
    var wordAkhar = NormalizeUnicode(word);
   
    // Handles cases for short form characters i.e. akharUnicode[58,] to akharUnicode[60,] separately,
    // so that these do not get replaced by their full forms prior to their desired(i.e. this) replacement.
    wordAkhar = replaceAll(wordAkhar,String.fromCharCode(akharUnicode[58][1].toString().substring(0,akharUnicode[58][1].toString().indexOf(","))) + String.fromCharCode(akharUnicode[58][1].toString().substring(akharUnicode[58][1].toString().indexOf(",") + 1)),akharUnicode[58][0]);
    wordAkhar = replaceAll(wordAkhar,String.fromCharCode(akharUnicode[59][1].toString().substring(0,akharUnicode[59][1].toString().indexOf(","))) + String.fromCharCode(akharUnicode[59][1].toString().substring(akharUnicode[59][1].toString().indexOf(",") + 1)),akharUnicode[59][0]);
    wordAkhar = replaceAll(wordAkhar,String.fromCharCode(akharUnicode[60][1].toString().substring(0,akharUnicode[60][1].toString().indexOf(","))) + String.fromCharCode(akharUnicode[60][1].toString().substring(akharUnicode[60][1].toString().indexOf(",") + 1)),akharUnicode[60][0]);

    // For /RaRa/ i.e. character 'V' in Akhar font.
    wordAkhar = replaceAll(wordAkhar,String.fromCharCode(akharUnicode[52][1].toString().substring(0,akharUnicode[52][1].toString().indexOf(","))) + String.fromCharCode(akharUnicode[52][1].toString().substring(akharUnicode[52][1].toString().indexOf(",") + 1)), akharUnicode[52][0]);

    // Replaces every character or character combination of the 'word' by their Unicode codes,
    // loop from 0 to 60 and ignore 58 to 60 as cases for 58 to 60 has been handled already above.
    for (i = 0; i <= 60; i++)
    {
        if (i == 52 || i == 58 || i == 59 || i == 60)
        {
            continue;
        }

        wordAkhar = replaceAll(wordAkhar,String.fromCharCode(akharUnicode[i][1]), akharUnicode[i][0]);
    }

    // Replaces 'akharUnicode[61, 1]' and 'akharUnicode[62, 1]' with '///'.
    // This is done to avoid 'akharUnicode[61, 0]' and 'akharUnicode[62, 0]' i.e. '.' ('akharUnicode[63, 1]')
    // being replaced by 'akharUnicode[63, 0]' in next line.
    wordAkhar = replaceAll(wordAkhar,String.fromCharCode(akharUnicode[61][1]), "///")
    wordAkhar = replaceAll(wordAkhar,String.fromCharCode(akharUnicode[62][1]), "///");
    // Replaces 'akharUnicode[63, 1]' with 'akharUnicode[63, 0]'.
    wordAkhar = replaceAll(wordAkhar,String.fromCharCode(akharUnicode[63][1]), akharUnicode[63][0]);
    // Now replaces '///' with 'akharUnicode[61, 0]' and 'akharUnicode[62, 0]'.
    wordAkhar = replaceAll(wordAkhar,"///", akharUnicode[61][0]);
    wordAkhar = replaceAll(wordAkhar,"///", akharUnicode[62][0]);


    // Shifts 'siharis' i.e. 'i' in 'Akhar' font, one charater to left
    // as already for AkharTOUnicode these were shifted to one character position right
    // reverse version of similar code block in AkharTOUnicode(...) method.
    for(pos = 0; pos < wordAkhar.length; pos++)
    {
	    if(wordAkhar.charAt(pos) == 'i' && pos != 0)
	    {
		    // Continues if 'i' is before 'e' i.e. 'ie' case akharUnicode[5].
		    if(pos < (wordAkhar.length - 2) && wordAkhar.charAt(pos+1) == 'e')
			{
			    continue;
			}

		    // Checks if previous char is 'R' or 'H' or 'W', in 'Akhar' font.
		    if(pos >= 2 && (wordAkhar.charAt(pos-1) == 'R' || wordAkhar.charAt(pos-1) == 'H' || wordAkhar.charAt(pos-1) == 'W' || wordAkhar.charAt(pos-1) == 'L'))
		    {
			    // Removes 'i'.
				wordAkhar = wordAkhar.substring(0,pos) + wordAkhar.substring(pos + 1);
			    // Inserts 'i' at position of 'previous char'.
                wordAkhar = wordAkhar.substring(0, pos - 2) + "i" + wordAkhar.substring(pos - 2);
		    }
		    else	
		    {
			    // Removes 'i'.
				wordAkhar = wordAkhar.substring(0,pos) + wordAkhar.substring(pos + 1);
			    // Inserts 'i' at position of 'previous char'.
                wordAkhar = wordAkhar.substring(0, pos - 1) + "i" + wordAkhar.substring(pos - 1);
		    }
	    }
    }

    // Returns 'word' in Unicode.
    //alert(wordAkhar);// + " " + String.fromCharCode(2613) + String.fromCharCode(akharUnicode[52][1]));
    return wordAkhar;
}

// Function to implement replace all functionality using replace function for strings in javascript.
function replaceAll(replaceIn, replaceThis, replaceWith)
{
    // Replaces all instances of 'replaceThis' with 'replaceWith' in 'replaceIn'.
    while(replaceIn.search(replaceThis) > -1)
    {
        replaceIn = replaceIn.replace(replaceThis, replaceWith);
    }
    
    // Returns the modified 'replaceIn'.
    return replaceIn;
}
