
var UseActiveX = 1;
var USEIVS = 1;

var TVS_VERSION = ((parseInt(readSetupByKey("SYS_BOARDID")) & 0xF000) == 0xD000 || (parseInt(readSetupByKey("SYS_BOARDID")) & 0xF000) == 0xE000) ? '1,6,57,124' : '1,6,57,118';

var TN_CS_CONNECTED = 0x00000001;
var TN_CS_STREAMED	= 0x00000002;
var TN_CS_DECODED	= 0x00000004;
var TN_CS_DISPLAYED = 0x00000008;
var TN_CS_RECORDED =  0x00000010;

var TN_SNC_CN_SERVER_UNREACHABLE = 10;
var TN_SNC_CN_TOO_MANY_CONNECTION = 11;
var TN_SNC_CN_PASSWORD_MISMATCH = 12;
var TN_SNC_CN_CONNECTION_DISABLED = 13;
var TN_SNC_CN_ID_NOT_EXIST = 14;
var TN_SNC_CN_DOMAINNAME_UNKNOWN = 15;
var TN_SNC_CN_PARAMETER_OUTOFRANGE = 16;
var TN_SNC_CN_DUPLICATE_CONNECT = 17;
var TN_SNC_CN_KEEPALIVE_FAILED = 18;
var TN_SNC_CN_NO_AUTHORITY_OPERATION = 19;
var TN_SNC_CN_OPERATION_FAILED = 20;

var TN_ET_DI = 0;
var TN_ET_VIDEOLOSS = 1;
var TN_ET_MOTION = 2;
var TN_ET_DO = 4;
var TN_ET_RECORD = 5;
var TN_ET_RESOLUTION = 6;

var TN_ES_OFF = 0;
var TN_ES_ON = 1;

var TN_ET_OFF = 0;
var TN_ET_SET = 1;
var TN_ET_ERASE = 2;
var TN_ET_APPLY = 3;
var TN_ET_ERASEALL = 4;
var TN_ET_SETALL = 5;

var TN_MVD_PAUSE = 0;
var TN_MVD_KEYFRAME = 1;
var TN_MVD_LIVE = 2;


var g_lChID = 0;
var g_display = null;
var g_lOsdID_VL = -1;
var g_lOsdsysID = -1;
var g_lOsdsysTime = -1;
var g_ivs = null;

const SUPPORT_DETECTION_CAM_4CH = 1;
const NOT_SUPPORT_DETECTION_CAM_4CH = 0 ;

var g_detection_4ch_support = NOT_SUPPORT_DETECTION_CAM_4CH;
var g_detection_4ch_IDs = [0,0,0,0] ; 
var g_detection_4ch_Displays = [null ,null ,null ,null ]; 


function tvs_init_detection_4ch(index , server_addr , port, id, password)
{
    var max_resolution = parseInt(readSetupByKey("VID_TVS_DISPLAY_RES"));
        
    if(UseActiveX == 1){
        var agt = navigator.userAgent.toLowerCase();
        if (agt.indexOf("msie") == -1 && agt.indexOf("trident") == -1) return;
	}
    
	if (UseActiveX == 1) {
		tvs_connect_detection_4ch(server_addr, port, id, password, index, max_resolution);
    }
    else{
        // setTimeout(function() { tvs_connect(server_addr, ori_port, id, password, sub, max_resolution); }, 0);
    }
}

function tvs_init(server_addr, port, id, password, sub)
{
    var http_port = parseInt(readSetupByKey("NET_HTTPPORT"));
    var max_resolution = parseInt(readSetupByKey("VID_TVS_DISPLAY_RES"));
    var url = document.URL;
    var change_url;
    var change_port;
    var ori_port;
    
    if(UseActiveX == 1){
        var agt = navigator.userAgent.toLowerCase();
        if (agt.indexOf("msie") == -1 && agt.indexOf("trident") == -1) return;
    }

    ori_port = port;

    change_url = url.split(":");
    if(typeof change_url[2] != "undefined")
    {
        change_port = change_url[2].split("/");
	    if( typeof change_port[0] != "undefined")
	    {
		    if(parseInt(change_port[0]) >= parseInt(59000) && parseInt(change_port[0]) <= parseInt(59255))
		    {
			    if(http_port != change_port[0])
			    {
				    ori_port = parseInt(change_port[0]) - parseInt(1000);
			    }
		    }
	    }
    }
    
    if(UseActiveX == 1){
        g_display = document.Tvs.GetDisplay();
        if(USEIVS == 1){
            g_ivs = document.Tvs.GetIVSControl();
        }
    }

	if (UseActiveX == 1) {
        tvs_connect(server_addr, ori_port, id, password, sub, max_resolution);
    }
    else{
        setTimeout(function() { tvs_connect(server_addr, ori_port, id, password, sub, max_resolution); }, 0);
    }
}

/*
 * max_resolution (tvs activeX object width/height)
 * 2    : 2056x1536
 * 5    : 1944x2592
 * 6    : 4096x3000
 * else : 800x600
 */
function tvs_init2(server_addr, port, id, password, sub, max_resolution)
{
    var http_port = parseInt(readSetupByKey("NET_HTTPPORT"));
    var url = document.URL;
    var change_url;
    var change_port;
    var ori_port;
    
    if(UseActiveX == 1){
        var agt = navigator.userAgent.toLowerCase();
        if (agt.indexOf("msie") == -1 && agt.indexOf("trident") == -1) return;
    }

    ori_port = port;

    change_url = url.split(":");
    if(typeof change_url[2] != "undefined")
    {
        change_port = change_url[2].split("/");
	    if( typeof change_port[0] != "undefined")
	    {
		    if(parseInt(change_port[0]) >= parseInt(59000) && parseInt(change_port[0]) <= parseInt(59255))
		    {
			    if(http_port != change_port[0])
			    {
				    ori_port = parseInt(change_port[0]) - parseInt(1000);
			    }
		    }
	    }
    }
    
    if(UseActiveX == 1){
        g_display = document.Tvs.GetDisplay();
        if(USEIVS == 1){
            g_ivs = document.Tvs.GetIVSControl();
        }
    }

	if (UseActiveX == 1) {
        tvs_connect(server_addr, ori_port, id, password, sub, max_resolution);
    }
    else{
        setTimeout(function() { tvs_connect(server_addr, ori_port, id, password, sub, max_resolution); }, 0);
    }
}

function tvs_init_playback(server_addr, port, id, password, time)
{
    var max_resolution = parseInt(readSetupByKey("VID_TVS_DISPLAY_RES"));
    var url = document.URL;
    
    if(UseActiveX == 1){
        var agt = navigator.userAgent.toLowerCase();
        if (agt.indexOf("msie") == -1 && agt.indexOf("trident") == -1) return;
    }

    if(UseActiveX == 1){
        g_display = document.Tvs.GetDisplay();
        if(USEIVS == 1){
            g_ivs = document.Tvs.GetIVSControl();
        }
    }
    
	if (UseActiveX == 1) {
        tvs_connect_playback(server_addr, port, id, password, time, max_resolution);
    }
    else{
        setTimeout(function() { tvs_connect_playback(server_addr, port, id, password, time, max_resolution); }, 0);
    }
}

function tvs_term(sub)
{
    if(UseActiveX == 1){
        var agt = navigator.userAgent.toLowerCase();
        if (agt.indexOf("msie") == -1 && agt.indexOf("trident") == -1) return;
    }
	tvs_discon(sub);
}

