//Intellectual property of Sitemarvel Pty.ltd.,
//Copying and use of these codes are expressly prohibited
//(c) Copyright by N. R. Das
//Disclaimer: No liability, express or implied, is assumed 
//by the intellectual owners of these programs for any damage
//caused to orsuffered by any users or third parties


//alert(browzer)


var thisFrame=""
var isAlt = false;
var isShift = false;
var isCapslock = false;
var isCompose=false;
var key1 = null;
var key2 = null;
var TransKey="";
var cursorAt=0;
var fText="";
var txtLen=0;
var cursorPos=0;
var txtLeft="";
var txtRight="";
var selectedText="";
var preText="";
var postText="";
var inkey="";
var ffnsFuzzIteration=0;
var cursorPos=0;
var editText="";

var ansiKeys=  "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~:";
var banglaKeys="AQXGYNqBSLOcVDTa[\\]^_wrgPWxfMpsRKlidIebkoUuhmnZ{|}F:"; 

var outCasts="핖";

// function to get characters between 129 and 160
function getOutcasts(x){
	return outCasts.charAt(x-128);
}

function kbtranSingle(k) {
	var found=ansiKeys.indexOf(String.fromCharCode(k));
	if (found != -1) {			//normally typable chars
		if (k==124) 
			return String.fromCharCode(252);
		else	
			return banglaKeys.charAt(found);
	}
	else {
		//if (k==124) 
		//	return String.fromCharCode(252);
		//else {	
			if (k==38) 
				return String.fromCharCode(249);
			else
				return String.fromCharCode(k);
		//}		
	}
}

