// JavaScript Document

function convert_to_unicode_dvyogesh(fromctrl, toctrl)
{

var array_one = new Array( 

"$","%","&","*","£Ã",

"+Éä","+Éè","+É","+","<Ç","<",">","=","@","A","Bä","‹ä","B","‹","आå","आé",

"C","D","E","F","G","H","I","J","K","L","M","N","O",
"P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_",

"`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o",
"p","q","r","s","t","u","v","w","x","y","z","{","|","}","~",


"¡","¢","£","¤","¥","¦","§","¨","©","ª","«","®","¯",                 //"¬","­", 
"°","±","²","³","´","µ","¶","•","¸","¹","º","»",

"À","Á","¾","¿","¼","½",

"Â","Ã","Ä","Æ","Å",

// "Ó","Ô","Õ",
//"æ","ç","ê","ë",

"ä","è","Éä","Éè","É","Ò","Ö","×","Ù","Ú","Ý","Þ","ß","्ा",
"å","é","ì",
// "à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï",

"ð","ñ","ò","ó","ô","õ","ö","÷","ø","ù","ú","û","ü","ý","þ",
"ाे","ाै","आॅ","ाॅ" ) 

var array_two = new Array(
"ॐ","ऽ","ः","।","फ़्र",

"ओ","औ","आ","अ","ई","इ","ऊ","उ","ऋ","ॠ","ऐ","ऐ","ए","ए","ओं","औं",

"क्","क़्","क","क़","क्र","क्त","क्ष्","ख्","ख़्","ख्र्","ग्","ग़्","ग्र्",
"घ्","घ्र्","ङ","च्","च्र्","छ","ज्","ज़्","ज्र्","ज्ञ्","झ्","झ्र्","ञ्","ट","ट्ट","ट्ठ",

"ठ","ठ्ठ","ड","ड़","ड्ड","ड्ढ","ढ","ढ़","ण्","त्","त्र्","त्त्","थ्","थ्र्","द","दृ",
"द्र","द्द" ,"द्ध","द्म","द्य","द्व","ध्","ध्र्","न्","न्र्","न्न्","प्","प्र्","फ्","फ़्",

"फ","फ़","फ्र","ब्","ब्र","भ्","भ्र्","म्","म्र्","य्","य्र्","र","रु",                       //"्य","र्",
"रू","ल्","ळ्","ळ","व्","व्र्","श्","श्व्","श्र्","ष्","स्","रु",

"हृ","ह्र","ह्म","ह्य","ह्","ह",

"्","़","ँ","ं","्र",

// "Ó","Ô","Õ",
// "ीं","र्ी","र्ीं",
//"र्ä","र्å","र्è","र्é",

"े","ै","ो","ौ","ा","ी","ु","ु","ु","ू","ू","ृ","ॄ","",
"ें","ैं","ॅ",
// "ॆ","ॆं","र्ॆ","र्ॆं","े","ें","र्े","र्ें","ै","ैं","र्ै","र्ैं","ॅ","ॅं","र्ॅ","र्ॅं",

"","","","","","","","","","","","","","","",
"ो","ौ","ऑ","ॉ" )

//**************************************************************************************
// The following two characters are to be replaced through proper checking of locations:
//**************************************************************************************
// "Ç" 
// "र्" (reph) 
//
// "Ê"  or "Î"
// "ि"  
//**************************************************************************************

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 DV-TTYogeshEN  to convert into Unicode."  ;  

    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

//**********************************************
//    alert("text size = "+text_size);
//**********************************************

    var sthiti1 = 0 ;  var sthiti2 = 0 ;  var chale_chalo = 1 ;
 
    var max_text_size = 6000;

    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 }
      
//     alert(" sthiti 1 = "+sthiti1); alert(" sthit 2 = "+sthiti2) 

     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( /mangal/g , "DV-TTYogeshEN " ) ;   

    document.getElementById(toctrl).value = processed_text  ;
   }

}

else    // if input is HTML then

