// JavaScript Document	
	
	//Cookie: "ServerIP","ServerPass","TotalChannel",
	var m_Port     = 12345;
	var m_Port_Ftp = 12347;
	var m_Port_Web = 80;
	
	var m_TotalCh= 16;
	var m_arrayChState = new Array();//定义一维数组
	var m_QueryFile= "";
	var m_strIP= "",m_strPass= "";
	var m_PubState= 0;
	var m_PlayNum= 0;	//当前播放的文件序数，停止时需要用到
	var m_ListenCh= -1;
	var m_timerID;

	
	function SetCookie(sName, sValue){
   		date = new Date();
   		date.setFullYear(date.getFullYear()+1);
   		document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
	}

	function GetCookie(sName){
   		var aCookie = document.cookie.split("; ");
   		for (var i=0; i < aCookie.length; i++){
      		var aCrumb = aCookie[i].split("=");
      		if (sName == aCrumb[0]) return unescape(aCrumb[1]);
   		}
   		return null;
	} 
	
	function CloseAppPage(){
		javascript:window.close();
	}
	
	function GetLocalIPAddress()
	{
		/*var obj = null;
		var rslt = "";
		try	{
			obj = new ActiveXObject("rcbdyctl.Setting");
			rslt = obj.GetIPAddress;
			obj = null;
		}
		catch(e)		{
			//异常发生
		}
		return rslt;*/
		return window.location.host;
	}

	//给String类增加3个去除空格方法
	String.prototype.Trim = function()	{
	return this.replace(/(^s*)|(s*$)/g, "");
	}
	String.prototype.LTrim = function(){
	return this.replace(/(^s*)/g, "");
	}
	String.prototype.Rtrim = function(){
	return this.replace(/(s*$)/g, "");
	} 

	
	
	function DisconnectTerminal(){
		if(m_strIP == "" || m_strIP== null)
			return;
		GxSocketOcx.GX_Disconnect(m_strIP);
	}
	
	
	/****************************************************/
	/************index.html JavaScript code**************/
	/****************************************************/
	function ConnectTerminal_login(){
			//获取访问服务器的IP,并写到cookie"ServerIP"里保存			
			m_PubState	= 0;
			m_strIP		= document.getElementById("txtIP").value;
			m_strPass	= document.getElementById("txtPass").value;
			m_Port	 	= document.getElementById("txtPort").value;
			m_Port_Ftp	= document.getElementById("txtPort_Ftp").value;
		
			if(m_strIP == ""){
				alert("Please input standalone IP address.");
				return;
			}
			if(m_strPass == ""){
				alert("Please input standalone password.");
				return;
			}
            
			var nRet = -1;
			try{
				nRet = GxSocketOcx.GX_Connect("AAAA", m_strPass, m_strIP,m_Port);
			}
			catch(e){}
		
            if (nRet < 0){
                alert("Connect failed. Please press F5 to refresh the page.");
				return;
			}
            else{		
				SetCookie("ServerIP", m_strIP);
				SetCookie("ServerPass", m_strPass);
				SetCookie("ServerPort", m_Port);
				SetCookie("ServerPort_Ftp", m_Port_Ftp);
			
				GxSocketOcx.GX_Disconnect(m_strIP);
				window.location.href="RecStatus.html"; 
				//window.open("mainfom.html");//alert("连接成功!");				
			}
	}
	
	function SetTxtIP(){
		m_strIP= GetLocalIPAddress();
		document.getElementById("txtIP").value= m_strIP;		
	}
	
	function EnterKeyPressNext(){
		if(window.event.keyCode  == 13){ 
			document.getElementById("txtPass").focus();
		}
	}
	
	function EnterKeyPress(){
		if(window.event.keyCode  == 13){ 
			ConnectTerminal_login();
		}
	}
	
	
	
	
	/****************************************************/
	/**********RecStatus.html JavaScript code************/
	/****************************************************/
	function ConnectTerminal_Status(){
		m_PubState= 1;
		m_strIP= GetCookie("ServerIP");
		m_strPass= GetCookie("ServerPass");
		m_Port		= GetCookie("ServerPort");
		m_Port_Ftp	= GetCookie("ServerPort_Ftp");
        var nRet = GxSocketOcx.GX_Connect("AAAA", m_strPass, m_strIP, m_Port);
        if (nRet < 0)
        	alert("Connect failed. Please press F5 to refresh the page.");
        else{		
			GetHardwareInfo();
			GetChnlStatus_2();
			window.setTimeout("RunCardWork()",2000);				//启动定时函数执行CardWork()
			window.setInterval("DisplayRecTimeLen()",1000);			//显示录音时长
			//alert("连接成功!");							
		}
	}
	
	function GetHardwareInfo() {
		var arrayVariants = new VBArray(GxSocketOcx.Gx_GetHardwareInfo0(m_strIP));
        var arrayBSTR = arrayVariants.toArray(); 	//用VBArray将安全数组转为JS数组
        m_TotalCh= arrayBSTR[2];					//获取总通道
		SetCookie("TotalChannel", m_TotalCh);
	}
	
	/*function GetChnlStatus() {		
		var strTemp= "";
        for (var i=0; i < m_TotalCh; i++){		
			m_arrayChState[i]=new Array(); //将一围数组的每一个子元素又定义为数组  
			
			var arrayVariants = new VBArray(GxSocketOcx.GX_GetChnlStatus2(m_strIP, i));
			var arrayBSTR = arrayVariants.toArray();	//用VBArray将安全数组转为JS数组
			
			for(var j=0; j < 14; j++) {
				m_arrayChState[i][j]  = arrayBSTR[j];		//读取通道状态到二围数组
				strTemp= strTemp+ arrayBSTR[j] + "-+";
			}
		}
		SetCookie("ChannelState", strTemp);	//写通道状态到cookie里以便读出来
	}*/
		
	function GetChnlStatus_2() {		
		var strTemp= "";
		var objTable = document.getElementById("tbl_State");					//获得表格对象table			
		var newRow = "";
		var newTD  = "";
		
        for (var i=0; i < m_TotalCh; i++){		
			var arrayVariants = new VBArray(GxSocketOcx.GX_GetChnlStatus2(m_strIP, i));
			var arrayBSTR = arrayVariants.toArray();							//用VBArray将安全数组转为JS数组	
			newRow = objTable.insertRow(); 
		
			//1通道号 2本线号码 3模式 4状态 5方向 6拨入号码 7拨出号码 8开始时间 9通话时长
			//0:local_code 1:name 2:start_record_mode 3:cStatus 4:cHook 5:cIDIsDtmf 6:acDtmf 
			//7:sDtmfLen 8:cCIDFlag 9:cCallDirection 10:acCID 11:HookOffTime 12:lRingTime 13:ucRingCnt
			
			newTD = newRow.insertCell();
			newTD.innerHTML= '<input type="button" value="Monitor" id="tdListenBtn'+i+'" onclick="ListenTerminal('+i+')">';
			newTD = newRow.insertCell().innerHTML = i+1;						//通道号			  
			newTD = newRow.insertCell().innerHTML = arrayBSTR[0];				//本线号码			
			if(arrayBSTR[2]== 0) strTemp= "Audio";								//模式 0:voice control; 1:voltage control; 2:key control
			else if(arrayBSTR[2]== 1) strTemp= "Voltage";		
			else if(arrayBSTR[2]== 2) strTemp= "Keying";				
			//else strTemp= "压控";
			newTD = newRow.insertCell().innerHTML = strTemp;
			
			if(arrayBSTR[3]== 0) strTemp= "Idle";								//状态
			else if(arrayBSTR[3]== 1) strTemp= "Disconnect";
			else if(arrayBSTR[3]== 2) strTemp= "Ringing...";
			else if(arrayBSTR[3]== 3) strTemp= "Recording...";	//摘机
			else if(arrayBSTR[3]== 4) strTemp= "Recording...";
			else if(arrayBSTR[3]== 5) strTemp= "Idle";	//挂机
			newTD = newRow.insertCell().innerHTML =strTemp;			
							
			var strFskNo= "",strDtmfNo= "";
			if(arrayBSTR[8]== "1")	{											//判断是否有拨出
				strFskNo= arrayBSTR[10];									
				if(arrayBSTR[7] > 0)											//判断DTMF码长度.length
					strDtmfNo= arrayBSTR[6];				   
			}
			else {
				strFskNo= ""
				if(arrayBSTR[7] > 0)	//if(arrayBSTR[7] != "0") 
					strDtmfNo= arrayBSTR[6];					
			}		
			
			if(strFskNo != "") strTemp= "In";							
			else {
				if(strDtmfNo!= "") strTemp= "Out";	
				else strTemp= "";	
			}
			if(strFskNo.length > 20) strFskNo= strFskNo.substr(0,20);
			if(strDtmfNo.length > 20) strDtmfNo= strDtmfNo.substr(0,20);		
		
			newTD = newRow.insertCell().innerHTML = strTemp;					//方向 1: call in; 0: call out
			newTD = newRow.insertCell().innerHTML = strFskNo;					//拨入号码
			newTD = newRow.insertCell().innerHTML = strDtmfNo;					//拨出号码
			
			if(strTemp== "" || strTemp== 0 ) 
				strTemp= "";	
			else{
				strTemp= arrayBSTR[11];
				strTemp= SecondsStrToDate(strTemp);
			}
			newTD = newRow.insertCell().innerHTML = strTemp;					//开始时间
			newTD = newRow.insertCell().innerHTML = "";							//通话时长
		}
	}
	
	/**实时监听**/	
	function ListenTerminal(nChannel){		
		var nRet= -1;
		if(m_ListenCh != -1){
			GxSocketOcx.GX_StopMonitor(m_strIP,m_ListenCh);						//停止之前的通道的监听
			if(m_ListenCh == nChannel) {
				document.getElementById("tdListenBtn"+nChannel).value= "Monitor";
				m_ListenCh= -1;
				return;
			}
			else{
				document.getElementById("tdListenBtn"+m_ListenCh).value= "Monitor";
				m_ListenCh= -1;
			}
		}
		//document.getElementById getElementsByTagName document.all("")
		var btntext = document.getElementById("tdListenBtn"+nChannel).value;	
		if(btntext == "Monitor"){
			nRet= GxSocketOcx.GX_StartMonitor(m_strIP,nChannel);				//开启当前通道监听功能
			//if(nRet > 0){				
				document.getElementById("tdListenBtn"+nChannel).value= " Stop  ";
				m_ListenCh= nChannel;
			//}
		}
	}
	
	/***定时显示录音时长***/
	function DisplayRecTimeLen(){
		var timerTdText= "";
		for(var i=1; i<=m_TotalCh; i++){
			timerTdText= document.all.tbl_State.rows[i].cells[9].innerText;
			if(timerTdText != ""){
				var dt = "2011-11-11 "+ timerTdText;
				var ss = 1* 1000;
				var r = new Date(Date.UTC.apply(null,dt.split(/\D/))+ ss).toGMTString();
				document.all.tbl_State.rows[i].cells[9].innerText= r.substr(17,8);
			}
		}		
	}

	function SecondsStrToDate(str){   
		var startyear = 1970;    
		var startmonth = 1;    
		var startday = 1;    
		var d, s;    
		var sep   =   ":";    
		d = new Date();    
		d.setFullYear(startyear,startmonth,startday);    
		d.setTime(0);    
		d.setSeconds(str);    //setMilliseconds 毫秒
		s = d.getYear() + "-" + (d.getMonth()+1) + "-" + d.getDate() + " "+ d.getHours()+ ":" + d.getMinutes()+":" +d.getSeconds();    
        return s;//d.toLocaleString();   
    } 

	
	

		

	
		
		
	/****************************************************/
	/*********QueryData.html JavaScript code*************/
	/****************************************************/
	function ConnectTerminal_Query(){
		m_PubState= 2;
		m_strIP= GetCookie("ServerIP");
		m_strPass= GetCookie("ServerPass");
		m_Port		= GetCookie("ServerPort");
		m_Port_Ftp	= GetCookie("ServerPort_Ftp");
		var nRet = GxSocketOcx.GX_Connect("AAAA", m_strPass, m_strIP, m_Port);
        if (nRet < 0)
            alert("Connect failed. Please press F5 to refresh the page.");
        else{		
			//alert("连接成功!");				
			var nRet= GxSocketOcx.GX_SetSavePath("C:\\recorder");
			if(nRet < 0){
				alert('Set path failed "C:\recorder",  Please press F5 to refresh the page');				
				}
			RunCardWork();	//启动定时函数执行CardWork()
			m_TotalCh= parseInt(GetCookie("TotalChannel"),10);
			for(var i=1; i<=m_TotalCh; i++){
				document.getElementById("txtCh").options.add(new Option(i,i)); 
			}
			document.getElementById("txtBeginTime").value= get_time(0);
			document.getElementById("txtEndTime").value= get_time(1);

		}
	}
	
	//----------条件判断-----------
	var txtTerminal,txtTerminalIP,txtCh,txtFX,txtPhonNo,txtLocalNo,txtLength1,txtLength2,txtUserName;
	var m_nRow= 0;
	var m_strDownloadSavePath= "";
	function btnSearch(){
		m_QueryFile= "C:\\recorder\\"+ m_strIP +"_query.txt";
		
		var fso = new ActiveXObject("Scripting.FileSystemObject");
		if(fso.FileExists(m_QueryFile))
			fso.DeleteFile(m_QueryFile, true); 						//查询之前如果此查询文件已存在先删除
		//alert("DeleteFile m_QueryFile");
		
		var iCh_s= 0,iCh_e= 15;
		var iIP,sDate_s,sDate_e,sTime_s,sTime_e;

		var ch = document.getElementById("txtCh").value;
		if(ch == "ALL"){
			iCh_s= 0;
			iCh_e= m_TotalCh- 1; 
		}
		else{
		    iCh_s= parseInt(ch,10)-1;
			iCh_e= parseInt(ch,10)-1;
		}
		var temp;
		temp= document.getElementById("txtBeginTime").value;	//开始日期和时间
		sDate_s= temp.substr(0,temp.lastIndexOf(' '));
		if(sDate_s == "")
			sDate_s= get_time(3);
		sTime_s= temp.substr(temp.lastIndexOf(' ')+1);
		if(sTime_s == "")
			sTime_s= "00:00:01";
			
		temp= document.getElementById("txtEndTime").value;		//结束日期和时间
		sDate_e= temp.substr(0,temp.lastIndexOf(' '));
		if(sDate_e == "")
			sDate_e= get_time(3);
		sTime_e= temp.substr(temp.lastIndexOf(' ')+1);
		if(sTime_e == "")
			sTime_e= "23:59:59";
		//alert(sDate_s+sTime_s+"v:"+sDate_e+sTime_e);
		
		var nRet = GxSocketOcx.GX_QueryFile(m_strIP, sDate_s, sTime_s, sDate_e, sTime_e, iCh_s, iCh_e,"query.txt");
        if (nRet < 0){
			alert("Search failed.");
		} 
		else{		
		    DisplayDivJD("1");			
			
			//txtTerminal= document.getElementById("txtTerminal").value;
			txtTerminalIP= document.getElementById("txtTerminalIP").value;
			txtCh= document.getElementById("txtCh").value;
			txtFX= document.getElementById("txtFX").value;
			/*var r= document.getElementsByName("txtCh"); 
    		for(var i=0;i<r.length;i++){
         		if(r[i].selected){
         			txtFX= r[i].value;
       			}
    		}*/
			txtPhonNo= document.getElementById("txtPhonNo").value;
			txtLocalNo= document.getElementById("txtLocalNo").value;
			txtLength1= document.getElementById("txtLength1").value;
			txtLength2= document.getElementById("txtLength2").value;
			//txtUserName= document.getElementById("txtUserName").value;

			ClearAllRow();
			//window.setTimeout("ReadFile()",1000);
		}
			
	}	
	
	function ReadFile(){		
　　	var fso = new ActiveXObject("Scripting.FileSystemObject");
		var ForReading= 1;
	　　var f = fso.OpenTextFile(m_QueryFile,ForReading,true);
	　　var strtext= "",strtext2= "";
		if(f.AtEndOfLine){
	    	alert("No find data!");
        }
  		else{
			var nLine= 1;
			m_nRow= 0;
        	while (!f.AtEndOfStream){	//while(!txtfile.AtEndOfLine) http://msdn.microsoft.com/zh-cn/library/314cz14s(v=vs.85).aspx
                 strtext = f.ReadLine();
				 TableAddRow(nLine,strtext);
				 nLine++;
            }
       	}	
	　　f.Close();　　	
　　}

	function TableAddRow(nRow,s){ 
		var nFlag = new Array();
		var sTerminal="",sCh="",sLocalNo="",sFX="",sDT="",sDtmf="",sDtmf_back="",sCID="",sRecLen="",sIP="",sChName="",sFilePath="";
		var stemp;
		var nLen= s.length;
		var j= 0;
		for(var i= 0; i< nLen; i++)
		{
			if(s.charAt(i)== "/" || s.charAt(i)== "-" || s.charAt(i)== "."){
				nFlag[j]= i; //alert("nFlag[j]:" + nFlag[j]);	
				j++;
			}
		}

		// 0      1      2        3 4 5        6      7   8 9    10  11    12  14       22
		//[/sdcard/record/20120629/1/g-20120629-144647-001-O-0011-002-92000-.729---------]
		
		//[/sdcard/record/20111106/1/g-213913-O-0012-6002-10000-.729]
		//[/sdcard/record/20111106/1/g-235600-I-0007-6002-13632929931-23z99.729]		
		
		//sFilePath= s.substr(1,s.lastIndexOf(".729")+ 3);							//获取文件路径
		//sFilePath = sFilePath.replace(/\//g, "\\");
		//sTerminal= m_strBranchName;		
		sCh= parseInt(s.substr(nFlag[3]+1,nFlag[4]-nFlag[3]-1),10)+1;
		sLocalNo= s.substr(nFlag[10]+1,nFlag[11]- nFlag[10]-1);
		sFX= s.substr(nFlag[8]+1,1);
		sDT= s.substr(nFlag[2]+1,4)+ "-"+ s.substr(nFlag[2]+5,2)+ "-"+ s.substr(nFlag[2]+7,2);
		sDT+= " "+ s.substr(nFlag[6]+1,2)+ ":"+ s.substr(nFlag[6]+3,2)+ ":"+ s.substr(nFlag[6]+5,2);
		if(sFX=="I"){		
			sCID= s.substr(nFlag[11]+1,nFlag[12]-nFlag[11]-1);
			sCID= sCID.Trim();
			if(sCID== "N")
				sCID= "";
			sDtmf= s.substr(nFlag[12]+1,nFlag[13]-nFlag[12]-1);
			sDtmf= sDtmf.Trim();
			if(sDtmf== "N")
				sDtmf= "";
			else{
				//sDtmf.replace(/x/g,"*");
				//sDtmf.replace(/z/g,"#");				
				var strDtmfTemp= "";
				for(var i=0;i<sDtmf.length;i++)
				{
					if(sDtmf.substr(i,1) == 'x') strDtmfTemp+= '*';
					else if(sDtmf.substr(i,1) == 'z') strDtmfTemp+= '#';
					else strDtmfTemp+= sDtmf.substr(i,1);
				}
				sDtmf= strDtmfTemp;
			}
		}
		else{
			sCID= "";
			sDtmf= s.substr(nFlag[11]+1,nFlag[12]-nFlag[11]-1);
			sDtmf= sDtmf.Trim();
			if(sDtmf== "N")
				sDtmf= "";
			sDtmf_back= sDtmf;
			sDtmf+= s.substr(nFlag[12]+1,nFlag[13]-nFlag[12]-1);
			var strDtmfTemp= "";
			for(var i=0;i<sDtmf.length;i++)
			{
				if(sDtmf.substr(i,1) == 'x') strDtmfTemp+= '*';
				else if(sDtmf.substr(i,1) == 'z') strDtmfTemp+= '#';
				else strDtmfTemp+= sDtmf.substr(i,1);
			}
			sDtmf= strDtmfTemp;				
		}
		if(sDtmf.length > 15) sDtmf= sDtmf.substr(0,15);
		if(sCID.length > 15) sCID= sCID.substr(0,15);
		
		sRecLen= s.substr(nFlag[9]+1,4);		

				
		sIP= m_strIP;	
		if(sFX=="I") 
			stemp= "In";
		else 
			stemp= "Out";
			
		//-------------------------条件判断-------------------------
		//if((txtTerminal != "") && (txtTerminal != sTerminal)) return;		
		if((txtTerminalIP != "") && (txtTerminalIP != sIP)) return;		
		
		if((txtCh != "ALL") && (parseInt(txtCh,10) != sCh)) return;
		if(txtFX != "ALL"){
			if(txtFX != stemp) return;
		}
		if(txtPhonNo != ""){
			//if((txtPhonNo != sCID)&&(txtPhonNo != sDtmf_back)) return;
			if(sFX == "I"){
				if(txtPhonNo.length > sCID.length) return;
				if(txtPhonNo != sCID.substr(0,txtPhonNo.length)) return;
			}
			else{
				if(txtPhonNo.length > sDtmf_back.length) return;
				if(txtPhonNo != sDtmf_back.substr(0,txtPhonNo.length)) return;
			}			
		}		
		if(txtLocalNo != ""){
			if(txtLocalNo.length > sLocalNo.length) return;
			if(txtLocalNo != sLocalNo.substr(0,txtLocalNo.length)) return;
		}		
		if((txtLength1 != "") && (txtLength2 != "")){
			var iLen1= parseInt(txtLength1,10);
			var iLen2= parseInt(txtLength2,10);
			var len= parseInt(sRecLen,10);
			if(len >= iLen1 && len <= iLen2){}
			else return;
		}
		//if((txtUserName != "") && (txtUserName != sChName)) return;
		sFilePath= s.substr(1,nLen-2);							//获取文件路径
		
		
		
		var objTable = document.getElementById("QueryTable");	//获得表格对象table			
		var newRow;
		newRow = objTable.insertRow(); 
		//newRow = objTable.insertRow(1); //在第一行后插入一行
		var newTD;
		newTD= newRow.insertCell();
		newTD.innerHTML = ++m_nRow;	//nRow;
		newTD= newRow.insertCell();
		newTD.innerHTML = '<input type="button" value="Play" id="tdPlayBtn'+m_nRow+'" onclick="PlayTerminalFile2('+m_nRow+')">';
		newTD= newRow.insertCell();
		newTD.innerHTML = '<input type="button" value="Download" id="tdDownloadBtn'+m_nRow+'" onclick="DownloadTerminalFile('+m_nRow+')">';
		//newTD= newRow.insertCell();		
		//newTD.innerHTML = sTerminal;
		newTD= newRow.insertCell();
		newTD.innerHTML = sCh;
		newTD= newRow.insertCell();
		newTD.innerHTML = sLocalNo;		
		newTD= newRow.insertCell();
		newTD.innerHTML = stemp;
		newTD= newRow.insertCell();
		newTD.innerHTML = sDT;	
		newTD= newRow.insertCell();
		newTD.innerHTML = sDtmf;
		newTD= newRow.insertCell();
		newTD.innerHTML = sCID;	
		newTD= newRow.insertCell();
		sRecLen= Get_SecToTimeStr(sRecLen);
		newTD.innerHTML = sRecLen;	//get_RecordLength(sRecLen);
		newTD= newRow.insertCell();
		newTD.innerHTML = sIP;
		//newTD= newRow.insertCell();
		//newTD.innerHTML = sChName;
		
		newTD= newRow.insertCell();		//保存linux路径
		newTD.innerHTML = sFilePath;	
		newTD.style.display = "none";	//使表格TD字段不显示
	}
	
	/**清除表格的所有行**/
	function ClearAllRow(){	
		var objTable = document.getElementById("QueryTable");
		var len = objTable.rows.length;
		for(var i=1; i< len; i++)
			objTable.deleteRow(1);
	}
	
	/**获取当前系统时间**/
	function get_time(curDateTime){
		var now= new Date();   
		var _year = now.getYear();   
		var _month = ( 10 > (now.getMonth()+1) ) ? '0' + (now.getMonth()+1) : now.getMonth()+1;   
		var _day = ( 10 > now.getDate() ) ? '0' + now.getDate() : now.getDate();   
		var _hour = ( 10 > now.getHours() ) ? '0' + now.getHours() : now.getHours();   
		var _minute = ( 10 > now.getMinutes() ) ? '0' + now.getMinutes() : now.getMinutes();   
		var _second = ( 10 > now.getSeconds() ) ? '0' + now.getSeconds() : now.getSeconds();   
		var _week= '';
		switch (now.getDay()) {
			case 0:_week="SunDay";break;
			case 1:_week="Monday";break;
			case 2:_week="Tuesday";break;
			case 3:_week="Wednesday";break;
			case 4:_week="Thursday";break;
			case 5:_week="Friday";break;
			case 6:_week="Saturday";break;
		}
		//return _year + '-' + _month + '-' + _day + ' ' + _hour + ':' + _minute + ':' + _second + ' '+ _week;  
		if(curDateTime == 0)
			return(_year + '-' + _month + '-' + _day+ " 00:00:01");
		else if(curDateTime == 1)
			return(_year + '-' + _month + '-' + _day+ " 23:59:59");
		else if(curDateTime == 2)
			return(_year + '-' + _month + '-' + _day + ' ' + _hour + ':' + _minute + ':' + _second);
		else if(curDateTime == 3)
			return(_year + '-' + _month + '-' + _day);
	}; 
	
	
	/*获取时间“hh:mm:ss”秒数转时间字符串“hh:mm:ss”*/
	function Get_SecToTimeStr(SecLen){
		var i_hh,i_mm,i_ss;
		var s_hh="",s_mm="",s_ss="";
		var iTotalSec= SecLen;
		
		i_hh = parseInt((iTotalSec / 3600),10);
		s_hh= i_hh;
		if(i_hh < 10){	
			s_hh= "0"+ s_hh;
		}			
		
  		i_mm = parseInt(((iTotalSec %= 3600) / 60),10);
		s_mm= i_mm;i_hh;
		if(i_mm < 10){	
			s_mm= "0"+ s_mm;
		}
			
  		i_ss = parseInt((iTotalSec - s_hh * 3600 - s_mm * 60),10);
		s_ss= i_ss;i_hh;
		if(i_ss < 10){	
			s_ss= "0"+ s_ss;
		}
		
		return(s_hh+":"+s_mm+":"+s_ss);
	}
	
	/**终端文件实时播放**/	
	function PlayTerminalFile(nTableRow){

		GxSocketOcx.GX_StopPlaySound2(m_strIP);
		var btntext = document.getElementById("tdPlayBtn"+nTableRow).value;			//document.getElementById getElementsByTagName document.all("")
		if(btntext == " Stop  "){
			 document.getElementById("tdPlayBtn"+nTableRow).value= "Play";
			 clearTimeout(m_timerID);
			 m_PlayNum= 0;
		}
		else
		{			
			var sFilePath = document.all.QueryTable.rows[nTableRow].cells[11].innerText;	
			var len= sFilePath.length;
			var strFromPath= "";
			for(var i=0; i<len; i++){
				if(sFilePath.charAt(i) == "/")
					strFromPath += "\\";
				else
			  		strFromPath += sFilePath.charAt(i);
			}
			var nRet= GxSocketOcx.GX_StartPlaySound2(m_strIP,strFromPath,0,0xffffffff);
			/*var arrayVariants = new VBArray( GxSocketOcx.GX_StartPlaySound2(m_strIP,strFromPath,0,0xffff));
			var arrayBSTR = arrayVariants.toArray();							//用VBArray将安全数组转为JS数组	
			var nRet= parseInt(arrayBSTR);*/
			if(nRet > 0){
				if(m_PlayNum > 0){
					clearTimeout(m_timerID);
					document.getElementById("tdPlayBtn"+m_PlayNum).value= "Play";
				}				
				document.getElementById("tdPlayBtn"+nTableRow).value= " Stop  ";
				m_PlayNum= nTableRow;
				m_timerID= setTimeout("SetBtnCaption()",nRet+1000);
			}			
		}		
		//window.open("PlayForm.html","","width=300,height=200");
		//window.open ('PlayForm.html','newwindow','height=200,width=450,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
	}
	
	/**终端文件实时播放，在另一个页来播放**/	
	function PlayTerminalFile2(nTableRow){
		SetCookie("PlayFileNo", nTableRow);
		SetCookie("TotalRecord", m_nRow);
		//var atop=(screen.availHeight - 200)/2; //窗口顶部位置
		//var aleft=(screen.availWidth - 540)/2;	 //窗口放中央		
		//window.open ('PlayForm.html','newwindow','height=200,width=540,top=356,left=370,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
		MM_openBrWindow('PlayForm.html','newwindow',560,200);
	}
	
	function MM_openBrWindow( url, winName, width, height)   
	{  
		var xposition=0; 
		var yposition=0;  
		if ((parseInt(navigator.appVersion) >= 4 ))  
		{  
			xposition = (screen.width - width) / 2;  
			yposition = (screen.height - height) / 2 -100;  
		}  
		theproperty= "width=" + width + ","   
		+ "height=" + height + ","   
		+ "location=0,"   
		+ "menubar=0,"  
		+ "resizable=1,"  
		+ "scrollbars=0,"  
		+ "status=0,"   
		+ "titlebar=0,"  
		+ "toolbar=0,"  
		+ "hotkeys=0,"  
		+ "screenx=" + xposition + "," //仅适用于Netscape  
		+ "screeny=" + yposition + "," //仅适用于Netscape  
		+ "left=" + xposition + "," //IE  
		+ "top=" + yposition; //IE   
		window.open( url,winName,theproperty );  
	} 


	function SetBtnCaption(){
		//终端文件播放完成
		if(m_PlayNum > 0)
			document.getElementById("tdPlayBtn"+m_PlayNum).value= "Play";
	}
	
	
	/**下载文件**/	
	function DownloadTerminalFile(nTableRow){
			var sFilePath = document.all.QueryTable.rows[nTableRow].cells[11].innerText;			
						
			var strSavePath= sFilePath.substr(sFilePath.lastIndexOf("/")+1);
			strSavePath= "c:\\" + strSavePath;

			var len= sFilePath.length;
			var strFromPath= "";
			for(var i=0; i<len; i++){
				if(sFilePath.charAt(i) == "/")
					strFromPath += "\\";
				else
			  		strFromPath += sFilePath.charAt(i);
			}
			
			m_strDownloadSavePath = "";
			//var nRet = GxSocketOcx.GX_DownloadFile2(m_strIP,strFromPath,strSavePath);
			//var nRet = GxSocketOcx.GX_DownloadFileFast(m_strIP,strFromPath,"2M",0,strSavePath);
			var nRet = GxSocketOcx.GX_DownloadFileFast(m_strIP, m_Port_Ftp, strFromPath,"2M",0,strSavePath);
        	if (nRet < 0)
        		alert("Download file failed.");			
        	else
            	alert('Start download file...');
			m_strDownloadSavePath = strSavePath;
	}
	
	
	
	/**************************************
	* 打开各种对话框
	* 调用方式：<input type="button" value="Click to show dialog" onclick="document.execCommand('showDialog')">
	* function showDialog(){
	* MSComDlg_CommonDialog(0|1|2|3|4);
	***************************************/
	function GetLocalPath(nOpenValue){
		if(!ActiveXObject)
			return;
		var obj=new ActiveXObject('MSComDlg.CommonDialog');
		var filename=false;
		try{
			switch(nOpenValue){
			case 2:
				obj.ShowColor();
				break;
			case 3:
				obj.ShowFont();
				break;
			case 4:
				obj.ShowHelp();
				break;
			default:
				obj.Filter='All files(*.*)';
				obj.FilterIndex = 1;
				obj.MaxFileSize = 255;
				if(nOpenValue == 0)
					obj.ShowSave();
				else
					obj.ShowOpen();
			}
			filename = obj.filename;
		}catch(e){alert(e.message);}
		return filename;
	} 

	//输入网址路径
	function smt(){   
  		path = prompt("Please input path","");   
  		if(path!=null&&path!=""){   
      		document.form1.filepath.value=path;   
      		document.form1.submit();   
  		}   
  	} 





	
	
	
	
	/****************************************************/
	/**********SystemSet.html JavaScript code************/
	/****************************************************/
	function ConnectTerminal_SystemSet(){
		m_PubState= 3;
		m_strIP= GetCookie("ServerIP");
		m_strPass= GetCookie("ServerPass");
		m_Port		= GetCookie("ServerPort");
		m_Port_Ftp	= GetCookie("ServerPort_Ftp");
		var nRet = GxSocketOcx.GX_Connect("AAAA", m_strPass, m_strIP,m_Port);
        if (nRet < 0)
            alert("Connect failed. Please press F5 to refresh the page.");
        else{		
			//alert("连接成功!");				
			var nRet= GxSocketOcx.GX_SetSavePath("C:\\recorder");
			if(nRet < 0){
				alert('Set path "C:\recorder" failed, Please press F5 to refresh the page.');
				return;
			}
			RunCardWork();	//启动定时函数执行CardWork()
			//m_TotalCh= parseInt(GetCookie("TotalChannel"));
			document.getElementById("txtIP").value = m_strIP;
			document.getElementById("txtMask").value = "255.255.255.0";
			document.getElementById("txtGateway").value = m_strIP.substr(0,m_strIP.lastIndexOf("."))+ ".1";
			//document.getElementById("txtDNS").value = "202.96.134.133";
			//document.getElementById("txtMAC").value = "08:90:90:90:90:90";
		}
	}
	
	
	function UpdateIP(){
		var strIP= document.getElementById("txtIP").value;		//document.getElementById getElementsByTagName document.all("")
		strIP = strIP.Trim();
		if(strIP == ""){alert("Please input IP address!");return;	}
		if(strIP.length < 7 || CheckIP(strIP) == -1){alert("IP address is wrong!");return;}
		
		var strMask= document.getElementById("txtMask").value;
		strMask = strMask.Trim();
		if(strMask == ""){alert("Please input mask!");return;	}
		if(strMask.length < 7 || CheckIP(strMask) == -1){alert("Mask is wrong!");return;}
		
		var strGateway= document.getElementById("txtGateway").value;
		strGateway = strGateway.Trim();
		if(strGateway == ""){alert("Please input getway!");return;}
		if(strGateway.length < 7 || CheckIP(strGateway) == -1){alert("Getway is wrong!");return;}
		
		var strDNS= "";//document.getElementById("txtDNS").value;
		//strDNS = strDNS.Trim();
		//if(strDNS == ""){alert("Please input DNS!");return;}
		//if(strDNS.length < 7 || CheckIP(strDNS) == -1){alert("DNS is wrong!!");return;}
		
		strMAC = "MAC=08:90:90:90:90:";
		var nLastIP = parseInt((strIP.substr(m_strIP.lastIndexOf(".")+ 1)),10);
		var n_high = Math.round(nLastIP / 16);
		switch (n_high) {
			case 10: n_high="A";break;
			case 11: n_high="B";break;
			case 12: n_high="C";break;
			case 13: n_high="D";break;
			case 14: n_high="E";break;
			case 15: n_high="F";break;
		}
		var n_low = Math.round(nLastIP %= 16);
		switch (n_low) {
			case 10: n_low="A";break;
			case 11: n_low="B";break;
			case 12: n_low="C";break;
			case 13: n_low="D";break;
			case 14: n_low="E";break;
			case 15: n_low="F";break;
		}
		strMAC += n_high + ""+ n_low;
		var strText = "IP="+ strIP+ "\nMask="+ strMask+ "\nGateway="+ strGateway+ "\nDNS="+ strDNS+ "\n"+ strMAC+ "\n";
		WriteIpFile(strText);
	}
	
	//校验输入的IP地址等是否合法
	function CheckIP(sIP){
		var i =0;
		var pos = sIP.indexOf(".");
		if(pos == -1)
			return -1;
		else
			i++;
		pos = sIP.indexOf(".",pos+1);
		if(pos == -1)
			return -1;
		else
			i++;
		pos = sIP.indexOf(".",pos+1);
		if(pos == -1)
			return -1;
		else
			i++;
		return 0;
	}
	
	function WriteIpFile(s){ 
		//var strText="IP=192.168.1.232\nMask=255.255.255.0\nGateway=192.168.1.1\nDNS=192.168.1.1\nMAC=08:90:90:90:90:92"
　　	if(! confirm("Do you  want to change standalone IP, yes or no?"))return;
		
		var fso = new ActiveXObject("Scripting.FileSystemObject");
		var ForReading= 1; ForWriting= 2; ForAppending= 3; 
	　　var f = fso.OpenTextFile("C:\\recorder\\eth0-setting",ForWriting,true);
		f.WriteLine(s);       
		f.Close();
		var nRet = GxSocketOcx.GX_StartUpateFile(m_strIP, "c:\\recorder\\eth0-setting", "\\etc\\eth0-setting");
    	if(nRet < 0 ){
			alert("Change standalone IP failed!");
			return;
		}
		document.getElementById("tdUpate_caption").innerText = "Update: ";
		document.getElementById("tdUpate_text").innerHTML = "Updating standalone IP, Please don't close the page";
　　}
	
	
	function UpdateFiles(){
		var sfilepath = document.getElementById("txtBrowseFile").value;
		sfilepath = sfilepath.Trim();
		if(sfilepath == ""){
			alert("Please choose a file!");
			return;
		}
		var upfile = sfilepath.substr(sfilepath.lastIndexOf("\\")+1);
		if(upfile == "gxem_record" || upfile == "gx_dsp_drv.ko" || upfile == "gxcfg.ini"){
			if(! confirm("Do you  want to change standalone program file, yes or no?"))return;
			var nRet = GxSocketOcx.GX_StartUpateFile(m_strIP, sfilepath, "\\gxvoice\\exe\\" + upfile);
    		if(nRet < 0 ){
				alert("Call API command failed!");
				return;
			}
			document.getElementById("tdUpate_caption").innerText = "Update: ";
			document.getElementById("tdUpate_text").innerHTML = "Updating standalone program file, Please don't close the page";
		}
		else if(upfile == "gxv729.bin"){
			if(! confirm("Do you  want to change standalone program file, yes or no?"))return;
			var nRet = GxSocketOcx.GX_StartUpateFile(m_strIP, sfilepath,"\\gxvoice\\dsp\\" + upfile);
    		if(nRet < 0 ){
				alert("Call API command failed!");
				return;
			}
			document.getElementById("tdUpate_caption").innerText = "Update: ";
			document.getElementById("tdUpate_text").innerHTML = "Updating standalone program file, Please don't close the page";
		}
		else{
			alert("Please choose a rigth file!");
		}
	}
	
	function RebootTerminal(){
		var nRet =GxSocketOcx. GX_RebootDevice(m_strIP);
    	if(nRet < 0 )
			alert("Call reboot standalone api command failed!");
		else{
			alert("Call reboot standalone api command successfully!");
			SetCookie("ServerIP", "")
			window.location.href="index.html"; 
		}
	}
	
	
	
	
	
	
	
	
	
	/****************************************************/
	/***********SetInterval CardWork() JavaScript code**************/
	/****************************************************/
	var timerID= 0;
	function RunCardWork(){
		timerID = setInterval("CardWork()",10);
		//timerID = setTimeout("CardWork()",500);	//页面Load后，500毫秒后开始执行CardWork()
	}
	
	function CardWork(){
		//while(GX_GetMessageForWindows(5, @MsgBuff) = 0 do
		//"@MsgBuff: MessageCode,ChannelNo,ParamLen,Parameter,IPAddr;
		var arrayVariants = new VBArray(GxSocketOcx.GX_GetMessageForWindows(0));
		var arrayBSTR = arrayVariants.toArray();
		
		var nMsgCode = parseInt(arrayBSTR[0],10);		
		var nChannel = parseInt(arrayBSTR[1],10)+1;
		var nValidLen = parseInt(arrayBSTR[2],10);
		var sParam = arrayBSTR[3];
		var sIP = arrayBSTR[4];
	  
		if(arrayBSTR[0] != 0xffff)
		{
			if(m_PubState== 0)
			{}
			else if(m_PubState== 1)
			{
				switch(nMsgCode)
				{
					case 0x4001:					//0x4001 连接成功
						break;
					case 0x4002:					//0x4002 断开连接				
						break;
					/*case 0x4008:				//0x4008 摘机
						document.all.tbl_State.rows[nChannel].cells[4].innerText= "录音";			//状态
						if(document.all.tbl_State.rows[nChannel].cells[6].innerText== "")
							document.all.tbl_State.rows[nChannel].cells[5].innerText= "拨出";		//方向
						document.all.tbl_State.rows[nChannel].cells[8].innerText= get_time(2);		//录音时间
						document.all.tbl_State.rows[nChannel].cells[9].innerText= "00:00:01";		//录音长度
						break;
					case 0x4009:				//0x4009 挂机
						SetTdValue(nChannel,"空闲");
						break;*/
					case 0x400a:					//0x400a 振铃
						document.all.tbl_State.rows[nChannel].cells[4].innerText= "Ringing...";			//状态
						document.all.tbl_State.rows[nChannel].cells[5].innerText= "In";			//方向
						break;			
					case 0x400f:					//0x400f 断线
						SetTdValue(nChannel,"Disconnect");
						break;
					case 0x4010:					//0x4010 接线
						SetTdValue(nChannel,"Idle");
						break;
					case 0x400e:					//0x400e FSK来电
						//'LTI' L:本地号码，T:时间，I:来电号码，D:拨出号码，N:姓名
						var arrData = sParam.split(",");
						document.all.tbl_State.rows[nChannel].cells[5].innerText= "In";			//方向
						document.all.tbl_State.rows[nChannel].cells[6].innerText= arrData[2];		//拨入号码
						document.all.tbl_State.rows[nChannel].cells[7].innerText= "";				//拨出号码
						break;
					case 0x4013:					//0x4013 DTMF来电
						//'LTI' L:本地号码，T:时间，I:来电号码，D:拨出号码，N:姓名
						var arrData = sParam.split(",");
						document.all.tbl_State.rows[nChannel].cells[5].innerText= "In";			//方向
						document.all.tbl_State.rows[nChannel].cells[6].innerText= arrData[2];		//拨入号码
						document.all.tbl_State.rows[nChannel].cells[7].innerText= "";				//拨出号码
						break;
					case 0x4004:					//0x4004 拨出DTMF码
						document.all.tbl_State.rows[nChannel].cells[4].innerText= "Recording";			//状态						 
						if(nValidLen >= 1){
							sParam = sParam.substr(0,nValidLen);
				            //if(sParam == 'x') sParam = '*';
    	        			//if(sParam == 'z') sParam = '#';
							
							//sDtmf.replace(/x/g,"*");
							//sDtmf.replace(/z/g,"#");	
							var strDtmfTemp= "";
				            for(var i=0;i<nValidLen;i++)
							{
								if(sParam.substr(i,1) == 'x') strDtmfTemp+= '*';
								else if(sParam.substr(i,1) == 'z') strDtmfTemp+= '#';
								else strDtmfTemp+= sParam.substr(i,1);
							}
							sParam= strDtmfTemp;
							
				            var stemp = document.all.tbl_State.rows[nChannel].cells[7].innerText;
							stemp += sParam;	
							if(stemp.length > 20) stemp= stemp.substr(0,20);
							document.all.tbl_State.rows[nChannel].cells[7].innerText= stemp;		//拨出DTMF按键
							if(document.all.tbl_State.rows[nChannel].cells[9].innerText == "")
								document.all.tbl_State.rows[nChannel].cells[9].innerText= "00:00:01";		//录音长度
						}
						break;			
					case 0x4006:					//0x4006 未接来电
						SetTdValue(nChannel,"Idle");
						break;
					case 0x4011:					//0x4011 开始录音
						document.all.tbl_State.rows[nChannel].cells[4].innerText= "Recording";			//状态
						if(document.all.tbl_State.rows[nChannel].cells[6].innerText== "")
							document.all.tbl_State.rows[nChannel].cells[5].innerText= "Out";		//方向
						document.all.tbl_State.rows[nChannel].cells[8].innerText= get_time(2);		//录音时间
						document.all.tbl_State.rows[nChannel].cells[9].innerText= "00:00:01";		//录音长度
						break;
					case 0x4012:					//0x4012 结束录音
						SetTdValue(nChannel,"Idle");
						if(m_ListenCh != -1){
							GxSocketOcx.GX_StopMonitor(m_strIP,m_ListenCh);							//停止录音时就停止通道的监听
							document.getElementById("tdListenBtn"+m_ListenCh).value= "Monitor";
							m_ListenCh= -1;
						}
						break;
				}			
			}
			else if(m_PubState== 2)
			{
				switch(nMsgCode)
				{					
					case 0x4017:					//0x4017 查询出错
					    DisplayDivJD("0");
						alert("Search failed.");	
						break;
					case 0x4018:					//0x4018 查询成功					
						ReadFile(); 				//创建记录行
						DisplayDivJD("0");
						break;
					
					case 0x4016:					//0x4016 //当下载一个录音文件时，连续15秒此文件大小的不变DLL会产生一个事件
						m_strDownloadSavePath
						alert("Download file failed.");
						DelDownloadfile_HaveErr(m_strDownloadSavePath);
						break;
					case 0x401c:					//0x401c 下载录音文件成功完成
						alert('Download file successfully, the file save in "c:\\" ');
						break;
					case 0x401d:					//0x401d 下载录音文件时，如果此文件不存在DLL会产生一个事件，并且终止下载
						alert("Download file failed.");
						DelDownloadfile_HaveErr(m_strDownloadSavePath);
						break;
					case 0x401ce:					//0x401e 下载录音文件时，如果中途出错DLL会产生一个事件，并且终止下载
						DelDownloadfile_HaveErr(m_strDownloadSavePath);
						alert("Download file failed.");
						break;
				}
			}
			else if(m_PubState== 3)
			{
				if(nMsgCode== 0x4015){				//更新文件完成
					document.getElementById("tdUpate_caption").innerHTML = "";
					document.getElementById("tdUpate_text").innerHTML = "";
					alert('Upate successfully, please roboot standalone!');	
				}
			}
			
			
		}		
	}
	
	//删除下载出错时,只下载了部分的录音文件
	function DelDownloadfile_HaveErr(filepath){
		var objFSO = new ActiveXObject("Scripting.FileSystemObject");		
		if (objFSO.FileExists(filepath))	  										// 检查文件是否存在
		   objFSO.DeleteFile(filepath, true); 
	}
		
	function SetTdValue(nChannel,strText){
		document.all.tbl_State.rows[nChannel].cells[4].innerText= strText;			//状态
		document.all.tbl_State.rows[nChannel].cells[5].innerText= "";				//方向
		document.all.tbl_State.rows[nChannel].cells[6].innerText= "";				//拨入号码
		document.all.tbl_State.rows[nChannel].cells[7].innerText= "";				//拨出号码
		document.all.tbl_State.rows[nChannel].cells[8].innerText= "";				//录音开始时间
		document.all.tbl_State.rows[nChannel].cells[9].innerText= "";				//录音通话时长
	}
	
  	function PopProgressBarForm(){ 
		//var win = window.showModalDialog("http://www.baidu.com?source="+Math.random(),window,"dialogWidth=350px;dialogHeight=400px;center=yes;");
		var win = window.showModalDialog("ProgressBar.html",window,"dialogWidth=350px;dialogHeight=400px;center=yes;"); 
	} 
	
	function DisplayDivJD(n){
		var objdiv= document.getElementById("jd_view");
		if(n == 0){
			objdiv.style.display ="none";//将div隐藏并且释放占用的页面空间
			objdiv.innerText= "";
		}
		else
		{
			objdiv.style.display ="";
			objdiv.style.visibility="visible";
			objdiv.innerHTML= '<br><br><br>Searching...        <img src="images/cxdd.gif" />';//	已完成"+ m +"%";
		}
	}
	
	
	
