/*
  GNU通用公共许可证(GPL)
  这是一个自由软件，任何人或机构都可以在遵守GPL守则下使用该软件。
  原 作 者 ：亦會編程（万码论坛注册 http://www.suoai.com/88988/）
  修 改 者 ：单振芳 (全国铁路时刻查询 Http://www.txtone.com.cn/)
  版  本　 ：4.0
  修改日期 ：2006-7-15
*/


LastNo=0;
SPACECHAR=" "; // 空白字元。
CandChinesePart=new Array(); //侯选字，中文字部份
CandCompPart=new Array();    //侯选字, 后码部份，如“我o”里的o
AsciiStr="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; //定义使用的编码。
AsciiStr=AsciiStr.split(',');
CodeList=CodeList.split(',');

CtrlDown=false;
CancelKey=false;
passNextKeyPress = true;

// 二分搜索法
function FindIn(s) {
var find=-1,low=0,mid=0,high=CodeList.length;
var sEng="";
  while(low<high){
    mid=(low+high)/2;
    mid=Math.floor(mid);
    sEng=CodeList[mid];
    if(sEng.indexOf(s,0)==0){find=mid;break;}
    sEng=CodeList[mid-1];
    if(sEng.indexOf(s,0)==0){find=mid;break;}
    if(sEng<s){low=mid+1;}else{high=mid-1;}
  }
  while(find>0){
    sEng=CodeList[find-1];
    if(sEng.indexOf(s,0)==0){find--;}else{break;}
  }
  return(find);
}

function GetStr(No, s){
  var sTmp="",sChi="";
  var i;
  for(i=0;i<=100;i++){
    if(No+i>CodeList.length-1){break;}
    sTmp=CodeList[No+i];
    if(sTmp.indexOf(s)==0){
      sChi=CodeList[No+i];
     // CandCompPart[i]=sChi.substring(s.length,sChi.indexOf(SPACECHAR));
      CandChinesePart[i]=sChi.substr(sChi.lastIndexOf(SPACECHAR)+1);
        var e = document.createElement("option");
            e.value = CandChinesePart[i];
            e.text = CandChinesePart[i];
            if(document.activeElement.id == "InputArea")
			{   frm.list.add(e);}
			 if(document.activeElement.id == "InputArea1")
			 { frm.list1.add(e);}
	 
       }else{
      break;
    }
  }
 
}

function Grep(s){
  var No=-1;
  for(i=0;i<=100;i++){CandChinesePart[i]="";}
  if(s!=""){
    No=FindIn(s);
    if(No>=0){GetStr(No, s);}
  }

  
  
}

// 送候选字到输入区
function SendCand(n){
  if ( n>=0 && n<=9 ) {
	  if(document.activeElement.id == "InputArea") {IME.InputArea.value="";}
	  if(document.activeElement.id == "InputArea1") {IME.InputArea1.value="";}
    SendStr(CandChinesePart[n]);
    frm.list.style.display="none" ;
	frm.list1.style.display="none" ;
  }
}




// 送字串到输入区
function SendStr(s) {
  if (s=="") { return }

  switch (browser) {
  case 1: // IE
    var r=document.selection.createRange();
    r.text=s;
    r.select(); // 不知何故，此码可取消选区。
    break;
 
  
  }
}

function ListKeyDown(e)
{
	if (!e) e=window.event;
  var key = e.which ? e.which : e.keyCode;
  switch (key) {
   //=上箭头
   case 38:
   case 40://=下箭头
   if(document.activeElement.id == "list")
    { frm.InputArea.value =frm.list.options[frm.list.selectedIndex].value;
	}
	if(document.activeElement.id == "list1")
    {frm.InputArea1.value =frm.list1.options[frm.list1.selectedIndex].value;}
	break;
     case 13:
	   if(document.activeElement.id == "list")
    { frm.InputArea.value =frm.list.options[frm.list.selectedIndex].value;
	   frm.list.style.display="none"
	}
	if(document.activeElement.id == "list1")
    {frm.InputArea1.value =frm.list1.options[frm.list1.selectedIndex].value;
	   frm.list1.style.display="none"
	}
	 
 }
}

