var session;
var id ="0";
var va = false;
var bl = true ;
var s_t = false;
var c_t = false;
var l_a_t = new Date(); 
var s_i;
var s_n; 
var s_m =""; 
var c_m = ""; 
var t_o = false; 
var over;
var welcome = "您好,欢迎访问" ; 
var is_to = false;
var err_num = 0;

function _3Qsoft(){   
    if (s_o == false) {
          turn_off() ; 
          
    } else if (va == false){
            ini();
    } else if (bl == true ) {
          turn_off();
    } else if (t_o==true ){ 
          loginout();
    } else { 
        send();
    }
}

function loginout() {
    setTimeout('parent.window.opener=null;parent.window.close()', 1 );
}

function check_timeout() {
    var t1 = l_a_t.getHours() * 60 + l_a_t.getMinutes() ;
    var t = new Date ;
    var t2 = t.getHours() * 60 + t.getMinutes()  ;
    var t = 0;
if (! isNaN(timer)) {
    t = timer;
} else {
    t = 5;
}

if (t2-t1 > t && !is_to) {
    is_to = true;
    string_format("103","");
}

if (((t2-t1)> (t+3) ) && !t_o )  {
  t_o =true;
  over =  setTimeout(loginout(),60*1000);
  
}

}


function send(action) {
    PageMethods.process_msg(id,session ,c_t,c_m ,HtmlMode($get("TextInput").value) ,  On_send_Success,onFailed);
    c_m = "";
    check_timeout();

}

function ini() {
    if (getCookie("id").length > 0) {id= getCookie("id");}
    PageMethods.process_ini(id,ip,On_ini_Success,onFailed);
}

function On_ini_Success(msg_back) {
if (msg_back=="invalid") {
    va = false;
} else if (msg_back == "blocked") {
    bl = true;
} else if (msg_back == "offline") {
    s_o= false; 
} else {
    
    var s = msg_back.split("{[<;>]}");
    id = s[0];
    session = s[1];
    s_i = s[2];
    s_n = s[3];
    welcome = s[4];
    va = true;
    s_o = true;
    bl = false;
    $get("title").innerHTML =s[5];
    $get("bar").innerHTML =s[6];
    $get("logo").innerHTML =s[7];
    $get("info").innerHTML =s[8];
    delCookie("id");
    delCookie("session");
    SetCookie("id",s[0]);
    SetCookie("session",s[1]);
    string_format("100","");
   }
}

function On_send_Success(msg_back) {
if (msg_back=="invalid") {
   va = false; 
} else if (msg_back == "blocked") {
    bl = true;
} else if (msg_back == "offline") {
    s_o= false;
}  else {
    var s = msg_back.split("{[<$>]}");
    id = s[0];
    session = s[1];
    
    if (s[3].length > 0 ) {
           string_format("102",s[3]); 
    }
    if (s[2].toLowerCase() == "true") { 
       $get("tips").innerHTML = "<img src='images/writing.gif' />客服正在输入中..." ; 
       } else {
       $get("tips").innerHTML ="";
       }
    }
	reset_typing(); 
}

function onFailed(msg) {
    string_format("104","");
    err_num = err_num + 1;
    if (err_num>10) {
    alert("对不起，经多次尝试连接服务器\n依然无法连接服务器,只好关闭对话窗口。\n请检查本地网络或服务器是否可用！\n本窗口即将关闭！");
    window.close();
    }
}

function OnSend() {
    check();
    if ($get("TextInput").value.length > 0) {
    string_format("101",$get("TextInput").value);
    c_m = c_m  + HtmlMode($get("TextInput").value);
    $get("TextInput").value = "" ;
    } else {
        alert("发送信息不能为空！");
    }
}

 var ttt = window.setTimeout("reset_typing()",3000);
 
 function check() {
    c_t = true;
   ttt = window.setTimeout("reset_typing()",3000);
 }
 
 function reset_typing() {
     c_t = false ;
     clearTimeout(ttt);
 }

function OnPress() {
	c_t = true;
	t_o = false;
	l_a_t = new Date ; 
	clearTimeout("over");
}

function document.onkeydown() {
    if ((window.event.ctrlKey)&&(window.event.keyCode==13)) { 
    OnSend(); 
    }

}