function tvs_connect_detection_4ch(server_addr, port, id, password, index, max_resolution)
{
    var disable_vsnm = parseInt(readSetupByKey("SYS_DISABLE_VSNM"));
    var uri_str = 'vsnm://';
		
	if(g_detection_4ch_IDs[index] > 0) {
		tvs_discon_detection_4ch(index);
		return ;
	}

    if(disable_vsnm == 1){
        if(document.URL.indexOf("https://") == -1){
            uri_str = 'rtsp://';
            port = parseInt(readSetupByKey("NET_RTSPPORT"));
        }
        else{
            uri_str = 'https://';
            port = parseInt(readSetupByKey("NET_HTTPSPORT"));
        }
    }
    
    if(index == 0){
		sAddress = uri_str + server_addr + ':' + port + '/video1';
	}
	else if(index == 1){
		sAddress = uri_str + server_addr + ':' + port + '/video2';
	}
	else if(index == 2){
		sAddress = uri_str + server_addr + ':' + port + '/video3';
	}
	else if(index == 3){
		sAddress = uri_str + server_addr + ':' + port + '/video4';
	}
	else if(index == 8){
        if (disable_vsnm == 1) {
            sAddress = uri_str + server_addr + ':' + port + '/quad';
        } else {
            sAddress = uri_str + server_addr + ':' + port + '/video5';
        }
	}
	else{
		sAddress = uri_str + server_addr + ':' + port + '/video1';
	}
	console.log("index : " + index + ", address : " + sAddress);
	
	if(0 == index) {
		document.Tvs4_0.SetLocalConfig(3,max_resolution);
		document.Tvs4_0.SetLocalConfig(9, -1); // Autoplay on
		document.Tvs4_0.SetLocalConfig(42, 0); // SetSnapshotAspectRatio
	
		g_detection_4ch_IDs[index] = document.Tvs4_0.Connect(sAddress, id, password);
	
		document.Tvs4_0.SetMute(g_detection_4ch_IDs[index], 0);
		document.Tvs4_0.SetAutoReconnect(g_detection_4ch_IDs[index], -1);
		document.Tvs4_0.SetPTZControlSpeed(g_detection_4ch_IDs[index], 0, -1);
		document.Tvs4_0.SetPTZControlSpeed(g_detection_4ch_IDs[index], 1, -1);
		document.Tvs4_0.SetPTZControlSpeed(g_detection_4ch_IDs[index], 2, -1);		
		
		g_detection_4ch_Displays[0] = document.Tvs4_0.GetDisplay();	
	}
	else if(1 == index) {
		document.Tvs4_1.SetLocalConfig(3,max_resolution);
		document.Tvs4_1.SetLocalConfig(9, -1); // Autoplay on
		document.Tvs4_1.SetLocalConfig(42, 0); // SetSnapshotAspectRatio
	
		g_detection_4ch_IDs[index] = document.Tvs4_1.Connect(sAddress, id, password);
	
		document.Tvs4_1.SetMute(g_detection_4ch_IDs[index], 0);
		document.Tvs4_1.SetAutoReconnect(g_detection_4ch_IDs[index], -1);
		document.Tvs4_1.SetPTZControlSpeed(g_detection_4ch_IDs[index], 0, -1);
		document.Tvs4_1.SetPTZControlSpeed(g_detection_4ch_IDs[index], 1, -1);
		document.Tvs4_1.SetPTZControlSpeed(g_detection_4ch_IDs[index], 2, -1);	

		g_detection_4ch_Displays[1] = document.Tvs4_1.GetDisplay();
	}
	else if(2 == index) {
		document.Tvs4_2.SetLocalConfig(3,max_resolution);
		document.Tvs4_2.SetLocalConfig(9, -1); // Autoplay on
		document.Tvs4_2.SetLocalConfig(42, 0); // SetSnapshotAspectRatio
	
		g_detection_4ch_IDs[index] = document.Tvs4_2.Connect(sAddress, id, password);
	
		document.Tvs4_2.SetMute(g_detection_4ch_IDs[index], 0);
		document.Tvs4_2.SetAutoReconnect(g_detection_4ch_IDs[index], -1);
		document.Tvs4_2.SetPTZControlSpeed(g_detection_4ch_IDs[index], 0, -1);
		document.Tvs4_2.SetPTZControlSpeed(g_detection_4ch_IDs[index], 1, -1);
		document.Tvs4_2.SetPTZControlSpeed(g_detection_4ch_IDs[index], 2, -1);	

		g_detection_4ch_Displays[2] = document.Tvs4_2.GetDisplay();
	}
	else {
		document.Tvs4_3.SetLocalConfig(3,max_resolution);
		document.Tvs4_3.SetLocalConfig(9, -1); // Autoplay on
		document.Tvs4_3.SetLocalConfig(42, 0); // SetSnapshotAspectRatio
	
		g_detection_4ch_IDs[index] = document.Tvs4_3.Connect(sAddress, id, password);
	
		document.Tvs4_3.SetMute(g_detection_4ch_IDs[index], 0);
		document.Tvs4_3.SetAutoReconnect(g_detection_4ch_IDs[index], -1);
		document.Tvs4_3.SetPTZControlSpeed(g_detection_4ch_IDs[index], 0, -1);
		document.Tvs4_3.SetPTZControlSpeed(g_detection_4ch_IDs[index], 1, -1);
		document.Tvs4_3.SetPTZControlSpeed(g_detection_4ch_IDs[index], 2, -1);	

		g_detection_4ch_Displays[3] = document.Tvs4_3.GetDisplay();
	}
}

