/** extract from VietInterfaceIE5.js V1.1 10/01/1999
* MViet70.js   or BViet  by SonPham ca46617@csun.edu 6/15/2002
* Copyright (c) 1999, 2000,2001, 2002, 2003 MDSS Inc. All Rights Reserved.
**/
	//alert("BV");
var key;//Moz
var MVhnVal= 0; var iHN = new Array(0,0,0,0,0);//8.1 
var hnNew=0;
var MVtmVal= 0; var iTM = new Array(0,0,0,0,0);//8.2   MViet thammy,thuonghoa,AnhViet
var BoDauMV=""; // "1|\'1|`2|?3|~4|5.|^6|+*7|(8|d9-||\\";    //'' will be off
var currElm;   var popupWinMV; 

var MVOff = false;  
var tempOff=1; var hoa =0;  var MVsrc=null; 
var TatTamMV=""; var TatTamW; var offP = false; var TroVeMV=""; var TocKyMV="0|"; 
var tkType="0|";
var elm; var ready=false;
var HNWindow=0;
var RTE=0; //for htmlArea Only

//===========
function checkBoDau(){
 var tempBD=getMV("BoDau", "1|\'1|`2|?3|~4|5.|^6|+*7|(8|d9-||\\") ;
 var indBar = tempBD.indexOf('|');

 MVtmVal =   parseInt(tempBD.substring(0, indBar));  //8.2P 
  var t=  MVtmVal;
  for (var i =0; i< 5; i++){ iTM[i] = t%2; t =  Math.floor(t/2);}

BoDauMV= tempBD.substring(indBar+1)
var tatTam = getMV("TatTamMV","http|@|www|[*]" ) ; 
var indTT=tatTam.indexOf('*');
if (indTT>-1){
TatTamMV= tatTam.substring(0, indTT); TatTamW=TatTamMV.split('|'); TroVeMV= tatTam.substring(indTT+1);} 
TocKyMV= getMV("TocKyMV", "0|") ;
}

//===========

document.onclickREMOVE= function(){  
  
  if ( (event.srcElement.type == 'textarea' || event.srcElement.type == 'text') && currElm != event.srcElement) 
       { currElm=event.srcElement;  
         TocKyMV= getMV("TocKyMV", "0|") ;   
         MVhnVal=parseInt(getMV("MVhnVal", 0));
       }              
}

function MVonclick(mvevent){  
//alert("BVclick"); 
//if (BoDauMV=="") checkBoDau();
if (currElm==null) currElm=mvevent.srcElement;
else 
   if ( (mvevent.srcElement.type == 'textarea' || mvevent.srcElement.type == 'text') && currElm != mvevent.srcElement)  
     {currElm=mvevent.srcElement; TocKyMV= getMV("TocKyMV", "0|") ; MVhnVal=parseInt(getMV("MVhnVal", 0));}
}//MV

document.onkeydown= function(e){
if  (document.all){
elm= event.srcElement;
ready = elm.type=='textarea' || (elm.type=='text' && elm.name!="BoDau"
&& elm.name!="Email" && elm.name!="Email2" && elm.name!="Email3"); 
key = event.keyCode; // Cap let

} else if (e && e.which && e.target){ 
         elm= e.target;
         ready = elm.type=='textarea' || (elm.type=='text' && elm.name!="BoDau" && elm.name!="Email" && elm.name!="Email2" && elm.name!="Email3");
         key=e.which; 
        }
//==
if (ready)
    	if (key==35) {if (MVOff) {MVietOnOffButton();} else {MVietOnOffButton(); } return;} //End
}//down

document.onkeypress= function(e){

if (ready){ 
  currElm = elm; 
  if  (document.all) key = String.fromCharCode(event.keyCode); else key = String.fromCharCode(e.which);
  
  if(document.all){
     var src = document.selection.createRange(); 
     if (src.text.length ==0 && BoDauMV !="" && !MVOff) processLet(elm, event); 
  }else { if(e.target && !MVOff) processLetZ(elm);}
} //ready
}

