// 	Author: Joju John	
// 	$Date: 2006-09-27 	
<!-- 	Modified: 	 -->
<!-- 	$Date: Aug 7,2007 -->
// dictionary.js

var xmlHttp;
var Gsid=0;
var Gmode ='';
var Gstr = '';
var Ginput_str = '';


var attempt_no =0;

var Guser_name='NONAME';
var Guser_sid =0;

var temp_str = '';
var span_meaning_str = "";
var span_matching_str = '';
var span_malbase_str = '';

var next_or_show_next = '0'; // 0 means next buttion
                       // 1 means Show next comment
                       
var pr_report_inline = '';

var result_meaning_found=0;
var result_matching_found=0;
var got_result='XXX';
var time_taken=0;

//------------------ DICT MODE _--------------------------
var dict_mode = 0; // maleng
           // = 1    engmal
           // = 2    unknown
    
function set_dict_maleng() {
   dict_mode = 0;  
   
   document.getElementById("input_box_id").value = '';
   document.getElementById("input_box_id").focus();
   document.getElementById("txtHint").innerHTML = '&nbsp;';
   document.getElementById("timeHint").innerHTML = '';
   
   delay_timer_stop();
   //turn_on_double_click();
   buttons_set_turn_on();
   get_malayalam_keyboard();
   document.getElementById("mal_box_id").value = '';
   document.getElementById("contentsHint").innerHTML = 'Contains 61,952 words with meaning + 4,53,716 Spell correcting words';
}

function set_dict_engmal() {
   dict_mode = 1;  
   document.getElementById("mal_box_id").value = '';
   document.getElementById("input_box_id").value = '';
   document.getElementById("input_box_id").focus();
   document.getElementById("txtHint").innerHTML = '&nbsp;';
   document.getElementById("timeHint").innerHTML = '';
   document.getElementById("contentsHint").innerHTML = 'Contains 27,929 words with meaning + 75,287 Spell correcting words';
   
   delay_timer_stop();
   //turn_on_double_click();
   buttons_set_turn_on();
   
}

function is_eng_dict() {

   //alert(dict_mode);
   return dict_mode;
     

}

function clear_contentsHint() {
     document.getElementById("contentsHint").innerHTML = '';
}

//------------------ DICT MODE _--------------------------

//------------------- TIMER -------------------------------

// FIVE SECOND wait for result
var timer_id = 0;  /// timer to detect network delay
var toggle_msg =0;
function timer_start() {
   timer_id = setTimeout("timer_timeout()", 10000);
}

function timer_stop() {
   if(timer_id) {
      clearTimeout(timer_id);
      timer_id  = 0;
   }
}

function timer_timeout() {   
 
    if (toggle_msg ==0) {
        toggle_msg =1;
        norecorded_result("Suspecting that your internet is too slow... <br><br>OR<br><br>Mashithantu Server has too many pending request to serve:-(");
        timer_stop();
        timer_start();
    } else {
        toggle_msg =0;
        norecorded_result(
               "<img border=\"0\" src=\"img/delete.PNG\" width=\"19\" height=\"19\">Sorry for the delay... could you please try again after sometime? Thank You");
        timer_stop();
        discard_magic("TRYAGAIN20sec");
    }    
}

// TWO SECOND DELAY ....at input
var timer_delayed_start_id =0; // timer to cut down the traffic to server

function delay_timer_stop() {
   if(timer_delayed_start_id) {
      clearTimeout(timer_delayed_start_id);
      timer_delayed_start_id  = 0;
   }
}
function delay_timer_start() {
   delay_timer_stop();
   timer_delayed_start_id = setTimeout("get_matching_str()", 2000);
}



//-------------------- TIMER END ------------------------------


// -- measure search time
var start_time = new Date();
var end_time = new Date();


function search_time_display()
{
   time_taken =  (end_time.getTime() - start_time.getTime())/1000;

   document.getElementById("timeHint").innerHTML = "Suggestions...("+ time_taken + " seconds)";
}

function search_time_abort()
{
   time_taken =  (end_time.getTime() - start_time.getTime())/1000;
   
}

function search_start_time()
{
  start_time = new Date();
  time_taken =0;
  document.getElementById("timeHint").innerHTML = "";
}

function search_end_time()
{  
   end_time = new Date();
   search_time_display();
}



// END measure search time

//-------------------- PREV NEXT----------------
//undo package: un_
var un_mytexts = new Array();
var un_max_save = 50;
var un_recent_ptr = 0;
var last_recoreded = '';

function un_save_text(newtext)
{
   var length = un_mytexts.length;

   if (length < un_max_save ) {
      un_mytexts[length] = "";
      length +=1;
   }
   for (i=0;i< length-1;i++) {
       un_mytexts[length-1 -i] = un_mytexts[length-1 -i-1];
   }
   un_mytexts[0] = newtext;
   un_recent_ptr = 0;
}


function un_get_undo_text()
{
   if ( un_recent_ptr < (un_mytexts.length-1) )
   {
       un_recent_ptr +=1;
   }
//   if ( un_recent_ptr == (un_mytexts.length-1) )
//      disable_prev_but();
   
   return un_mytexts[un_recent_ptr];
}

function un_get_redo_text()
{
   if (un_recent_ptr > 0)
       un_recent_ptr -=1;

//   if (un_recent_ptr == 0 )
//    disable_next_but();
   return un_mytexts[un_recent_ptr];
}