function tvs_connect(server_addr, port, id, password, sub, max_resolution)
{
    var disable_vsnm = parseInt(readSetupByKey("SYS_DISABLE_VSNM"));
    var uri_str = 'vsnm://';
        
    if (g_lChID > 0)
    {
	    tvs_discon(0);	    
	    return;
    }    
    if(disable_vsnm == 1){
        if(document.URL.indexOf("https://") == -1){
            uri_str = 'rtsp://';
            port = parseInt(readSetupByKey("NET_RTSPPORT"));
        }
        else{
            uri_str = 'https://';
            port = parseInt(readSetupByKey("NET_HTTPSPORT"));
        }
    }
    
    if($("[name=SYS_BOARDID]").val() == 0x1003 || $("[name=SYS_BOARDID]").val() == 0x1007){
        if(sub == 0){
	        sAddress = uri_str + server_addr + ':' + port + '/video1+audio1';
	    }
        else if(sub == 1){
	        sAddress = uri_str + server_addr + ':' + port + '/video2';
	    }
        else if(sub == 2){
	        sAddress = uri_str + server_addr + ':' + port + '/video3';
	    }
        else if(sub == 3){
	        sAddress = uri_str + server_addr + ':' + port + '/video4';
        }
        else if(sub == 4){
	        sAddress = uri_str + server_addr + ':' + port + '/video1s+audio1';
	    }
        else if(sub == 5){
	        sAddress = uri_str + server_addr + ':' + port + '/video2s';
	    }
        else if(sub == 6){
	        sAddress = uri_str + server_addr + ':' + port + '/video3s';
	    }
        else if(sub == 7){
	        sAddress = uri_str + server_addr + ':' + port + '/video4s';
        }
        else if(sub == 8){
            if (disable_vsnm == 1) {
                sAddress = uri_str + server_addr + ':' + port + '/quad';
            } else {
                sAddress = uri_str + server_addr + ':' + port + '/video5';
            }
        }
    }
    else if($("[name=SYS_BOARDID]").val() == 0xB001){ // A400 (4Ch + Quad Channel)
        if(sub == 0){
	        sAddress = uri_str + server_addr + ':' + port + '/video1';
	    }
        else if(sub == 1){
	        sAddress = uri_str + server_addr + ':' + port + '/video2';
	    }
        else if(sub == 2){
	        sAddress = uri_str + server_addr + ':' + port + '/video3';
	    }
        else if(sub == 3){
	        sAddress = uri_str + server_addr + ':' + port + '/video4';
        }
        else if(sub == 4){
            sAddress = uri_str + server_addr + ':' + port + '/video1s';
        }
        else if(sub == 5){
            sAddress = uri_str + server_addr + ':' + port + '/video2s';
        }
        else if(sub == 6){
            sAddress = uri_str + server_addr + ':' + port + '/video3s';
        }
        else if(sub == 7){
            sAddress = uri_str + server_addr + ':' + port + '/video4s';
        }
        else if(sub == 8){
            if (disable_vsnm == 1) {
                sAddress = uri_str + server_addr + ':' + port + '/quad';
            } else {
                sAddress = uri_str + server_addr + ':' + port + '/video5';
            }
	    }
        else if(sub == 9){
            if (disable_vsnm == 1) {
                sAddress = uri_str + server_addr + ':' + port + '/quads';
            } else {
                sAddress = uri_str + server_addr + ':' + port + '/video5s';
            }
        }
        else{
	        sAddress = uri_str + server_addr + ':' + port + '/video1';
        }
        console.log("sub : " + sub + ", address : " + sAddress);
    }
    else{
        if(sub == 0){
	        sAddress = uri_str + server_addr + ':' + port + '/video1+audio1';
	    }
        else if(sub == 1){
	        sAddress = uri_str + server_addr + ':' + port + '/video1s+audio1';
	    }
        else if(sub == 2){
	        sAddress = uri_str + server_addr + ':' + port + '/video1s2+audio1';
	    }
        else if(sub == 3){
	        sAddress = uri_str + server_addr + ':' + port + '/video1s3+audio1';
        }
        else{
	        sAddress = uri_str + server_addr + ':' + port + '/video1+audio1';
        }        
    }
	document.Tvs.SetLocalConfig(3,max_resolution);
    document.Tvs.SetLocalConfig(9, -1); // Autoplay on
    document.Tvs.SetLocalConfig(42, 0); // SetSnapshotAspectRatio

    g_lChID = document.Tvs.Connect(sAddress, id, password);
//	if (!document.Tvs.Play(g_lChID))
//	{
//		return;
//	}

    document.Tvs.SetMute(g_lChID, 0);
    document.Tvs.SetAutoReconnect(g_lChID, -1);
    document.Tvs.SetPTZControlSpeed(g_lChID, 0, -1);
    document.Tvs.SetPTZControlSpeed(g_lChID, 1, -1);
    document.Tvs.SetPTZControlSpeed(g_lChID, 2, -1);
//    document.Tvs.DecodingOption(2) = 0;
}

function tvs_connect_playback(server_addr, port, id, password, time, max_resolution)
{
    if (g_lChID > 0)
    {
	    tvs_discon(0);	    
	    return;
    }
    if($("[name=SYS_BOARDID]").val() == 0x1003 || $("[name=SYS_BOARDID]").val() == 0x1007){
	    return;
    }
    else{
	    sAddress = 'rtsp://' + server_addr + ':' + port + '/playback_' + time;
    }
	document.Tvs.SetLocalConfig(3,max_resolution);
    document.Tvs.SetLocalConfig(9, -1); // Autoplay on
    document.Tvs.SetLocalConfig(42, 0); // SetSnapshotAspectRatio

    g_lChID = document.Tvs.Connect(sAddress, id, password);
    document.Tvs.SetMute(g_lChID, 0);
    document.Tvs.SetAutoReconnect(g_lChID, -1);
}

function tvs_SetBuffer_DirectionCam(buffer)
{
	if(UseActiveX == 1){
        var agt = navigator.userAgent.toLowerCase();
        if (agt.indexOf("msie") == -1 && agt.indexOf("trident") == -1) return;
    }
    if(buffer <= 0){
		if(SUPPORT_DETECTION_CAM_4CH == g_detection_4ch_support) {
			document.Tvs4_0.SetLocalConfig(7, 1); // Buffer
			document.Tvs4_1.SetLocalConfig(7, 1); // Buffer
			document.Tvs4_2.SetLocalConfig(7, 1); // Buffer
			document.Tvs4_3.SetLocalConfig(7, 1); // Buffer
		}		
    }
    else if(buffer >= 90){
		if(SUPPORT_DETECTION_CAM_4CH == g_detection_4ch_support) {
			document.Tvs4_0.SetLocalConfig(7, 30); // Buffer
			document.Tvs4_1.SetLocalConfig(7, 30); // Buffer
			document.Tvs4_2.SetLocalConfig(7, 30); // Buffer
			document.Tvs4_3.SetLocalConfig(7, 30); // Buffer
		}		
    }
    else{
		if(SUPPORT_DETECTION_CAM_4CH == g_detection_4ch_support) {
			document.Tvs4_0.SetLocalConfig(7, buffer/3); // Buffer
			document.Tvs4_1.SetLocalConfig(7, buffer/3); // Buffer
			document.Tvs4_2.SetLocalConfig(7, buffer/3); // Buffer
			document.Tvs4_3.SetLocalConfig(7, buffer/3); // Buffer
		}
    }
}

function tvs_SetBuffer(buffer)
{
    if(UseActiveX == 1){
        var agt = navigator.userAgent.toLowerCase();
        if (agt.indexOf("msie") == -1 && agt.indexOf("trident") == -1) return;
    }
    if(buffer <= 0){
		document.Tvs.SetLocalConfig(7, 1); // Buffer		
    }
    else if(buffer >= 90){
		document.Tvs.SetLocalConfig(7, 30); // Buffer		
    }
    else{
		document.Tvs.SetLocalConfig(7, buffer/3); // Buffer		
    }
}

var g_lSession;
function tvs_recstart(path)
{
    g_lSession = document.Tvs.StartFileRecord(g_lChID, 1, path);
    if(g_lSession != 0){
        if(g_lSession == -2147024773){
            alert(GetLan(641));
        }
        else if(g_lSession == -2147024893){
            alert(GetLan(642));
        }
        else if(g_lSession == -2147024891){
            alert(GetLan(643));
        }
        else {
            alert(GetLan(644));
        }
        return -1;
//        alert('tvs_recstart' + g_lSession);
    }
    else{
        return 0;
//        alert('ok');
    }
}

function tvs_recstop()
{
    document.Tvs.StopFileRecord(g_lChID);
}

function tvs_digitalzoom(ratio, nx, ny)
{
    if (g_lChID > 0){
        if(UseActiveX == 1){
			g_display.SetDigitalZoom(g_lChID, ratio, nx, ny);			
        }
        else{
            document.Tvs.SetDigitalZoom(g_lChID, ratio, nx, ny);
        }
    }
}

