///
///
try{
	if(undefined==i18_res){
		i18_res={};
	}
}catch(e){
	i18_res={};
}
i18_res["js.chat.contentNeed"]="您輸入的內容不能為空";
i18_res["js.chat.contentNumLimit"]="字數不能超過40";
i18_res["js.chat.emoFormat"]="請勿拆散表情格式";
i18_res["js.chat.timeLimit"]="系統設定";
i18_res["js.chat.timeLimittail"]="秒內只可發言一次，請勿頻繁發言";
i18_res["js.chat.noPlayer"]="該玩家不存在";
i18_res["js.chat.contentInvalid"]="您輸入的內容包含敏感字";
i18_res["js.chat.noAlliance"]="你沒有加入聯盟";
i18_res["js.chat.closed"]="聊天功能暫時關閉";
i18_res["js.chat.forbidden"]="您已經被禁言了";
i18_res["js.chat.syserr"]="操作未成功，請檢查您的操作是否正確，稍後再試。";
///
///
var page = '';
var domain ='public_content';
var tlast;
var right = '';
function enterHandler(event,time)
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		sendRequest(time);
	} 
}
function initControls(){
	document.getElementById('pub').style.display='';
	document.getElementById('pro').style.display='none';
	document.getElementById('pri').style.display='none';
}
function initAjax(){
	
}

function ajaxSendPost(url, data, processRequest) 
{
	var pars = ""+data;
    var url = ""+url;  
    new Ajax.Request(    
        url,    
        {    
            method: 'post', 
            parameters: pars, 
            onComplete: processRequest    
        }    
    );    
} 


function sendNull(){
	var speaker = parent.window.nickname;
	var system = parent.window.tribe;
	var allianceId=parent.window.allianceId;
	var data="domain="+escape(encodeURIComponent(domain))+"&speaker="+escape(encodeURIComponent(speaker));
	data+="&allianceId="+allianceId;
	if(system=='Gaul'){data+="&system=AL";}
	if(system=='Rome'){data+="&system=BY";}
	if(system=='Teuton'){data+="&system=ZY";}
	
	ajaxSendPost("chat.do",data , processSendRequest);
	//add by conel 20090811
	window.setTimeout("sendNull()",parent.refreshTime); 
}
function sendRequest(time){
	var data="";
	var content = document.getElementById('chatContent').value;
	var speaker = parent.window.nickname;
	var system = parent.window.tribe;
	var allianceId=parent.window.allianceId;
	if(content==null || content==""){
		alert(i18_res["js.chat.contentNeed"]);
		return ;
	}
	else if(content.length>40){
		alert(i18_res["js.chat.contentNumLimit"]);
		return ;
	}

    if (content.indexOf('[em') != -1) {
        var end = content.indexOf('[em') + 4;
        var end1 = content.indexOf('[em') + 5;
        var end2 = content.indexOf('[em') + 7;
        if (content.substring(end, end + 1) != ']' && content.substring(end1, end1 + 1) != ']' && content.substring(end2, end2 + 1) != ']') {
            alert(i18_res["js.chat.emoFormat"]);
            return;
        }
    }
	
	
	
	if(speaker==null || speaker==""){
		return ;
	}
	
	var difference =Number((time-tlast)/1000).toFixed(2);
	if(difference<parseInt(parent.chatTime)/1000)
	{
		alert(i18_res["js.chat.timeLimit"] + parseInt(parent.chatTime)/1000+i18_res["js.chat.timeLimittail"] );
		return false;
	}
	tlast=time;
	
	
	data+="speaker="+escape(encodeURIComponent(speaker))+"&content="+escape(encodeURIComponent(content));
	data+="&allianceId="+allianceId;
	if(domain=='protected_content'){
		data+="&domain=protected";
		if(system=='Gaul'){data+="&system=AL";}
		if(system=='Rome'){data+="&system=BY";}
		if(system=='Teuton'){data+="&system=ZY";}
	}
	if(domain=='private_content'){
		data+="&domain=private";
		var receiver = document.getElementById('receiver').value;
		if(receiver==null || receiver==""){
			return ;
		}
		data+="&receiver="+escape(encodeURIComponent(receiver));
	}
	if(domain=='public_content'){
		data+="&domain=public";
	}

	ajaxSendPost("chat.do", data,processSendRequest);
	
	document.getElementById('chatContent').value=""
	document.getElementById('chatContent').focus();
}

String.prototype.ReplaceAll = stringReplaceAll;
function  stringReplaceAll(AFindText,ARepText){
  raRegExp = new RegExp(AFindText,"g");
  return this.replace(raRegExp,ARepText)
}