function display_it_in_manglish()
{
    recorded_result_manglish(covert_mal2mang(last_recoreded));
    discard_magic("MANGLISH_REQ");
    
}

function get_formatted_result(result)
{

  if ( Gmode == 'E_EXPLUS') {
       return "<p>"
               + result
               + "</p>";      
  }


  return "<p align=\"right\"> <input type=\"button\" value=\"Display in Manglish\" onclick=\"display_it_in_manglish();\" style=\"float: right\">  </p>"
         + "<p>"
         + result
         + "</p>";

}

function recorded_result_manglish(newresult)
{

    show_result_span();
    un_save_text( newresult ); 
    last_recoreded = newresult;
    
    
    document.getElementById("txtHint").innerHTML = newresult 
                                                  + "<br><img border=\"0\" src=\"img/warning.PNG\" width=\"19\" height=\"19\">"
                                                  + "<font color=\"red\">Warning: Double clicking on manglish words may bring up unexpected or wrong result because the system may mistake manglish words as English and search for corresponding words and meanings..</font><br><br>";
}

function add_words_recorded_result(newresult)
{
   document.getElementById("txtHint").innerHTML = newresult;
}

function recorded_result(newresult)
{
    show_result_span();
    un_save_text( newresult ); 
    last_recoreded = newresult;
    
    if ((result_meaning_found==0) && (result_matching_found==0) ){
    
       if ( (is_any_mal_letters(document.getElementById("input_box_id").value )==0) 
            && (is_eng_dict()==0) 
            && (is_any_mix_caps_underscore(document.getElementById("input_box_id").value ) == 0)
          ) {
            newresult = newresult          
	                + "<br><img border=\"0\" src=\"img/info.PNG\" width=\"19\" height=\"18\">"
                        + "&nbsp;&nbsp;<blink>Tips</blink>: if you are searching for the meaning of an English Word,<br>"
                        + "Please change the mode to 'English Dictionary' ";
       }

       document.getElementById("txtHint").innerHTML = newresult;
    }   else  if ((result_meaning_found==1) || (result_matching_found==1)){
    
          newresult = newresult          
                       + "<img border=\"0\" src=\"img/info.PNG\" width=\"19\" height=\"18\">"
                       + "&nbsp;&nbsp;<blink>Hint</blink>: Double Click on the word(s) to get its meaning."
                       + "<br><br>&nbsp;&nbsp;<a href='http://www.dictionary.mashithantu.com/malayalam_words/new'>Add new words </a> &nbsp; |&nbsp; <a href='http://mashithantu.com/blog/?p=8#respond'>Post Comments</a> &nbsp; |&nbsp; <a href='help.htm'>Help</a>  " 
                      ;
          document.getElementById("txtHint").innerHTML = get_formatted_result(newresult);
    
    }
    
    if (Gmode == 'E_EXACT' || Gmode == 'E_EXPLUS' || Gmode == 'E_EXBOTH') {
       // document.getElementById("txtHint").innerHTML = newresult;
    } else {
       // document.getElementById("txtHint").innerHTML = get_formatted_result(newresult);
    }

    result_meaning_found=0;
    result_matching_found=0;
    set_keyboard_off();
    enable_prev_but();
    Ginput_str = 'CLEAR_INPUT';
 
}


function norecorded_result(justshowresult)
{
    show_result_span();
    document.getElementById("txtHint").innerHTML = justshowresult;
    set_keyboard_off();
}


function prev_button()
{  
   enable_next_but();
   un_get_undo_text();// just adjusting un_recent_ptr
   document.getElementById("txtHint").innerHTML = un_mytexts[un_recent_ptr];
   set_keyboard_off();
   turn_on_double_click();
   
}
function next_button()
{
   enable_prev_but();
   un_get_redo_text(); // adjust
   document.getElementById("txtHint").innerHTML = un_mytexts[un_recent_ptr];
   set_keyboard_off();
   turn_on_double_click();
}

//-------------------- PREV NEXT END----------------


// -----------------double click---------------------

var double_click_status = 1;

function turn_on_double_click()
{
  //alert("turn_ON_double_click");
  double_click_status = 1;
}

function turn_off_double_click()
{
  //alert("turn_off_double_click");
  double_click_status = 0;
}

function is_double_click_on()
{
  return double_click_status;
}

function buttons_set_turn_on()
{
  //enable_unsatisfied_no_but();
  //enable_prev_but();
  
  return;
}
function buttons_set_turn_off()
{
  disable_unsatisfied_no_but();
  disable_satisfied_yes_but();
  //disable_prev_but();
  return;
}




// -----------------double click END---------------------


//var Guser_name='NONAME';
//var Guser_sid =0;


function ajax_magic_core(url_str)
{

  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  } 
  xmlHttp.onreadystatechange=stateChanged;
  xmlHttp.open("GET",url_str,true);
  xmlHttp.send(null);
  
}
function ajax_magic_dict(mode)
{
   if (mode == 'ADD_EXEC') {
      return;   
   }
   
   if (html_page == 'addwords')
      return;
   if (mode == 'DISCARD')
      return;
      
   set_keyboard_on();
   
   pr_hide_submit(); 
   timer_stop();
   timer_start();
   
   buttons_set_turn_on();
   search_start_time();
   delay_timer_stop();
   got_result='START_SEARCH';
   

   turn_on_double_click();

}