function kbtranMulti(K1, K2) {
	K1=String.fromCharCode(K1);
	K2=String.fromCharCode(K2);
	if (K1== '.' ){				//46
		if (K2 =='i') return 116;	//dirgha i kar(lc)
		if (K2 =='I') return 67;	//'C';  dirgha I (uc)
		if (K2 =='u') return 118;	//dirgha u lower case (lc)
		if (K2 =='U') return 69;	//'E' dirgha U uppercase (uc)
		if (K2 =='e') return 121;	//oikar lc
		if (K2 =='E') return 72;	//oi uc 72
		if (K2 =='O') return 74;	//ou uc 74
		if (K2 =='o') return 122;	//oukar lc
		if (K2 ==';') return 133;	//anuswar
		if (K2 =='.') return 134;	//chandravindu
		if (K2 =='d') return 130;	//rha
		if (K2 =='D') return 131;	//rhha
		if (K2 =='z') return 135;	//hasant ta
		if (K2 =='Z') return 137;	//lower t_ta z
		if (K2 =='c') return 136;	//cha left
		if (K2 =='q') return 138;	//'the' top
		if (K2 =='n') return 153;	//na fola
		if (K2 =='j') return 236;	//ja fola (lc) 147 too
		if (K2 =='J') return 106;	//'j';	antostha ja (uc)
		if (K2 =='b') return 154;	//ba fola
		if (K2 =='v') return 155;	//va fola
		if (K2 =='r') return 159;	//ra fola
		if (K2 =='l') return 161;	//la fola
		if (K2 =='m') return 156;	//ma fola
		if (K2 =='h') return 162;	//ha fola
		if (K2 =='\'') return 96;	//39  in single quote left - out single quote right
		if (K2 =='\"') return 126;	//34 in double quote left - out double quote right
		if (K2 ==':') return 242;	//small umo - left partner of a conjunct
		if (K2 =='y') return 243;	//small yeeon - left partner of a conjunct
	}
	if (K1=='|') {
		if (K2=='|') return 253;
	}
	if (K1 =='b'){
		if (K2 =='Q') return 200;	//bQ
		if (K2 =='b') return 201;	//bQ
	}
	if (K1 =='l'){
		if (K2 =='l') return 212;	//ll
	}
	if (K1 =='g'){
		if (K2 =='u') return 170;	//gu
		if (K2 =='Q') return 171;	//gQ
	}
	if (K1 =='h'){
		if (K2 =='u') return 232;	//hu
		if (K2 =='r') return 233;	//hre
		if (K2 =='n') return 234;	//hn
		if (K2 =='m') return 235;	//hm				
	}
	if (K1 =='j'){
		if (K2 =='g') return 174;	//jg
		if (K2 =='j') return 175;	//jj
	}
	if (K1 =='k') {
		if (K2 =='k') return 163;	//kk
		if (K2 =='t') return 164;	//kt
		if (K2 =='z') return 165;	//kz
		if (K2 =='r') return 166;	//kr
		if (K2 =='l') return 167;	//kl
		if (K2 =='m') return 168;	//km
		if (K2 =='s') return 169;	//ks
	}	
	if (K1 =='L'){ 
		if (K2 =='t') return 176;	//tt
	}
	if (K1 =='m'){
		if (K2 =='n') return 204;	//mn
		if (K2 =='b') return 205;	//mb
		if (K2 =='v') return 206;	//mv
		if (K2 =='m') return 207;	//mm				
		if (K2 =='r') return 208;	//mr				
		if (K2 =='l') return 209;	//ml				
	}
	if (K1 =='n'){
		if (K2 =='k') return 187;	//nk
		if (K2 =='g') return 188;	//ng
		if (K2 =='c') return 189;	//nc
		if (K2 =='j') return 190;	//nj
		if (K2 =='z') return 191;	//nz
		if (K2 =='r') return 192;	//nr
		if (K2 =='Q') return 193;	//nQ
		if (K2 =='q') return 193;	//nq
		if (K2 =='n') return 194;	//nn				
		if (K2 =='b') return 195;	//nb														
		if (K2 =='y') return 196;	//noyjawFola
	}
	if (K1 =='O'){
		if (K2 =='k') return 172;	//umaka				
		if (K2 =='K') return 173;	//umaKHa									
	}
	if (K1 == 'p' ) {
		if (K2 =='z') return 197;	//pz
		if (K2 =='p') return 198;	//pp
		if (K2 =='r') return 199;	//pr				
	}
	if (K1 =='q') {
		if (K2 =='q') return 181;	//qq
		if (K2 =='Q') return 182;	//qQ
		if (K2 =='b') return 183;	//qb
		if (K2 =='v') return 184;	//qv
		if (K2 =='m') return 185;	//qm
		if (K2 =='r') return 186;	//qr					
	}
	if (K1 =='r'){
		if (K2 =='u') return 210;	//ru
		if (K2 =='U') return 211;	//rU
	}
	
	if (K1 =='s'){
		if (K2 =='k') return 220;	//sk
		if (K2 =='t') return 223;	//st
		if (K2 =='z') return 224;	//sz
		if (K2 =='Z') return 225;	//sZu
		if (K2 =='n') return 226;	//sn
		if (K2 =='b') return 227;	//sb
		if (K2 =='m') return 228;	//sm
		if (K2 =='j') return 229;	//sj				
		if (K2 =='r') return 230;	//sr
		if (K2 =='h') return 231;	//sh
	}
	if (K1 =='t'){
		if (K2 =='t') return 176;	//ZZ
	}
	if (K1 =='v'){
		if (K2 =='r') return 202;	//vr
		if (K2 =='R') return 203;	//vR
	}
	if (K1 =='w'){
		if (K2 =='u') return 213;	//Su talabya sha rhasha-u
		if (K2 =='r') return 214;	//Sr talabya sha ra fola
		if (K2 =='R') return 215;	//SR talabya sha ra fola rhasha-u
	}
	if (K1 =='x'){
		if (K2 =='t') return 216;	//xt
		if (K2 =='T') return 217;	//xT
		if (K2 =='n') return 218;	//wR
		if (K2 =='m') return 219;	//wm
	}
	if (K1 =='Z'){
		if (K2 =='Z') return 180;	//ZZ
	}
	if (K1 =='z'){
		if (K2 =='z') return 177;	//zz
		if (K2 =='r') return 178;	//zr
		if (K2 =='R') return 179;	//zRu
	}
	else {
		return 0;
	}
}




function checkDownNew(e) {	
	var pressed;
	if(!e){ // if IE
        e=window.event;
    }
	pressed = (window.event) ? e.keyCode : e.which;
	
	if (!window.event) {
		if (e.target.type != "textarea")
			return true;
		if (CtrlPressed(e) && AltPressed(e)) {
			//window.status = "Begin Composing: FF/NS"
			isCompose=true;
			ffnsFuzzIteration=0;
			key1=null;
			key2=null;
			return true;
		}
		return true;	
	}
	else {
		if (CtrlPressed(e) && AltPressed(e)) { //ie
			//window.status = "Begin Composing: IE"
			isCompose=true;
			key1=null;
			key2=null;		
			ffnsFuzzIteration=1;	
			window.event.cancelBubble=true;
			window.event.returnValue=false;
		}
		return true;
	}
}

