/*********************************************************************************************************************************/
//Content of malayalam.js alone.Start
/*********************************************************************************************************************************/

var consonants = {"&#3349;":"&#3349;","&#3350;":"&#3350;","&#3351;":"&#3351;","&#3352;":"&#3352;","&#3353;":"&#3353;","&#3354;":"&#3354;","&#3355;":"&#3355;","&#3356;":"&#3356;","&#3357;":"&#3357;","&#3358;":"&#3358;","&#3359;":"&#3359;","&#3360;":"&#3360;","&#3361;":"&#3361;","&#3362;":"&#3362;","&#3363;":"&#3363;","&#3364;":"&#3364;","&#3365;":"&#3365;","&#3366;":"&#3366;","&#3367;":"&#3367;","&#3368;":"&#3368;","&#3370;":"&#3370;","&#3371;":"&#3371;","&#3372;":"&#3372;","&#3373;":"&#3373;","&#3374;":"&#3374;","&#3375;":"&#3375;","&#3376;":"&#3376;","&#3378;":"&#3378;","&#3381;":"&#3381;","&#3382;":"&#3382;","&#3383;":"&#3383;","&#3384;":"&#3384;","&#3385;":"&#3385;","&#3379;":"&#3379;","&#3380;":"&#3380;","&#3377;":"&#3377;","&#3377;&#3405;&#3377;":"&#3377;&#3405;&#3377;"};
var chillaksharam = {"&#3363;&#3405;&#8205;":"&#3363;","&#3368;&#3405;&#8205;":"&#3368;","&#3330;":"&#3374;","&#3376;&#3405;&#8205;":"&#3376;","&#3378;&#3405;&#8205;":"&#3378;","&#3379;&#3405;&#8205;":"&#3379;","&#3405;\\u200D":""};