function tvs_setnewosd(sysid, time, position, idstr, width, height)
{
        var x, y, w, h, f;
        if(sysid == 1){
            if(g_lOsdsysID >= 0){
                if(UseActiveX == 1){
					g_display.RemoveOsdArea(g_lOsdsysID);
                }
                else{
                    document.Tvs.RemoveOsdArea(0);
                }
                g_lOsdsysID = -1;
            }

            x = width/60 *2;
            w = x + (width/3*2);
            
            if(position == 0){
                y = height - height/24 - height/12; // bottom
            }
            else{
                y = height/24; // top
            }
            h = y + height/12+4;
            f = width/36;
            
            if(UseActiveX == 1){
                g_lOsdsysID = g_display.AddOsdArea(g_lChID, x, y, w, h, 0);
                g_display.SetOsdFontSize(g_lOsdsysID, f);
				g_display.UpdateOsdArea(g_lOsdsysID, idstr);
            }
            else{
                g_lOsdsysID = 0;
                document.Tvs.UpdateOsdArea(g_lChID, 0, x, y, w, h, f, idstr, 0);
            }
        }
        
	    if(time == 1){
	       if(g_lOsdsysTime >= 0){
                if(UseActiveX == 1){
					g_display.RemoveOsdArea(g_lOsdsysTime);
    			}
    			else{
                    document.Tvs.RemoveOsdArea(1);
    			}
				g_lOsdsysTime = -1;
	       }
	       
            x = width - (width/60 *2) - (width/6) - 20;
            w = x + (width/3);
            
            if(position == 0){
                y = height - height/24 - height/12; // bottom
            }
            else{
                y = height/24; // top
            }
            h = y + height/12+4;
            f = width/36;
            
           if(UseActiveX == 1){
	           g_lOsdsysTime = g_display.AddOsdArea(g_lChID, x, y, w, h, 0);
               g_display.SetOsdFontSize(g_lOsdsysTime, f);
			   g_display.UpdateOsdArea(g_lOsdsysTime, '%SERVERTIME%');
           }
           else{
                g_lOsdsysTime = 1;
                document.Tvs.UpdateOsdArea(g_lChID, 1, x, y, w, h, f, '%SERVERTIME%', 0);
           }
       }
}

function tvs_setvideolossosd(x, y, w, h)
{
    if(UseActiveX == 1){
        g_lOsdID_VL = g_display.AddOsdArea(g_lChID, x, y, w, h, 0);
        g_display.SetOsdFontSize(g_lOsdID_VL, 20);
		g_display.UpdateOsdArea(g_lOsdID_VL, 'Video Loss');
    }
    else{
        g_lOsdID_VL = 2;
        document.Tvs.UpdateOsdArea(g_lChID, 2, x, y, w, h, 20, 'Video Loss', 0);
    }
}
function tvs_clrvideolossosd()
{
	if (g_lOsdID_VL >= 0)
	{
        if(UseActiveX == 1){
			g_display.RemoveOsdArea(g_lOsdID_VL);
		}
		else{
    		document.Tvs.RemoveOsdArea(2);
		}
		g_lOsdID_VL = -1;
	}
}

function pause(msec) {
    var now = new Date();
    var exitTime = now.getTime() + (msec);
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime) return;
    }
} 

function tvs_discon_detection_4ch(index)
{
	if(SUPPORT_DETECTION_CAM_4CH == g_detection_4ch_support) {
	    if(index >= 0 && index < 4 ) {	
		    if(0 == index) {
			    document.Tvs4_0.Pause(g_detection_4ch_IDs[index]);
    			document.Tvs4_0.SetMute(g_detection_4ch_IDs[index], 1);		
	    		document.Tvs4_0.Disconnect(g_detection_4ch_IDs[index]);	

		    	g_detection_4ch_IDs[index] = 0;	 
    		}
	    	else if(1 == index) {
		    	document.Tvs4_1.Pause(g_detection_4ch_IDs[index]);
			    document.Tvs4_1.SetMute(g_detection_4ch_IDs[index], 1);		
    			document.Tvs4_1.Disconnect(g_detection_4ch_IDs[index]);	

	    		g_detection_4ch_IDs[index] = 0;	 
		    }
    		else if(2 == index) {
	    		document.Tvs4_2.Pause(g_detection_4ch_IDs[index]);
		    	document.Tvs4_2.SetMute(g_detection_4ch_IDs[index], 1);		
			    document.Tvs4_2.Disconnect(g_detection_4ch_IDs[index]);	

    			g_detection_4ch_IDs[index] = 0;	 
	    	}
		    else {
    			document.Tvs4_3.Pause(g_detection_4ch_IDs[index]);
	    		document.Tvs4_3.SetMute(g_detection_4ch_IDs[index], 1);		
		    	document.Tvs4_3.Disconnect(g_detection_4ch_IDs[index]);	

    			g_detection_4ch_IDs[index] = 0;	 
	    	}
    	}
    	// alert('disconnect ' + index );
        pause(500);
    }
}

function tvs_discon(sub)
{
    if (g_lChID == 0)
    {
	    return;
    }

    document.Tvs.Pause(g_lChID);
    document.Tvs.SetMute(g_lChID, 1);

    document.Tvs.Disconnect(g_lChID);
    g_lChID = 0;
//	alert('connected');

    pause(500);
}
/*
function tvs_on_ChannelNotify(lChID, eNewState, eReason)
{
//	alert('tvs_on_ChannelNofy');

	if (eNewState & TN_CS_CONNECTED)
	{
//		alert('connected');	
	}
	else
	{
//		alert('disconnected(reason=' + eReason + ')');	
		if (eReason >= TN_SNC_CN_SERVER_UNREACHABLE)
		{
			switch (eReason)
			{
			case TN_SNC_CN_SERVER_UNREACHABLE:
//				alert('Server is unreachable for connection');
				break;
			case TN_SNC_CN_TOO_MANY_CONNECTION:
//				alert('Too many connections on the server');
				break;
			case TN_SNC_CN_PASSWORD_MISMATCH:
//				alert('Password is incorrect');
				break;
			case TN_SNC_CN_CONNECTION_DISABLED:
//				alert('Server disabled the connection');
				break;
			case TN_SNC_CN_ID_NOT_EXIST:
//				alert('Login ID does not exist');
				break;
			case TN_SNC_CN_DOMAINNAME_UNKNOWN:
//				alert('Can not resolve the domain name');
				break;
			case TN_SNC_CN_PARAMETER_OUTOFRANGE:
//				alert('Invalid channels is one of this case');
				break;
			case TN_SNC_CN_DUPLICATE_CONNECT:
//				alert('Multiple connections to a channel are not allowed');
				break;
			case TN_SNC_CN_KEEPALIVE_FAILED:
//				alert('Disconnected due to keep alive timeout');
				break;
			case TN_SNC_CN_NO_AUTHORITY_OPERATION:
//				alert('No authority operation');
				break;
			case TN_SNC_CN_OPERATION_FAILED:
//				alert('General failure');
				break;
			}
		}
	}
}

function tvs_on_MouseEvent(lChID, lMsg, wParam, lParam)
{
}

function tvs_on_ServerEvent(lChID, eEventType, nIndex, eEventStatus, vData, motion_img, record_img)
{
	switch (eEventType)
	{
	case TN_ET_MOTION:
		if (eEventStatus)
		{
				document[motion_img].src = '/image/motion.jpg';
		}
		else
		{
				document[motion_img].src = '/image/null.jpg';
		}
		break;
	case TN_ET_RECORD:
		if (eEventStatus == 1)
		{
				document[record_img].src = '/image/rec_on.jpg';
		}
		else if (eEventStatus == -1)
		{
				document[record_img].src = '/image/rec_err.jpg';
		}
		else
		{
				document[record_img].src = '/image/null.jpg';
		}
		break;
	case TN_ET_VIDEOLOSS:
		if (eEventStatus)
		{
//			alert('Video Loss(1)');
			if (g_lOsdID_VL >= 0)
			{
				g_display.RemoveOsdArea(g_lOsdID_VL);
				g_lOsdID_VL = -1;
			}
		}
		else
		{
//			alert('Video Loss(0)');
			var rcVL = new Array(document.Tvs.width / 2 - 60, document.Tvs.height / 2 - 30, document.Tvs.width / 2 + 80, document.Tvs.height / 2 + 30);
			
			g_lOsdID_VL = g_display.AddOsdArea(g_lChID, rcVL[0], rcVL[1], rcVL[2], rcVL[3], 0);
			g_display.SetOsdFontSize(g_lOsdID_VL, 20);
			g_display.UpdateOsdArea(g_lOsdID_VL, 'Video Loss');
		}
		break;
	 }
}

function tvs_ResetVLossOSD()
{
	if (g_lOsdID_VL >= 0)
	{
    	var rcVL = new Array(document.Tvs.width / 2 - 60, document.Tvs.height / 2 - 30, document.Tvs.width / 2 + 80, document.Tvs.height / 2 + 30);
		g_display.RemoveOsdArea(g_lOsdID_VL);
	    g_lOsdID_VL = g_display.AddOsdArea(g_lChID, rcVL[0], rcVL[1], rcVL[2], rcVL[3], 0);
	    g_display.SetOsdFontSize(g_lOsdID_VL, 20);
	    g_display.UpdateOsdArea(g_lOsdID_VL, 'Video Loss');
	}
	
}

function tvs_on_ServerEventVLoss(lChID, eEventType, nIndex, eEventStatus, vData)
{
	switch (eEventType)
	{
	case TN_ET_VIDEOLOSS:
		if (eEventStatus)
		{
//			alert('Video Loss1 - 1');
			if (g_lOsdID_VL >= 0)
			{
				g_display.RemoveOsdArea(g_lOsdID_VL);
				g_lOsdID_VL = -1;
			}
		}
		else
		{
//			alert('Video Loss0 - 1');
			var rcVL = new Array(document.Tvs.width / 2 - 60, document.Tvs.height / 2 - 30, document.Tvs.width / 2 + 80, document.Tvs.height / 2 + 30);
			
			g_lOsdID_VL = g_display.AddOsdArea(g_lChID, rcVL[0], rcVL[1], rcVL[2], rcVL[3], 0);
			g_display.SetOsdFontSize(g_lOsdID_VL, 20);
			g_display.UpdateOsdArea(g_lOsdID_VL, 'Video Loss');
		}
		break;
	 }
}

function tvs_on_ServerEventVideo(lChID, eEventType, nIndex, eEventStatus, vData, vmotion_img)
{
	switch (eEventType)
	{
	case TN_ET_MOTION:
		if (eEventStatus)
		{
				document[vmotion_img].src = '/image/motion.jpg';
		}
		else
		{
				document[vmotion_img].src = '/image/null.jpg';
		}
		break;
	 }
}

function tvs_on_ServerEventView(lChID, eEventType, nIndex, eEventStatus, vData, sensor_img, alarm_img, sensor2_img, alarm2_img, sensor3_img, sensor4_img)
{
}

function tvs_on_SystemNotify(lNotifyCode, vData)
{
}
*/
function tvs_snapshot(fileName)
{
	if (g_lChID == 0) return;
//	document.Tvs.Snapshot(g_lChID, true);

	document.Tvs.Snapshot(g_lChID, 0, false); // 0 = bmp, 1=jpg
//	document.Tvs.Snapshot(g_lChID, false); // 0 = bmp, 1=jpg
	document.Tvs.PreviewSnapshot(g_lChID);
}