function ajax_magic_sanity(str, mode)
{
  if (str.length==0)
  {
     Gsid=0;
     Gmode ='';
     Gstr = '';
     if (mode == 'EXACT' || mode == 'EXPLUS' || mode == 'STRENGTH') {
         norecorded_result('Please Type Manglish in the box given above');
     }
     if (mode == 'E_EXACT' || mode == 'E_EXPLUS' || mode == 'E_STREN') {
         norecorded_result('Please Type English Words in the box given above');
     }
     return 1;
  }
  return 0;
}

function ajax_magic_dict_admin(str, mode)
{
  sid = Math.random();
  if (mode !='COUNT' || mode != 'DISCARD') {
     Gsid = sid;
     Gmode = mode;
     Gstr = str;
  } else {
     Guser_sid = sid;
     Guser_name= str;
  
  }
  var url= script_url;
  url=url+"?word="+encodeURIComponent(str);
  url=url+"&mode="+mode;
  url=url+"&sid="+sid;
  
  if (mode == 'DISCARD'){
        return url;
  }

  if (html_page=="nighantu") {
     url=url+"&input_box=" + encodeURIComponent(document.getElementById("input_box_id").value ); 
     url=url+"&mal_box=" + encodeURIComponent(document.getElementById("mal_box_id").value );   
  }  
  if (html_page=="addwords") {
     url=url+"&input_box=" + encodeURIComponent(document.getElementById("word_box_id").value ); 
     url=url+"&mal_box=" + encodeURIComponent(document.getElementById("word_box_id").value);  // not thinking about add-word-mode 
  }    
  url=url+"&atpt_no="+attempt_no++;
  
  if (attempt_no > 1234) {
     alert ('A lot of keystrokes encountered from this page...\n...may be your "Enter" key has got stuck :-(\nPlease refresh your browser to avoid this message again');
  }
  return url;
}

function ajax_magic(str, mode, unarg1, unarg2, unarg3, unarg4)
{

  //alert('ajax_magic');
  if (ajax_magic_sanity(str, mode) == 1 )
      return;
 
  ajax_magic_dict(mode);
 
  var url = ajax_magic_dict_admin(str, mode);

  if (mode == 'PR_UNSATISFIED') {
      
      url=url+"&inputtext="+ encodeURIComponent(unarg1);
      url=url+"&mal_text="+ encodeURIComponent(unarg2);
      url=url+"&displayed="+ encodeURIComponent(unarg3); // this function call makes problem
      url=url+"&dict_mode="+ encodeURIComponent(unarg4);
      url=url+"&last_r="+ encodeURIComponent(last_recoreded); // doing the duty of displayed
      last_recoreded = 'reset';
      
  }
  if (mode == 'ADD_EXEC') {
      //unarg1 is mode_url
     url=url+ unarg1;
     ajax_magic_core(url);
     
     //document.getElementById("name_id").value = 'ANILA';
     return;
  }  
 
  ajax_magic_core(url);
} 

function discard_magic(str)
{
    ajax_magic(str, 'DISCARD', 'chumma', 'chumma', 'chumma', 'chumma');
}


//add_words specific 
function check_availability_mal(str)
{     
     add_words_get_meaning(covert_exp_mal(convert_manglish(str)));
}

function check_availability_eng(str)
{
     //document.getElementById("name_id").value = "JOHN";   
     add_words_eng_get_meaning(str);
}

function check_availability(str)
{

     //alert('str=' + str + 'mode=' + add_word_mode);
     if (add_word_mode == 0) {
        check_availability_mal(str);
     } else {
        check_availability_eng(str);
     }
}


// -------------- split str -----

/// check this
function clickable_split_str_action(word, mode)
{
   if ( mode == 'E_EXPLUS' || mode == 'E_EXACT' || mode == 'E_STREN' || mode == 'E_EXBOTH' ) {
      if (html_page == "addwords") {
         document.getElementById("word_box_id").value = word ;
      } else {
         document.getElementById("input_box_id").value = word ;
         document.getElementById("mal_box_id").value = word ;
      }
   } else {
      if (html_page == "addwords") {
          document.getElementById("word_box_id").value = covert_exp_mal('%'+word);
          
      } else {
          document.getElementById("input_box_id").value = covert_exp_mal('%'+word) ;
          document.getElementById("mal_box_id").value = covert_exp_mal('%'+word) ;   
      }
      word = covert_exp_mal(word);
   }
   if (mode == 'EXPLUS') { 
      get_matches(word);
      
   } else if (mode == 'EXACT') { 
      get_meaning(word);
      
   } else if (mode == 'STRENGTH') { 
      get_word_strength(word);
      
   } else if (mode == 'E_EXPLUS') { 
      eng_get_matches(word);
         
   } else if (mode == 'E_EXACT') { 
      eng_get_meaning(word);
   } else if (mode == 'E_STREN') { 
      eng_get_word_strength(word);
      
   }else if (mode == 'E_EXBOTH') { 
      eng_get_both_meaning_n_matching(word);
      
   }else if (mode == 'EXBOTH') { 
      get_both_meaning_n_matching(word);
      
   }

}

function clickable_split_str(word, mode)
{
   if ( mode == 'E_EXPLUS' || mode == 'E_EXACT' || mode == 'E_STREN'|| mode == 'E_EXBOTH' ) {
      return "<a href=\"#\" onclick=\"clickable_split_str_action('"+ word + "','"+ mode + "'); return false;\"> " +word+ "</a>";
   } else {  
      return "<a href=\"#\" onclick=\"clickable_split_str_action('"+ word + "','"+ mode + "'); return false;\"> " + covert_exp_mal('%'+word)+ "</a>";
   }
} 