var vowels = '"&#3405;a":"","&#3405;e":"&#3398;","&#3405;i":"&#3391;","&#3405;o":"&#3402;","&#3405;u":"&#3393;","&#3405;A":"&#3390;","&#3405;E":"&#3399;","&#3405;I":"&#3392;","&#3405;O":"&#3403;","&#3405;U":"&#3394;","&#3405;Y":"&#3400;","&#3398;e":"&#3392;","&#3402;o":"&#3394;","&#3391;i":"&#3392;","&#3391;e":"&#3392;","&#3393;u":"&#3394;","&#3393;o":"&#3394;","&#3405;r":"&#3405;&#3376;&#3405;",';
var roman = '"k":"&#3349;&#3405;","&#3349;&#3405;h":"&#3350;&#3405;","g":"&#3351;&#3405;","&#3351;&#3405;h":"&#3352;&#3405;","&#3368;&#3405;&#8205;g":"&#3353;&#3405;","c":"&#3349;&#3405;\\u200D","&#3349;&#3405;\\u200Dh":"&#3354;&#3405;","&#3354;&#3405;h":"&#3355;&#3405;","j":"&#3356;&#3405;","&#3356;&#3405;h":"&#3357;&#3405;","&#3368;&#3405;&#8205;j":"&#3358;&#3405;","&#3368;&#3405;&#8205;h":"&#3358;&#3405;","T":"&#3359;&#3405;","&#3359;&#3405;h":"&#3360;&#3405;","D":"&#3361;&#3405;","&#3361;&#3405;h":"&#3362;&#3405;","&#3377;&#3405;&#3377;&#3405;h":"&#3364;&#3405;","&#3364;&#3405;h":"&#3365;&#3405;","d":"&#3366;&#3405;","&#3366;&#3405;h":"&#3367;&#3405;","p":"&#3370;&#3405;","&#3370;&#3405;h":"&#3371;&#3405;","f":"&#3371;&#3405;","b":"&#3372;&#3405;","&#3372;&#3405;h":"&#3373;&#3405;","y":"&#3375;&#3405;","v":"&#3381;&#3405;","w":"&#3381;&#3405;","z":"&#3382;&#3405;","S":"&#3382;&#3405;","&#3384;&#3405;h":"&#3383;&#3405;","s":"&#3384;&#3405;","h":"&#3385;&#3405;","&#3382;&#3405;h":"&#3380;&#3405;","x":"&#3349;&#3405;&#3383;&#3405;","R":"&#3377;&#3405;","t":"&#3377;&#3405;&#3377;&#3405;",';
var chill = '"N":"&#3363;&#3405;&#8205;","n":"&#3368;&#3405;&#8205;","m":"&#3330;","r":"&#3376;&#3405;&#8205;","l":"&#3378;&#3405;&#8205;","L":"&#3379;&#3405;&#8205;",';
//var chill = '"N":"\\u0D23\\u0D4D\\u200D","n":"\\u0D28\\u0D4D\\u200D","m":"&#3330;","r":"\\u0D30\\u0D4D\\u200D","l":"\\u0D32\\u0D4D\\u200D","L":"\\u0D33\\u0D4D\\u200D",';
//when changed vannu had problem in mozilla
//one solution is replace every where.or find how IE there is no problem but mozilla has problm
var swaram = '"a":"&#3333;","&#3333;a":"&#3334;","A":"&#3334;","e":"&#3342;","E":"&#3343;","&#3342;e":"&#3336;","i":"&#3335;","&#3335;i":"&#3336;","&#3335;e":"&#3336;","&#3333;i":"&#3344;","I":"&#3344;","o":"&#3346;","&#3346;o":"&#3338;","O":"&#3347;","&#3333;u":"&#3348;","&#3346;u":"&#3348;","u":"&#3337;","&#3337;u":"&#3338;","U":"&#3338;","H":"&#3331;","&#3377;&#3405;h":"&#3339;","&#3376;&#3405;&#8205;^":"&#3339;","&#3339;^":"&#3424;","&#3378;&#3405;&#8205;^":"&#3340;","&#3340;^":"&#3425;",';
var numerals = '"1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7","8":"8","9":"9","0":"0",';
var conjuncts = '"&#3368;&#3405;&#8205;t":"&#3368;&#3405;&#3377;&#3405;","&#3368;&#3405;&#3377;&#3405;h":"&#3368;&#3405;&#3364;&#3405;","&#3368;&#3405;&#8205;k":"&#3353;&#3405;&#3349;&#3405;","&#3368;&#3405;&#8205;n":"&#3368;&#3405;&#3368;&#3405;","&#3363;&#3405;&#8205;N":"&#3363;&#3405;&#3363;&#3405;","&#3379;&#3405;&#8205;L":"&#3379;&#3405;&#3379;&#3405;","&#3378;&#3405;&#8205;l":"&#3378;&#3405;&#3378;&#3405;","&#3330;m":"&#3374;&#3405;&#3374;&#3405;","&#3368;&#3405;&#8205;m":"&#3368;&#3405;&#3374;&#3405;","&#3368;&#3405;&#3368;&#3405;g":"&#3353;&#3405;&#3353;&#3405;","&#3368;&#3405;&#8205;d":"&#3368;&#3405;&#3366;&#3405;","&#3363;&#3405;&#8205;m":"&#3363;&#3405;&#3374;&#3405;","&#3378;&#3405;&#8205;p":"&#3378;&#3405;&#3370;&#3405;","&#3330;p":"&#3374;&#3405;&#3370;&#3405;","&#3377;&#3405;&#3377;&#3405;t":"&#3359;&#3405;&#3359;&#3405;","&#3368;&#3405;&#8205;T":"&#3363;&#3405;&#3359;&#3405;","&#3363;&#3405;&#8205;T":"&#3363;&#3405;&#3359;&#3405;","&#3405;&#3376;&#3405;^":"&#3395;","&#3368;&#3405;&#8205;c":"&#3368;&#3405;&#8205;\\u200D","&#3368;&#3405;&#8205;\\u200Dh":"&#3358;&#3405;&#3354;&#3405;","&#3363;&#3405;&#8205;D":"&#3363;&#3405;&#3361;&#3405;",';
var others = '"&#3405;L":"&#3405;&#3378;&#3405;","~":"&#3405;\\u200C","&#3405;~":"&#3405;\\u200C","\\u200C~":"\\u200C","&#3330;~":"&#3374;&#3405;","&#3349;&#3405;\\u200Dc":"&#3349;&#3405;&#3349;&#3405;\\u200D","&#3349;&#3405;&#3349;&#3405;\\u200Dh":"&#3354;&#3405;&#3354;&#3405;","q":"&#3349;&#3405;&#3375;&#3394;",';
var caps = '"B":"&#3372;&#3405;&#3372;&#3405;","C":"&#3349;&#3405;&#3349;&#3405;\\u200D","F":"&#3371;&#3405;","G":"&#3351;&#3405;&#3351;&#3405;","J":"&#3356;&#3405;&#3356;&#3405;","K":"&#3349;&#3405;&#3349;&#3405;","M":"&#3374;&#3405;&#3374;&#3405;","P":"&#3370;&#3405;&#3370;&#3405;","Q":"&#3349;&#3405;&#3375;&#3394;","V":"&#3381;&#3405;&#3381;&#3405;","W":"&#3381;&#3405;&#3381;&#3405;","X":"&#3349;&#3405;&#3383;&#3405;","Y":"&#3375;&#3405;&#3375;&#3405;","Z":"&#3382;&#3405;&#3382;&#3405;",';
var ZWNJ = '"_":"\\u200C"';