function checkDown(e) {	
	var pressed;
	pressed=(isNS4) ? e.which : window.event.keyCode;
	window.status = "Browser: " + browzer + ", Event: " + window.event + ", e.Which: " + e.which;return true;
	
	if (isNS4) {
		//ctrl-t
		if (e.target.type != "textarea")
			return true;
		if (e.modifiers==2 && pressed==20) {
			isCompose=true;
			key1=null;
			key2=null;			
			return false;
		}
	}
	if (isIE4) {
		//ctrl-alt
		
		if(window.event.altKey && window.event.ctrlKey) { 		//^t=84	pressed==84 && 
			isCompose=true;
			key1=null;
			key2=null;
			window.event.cancelBubble=true;
			return false;
		}
	}	
	
	return true;
}

function mapCode(e) {
	preText=window.document.Keyed.inText.value;
	doChar(e)
	//NSfix();
	window.document.Keyed.inText.value=postText;
}

function IEfix() {
	var i, edited=false, revised="";
	var pre, pst, prePart, postPart;
	pre = preText; 
	pst = window.document.Keyed.inText.value; 
	
	var bt = (isIE4)?"IE4 ":"NS4 "
	//status=bt + pre +" <==Old:New ==> "+pst+" Now entered: "+TransKey;

	if (TransKey=="") { //nothing was done
		window.document.Keyed.inText.value=preText;
		return;
	}
	if (pst.length<=pre.length) { //text was edited
		edited=true;
	}
	
	//now search and compare and fix any anomalies
	for (i=0; i<pre.length; i++) {
		if (pre.charCodeAt(i) != pst.charCodeAt(i)) {	//alert("Diff Char at Position: "+i);
			break;
		}
	}
	if (edited) { //char changed in text
		prePart=pre.substring(0,i);
		postPart=pre.substring(i+1,pre.length);
		revised=prePart+TransKey+postPart;
	}
	else { 
		if (i<pre.length) { //if character inserted in existing text
			prePart=pre.substring(0,i);
			postPart=pre.substring(i,pre.length);
			revised=prePart+TransKey+postPart;
		}
		else {
			revised=preText+TransKey; //char entered at end of text
		}
	}
	preText=revised
	window.document.Keyed.inText.value=revised;
	TransKey=""
}

function NSfix()
{
	var i, edited=false, revised="";
	var pre, pst, prePart, postPart;
	pre = preText; 
	pst = window.document.Keyed.inText.value; 

	var bt = (isIE4)?"IE4 ":"NS4 "
	//status=bt + pre +" <==Old:New ==> "+pst+" "+TransKey;
	//status=pst+" length="+pst.length

	if (TransKey=="") {
		window.document.Keyed.inText.value=preText;
		return;
	}
	if (pst.length<=pre.length) {
		edited=true;
	}
	for (i=0; i<pre.length; i++) {
		if (pre.charCodeAt(i) != pst.charCodeAt(i)) {	//alert("Diff Char at Position: "+i);
			break;
		}
	}
	if (edited) {
		prePart=pre.substring(0,i);
		postPart=pre.substring(i+1,pre.length);
		revised=prePart+TransKey+postPart;
	}
	else { //if character inserted in existing text
		if (i<pre.length) {
			prePart=pre.substring(0,i);
			postPart=pre.substring(i,pre.length);
			revised=prePart+TransKey+postPart;
		}
		else {
			revised=preText+TransKey;
		}
	}
	preText=revised
	window.document.Keyed.inText.value=revised;
	TransKey=""
}

