var A_RULE=new Array();  
var N_RULE=new Array();  
var ALL_ROWS;   
var ALL_CHARS;  
var CUR_ROW;    
var CUR_COL;    
var RHY_NAME=new Array();   
var RHY_CONT=new Array();   
var RHY_ABOUT=new Array();  
var PSTR;                   
var ZSTR;                  
var ori_CHR;                
var A_showrhy=new Array();   
var CUR_RHYIDX;             
var CUR_RHYCHR;             
var RULEID;
var RULENAME;
var BOOKID;
var BOOKNAME;
var RHYTHMID;
var RHYTHMNAME;
var PHASEROW;
var CHKSTAT;        
var AUTHNAME;
var BLOGNAME;
var RMNDAYS;
//======================================
//浏览器识别
//判断某种浏览器只需用if(Sys.ie)或if(Sys.firefox)等形式，而判断浏览器版本只需用if(Sys.ie == '8.0')或if(Sys.firefox == '3.0')等形式
var Sys = {};
getagent();
function getagent(){
var ua = navigator.userAgent.toLowerCase();
var s;
(s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
(s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
(s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
(s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
(s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;

/*以下进行测试 
if (Sys.ie) alert('IE: ' + Sys.ie);
if (Sys.firefox) alert('Firefox: ' + Sys.firefox);
if (Sys.chrome) alert('Chrome: ' + Sys.chrome);
if (Sys.opera) alert('Opera: ' + Sys.opera);
if (Sys.safari) alert('Safari: ' + Sys.safari);*/
}
//======================================
function GE(a){return document.getElementById(a);}  
//======================================
function WIF(if_name,str){if (window.frames[if_name]) window.frames[if_name].document.write(str);} 
//======================================
function CLS(a){
if (GE(a)) GE(a).innerHTML="";
return false;
}  
//======================================
function ctrim(a){
if (a=="" || a === undefined ) return "";
return a.replace(/(^\s*)|(\s*$)/g,"");  //trim
}
//======================================
function doinit(){
//GET RULES,PSTR/ZSTR
getrules();
getuserinfo();
if (PSTR.length<500 || RHY_NAME.length<10) alert("音韵信息出错，程序格律检查功能无法正常工作！(err:364720)");
//set div POS
setdiv();
//if(GE("r1c1")) GE("r1c1").focus();  
CHKSTAT="ON";
}
//======================================
function chkCopy(id){
clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))
GE(id).value=GE(id).value.replace(/[^\u4E00-\u9FA5]/g,'');  
}
//======================================
function txfocus(id){
if (GE("tipsDiv").style.display=="block") GE("tipsDiv").style.display="none";
GE(id).value=GE(id).value.replace(/[^\u4E00-\u9FA5]/g,'');  
ori_CHR=GE(id).value;
if (GE(id).value!="") {var a=showrhy(GE(id).value); } 
GE(id).select();
var s_tmp=id.replace("r","");
s_tmp=s_tmp.split("c");
CUR_ROW=Number(s_tmp[0]);
CUR_COL=Number(s_tmp[1]);
window.status="【当前位置】第"+CUR_ROW+"句,第"+CUR_COL+"字:";
switch (A_RULE[CUR_ROW-1].substr(CUR_COL-1,1)){
case "p":
window.status+="平声。";
break;
case "z":
window.status+="仄声。";
break;
case "x":
window.status+="可平可仄。";
break;
case "a":
window.status+="起平声韵。";
break;
case "b":
window.status+="押平声韵。";
break;
case "c":
window.status+="起仄声韵。";
break;
case "d":
window.status+="押仄声韵。";
break;
default:
window.status+="未知";
}

//alert (CUR_ROW+","+CUR_COL);
}
//======================================
function txblur(id){
window.status="";
if (ischn(GE(id).value)==false){ GE("ck"+id).style.backgroundImage = "url()"; return false; }
//alert (GE(id).value);
if (ori_CHR==GE(id).value) return false;  
if (CHKSTAT!="ON") {     
	GE(id).style.color="#000000";
	GE("ck"+id).style.backgroundImage = "url()"; 
	return false;
}

if (ischn(GE(id).value)==true) {var a=showrhy(GE(id).value); }  

var pz_tmp=A_RULE[CUR_ROW-1].substr(CUR_COL-1,1)  
switch (chkpz(GE(id).value)) { 
case "n": 
 GE(id).style.color="#999999";
 GE("ck"+id).style.backgroundImage = "url(pic/question.gif)";
 break;
case "p":
 GE(id).style.color="#0000ff";
 if (pz_tmp=="p") {GE("ck"+id).style.backgroundImage = "url(pic/good.gif)"; }
 if (pz_tmp=="x") {GE("ck"+id).style.backgroundImage = "url(pic/good.gif)"; }
 if (pz_tmp=="z") {GE("ck"+id).style.backgroundImage = "url(pic/bad.gif)"; }
 if (pz_tmp=="a") {
	GE("ck"+id).style.backgroundImage = "url(pic/good.gif)"; 
	var arhy_tmp=chkrhy(GE(id).value);
	if (arhy_tmp.length>1) sAlert(GE(id).value,"a");
	if (arhy_tmp.length==1) {
		CUR_RHYIDX=arhy_tmp[0];
		GE("rhy_chr").innerHTML="当前韵："+"<a href='javascript:showrhy2("+String(CUR_RHYIDX)+");'>"+RHY_NAME[CUR_RHYIDX]+"</a> ";
		CUR_RHYCHR=GE(id).value;
		}
	}
 if (pz_tmp=="b") {
	 if (CUR_RHYIDX>=0){ 
	   if (RHY_CONT[CUR_RHYIDX].indexOf(GE(id).value)>=0) {GE("ck"+id).style.backgroundImage = "url(pic/good.gif)";	}
	   else {GE("ck"+id).style.backgroundImage = "url(pic/bad.gif)"; }
	} else {GE("ck"+id).style.backgroundImage = "url(pic/good.gif)"; }	 
	}  
 if (pz_tmp=="c") {GE("ck"+id).style.backgroundImage = "url(pic/bad.gif)"; }
 if (pz_tmp=="d") {GE("ck"+id).style.backgroundImage = "url(pic/bad.gif)"; }
 break;
case "z":
 GE(id).style.color="#ff0000";
 if (pz_tmp=="x") {GE("ck"+id).style.backgroundImage = "url(pic/good.gif)"; }
 if (pz_tmp=="p") {GE("ck"+id).style.backgroundImage = "url(pic/bad.gif)"; }
 if (pz_tmp=="z") {GE("ck"+id).style.backgroundImage = "url(pic/good.gif)"; }
 if (pz_tmp=="a") {GE("ck"+id).style.backgroundImage = "url(pic/bad.gif)"; }
 if (pz_tmp=="b") {GE("ck"+id).style.backgroundImage = "url(pic/bad.gif)"; }
 if (pz_tmp=="c") {
	GE("ck"+id).style.backgroundImage = "url(pic/good.gif)"; 
	var arhy_tmp=chkrhy(GE(id).value);
	if (arhy_tmp.length>1) sAlert(GE(id).value,"c");
	if (arhy_tmp.length==1) {
		CUR_RHYIDX=arhy_tmp[0];
		GE("rhy_chr").innerHTML="当前韵："+"<a href='javascript:showrhy2("+String(CUR_RHYIDX)+");'>"+RHY_NAME[CUR_RHYIDX]+"</a> ";
		}
	}
 if (pz_tmp=="d") {
	if (CUR_RHYIDX>=0){ 
	   if (RHY_CONT[CUR_RHYIDX].indexOf(GE(id).value)>=0) {GE("ck"+id).style.backgroundImage = "url(pic/good.gif)";	}
	   else {GE("ck"+id).style.backgroundImage = "url(pic/bad.gif)"; }
	} else {GE("ck"+id).style.backgroundImage = "url(pic/good.gif)"; }	 
	}
 break;
case "x": 
 GE(id).style.color="#00bb5e";
 GE("ck"+id).style.backgroundImage = "url(pic/attention.gif)";
 if (pz_tmp=="a" || pz_tmp=="c") sAlert(GE(id).value,pz_tmp);  
 if (pz_tmp=="b" || pz_tmp=="d") {
	if (CUR_RHYIDX>=0){ 
	   if (RHY_CONT[CUR_RHYIDX].indexOf(GE(id).value)>=0) {GE("ck"+id).style.backgroundImage = "url(pic/good.gif)";	}
	   else {GE("ck"+id).style.backgroundImage = "url(pic/bad.gif)"; }
	} else {GE("ck"+id).style.backgroundImage = "url(pic/good.gif)"; } 
	}
 break;
default:
 GE(id).style.color="#000000";
 GE("ck"+id).style.backgroundImage = "url()"; 
}
}
function  txkeyup(id,evt)  
{  
GE(id).value=GE(id).value.replace(/[^\u4E00-\u9FA5]/g,'');  

//var keycode = event.keyCode?event.keyCode:event.which?event.which:event.charCode;
var keycode = evt.keyCode||evt.which;
//alert (keycode);
switch (keycode){
case 8:  //backspace
if(GE(id).value==""){
var tmp_c = CUR_COL-1;
var tmp_r = CUR_ROW;
if (tmp_c<1) { 
tmp_r = CUR_ROW-1;
if (tmp_r<1) tmp_r = 1;
tmp_c=N_RULE[tmp_r-1];
}
var tmp_s="r"+String(tmp_r)+"c"+String(tmp_c);
if(GE(tmp_s)) GE(tmp_s).focus();
}
break;
case 32:  //space
if (evt.keyCode==17) break;  
if (Sys.ie) {if (event.ctrlKey) break;}
var tmp_c = CUR_COL+1;
var tmp_r = CUR_ROW;
if (tmp_c>N_RULE[tmp_r-1]) { 
tmp_r = CUR_ROW+1;
if (tmp_r>ALL_ROWS) tmp_r = ALL_ROWS;
tmp_c=1;
}
var tmp_s="r"+String(tmp_r)+"c"+String(tmp_c);
if(GE(tmp_s)) GE(tmp_s).focus();
break;
case 35:  //end
var tmp_r = ALL_ROWS;
var tmp_c = N_RULE[tmp_r-1];
var tmp_s="r"+String(tmp_r)+"c"+String(tmp_c);
if(GE(tmp_s)) GE(tmp_s).focus();
break;
case 36:  //home
if(GE("r1c1")) GE("r1c1").focus();
break;
case 37:  //left
var tmp_c = CUR_COL-1;
var tmp_r = CUR_ROW;
if (tmp_c<1) { 
tmp_r = CUR_ROW-1;
if (tmp_r<1) tmp_r = 1;
tmp_c=N_RULE[tmp_r-1];
}
var tmp_s="r"+String(tmp_r)+"c"+String(tmp_c);
if(GE(tmp_s)) GE(tmp_s).focus();
break;
case 38:  //up
var tmp_c = CUR_COL;
var tmp_r = CUR_ROW-1;
if (tmp_r<1) tmp_r = 1;
if (tmp_c>N_RULE[tmp_r-1]) tmp_c=N_RULE[tmp_r-1]; 
var tmp_s="r"+String(tmp_r)+"c"+String(tmp_c);
if(GE(tmp_s)) GE(tmp_s).focus();
break;
case 39:  //right
var tmp_c = CUR_COL+1;
var tmp_r = CUR_ROW;
if (tmp_c>N_RULE[tmp_r-1]) { 
tmp_r = CUR_ROW+1;
if (tmp_r>ALL_ROWS) tmp_r = ALL_ROWS;
tmp_c=1;
}
var tmp_s="r"+String(tmp_r)+"c"+String(tmp_c);
if(GE(tmp_s)) GE(tmp_s).focus();
break;
case 40:  //down
var tmp_c = CUR_COL;
var tmp_r = CUR_ROW+1;
if (tmp_r>ALL_ROWS) tmp_r = ALL_ROWS;
if (tmp_c>N_RULE[tmp_r-1]) tmp_c=N_RULE[tmp_r-1]; 
var tmp_s="r"+String(tmp_r)+"c"+String(tmp_c);
if (GE(tmp_s)) GE(tmp_s).focus();
break; 
default:
if(ori_CHR != GE(id).value && ischn(GE(id).value)==true){
var tmp_c = CUR_COL+1;
var tmp_r = CUR_ROW;
if (tmp_c>N_RULE[tmp_r-1]) { 
tmp_r = CUR_ROW+1;
if (tmp_r>ALL_ROWS) tmp_r = ALL_ROWS;
tmp_c=1;
}
var tmp_s="r"+String(tmp_r)+"c"+String(tmp_c);
if(GE(tmp_s)) GE(tmp_s).focus();
}
}
}
//======================================
function txchange(id){
//alert (GE(id).value);
}
//======================================
function subtfocus(id){
if(GE(id))
GE(id).style.background="#F7FFD4";
if (GE(id).value=="输入标题" || GE(id).value=="输入序言")
GE(id).value="";
GE(id).select();
}

//======================================
function subtblur(id){
if(GE(id))
GE(id).style.background="#ffffff";
GE(id).value=ctrim(GE(id).value)
if (GE(id).value==""){
if (id=="txsubtitle") GE(id).value="输入标题";
if (id=="txperface") GE(id).value="输入序言";
}
}
//======================================
function  ischn(t_chr){  
//alert (t_chr.charCodeAt(0));
if(t_chr.length  ==  0) return  false;    
if(t_chr.charCodeAt(0)>19967 && t_chr.charCodeAt(0)<40870) return  true; 
return  false; 
} 
//======================================
function chkpz(t_chr) 
{ 
if(t_chr=="") return "";

t_chr=t_chr.substr(0,1)
var pflag=PSTR.indexOf(t_chr)
var zflag=ZSTR.indexOf(t_chr)
switch (true){
case pflag<0 && zflag<0 : 
return "n";
break;
case pflag>=0 && zflag<0 : 
return "p";
break;
case pflag<0 && zflag>=0 : 
return "z";
break;
case pflag>=0 && zflag>=0 : 
return "x";
break;
default:                   
return "";
}
} 
//======================================
function chkrhy(t_chr) 
{ 
var rhy_a=new Array(); 
if(t_chr=="") return rhy_a;

t_chr=t_chr.substr(0,1)
//alert (t_chr);
for (var i=0;i<RHY_NAME.length;i++){
if (RHY_CONT[i].indexOf(t_chr)>=0) rhy_a.push(i);
}
return rhy_a;
}
//======================================
function showrhy(t_chr) 
{
if (t_chr=="") return false;
t_chr=t_chr.substr(0,1);
A_showrhy=A_showrhy.slice(0,15); 
if (A_showrhy.join(",").indexOf(t_chr)>=0) {return false;}
if (A_showrhy.length>14) A_showrhy.splice(0,1);
A_showrhy.push(t_chr);
s_tmp="";
var a_tmp=chkrhy(t_chr);
switch (chkpz(t_chr)){
case "n":
s_tmp+="<span style='color:#999999;font-weight:bold'>"+t_chr+"</span>：";
break;
case "p":
s_tmp+="<span style='color:#0000ff;font-weight:bold'>"+t_chr+"</span>：";
break;
case "z":
s_tmp+="<span style='color:#ff0000;font-weight:bold'>"+t_chr+"</span>：";
break;
case "x":
s_tmp+="<span style='color:#00bb5e;font-weight:bold'>"+t_chr+"</span>：";
break;
default:
s_tmp+="<span style='color:#000000;font-weight:bold'>"+t_chr+"</span>：";
}
if (a_tmp.length<1) s_tmp+="暂未收录";
if (a_tmp.length>1) s_tmp+="(多音字) ";
for (var i=0;i<a_tmp.length;i++) {
s_tmp+="<a href='javascript:showrhy2("+String(a_tmp[i])+");'>"+RHY_NAME[a_tmp[i]]+"</a> ";
}
s_tmp+="<br>";
if (GE("msg_content").style.display=="none" && GE("msg_content").innerHTML=="") {
GE("msg_content").style.display="block";
GE("msgstat").innerHTML="-";
}
GE("msg_content").innerHTML=s_tmp+GE("msg_content").innerHTML;
//GE("msg_content").scrollTop=GE("msg_content").scrollHeight;
}
//======================================
function showrhy2(rhy_id) 
{
var s_tmp="";
s_tmp+=RHY_NAME[rhy_id]+"<br>";
s_tmp+=RHY_ABOUT[rhy_id].replace(/\n/g,'<br>')+"<br><br>";
GE("msg_content").innerHTML=s_tmp+GE("msg_content").innerHTML;
}
//======================================
function setdiv() {
//set chkdiv pos
var objs = document.getElementsByTagName("input"); 
for (var i=0; i<objs.length; i++){
	if(objs[i].type=="text" && objs[i].id.substr(0,1)=="r" && objs[i].id.indexOf("c")>1) { 
	if (GE("ck"+objs[i].id)){
	GE("ck"+objs[i].id).style.left=getPos(objs[i].id).x+18+"px";
	GE("ck"+objs[i].id).style.top=getPos(objs[i].id).y-3+"px";
}}}
//set MESSAGE pos
//GE("msg_about").style.left=document.body.clientWidth-330+"px";
GE("msg_about").style.left=document.documentElement.clientWidth-340+"px";
GE("msg_about").style.top="150px";
GE("msg_about").style.display="block";
//set tips pos
//alert (getCookie("notshowtipsdiv"));
if (getCookie("notshowtipsdiv")=="yes"){
	GE("tipsDiv").style.display="none";
	GE("r1c1").focus();
} else {
	GE("tipsDiv").style.left=getPos("r1c1").x+15+"px";
	GE("tipsDiv").style.top=getPos("r1c1").y+13+"px";
	GE("tipsDiv").style.display="block";	
	addCookie("notshowtipsdiv","yes","365","","");
	setTimeout("GE('r1c1').focus()",60000);
}
}
//======================================
function getPos(elementId) {
var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera') != -1);
var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof
var el = document.getElementById(elementId);
if(el.parentNode === null || el.style.display == 'none') {
   return false;
}      
var parent = null;
var pos = [];     
var box;     
if (Sys.ie)    //if(el.getBoundingClientRect)    //IE
{         
   box = el.getBoundingClientRect();
   var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
   var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
   return {x:box.left + scrollLeft, y:box.top + scrollTop};
}else if (Sys.firefox || Sys.chrome) //if(document.getBoxObjectFor)    // gecko    
{
   //box = document.getBoxObjectFor(el); 
   box = el.getBoundingClientRect();
   var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0; 
   var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0; 
   //pos = [box.x - borderLeft, box.y - borderTop];
   return {x:box.x - borderLeft, y:box.y - borderTop};
} else    // safari & opera    
{
   pos = [el.offsetLeft, el.offsetTop]; 
   parent = el.offsetParent;     
   if (parent != el) { 
    while (parent) { 
     pos[0] += parent.offsetLeft; 
     pos[1] += parent.offsetTop; 
     parent = parent.offsetParent;
    } 
   }   
   if (ua.indexOf('opera') != -1 || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' )) { 
    pos[0] -= document.body.offsetLeft;
    pos[1] -= document.body.offsetTop;         
   }    
}              
if (el.parentNode) { 
   parent = el.parentNode;
} else {
   parent = null;
}
while (parent && parent.tagName != 'body' && parent.tagName != 'html') { // account for any scrolled ancestors
   pos[0] -= parent.scrollLeft;
   pos[1] -= parent.scrollTop;
   if (parent.parentNode) {
    parent = parent.parentNode;
   } else {
    parent = null;
   }
}
//alert (pos[0]);
return {x:pos[0], y:pos[1]};
}
//======================================
//添加cookie
function addCookie(name,value,expires,path,domain){
 var str=name+"="+escape(value);
 if(expires!=""){
  var date=new Date();
  date.setTime(date.getTime()+expires*24*3600*1000);//expires单位为天
  str+=";expires="+date.toGMTString();
 }
 if(path!=""){
  str+=";path="+path;
 }
 if(domain!=""){
  str+=";domain="+domain;
 }
 document.cookie=str;
 //alert (document.cookie);
}
//-----------------------------------------
//取得cookie
function getCookie(name){
 var str=document.cookie.split(";")
 //alert (str);
 for(var i=0;i<str.length;i++){
 var str2=str[i].split("=");
  //alert ("["+ctrim(unescape(str2[0]))+"]["+unescape(str2[1])+"]");
  if(ctrim(unescape(str2[0]))==name) {return unescape(str2[1]);}

 }
}
//-----------------------------------------
//删除cookie
function delCookie(name){
 var date=new Date();
 date.setTime(date.getTime()-10000);
 document.cookie=name+"=n;expire="+date.toGMTString();
}
//======================================
function copy2clip(txt) {   
     if(window.clipboardData) {   
             window.clipboardData.clearData();   
             window.clipboardData.setData("Text", txt);   
			 alert("工具区文本框中的内容已成功复制到剪贴板！"); 
     } else if(navigator.userAgent.indexOf("Opera") != -1) {   
          window.location = txt;   
     } else if (window.netscape) {   
          try {   
               netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");   
          } catch (e) {   
               alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");   
          }   
          var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);   
          if (!clip)   
               return;   
          var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);   
          if (!trans)   
               return;   
          trans.addDataFlavor('text/unicode');   
          var str = new Object();   
          var len = new Object();   
          var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);   
          var copytext = txt;   
          str.data = copytext;   
          trans.setTransferData("text/unicode",str,copytext.length*2);   
          var clipid = Components.interfaces.nsIClipboard;   
          if (!clip)   
               return false;   
          clip.setData(trans,null,clipid.kGlobalClipboard);   
          alert("工具区文本框中的内容已成功复制到剪贴板！");   
     }   
}  