var conversionHash = undefined;
var translitHash = undefined;
var maxcyrlength = 0;
var stateHash = new Array();

function getConversionHash() {
	if (conversionHash == undefined) {
		// TODO
		var opr = "{" + vowels + roman + chill + swaram + numerals + conjuncts + caps + others;
		for (var consonant in consonants) {
			opr += '"' + consonant + 'a":"' + consonant + '&#3390;",';
			opr += '"' + consonant + 'e":"' + consonant + '&#3399;",';
			opr += '"' + consonant + 'i":"' + consonant + '&#3400;",';
			opr += '"' + consonant + 'o":"' + consonant + '&#3403;",';
			opr += '"' + consonant + 'u":"' + consonant + '&#3415;",';
		}
		
		for (var chk in chillaksharam) {
			opr += '"' + chk + 'a":"' + chillaksharam[chk] + '",';
			opr += '"' + chk + 'e":"' + chillaksharam[chk] + '&#3398;",';
			opr += '"' + chk + 'i":"' + chillaksharam[chk] + '&#3391;",';
			opr += '"' + chk + 'o":"' + chillaksharam[chk] + '&#3402;",';
			opr += '"' + chk + 'u":"' + chillaksharam[chk] + '&#3393;",';
			opr += '"' + chk + 'A":"' + chillaksharam[chk] + '&#3390;",';
			opr += '"' + chk + 'E":"' + chillaksharam[chk] + '&#3399;",';
			opr += '"' + chk + 'I":"' + chillaksharam[chk] + '&#3392;",';
			opr += '"' + chk + 'O":"' + chillaksharam[chk] + '&#3403;",';
			opr += '"' + chk + 'U":"' + chillaksharam[chk] + '&#3394;",';
			opr += '"' + chk + 'Y":"' + chillaksharam[chk] + '&#3400;",';
			opr += '"' + chk + 'r":"' + chillaksharam[chk] + '&#3405;&#3376;&#3405;",';
			opr += '"' + chk + 'y":"' + chillaksharam[chk] + '&#3405;&#3375;&#3405;",';
			opr += '"' + chk + 'v":"' + chillaksharam[chk] + '&#3405;&#3381;&#3405;",';
			opr += '"' + chk + 'w":"' + chillaksharam[chk] + '&#3405;&#3381;&#3405;",';
			opr += '"' + chk + '~":"' + chillaksharam[chk] + '&#3405;\\u200C",';
		}
		
		opr += ZWNJ + "}";
		// var tb = document.getElementById('txtBox');
		// tb.value = opr;
		conversionHash = eval("("+opr+")");
		maxcyrlength=6;
	}

	return conversionHash;
}

function to_cyrillic(src, output, chunks) {
	if (src == undefined || src == "" || src == null)
		return src;
	if (output == undefined)
		output = new String();

	var hash = getConversionHash();
	
	var location = 0;
	
	while (location < src.length) {
		var len = Math.min(maxcyrlength, src.length - location);
		var arr = undefined;
		var sub;
		while (len > 0) {
			sub = src.substr(location, len);
			arr = hash[sub];
			if (arr != undefined) 
				break;
			else 
				len--;
		}
		
		// need this for translit on the fly
		if (chunks != undefined)
			chunks[chunks.length] = sub;
			
		if (arr == undefined) {
			output += sub;
			location ++;
		}
		else {

			// case analysis
			var newChar = arr;
			
			if (sub.toLowerCase() == sub.toUpperCase() && arr.length > 1 && arr[1] && (newChar.toUpperCase() != newChar.toLowerCase())) {
			
				// need translit hash to determine if previous character (and possibly the one before it) 
				// were converted and are in upper case
				
				// set prevDud to true previous is not a translated character or simply a blank
				// set prevCap to true if previous was translated and was upper case

				var prevCh = output.length == 0 ? null : output.substr(output.length - 1, 1);
				var prevDud = !prevCh || !getTranslitString(prevCh);
				var prevCap = (!prevDud && prevCh == prevCh.toUpperCase());

				// sub is caseless but result isn't. case will depend on lookbehind and lookahead
				if (prevDud || !prevCap) {
					output += newChar.toLowerCase();
					prevCap = false;
				}
				else {
					var next = " ";
					if (location + len < src.length)
						next = src.substr(location + len, 1);

					if (next != next.toUpperCase() && next == next.toLowerCase() ) {
						//next is lowercase (and not caseless)
						output += newChar.toLowerCase();
					}
					else if (next == next.toUpperCase() && next != next.toLowerCase() ) {
						// next is uppercase (and not caseless)
						output += newChar.toUpperCase();
					}
					else {
						// next is caseless. output case determined by the case of output[length - 2]
						var pprevCh = output.length == 1 ? null : output.substr(output.length - 2, 1);
						var pprevDud = !pprevCh || !getTranslitString(pprevCh);
						if (!pprevDud && (pprevCh == pprevCh.toUpperCase())) {
							//pre-prev is in upper case. output is also uppercase
							output += newChar.toUpperCase();
						}
						else {
							output += newChar.toLowerCase();
						}
						
					}
				}
					
			}
			else if ((sub.toLowerCase() == sub.toUpperCase()) && (arr.length < 2 || !arr[1])) {
				
				// literal treatment of newChar
				output += newChar;

			}
			else if (sub != sub.toLowerCase()) {
			
				// sub not all-lowercase
				output += newChar.toUpperCase();
			}
			else {
					
					
					
				// sub is lowercase
				output += newChar.toLowerCase();
			}
			location += len;
		}
	}
	
	return output;
}