function textCounter(obj,maxlength){
	if (obj.value.length>maxlength){
		obj.value=obj.value.substr(0,maxlength);
	}
	OnPress();

}

function SetCookie(name,value)
{
    var Days = 730;
    var exp  = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

function getCookie(name)  
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return "";
}

function delCookie(name)
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();

}

function string_format(code,str) {
    switch (code) {
    case "100" :
        $get("msg").innerHTML = $get("msg").innerHTML + "<font color=\"green\">" + s_n + "说:("  + getTime() +  ")</font>&nbsp;&nbsp;<p class='server_msg'>" + welcome +"</p>" ;
        $get("msg").scrollTop = $get("msg").scrollTop + $get("msg").scrollHeight;
        break;
    case "101" :
        $get("msg").innerHTML = $get("msg").innerHTML + "<font color=\"orange\">我 说:(" + getTime() +  ")</font>&nbsp;&nbsp;<p class='client_msg'>" + HtmlMode(str) +"</p>";
        $get("msg").scrollTop = $get("msg").scrollTop + $get("msg").scrollHeight;
        break;
    case "102":
        $get("msg").innerHTML = $get("msg").innerHTML  + str;
        $get("msg").scrollTop = $get("msg").scrollTop + $get("msg").scrollHeight;
        break;
    
    case "103" :
         $get("msg").innerHTML = $get("msg").innerHTML + "<font color=\"red\">系统信息：(" + getTime() +  ")</font>&nbsp;&nbsp;<p class='info'>对不起，你长时间没有对话，窗口即将关闭！</p>" ;
        $get("msg").scrollTop = $get("msg").scrollTop + $get("msg").scrollHeight;
        break;
    
    case "104" :
        $get("msg").innerHTML = $get("msg").innerHTML + "<font color=\"red\">系统信息：(" + getTime() +  ")</font>&nbsp;&nbsp;<p class='info'>网络故障，无法连接服务器！</p>" ;
        $get("msg").scrollTop = $get("msg").scrollTop + $get("msg").scrollHeight;
        break;

    }
}

function turn_off() {
    $get("words").style.display = "block";
    $get("msg").style.display = "none";
    $get("tips").style.display = "none";
    $get("sendbox").style.display = "none";
}

function getTime(){
  var date=new Date();
  var hour="000"+date.getHours();
  var min="000"+date.getMinutes();
  var sec="000"+date.getSeconds();
  hour=hour.substr(hour.length-2);
  min=min.substr(min.length-2);
  sec=sec.substr(sec.length-2);
  return hour+":"+min+":"+sec;
}

function HtmlMode(msg){
	if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion.match(/MSIE \d\.\d/) == "MSIE 5.0")){
		;
	}else{
		msg=msg.replace(new RegExp('<scr'+'ipt[^>]*?>.*?</scr'+'ipt>','g'), "") ;
		msg=msg.replace(new RegExp('\<\!\-\-.*?\-\-\>','g'), "") ;
	}
	msg=msg.replace('\<\!\-\-', "") ;
	//msg+="</a>";
	msg=msg.replace(/ /g, "&nbsp;&nbsp;") ;
	msg=msg.replace(/(\r\n)/g,"<br />");
	return msg;
}

function msg_save() {
    if ($get("msg_title").value.length == 0) { 
        alert("温馨提示：标题不能为空！");
    } else if ($get("msg_email").value.length == 0) {
        alert("温馨提示：email地址不能为空！");
     } else if (!/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test($get("msg_email").value)) {
        alert("温馨提示：请输入格式正确的 e-mail 地址，谢谢！");
        $get("msg_email").value = "";
        $get("msg_email").focus();
     } else if ($get("msg_words").value.length == 0) {
        alert("温馨提示：留言内容不能为空！");
    } else {
        PageMethods.save_msg(ip,$get("msg_title").value,$get("msg_email").value,$get("msg_name").value,$get("msg_tel").value,HtmlMode($get("msg_words").value),msg_ok,onFailed);
    }
}

function msg_ok(ok) {
    if ( ok == "ok" ) {
        alert("恭喜，留言提交成功！\n点击确定以关闭窗口");
        loginout();
    } else {
        alert("很抱歉，留言未能提交成功！" + ok);
    }

}