function processSendRequest(response) {
	if(response.readyState==4 && response.status==200) {
		if(response.responseText!="") {
			if(response.responseText=="nickname_noexist"){
				alert(i18_res["js.chat.noPlayer"]);
				return ;
			}else if(response.responseText=="dirty_words"){
				alert(i18_res["js.chat.contentInvalid"]);
				return ;
			}
			else if(response.responseText=="noAlly"){
				alert(i18_res["js.chat.noAlliance"]);
				return ;
			}
			
			var chatContent = document.getElementById(domain);

			var msgDiv = document.createElement("div");
			
			msgDiv.innerHTML = response.responseText+"";
			if (response.responseText=="syserr")
			{
				msgDiv.innerHTML = i18_res["js.chat.syserr"];
			}
			var str1=("<div>"+msgDiv.innerHTML+"</div>").toLowerCase();
			var str2=chatContent.innerHTML.toLowerCase();
			
			if(response.responseText=="close"){
				chatContent.innerHTML=i18_res["js.chat.closed"];
				return ;
			}

			if(response.responseText=="forbidden"){
				chatContent.innerHTML=i18_res["js.chat.forbidden"];
				return ;
			}
			var strtext = response.responseText.toLowerCase();
			if(strtext.indexOf("kscripts")>0)//find kscript, filter it for the following code.
			{
				strtext=strtext.replace(/kscripts/g,        "ks");
			}
			if(strtext.indexOf("script")>0)//WARNING!!!! find script. skipt it.
			{
				return;
			}
			//chatContent.innerHTML="";
			if(chatContent.childNodes.length>30){
				chatContent.removeChild(chatContent.firstChild);
			}
			chatContent.appendChild(msgDiv);
			
			if(str1!=str2)
			{
				scrollbarMovetoTail();
			}
			
			//foo();
		}
		//add by conel 20090811
		//window.setTimeout("sendNull()",parent.refreshTime); 
	}
}
function foo()
{
    window.setInterval(function()
    {
    	sendNull(domain);
    }, 2000);
}


/*
*utils
*
*/
function openFace() {
    var pars = "method=checkRight";
    var url = "commonProcess.do";
    var checkAjax = new Ajax.Request(
        url,
        {
            method: 'post',
            parameters: pars,
            onComplete: checkRightResponse
        }
    );
}

function checkRightResponse(response) {
    var top = parent;
    var rr = response.responseText;
    if (rr == "e1") {
        top.location.href = "index.jhtml";
    } else if (rr == "e2") {
        $("errInfo").innerHTML = i18_res["js.map.systemError"];
    } else if (rr == "f0" || rr == "f1" || rr == "f2" || rr == "f3" || rr == "f4" || rr == "f5" || rr == "f6") {
        window.location.href = "frequency.jhtml?" + rr;
    } else {
        right = rr;
        doopenFace();
    }
}