{

document.getElementById(toctrl).value = "You have chosen HTML TEXT in DV-TTYogeshEN  to convert into Unicode."  ;  

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 occure 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( /Sanskrit 99/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 stringto be converted is non-blank then no need of any processing.
{
for ( input_symbol_idx = 0;   input_symbol_idx < array_one_length;    input_symbol_idx++ )

{ 
//***********************************************************
// if (input_symbol_idx==106) 
//   { alert(" input_symbol_idx = "+input_symbol_idx);
//     alert(" character = "+string.fromCharCode(input_symbol_idx)) 
//   }
// if (input_symbol_idx == 107) 
//   { alert(" input_symbol_idx = "+input_symbol_idx);
//    alert(" character = ",+string.fromCharCode(input_symbol_idx)) 
//   }
//***********************************************************
idx = 0  ;  // index of the symbol being searched for replacement

while (idx != -1 ) //while-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

//alert(" modified substring = "+modified_substring)

//**********************************************************************************
// Code for Replacing five Special glyphs
//**********************************************************************************
//"æ","ç","ê","ë",
//"र्ä","र्å","र्è","र्é",    

//**********************************************************************************
// Code for Glyph1 : È (reph+anusvAr)
//**********************************************************************************
   modified_substring = modified_substring.replace( /È/g , "Çं" ) ; // at some places  ì  is  used eg  in "कर्कंधु,पूर्णांक".

//**********************************************************************************
// Code for Glyph2 : ç (ekAr+reph+anusvAr)
//**********************************************************************************
   modified_substring = modified_substring.replace( /ç/g , "Çें") ; 

//**********************************************************************************
// Code for Glyph3 : æ (ekAr+reph)
//**********************************************************************************
   modified_substring = modified_substring.replace( /æ/g , "Çे") ; 

//**********************************************************************************
// Code for Glyph4 : ë (aikAr+reph+anusvAr)
//**********************************************************************************
   modified_substring = modified_substring.replace( /ë/g , "Çैं") ; 

//**********************************************************************************
// Code for Glyph5 : ê (aikAr+reph)
//**********************************************************************************
   modified_substring = modified_substring.replace( /ê/g , "Çै") ; 

//**********************************************************************************
// Code for Glyph6 : Ô ( reph + ी )
//**********************************************************************************
   modified_substring = modified_substring.replace( /Ô/g , "Çी" ) ; 

//**********************************************************************************
// Code for Glyph7 : Ó ( ी + anusvAr )
//**********************************************************************************
   modified_substring = modified_substring.replace( /Ó/g , "ीं" ) ; 

//**********************************************************************************
// Code for Glyph8 : Õ ( reph + ी + anusvAr )
//**********************************************************************************
   modified_substring = modified_substring.replace( /Õ/g , "Çीं" ) ; 


//**********************************************************************************
// Code for Glyph09 : Ì  ( reph + ि )
// Code for Glyph10 : Î  (ि) [ikAr mAtrA before a संयुक्ताक्षर as in क्लिष्ट, स्थित etc)
//   replace "Ê" and "Î" with "ि" and correcting its position too(moving it one position forward)
//**********************************************************************************
   modified_substring = modified_substring.replace( /Ì/g , "र्Ê" ) ;  // at some places  Ì  is  used eg  in "धार्मिक".
   modified_substring = modified_substring.replace( /Î/g , "Ê" ) ;  

var position_of_i = modified_substring.indexOf( "Ê" )

while ( position_of_i != -1 )  //while-02
{
var charecter_next_to_i = modified_substring.charAt( position_of_i + 1 )
var charecter_to_be_replaced = "Ê" + charecter_next_to_i
modified_substring = modified_substring.replace( charecter_to_be_replaced , charecter_next_to_i + "ि" ) 
position_of_i = modified_substring.search( /Ê/ , position_of_i + 1 ) // search for i ahead of the current position.

} // end of while-02 loop

//**********************************************************************************
// Code for Glyph8 : Ë ("िं") 
// Code for Glyph9 : Í ("र्िं")
//   replace Ë with "िं"  and correcting its position too(moving it two positions forward)
//**********************************************************************************
modified_substring = modified_substring.replace( /Í/g , "र्Ë" ) ; // at some places  Í  is  used eg  in "शर्मिंदा"
modified_substring = modified_substring.replace( /Ë/g , "ÊÆ" ) ; // at some places  Ë  is  used eg  in "किंकर".

var position_of_i = modified_substring.indexOf( "ÊÆ" )

while ( position_of_i != -1 )  //while-02
{
var charecter_next_to_ip2 = modified_substring.charAt( position_of_i + 2 )
var charecter_to_be_replaced = "ÊÆ" + charecter_next_to_ip2
modified_substring = modified_substring.replace( charecter_to_be_replaced , charecter_next_to_ip2 + "िं" ) 
position_of_i = modified_substring.search( /ÊÆ/ , position_of_i + 2 ) // search for i ahead of the current position.

} // end of while-02 loop


//**********************************************************************************
// End of Code for Replacing four Special glyphs
//**********************************************************************************

// following loop to eliminate 'chhotee ee kee maatraa' on half-letters as a result of above transformation.

var position_of_wrong_ee = modified_substring.indexOf( "ि्" ) 

while ( position_of_wrong_ee != -1 )  //while-03

{
var consonent_next_to_wrong_ee = modified_substring.charAt( position_of_wrong_ee + 2 )
var charecter_to_be_replaced = "ि्" + consonent_next_to_wrong_ee 
modified_substring = modified_substring.replace( charecter_to_be_replaced , "्" + consonent_next_to_wrong_ee + "ि" ) 
position_of_wrong_ee = modified_substring.search( /ि्/ , position_of_wrong_ee + 2 ) // search for 'wrong ee' ahead of the current position. 

} // end of while-03 loop


// Eliminating reph "Ç" and putting 'half - r' at proper position for this.
set_of_matras = "ा ि ी ु ू ृ े ै ो ौ ं : ँ ॅ" 
var position_of_R = modified_substring.indexOf( "Ç" )

while ( position_of_R > 0 )  // while-04
{
probable_position_of_half_r = position_of_R - 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 O (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_R - 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_R = modified_substring.indexOf( "Ç" ) ;

} // end of while-04

} // end of IF  statement  meant to  supress processing of  blank  string.

   modified_substring = modified_substring.replace( /ाे/g , "ो") ; 
   modified_substring = modified_substring.replace( /ाै/g , "ौ") ; 


} // end of the function  Replace_Symbols


} // end of legacy_to_unicode function

