// -*- mode: java; -*-

// Indian Language Converter - transliterates from Roman scripts to
// Indic scripts.

// Copyright (C) 2005, 2006 Vijay Lakshminarayanan <liyer.vijay@gmail.com>

// Indian Language Converter is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301, USA.

// 	$Id: converter.js,v 1.1.1.1 2006-05-26 11:49:07 vijay Exp $	
// 	Author: Vijay Lakshminarayanan	
// 	$Date: 2006-05-26 11:49:07 $	

var vowels = "(A((O(M)?)|(o))?)|(En)|(H)|(I)|(M)|(TR)|(U)|(\\:)|(\\|(\\|)?)|(a((A)|(a)|(i)|(u))?)|(e(e)?)|(i)|(o(o)?)|(tR)|(u)"
var consonants =  "(B(h)?)|(Ch)|(D((dD)|(h))?)|(G)|(L(lL)?)|(N(nN)?)|(R(rR)?)|(Sh)|(T(h)?)|(Y)|(b(h)?)|(ch)|(d(h)?)|(f)|(g((G)|(h))?)|(h)|(j(h)?)|(k(h)?)|(l)|(m)|(n(Y)?)|(p(h)?)|(q(h)?)|(r)|(s(h)?)|(t(h)?)|(v)|(y)|(z)|(W)|(w)|(_)"


var letter_codes = {
"~a" : "&#2309;",
"~aa" : "&#2310;",
"~A" : "&#2310;",
"~i" : "&#2311;",
"~e" : "&#2319;",
"~ee" : "&#2312;",
"~I" : "&#2312;",
"~u" : "&#2313;",
"~oo" : "&#2314;",
"~U" : "&#2314;",
"~tR" : "&#2315;",
"~En" : "&#2317;",
"~o" : "&#2323;",
"~ai" : "&#2320;",
"~Ao" : "&#2321;",
"~au" : "&#2324;",
"~TR" : "&#2400;",
"j~j" : "&#2419;",
"a" : "",
"aa" : "&#2366;",
"A" : "&#2366;",
"A" : "&#2366;",
"i" : "&#2367;",
"e" : "&#2375;",
"ee" : "&#2368;",
"I" : "&#2368;",
"u" : "&#2369;",
"oo" : "&#2370;",
"U" : "&#2370;",
"tR" : "&#2371;",
"En" : "&#2373;",
"o" : "&#2379;",
"ai" : "&#2376;",
"Ao" : "&#2377;",
"au" : "&#2380;",
"TR" : "&#2372;",
"k" : "&#2325;",
"kh" : "&#2326;",
"g" : "&#2327;",
"gh" : "&#2328;",
"G" : "&#2329;",
"ch" : "&#2330;",
"Ch" : "&#2331;",
"j" : "&#2332;",
"jh" : "&#2333;",
"nY" : "&#2334;",
"t" : "&#2340;",
"T" : "&#2335;",
"d" : "&#2342;",
"D" : "&#2337;",
"N" : "&#2339;",
"th" : "&#2341;",
"Th" : "&#2336;",
"dh" : "&#2343;",
"Dh" : "&#2338;",
"n" : "&#2344;",
"NnN" : "&#2345;",
"p" : "&#2346;",
"ph" : "&#2347;",
"b" : "&#2348;",
"bh" : "&#2349;",
"B" : "&#2349;",
"Bh" : "&#2349;",
"m" : "&#2350;",
"y" : "&#2351;",
"r" : "&#2352;",
"R" : "&#2353;",
"l" : "&#2354;",
"L" : "&#2355;",
"LlL" : "&#2356;",
"v" : "&#2357;",
"sh" : "&#2358;",
"Sh" : "&#2359;",
"s" : "&#2360;",
"h" : "&#2361;",
"q" : "&#2392;",
"qh" : "&#2393;",
"gG" : "&#2394;",
"z" : "&#2395;",
"DdD" : "&#2396;",
"RrR" : "&#2397;",
"f" : "&#2398;",
"Y" : "&#2399;",
"AO" : "&#2305;",
"M" : "&#2306;",
"H" : "&#2307;",
":" : "&#2307;",
"aA" : "&#2365;",
"|" : "&#2404;",
"||" : "&#2405;",
"AOM" : "&#2384;",
"~AO" : "&#2305;",
"~M" : "&#2306;",
"~H" : "&#2307;",
"~:" : "&#2307;",
"~aA" : "&#2365;",
"~|" : "&#2404;",
"~||" : "&#2405;",
"~AOM" : "&#2384;",
"*" : "&#2381;",
"W" : "&#2364;",
"w" : "&#2385;",
"_" : "&#2386;"
}
function split_word(word)
{
  var syllables = new Array(0);
  var vowel_start_p = true;
  while (word.length) {
    re = new RegExp(vowels);
    var index = word.search(vowels);
    if (index == 0) {  //the vowel's at the start of word
      var matched = re.exec(word)[0]; //what is it?
      if (vowel_start_p) {
	syllables.push(("~"+matched)); //one more to the syllables
      } else {
	syllables.push(matched);
      }
      vowel_start_p = true;
      word = word.substring(matched.length);
    } else {
      re = new RegExp(consonants);
      var index = word.search(consonants);
      if (index == 0) {
	var matched = re.exec(word)[0];
	syllables.push(matched);
	vowel_start_p = false;
	word = word.substring(matched.length);

	//look ahead for virama setting
	var next = word.search(vowels);
	if (next != 0 || word.length == 0)
	  syllables.push('*');
      } else {
	syllables.push(word.charAt(0));
	word = word.substring(1);
      }
    }
  }
  return syllables;
}