function ImeKeyDown(e) {
  var s="";
  if(!e) e=window.event;
  var key = e.which ? e.which : e.keyCode;
  CtrlDown=false;
  passNextKeyPress=false;
  
  switch (key) {
   //=上箭头
   case 38:
   case 40://=下箭头
   if(document.activeElement.id == "InputArea")
    { IME.list.focus();
	}
	if(document.activeElement.id == "InputArea1")
    { IME.list1.focus();
	}
   
  //==Backspace
  case 8:
  
   if(document.activeElement.id == "InputArea")
     {   
	   if (IME.InputArea.value!="" &&  FindIn(IME.InputArea.value)>=0)
	   {   frm.list.style.display="block" ;
      	 s=IME.InputArea.value;
     	 IME.InputArea.value = s.substr(0, s.length-1);
     	 frm.list.options.length=0;
         Grep(IME.InputArea.value);
      	 CancelKey = true;
      	 return (false);
      }
	  else { frm.list.style.display="none" ;}
	 }
  if(document.activeElement.id == "InputArea1"  )
     {     
	   if (IME.InputArea1.value!=""  &&  FindIn(IME.InputArea1.value)>=0)
	   { frm.list1.style.display="block";
      	 s=IME.InputArea1.value;
     	 IME.InputArea1.value = s.substr(0, s.length-1);
     	  frm.list1.options.length=0;
     	 Grep(IME.InputArea1.value);
      	 CancelKey = true;
      	 return (false);
      }
	      else { frm.list1.style.display="none" ;}
	 }
    
    return (true);
 

  //==Esc
  case 27:
    IME.InputArea.value="";
	IME.InputArea1.value="";
    IME.list.value="";
	IME.list1.value=""
    CancelKey = true;
    return (false); //Esc会把全部文字删除，故禁止 Esc键 起任何作用。

  //向前翻页
  case 109: //firfox keycode '-'
       if(browser != 2) break;
  case 189: //ie keycode '-'
       if(browser != 1 && key != 109) break;
  

  //向后翻页
  case 61: //firfox keycode '-'
       if(browser != 2) break;
  case 187: //ie keycode '-'
       if(browser != 1 && key != 61) break;
   

  //==Space
  case 32:
  if(document.activeElement.id == "InputArea")
  {
    if(IME.InputArea.value!="") 
      //TODO: sound if nothing in InputArea
      SendCand(0);
      CancelKey = true;
      return(false);
    }
	if(document.activeElement.id == "InputArea1")
  {
    if(IME.InputArea1.value!="") 
      //TODO: sound if nothing in InputArea
      SendCand(0);
      CancelKey = true;
      return(false);
    }
    return(true);

  }

 //==数字和符号
  if (key>=48 && key<=57) {
   
          SendCand( key==48 ? 9 : (key-49) );
          CancelKey = true;
          return (false);
          
    }
   // return (true);
  }

function ImeKeyPress(e) {
  if(!e) e=window.event;
  var key = e.which ? e.which : e.keyCode;

	// pass keypress without processing it
	if(passNextKeyPress) {
		return (true);
	}


  //==a-z
  if (key>=97 && key<=122)
  {     
    
      if(document.activeElement.id == "InputArea")
     {   frm.list.style.display="block"   
	     s=IME.InputArea.value;
         if (s.length<MAX)
	     {
          IME.InputArea.value+=AsciiStr[key-97];
          frm.list.options.length=0;
		  Grep(IME.InputArea.value);
          }
	 }
	 
   if(document.activeElement.id == "InputArea1")
     {   frm.list1.style.display="block"   
	     s=IME.InputArea1.value;
         if (s.length<MAX)
	     {
          IME.InputArea1.value+=AsciiStr[key-97];
		  frm.list1.options.length=0;
          Grep(IME.InputArea1.value);
          }
	 }
	  
	 
      return (false);
   
  }
//    return (true);
}
  function listclick()
  {
	 frm.InputArea.value =frm.list.options[frm.list.selectedIndex].value;
	 frm.list.style.display="none"
  }

 function listclick1()
  {
	 frm.InputArea1.value =frm.list1.options[frm.list1.selectedIndex].value;
	 frm.list1.style.display="none"
  }
function BodyOnLoad() {
  browser = 
    (navigator.appName.indexOf('Microsoft') != -1) ? 1 :
    (navigator.appName.indexOf('Netscape')  != -1) ? 2 :
    (navigator.appName.indexOf('Opera')     != -1) ? 3 :
    4;
   frm.list.style.display="none";
   frm.list1.style.display="none"
  IME = {
    InputArea: document.getElementById("InputArea"),
    InputArea1: document.getElementById("InputArea1"),
	list: document.getElementById("list"),
	list1: document.getElementById("list1")
	
  }
}