function make_clickable_split_str(word, mode)
{
   var x;
   var count=0;
   var ret_str = '';
   var single_str = '';
   var temp = new Array();
   temp = word.split(' ');
   
   //alert(word + mode);
   for (x in temp) {
       if (temp[x] == '' || temp[x] == ' ' )
          continue;
       if (ret_str == '') {
           count++;
           ret_str =  clickable_split_str(temp[x], mode); 
           single_str = temp[x];
       } else {
           count++;
           ret_str = ret_str +  ' + ' + clickable_split_str(temp[x], mode);
       }
   }
    
   if (count ==1) 
      return single_str;
      

   
   if (count ==0) {
      //document.getElementById("mal_box_id").value = 'Please remove spaces and type some words above';
      norecorded_result ('Please remove spaces and type some words in the first box');
      disable_unsatisfied_no_but();
      return 'NOTREQD';
   }
   display_str = 'This Phrase NOT found in this dictionary. Please try these words separately <br>';  
   
   display_str += covert_exp_mal('%' + 'R hdj/aix c<fldlCd\'d vfICd\'bf> nYd/cndn. o/be/f ~jaja hoUdUNe/f lfj/t...' +'%');
   
   display_str += '<br>';
   result_matching_found =1; // just to fake the tips
 
   recorded_result(display_str + ret_str);
   
   return 'NOTREQD';
}


// -------------- split str end-----

function get_matches(str)
{
  Ginput_str = str;
  if ( (str = make_clickable_split_str(str, 'EXPLUS')) != 'NOTREQD') {
    temp_str = covert_exp_mal('%' +  'o/be/f keldlfjfkdkt... ' +str+ '- Zvdv holdlfvdJz mecd/UdU*kdke/gNdN lfj;d;f> lg\'jgvdvg.' + '%');
    temp_str += "<br>Please wait... we are searching for the matches of "+covert_exp_mal('%'+str+'%');
    norecorded_result(temp_str);
 
    ajax_magic(str, 'EXPLUS');
    enable_unsatisfied_no_but();
    enable_satisfied_yes_but();
  }
}

function get_meaning(str)
{
  Ginput_str = str;
  
  if ( (str =make_clickable_split_str(str, 'EXACT')) != 'NOTREQD') {
     temp_str = covert_exp_mal('%' +  'o/be/f keldlfjfkdkt... ' +str+ '- Zvdv holdlfvdJz D|ldLldlfve/gNdN Dvdbs<Cx EjxYfkdkgvdvg' + '%');
     temp_str +="<br>Please wait... we are searching for the meaning of "+covert_exp_mal('%'+str+'%');
     
     norecorded_result(temp_str);
     ajax_magic(str, 'EXACT');

     enable_unsatisfied_no_but();
     enable_satisfied_yes_but();
  } 
}

function add_words_get_meaning(str)
{
  ajax_magic(str, 'EXACT');
}

function add_words_eng_get_meaning(str)
{
  ajax_magic(str, 'E_EXACT');
}

function get_both_meaning_n_matching(str)
{
  Ginput_str = str;
  if ( (str =make_clickable_split_str(str, 'EXBOTH')) != 'NOTREQD') {
     temp_str = covert_exp_mal('%' +  'o/be/f keldlfjfkdkt... ' +str+ '- Zvdv holdlfvdJz D|ldLldlfve/gNdN Dvdbs<Cx EjxYfkdkgvdvg' + '%');
     temp_str +="<br>Please wait... we are searching for the meaning of "+covert_exp_mal('%'+str+'%');
     
     norecorded_result(temp_str);
     ajax_magic(str, 'EXBOTH');
     enable_unsatisfied_no_but();
     enable_satisfied_yes_but();
  } 
}



// ENG 
function eng_get_matches(str)
{
   Ginput_str = str;
 // if ( (str = make_clickable_split_str(str, 'E_EXPLUS')) != 'NOTREQD') {
  
      str= str.replace(/[ -]/g, "");
      norecorded_result("<br>Please wait... we are searching for the matches of "+ str);
      ajax_magic(str, 'E_EXPLUS');
      enable_unsatisfied_no_but();
      enable_satisfied_yes_but();
  //} 
}



function eng_get_both_meaning_n_matching(str)
{
  Ginput_str = str;
  //if ( (str = make_clickable_split_str(str, 'E_EXBOTH')) != 'NOTREQD') {

      str= str.replace(/[ -]/g, "");
      
      
      norecorded_result("<br>Please wait... we are searching for the meaning of "+ str);
      ajax_magic(str, 'E_EXBOTH');
      enable_unsatisfied_no_but();
      enable_satisfied_yes_but();
  //} 

}

function get_counter(str)
{
  ajax_magic(str, 'COUNT');
}



function get_init_str()
{


  if (is_eng_dict() == 0) {
      document.getElementById("mal_box_id").value = convert_manglish(    document.getElementById("input_box_id").value);
      init_str = covert_exp_mal(document.getElementById("mal_box_id").value);
      ajax_magic(init_str, 'EXACT'); // previously INIT
  } else {
      document.getElementById("mal_box_id").value = document.getElementById("input_box_id").value;
      init_str = document.getElementById("mal_box_id").value;
      document.getElementById("txtCount").innerHTML= "<table class=\"bot\" border=\"1\" width=\"100%\" style=\"font-size:80%;padding:0 0 4px\" id=\"AutoNumber1\">  <tr> <td>&nbsp;</td><td width=\"10%\" align=\"left\">[?] </td></tr><tr><td>Please refresh your browser (Press Ctrl + F5) to get the counter stats </td>   </tr>      <tr>        <td>&nbsp;</td>       </tr>          </table>";
      eng_get_meaning(init_str);
  }
}