function checkDown(e) {	
	var pressed;
	pressed =(!isIE4) ? e.which : window.event.keyCode;
	
	if (isNS4) {
		//ctrl-t
		if (e.target.type != "textarea")
			return true;
		if (e.modifiers==2 && pressed==20) {
			isCompose=true;
			key1=null;
			key2=null;			
			return false;
		}
	}
	if (isIE4) {
		//ctrl-alt
		
		if(window.event.altKey && window.event.ctrlKey) { 		//^t=84	pressed==84 && 
			isCompose=true;
			key1=null;
			key2=null;
			window.event.cancelBubble=true;
			return false;
		}
	}	
	
	return true;
}
function doChar(e) {
	var pressed;
	var x,y;
	pressed=(isNS4)?e.which:window.event.keyCode;
	
	inkey=String.fromCharCode(pressed);
	if (isNS4) {
		if (e.target.type != "textarea")
			return true;
		
		
		if (e.modifiers==2 && pressed==20) {
	
			isCompose=true;
			key1=null;
			key2=null;
			inkey="";
			TransKey="";
			return false;
		}
		if (pressed==8) {
			postText=postText.substring(0,postText.length-1);
			changeLine();
			return true;
		}
		if (pressed >21 && pressed<26)
			return false;
	}
	if (isIE4) {
		if(window.event.altKey && window.event.ctrlKey){
			isCompose=true;
			key1=null;
			key2=null;
			return false;
		}
	}	
	if (isCompose) {
		if (key1==null){
			key1=pressed;
			if (isIE4){
				window.event.cancelBubble=true;
				window.event.returnValue=false;
			}
			TransKey="";
			inkey=pressed;
			return false;
		}
		else {
			key2=pressed;
			TransKey=kbtranMulti(key1, key2);
			if (TransKey>127 && TransKey < 160){
				TransKey=getOutcasts(TransKey);
			}
			else {
				TransKey=String.fromCharCode(TransKey);
			}
			key1=null;
			key2=null;
			isCompose=false;
		}
	}
	else {
		TransKey=kbtranSingle(pressed);
	}	
	//fTextLeft=fTextLeft+TransKey;	//fText=fTextLeft+fTextRight; 	//cursorPos=fTextLeft.length;	//status = "Cursor Position: "+fTextLeft+" _ "+fTextRight;
	//window.status="ASCII: "+String.fromCharCode(pressed)+"["+pressed+"] ==> Bengali: "+TransKey+"["+TransKey.charCodeAt(0)+"]"
	window.status="Browser: " + browzer + ", Event: " + window.event + ", e.Which: " + e.which
	if (isNS4) {
		preText= window.document.Keyed.inText.value;	
		window.document.Keyed.inText.blur();
		postText=preText+TransKey;
		window.document.Keyed.inText.focus();
		return false;
	}
	else if (isIE4) {
		preText= window.document.Keyed.inText.value;
		window.event.cancelBubble=false;
		window.event.returnValue=true;	
		window.document.Keyed.inText.blur();
		window.document.Keyed.inText.focus();
		return true;
	}
}

function changeLine()
{
	
	window.document.Keyed.inText.value=postText;
}
function doCharNew(e) {
	var pressed;
	var x,y;

	if(!e){ // if IE
        e=window.event;
    }
	
	pressed = window.event ? e.keyCode : e.which
	inkey=String.fromCharCode(pressed);
	//window.status="Pressed Key: "+inkey

	
	
	if (!window.event) { //not IE
	
		if (isCompose && ffnsFuzzIteration==0) {
			ffnsFuzzIteration=1;
			return false;
		}
		
		if (pressed<=32) {
			
			if (pressed==8) { //backspace 
				preText = window.document.Keyed.inText.value;
				cursorPos=getCaretPosition(document.getElementById("inText"))
				txtLeft=preText.substring(0,cursorPos-1)
				txtRight=preText.substring(cursorPos+1)
				//status="Cursor Position: "+txtLeft+" <" +cursorPos+ "> "+txtRight
				postText = txtLeft+txtRight;
				return true;
			}
			else {
				preText = window.document.Keyed.inText.value;
				postText=preText;
				return true;
			}
		}
		
		if ( CtrlPressed(e) && (inkey=="c" || inkey=="C")) {
			//copy
			return true;
		}
		if ( CtrlPressed(e) && (inkey=="v" || inkey=="V")) {
			//paste
			return true;
		}
	}
	
		
	if (isCompose) {
		if (key1==null && key2==null && ffnsFuzzIteration > 0){
			key1 = pressed;
			if (isIE4){
				window.event.cancelBubble=true;
				window.event.returnValue=false;
			}
			TransKey="";
			inkey="";
			//window.status="Composition in progress"
			return false;
		}
		else {
			key2=pressed;
			TransKey=kbtranMulti(key1, key2);
			if (TransKey>127 && TransKey < 160){
				TransKey=getOutcasts(TransKey);
			}
			else {
				TransKey=String.fromCharCode(TransKey);
			}
			//window.status="ASCII: ["+String.fromCharCode(key1) + " + " + String.fromCharCode(key2) + "] ==> Bengali: " + TransKey + "[" + TransKey.charCodeAt(0) + "]"
			key1=null;
			key2=null;
			isCompose=false;
		}
	}
	else {
		TransKey=kbtranSingle(pressed);
		//window.status="ASCII: "+String.fromCharCode(pressed)+" [" + pressed + "] ==> Bengali: " + TransKey + " [" + TransKey.charCodeAt(0)+"]"
	}	
	
	
	
	
	if (TransKey) {
		if (!window.event) { // not IE
			preText = postText
			cursorPos=getCaretPosition(document.getElementById("inText"));
			editText = document.Keyed.inText.value;
		
			if (preText=="") {
				textLeft="";
				textRight="";
			}
			else {
				txtLeft=preText.substring(0,cursorPos-1);
				txtRight=(preText.length>cursorPos-1)?preText.substring(cursorPos):"";
			}
			postText=txtLeft+TransKey+txtRight;
			//cursorPos++;
		}
		else { //IE
			preText = postText
			cursorPos=getCaretPosition(document.getElementById("inText"));
			editText = document.Keyed.inText.value;
		
			if (preText=="") {
				textLeft="";
				textRight="";
			}
			else {
				txtLeft=preText.substring(0,cursorPos-1);
				txtRight=(preText.length>=cursorPos)?preText.substring(cursorPos):"";
			}
			postText=txtLeft+TransKey+txtRight;
			//cursorPos++;
		}
	}
	window.status="Text L: "+txtLeft+" Typed: "+TransKey+ " Pos: " + cursorPos + " Text R: "+txtRight + "FullText: " + postText
	if (!window.event) {
		changeLine(cursorPos+1)	
	}
	
	var rvThis 
	if (window.event) {
		window.event.cancelBubble=false;
		window.event.returnValue=true;	
		window.document.Keyed.inText.blur();
		window.document.Keyed.inText.focus();
		setCaretPosition(document.getElementById("inText"), cursorPos)
		rvThis = true;
	}
	else {
		
		rvThis = false;
	}
	
	
	//alert("Length: " +editText.length + " CsrPos: "+ cursorPos+" PreText: " +preText )	
	return rvThis;
}

