// JavaScript Document

function convert_to_unicode_chanakya(fromctrl, toctrl)
{

var array_one = new Array(

"¤","U",

// correct anusvAr+ekAr, ekAr+nuqta to the reverse order
"¢ð","´ð","ð¸",
   
"Ò","¥æò","¸",
"¸•", "¸¹", "¸»", "¸Á","¸Ç", "¸É", "¸È","¸Ø","¸Ú","¸Ù",
 
"A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","T","V","W","X","Y",

"`","a","b","d","e","f","g","h","i","j","k","l","m","n","p","q","r","s","t","u",

"žæ","ž","#","%","@",
"„","¦","¨","¯","µ","º",

"Cþ","q","Ê","u","g",    
"Ÿæ","Åþ","Çþ","Éþ", 
"^","h","Ð","ý","þ",

 
"¥ô","¥æð","¥õ","¥æñ","¥æ","¥","§Z","§ü","§","©","ª","«","¬","­","°ð","°",

"€", "·", "",  "¹", "‚", "»", "ƒæ", "ƒ", "¾", 
"“","‘","¿","À","”","…","’","Á","Ûæ","Û","†æ","†",

"Å","Æ","Ç","É","‡æ","‡",
"ˆ","Ì","‰","Í","Î","¼","Š","Ï","óæ","ó","‹æ","Ù","‹",

"Œ","Â","","È","Ž","Õ","","Ö","","×",
"Ä","Ø","Ú","Ë","Ü","¶","Ý","Ã","ß",
"àæ","³æ","o","³","à","c","á","S","â","ã","±",
"ÿæ","ÿ","˜æ","˜","™æ","™","üð´",
 
"æò","æñ","æ","è","é","ê","ä","å","ë","ì","í","Ô","ñ","ô","õ",
"¢","´","¡","Ñ","¸","ò","ù","÷","ð",

"0","1","2","3","4","5","6","7","8","9",
"®","v","w","x","y","z","{","|","}","~",
"्ो","्ौ","्ाे", "्ाा","ाे","ाे","ाै","्ा","ंु","ओे","ोे","ाे","ईंं" )   


var array_two = new Array(

"","",

// correct anusvAr+ekAr, ekAr+nuqta to the reverse order
"ð¢","ð´","¸ð",

"'","ऑ","फ़्", 
"क़","ख़","ग़","ज़","ड़","ढ़","फ़","य़","ऱ","ऩ",

"्र","क्च","ष्ट","ष्ठ","श्व","स्न","त्र","॥","ढ्ढ","छ्व","्य","रु","रू",
"हृ","ह्र","क्क","क्त","क्र","ञ्ज","ङ्क","ङ्ख","ङ्ग","ङ्घ",
"क्व","ड्ड","ड्ढ","स्र","द्ग","द्घ","द्द","द्ध","द्ब","द्भ","द्म","द्य","द्व","ठ्ठ","श्च","ह्न","ह्म्","ह्य","ह्ल","ह्व",
 
"त्त","त्त्","प्त","त्न","ञ्च",
"ल्ल","ष्ट्व","ङ्क्ष","ख्न","द्ब्र","ख्र",

"ष्ट्र","ह्न","ज़्","ह्व","द्द",  
"श्र","ट्र","ड्र","ढ्र",
"ट्ट","द्ध","।","्र","्र",
 
"ओ","ओ","औ","औ","आ","अ","ईं","ई","इ","उ","ऊ","ऋ","ॠ","ऌ","ऐ","ए",

"क्", "क","ख्","ख","ग्","ग","घ","घ्","ङ",
"च्च्","च्","च","छ","ज्ज्","ज्","ज्","ज","झ","झ्","ञ","ञ्",
 
"ट","ठ","ड","ढ","ण","ण्",
"त्","त","थ्","थ","द","द","ध्","ध","न्न","न्न्","न","न","न्",

"प्","प","फ्","फ","ब्","ब","भ्","भ","म्","म",
"य्","य","र","ल्","ल","ल","ळ","व्","व",
"श","श","श","श्","श्","ष्","ष","स्","स","ह","ह्",
"क्ष","क्ष्","त्र","त्र्","ज्ञ","ज्ञ्","ðZ",

"ॉ","ौ","ा","ी","ु","ू","ु","ू","ृ","ॄ","ॢ","े","ै","ो","ौ",
"ं","ं","ँ",":","़", "ॅ","ऽ","्","े",

"०","१","२","३","४","५","६","७","८","९",
"0","1","2","3","4","5","6","7","8","9",

"े", "ै", "े","ा","ो","ो","ौ","","ुं","ओ","ो","ो","ईं")   

var array_one_length = array_one.length ;


if ( (document.getElementById("text_or_html")).selectedIndex == 0 )  // if the input is plain text

   { 

      document.getElementById(toctrl).value = "You have chosen SIMPLE TEXT  in Chanakya font to convert into Unicode. Conversion in progress.."  ;  

      var modified_substring = document.getElementById(fromctrl).value  ;

//****************************************************************************************
//  Break the long text into small bunches of max. max_text_size  characters each.
//****************************************************************************************
    var text_size = document.getElementById(fromctrl).value.length ;

    var processed_text = '' ;  //blank

    var sthiti1 = 0 ;  var sthiti2 = 0 ;  var chale_chalo = 1 ;
 
    var max_text_size = 4000;

    while ( chale_chalo == 1 ) 
    {
     sthiti1 = sthiti2 ;

     if ( sthiti2 < ( text_size - max_text_size ) )  
     { 
      sthiti2 +=  max_text_size ;
      while (document.getElementById(fromctrl).value.charAt ( sthiti2 ) != ' ') {sthiti2--;}
     } 
     else  { sthiti2 = text_size  ;  chale_chalo = 0 }

     var modified_substring = document.getElementById(fromctrl).value.substring ( sthiti1, sthiti2 )  ;

    Replace_Symbols( ) ;

    processed_text += modified_substring ;

//****************************************************************************************
//  Breaking part code over
//****************************************************************************************
//
//    processed_text = processed_text.replace( /Sanskrit 99/g , "mangal" ) ;   

      document.getElementById(toctrl).value = processed_text  ;

   }
}
else    // if input is HTML then
   {
    document.getElementById(toctrl).value = "You have chosen HTML TEXT  in Agra font to convert into Unicode. Conversion in progress.."  ;  

    var remaining_text = document.getElementById(fromctrl).value ;
    var processed_text = "" ;  //blank initially

    var position_of_current_opening_bracket = 0 ;
    var position_of_next_closing_bracket = 1 ;

    var idx = remaining_text.indexOf( "<p " )  // search starting from paragraphs. then search for sanskrit 99.
    idx = remaining_text.indexOf( "Sanskrit 99" , idx )
    var idx2 = 0  //  position_of_current_opening_bracket  ">"
    var idx3 = 0  //  position of "/span"
    var idx4 = 0  //  postion of "span" only , ie span without "/"

    while( idx != -1 )    // while-01 loop     ;  while "Sanskrit 99"  is found..
       {  
        idx2 = remaining_text.indexOf( ">" , idx )  
        idx3 = remaining_text.indexOf( "/span" , idx2 )
        idx4 = remaining_text.indexOf( "span" , idx2 )

        while( idx4 < idx3 )     // this loop to take care of  nested span.
           {
            idx4 = remaining_text.indexOf( "span" , idx3+4 ) ;
            idx3 = remaining_text.indexOf( "/span" , idx3+4 ) ;
           }
        var modified_substring = remaining_text.substring( idx2 , idx3 ) ;
        modified_substring = modified_substring.replace( />/g , ">>" ) ; 
        processed_text = processed_text + remaining_text.substring( 0 , idx2 ) + modified_substring + "/span" ;   

        remaining_text = remaining_text.substring( idx3 + 5 ) ;  //remaining_text excludes "/span"
        idx = remaining_text.indexOf( "Sanskrit 99" ) ;
       } //end of outermost while-01
    
    processed_text = processed_text + remaining_text ;

    // -----------------------------

    //  This section for taking care of paragraphs marked class = MsoBodyText  or class = MsoBodyText

    remaining_text = processed_text ;
    processed_text = "" ;  //blank initially

    var position_of_start_of_paragraph = 0 ;
    var position_of_end_of_paragraph = 1 ;

    position_of_start_of_paragraph = remaining_text.indexOf("<p ")   ;

    while( position_of_start_of_paragraph != -1 )  //search for <p  in the remaining_text
       {
        position_of_start_of_paragraph = remaining_text.indexOf("<p ")   ;
        position_of_end_of_paragraph = remaining_text.indexOf("/p>")   ;

        modified_substring = remaining_text.substring( position_of_start_of_paragraph + 3 ,  position_of_end_of_paragraph ) ;

        if ( modified_substring.indexOf( "MsoBodyText" ) != -1 )
           { 
            modified_substring = modified_substring.replace( />/g , ">>" )  ;    // repace all ">" with ">>" in this paragraph

            idx = modified_substring.indexOf("font-family")  ;  // in Mybodytext, whereever font-family is found, it means it is roman text.
            idx2 = 0 ;  // position of ">>"
            idx3 = 0 ;  // position of "/span"
            idx4 = 0 ;  // position of "span" only without "/"

            while( idx != -1 )     // again change ">>" to ">" only those which occur immediately after font-family:"Times new Roman"
               {
                idx2 = modified_substring.indexOf( ">>" , idx ) ;
                idx3 = modified_substring.indexOf( "/span" , idx2 ) ;
                idx4 = modified_substring.indexOf( "span" , idx2 ) ;

                while( idx4 < idx3 )
                   {
                    idx4 = modified_substring.indexOf( "span" , idx3+4 )  ;
                    idx3 = modified_substring.indexOf( "/span" , idx3+4 )  ;
                   }

                modified_substring = modified_substring.substring( 0, idx2 ) + (modified_substring.substring( idx2 , idx3 )).replace( />>/g , ">" ) + modified_substring.substring( idx3 ) ; 
                idx = modified_substring.indexOf( "font-family" , idx3 ) ;

               } // end of while inner loop
            } // end of if statement

         processed_text = processed_text + remaining_text.substring( 0 , position_of_start_of_paragraph + 3 ) +  modified_substring + "/p>" ;   
         remaining_text = remaining_text.substring( position_of_end_of_paragraph + 3 ) ; 
         position_of_start_of_paragraph = remaining_text.indexOf("<p ")   ;

        } // end of outer while loop

     processed_text = processed_text + remaining_text ;

     // ------------------------

     // Now do actual font conversion  of text occuring between  all the  pairs  >>  and  <

     remaining_text = processed_text ;     processed_text = "" ; 

     idx2 = remaining_text.indexOf( ">>" )  ;

     while( idx2 != -1 )    // while-01 loop (checks if ">>" is still present
        {  
         position_of_current_opening_bracket = remaining_text.indexOf( ">>" , idx2 )  
         position_of_next_closing_bracket = remaining_text.indexOf( "<" , position_of_current_opening_bracket )

         modified_substring = remaining_text.substring( position_of_current_opening_bracket + 2 , position_of_next_closing_bracket ) ;

         processed_text = processed_text + remaining_text.substring( 0 , position_of_current_opening_bracket + 1 ) ;   // ">" included by using +1 here
         remaining_text = remaining_text.substring( position_of_next_closing_bracket + 1 ) ;  //remaining_text excludes the closing bracket

         Replace_Symbols() ;   // call the subroutine and replace the legacy symbols  with corresponding Unicode.

         processed_text = processed_text + modified_substring + "<"  ;

         idx2 = remaining_text.indexOf( ">>" ) ;

        } //end of outermost while-01

     processed_text = processed_text + remaining_text ; 

     // do follwing conversions which are still left  or  were done incorrectly due to unavoidable reasons.
 
     processed_text = processed_text.replace( /Agra/g , "mangal" ) ;   

     processed_text = processed_text.replace( /-सजय/g , "ढ" )   ;  
     processed_text = processed_text.replace( /-ुनवजय/g , "ष्" )  ;
     processed_text = processed_text.replace( /-ंउचय/g , "-" )  ;
     processed_text = processed_text.replace( /-हजय/g , "झ" )  ;
     processed_text = processed_text.replace( /-दइेचय/g , "/&nbsp" )  ; 
     processed_text = processed_text.replace( /ाॅ/g , "ॉ" )  ; 

     // now put the processed text in the output box finally.

     document.getElementById(toctrl).value = processed_text  

    } // end of else loop for HTML case

 //--------------------------------------------------

 function Replace_Symbols( )
    {
     //substitute array_two elements in place of corresponding array_one elements

     if ( modified_substring != "" )  // if string to be converted is non-blank then no need of any processing.
        {

// first replace the two-byte nukta_varNa with corresponding one-byte nukta varNas.

modified_substring = modified_substring.replace ( /क़/ , "क़" )  ; 
modified_substring = modified_substring.replace ( /ख़‌/g , "ख़" )  ;
modified_substring = modified_substring.replace ( /ग़/g , "ग़" )  ;
modified_substring = modified_substring.replace ( /ज़/g , "ज़" )  ;
modified_substring = modified_substring.replace ( /ड़/g , "ड़" )  ;
modified_substring = modified_substring.replace ( /ढ़/g , "ढ़" )  ;
modified_substring = modified_substring.replace ( /ऩ/g , "ऩ" )  ;
modified_substring = modified_substring.replace ( /फ़/g , "फ़" )  ;
modified_substring = modified_substring.replace ( /य़/g , "य़" )  ;
modified_substring = modified_substring.replace ( /ऱ/g , "ऱ" )  ;


         for(input_symbol_idx = 0;   input_symbol_idx < array_one_length;    input_symbol_idx++ )
            {
             idx = 0  ;  // index of the symbol being searched for replacement
             
             while (idx != -1 ) //whie-00
                {
                 modified_substring = modified_substring.replace( array_one[ input_symbol_idx ] , array_two[input_symbol_idx] )
                 idx = modified_substring.indexOf( array_one[input_symbol_idx] )

                } // end of while-00 loop
            } // end of for loop
//**********************************************************************************
// Code for Replacing Special glyph : Z (reph+anusvAr)
//**********************************************************************************
   modified_substring = modified_substring.replace( /Z/g , "üं" ) ; // at some places  ì  is  used eg  in "कर्कंधु,पूर्णांक".

//=============================================================================================

// code for replacing  "ç"   with "ि" (chhotee ee kii maatraa) and correcting its position too.

var position_of_f = modified_substring.indexOf( "ç" )  ;

while ( position_of_f != -1 )  //while-02
{
var charecter_right_to_f = modified_substring.charAt( position_of_f + 1 )  ;

modified_substring = modified_substring.replace( "ç" + charecter_right_to_f  ,  charecter_right_to_f + "ि" )  ;

position_of_f = position_of_f + 1  ;

while ( ( modified_substring.charAt( position_of_f + 1 ) == "्" ) & ( position_of_f < modified_substring.length - 1 ) )
{
var string_to_be_replaced = modified_substring.charAt( position_of_f + 1 ) + modified_substring.charAt( position_of_f + 2 )  ;

modified_substring = modified_substring.replace( "ि" + string_to_be_replaced , string_to_be_replaced + "ि" )  ;

position_of_f = position_of_f + 2  ;

}

position_of_f = modified_substring.search( /ç/ , position_of_f + 1 ) ; // search for ç ahead of the current position.

} // end of while-02 loop


//======================================================================================
// code for moving "¨"  at right place and replacing it with "ç" first; and then finally replacing "ç" with "िं" (chhotee ee kii maatraa with anuswaar) and correcting its position too.
//
//var position_of_f = modified_substring.indexOf( "¨" )  ;
//
//while ( position_of_f != -1 )  //while-02
//{
//var charecter_right_to_f = modified_substring.charAt( position_of_f + 1 )  ;
//
//modified_substring = modified_substring.replace( "¨" + charecter_right_to_f  ,  charecter_right_to_f + "ç" )  ;
//
//
//position_of_f = position_of_f + 1  ;
//
//while (( modified_substring.charAt( position_of_f + 1 ) == "्" )  & ( position_of_f < modified_substring.length - 1 ) )
//{
//var string_to_be_replaced = modified_substring.charAt( position_of_f + 1 ) + modified_substring.charAt( position_of_f + 2 )  ;
//
//modified_substring = modified_substring.replace( "ç" + string_to_be_replaced , string_to_be_replaced + "ç" )  ;
//
//position_of_f = position_of_f + 2  ;
//
//}
//
//position_of_f = modified_substring.search( /¨/ , position_of_f + 1 ) ; // search for "¨" ahead of the current position.
//
//} // end of while-02 loop
//
//modified_substring = modified_substring.replace( /ç/g , "िं" )  ;
//======================================================================================
        //Eliminating "ü"(reph) and putting 'half - r' at proper position for this.
        set_of_matras = "ा ि ी ु ू ृ े ै ो ौ ं ः ँ ॅ" 
         var position_of_Z = modified_substring.indexOf( "ü" )

        while( position_of_Z > 0 )  // while-04
            {
             probable_position_of_half_r = position_of_Z - 1 ;
             var charecter_at_probable_position_of_half_r = modified_substring.charAt( probable_position_of_half_r )

             // trying to find non-maatra position left to current ü (ie, half -r).

             while( set_of_matras.match( charecter_at_probable_position_of_half_r ) != null )  // while-05
                {
                 probable_position_of_half_r = probable_position_of_half_r - 1 ;
                 charecter_at_probable_position_of_half_r = modified_substring.charAt( probable_position_of_half_r ) ;

                }// end of while-05

             charecter_to_be_replaced = modified_substring.substr ( probable_position_of_half_r , ( position_of_Z - probable_position_of_half_r ) ) ;
             new_replacement_string = "र्" + charecter_to_be_replaced ; 
             charecter_to_be_replaced = charecter_to_be_replaced + "ü" ;
             modified_substring = modified_substring.replace( charecter_to_be_replaced , new_replacement_string ) ;
             position_of_Z = modified_substring.indexOf( "ü" ) ;
            }// end of while-04
        }//end of IF  statement  meant to  supress processing of  blank  string.

    } // end of the function  Replace_Symbols

 } // end of legacy_to_unicode function