function convertIt(src,converter){
 var resultbuffer=""; 
	for(var i=0;i<src.length;i++){
	resultbuffer=converter(resultbuffer+src[i]);
	}
		return converter(resultbuffer);

}

function initTranslit() {
	if (translitHash == undefined) {
		translitHash = new Array();

		for (var i = 0; i < conversionHash.length; i++) {
			var ch = conversionHash[i][1];
			// if the translit string is not caseless, convert cyr string to upper case
			// otherwise maintain its case
			if (conversionHash[i][0].toUpperCase() != conversionHash[i][0].toLowerCase())
				ch = ch.toUpperCase();
				
			if (translitHash[ch] == undefined)
				translitHash[ch] = conversionHash[i][0];
		}
	}
}

//-- translit on-the-fly -- 

function replaceValue(node, value, stepback) {
	if (stepback == undefined)
		stepback = 0;
		
	if (isExplorer()) {
		var range = document.selection.createRange();
		range.moveStart("character", -stepback);
		range.text = value;
		range.collapse(false);
		range.select();
	}
	else {
		var scrollTop = node.scrollTop;
		var cursorLoc =  node.selectionStart;
		node.value = node.value.substring(0, node.selectionStart - stepback) + value + 
				node.value.substring(node.selectionEnd, node.value.length);
		node.scrollTop = scrollTop;
		node.selectionStart = cursorLoc + value.length - stepback;
		node.selectionEnd = cursorLoc + value.length - stepback;
	}
}

// compare positions
function positionIsEqual(other) {
	if (isExplorer())
		return this.position.isEqual(other.position);
	else
		return this.position == other.position;
  
}

function resetState() {
	this.position = new Position(this.node);
	this.transBuffer = "";
	this.cyrBuffer = "";
}


function transliterateKey(event) {
	
	if (event == undefined)
		event = window.event;
	
	var node = null;
	if (event.target) {
		node = event.target;
		}
	else if (event.srcElement) {
		node = event.srcElement;
		}
		
		
	// initialize state
	var state = stateHash[node];
	//if (state == null) {//modified below by luap.Initially type 'a'.delete it ,then type 'a'.In IE it comes outside so corrected.
	if (state == null || textBox.value=="") {
		state = new StateObject(node);
		stateHash[node] = state;
	}
	if ( (pressedKey(event) > 20) && !event.ctrlKey && !event.altKey && !event.metaKey) {

		var c = String.fromCharCode(pressedKey(event));

		// process input
		var result = process_translit(state, c);
		// finish up
		if (c != result.out || result.replace != 0) {
		  if (isExplorer())
			event.returnValue = false;
		  else
			event.preventDefault();
		  
		  replaceValue(node, result.out, result.replace);
		  
		  state.position = new Position(node);

		}
	}
	
}

