var ws;
//--------------------------------加载连接websocket-----------------------------------------//
function initwebsocket(cti_ws_url) {
    if ('WebSocket' in window) {
        ws = new WebSocket(cti_ws_url);
    } else {
        alert("do not suports  websocket");
    }
    ws.onopen = function(evt) {
        alert("open websocket success");
    }
    ws.onclose = function(evt) {
        alert("websocket close");
    }
    //接收数据
    ws.onmessage = function(evt) {
        var reader = new FileReader();
        reader.onloadend = function(e) {
            console.log(this.result);
            //var msgdiv = document.getElementById("msg");
            //var span = document.createElement("span");
            //span.innerHTML = "receive_msg:"+this.result + "<br />";
            //msgdiv.appendChild(span);
            var obj = eval('(' + this.result + ')');
            if(obj.cmd=="system"&&obj.action=="keepalive"){
                keepalive(obj.seatname,obj.seatnum);
            }
            var uuid = document.getElementById("uuid");
            if(obj.cmd=="control"&&obj.action=="tp_callin"){
                uuid.value=obj.uuid;
            }
        };
        reader.readAsText(event.data);
    }
    ws.onerror = function(evt) {
        alert("websocket error");
    }
}
function sendmessage(protocolStr){
    console.log(protocolStr);
    var msgdiv = document.getElementById("msg");
    var span = document.createElement("span");
    span.innerHTML = "send_msg:"+protocolStr + "<br />";
    msgdiv.appendChild(span);
    ws.send(protocolStr);
}
function testclear(){
    var msgdiv = document.getElementById("msg");
    msgdiv.innerHTML="";
}
//--------------------------------command send-----------------------------------------//
//座席签入
function seatlogin(seatname,seatnum,password) {

    if(seatname=="")
    {
        alert("seatname is  null");
        return;
    }
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"system","action":"seatlogin","seatname":"<seatname>","seatnum":"<seatnum>","password":"<password>","timestamp":"<timestamp>"};
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    protocol.password=password;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//座席签出
function seatlogout(seatname,seatnum) {
    if(seatname=="")
    {
        alert("seatname is  null");
        return;
    }
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"system","action":"seatlogout","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//暂离
function afk(seatname,seatnum) {
    if(seatname=="")
    {
        alert("seatname is  null");
        return;
    }
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"system","action":"afk","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//在线
function online(seatname,seatnum) {
    if(seatname=="")
    {
        alert("seatname is  null");
        return;
    }
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"system","action":"online","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//外呼
function callout(seatname,seatnum,phone) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(phone=="")
    {
        alert("phone is  null");
        return;
    }
    var protocol={"cmd":"control","action":"callout","phone":"<phone>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.phone=phone;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//转接
function transfer(seatname,seatnum,phone,uuid) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(phone=="")
    {
        alert("phone is  null");
        return;
    }
    if(uuid=="")
    {
        alert("uuid1 is  null");
        return;
    }
    var protocol={"cmd":"control","action":"transfer","uuid":"<uuid>","phone":"<phone>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.phone=phone;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    protocol.uuid=uuid;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//转接收回
function transferresume(seatname,seatnum,phone,uuid) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(phone=="")
    {
        alert("phone is  null");
        return;
    }
    if(uuid=="")
    {
        alert("uuid2 is  null");
        return;
    }
    var protocol={"cmd":"control","action":"transferresume","uuid":"<uuid>","phone":"<phone>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.phone=phone;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    protocol.uuid=uuid;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}

//通话保留
function hold(seatname,seatnum,uuid) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(uuid=="")
    {
        alert("uuid3 is  null");
        return;
    }
    var protocol={"cmd":"control","action":"hold","uuid":"<uuid>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.uuid=uuid;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//通话保留收回
function holdresume(seatname,seatnum,uuid) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(uuid=="")
    {
        alert("uuid4 is  null");
        return;
    }
    var protocol={"cmd":"control","action":"holdresume","uuid":"<uuid>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.uuid=uuid;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//接听
function pickup(seatname,seatnum) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"control","action":"pickup","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatnum=seatnum;
    protocol.seatname=seatname;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//挂断
function hangup(seatname,seatnum) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"control","action":"hangup","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatnum=seatnum;
    protocol.seatname=seatname;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}

//通话强拆
function remove(seatname,seatnum,phone) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(phone=="")
    {
        alert("phone is  null");
        return;
    }
    var protocol={"cmd":"control","action":"remove","phone":"<phone>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.phone=phone;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//通话强插
function insert(seatname,seatnum,phone) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(phone=="")
    {
        alert("phone is  null");
        return;
    }
    var protocol={"cmd":"control","action":"insert","phone":"<phone>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.phone=phone;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//监听
function monitor(seatname,seatnum,phone) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(phone=="")
    {
        alert("phone is  null");
        return;
    }
    var protocol={"cmd":"control","action":"monitor","phone":"<phone>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.phone=phone;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//监听转通话
function monitor_to_talk(seatname,seatnum,phone) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(phone=="")
    {
        alert("phone is  null");
        return;
    }
    var protocol={"cmd":"control","action":"monitor_to_talk","phone":"<phone>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.phone=phone;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//监听结束
function monitor_end(seatname,seatnum,phone) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(phone=="")
    {
        alert("phone is  null");
        return;
    }
    var protocol={"cmd":"control","action":"monitor_end","phone":"<phone>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.phone=phone;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//选接
function choosecall(seatname,seatnum,uuid) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(uuid=="")
    {
        alert("uuid5 is  null");
        return;
    }
    var protocol={"cmd":"control","action":"choosecall","uuid":"<uuid>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.uuid=uuid;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//代接
function replacecall(seatname,seatnum,phone) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(phone=="")
    {
        alert("phone is  null");
        return;
    }
    var protocol={"cmd":"control","action":"replacecall","phone":"<phone>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.phone=phone;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//三方通话
function three(seatname,seatnum,phone) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(phone=="")
    {
        alert("phone is  null");
        return;
    }
    var protocol={"cmd":"control","action":"three","phone":"<phone>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.phone=phone;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//接收座席状态
function openseatlist(seatname,seatnum) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"status","action":"openseatlist","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatnum=seatnum;
    protocol.seatname=seatname;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//关闭座席状态
function closeseatlist(seatname,seatnum) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"status","action":"closeseatlist","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatnum=seatnum;
    protocol.seatname=seatname;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//接收排队信息
function openqueues(seatname,seatnum) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"status","action":"openqueues","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatnum=seatnum;
    protocol.seatname=seatname;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//关闭排队信息
function closequeues(seatname,seatnum) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"status","action":"closequeues","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatnum=seatnum;
    protocol.seatname=seatname;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//接收通话信息
function opencalllist(seatname,seatnum) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"status","action":"opencalllist","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatnum=seatnum;
    protocol.seatname=seatname;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//关闭通话信息
function closecalllist(seatname,seatnum) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"status","action":"closecalllist","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatnum=seatnum;
    protocol.seatname=seatname;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//接收中继信息
function openroutelist(seatname,seatnum) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"status","action":"openroutelist","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatnum=seatnum;
    protocol.seatname=seatname;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//关闭中继信息
function closeroutelist(seatname,seatnum) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"status","action":"closeroutelist","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatnum=seatnum;
    protocol.seatname=seatname;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}



//获取座席信息
function seatlist(group) {
    if(group=="")
    {
        alert("group is  null");
        return;
    }
    var protocol={"cmd":"status","action":"seatlist","group":"<group>","timestamp":"<timestamp>"};
    protocol.group=group;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}

//获取排队信息
function queues() {
    var protocol={"cmd":"status","action":"queues","timestamp":"<timestamp>"};
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//获取通话信息
function calllist() {
    var protocol={"cmd":"status","action":"calllist","timestamp":"<timestamp>"};
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//获取中继信息
function routelist() {
    var protocol={"cmd":"status","action":"routelist","timestamp":"<timestamp>"};
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//获取批量外呼任务信息
function batch(paramid) {
    if(paramid=="")
    {
        alert("paramid is  null");
        return;
    }
    var protocol={"cmd":"status","action":"batch","paramid":"<paramid>","timestamp":"<timestamp>"};
    protocol.paramid=paramid;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//开始批量外呼
function batch_start(seatname,seatnum) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"system","action":"batch_start","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatnum=seatnum;
    protocol.seatname=seatname;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//关闭批量外呼
function batch_stop(seatname,seatnum) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"system","action":"batch_stop","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatnum=seatnum;
    protocol.seatname=seatname;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//通话咨询开始
function handoff_ready(seatname,seatnum,uuid) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(uuid=="")
    {
        alert("uuid6 is  null");
        return;
    }
    var protocol={"cmd":"control","action":"handoff_ready","uuid":"<uuid>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.uuid=uuid;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//咨询
function handoff_call(seatname,seatnum,phone,uuid) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(phone=="")
    {
        alert("phone is  null");
        return;
    }
    if(uuid=="")
    {
        alert("uuid7 is  null");
        return;
    }
    var protocol={"cmd":"control","action":"handoff_call","uuid":"<uuid>","phone":"<phone>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.phone=phone;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    protocol.uuid=uuid;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//咨询收回
function handoff_resume(seatname,seatnum,uuid) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(uuid=="")
    {
        alert("uuid8 is  null");
        return;
    }
    var protocol={"cmd":"control","action":"handoff_resume","uuid":"<uuid>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    protocol.uuid=uuid;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//咨询转移
function handoff_transfer(seatname,seatnum,phone,uuid) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(phone=="")
    {
        alert("phone is  null");
        return;
    }
    if(uuid=="")
    {
        alert("uuid9 is  null");
        return;
    }
    var protocol={"cmd":"control","action":"handoff_transfer","uuid":"<uuid>","phone":"<phone>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.phone=phone;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    protocol.uuid=uuid;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//咨询会议
function handoff_three(seatname,seatnum,uuid) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(uuid=="")
    {
        alert("uuid10 is  null");
        return;
    }
    var protocol={"cmd":"control","action":"handoff_three","uuid":"<uuid>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    protocol.uuid=uuid;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//保活
function keepalive(seatname,seatnum) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    var protocol={"cmd":"system","action":"keepalive","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.seatnum=seatnum;
    protocol.seatname=seatname;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
}
//通话启动录音
function record_start(seatname,seatnum,uuid) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(uuid=="")
    {
        alert("uuid11 is  null");
        return;
    }
    var protocol={"cmd":"control","action":"record_start","uuid":"<uuid>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.uuid=uuid;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}
//通话停止录音
function record_stop(seatname,seatnum,uuid) {
    if(seatnum=="")
    {
        alert("seatnum is  null");
        return;
    }
    if(uuid=="")
    {
        alert("uuid12 is  null");
        return;
    }
    var protocol={"cmd":"control","action":"record_stop","uuid":"<uuid>","seatname":"<seatname>","seatnum":"<seatnum>","timestamp":"<timestamp>"};
    protocol.uuid=uuid;
    protocol.seatname=seatname;
    protocol.seatnum=seatnum;
    var protocolStr=JSON.stringify(protocol);
    sendmessage(protocolStr);
    //ws.send(protocolStr);
}

//---------------------------------------------------------------------------------//