function tvs_setDO(index, value)
{
	if (g_lChID == 0) return;
    document.Tvs.SetDO(g_lChID, index, value);
}

function tvs_enable_talk(enable)
{
	if (g_lChID == 0) return;
	if (enable)
	{
		document.Tvs.StartTalking(g_lChID);
	}
	else
	{
		document.Tvs.StopTalking(g_lChID);
	}
}

function tvs_set_motion_region_edit(set)
{
	if (g_lChID == 0) return;
	document.Tvs.SetMotionRegionEdit(g_lChID, set, TN_MVD_KEYFRAME);	
}

function tvs_set_motion_region_edit2(set,region)
{
	if (g_lChID == 0) return;
	document.Tvs.SetMotionRegionEdit2(g_lChID, region,set,TN_MVD_LIVE);	
}


function tvs_set_motion_region_edit2_detection_cam(channel,set,region)
{
	if(channel >= 0 && channel < 4) {
		if(0 == g_detection_4ch_IDs[channel]) {
			return ;
		}
		if(0 == channel) {
			document.Tvs4_0.SetMotionRegionEdit2(g_detection_4ch_IDs[channel], region,set,TN_MVD_LIVE);	
		}
		else if(1 == channel) {
			document.Tvs4_1.SetMotionRegionEdit2(g_detection_4ch_IDs[channel], region,set,TN_MVD_LIVE);	
		}
		else if(2 == channel) {		
			document.Tvs4_2.SetMotionRegionEdit2(g_detection_4ch_IDs[channel], region,set,TN_MVD_LIVE);	
		}
		else {
			document.Tvs4_3.SetMotionRegionEdit2(g_detection_4ch_IDs[channel], region,set,TN_MVD_LIVE);	
		}		
	}
	
	
}


function tvs_set_scale_enable(mode,id)
{
	if (g_lChID == 0) return;
	document.Tvs.SetScaleEnable(g_lChID, mode,id);	
}


function tvs_set_scale_edit(id,x,y,w,h)
{
	if (g_lChID == 0) return;
	document.Tvs.SetScaleEdit(g_lChID, id,x,y,w,h);	
}

function tvs_get_scale_x(id)
{
	if (g_lChID == 0) return 0;
	return document.Tvs.GetScaleX(g_lChID, id);
}
function tvs_get_scale_y(id)
{
	if (g_lChID == 0) return 0;
	return document.Tvs.GetScaleY(g_lChID, id);
}
function tvs_get_scale_w(id)
{
	if (g_lChID == 0) return 255;
	return document.Tvs.GetScaleWidth(g_lChID, id);
}
function tvs_get_scale_h(id)
{
	if (g_lChID == 0) return 255;
	return document.Tvs.GetScaleheight(g_lChID, id);
}


function tvs_set_privacy_mask_region_edit(set,region)
{
	if (g_lChID == 0) return;
	document.Tvs.SetPrivacyRegionEdit(g_lChID, region,set,TN_MVD_LIVE);	
}

function tvs_set_privacy_mask_region_edit3(set,region)
{
	if (g_lChID == 0) return;
	document.Tvs.SetPrivacyRegionEdit3(g_lChID, region,set,TN_MVD_LIVE);	
}

function tvs_set_screen_calibration_mode(mode)
{
	if (g_lChID == 0) return;
	setTimeout(function() { document.Tvs.SetScreenCalibrationMode(g_lChID, mode); }, 0);	
	
}

function tvs_config_camera_module(code, down)
{
	if (g_lChID == 0) return;
	document.Tvs.ConfigCameraModule(g_lChID, code, down);	
}

function tvs_get_keepaspectratio()
{
	if (g_lChID == 0) return true;
	return document.Tvs.KeepAspectRatio(g_lChID);	
}

function tvs_set_fullscreen(mode)
{
	if (g_lChID == 0) return;
    return document.Tvs.SetFullScreen(g_lChID, mode);
}

function tvs_get_width()
{
    return document.Tvs.width;
}

function tvs_get_height()
{
    return document.Tvs.height;
}

function tvs_set_width(w)
{
	document.Tvs.width = w;	
}

function tvs_set_height(h)
{
	document.Tvs.height = h;	
}

function tvs_set_width_detection_4ch(w)
{
	if(SUPPORT_DETECTION_CAM_4CH == g_detection_4ch_support) {
	    document.Tvs4_0.width = w ;
    	document.Tvs4_1.width = w ;
	    document.Tvs4_2.width = w ;
    	document.Tvs4_3.width = w ;
    }
}
function tvs_set_height_detection_4ch(h)
{
	if(SUPPORT_DETECTION_CAM_4CH == g_detection_4ch_support) {
	    document.Tvs4_0.height = h ;
    	document.Tvs4_1.height = h ;
	    document.Tvs4_2.height = h ;
    	document.Tvs4_3.height = h ;
    }
}