function process_translit(state, c) {
	// reset state if position changed
	if (!state.position.isEqual(new Position(state.node)))
		state.reset();
		
	var result = new TranslitResult();
	
	// initial backbuffer. Add to it as characters are converted
	var backbuffer = getBackBuffer(state.node, state.cyrBuffer.length, 2);
	var chunks = new Array();
	
	state.transBuffer = state.transBuffer+ c

	var str = to_cyrillic(state.cyrBuffer+c, backbuffer, chunks);

	// remove backbuffer from output
	str = str.substr(backbuffer.length);
	result.out = str; 
	/* str is now left alone - it has the output matching contents of chunks and 
	   will be used to reinitialize backbuffers, along with chunks and state.transBuffer
	*/
	
	// get the difference between state.cyrBuffer and output
	for (var i = 0; i < Math.min(state.cyrBuffer.length, result.out.length); i++) {
		if (state.cyrBuffer.substr(i, 1) != result.out.substr(i, 1)) {
			result.replace = state.cyrBuffer.length - i;
			result.out = result.out.substr(i);
			break;
		}
	}
	if (result.replace == 0) {
			   if(result.out.length<state.cyrBuffer.length)
				result.replace=state.cyrBuffer.length- result.out.length;
		result.out = result.out.substr(Math.min(state.cyrBuffer.length, result.out.length));
				 
					   //    result.out+="\u0008"
						 
	}
	
	// update state: backbuffer, bufferArray
	if (chunks.length > 0 && chunks[chunks.length - 1] == result.out.substr(result.out.length - 1)) {
		// no convertion took place, reset state
		state.reset();
	}
	else {
		while (state.transBuffer.length > maxcyrlength) {
			state.transBuffer = state.transBuffer.substr(chunks[0].length);
			chunks.shift();
			str = str.substr(1);
		}
		state.cyrBuffer = str;
	}
	return result;
}

function getBackBuffer(node, offset, count) {
		
	if (isExplorer()) { //.tagName.toUpperCase() == "EDITOR") {
	
		var range = document.selection.createRange();
		range.moveStart("character", -offset);
		var result = range.text.substr(-count);
		if (!result)
			result = "";
			
		return result;

	} else {
		return node.value.substring(0, node.selectionStart - offset).substr(-count);
	}
}

// need this for bookmarklets
function getSelectedNode() {
  if (document.activeElement)
	return document.activeElement;
  else
	if (window.getSelection && window.getSelection() && window.getSelection().rangeCount > 0) {
		var range = window.getSelection().getRangeAt(0);
		if (range.startContainer && range.startContainer.childNodes && range.startContainer.childNodes.length > range.startOffset)
			return range.startContainer.childNodes[range.startOffset]
	}
  return null;
}
function getSelectedText() {
	if (isExplorer()) {
		return document.selection.createRange().text;
	}
	else {
		var node = getSelectedNode();
		if (node && node.value && node.selectionStart != undefined && node.selectionEnd != undefined)
			return node.value.substring(node.selectionStart, node.selectionEnd);
	}
	return "";
}
/*********************************************************************************************************************************/
//Content of malayalam.js alone.End
/*********************************************************************************************************************************/
/*********************************************************************************************************************************/
//Content of classes.js alone.Start
/*********************************************************************************************************************************/
function Position(node) {
  if (node.selectionStart != undefined)
	this.position = node.selectionStart;
  else if (document.selection && document.selection.createRange())
	this.position = document.selection.createRange();
	
  this.isEqual = positionIsEqual;
}

function StateObject(node) {
	this.node = node;
	this.reset = resetState;
	this.cyrBuffer = "";
	this.transBuffer = "";
	this.position = new Position(node);
}

function TranslitResult() {
	this.out = "";
	this.replace = 0;
}

/*********************************************************************************************************************************/
//Content of classes.js alone.End
/*********************************************************************************************************************************/
/*********************************************************************************************************************************/
//Content of utils.js alone.Start
/*********************************************************************************************************************************/
function pressedKey(event) {
  if (isExplorer())
	return event.keyCode;
  else
	return event.which;
}

function isExplorer() {
  return (document.selection != undefined && document.selection.createRange().isEqual != undefined);
}

/*********************************************************************************************************************************/
//Content of utils.js alone.End
/*********************************************************************************************************************************/

/*********************************************************************************************************************************/
//Content of UniEdit.html alone.End
/*********************************************************************************************************************************/
var defaultLang = 1;
var sellang = 1;  
var textBox;  
var e; 
var transupport = true;
function processKeys(event,txtId) {
	e = event || window.event;	e.keyValue = e.keyCode || e.which;
	var txtId = (txtId == null) ? "ta_ue" : txtId;// VIN added
	textBox= document.getElementById(txtId); //OLD Vin Commented textBox= document.getElementById('ta_ue');
	if (sellang == 1) {
		transliterateKey(event); // call transliteration function
	}
}
/*********************************************************************************************************************************/
//Content of UniEdit.html alone.End
/*********************************************************************************************************************************/


