// -*- 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.8 2006-03-26 03:08:29 vijay Exp $	
// 	Author: Vijay Lakshminarayanan	
// 	$Date: 2006-03-26 03:08:29 $	
<!-- 	Modified: Anthony Deign adeign@gmail.com	 -->
<!-- 	$Date: 2006-Aug-01 02:44:19 $	 -->
//	Modified: Anoop Ramanujam
//	$Date: 2006-Sep-11 20:16:22 $

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("");
}

// returns malayalam
function GetMalayalam(manglish_text)
{

  var text_pmw = many_words(manglish_text);
  
  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 = "";
    }
  }
  return ans;
  
}

function ClipBoard(x)
{
window.clipboardData.setData('Text',x);
}


// get manglish from malyalam
function GetManglish(mallu)
{
var reversed = ""; // the string
var letter = ""; // syllable 
var rx_vowel = /(a|e|i|o|u)+/i; 
var rx_dlimit_a = /(a|e|i|o|u| )+/i;  // not used
last_s = 0;
for (i=0;i<mallu.length;i++) {
	// get unicode no
	s = (mallu.charCodeAt(i));
	
	if (reverse_codes[s]) { // we have a mapping
		letter += reverse_codes[s]; 
		if (!letter_codes[letter]) {	// letter code found	
			// a needs to be added since in letter_code "a" is mapped to null
			if (!rx_vowel.test(letter)) {
				reversed += "a";
				letter = reverse_codes[s];
			}
		} else {
			// who is going to take care of "manaththu" becoming "manththu"
		}
		reversed += reverse_codes[s];
	} else {
		if ( (s>=13) && (s<=127)) {
			if ( last_s != 3405 ) { // does not end with chandrakala
				if (!rx_vowel.test(reversed.charAt(reversed.length-1))) {
					if (last_s >3300) {  // only valid mal unicodes
						reversed += "a";
					}
				}
			}
			reversed += String.fromCharCode(s);
			letter = "";
		} else {

			switch (s) {
				case 160 : reversed += " ";break; //space
				case 3405 : break;									// chandrakala
				case 3330 :												// am
										// make sure it reversed does not currently end with 
										//	a vowel 
										if (!rx_vowel.test(reversed.charAt(reversed.length-1))) {
											reversed += "a";
										}
										// i forgot why this was here
										if (last_s == 3405) {
											reversed += "a";
										}
										reversed += "m"; break; 
				case 8205 : break;									// needs to dig
				case 10 : 	break;									// linebreak
				//default  : reversed += s; // debugging only
			}
			letter = "";
		}
	}
	
	// ending with vowel : reset
	if (rx_vowel.test(reversed.charAt(reversed.length-1))) {
		letter = "";
	}
	last_s = s;
}
return reversed;
}

var caller = new Object;
var kbdiv = "malludivarea";
var kbtxt = "malluarea";

// make the div visible and position it
function PullMalluArea(obj)
{
	var mallu = document.getElementById(kbdiv);
	mallu.style.left = document.body.clientWidth-mallu.clientWidth-10;
	mallu.style.top = document.body.clientHeight-mallu.clientHeight-120;
	mallu.style.visibility = 'visible';
	FocusMalluBox(obj);
}

// transfer focus to manglish box
//	fill this in case of edit
function FocusMalluBox(obj)
{
	document.getElementById(kbtxt).focus();
	caller = obj;
	if ((obj.value != '')) {  // edit mode : (try) filling with manglish
		previous_mallu = obj.value;
		manglish = GetManglish(previous_mallu);
		new_mallu = GetMalayalam(manglish);
		
		if (previous_mallu != new_mallu) { // kalipp
			window.status = "#";
			/*
			if (confirm('Errors while translating to English. Are you sure you want to edit?\n(Program Bug - I apologize)')) 
			{
				document.getElementById(kbtxt).value = manglish;
			} else {
				document.getElementById(kbdiv).style.visibility = 'hidden';
			}
			*/

		} else {
			window.status = "";
			//document.getElementById(kbtxt).value = manglish;
		}
		document.getElementById(kbtxt).value = manglish;		
	} else { // create new - not edit
		document.getElementById(kbtxt).value = '';
	}
}

