abc_str="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"; 
abc_str=abc_str.split(",");
ABC_str="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"; 
ABC_str=ABC_str.split(",");
var temp_words = new Array();
var page = 0;
var is_big = 0;
function change_big(){
	if(is_big==0){
		is_big=1;
		document.getElementById('message').innerHTML = '字母输入';
	}else{
		is_big=0;
		document.getElementById('message').innerHTML = '汉字输入';
	}
}
function add(str){     
        var textBox = document.getElementById("contents");
		if(typeof(textBox.selectionStart) == "number"){
            start = textBox.selectionStart;
            end = textBox.selectionEnd;
        }else{
			var start=0;
			var end=0;
		}
        var pre = textBox.value.substr(0, start);
        var post = textBox.value.substr(end);
        textBox.value = pre + str + post;
		textBox.selectionStart = start + str.length;
		textBox.selectionEnd = end + str.length;
		temp_words = '';
		document.getElementById('pinyin').value = '';
		document.getElementById('temp_word').value = '';
		page=0;
		return;
    }
function get_ch(p,a){
	var ch = '';
	var list_ch = '';
	var tmp_ch = a.split('|');
	var p_len = p.length;
	var tmp_cn_len = tmp_ch.length;
	var no = 0;
	for(k=0;k< tmp_cn_len;k++){
		if(tmp_ch[k].substr(0,p_len)==p){
			list_ch += tmp_ch[k]+",";
		}
	}
	list_ch = list_ch.split(',');
	//alert(page);
	for (i=page*10;i< page*10+10 ;i++ )
	{
		if(list_ch[i]){
			ch += no+"."+list_ch[i]+"\n";
		}
		no +=1;
	}
	temp_words = list_ch;
	return ch;
}
function send_ch(no){
	var tmp_ch = temp_words[no];
		if(tmp_ch){
		var ch_str = tmp_ch.split(' ');
		document.getElementById('pinyin').value = '';
		add(ch_str[1]);
	}
}
function wb_up(event){
var currentKey = event.charCode||event.keyCode;
	var pinyin = document.getElementById('pinyin').value ;
	if(is_big==1&& currentKey>=65 &&currentKey<=89){
		add(ABC_str[currentKey-65]);
		return false;
	}
	if(pinyin.length<=4&& currentKey>=65 &&currentKey<=89){
		var first_pinyin = pinyin.split('');
		var code_tmp = codelist[first_pinyin[0]];
		if(code_tmp){document.getElementById('temp_word').value = get_ch(pinyin,code_tmp);}
	}
	if(currentKey==8&&temp_words!=''&&pinyin.length>0){
		var first_pinyin = pinyin.split('');
		var code_tmp = codelist[first_pinyin[0]];
		if(code_tmp){document.getElementById('temp_word').value = get_ch(pinyin,code_tmp);}
		return false;
	}
	if((currentKey==33||currentKey==34)&&temp_words.length>10&&pinyin.length>0){
		var first_pinyin = pinyin.split('');
		var code_tmp = codelist[first_pinyin[0]];
		if(code_tmp){document.getElementById('temp_word').value = get_ch(pinyin,code_tmp);}
		return false;
	}
	if(pinyin.length==4&&(temp_words.length==2)){
		send_ch(page*10+0);
	}
	if(pinyin.length>4){
		document.getElementById('pinyin').value='';
		document.getElementById('temp_word').value ='';
	}
	if(currentKey>=48 &&currentKey<=57){
		send_ch(page*10+(currentKey-48));
	}
	if(currentKey==32&&temp_words!=''){
		send_ch(page*10+0);
	}
	if(!((currentKey>=48&&currentKey<=57)||(currentKey>=65 &&currentKey<=89))){
		document.getElementById('pinyin').value = '';
		document.getElementById('temp_word').value = '';
	}

}
function wb_press(event){
	//return false;

}
function wb_down(event){
	var currentKey = event.charCode||event.keyCode;
	if(currentKey>=48&&currentKey<=57&&temp_words!=''){
		return false;
	}
	if(currentKey==32&&temp_words!=''){
		return false;
	}
	if(currentKey==33&&page>0){
		page -=1;
		return false;
	}
	if(currentKey==20){
		change_big();
		return false;
	}
	if(currentKey==34&&temp_words.length>10&&page< Math.ceil(temp_words.length/10)-1){
		page +=1;
		return false;
	}
	if(currentKey==8&&temp_words!=''&&document.getElementById('pinyin').value.length>0){
		document.getElementById('pinyin').value = document.getElementById('pinyin').value.substr(0,document.getElementById('pinyin').value.length-1);
		return false;
	}
	//alert(currentKey);
	if(currentKey>=65&&currentKey<=89){
		if(is_big==0){
			if(document.getElementById('pinyin').value.length<4){
			document.getElementById('pinyin').value += abc_str[currentKey-65];
			}
		}	
	return false;
	}

}

function go_focus(){
	document.getElementById('contents').focus();
	return ;
}
function load(){
	document.getElementById('contents').focus();
}