function  processLetZ(elm){ 
	//alert("Z=");
var strOri = key;
var textIn = extract(elm);//get a word before the last char
var n = textIn.length;

           var newWord = document.VietInterface.getVietOneWord(elm.value, -1, TocKyMV, BoDauMV, MVtmVal); 
           if (newWord.length<1) {
           //only for hn : add 0 ; ck if strOri is not a punctuation to avoid killing sx in xai?:
                      if (iHN[0]==1 && " ,'.?:;-\n\r".indexOf(strOri)==-1 ) hnQuickZ(textIn, 0);  
              return;
           } else { 
           	   var indBar = newWord.indexOf('|'); 
           	   if (iHN[0]==1) hnQuickZ(newWord.substring(indBar+1), 0);
               if (indBar > -1) 
                     { var n = parseInt(newWord.substring(0, indBar)); 
                       var headStr=elm.value.substring(0, elm.value.length-n);
                       elm.value = headStr + newWord.substring(indBar+1); 
                     }
           }
    //=====================
		
}//end func
function extract(elm)
{
	var text=elm.value;	
	var n = text.length;
	text = text.substring(0, n-1);
	var lastInd = text.lastIndexOf(" "); //-1 at the beginning
	//alert("last="+ lastInd);
	return (text.substring(lastInd+1));
}

function MVonkeypress(mvevent){
   //alert("inMVPress");
     elm = mvevent.srcElement; 
     //alert("inPress2="+elm.value);
     currElm = elm; 
     if  (document.all) key = String.fromCharCode(mvevent.keyCode); 
     else key = String.fromCharCode(mvevent.which);
     //alert(String.fromCharCode(mvevent.which));
     if(document.all) { 
        var src = document.selection.createRange();
        //alert("src"+src.text.length) 
        if (src.text.length == 0 && BoDauMV !="" && !MVOff){processLet(elm, mvevent); }
     }else return;
     //if(mvevent.target && !MVOff) { alert("src"); processLetZ(elm); }
  
} //press