var mode_pattern = "(#(.*?)#)";
var word_pattern = "(##(.*?)#)";  
var meaning_pattern = "(#(.*?)#)";


//------------------ VERSION CHECK  _--------------------------
dict_version = 'DICTVER16';

function check_dict_version(str)
{
  index = str.search(mode_pattern);       
  if(index == 0){
      re_mode = new RegExp(mode_pattern);
      var mode_array = re_mode.exec(str);
      dict_ver = mode_array[2]; 
      str = str.substring(mode_array[0].length);
  }
  if (dict_ver != dict_version) {
      norecorded_result('Please refresh your browser (Press Ctrl + F5)');
      return 'refresh';
  }
 
  return str;
}

//------------------ VERSION CHECK DONE _--------------------------


//parse meaning  ##word#malayalam#english#

var mal_meaning_pattern = "(##(.*?)#(.*?)#(.*?)#)";
function parse_show_meaning(str, mal_box)
{

  while (str.length >0) {
	  index = str.search(mal_meaning_pattern);       
	  if(index == 0){
	      flag = 0;
	      re_mode = new RegExp(mal_meaning_pattern);
	      var mode_array = re_mode.exec(str);
	      retword = mode_array[2]; 
	      ret_mal_meaning = mode_array[3]; 
	      ret_eng_meaning = mode_array[4]; 
	      str = str.substring(mode_array[0].length );   
	  } else {
	      str = str.substring(1);
	      continue;
	  }
  
	  if (retword == 'NOWORDEXACT') {
	      result_meaning_found=0;
	      if (html_page=="nighantu") {
	         span_meaning_str = check_for_capitals(document.getElementById("input_box_id").value, dict_mode);
	      }
	      span_meaning_str = span_meaning_str + "<b>" + mal_box+ "</b>" + convert_manglish(' enna padaththinte ar_ththham labhyamalla. ');
	      got_result ='NOWORDEXACT';
	      //alert('check the input');
	      return  'NOWORDEXACT';
	  } else {
	      result_meaning_found=1;
	      span_meaning_str = span_meaning_str 
	                        + "<br><b>" + covert_exp_mal('%'+retword+'%') + " :</b>"
	                        + "<a href="+make_url_word_str(covert_exp_mal('%'+retword+'%'))+"><img border=\"0\" src=\"img/shortcut.PNG\" width=\"11\" height=\"11\"></a>"
	                        + "<br>";	     
	      if (ret_eng_meaning == '_SYN_') {
	     	  span_meaning_str = span_meaning_str 
	     	                   + convert_manglish(' paryaayam_ : ')
	     	                   + "<i>"
	     	                   + covert_exp_mal('%'+ret_mal_meaning+'%') 
	     	                   + "</i> <br>"; 
	      } else if (ret_eng_meaning == '_AYM_') {
	     	  span_meaning_str = span_meaning_str 
	     	                   + convert_manglish(' vipareetham_ : ')
	     	                   + "<i>" 
	     	                   + covert_exp_mal('%'+ret_mal_meaning+'%') 
	     	                   + "</i> <br>"; 
	      } else {

	          //span_meaning_str = span_meaning_str  +  "<font color=\"blue\">";
	          if (ret_mal_meaning.length > 1) {
	              span_meaning_str = span_meaning_str + covert_exp_mal('%'+ret_mal_meaning+'%') + " <br>"; 
	          }
	          if (ret_eng_meaning.length > 1) {
	              // ret_eng_meaning = ret_eng_meaning.replace(/</, "&lt;");
	              span_meaning_str = span_meaning_str + ret_eng_meaning+"<br><br>";  
	          }
	          //span_meaning_str = span_meaning_str  + "</font>";
	      }
	  }
   } //while
   
   return 'DONE';
}


//parse meaning  ##engword#malayalam#english#

var eng_meaning_pattern = "(##(.*?)#(.*?)#(.*?)#)";
function eng_parse_show_meaning(str, mal_box)
{

  while (str.length >0) {
	  index = str.search(eng_meaning_pattern);       
	  if(index == 0){
	      flag = 0;
	      re_mode = new RegExp(eng_meaning_pattern);
	      var mode_array = re_mode.exec(str);
	      retword = mode_array[2]; 
	      ret_mal_meaning = mode_array[3]; 
	      ret_eng_meaning = mode_array[4]; 
	      str = str.substring(mode_array[0].length );   
	  } else {
	      str = str.substring(1);
	      continue;
	  }
  
	  if (retword == 'NOWORDEXACT') {
	      result_meaning_found=0;
      
	      span_meaning_str = span_meaning_str + "<br>No meaning available for <b>" + mal_box+ "</b> in this dictionary.";
	      got_result ='NOWORDEXACT';

	      return  'NOWORDEXACT';
	  } else {
	      result_meaning_found=1;
	      span_meaning_str = span_meaning_str 
	                        + "<br><b>" + retword + " :</b>"
	                        + "<a href="+make_url_word_str(retword)+"><img border=\"0\" src=\"img/shortcut.PNG\" width=\"11\" height=\"11\"></a>"
	                        + "<br>";	     
	      if (ret_eng_meaning == '_SYN_') {

	      } else if (ret_eng_meaning == '_AYM_') {

	      } else {

	          //span_meaning_str = span_meaning_str  +  "<font color=\"blue\">";
	          if (ret_mal_meaning.length > 1) {
	              span_meaning_str = span_meaning_str + covert_exp_mal('%'+ret_mal_meaning+'%') + " <br>"; 
	          }
	          if (ret_eng_meaning.length > 1) {
	              // ret_eng_meaning = ret_eng_meaning.replace(/</, "&lt;");
	              span_meaning_str = span_meaning_str + ret_eng_meaning+"<br><br>";  
	          }
	          //span_meaning_str = span_meaning_str  + "</font>";
	      }
	  }
   } //while
   
   return 'DONE';
}