function tvs_get_width_detection_4ch(ch)
{
	if(ch >= 0 && ch < 4) {
		if(0 == ch) {
			return document.Tvs4_0.width ;
		}
		else if(1 == ch) {
			return document.Tvs4_1.width ;
		}
		else if(2 == ch) {
			return document.Tvs4_2.width ;
		}
		else if(3 == ch) {
			return document.Tvs4_3.width ;
		}
	}	
	return document.Tvs4_0.width ;
}
function tvs_get_height_detection_4ch(ch)
{
	if(ch >= 0 && ch < 4) {
		if(0 == ch) {
			return document.Tvs4_0.height ;
		}
		else if(1 == ch) {
			return document.Tvs4_1.height ;
		}
		else if(2 == ch) {
			return document.Tvs4_2.height ;
		}
		else if(3 == ch) {
			return document.Tvs4_3.height ;
		}
	}	
	return document.Tvs4_0.height ;
}

function tvs_get_location(path)
{
    return document.Tvs.FindLocation(path);
}

function tvs_ivs_StartLineEdit()
{
	if (g_lChID == 0) return;
    if(USEIVS == 1){
    g_ivs.StartLineEdit(g_lChID);
    g_ivs.SetLineEditColor(g_lChID, 0x80FF0000);
    g_ivs.SetLineEditWidth(g_lChID, 0.8);
    }
}

function tvs_ivs_StopLineEdit()
{
	if (g_lChID == 0) return;
    if(USEIVS == 1)
    g_ivs.StopLineEdit(g_lChID);
}

function tvs_ivs_SetLineData2(x1, y1, x2, y2)
{
	if (g_lChID == 0) return;
    if(USEIVS == 1)
    g_ivs.SetLineData2(g_lChID, x1, y1, x2, y2);
}

function tvs_ivs_GetLineData2(n)
{
	if (g_lChID == 0) return 0;
    if(USEIVS == 1)
    return g_ivs.GetLineData2(g_lChID, n);
}

function tvs_ivs_StartCellEdit()
{
	if (g_lChID == 0) return;
    if(USEIVS == 1){
    g_ivs.StartCellEdit(g_lChID);
    g_ivs.SetCellEditColor(g_lChID, 0xFF000000, 0x60FF8080);
    g_ivs.SetCellCount(g_lChID, 24, 32);
    }
}

function tvs_ivs_StopCellEdit()
{
	if (g_lChID == 0) return;
    if(USEIVS == 1)
    g_ivs.StopCellEdit(g_lChID);
}

function tvs_ivs_SetCellEditMode(b)
{
	if (g_lChID == 0) return;
    if(USEIVS == 1)
    g_ivs.SetCellEditMode(g_lChID, b);
}

function tvs_ivs_SetCellData2(str)
{
	if (g_lChID == 0) return;
    if(USEIVS == 1)
    g_ivs.SetCellData2(g_lChID, 24, 32, str);
}

function tvs_ivs_GetCellData2()
{
	if (g_lChID == 0) return null;
    if(USEIVS == 1)
    return g_ivs.GetCellData2(g_lChID);
}

function tvs_ivs_StartIVSObjectDisplay()
{
	if (g_lChID == 0) return null;
    if(USEIVS == 1)
        g_ivs.StartIVSObjectDisplay(g_lChID, 3, 0xFF000000, 0xFFFF0000, 0, 0);
}

function tvs_ivs_StopIVSObjectDisplay()
{
	if (g_lChID == 0) return null;
    if(USEIVS == 1)
        g_ivs.StopIVSObjectDisplay(g_lChID);
}

function tvs_StatShowTrigger()
{
	if (g_lChID == 0) return null;
	document.Tvs.StatShowTrigger();	
}

var g_lChID2 = 0;

function tvs2_connect(server_addr, port, id, password, sub)
{
    var max_resolution = parseInt(readSetupByKey("VID_TVS_DISPLAY_RES"));
    
    if(UseActiveX == 1){
        var agt = navigator.userAgent.toLowerCase();
        if (agt.indexOf("msie") == -1 && agt.indexOf("trident") == -1) return;
    }

    if (g_lChID2 > 0)
    {
	    return;
    }

    if($("[name=SYS_BOARDID]").val() == 0x1003 || $("[name=SYS_BOARDID]").val() == 0x1007){
        if(sub == 0){
	        sAddress = 'vsnm://' + server_addr + ':' + port + '//video1+audio1';
	    }
        else if(sub == 1){
	        sAddress = 'vsnm://' + server_addr + ':' + port + '//video2';
	    }
        else if(sub == 2){
	        sAddress = 'vsnm://' + server_addr + ':' + port + '//video3';
	    }
        else if(sub == 3){
	        sAddress = 'vsnm://' + server_addr + ':' + port + '//video4';
        }
        else if(sub == 4){
	        sAddress = 'vsnm://' + server_addr + ':' + port + '//video1s+audio1';
	    }
        else if(sub == 5){
	        sAddress = 'vsnm://' + server_addr + ':' + port + '//video2s';
	    }
        else if(sub == 6){
	        sAddress = 'vsnm://' + server_addr + ':' + port + '//video3s';
	    }
        else if(sub == 7){
	        sAddress = 'vsnm://' + server_addr + ':' + port + '//video4s';
        }
        else if(sub == 8){
	        sAddress = 'vsnm://' + server_addr + ':' + port + '//video5';
        }
    }
    else{
        if(sub == 0){
	        sAddress = 'vsnm://' + server_addr + ':' + port + '//video1+audio1';
	    }
        else if(sub == 1){
	        sAddress = 'vsnm://' + server_addr + ':' + port + '//video1s+audio1';
	    }
        else if(sub == 2){
	        sAddress = 'vsnm://' + server_addr + ':' + port + '//video1s2+audio1';
	    }
	    else{
	        sAddress = 'vsnm://' + server_addr + ':' + port + '//video1s3+audio1';
        }
    }
	document.Tvs2.SetLocalConfig(3,max_resolution);
    document.Tvs2.SetLocalConfig(9, -1); // Autoplay on
    document.Tvs2.SetLocalConfig(42, 0); // SetSnapshotAspectRatio

    g_lChID2 = document.Tvs2.Connect(sAddress, id, password);
//   	alert('after connect');
//	if (!document.Tvs2.Play(g_lChID))
//	{
//		return;
//	}

    document.Tvs2.SetMute(g_lChID2, 0);
    document.Tvs2.SetAutoReconnect(g_lChID2, -1);
    document.Tvs2.SetPTZControlSpeed(g_lChID2, 0, -1);
    document.Tvs2.SetPTZControlSpeed(g_lChID2, 1, -1);
    document.Tvs2.SetPTZControlSpeed(g_lChID2, 2, -1);
//    document.Tvs2.DecodingOption(2) = 0;
}

function tvs2_discon()
{
    if(UseActiveX == 1){
        var agt = navigator.userAgent.toLowerCase();
        if (agt.indexOf("msie") == -1 && agt.indexOf("trident") == -1) return;
    }

    if (g_lChID2 == 0)
    {
	    return;
    }

    document.Tvs2.Pause(g_lChID2);
    document.Tvs2.SetMute(g_lChID2, 1);

    document.Tvs2.Disconnect(g_lChID2);
    g_lChID2 = 0;
//	alert('connected');

    pause(500);
}

function OnServerNotify() {
}
function OnFileRecordNotify() {
}