function mvsave(str)
{  
    mywin = window.open("text/html","mywin","width=1,height=1");
	mywin.document.open();
       //	mywin.document.write("<plaintext>"); //remove for wysiwyg
	mywin.document.write(str);
	mywin.document.execCommand("saveAs",true,".htm");
	mywin.document.close();
	mywin.close();
} 
function openWindowMV (url,name,widgets) 
{           
if (popupWinMV) popupWinMV.close();  
popupWinMV = window.open (url,name,widgets);
popupWinMV.opener.top.name="opener"; 
popupWinMV.moveTo(0, 0);
popupWinMV.focus();
}
function setCookie(name, value, expires, path, domain, secure) { var curCookie = name + "=" + escape(value) +((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") +((domain) ? "; domain=" + domain : "") +((secure) ? "; secure" : ""); document.cookie = curCookie;}
function getCookie(name) {var dc = document.cookie; var prefix = name + "="; var begin = dc.indexOf("; " + prefix);
if (begin == -1) { begin = dc.indexOf(prefix); if (begin != 0) return null; } else begin += 2; var end = document.cookie.indexOf(";", begin); if (end == -1) end = dc.length; return unescape(dc.substring(begin + prefix.length, end)); }
function fixDate(date) { var base = new Date(0); var skew = base.getTime(); if (skew > 0) date.setTime(date.getTime() - skew); }

function setMV(mvVal, mvName) 
{ var now = new Date(); fixDate(now);  now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000); setCookie(mvName, mvVal , now); }
function getMV(mvName, mvDef){ var t = getCookie(mvName); var temp=mvDef; if (t) temp = t; return temp;}

function substitute(txtArea, newWord, src){
var indBar = newWord.indexOf('|'); 

if (indBar > -1) { 
      var n = parseInt(newWord.substring(0, indBar));     
      src.moveStart("character", -n); //for keypress
      txtArea.curWord=src.duplicate();
      txtArea.curWord.text = newWord.substring(indBar+1);
      txtArea.curWord.collapse(); 
      //event.returnValue= true;  why event is null
       }
}

function hnQuickZ(word, ihn) {  //them = true (add more)

var HNList ="";
 if (MVhnVal > 0 && document.getElementById("mvframe") && document.getElementById("mvframe").contentDocument.BViet75) { 
 
 if (ihn==0) HNList = document.getElementById("mvframe").contentDocument.BViet75.duyetHN(word, 0 ); 
 else if (ihn==1) HNList = document.getElementById("mvframe").contentDocument.BViet75.duyetSX(word, 0);    //consider to restrict to s and x
 else if (ihn==2) {  HNList = document.getElementById("mvframe").contentDocument.BViet75.duyetCT(word, 0);}//consider to restrict to -c and -t
 else if (ihn==3) {  HNList = document.getElementById("mvframe").contentDocument.BViet75.duyetChTr(word, 0);}//consider to restrict to -c and -t
 else if (ihn==4) {  HNList = document.getElementById("mvframe").contentDocument.BViet75.duyetLN(word, 0);}
 //alert("HNList="+HNList);
 var hnIndex= HNList.indexOf('|',4); 
 if (hnIndex> -1  && HNList.substring(hnIndex+1) !="") 
 if (hnNew==0) {  hnNew=1; document.getElementById("mvframe").contentDocument.MVietForm.HNMsg.value = HNList.substring(hnIndex+1);}
 else { document.getElementById("mvframe").contentDocument.MVietForm.HNMsg.value += "\n----------------\n"+ HNList.substring(hnIndex+1);}
} 

}

//make sure the frame is on: window.mvframe
function hnQuick(word, ihn) {  //them = true (add more)
//alert("hnQuick="+word);

 var HNList ="";
 if (MVhnVal > 0 && window.mvframe && window.mvframe.document.BViet75) { 
 
 if (ihn==0) HNList = window.mvframe.document.BViet75.duyetHN(word, 0 );  //8.3P
 else if (ihn==1) HNList = window.mvframe.document.BViet75.duyetSX(word, 0);    //consider to restrict to s and x
 else if (ihn==2) {  HNList = window.mvframe.document.BViet75.duyetCT(word, 0);}//consider to restrict to -c and -t
 else if (ihn==3) {  HNList = window.mvframe.document.BViet75.duyetChTr(word, 0);}//consider to restrict to -c and -t
 else if (ihn==4) {  HNList = window.mvframe.document.BViet75.duyetLN(word, 0);}
 //alert("HNList="+HNList);
 var hnIndex= HNList.indexOf('|',4); 
 if (hnIndex> -1  && HNList.substring(hnIndex+1) !="") 
 if (hnNew==0) {  hnNew=1; window.mvframe.MVietForm.HNMsg.value = HNList.substring(hnIndex+1);}
 else { window.mvframe.MVietForm.HNMsg.value += "\n----------------\n"+ HNList.substring(hnIndex+1);}
} 
}

function  processLet(txtArea, mvevent){  
//alert("in processLet");
var strOri = String.fromCharCode(mvevent.keyCode); 
var src = document.selection.createRange();

var src2 = src.duplicate();  src2.moveStart("word", -1); //van
var textIn=src2.text;  

//==== 
//window.mvframe.doc &windowframe is not visible if typing in pop
if (window.mvframe && window.mvframe.document.DungCu) {TocKyMV=tkType+window.mvframe.MVietForm.KhungTK.value;}
else if (document.DungCu) {var temp = "0|";
if(document.MVietForm.ChuVan[1].checked)temp = "1|";
else if(document.MVietForm.ChuVan[2].checked)temp = "2|";
else if(document.MVietForm.ChuVan[3].checked)temp = "3|";
else if(document.MVietForm.ChuVan[4].checked)temp = "4|";
TocKyMV=temp+document.MVietForm.KhungTK.value;}

hnNew=0;  //for retaining info hn 8.3
  
//1:chu 2:van 3:instant 4:auto 
if ( " .?:;-\n\r".indexOf(strOri)>-1 && BoDauMV.indexOf(strOri)==-1){ //blank  

//check sx: 1 word 
var n = textIn.length;
if (" .?:,;-\n\r".indexOf(textIn.charAt(n-1))==-1){
var textIn2=textIn.toLowerCase();
if (iHN[1]==1  && (textIn2.charAt(0)=='s' ||textIn2.charAt(0)=='x') ) {hnQuick(textIn2, 1);}  //check iHN[1]: 8.1  see the end
if (iHN[2]==1 && (textIn2.charAt(n-1)=='c' ||textIn2.charAt(n-1)=='t') ) {hnQuick(textIn2, 2);} //8.2  
if (iHN[3]==1 && (textIn2.indexOf('ch')==0 ||textIn2.indexOf('tr')==0 ) ) {hnQuick(textIn2, 3); } //8.3 
if (iHN[4]==1  && (textIn2.charAt(0)=='l' ||textIn2.charAt(0)=='n') ) {hnQuick(textIn2, 4);} 
}

//now tocky 124 : try tocky direct via window
if  (TocKyMV.charAt(0)=='1' || TocKyMV.charAt(0)=='2'|| TocKyMV.charAt(0)=='4' )  
{ tempOff = 1; 


var newWord = null;
if ((TocKyMV.charAt(0)=='2'|| TocKyMV.charAt(0)=='4') && textIn.length>0) {
       if ( "`^".indexOf(textIn)>-1) src2.moveStart("word", -1);   //-2  get 1 more word
       textIn=src2.text; n =textIn.length; 
 //alert(textIn);
     //modify for RTE
     	 
     	 if (RTE==0) newWord = document.VietInterface.scanVan2(textIn, TocKyMV); 
         else {//RTE
         }

         if (newWord.length !=0) { 
            substitute(txtArea, newWord, src, mvevent);
            return;
         }

} //if 24 

//put length > 0 to prevent double enter
if ((TocKyMV.charAt(0)=='1'|| TocKyMV.charAt(0)=='4')&& textIn.length>0){
    
    for (var k=0; k < 6; k++){ 
       newWord = document.VietInterface.scanChu(textIn,TocKyMV );  
       if (newWord.length !=0) { 
         substitute(txtArea, newWord, src, mvevent);
         return;
       }
       src2.moveStart("word", -1); textIn=src2.text;       
    } //for k
//src1.moveStart("word", +3);   //-1 if set 0 will get the whole
//alert("0="+src.text); 
} //if 14 
 

//return; //why return
} //if  124 
}//blank

//3:instant down here
var src3 = src.duplicate();  src3.moveStart("word", -1); //van 
textIn=src3.text;
if (("  \n\r"+TroVeMV).indexOf(strOri) > -1) {tempOff = 1; return;} 
                
if(tempOff==0) return;
if (textIn.length > 0){   //remove blankbefore .
 var pLet=textIn.charAt(textIn.length-1); 
 if (pLet ==' ' && (".?'".indexOf(strOri)>-1)  )  { //7.713
   src.moveStart("character", -1); 
   txtArea.curWord=src.duplicate();
   txtArea.curWord.text = ""; 
   txtArea.curWord.collapse(); 
   return;
 }
 if (("  \n\r"+TroVeMV).indexOf(pLet)>-1) textIn="";
}  //textIn

 
if(TatTamW) for (var i=0; i<TatTamW.length; i++) { 
  if (TatTamW[i].length>0 && (textIn+strOri).indexOf(TatTamW[i] ) >-1){tempOff = 0; return;}}

if (textIn.length<1) return;

//alert(textIn);
var textInHoa=""; 
if ( iTM[1]==1) { src3.moveStart("character", -3); textInHoa=src3.text; if (textInHoa.length==1) textInHoa = textInHoa.toUpperCase(); var newWord = document.VietInterface.getVietOneWord(textInHoa+strOri, -1, TocKyMV, BoDauMV, MVtmVal);}
else {
//alert(textIn+"|"+strOri+"|"+TocKyMV+"|"+BoDauMV+"|"+MVtmVal);
var newWord = document.VietInterface.getVietOneWord(textIn+strOri, -1, TocKyMV, BoDauMV, MVtmVal);  }

if (newWord.length<1) {
  //only for hn : add 0 ; ck if strOri is not a punctuation to avoid killing sx in xai?:
    if (iHN[0]==1 && " ,'.?:;-\n\r".indexOf(strOri)==-1 ) hnQuick(textIn+strOri, 0);  
    return;} 
else { 
   var indBar = newWord.indexOf('|'); if (iHN[0]==1) hnQuick(newWord.substring(indBar+1), 0);
   if (indBar > -1) { var n = parseInt(newWord.substring(0, indBar)); src.moveStart("character", -n+1); 
       txtArea.curWord=src.duplicate(); txtArea.curWord.text = newWord.substring(indBar+1); 
       txtArea.curWord.collapse(); mvevent.returnValue= false; 
   }else return;
}
} 
 