function copy_to_mal_box(word,mode)
{
   document.getElementById("input_box_id").value = word ;
   change_mode_if_require();
   find_meaning();
}

function make_clickable_str(word, mode)
{
  
  if (mode == 'E_EXPLUS') {
      return "<a href=\"#\" onclick=\"copy_to_mal_box('"+ word + "','"+ mode + "'); return false;\"> " +word+ "</a>";
  } else {
      // MALAYALAM word
      word = word.replace(/'/g,'\\\'');
      return "<a href=\"#\" onclick=\"copy_to_mal_box('"+ covert_exp_mal('%'+word+'%')  + "','"+ mode + "'); return false;\"> " + covert_exp_mal('%'+word+'%') 
 + "</a>";
      //return covert_exp_mal('%'+word+'%') ;
  }
}

function make_clickable_str_old(word)
{
  
  if (Gmode == 'E_EXPLUS') {
      return "<a href=\"#\" onclick=\"copy_to_mal_box('"+ word + "','"+ Gmode + "'); return false;\"> " +word+ "</a>";
  } else {
      // MALAYALAM word
      word = word.replace(/'/g,'\\\'');
      return "<a href=\"#\" onclick=\"copy_to_mal_box('"+ covert_exp_mal('%'+word+'%')  + "','"+ Gmode + "'); return false;\"> " + covert_exp_mal('%'+word+'%') 
 + "</a>";
      //return covert_exp_mal('%'+word+'%') ;
  }
}

//mode == EXBOTH // word == malayalam 
function make_mal_clickable_str(word)
{

   word = word.replace(/'/g,'\\\'');
   return "<a href=\"#\" onclick=\"copy_to_mal_box('"+ covert_exp_mal('%'+word+'%')  + "','"+ 'EXBOTH' + "'); return false;\"> " 
           + covert_exp_mal('%'+word+'%') 
           + "</a>";
      

}


function make_non_clickable_str(word, mode)
{

    if (mode == 'E_EXPLUS') {
       return word;
    } else {
       return  covert_exp_mal('%'+word+'%') ;
    }

}
function make_url_word_str(word)
{
    url= "http://"+window.location.hostname+window.location.pathname;
    //alert ("window.location.hostname="+window.location.hostname + " window.location.pathname=" +window.location.pathname);
    return url+"?word="+word;

}


//parse matching 
//input #word1##..##wordn#
// or   #NOWORD#
// or   #word1##..##wordn##MOREWORDS##1234#

var matchword_pattern = "(#(.*?)#)";

function parse_show_matching(str,mode, mal_box)
{
  var first_word = 1;
  var more_flag =0;
  var count_it =0;
  
  
  while (str.length !=0) {
      index = str.search(matchword_pattern);      
      if(index == 0){
      
          re_mode = new RegExp(matchword_pattern);
          var mode_array = re_mode.exec(str);
          retword = mode_array[2]; 
          str = str.substring(mode_array[0].length);                  
      } else {
          //error 
          str = str.substring(1);
          continue;               
      }      
      if (retword == 'NOWORD' ) {
          // display it and quit TBD

          got_result ='NOWORD';
                  
          if (mode =='E_EXPLUS' ) {
              result_matching_found = 0;
              span_matching_str = "No matching words are available for <b>" + mal_box+ "</b> in this dictionary." ;
          }
          if (mode =='EXPLUS' ) {
              result_matching_found = 0;
              span_matching_str = "<b>" + mal_box+ "</b>"  
                                        + convert_manglish(' enna padaththinu~ saamyamuLLa padanGaL_ labhyamalla. ');
          }          
          return 'NOWORD';
          //break;
          
      }
      if (retword == 'MOREWORDS' ) {
          index = str.search(matchword_pattern);          
          if(index == 0){
        
            re_mode = new RegExp(matchword_pattern);
            var mode_array = re_mode.exec(str);
            retword = mode_array[2]; 
            str = str.substring(mode_array[0].length);         
                    
          }
          span_matching_str = span_matching_str + "...+" + retword + covert_exp_mal('%'+"> hjx hoUdU* kt'f.");
          continue;
      }
      if (first_word ==1 ) {
          first_word =2;
          
          //if (mode =='E_EXACT' ) {
          //    span_matching_str ="<br><b>" + mal_box+ "</b>:"
          //                        + "<a href="+make_url_word_str(mal_box)+"><img border=\"0\" src=\"img/shortcut.PNG\" width=\"11\" height=\"11\"></a>"
          //                        +"<br>";
          //}
          
          if (mode =='E_EXPLUS' ) {
              span_matching_str = "Matching words for \"<b>" + mal_box+ "</b> \" are the following ...<br><br>" ;
          }
          if (mode =='EXPLUS' ) {
              span_matching_str = //"<b>" + mal_box+ "</b>"  
                                  //  + convert_manglish(' enna padaththinu~ ') 
                                  //  + 
                                    "<b><font color=\"green\">"
                                    + convert_manglish('saamyamuLLa ') 
                                    + "</font></b>"
                                    + convert_manglish('padanGaL_ ... ') 
                                    +"<br>" ;
          }
          
      } 
      if (mode =='E_EXPLUS' || mode =='EXPLUS' ) {
          span_matching_str = span_matching_str + "<font color=\"green\">";
          result_matching_found=1;
          count_it++;
      } else {
          result_meaning_found=1;
          span_matching_str = span_matching_str + "<font color=\"black\">";
          count_it++;
      }
      if (first_word ==2 ) {
          first_word =0;
          span_matching_str = span_matching_str +  make_non_clickable_str(retword, mode);
      } else {
          span_matching_str = span_matching_str + ", " + make_non_clickable_str(retword, mode);
      }
      //if (mode =='E_EXPLUS' || mode =='EXPLUS' ) {
          span_matching_str = span_matching_str + " </font>";
      //}
      
          
  }
  //span_matching_str += "<br> <br>&nbsp;Hint: Double Click on the word(s) to get its meaning.";
  
  span_matching_str += " <br> <br>";
  
  got_result='DONE';
  
  if (count_it ==1 && result_meaning_found==1) {
      // if meaning found and matching count is only ONE; then it is the same word
      return 'NOWORD';
  }
    
  
  return 'DONE';
  
}