// conversion routine; also handle tab and help
function GoMallu(e,mallu)
{
	key = (e.keyCode) ? e.keyCode: e.which;

	if (key == 9) {
		editmode = false;
		document.getElementById(kbdiv).style.visibility = 'hidden';
		return false;
	}
	helpdiv = document.getElementById("helper");
	key = mallu.value.charCodeAt(mallu.value.length-1);
	key = (key < 97 ) ? key : (key-32);
	switch (key) {
		case 67 : 
		case 74 : helpdiv.innerHTML = showHelp('c'); break;
		case 71 : 
		case 75 : helpdiv.innerHTML = showHelp('k'); break;
		case 78 : helpdiv.innerHTML = showHelp('n'); break;
		case 68 : 
		case 84 : helpdiv.innerHTML = showHelp('t'); break;
		case 66 : 
		case 80 : helpdiv.innerHTML = showHelp('p'); break;
		case 82 : helpdiv.innerHTML = showHelp('r'); break;
		case 76 : helpdiv.innerHTML = showHelp('l'); break;
		case 83 : helpdiv.innerHTML = showHelp('s'); break;
		default : helpdiv.innerHTML = showHelp('a'); break;
	}
	caller.value = GetMalayalam(mallu.value);
}

function showHelp(letter)
{
	helpstring = '';
	switch (letter) {
		case 'a' :  helpstring = 'a:' + GetMalayalam('a');
								helpstring += ' aa:' + GetMalayalam('aa');
								helpstring += ' i:' + GetMalayalam('i');
								helpstring += ' ee:' + GetMalayalam('ee');
								helpstring += ' u:' + GetMalayalam('u');
								helpstring += ' oo:' + GetMalayalam('oo');
								helpstring += ' r~:' + GetMalayalam('r~');
								helpstring += ' oo:' + GetMalayalam('oo');
								helpstring += ' E:' + GetMalayalam('E');
								helpstring += ' ai:' + GetMalayalam('ai');
								helpstring += ' o:' + GetMalayalam('o');
								helpstring += ' O:' + GetMalayalam('O');
								helpstring += ' au:' + GetMalayalam('au');
								helpstring += ' am:' + GetMalayalam('am ');
								break;
		case 'k' :  helpstring = 'k:' + GetMalayalam('ka');
								helpstring += ' kh:' + GetMalayalam('kha');
								helpstring += ' g:' + GetMalayalam('ga');
								helpstring += ' gh:' + GetMalayalam('gha');
								helpstring += ' kk:' + GetMalayalam('kka');
								helpstring += ' gg:' + GetMalayalam('gga');
								break;
		case 'n' :  helpstring = 'n:' + GetMalayalam('na');
								helpstring += ' N:' + GetMalayalam('Na');
								helpstring += ' ng:' + GetMalayalam('nga');
								helpstring += ' nj:' + GetMalayalam('nja');
								helpstring += ' ngng:' + GetMalayalam('ngnga');
								helpstring += ' njnj:' + GetMalayalam('njnja');
								break;
		case 'c' :  helpstring = 'ch:' + GetMalayalam('cha');
								helpstring += ' chh:' + GetMalayalam('chha');
								helpstring += ' j:' + GetMalayalam('ja');
								helpstring += ' jh:' + GetMalayalam('jha');
								helpstring += ' chch:' + GetMalayalam('chcha');
								helpstring += ' jj:' + GetMalayalam('jja');
								break;
		case 't' :  helpstring = 'T:' + GetMalayalam('Ta');
								helpstring += ' Th:' + GetMalayalam('Tha');
								helpstring += ' D:' + GetMalayalam('Da');
								helpstring += ' Dh:' + GetMalayalam('Dha');
								helpstring += ' th:' + GetMalayalam('tha');
								helpstring += ' thh:' + GetMalayalam('thha');
								helpstring += ' d:' + GetMalayalam('da');
								helpstring += ' dh:' + GetMalayalam('dha');
								helpstring += ' t:' + GetMalayalam('ta');
								helpstring += ' tt:' + GetMalayalam('tta');
								helpstring += ' thth:' + GetMalayalam('ththa');
								helpstring += ' dd:' + GetMalayalam('dda');
								break;
		case 'p' :  helpstring = 'p:' + GetMalayalam('pa');
								helpstring += ' ph:' + GetMalayalam('pha');
								helpstring += ' b:' + GetMalayalam('ba');
								helpstring += ' bh:' + GetMalayalam('bha');
								helpstring += ' pp:' + GetMalayalam('ppa');
								helpstring += ' bb:' + GetMalayalam('bba');
								break;

		case 'r' :  helpstring = 'r:' + GetMalayalam('ra');
								helpstring += ' R:' + GetMalayalam('Ra');
								break;
		case 'l' : 	helpstring = 'l:' + GetMalayalam('la');
								helpstring += ' L:' + GetMalayalam('La');
								break;
		case 's' :  helpstring += 'S:' + GetMalayalam('Sa');
								helpstring += ' sh:' + GetMalayalam('sha');
								helpstring += ' s:' + GetMalayalam('sa');
								helpstring += ' SS:' + GetMalayalam('SSa');
								helpstring += ' ss:' + GetMalayalam('ssa');
								break;
	}
	return helpstring;
}