function changeLineNew(cp)
{
	window.document.Keyed.inText.value=postText;
	setCaretPosition(document.getElementById("inText"), cp)
}



// return true if Alt key is pressed
function AltPressed(e) {
        if (window.event) { //if IE
                return (window.event.altKey);
        } else {
                if(e.modifiers)
                        return (e.altKey || (e.modifiers % 2));
                else
                        return e.altKey;
        }
};

// return true if Ctrl key is pressed
function CtrlPressed(e) { //if IE
        if (window.event) {
                return (window.event.ctrlKey);
        } else {
                return (e.ctrlKey || (e.modifiers==2) || (e.modifiers==3) || (e.modifiers>5));
        }
};

// return true if Shift key is pressed
function ShiftPressed(e) {
        if (window.event) { //if IE
                return (window.event.shiftKey);
        } else {
                return (e.shiftKey || (e.modifiers>3));
        }
};



function getCaretPosition(ctrl) {
	var CaretPos = 0;
	// IE Support
	if (document.selection) {
		ctrl.focus();
		var Sel = document.selection.createRange ();
		var Sel2 = Sel.duplicate();
		Sel2.moveToElementText(ctrl);
		//var CaretPos = -1;
		while(Sel2.inRange(Sel)) {
			Sel2.moveStart('character');
			CaretPos++;
		}
		//alert(isIE4 + ", "+isNS4 +" IE: CaretPos: " +CaretPos  )
	}

	// Firefox support
	else {
		
		if (ctrl.selectionStart || ctrl.selectionStart == '0') {
			CaretPos = ctrl.selectionStart;
			//alert(isIE4 + ", "+isNS4 +" FF: CaretPos: " +CaretPos  )
		}
	}
	return (CaretPos);

}

function fetchCaretPosition (ctrl) {

	var CaretPos = 0;
	// IE Support
	if (document.selection) {

		ctrl.focus ();
		var Sel = document.selection.createRange ();

		Sel.moveStart ('character', -ctrl.value.length);

		CaretPos = Sel.text.length;
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionStart;

	return (CaretPos);

}


function setCaretPosition(ctrl, pos)
{

	if(ctrl.setSelectionRange)
	{
		ctrl.focus();
		ctrl.setSelectionRange(pos,pos);
	}
	else if (ctrl.createTextRange) {
		var range = ctrl.createTextRange();
		range.moveStart('character', pos);
		range.collapse(true);
		//range.moveEnd('character', pos);
		range.select();
	}
}