//#MALBASE##cjx##MALBASE_END#
function mal_base_parsing(str, mal_word)
{
  var result="";
  
  //recorded_result(str);
  //return;
  
  if (str.lastIndexOf('BASE_FAILED') != -1) {
      return 'FAILED';
  } 
  
  while (str.length !=0) {
      index = str.search(matchword_pattern);      
      if(index == 0){
      
          re_mode = new RegExp(matchword_pattern);
          var mode_array = re_mode.exec(str);
          retword = mode_array[2]; 
          str = str.substring(mode_array[0].length);                  
      } else {
          //error 
          str = str.substring(1);
          continue;               
      } 
      result = result + make_mal_clickable_str(retword) + " " ;
      break;
  }
             
  result = convert_manglish('moolaroopam_ ') 
           + "... <b>"+ result+ "</b><br> ";
  
  span_malbase_str = result;
  
  return 'DONE';
  
}
               

// str will be like #Mode##sid#result
// if mode/sid didn't match with Gmode/Gsid, then skip. (new request placed)
function check_return_str_mode(str)
{

  //alert('STR = ' + str);

  var header_pattern = "(#(.*?)##(.*?)##(.*?)##(.*?)#)";
  index = str.search(header_pattern);    
  
  while (index != 0 && str.length !=0) {
     str = str.substring(1);
     index = str.search(header_pattern);
  }
  
  if(index == 0){
      re_mode = new RegExp(header_pattern);
      var mode_array = re_mode.exec(str);
      retmode = mode_array[2]; 
      retver = mode_array[3];       
      retsid = mode_array[4]; 
      ret_mal_box= mode_array[5];
      str = str.substring(mode_array[0].length);
  } else {
      norecorded_result(
              "<img border=\"0\" src=\"img/delete.PNG\" width=\"19\" height=\"19\">"
            + "Some Error encountered; please press \"No\" button below");
      timer_stop();
      return "";  
  }
  if ( retver === dict_version) {
      //alert ( retver + dict_version);
  } else {
      window.location.href = window.location.pathname;
      alert ('New Version is available. Press OK to refresh the site...');
      location.reload(true);
  }
  //document.getElementById("txtHint").innerHTML= retmode +  retsid + 'len'+ mode_array[0].length +str;
  //return 'SKIP';
  
  if (retmode == 'DISCARD'){
      return 'SKIP';
  }
  
  //if (retmode != Gmode ||  retsid!=Gsid){
  if (retsid!=Gsid){
      //norecorded_result("Please perform the last action again and be patient");
      //timer_stop();
      return "";
  }

  timer_stop();
  var tstr = Gstr;
  
  if (retmode == 'ADD_EXEC')
  {

      index = str.search(matchword_pattern);  
      
      if(index == 0){      
             re_mode = new RegExp(matchword_pattern);
             var mode_array = re_mode.exec(str);
             retword = mode_array[2]; 
             
             if (retword == 'ADD_SUCC' ) {
                //document.getElementById("txtHint").innerHTML= "ADD_SUC";
                add_word_clear();
             } else {
                //document.getElementById("txtHint").innerHTML= "ADD_FAIL";
             }
      }
      str = str.substring(mode_array[0].length);
      document.getElementById("txtHint").innerHTML= str;
      return 'SKIP';
    
  }  

  if (retmode == 'PR_UNSATISFIED' ) {
      norecorded_result (str);
      return 'SKIP';
  }

  if (html_page=="nighantu") {
      delay_timer_stop();
      search_end_time();
      got_result='SERCH_END';
  
  }
    
  turn_on_double_click();
  set_keyboard_off();
    
  
  if (retmode == 'EXPLUS' || retmode == 'E_EXPLUS') {
      span_matching_str = '';
      parse_show_matching(str, retmode, ret_mal_box);
      
      span_matching_str = span_matching_str.replace(/\ufffd/,'');
      
      
      if ((retmode == 'E_EXPLUS') &&(result_matching_found==0)) {
          if (make_clickable_split_str(Ginput_str, 'E_EXPLUS') != 'NOTREQD') {
                    
          } else {
              return 'SKIP';
          }
      }

      recorded_result(span_matching_str);
      return 'SKIP';
  }     
  if (retmode == 'E_EXACT') {
     
     span_meaning_str ="";
     eng_parse_show_meaning(str,ret_mal_box);
     span_meaning_str = span_meaning_str.replace(/\ufffd/, '');

     //alert(span_meaning_str);
     add_words_recorded_result(span_meaning_str);// only add words using this

     return 'SKIP';
  }  
  if (retmode == 'EXACT') {
  

     span_meaning_str ="";
     parse_show_meaning(str,ret_mal_box);
     span_meaning_str = span_meaning_str.replace(/\ufffd/, '');
     
     add_words_recorded_result(span_meaning_str); // only add words using this

     return 'SKIP';
  }
  
  if (retmode == 'EXBOTH') {


     var exboth_pattern = "(##EXACT#(.*?)#EXACTEND####EXPLUS#(.*?)#EXPLUSEND####MALBASE#(.*?)#MALBASE_END##)";
     
     while (str.length !=0) {
           index = str.search(exboth_pattern);      
           if(index == 0){
           
               re_mode = new RegExp(exboth_pattern);
               var mode_array = re_mode.exec(str);
               meaning_str = mode_array[2]; 
               matching_str = mode_array[3]; 
               malbase_str = mode_array[4]; 
               str = str.substring(mode_array[0].length);                  
           } else {
               //error 
               str = str.substring(1);
               continue;               
           } 
     }     
     span_meaning_str ="";
     if (parse_show_meaning(meaning_str, ret_mal_box) == 'NOWORDEXACT') {
     
         // Need to do something for 'DOd/kd<V' 
      //   mal_ret = mal_base_parsing(malbase_str, ret_mal_box);
      //   if (mal_ret == 'DONE') {
      //      span_meaning_str = span_malbase_str;
      //      span_meaning_str = span_meaning_str.replace(/\ufffd/, '');
      //      recorded_result(span_meaning_str);
      //      return 'SKIP';
      //   }
     }
     span_matching_str = '';
     ret_matching = parse_show_matching(matching_str, 'EXPLUS', ret_mal_box);
     
     span_meaning_str = span_meaning_str.replace(/\ufffd/, '');
     span_matching_str = span_matching_str.replace(/\ufffd/, '');
     
     if (ret_matching == 'DONE') {
         result = span_meaning_str + "<hr>" + span_matching_str ;
     } else {
         result = span_meaning_str;
     }
     
     //if ((result_meaning_found==0) && (result_matching_found==0)){
        // alert("result_meaning_found and result_matching_found NULL");
     //} 

     if ((result_meaning_found==0) && (result_matching_found==0)){
         mal_ret = mal_base_parsing(malbase_str, ret_mal_box);
         if (mal_ret == 'DONE') {
             result_meaning_found=1;
             span_meaning_str = span_malbase_str;
             span_meaning_str = span_meaning_str.replace(/\ufffd/, '');
             recorded_result(span_meaning_str);
             return 'SKIP';
         }     
     }
     recorded_result(result);

     return 'SKIP';
  }
  if (retmode == 'E_EXBOTH') {

     //document.getElementById("txtHint").innerHTML =  'RRRRRRR' ;
     //return;
     
     var exboth_pattern = "(##E_EXACT#(.*?)#E_EXACTEND####E_EXPLUS#(.*?)#E_EXPLUSEND##)";

     while (str.length !=0) {
           index = str.search(exboth_pattern);      
           if(index == 0){

               re_mode = new RegExp(exboth_pattern);
               var mode_array = re_mode.exec(str);
               meaning_str = mode_array[2]; 
               matching_str = mode_array[3]; 
               str = str.substring(mode_array[0].length);                  
           } else {
               //error 
               str = str.substring(1);
               continue;               
           } 
     }     

     span_meaning_str ="";
     eng_parse_show_meaning(meaning_str,ret_mal_box);
     span_meaning_str = span_meaning_str.replace(/\ufffd/, '');
     
     save_it =span_meaning_str;
     
     span_matching_str = '';
     ret_matching = parse_show_matching(matching_str, 'E_EXPLUS', ret_mal_box); 
     
     save_it = save_it.replace(/\ufffd/, '');
     span_matching_str = span_matching_str.replace(/\ufffd/, '');
     
     if ( ret_matching == 'DONE') {
         result = save_it + "<hr>" + span_matching_str;
     } else {
         result = save_it;
     }
     

     if ((result_meaning_found==0) && (result_matching_found==0) ){
          if (make_clickable_split_str(Ginput_str, 'E_EXBOTH') != 'NOTREQD') {
              
          } else {
             return 'SKIP';
          }
     }

     recorded_result(result);

     return 'SKIP';
  }
       
  
  return 'Error';
}


function stateChanged() 
{ 
  var expert_txt="";
  if (xmlHttp.readyState==4)
  { 
     //document.getElementById("txtHint").innerHTML =  "<pre>" + xmlHttp.responseText +"</pre>";
     //return;
     expert_txt = check_return_str_mode(xmlHttp.responseText);
     if (expert_txt == 'SKIP')
         return;
     //recored_result(covert_exp_mal('%'+expert_txt));
  }
}

function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
  {
  // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
  // Internet Explorer
    try
    {
       xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