function doopenFace() {
	var LayerFace = document.getElementById("LayerFace");
	if(LayerFace==null) {
		LayerFace = document.createElement("div");
		LayerFace.id = "LayerFace";
		LayerFace.style.position = "absolute";
		LayerFace.style.width = 260;
		LayerFace.style.height = "auto";
		LayerFace.style.left = 0;
		LayerFace.style.top = 0;
		LayerFace.style.border = "#95FF95 solid 1px";
		LayerFace.style.background = "#EEFFEE";
		LayerFace.style.padding = 4;
		
		var str = "";
		for(var i=0; i<=59; i++) {
			str += "<img id=\"" + i +"\" src=\"kscripts/face/" + i + ".gif\" width=20 height=20 style=\"border:#EEFFEE solid 1px;\" onMouseOver=\"this.style.border='#0000FF solid 1px;'\" onmouseout=\"this.style.border='#EEFFEE solid 1px;'\" onclick=\"addFace(this.id)\" /> ";
        }
        /*if (right == 'ok1') {
            for (var i = 96; i <= 107; i++) {
                str += "<img id=\"" + i + "\" src=\"kscripts/face/" + i + ".gif\" width=20 height=20 style=\"border:#EEFFEE solid 1px;\" onMouseOver=\"this.style.border='#0000FF solid 1px;'\" onmouseout=\"this.style.border='#EEFFEE solid 1px;'\" onclick=\"addFace(this.id)\" /> ";
            }
        } else if (right == 'ok2') {
            for (var i = 96; i <= 104; i++) {
                str += "<img id=\"" + i + "\" src=\"kscripts/face/" + i + ".gif\" width=20 height=20 style=\"border:#EEFFEE solid 1px;\" onMouseOver=\"this.style.border='#0000FF solid 1px;'\" onmouseout=\"this.style.border='#EEFFEE solid 1px;'\" onclick=\"addFace(this.id)\" /> ";
            }
        } else if (right == 'ok3') {
            for (var i = 96; i <= 101; i++) {
                str += "<img id=\"" + i + "\" src=\"kscripts/face/" + i + ".gif\" width=20 height=20 style=\"border:#EEFFEE solid 1px;\" onMouseOver=\"this.style.border='#0000FF solid 1px;'\" onmouseout=\"this.style.border='#EEFFEE solid 1px;'\" onclick=\"addFace(this.id)\" /> ";
            }
        } else if (right == 'ok4') {
            for (var i = 96; i <= 98; i++) {
                str += "<img id=\"" + i + "\" src=\"kscripts/face/" + i + ".gif\" width=20 height=20 style=\"border:#EEFFEE solid 1px;\" onMouseOver=\"this.style.border='#0000FF solid 1px;'\" onmouseout=\"this.style.border='#EEFFEE solid 1px;'\" onclick=\"addFace(this.id)\" /> ";
            }
        }*/
        var rightArr = right.split("");
        var startgif = 1001;
        for (var i = 0; i < rightArr.length; i++) {
            var tempr = rightArr[i];
            if ('1' == tempr) {
                str += "<img id=\"" + (startgif + i) + "\" src=\"kscripts/face/" + (startgif + i) + ".gif\" width=20 height=20 style=\"border:#EEFFEE solid 1px;\" onMouseOver=\"this.style.border='#0000FF solid 1px;'\" onmouseout=\"this.style.border='#EEFFEE solid 1px;'\" onclick=\"addFace(this.id)\" /> ";
            }
        }
        LayerFace.innerHTML = str;
		document.body.appendChild(LayerFace);
		document.getElementById("chatContent").focus();
	}
	else {
		document.body.removeChild(LayerFace);
		document.getElementById("chatContent").focus();
	}
}
function addFace(id) {
	var text = "[em" + id + "]";
	document.getElementById("chatContent").value += text;
	document.getElementById("chatContent").focus();
	document.body.removeChild(document.getElementById('LayerFace'));
}
function setSuggest(str) {
	document.getElementById("suggest").innerHTML = str;
}
function changeDomain(obj){
	if(obj.value == 'public')
	{
		document.getElementById('public_content').style.display='';
		document.getElementById('protected_content').style.display='none';
		document.getElementById('private_content').style.display='none';
	}
	if(obj.value == 'protected')
	{
		document.getElementById('public_content').style.display='none';
		document.getElementById('protected_content').style.display='';
		document.getElementById('private_content').style.display='none';
	}
	if(obj.value == 'private')
	{
		document.getElementById('public_content').style.display='none';
		document.getElementById('protected_content').style.display='none';
		document.getElementById('private_content').style.display='';
	}
}
function scrollbarMovetoTail()
{ 
	var dis1 = document.getElementById('public_content');
	dis1.scrollTop=dis1.scrollHeight;
	var dis2 = document.getElementById('protected_content');
	dis2.scrollTop=dis2.scrollHeight;
	var dis3 = document.getElementById('private_content');
	dis3.scrollTop=dis3.scrollHeight;
	//window.setTimeout("scrollbarMovetoTail()",80);
}
function g(o){return document.getElementById(o);}
function hoverli(n){
for(var i=1;i<=3;i++){g('tb_'+i).className='aa';g('tbc_0'+i).className='undis';}g('tbc_0'+n).className='list2';g('tb_'+n).className='bb';
}
function fun(){
hoverli(3);
}
function hoverli2(n){
for(var i=1;i<=3;i++){g('js_'+i).className='aa';g('jsc_0'+i).className='undis';}g('jsc_0'+n).className='list2';g('js_'+n).className='bb';
}
function fun2(){
hoverli2(3);
}
function hoverli3(n){
for(var i=1;i<=3;i++){g('be_'+i).className='aa';g('bec_0'+i).className='undis';}g('bec_0'+n).className='list2';g('be_'+n).className='bb';
}
function fun3(){
hoverli2(3);
}
function setTab(m,n){
 var tli=document.getElementById("menu"+m).getElementsByTagName("li");
 var mli=document.getElementById("main"+m).getElementsByTagName("ul");
 for(i=0;i<tli.length;i++){
  tli[i].className=i==n?"hover":"";
  mli[i].style.display=i==n?"":"";
 }
 
if(n==0){
domain='public_content';
document.getElementById('re').style.display='none';
document.getElementById('pub').style.display='';
document.getElementById('pro').style.display='none';
document.getElementById('pri').style.display='none';
//publicContent=""+msgDiv.innerHTML;
}
if(n==1){
domain='protected_content';
document.getElementById('re').style.display='none';
document.getElementById('pub').style.display='none';
document.getElementById('pro').style.display='';
document.getElementById('pri').style.display='none';
//protectedContent=""+msgDiv.innerHTML;
}
if(n==2){
domain='private_content';
document.getElementById('re').style.display='';
document.getElementById('pub').style.display='none';
document.getElementById('pro').style.display='none';
document.getElementById('pri').style.display='';
//privateContent=""+msgDiv.innerHTML;
}
 
}

var publicContent="";
var protectedContent="";
var privateContent="";