function match_code(syllable_mcc)
{
  var matched = letter_codes[syllable_mcc];

  if (matched != null) return matched;
  return syllable_mcc;
}

function one_word(word_ow)
{
  if (!word_ow) return "";
  var syllables_ow = split_word(word_ow);
  var letters_ow = new Array(0);

  for (var i_ow = 0; i_ow < syllables_ow.length; i_ow++) {
    letters_ow.push(match_code(syllables_ow[i_ow]));
  }
  return letters_ow.join("");
}

function many_words(sentence)
{
  var regex = "((" + vowels + ")|(" + consonants + "))+";
  var words = new Array(0);
  while (sentence.length >= 1) {
    re = new RegExp("^``" + regex);
    var match = re.exec(sentence);
    
    if (match != null) {
      match = match[0];
      words.push("`");
      words.push(one_word(match.substring(2)));
      sentence = sentence.substring(match.length);
    } else {
      re = new RegExp("^`" + regex);
      match = re.exec(sentence);
      if (match != null) {
	match = match[0];
	words.push(match.substring(1));
	sentence = sentence.substring(match.length);
      } else {
	re = new RegExp("^" + regex);
	match = re.exec(sentence);
	if (match != null) {
	  match = match[0];
	  words.push(one_word(match));
	  sentence = sentence.substring(match.length);
	} else {
	  words.push(sentence.charAt(0));
	  sentence = sentence.substring(1);
	}
      }
    }

  }
  return words.join("");
}

function print_many_words(name1, name2)
{
  
  var text_pmw = many_words(document.getElementById(name1).value);

  var ans = "";
  while (text_pmw.length) {
    var unicode_chars = /&#[0-9]+;/;
    re = unicode_chars;
    var matche = re.exec(text_pmw);
    if (matche != null) {
      matche = matche[0];
      search = text_pmw.search(unicode_chars);
      ans += text_pmw.substring(0, search);
      ans += String.fromCharCode(matche.match(/[0-9]+/));
      text_pmw = text_pmw.substring(search + matche.length);
    } else {
      ans += text_pmw.substring(0);
      text_pmw = "";
    }
  }
	
  document.getElementById(name2).value = ans;
}

function print_many_words_div(name1, name2)
{
  
  var text_pmw = many_words(document.getElementById(name1).value);

  var ans = "";
  while (text_pmw.length) {
    var unicode_chars = /&#[0-9]+;/;
    re = unicode_chars;
    var matche = re.exec(text_pmw);
    if (matche != null) {
      matche = matche[0];
      search = text_pmw.search(unicode_chars);
      ans += text_pmw.substring(0, search);
      ans += String.fromCharCode(matche.match(/[0-9]+/));
      text_pmw = text_pmw.substring(search + matche.length);
    } else {
      ans += text_pmw.substring(0);
      text_pmw = "";
    }
  }
  ans = ans.replace(/\<\>/g,'<u>');
  ans = ans.replace(/\<\/>/g,'</u>');
  ans = ans.replace(/\(/g,'<sup>');
  ans = ans.replace(/\)/g,'</sup>');
						
  document.getElementById(name2).innerHTML = ans;
}

function print_many_words_div_div(name1, name2)
{

  var value = document.getElementById(name1).innerHTML;
  value = value.replace(/<br>/g,'**');
  value = value.replace(/<BR>/g,'**');
  value = value.replace(/<br \/>/g,'**');
  value = value.replace(/<br\/>/g,'**');
  var text_pmw = many_words(value);

  var ans = "";
  while (text_pmw.length) {
    var unicode_chars = /&#[0-9]+;/;
    re = unicode_chars;
    var matche = re.exec(text_pmw);
    if (matche != null) {
      matche = matche[0];
      search = text_pmw.search(unicode_chars);
      ans += text_pmw.substring(0, search);
      ans += String.fromCharCode(matche.match(/[0-9]+/));
      text_pmw = text_pmw.substring(search + matche.length);
    } else {
      ans += text_pmw.substring(0);
      text_pmw = "";
    }
  }
 
  ans = ans.replace(/\*\*/g, '<br>');
  ans = ans.replace(/\<\>/g,'<u>');
  ans = ans.replace(/\<\/>/g,'</u>');
  ans = ans.replace(/\(/g,'<sup>');
  ans = ans.replace(/\)/g,'</sup>');
						
  document.getElementById(name2).innerHTML = ans;
}