function tvs_ivs_SetLineEditEntryDirection(){
	if(g_lChID == 0) {
		return;
	}

	if(USEIVS == 1) {
		if(UseActiveX == 1) {
			g_ivs.SetLineEditEntryDirection(g_lChID, 1); // 방향을 지정함.
		}
	}	
}

function tvs_ivs_GetLineEditEntryDirection(){
	if(g_lChID == 0) {
		return;
	}

	if(USEIVS == 1) {
		if(UseActiveX == 1) {
			return g_ivs.GetLineEditEntryDirection(g_lChID); // 방향을 지정함.
		}
	}
}

function tvs_ivs_StartPolygonEdit()
{
	if (g_lChID == 0) return;
    if(USEIVS == 1){
		g_ivs.StartPolygonEdit(g_lChID);
    }
}

function tvs_ivs_StopPolygonEdit()
{
	if (g_lChID == 0) return;
    if(USEIVS == 1){
		g_ivs.StopPolygonEdit(g_lChID);
    }
}

function tvs_ivs_SetPolygonData(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8)
{
	if (g_lChID == 0) return;
	if(USEIVS == 1) {
		g_ivs.SetPolygonData(g_lChID, x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8);
	}
}

function tvs_ivs_SetPolygonData16(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8, x9, y9, x10, y10, x11, y11, x12, y12, x13, y13, x14, y14, x15, y15, x16, y16)
{
	if (g_lChID == 0) return;
	if(USEIVS == 1) {
		g_ivs.SetPolygonData16(g_lChID, x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8, x9, y9, x10, y10, x11, y11, x12, y12, x13, y13, x14, y14, x15, y15, x16, y16)
	}
}

function tvs_ivs_GetPolygonX(n)
{
	if (g_lChID == 0) return;
	if(USEIVS == 1) {
		return g_ivs.GetPolygonDataX(g_lChID, n);
	}
}

function tvs_ivs_GetPolygonY(n)
{
	if (g_lChID == 0) return;
	if(USEIVS == 1) {
		return g_ivs.GetPolygonDataY(g_lChID, n);
	}
}

function tvs_ivs_imp_StartLineCrossEdit()
{
	if (g_lChID == 0) return;
    if(USEIVS == 1){
		g_ivs.StartLineCrossEdit(g_lChID);

		g_ivs.SetLineCrossConfig(g_lChID, 0, 0.8, 0xFFD01202);
		g_ivs.SetLineCrossConfig(g_lChID, 1, 0.8, 0xFFA35827);
		g_ivs.SetLineCrossConfig(g_lChID, 2, 0.8, 0xFFCEB87B);
		g_ivs.SetLineCrossConfig(g_lChID, 3, 0.8, 0xFF7CCC83);
		g_ivs.SetLineCrossConfig(g_lChID, 4, 0.8, 0xFF7EC4CB);
		g_ivs.SetLineCrossConfig(g_lChID, 5, 0.8, 0xFF7C93CC);
		g_ivs.SetLineCrossConfig(g_lChID, 6, 0.8, 0xFFC783A3);
		g_ivs.SetLineCrossConfig(g_lChID, 7, 0.8, 0xFF96A327);
    }
}

function tvs_ivs_imp_StopLineCrossEdit()
{
	if (g_lChID == 0) return;
    if(USEIVS == 1){
		g_ivs.StopLineCrossEdit(g_lChID);
    }
}

// num(0~7), mode(0(erase), 1(set))
function tvs_ivs_imp_SetLineCrossEditMode(num, mode)
{
	if (g_lChID == 0) return;
    if(USEIVS == 1){
		g_ivs.SetLineCrossEditMode(g_lChID, num, mode);
    }
}

function tvs_ivs_imp_SetLineCrossData(startx1, starty1, endx1, endy1, startx2, starty2, endx2, endy2,
	   	startx3, starty3, endx3, endy3, startx4, starty4, endx4, endy4, startx5, starty5, endx5, endy5,
	   	startx6, starty6, endx6, endy6, startx7, starty7, endx7, endy7, startx8, starty8, endx8, endy8)
{
	if (g_lChID == 0) return;
	if(USEIVS == 1) {
		g_ivs.SetLineCrossData(g_lChID, startx1, starty1, endx1, endy1, startx2, starty2, endx2, endy2,
			   	startx3, starty3, endx3, endy3, startx4, starty4, endx4, endy4, startx5, starty5, endx5, endy5,
			   	startx6, starty6, endx6, endy6, startx7, starty7, endx7, endy7, startx8, starty8, endx8, endy8);
	}
}

function tvs_ivs_imp_GetLineCrossStartX(n)
{
	if (g_lChID == 0) return;
	if(USEIVS == 1) {
		return g_ivs.GetLineCrossStartX(g_lChID, n);
	}
}

function tvs_ivs_imp_GetLineCrossStartY(n)
{
	if (g_lChID == 0) return;
	if(USEIVS == 1) {
		return g_ivs.GetLineCrossStartY(g_lChID, n);
	}
}

function tvs_ivs_imp_GetLineCrossEndX(n)
{
	if (g_lChID == 0) return;
	if(USEIVS == 1) {
		return g_ivs.GetLineCrossEndX(g_lChID, n);
	}
}

function tvs_ivs_imp_GetLineCrossEndY(n)
{
	if (g_lChID == 0) return;
	if(USEIVS == 1) {
		return g_ivs.GetLineCrossEndY(g_lChID, n);
	}
}

function tvs_ivs_imp_SetPolygonConfig(line_thickness, line_color, circle_diameter, circle_color_selected)
{
	/*
		color : ARGB DWORD Value (Default : 0xFFFF0000)
		thickness : 1.0(Default)
		diameter : 1.0(Default)
	 */
	if (g_lChID == 0) return;
    if(USEIVS == 1){
		g_ivs.SetPolygonConfig(g_lChID, line_thickness, line_color, circle_diameter, circle_color_selected);
    }
}

function tvs_ivs_imp_SetLineCrossConfig(line_num, line_thickness, line_color)
{
	/*
		thickness : 1.0(Default)
	 */
	if (g_lChID == 0) return;
    if(USEIVS == 1){
		g_ivs.SetLineCrossConfig(g_lChID, line_num, line_thickness, line_color);
    }
}

function tvs_HiRoi_SetROIMode(stream_index, region_index, mode)
{
	// mode : off(0), set(1), erase(2), display(3)
	if(g_lChID == 0) return;

	if(stream_index < 0 || stream_index > 15) {
		return;
	}

//	if(region_index < 0 || region_index > 7) {
	if(region_index < 0 || region_index > 3) {
		return;
	}

	if(mode < 0 || mode > 3) {
		return;
	}

	document.Tvs.SetROIMode(g_lChID, stream_index, region_index, mode);
}

function tvs_HiRoi_SetROIDataWithMin(stream_index, region_index, x, y, w, h, min_w, min_h)
{
	if(g_lChID == 0) return;

	if(stream_index < 0 || stream_index > 15) {
		return;
	}

//	if(region_index < 0 || region_index > 7) {
	if(region_index < 0 || region_index > 3) {
		return;
	}
	document.Tvs.SetROIDataWithMin(g_lChID, stream_index, region_index, x, y, w, h, min_w, min_h);
}

function tvs_HiRoi_SetROIData(stream_index, region_index, x, y, w, h)
{
	if(g_lChID == 0) return;

	if(stream_index < 0 || stream_index > 15) {
		return;
	}

//	if(region_index < 0 || region_index > 7) {
	if(region_index < 0 || region_index > 3) {
		return;
	}

	document.Tvs.SetROIData(g_lChID, stream_index, region_index, x, y, w, h);
}

function tvs_HiRoi_GetROIStartX(stream_index, region_index)
{
	if(g_lChID == 0) return;

	if(stream_index < 0 || stream_index > 15) {
		return;
	}

//	if(region_index < 0 || region_index > 7) {
	if(region_index < 0 || region_index > 3) {
		return;
	}

	return document.Tvs.GetROIStartX(g_lChID, stream_index, region_index); 
}

function tvs_HiRoi_GetROIStartY(stream_index, region_index)
{
	if(g_lChID == 0) return;

	if(stream_index < 0 || stream_index > 15) {
		return;
	}

//	if(region_index < 0 || region_index > 7) {
	if(region_index < 0 || region_index > 3) {
		return;
	}

	return document.Tvs.GetROIStartY(g_lChID, stream_index, region_index);
}

function tvs_HiRoi_GetROIWidth(stream_index, region_index)
{
	if(g_lChID == 0) return;

	if(stream_index < 0 || stream_index > 15) {
		return;
	}

//	if(region_index < 0 || region_index > 7) {
	if(region_index < 0 || region_index > 3) {
		return;
	}

	return document.Tvs.GetROIWidth(g_lChID, stream_index, region_index);
}

function tvs_HiRoi_GetROIHeight(stream_index, region_index)
{
	if(g_lChID == 0) return;

	if(stream_index < 0 || stream_index > 15) {
		return;
	}

//	if(region_index < 0 || region_index > 7) {
	if(region_index < 0 || region_index > 3) {
		return;
	}

	return document.Tvs.GetROIHeight(g_lChID, stream_index, region_index);
}

// x, y : 0 ~ 100
function tvs_SetDrawCross(x, y, length, width,  color)
{
	if (g_lChID == 0) return;
	setTimeout(function() { g_display.SetDrawCross(g_lChID, x, y, length, width, color); }, 0);	
}

function tvs_SetDrawCross_4Ch(ch_id, x, y, length, width,  color)
{
	// console.log("-- ch_id : " + ch_id + " x : "+ x + " y :" + y );
	if(UseActiveX == 1){
		if(null != g_detection_4ch_Displays[ch_id]) {
			g_detection_4ch_Displays[ch_id].SetDrawCross(g_detection_4ch_IDs[ch_id], x, y, length, width , color);
			// g_detection_4ch_Displays[ch_id].SetDrawCross(g_detection_4ch_IDs[ch_id], "cross.png", x, y, width );
		}
	}
	else {
		if(0 == ch_id) {
			document.Tvs4_0.SetDrawCross(g_detection_4ch_IDs[ch_id], x, y, length, width , color);
		}
		else if(1 == ch_id) {
			document.Tvs4_1.SetDrawCross(g_detection_4ch_IDs[ch_id], x, y, length, width , color);
		}
		else if(2 == ch_id) {
			document.Tvs4_2.SetDrawCross(g_detection_4ch_IDs[ch_id], x, y, length, width , color);
		}
		else {
			document.Tvs4_3.SetDrawCross(g_detection_4ch_IDs[ch_id], x, y, length, width , color);
		}		
	}	
}

function tvs_SetStraightLineMode(stream_index, region_index, mode, color, width)
{
	// mode : off(0), set(1), erase(2), display(3)
	if(g_lChID == 0) {
        return;
    }

	if(stream_index < 0 || stream_index > 15) {
		return;
	}

	if(region_index < 0 || region_index > 7) {
		return;
	}

	if(mode < 0 || mode > 3) {
		return;
	}

	document.Tvs.SetStraightLineMode(g_lChID, stream_index, region_index, mode);
	document.Tvs.SetStraightLineColor(g_lChID, stream_index, region_index, color);
	document.Tvs.SetStraightLineWidth(g_lChID, stream_index, region_index, width);
}

function tvs_SetStraightLineData(stream_index, region_index, direction, position)
{
    // direction : horizontal(0), vertical(1)
	if(g_lChID == 0) return;

	if(stream_index < 0 || stream_index > 15) {
		return;
	}

	if(region_index < 0 || region_index > 7) {
		return;
	}

	if(direction != 0 && direction != 1) {
		return;
	}

    if(position < 0)    position = 0;
    if(position > 1000) position = 1000;

    document.Tvs.SetStraightLineData(g_lChID, stream_index, region_index, direction, position);
}

function tvs_GetStraightLineDirection(stream_index, region_index)
{
	if(g_lChID == 0) return;

	if(stream_index < 0 || stream_index > 15) {
		return -1;
	}

	if(region_index < 0 || region_index > 7) {
		return -1;
	}

    return document.Tvs.GetStraightLineDirection(g_lChID, stream_index, region_index);
}

function tvs_GetStraightLinePosition(stream_index, region_index)
{
	if(g_lChID == 0) return;

	if(stream_index < 0 || stream_index > 15) {
		return -1;
	}

	if(region_index < 0 || region_index > 7) {
		return -1;
	}

    return document.Tvs.GetStraightLinePosition(g_lChID, stream_index, region_index);
}


/* Polygon8 (TVS_VERSION >= 1,6,57,122) */
// mode : 0(off), 1(edit), 2(erase), 3(display)
// thick : float
// color : hex
function tvs_SetPolygon8Mode(stream_index, polygon_index, mode, thick, line_color, circle_color, circle_sel_color)
{
    if (g_lChID == 0) return;

    document.Tvs.SetPolygon8Color(g_lChID, stream_index, polygon_index, line_color, circle_color, circle_sel_color);
    document.Tvs.SetPolygon8Thickness(g_lChID, stream_index, polygon_index, thick);
    return document.Tvs.SetPolygon8Mode(g_lChID, stream_index, polygon_index, mode);
}

function tvs_SetPolygon8Data(stream_index, polygon_index, x0, y0, x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7)
{
    if (g_lChID == 0) return;

    return document.Tvs.SetPolygon8Data(g_lChID, stream_index, polygon_index, x0, y0, x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7);
}

function tvs_GetPolygon8DataX(stream_index, polygon_index, point_index)
{
    if (g_lChID == 0) return;

    return document.Tvs.GetPolygon8DataX(g_lChID, stream_index, polygon_index, point_index);
}

function tvs_GetPolygon8DataY(stream_index, polygon_index, point_index)
{
    if (g_lChID == 0) return;

    return document.Tvs.GetPolygon8DataY(g_lChID, stream_index, polygon_index, point_index);
}


/* Line (TVS_VERSION >= 1,6,57,122) */
// mode : 0(off), 1(edit), 2(erase), 3(display)
// thick : float
// color : hex
function tvs_SetLineMode(stream_index, line_index, mode, thick, color)
{
    if (g_lChID == 0) return;

    document.Tvs.SetLineColor(g_lChID, stream_index, line_index, color);
    document.Tvs.SetLineThickness(g_lChID, stream_index, line_index, thick);
    return document.Tvs.SetLineMode(g_lChID, stream_index, line_index, mode);
}

// draw_arrow : 0(normal line), 1(arrow)
function tvs_SetLineData(stream_index, line_index, draw_arrow, startx, starty, endx, endy)
{
    if (g_lChID == 0) return;

    return document.Tvs.SetLineData(g_lChID, stream_index, line_index, draw_arrow, startx, starty, endx, endy);
}

// point_index : 0(start x), 1(end x)
function tvs_GetLineDataX(stream_index, line_index, point_index)
{
    if (g_lChID == 0) return;

    return document.Tvs.GetLineDataX(g_lChID, stream_index, line_index, point_index);
}

// point_index : 0(start y), 1(end y)
function tvs_GetLineDataY(stream_index, line_index, point_index)
{
    if (g_lChID == 0) return;

    return document.Tvs.GetLineDataY(g_lChID, stream_index, line_index, point_index);
}
