
/*****************************************************************************
 * AJAX 관련 함수
 *****************************************************************************/

function ajaxGet(pageURL)
{
    var page = null;

    $.ajax({
        url: pageURL,
        type: 'GET',
        dataType: 'text',
        timeout: 1000,
        async: false,
//        error: function(){
//            alert('Error: Loading Page(' + pageURL + ')');
//        },
        success: function(data) {
			page = data;
        }
    });

	return page;
}

function ajaxGetAsync(pageURL)
{
    var page = null;

    $.ajax({
        url: pageURL,
        type: 'GET',
        dataType: 'text',
        timeout: 100,
        async: true,
//        error: function(){
//            alert('Error: Loading Page(' + pageURL + ')');
//        },
        success: function(data) {
			page = data;
        }
    });

	return page;
}

function ajaxGetAsyncCb(pageURL, params, dcb, cb)
{
    var page = null;

    $.ajax({
        url: pageURL,
        type: 'GET',
        dataType: 'text',
        timeout: 1000,
        async: true,
//        error: function(){
//            alert('Error: Loading Page(' + pageURL + ')');
//        },
        success: function(data) {
            if(data) {
                if (cb != undefined) {
                    if (dcb != undefined) {
                        dcb(pageURL, params, data, cb);
                    }
                    else {
                        cb(data);
                    }
                }
            }
        }
    });
}

function ajaxPost(toURL, toData)
{
    var result = false;

    $.ajax({
        url: toURL,
        type: 'POST',
        dataType: 'text',
        timeout: 1000,
//        async: false,
		data: toData,
//        error: function(){
//            alert('Error: Post failed.(' + pageURL + ')');
//        },
        success: function(data) {
			result = true;
        }
    });

	return result;
}

function ajaxPostFile(toURL, toData)
{
    var result = false;

    $.ajax({
        url: toURL,
        type: 'POST',
        contentType: false,
        processData: false,
        data: toData,
		timeout: 5000,
        success: function(data) {
            result = true;
        },
		error: function() {
		}
    });

    return result;
}

/*****************************************************************************
 * Setup 관련 함수
 *****************************************************************************/

function readAuthInfo()
{
	var url = "";
	var data;
	var setupArray = new Array();


	url = "/httpapx/ReadParam?action=getauth";

	data = ajaxGet(url);

	if(data) {
		var lines = data.split("\n");
		for(i = 0; i < lines.length; i++) {
			var token = lines[i].split("=");
			if(token.length != 2)
				break;
			setupArray[token[0]] = token[1];
			//console.log(token[0] + " = " + token[1]);
		}
	}

	return setupArray;
}

function readSetupByPage(pageName)
{
	var url = "";
	var data;
	var setupArray = new Array();


	url = "/httpapx/ReadParam?action=readpage&page=" + pageName;

	data = ajaxGet(url);

	if(data) {
		var lines = data.split("\n");
		for(i = 0; i < lines.length; i++) {
			var token = lines[i].split("=");			
			if(token.length == 2){
    			setupArray[token[0]] = token[1];
    		}
			else if(token.length == 1){
    			setupArray[token[0]] = "";
    		}
    		else{
                var n = lines[i].search("="); 
                var res = lines[i].substring(n+1);
    			setupArray[token[0]] = res;
    		}
		}
	}

	return setupArray;
}

function readSetupByKey(key)
{
	var url = "";
	var data;
	var result = "";

	url ="/httpapx/ReadParam?action=readparam&" + key + "=0";

	data = ajaxGet(url);

	if(data) {
		var lines = data.split("\n");
		var token = lines[0].split("=");
		if(token.length != 2)
			return result;

		if(token[0] == key)
			result = token[1];
	}

	return result;
}

function readSetupByKey2(key) // Multi Line
{
	var url = "";
	var data;
	var result = "";

	url = "/httpapx/ReadParam?action=readparam&" + key + "=0";

	data = ajaxGet(url);

	if(data) {
		var token = data.split("=");
		if (token.length != 2)
			return result;

		if (token[0] == key)
			result = token[1];
	}

	return result;
}

function __dcb_readSetupByKeyAsync(url, key, data, cb)
{
    var result = "";

    if (data) {
        var lines = data.split("\n");
        var token = lines[0].split("=");
        if(token.length != 2)
            cb(result);

        if(token[0] == key)
            result = token[1];
    }

    cb(result);
}

function readSetupByKeyAsync(key, cb)
{
    var url = "";

    url ="/httpapx/ReadParam?action=readparam&" + key + "=0";

    ajaxGetAsyncCb(url, key, __dcb_readSetupByKeyAsync, cb);
}

function readSetupByKeyArray(keyArray)
{
	//alert("readSetupByKeyArray is not implemented");
}

function readDefaultByKey(key)
{
	var url = "";
	var data;
	var result = "";

	url ="/httpapx/ReadParam?action=readdefault&" + key + "=0";

	data = ajaxGet(url);

	if(data) {
		var lines = data.split("\n");
		var token = lines[0].split("=");
		if(token.length != 2)
			return result;

		if(token[0] == key)
			result = token[1];
	}

	return result;
}


function writeSetup(params)
{
	var url;

	url = "/httpapx/WriteParam?action=writeparam&" + params;

	return ajaxGet(url);
}

function writeSetupAsync(params)
{
	var url;

	url = "/httpapx/WriteParam?action=writeparam&" + params;
	
	return ajaxGetAsync(url);
}

function writeState(params)
{
	var url;

	url = "/httpapx/SetState?action=setoutput&" + params;

	return ajaxGet(url);
}

function writeStateAsync(params)
{
	var url;

	url = "/httpapx/SetState?action=setoutput&" + params;

	return ajaxGetAsync(url);
}



/******************************************************************************
 * State 관련 함수
 *****************************************************************************/

function _readState(params)
{
	var url = "";
	var data;
	var stateArray = new Array();

	url = "/httpapx/GetState?" + params;

	data = ajaxGet(url);

	if(data) {
		var lines = data.split("\n");
		for(i = 0; i < lines.length; i++) {
			var token = lines[i].split("=");
			if(token.length != 2)
				break;
			stateArray[token[0]] = token[1];
		}
	}

	return stateArray;
}

function __dcb_readStateAsync(url, params, data, cb)
{
    var stateArray = new Array();

	if(data) {
		var lines = data.split("\n");
		for(i = 0; i < lines.length; i++) {
			var token = lines[i].split("=");
			if(token.length != 2)
				break;
			stateArray[token[0]] = token[1];
		}
	}

    cb(stateArray);
}

function _readStateAsync(params, cb)
{
	var url = "";

	url = "/httpapx/GetState?" + params;

	ajaxGetAsyncCb(url, params, __dcb_readStateAsync, cb);
}

function _readData(params)
{
	var url = "";
	var data;

	url = "/httpapx/GetState?" + params;

	return ajaxGet(url);

}

function readRateState()
{
	var result;

	result = _readState(
			"action=getrate&GRS_VENCFRAME1=0&GRS_VENCBITRATE1=0&GRS_VENCFRAME2=0&GRS_VENCBITRATE2=0&GRS_VENCFRAME3=0&GRS_VENCBITRATE3=0&GRS_VENCFRAME4=0&GRS_VENCBITRATE4=0&GRS_AENCBITRATE1=0&GRS_ADECBITRATE1=0&GRS_ADECALGORITHM1=0&GRS_ADECSAMPLERATE1=0");

	return result;
}

function readRateState_SCoding()
{
	var result;

	result = _readState(
			"action=getrate&GRS_VENCFRAME1=0&GRS_VENCBITRATE1=0&GRS_VENCFRAME2=0&GRS_VENCBITRATE2=0&GRS_VENCFRAME3=0&GRS_VENCBITRATE3=0&GRS_VENCFRAME4=0&GRS_VENCBITRATE4=0&GRS_AENCBITRATE1=0&GRS_ADECBITRATE1=0&GRS_ADECALGORITHM1=0&GRS_ADECSAMPLERATE1=0&GRS_VENCIDR1=0&GRS_VENCPIDR1=0&GRS_VENCPBASE1=0&GRS_VENCIDR2=0&GRS_VENCPIDR2=0&GRS_VENCPBASE2=0&GRS_VENCIDR3=0&GRS_VENCPIDR3=0&GRS_VENCPBASE3=0&GRS_VENCIDR4=0&GRS_VENCPIDR4=0&GRS_VENCPBASE4=0");

	return result;
}


function readInputState()
{
	var result;

	result = _readState(
			"action=getinput&GIS_SENSOR1=0&GIS_SENSOR2=0&GIS_SENSOR3=0&GIS_SENSOR4=0&GIS_SENSOR5=0&GIS_MOTION1=0&GIS_MOTION2=0&GIS_MOTION3=0&GIS_MOTION4=0&GIS_VIDEOLOSS1=0&GIS_VIDEOLOSS2=0&GIS_VIDEOLOSS3=0&GIS_VIDEOLOSS4=0&GIS_ALARM1=0&GIS_ALARM2=0&GIS_ALARM3=0&GIS_ALARM4=0&GIS_RECORD1=0&GIS_AIRWIPER=0");

	return result;
}

function readInputStateAsync(cb)
{
    var result;

	result = _readStateAsync(
			"action=getinput&GIS_SENSOR1=0&GIS_SENSOR2=0&GIS_SENSOR3=0&GIS_SENSOR4=0&GIS_SENSOR5=0&GIS_MOTION1=0&GIS_MOTION2=0&GIS_MOTION3=0&GIS_MOTION4=0&GIS_VIDEOLOSS1=0&GIS_VIDEOLOSS2=0&GIS_VIDEOLOSS3=0&GIS_VIDEOLOSS4=0&GIS_ALARM1=0&GIS_ALARM2=0&GIS_ALARM3=0&GIS_ALARM4=0&GIS_RECORD1=0&GIS_AIRWIPER=0", cb);

    return;
}

function readRateStateDec()
{
	var result;

	result = _readState(
			"action=getrate&GRS_VDECALGORITHM1=0&GRS_VDECFRAME1=0&GRS_VDECBITRATE1=0&GRS_VDECALGORITHM2=0&GRS_VDECFRAME2=0&GRS_VDECBITRATE2=0&GRS_VDECALGORITHM3=0&GRS_VDECFRAME3=0&GRS_VDECBITRATE3=0&GRS_VDECALGORITHM4=0&GRS_VDECFRAME4=0&GRS_VDECBITRATE4=0&GRS_AENCBITRATE1=0&GRS_ADECBITRATE1=0&GRS_ADECALGORITHM1=0&GRS_ADECSAMPLERATE1=0");

	return result;
}

function readRateState410()
{
	var result;

	result = _readState(
			"action=getrate&GRS_VENCFRAME1=0&GRS_VENCBITRATE1=0&GRS_VENCFRAME2=0&GRS_VENCBITRATE2=0&GRS_VENCFRAME3=0&GRS_VENCBITRATE3=0&GRS_VENCFRAME4=0&GRS_VENCBITRATE4=0&GRS_VENCFRAME5=0&GRS_VENCBITRATE5=0&GRS_AENCBITRATE1=0&GRS_ADECBITRATE1=0&GRS_ADECALGORITHM1=0&GRS_ADECSAMPLERATE1=0");

	return result;
}

function readRateStateA400()
{
    var result;

    result = _readState(
            "action=getrate&GRS_VENCFRAME1=0&GRS_VENCBITRATE1=0&GRS_VENCFRAME2=0&GRS_VENCBITRATE2=0&GRS_VENCFRAME3=0&GRS_VENCBITRATE3=0&GRS_VENCFRAME4=0&GRS_VENCBITRATE4=0&GRS_VENCFRAME5=0&GRS_VENCBITRATE5=0&GRS_VENCFRAME6=0&GRS_VENCBITRATE6=0&GRS_VENCFRAME7=0&GRS_VENCBITRATE7=0&GRS_VENCFRAME8=0&GRS_VENCBITRATE8=0&GRS_VENCFRAME9=0&GRS_VENCBITRATE9=0&GRS_VENCFRAME10=0&GRS_VENCBITRATE10=0");

    return result;
}

function readSerialState()
{
	var result;

	result = _readState(
			"action=getrate&GRS_STXRATE1=0&GRS_SRXRATE1=0&GRS_STXRATE2=0&GRS_SRXRATE2=0");

	return result;
}

function readSystemState()
{
	var result;

	result = _readState("action=getinfo&GIN_READ=0");

	return result;
}

function readSystemConn()
{
	var result;

	result = _readData("action=getinfo&GIN_CONN=0");

	return result;
}

function readSystemLog()
{
	var result;

	result = _readData("action=getlog&GLO_READ=0");

	return result;
}

function readSystemNewLog(params)
{
	var result;
	var url = "";

	url = "action=getnewlog&GNL_READ=" + params; 

	result = _readData(url);

	return result;
}

function runGeneralCommand(str)
{
    var cmd = "action=command&Command=" + str;
	var result;

	result = _readData(cmd);

	return result;
}

function stopGeneralCommand(str)
{
    var cmd = "action=command&Stop=" + str;
	var result;

	result = _readData(cmd);

	return result;
}

/******************************************************************************
 * Style 관련 함수
 *****************************************************************************/

function initLoading()
{
	$("#PopupMessage").html("<img src=\"img/loading.gif\"></img>").hide();
}

function showLoading()
{
	var popup = $("#PopupMessage");

	width = popup.width();
	height = popup.height();
	
	popup.css("position","absolute")
		.css("z-index","10001")
		.css("top","30%")
		.css("left","50%")
		.css("margin-left", -1 * width / 2)
		.css("margin-top", -1 * height / 2);

	popup.show();
}

function hideLoadingDelay(delay)
{
	setTimeout(function() { $("#PopupMessage").hide(); }, delay);
}

function beautifyButtons()
{
	// change design
	$(":input[type=radio]").button({ icons: { primary: "ui-icon-blank" } });
	$(":input[type=checkbox]").button({ icons: { primary: "ui-icon-blank" } });
	$("label.ui-button" ).css("height", "22px").css("font-size", "12px").css("padding-top", ".2em");
    $("a.ui-slider-handle").button({ icons: { primary: "ui-icon-blank" } , text: false });

	// apply default button image
	$(":input[type=radio]").button({ icons: { primary: "ui-icon-blank" } });
	$(":input[type=radio]:checked").button({ icons: { primary: "ui-icon-radio-on" } });

	$(":input[type=checkbox]").button({ icons: { primary: "ui-icon-blank" } });
	$(":input[type=checkbox]:checked").button({ icons: { primary: "ui-icon-check" } });

   	// install change event for icon change
	$( ":input[type=radio]" ).change(function(event) {
		id = $(this).attr("id");
		name = $(this).attr("name");
		$(":input[name=" + name + "]").button({ icons: { primary: "ui-icon-blank" } });
		$("#" + id).button({ icons: { primary: "ui-icon-radio-on" } });
	});
	
	$( ":input[type=checkbox]" ).change(function(event) {
		id = $(this).attr("id");
		if($("#" + id).prop("checked") == true){
    		$("#" + id).button({ icons: { primary: "ui-icon-check" } });
        }
        else{
    		$("#" + id).button({ icons: { primary: "ui-icon-blank" } });
        }
    	initLanguage();
	});
	
}

function beautifyRadioButtonsInGroup(groupId)
{
	// change design
	$(groupId + " :radio").button({ icons: { primary: "ui-icon-blank" } });
	$("label.ui-button" ).css("height", "22px").css("font-size", "12px").css("padding-top", ".2em");

	// apply default button image
	$(groupId + " :radio").button({ icons: { primary: "ui-icon-blank" } });
	$(groupId + " :radio:checked").button({ icons: { primary: "ui-icon-radio-on" } });

   	// install change event for icon change
	$( groupId + " :radio" ).change(function(event) {
		id = $(this).attr("id");
		name = $(this).attr("name");
		$(groupId + " :input[name=" + name + "]").button({ icons: { primary: "ui-icon-blank" } });
		$("#" + id).button({ icons: { primary: "ui-icon-radio-on" } });
	});
}

function beautifyButtonsForRadio()
{
	// change design
	$(":input[type=radio]").button({ icons: { primary: "ui-icon-blank" } });
	$("label.ui-button" ).css("height", "22px").css("font-size", "12px").css("padding-top", ".2em");

	// apply default button image
	$(":input[type=radio]").button({ icons: { primary: "ui-icon-blank" } });
	$(":input[type=radio]:checked").button({ icons: { primary: "ui-icon-radio-on" } });

   	// install change event for icon change
	$( ":input[type=radio]" ).change(function(event) {
		id = $(this).attr("id");
		name = $(this).attr("name");
		$(":input[name=" + name + "]").button({ icons: { primary: "ui-icon-blank" } });
		$("#" + id).button({ icons: { primary: "ui-icon-radio-on" } });
	});
		
}

function beautify() {
    $("select").customSelect();
}

function disableButton(obj)
{
	obj.attr("disabled", "disabled");
}

function enableButtonDelay(obj, delay)
{
	setTimeout(function() { obj.removeAttr("disabled"); }, delay);
}

const SUPPORT_MULTI_DEVICE_NONE_TYPE = 0 ;
const SUPPORT_MULTI_DEVICE_DETECTION_CAM_TYPE = 1 ;
const SUPPORT_MULTI_DEVICE_PTZ_TYPE = 2 ;
var support_multi_device_type = SUPPORT_MULTI_DEVICE_NONE_TYPE ;
var support_multi_device_ipaddress = "";

/*****************************************************************************/
function loadSetupTemplate(product)
{
	url = "/product/" + product + "/template.html";
	contents = ajaxGet(url);
	$("body").html(contents);
	
	var setup = readSetupByPage("setuptemplate");
    var mode = setup["SYS_MODE"];
    var ptztype = setup["SYS_PTZ_TYPE"];
    var boardid = setup["SYS_BOARDID"];
    var moduletype = setup["SYS_MODULE_TYPE"];
	var moduledetail = setup["SYS_MODULE_DETAIL"];
	var ft_ir4 = setup["SYS_FTCAMERA_IR4"]; 	
	var support_location_osd = (((boardid & 0xF000) == 0xB000) || ((boardid & 0xF000) == 0x9000) || ((boardid & 0xF00F) == 0xA004) || ((boardid & 0xF000) == 0xE000)) && (7 == ptztype) ; // (3519A || 3519 || CV2) && TRUEN ZOOM MODULE
	
	if( ((boardid & 0xF000) == 0x9000 || (boardid & 0xF000) == 0xA000 || (boardid & 0xF000) == 0xB000 ) && ( (4 == ft_ir4) )  ) {
	// if( ((boardid & 0xF000) == 0x9000 || (boardid & 0xF000) == 0xA000 || (boardid & 0xF000) == 0xB000 ) && ( (4 == ft_ir4) || (3 == ft_ir4) || (2 == ft_ir4) )  ) {
		support_multi_device_type = SUPPORT_MULTI_DEVICE_DETECTION_CAM_TYPE ;
	}
	else if(0xB001 == boardid) {  // A400
		support_multi_device_type = SUPPORT_MULTI_DEVICE_PTZ_TYPE ;
	}
	else {
		;
	}

	$("[name=li-systemptzcamera]").css("display", "none");
    $("[name=li-detectioncam-systemptzcamera]").css("display", "none");

	if(SUPPORT_MULTI_DEVICE_DETECTION_CAM_TYPE == support_multi_device_type) {
		$("[name=li-detectioncam]").css("display", "");
		$("[name=folderble-ptzcam]").css("display", "");
		
		$("[name=li-ptzcam-videoaudio]").css("display", "none");
		$("[name=li-ptzcam-videooutput]").css("display", "none");
		$("[name=li-ptzcam-imageschedule]").css("display", "none");
		$("[name=li-ptzcam-imageircontrol]").css("display", "none");
		$("[name=li-ptzcam-eventsilence]").css("display", "none");
		$("[name=li-ptzcam-eventshock]").css("display", "none");
		$("[name=li-ptzcam-eventsensor]").css("display", "none");
		$("[name=li-ptzcam-eventalarm]").css("display", "none");		
		$("[name=li-ptzcam-record]").css("display", "none");
		$("[name=li-ptzcam-device]").css("display", "none");
		$("[name=li-ptzcam-ptz]").css("display", "none");
		$("[name=li-ptzcam-systemdirectioncamera]").css("display", "none");
		$("[name=li-ptzcam-eventva]").css("display", "");
		$("[name=li-detectioncam-systemtiltposition]").css("display", "");
		$("[name=li-detectioncam-systemexttracking]").css("display", "");
		$("[name=li-detectioncam-systemptzcamera]").css("display", "");
		$("[name=li-systemptzcamera]").css("display", "none");
		
		
		var directioncam = readSetupByPage("directioncam");
        var protocol = location.protocol;
        if (protocol == "https:") {
            support_multi_device_ipaddress = protocol + "//" + directioncam["DIRECTIONCAM_IP"] + ":" + directioncam["DIRECTIONCAM_HTTPS_PORT"] + "/" ;		
        } else {
            support_multi_device_ipaddress = "http://" + directioncam["DIRECTIONCAM_IP"] + ":" + directioncam["DIRECTIONCAM_HTTP_PORT"] + "/" ;		
        }
	}
	else if(SUPPORT_MULTI_DEVICE_PTZ_TYPE == support_multi_device_type) {		
		$("[name=li-detectioncam]").css("display", "");
		$("[name=folderble-ptzcam]").css("display", "");

		$("[name=li-ptzcam-videoaudio]").css("display", "");
		$("[name=li-ptzcam-videooutput]").css("display", "");
		$("[name=li-ptzcam-imageschedule]").css("display", "");
		$("[name=li-ptzcam-imageircontrol]").css("display", "");
		$("[name=li-ptzcam-eventsilence]").css("display", "");
		$("[name=li-ptzcam-eventshock]").css("display", "");
		$("[name=li-ptzcam-eventsensor]").css("display", "");
		$("[name=li-ptzcam-eventalarm]").css("display", "");
		$("[name=li-ptzcam-record]").css("display", "");
		$("[name=li-ptzcam-device]").css("display", "");
		$("[name=li-ptzcam-ptz]").css("display", "");		
		$("[name=li-ptzcam-systemdirectioncamera]").css("display", "");
		$("[name=li-detectioncam-systemtiltposition]").css("display", "none");
		$("[name=li-detectioncam-systemexttracking]").css("display", "none");
		$("[name=li-ptzcam-eventva]").css("display", "none");
		$("[name=li-ptzcam-ptzintelligent]").css("display", "none");
		$("[name=li-detectioncam-systemptzcamera]").css("display", "none");
		$("[name=li-systemptzcamera]").css("display", "");        

		var directioncam = readSetupByPage("directioncam");		
        var protocol = location.protocol;
        if (protocol == "https:") {
            support_multi_device_ipaddress = protocol + "//" + directioncam["DIRECTIONCAM_IP"] + ":" + directioncam["DIRECTIONCAM_HTTPS_PORT"] + "/" ;		
        } else {
            support_multi_device_ipaddress = "http://" + directioncam["DIRECTIONCAM_IP"] + ":" + directioncam["DIRECTIONCAM_HTTP_PORT"] + "/" ;		
        }
	}
	else {
		$("[name=li-detectioncam]").css("display", "none");
		$("[name=folderble-ptzcam]").css("display", "none");
	}
        
	if(mode == 0){ // encoder system
	    $("[name=li-networkremote]").css("display", "none");
		$("[name=li-videooutputosd]").css("display", "none");

		if((boardid & 0xF000) == 0x4000 && setup["SYS_ISIPCAM"] == 0) 
		{
			// amba encoder does not have composite
			if(setup["SYS_AMBA_SUPPORT_HDMI_BCS"] == 0)
			{
				$("[name=li-image]").css("display", "none");	
			}
		}
	}
	else{ // decoder system
		if((boardid & 0xF000) != 0x3000 && (boardid & 0xF000) != 0xB000) {
			$("[name=li-record]").css("display", "none");	
			$("[name=li-videooutputosd]").css("display", "none");
		}
		$("[name=li-image]").css("display", "none");	
	    $("[name=li-videoencode]").css("display", "none");	
	    $("[name=li-networkoneway]").css("display", "none");	
	    $("[name=li-networkemail]").css("display", "none");	
	    $("[name=li-networkftp]").css("display", "none");	
	    $("[name=li-eventmotion]").css("display", "none");	
	    $("[name=li-ptzgroup]").css("display", "none");
	    $("[name=li-ptzadvanced]").css("display", "none");
	    $("[name=li-networkgoogledrive]").css("display", "none");	
	}
	
	if(ptztype != 7){
	    $("[name=li-ptzpattern]").css("display", "none");
	    $("[name=li-ptzswing]").css("display", "none");
	    if(moduletype == HITACHI_MODULE){
    	    $("[name=li-imagemask]").css("display", "none");    
	    }
	}
	if(!support_location_osd) {
		$("[name=li-ptzlocationosd]").css("display", "none");
		$("[name=li-ptzcam-ptzlocationosd]").css("display", "none");
	}
	
	if(setup["SYS_ISIPCAM"] == 0){
	    $("[name=li-imageschedule]").css("display", "none");    
	    $("[name=li-imagemask]").css("display", "none");    
    }

	if(mode == 0 && ((boardid & 0xF000) == 0x3000 || (boardid & 0xF000) == 0xB000) && setup["SYS_ISIPCAM"] == 0)
	    $("[name=li-imagemask]").css("display", "");    

	if(moduledetail == MODULE_TI_TECHWIN_W5)
	{
	    $("[name=li-imageschedule]").css("display", "none");    
	    $("[name=li-imagemask]").css("display", "none");    
	    $("[name=li-videooutput]").css("display", "none");    
	}

	if((boardid & 0xF000) == 0x3000){
	    $("[name=li-eventsilence]").css("display", "none");    
	    if(boardid == 0x3003){
	        $("[name=li-eventsensor]").css("display", "none");
	        $("[name=li-systemsystemmode]").css("display", "none");
	    }

        if(boardid == 0x3004) {
            $("[name=li-record]").css("display", "none");    
        }

		// Decoder Mode && Smooth Switching List Enable
		if((setup["SYS_MODE"] == 1) && (setup["SYS_IS_USE_SMOOTH_SW"] == 1)) {
			$("[name=li-networklist]").css("display", "");
		}
		else {
			$("[name=li-networklist]").css("display", "none");
		}
    }
	else if((boardid & 0xF000) == 0xB000){
        
        if(setup["SYS_ISIPCAM"] != 0){
	        $("[name=li-systemsystemmode]").css("display", "none");
	    }
	    else{
            if ((boardid & 0xFFFF) == 0xB005) { // TCS-1600
                $("[name=li-systemsystemmode]").css("display", "none");
            }
            $("[name=li-imagegeneral]").css("display", "none");
	    }
	    
		// Decoder Mode && Smooth Switching List Enable
		if((setup["SYS_MODE"] == 1) && (setup["SYS_IS_USE_SMOOTH_SW"] == 1)) {
			$("[name=li-networklist]").css("display", "");
		}
		else {
			$("[name=li-networklist]").css("display", "none");
		}
        if(boardid == 0xB001) { // IPM-A400
    	    $("[name=li-videoaudio]").css("display", "none");    
	        $("[name=li-videooutput]").css("display", "none");    
            $("[name=li-imageschedule]").css("display", "none");    
	        $("[name=li-eventsilence]").css("display", "none");    
	        $("[name=li-record]").css("display", "none");    
	        $("[name=li-device]").css("display", "none");    
	        $("[name=li-ptz]").css("display", "none");    
        
        }
	}
    else if((boardid & 0xF000) == 0xD000 || (boardid & 0xF000) == 0xE000){
	    $("[name=li-systemsystemmode]").css("display", "none");
		$("[name=li-networklist]").css("display", "none");
        $("[name=li-networkssl]").css("display", "none");
		if((boardid & 0xFFF000) == 0x10D000) {	// Scale AQ
			$("[name=li-record]").css("display", "none");
		}
    }
	else{
	    $("[name=li-systemsystemmode]").css("display", "none");
		$("[name=li-networklist]").css("display", "none");
	}
		
	$("[name=li-networkmulticast]").css("display", "none");
	if(boardid == 0x1003 || boardid == 0x1007){ // TCS-410
	    $("[name=li-networkoneway]").css("display", "none");
	    $("[name=li-ptzgroup]").css("display", "none");
	    $("[name=li-ptzadvanced]").css("display", "none");
	    $("[name=li-eventsilence]").css("display", "none");
	    if(setup["SYS_IS_MULTICAST_SPECIFIC"] == 1){
	        $("[name=li-networkmulticast]").css("display", "");
	    }
	    $("[name=li-ptzpositionosd]").css("display", "none");
	    $("[name=li-imagemask]").css("display", "");    
	}
	
    if((parseInt(setup["SYS_NOT_SUPPORT_FUNCTION"]) & parseInt(1<<PTZ_FUNCTION)) == parseInt(1<<PTZ_FUNCTION)){
	    $("[name=li-ptz]").css("display", "none");    
	    $("[name=li-device]").css("display", "none");    
    }
    else if(parseInt(setup["SYS_IS_FIR2"]) == 1){
	    $("[name=li-device]").css("display", "none");    
    }
    
    if((parseInt(setup["SYS_NOT_SUPPORT_FUNCTION"]) & parseInt(1<<RECORD_FUNCTION)) == parseInt(1<<RECORD_FUNCTION)){
	    $("[name=li-record]").css("display", "none");    
    }
    
	if(parseInt(setup["SYS_NUMOFSENSOR"]) == 0){
	    $("[name=li-eventsensor]").css("display", "none");    
    }
	if(parseInt(setup["SYS_NUMOFALARM"]) == 0){
	    $("[name=li-eventalarm]").css("display", "none");    
    }
	
	if((boardid & 0xF000) != 0x1000 && (boardid & 0xF000) != 0x3000 && (boardid & 0xF000) != 0xB000 && (boardid & 0xF000) != 0x5000 && (boardid & 0xF000) != 0x9000 && (boardid & 0xF000) != 0xA000 && (boardid & 0xF000) != 0xD000 && (boardid & 0xF000) != 0xE000 || mode != 0){
		$("[name=li-ptzpositionosd]").css("display", "none");
    }
	
	if((setup["SYS_IS_CTECNEW"] == 1 || setup["SYS_CYNIX_XIR"] == 1 || (setup["SYS_FTCAMERA"] == 1 && (boardid & 0x000F) == 0x0002 || boardid == 0x29000 || boardid == 0x49000 || boardid == 0x69000 || boardid == 0x89000 || boardid == 0x129000 || boardid == 0x11A004 || boardid == 0x12A004 || (boardid & 0xF0F00F) == 0x10B004) || (setup["SYS_IS_HOUSINGSPECIFIC"] == 1) || (moduletype == NXP_ISP_MODULE && ptztype == 1) || ((moduletype == HI_ISP_MODULE || moduletype == S5LM55_ISP_MODULE || moduletype == CV2_ISP_MODULE) && ptztype == 1 && (setup["SYS_ZOOMRATIO"] > 0x5)) || (moduletype == CV2_ISP_MODULE && ptztype == 7)) && (setup["SYS_IS_FIRHOUSING_SPECIFIC"] != 1)){	
	}
	else{
	    $("[name=li-imageircontrol]").css("display", "none");
	}

	if(moduletype == HI3519_ISP_MODULE && parseInt(setup["SYS_FTCAMERA_FA3"]) != 0)
	{
	    $("[name=li-imageircontrol]").css("display", "none");
	}

	if(setup["VID_FAKE_DEMIST"] == 1 && moduledetail == MODULE_SONY_EH6300){	
	}
	else{
	    $("[name=li-imagedemist]").css("display", "none");
	}

    if(setup["SYS_IS_PLCSPECIFIC"] == 1){
	    $("[name=li-ptzpositionosd]").css("display", "none");
    }
    else{
        $("[name=li-deviceplc]").css("display", "none");	
    }

	if(setup["SYS_IS_BOOKISPECIFIC"] != 1){
		$("[name=li-devicebooki]").css("display", "none");
	}
    
    if(ptztype == 1 && (moduletype == 1 || moduletype == 2 || moduletype == 3 || moduletype == 4 || moduletype == 6 || moduletype == 9) ){
    }
    else{
	    $("[name=li-devicefan]").css("display", "none");
    }
    
	if (moduletype != PANASONIC_MODULE && setup["SYS_USE_AUTO_PTZ"] != 1){
        $("[name=li-ptztracking]").css("display", "none");	
    }
    
   	if ((((setup["SYS_BOARDID"] & 0xF000) == 0x1000 || (setup["SYS_BOARDID"] & 0xF000) == 0x4000 || (setup["SYS_BOARDID"] & 0xF000) == 0x5000 || (setup["SYS_BOARDID"] & 0xF000) == 0x6000 || (setup["SYS_BOARDID"] & 0xF000) == 0x9000 || (setup["SYS_BOARDID"] & 0xF000) == 0xA000) && (setup["SYS_ISIPCAM"] == 1)) ||
        ((setup["SYS_BOARDID"] & 0xF000) == 0xB000 || (setup["SYS_BOARDID"] & 0xF000) == 0xD000 || (setup["SYS_BOARDID"] & 0xF000) == 0xE000)){
   	    if(parseInt(setup["VA_LicenseValid"]) == 1){
            $("[name=li-eventva]").css("display", "");	
   	    }
   	    else{
            $("[name=li-eventva]").css("display", "none");	
        }
   	}
   	else{
        $("[name=li-eventva]").css("display", "none");	
    }

    if (setup["SYS_IS_IIST_SPECIFIC"] == 1) {
        $("[name=li-eventflamedetection]").css("display", "");
    } else {
        $("[name=li-eventflamedetection]").css("display", "none");
    }

	if( ((boardid & 0xF000) == 0x4000 || (boardid & 0xF000) == 0x6000 || boardid == 0x1003 || boardid == 0x1007) ||
		mode == 1) {
		$("[name=li-eventosd]").css("display", "none");
	}

	if(boardid == 0x1003 || boardid == 0x1007) {
		$("[name=li-eventhttpaction]").css("display", "none");

	}
	if((boardid == 0x1A002 || boardid == 0x2A002 || boardid == 0x3A002 || boardid == 0x4A002 || boardid == 0x5A002 || boardid == 0x6A002 || boardid == 0xB003 || boardid == 0x1B003 ||  boardid == 0x2B003) && (setup["SYS_IS_FIRHOUSING_SPECIFIC"] != 1)) {
          $("[name=li-imageircontrol]").css("display", "");
	}


//	if((moduletype == NXP_ISP_MODULE || moduletype == HI_ISP_MODULE ||  moduletype == HI3519_ISP_MODULE) && ptztype == 1){
//    	$("[name=li-imagemask]").css("display", "none");    
//    }
    
    if(setup["SYS_IS_RAPIER_SPECIFIC"] == 1){
	    $("[name=li-image]").css("display", "none");
	    $("[name=li-ptzpositionosd]").css("display", "none");
	}

    if(setup["SYS_IS_SEMOCON_SPECIFIC"] == 1){
	    $("[name=li-devicehousing]").css("display", "");
	    $("[name=li-ptzpattern]").css("display", "");
//		$("[name=li-imagemask]").css("display", "none");    
//	    $("[name=li-ptzpositionosd]").css("display", "none");
	}
	else {
	    $("[name=li-devicehousing]").css("display", "none");
	}

	if(setup["SYS_IS_FIRHOUSING_SPECIFIC"] == 1){
		$("[name=li-deviceirhousing]").css("display", "");
		$("[name=li-ptzgeneral]").css("display", "none");
	} else {
		$("[name=li-deviceirhousing]").css("display", "none");
	}
	
    if(parseInt(setup["SYS_GDS_DISABLE"]) == 1){
	    $("[name=li-networkgoogledrive]").css("display", "none");
    }	

    if(parseInt(setup["SYS_LPR_ITSTECH"]) == 1 ||
       parseInt(setup["SYS_USE_LPR"]) == 1 ||
       parseInt(setup["SYS_USE_GIT_LPR"]) == 1){
	    $("[name=li-eventlpr]").css("display", "");
    }
    else{
	    $("[name=li-eventlpr]").css("display", "none");
    }

	if(setup["SYS_IS_OBJECT_DETECT"] == 1) {
		$("[name=li-eventobjectdetection]").css("display", "");
	}
	else {
		$("[name=li-eventobjectdetection]").css("display", "none");
	}

    if(setup["SYS_FTCAMERA_IR4"] != 3 && setup["SYS_FTCAMERA_IR4"] != 4){
        $("[name=li-devicewiper]").css("display", "none");    
    }
    
    if( ((boardid & 0xF000) == 0x9000 || (boardid & 0xF000) == 0xA000 || (boardid & 0xF000) == 0xB000 ) && (setup["SYS_FTCAMERA_IR4"] == 4)  ) {
    // if( ((boardid & 0xF000) == 0x9000 || (boardid & 0xF000) == 0xA000 || (boardid & 0xF000) == 0xB000 ) && ( (setup["SYS_FTCAMERA_IR4"] == 4) || (setup["SYS_FTCAMERA_IR4"] == 3)  || (setup["SYS_FTCAMERA_IR4"] == 2)) ) {		
        $("[name=li-systemdirectioncamera]").css("display", "");
    }
    else {
        $("[name=li-systemdirectioncamera]").css("display", "none");
	}	
    
    if(setup["SYS_ISIPCAM"] == 0){
	    $("[name=li-ptzextptz]").css("display", "none");    
    }
	else if(((boardid & 0xF000) == 0x1000 || (boardid & 0xF000) == 0x5000) && ptztype != 1 && ptztype != 7){
	    $("[name=li-ptzextptz]").css("display", "");
	}
	else if(moduletype == NXP_ISP_MODULE && ptztype == 1){
	    $("[name=li-ptzextptz]").css("display", "");
	}
	else if((moduletype == HI_ISP_MODULE || moduletype == HI3519_ISP_MODULE || moduletype == HI3519A_ISP_MODULE) && ptztype == 1){
	    $("[name=li-ptzextptz]").css("display", "");
	}
    else if((moduletype == S5LM55_ISP_MODULE || moduletype == CV2_ISP_MODULE) && ptztype == 1){
	    $("[name=li-ptzextptz]").css("display", "");
    }
	else{
	    $("[name=li-ptzextptz]").css("display", "none");
	}
	
    if(parseInt(setup["SYS_WIFI_DEVICEDETECT"]) != 1){
	    $("[name=li-networkwifi]").css("display", "none");
	}

    if(setup["SYS_CLOUD_INTERCONNECTION"] != 1){
	    $("[name=li-networkcloud]").css("display", "none");    
    }
	
    if(parseInt(setup["SYS_FIXEDMASK_TEMPORARY"]) != 1){
	    $("[name=li-imagemask2]").css("display", "none");
	}
	
	if(parseInt(setup["SYS_ILLUMINATOR"]) != 1) {
		$("[name=li-deviceilluminator]").css("display", "none");
	}

	if(parseInt(setup["SYS_RTU_SPECIFIC"]) != 1) {
		$("[name=li-devicertu]").css("display", "none");
	}

	if(parseInt(setup["SYS_SHOCK_SENSOR_EXIST"]) != 1) {
		$("[name=li-eventshock]").css("display", "none");
	}

	if(parseInt(setup["SYS_NEWLOG"]) != 1) {
		$("[name=li-systemlog]").css("display", "none");
	}

	if(mode == 0 && parseInt(setup["SYS_USE_LIVE555"]) == 1) {
	} else {
		$("[name=li-networkrtspmulticast]").css("display", "none");
	}

	if(parseInt(setup["SYS_TTA_SECURITY_VERIFIED"]) == 1) {
		$("[name=li-networksnmp]").css("display", "none");
	}

	if(mode == 1 || parseInt(setup["SYS_USE_SRT"]) != 1) {
		$("[name=li-networksrt]").css("display", "none");
	}

    if(boardid == 0x3D011 || boardid == 0x4D011 || boardid == 0x5D011 || boardid == 0x6D011 || boardid == 0x9D011) { // S5L TN
    }
    else {
        $("[name=li-networklink]").css("display", "none");
    }

	if(ptztype != 7 || parseInt(setup["SYS_INTELLIGENT_PTZ"]) != 1) {
		$("[name=li-ptzintelligent]").css("display", "none");
	}

	if(boardid == 0x1003 || boardid == 0x1007) { // TCS-410 
		$("[name=li-eventuserdefinedevent]").css("display", "none");
	}

	if(setup["SYS_ISIPCAM"] == 1) {
		$("[name=li-deviceled]").css("display", "none");
	}

	if((boardid & 0xFFFF) == 0xB000 || (boardid & 0xFFFF) == 0xB005) { // TCS-8500 or TCS-1600
	    $("[name=li-deviceled]").css("display", "none");
		if(boardid == 0x1B000) { // TCS-8500A
    	    $("[name=li-device]").css("display", "none");
    	}
    }

    if(boardid == 0xB001) {
        $("[name=li-systemtiltposition]").css("display", "");
		$("[name=li-systemexttracking]").css("display", "");		
    } else {
        $("[name=li-systemtiltposition]").css("display", "none");
		$("[name=li-systemexttracking]").css("display", "none");
    }

	if(setup["SYS_IS_DATATOYS_SPECIFIC"] == 1) {
		$("[name=li-deviceled]").css("display", "none");
	}

    if(setup["SYS_USE_GIT_LPR"] == 1) {
        $("[name=li-systemaudiofile]").css("display", "");
    } else {
        $("[name=li-systemaudiofile]").css("display", "none");
    }

	if(setup["SYS_IS_JIINTECH_SPECIFIC"] == 1) {
		$("[name=li-systemcrimemanager]").css("display", "");
	} else {
		$("[name=li-systemcrimemanager]").css("display", "none");
	}

	if(setup["SYS_IS_KEXMOLIT_SPEC"] == 1) {
		$("[name=li-systemmolit]").css("display", "");
	} else {
		$("[name=li-systemmolit]").css("display", "none");
	}

	if(setup["SYS_IS_AVEC_PTHOUSING_SPECIFIC"] == 1) {
		$("[name=li-imagemask]").css("display", "none");
	}
	if(setup["SYS_IS_HATTLELAND_SPEC"] == 1){
		$("[name=li-videoaudio]").css("display", "none");
		$("[name=li-eventsilence]").css("display", "none");
		$("[name=li-eventsensor]").css("display", "none");
		$("[name=li-eventva]").css("display", "none");
		$("[name=li-eventosd]").css("display", "none");
		$("[name=li-ptz]").css("display", "none");
		$("[name=li-videooutput]").css("display", "none");
		$("[name=li-device]").css("display", "none");    
		$("[name=li-eventhttpaction]").css("display", "none");
	}

    if(setup["SYS_REMOVE_DDNS"] == 1) {
        $("[name=li-networkddns]").css("display", "none");
    }
}

function isSupportMenu(menu)
{
	for(i = 0; i < T_SupportMenu.length; i++) {
		if(menu == T_SupportMenu[i])
			return true;
	}
	return false;
}
/*
function applyLanguage(lid)
{
	var table;
	var locale = [ "English", "Japanese", "Korean", ];

	table = ajaxGet("/resources/lang/LT_" + locale[lid] + ".tlf.json");

	if(table) {
		//console.log(table);
		LT = eval('(' + table + ')');

		for(var i = 0; i < LT.length; i++) {
			// IE9에서는 LT.length가 Chrome이나 FF보다 1더 많이 넘어온다.
			// 이를 해결하기 위해 null 체크
			if(LT[i] == null)
				break;
			$(".LAN_" + LT[i].id).text(LT[i].str);
			//console.log("#" + LT[i].id + " = " + LT[i].str);
		}
	}
}
*/

function initLanguage()
{
	var table;
	var locale = [ "English", "Japanese", "Korean", "Hebrew", "Russian", "Spanish", "Portuguese" , "Chinese", "German" , "Arabic", "Finnish", "Italian" , "Polish", "French", "Czech" , "Chinese_tw", "Turkish", "Dutch"];

	LID = readSetupByKey("SYS_LANGUAGE");

	table = ajaxGet("/resources/lang/LT_" + locale[LID] + ".tlf.json");

	if(table) {
		//console.log(table);
		LT = eval('(' + table + ')');

        if(1){
              $("[class*='LAN_']").each(function(i, d) {
             // n = n + 1;
              //console.log(d.className);
                var caption = d.className;
                id = parseInt(caption.substr(caption.indexOf("LAN_") + 4, 4), 10);               
			    $(this).text(LT[id].str);
              });
        }
        else{
    	    // 기존 로직
		    for(var i = 0; i < LT.length; i++) {
			    if(LT[i] == null)
				    break;
			    $(".LAN_" + LT[i].id).text(LT[i].str);
		    }
        }
	}
}

function initLanguage2()
{
	var table;
	var locale = [ "English", "Japanese", "Korean", "Hebrew", "Russian", "Spanish", "Portuguese" , "Chinese", "German" , "Arabic", "Finnish", "Italian" , "Polish", "French", "Czech" , "Chinese_tw", "Turkish", "Dutch"];

	LID = readSetupByKey("SYS_LANGUAGE");

	table = ajaxGet("/resources/lang/LT_" + locale[LID] + ".tlf.json");

	if(table) {
		//console.log(table);
		LT = eval('(' + table + ')');
		
//		$("[name^='LAN_']").text(LT[parseInt($(this).text())].str);
		$("[name^='LAN_']").text(function(){
//		    alert("xxx=" + parseInt($(this).text()));
    		return LT[parseInt($(this).text())].str;
		});
	}
}

function GetLan(index)
{
    return LT[index].str;
}
/*
function T_GetLangString(key)
{
	var id = key.slice(4);
	console.log("key = " + key + " id = " + id);
	for(i = 0; i < LT.length; i++) {
		if(LT[i].id == id)
			return LT[i].str;
	}
	return undefined;
}

*/
/*****************************************************************************/

var PRODUCT = "truen";
var LID = 2;

/*****************************************************************************/

function T_LoadSetup(page)
{
	var setup = readSetupByPage(page);

	$(":input").each(function(index, obj) {
		//console.log("name = " + $(this).prop("name") + " type = " + $(this).prop("type"));
		if($(this).prop("name") != undefined){
		    if(setup[$(this).prop("name")] != undefined){
		        if($(this).prop("type") == "checkbox") {
			        if(setup[$(this).prop("name")] == "0")
				        $(this).prop("checked", false);
			        else
				        $(this).prop("checked", true);
		        }
		        else if($(this).prop("type") == "radio") {
			        if($(this).val() == setup[$(this).prop("name")])
				        $(this).prop("checked", true);
			        else
				        $(this).prop("checked", false);
		        }
		        else if($(this).prop("type") == "select-one") {
			        var value = setup[$(this).prop("name")];
			        $(this).find("option").filter(function() {
				        return $(this).val() == value;
			        }).prop("selected", true);
		        }
		        else if($(this).prop("type") == "textarea") {
			        $(this).val(setup[$(this).prop("name")]);
		        }
		        else {
			        $(this).val(setup[$(this).prop("name")]);
		        }
		    }
		}
	});

	$("span").each(function(index, obj) {
		if($(this).attr("name") != undefined){
		    if(setup[$(this).attr("name")] != undefined){
		        $(this).text(setup[$(this).attr("name")]);
	        }
	    }
	});
}

function T_ApplySetup(formId, prefix)
{
	var message = "";

	if(formId == undefined)
		formFilter = "";
	else
		formFilter = "form[id=" + formId + "] ";

	$(formFilter + ":input[name^=" + prefix + "]").each(function(index, obj) {
		if(!$(this).readOnly && !$(this).disabled) {
			if($(this).prop("type") == "radio") {
				if($(this).prop("checked")) {
					var tmp = encodeURIComponent($(this).prop("name")) + "=" + $(this).val() + "&";
					message += tmp;
				}
			}
			else if($(this).prop("type") == "checkbox") {
				var tmp = encodeURIComponent($(this).prop("name")) + "=";
				if($(this).prop("checked")) {
					tmp += "1&";
				}
				else {
					tmp += "0&";
				}
				message += tmp;
			}
			else if($(this).prop("type") == "select-one") {
				var tmp = encodeURIComponent($(this).prop("name")) + "=" + encodeURIComponent($(this).val()) + "&";
				message += tmp;
			}
			else if($(this).prop("type") == "textarea") {
				var tmp = encodeURIComponent($(this).prop("name")) + "=" + encodeURIComponent($(this).val()) + "&";
				message += tmp;
			}
			else if($(this).prop("type") == "text") {
				var tmp = encodeURIComponent($(this).prop("name")) + "=" + encodeURIComponent($(this).val()) + "&";
				message += tmp;
			}
			else if($(this).prop("type") == "password") {
				var tmp = encodeURIComponent($(this).prop("name")) + "=" + encodeURIComponent($(this).val()) + "&";
				message += tmp;
			}
			else if($(this).prop("type") == "hidden") {
				var tmp = encodeURIComponent($(this).prop("name")) + "=" + encodeURIComponent($(this).val()) + "&";
				message += tmp;
			}
		}
	});
//	console.log("[MSG] = [" + message + "]");

	writeSetup(message);
}

function T_SelectMenu(product, menu)
{
	url = "/product/" + product + "/" + menu + ".html";
    var boardid = readSetupByKey("SYS_BOARDID");
	if(boardid == 0x1003 || boardid == 0x1007){
        if(menu == "systemsystemid" || menu == "imagegeneral" || menu == "imagemask" || menu == "videoinformation" || menu == "videoencode" || menu == "ptzgeneral" || menu == "ptzpreset" || menu == "eventmotion" || menu == "eventnotification" || menu == "recordgeneral" || menu == "recordschedule" || menu == "networkftp"){
       	    url = "/product/" + product + "/" + menu + "_410.html";
        }
	}
	else if(boardid == 0xB001){
        if(menu == "systemsystemid" || menu == "videoinformation" || menu == "videoencode" || menu == "imagegeneral" || menu == "imagemask" || menu == "eventnotification" || menu == "networkftp" || menu == "networkrtspmulticast" || menu == "networkoneway" || menu == "networksrt" || menu == "eventmotion" || menu == "eventva" || menu == "eventuserdefinedevent"){
       	    url = "/product/" + product + "/" + menu + "_a400.html";
        }
	}
	else{
        if(menu == "imagegeneral"){
            var val = readSetupByKey("SYS_MODULE_TYPE");
            var ipcam = readSetupByKey("SYS_ISIPCAM");
            if(ipcam == 0){
        	    url = "/product/" + product + "/" + menu + "_server.html";
            }
            else{
                if(val == NEXT_MODULE){
        	        url = "/product/" + product + "/" + menu + "_eagle.html";
                }
                else if(val == SONY_MODULE){
        	        url = "/product/" + product + "/" + menu + "_sony.html";
                }
                else if(val == PANASONIC_MODULE){
        	        url = "/product/" + product + "/" + menu + "_panasonic.html";
                }
                else if(val == TECHWIN_MODULE){
        	        url = "/product/" + product + "/" + menu + "_techwin.html";
                }
                else if(val == HITACHI_MODULE){
        	        url = "/product/" + product + "/" + menu + "_hitachi.html";
                }
                else if(val == LG_MODULE){
        	        url = "/product/" + product + "/" + menu + "_lg.html";
                }
                else if(val == SONYIS_MODULE){
        	        url = "/product/" + product + "/" + menu + "_imx103.html";
                }
                else if(val == WONWOO_MODULE){
        	        url = "/product/" + product + "/" + menu + "_wonwoo.html";
                }
                else if(val == CYNIX_MODULE){
        	        url = "/product/" + product + "/" + menu + "_cynix.html";
                }
                else if(val == POWERVIEW_MODULE){
        	        url = "/product/" + product + "/" + menu + "_powerview.html";
                }
				else if(val == NXP_ISP_MODULE) {
        	        url = "/product/" + product + "/" + menu + "_nxp.html";
				}
				else if(val == AMBA_ISP_MODULE) {
					url = "/product/" + product + "/" + menu + "_amba.html";
				}
				else if(val == HI_ISP_MODULE) {
					url = "/product/" + product + "/" + menu + "_hi.html";
				}
				else if(val == HI3519_ISP_MODULE) {
					url = "/product/" + product + "/" + menu + "_hi.html";
				}
                else if(val == HI3519A_ISP_MODULE) {
					url = "/product/" + product + "/" + menu + "_hi.html";
				}
                else if(val == S5LM55_ISP_MODULE) {
					url = "/product/" + product + "/" + menu + "_s5l.html"; // TODO: check
                }
				else if(val == CV2_ISP_MODULE) {
					url = "/product/" + product + "/" + menu + "_cv2.html"; // TODO: check
                }
            }
        }
        
        if(menu == "imagemask"){
            var val = readSetupByKey("SYS_MODULE_TYPE");
            var detail = readSetupByKey("SYS_MODULE_DETAIL");
            var ptz = readSetupByKey("SYS_PTZ_TYPE");

            if(val == NEXT_MODULE){
                if(readSetupByKey("SYS_IS_SEMOCON_SPECIFIC") == 1){
            	    url = "/product/" + product + "/" + menu + "_zoom.html";
                }
                else{
            	    url = "/product/" + product + "/" + menu + "_box.html";
        	    }
            }
            else if(val == SONYIS_MODULE){
        	    url = "/product/" + product + "/" + menu + "_box.html";
            }
            else if (detail == MODULE_SONY_SE600 || detail == MODULE_PACIFIC_20 || detail == MODULE_SONY_EH6300 || detail == MODULE_SONY_EH6500 || detail == MODULE_SONY_EV7500 || detail == MODULE_SONY_EV7300 || detail == MODULE_SONY_EV7310 || detail == MODULE_SONY_EV7520 || detail == MODULE_SONY_EV7520A || detail == MODULE_SONY_EV7320 || detail == MODULE_SONY_EV7517 || detail == MODULE_SONY_EV7317 || detail == MODULE_SONY_ER8300 || (val == HITACHI_MODULE && ptz == 7) || val == TECHWIN_MODULE || val == LG_MODULE || val == PANASONIC_MODULE || val == WONWOO_MODULE || val == CYNIX_MODULE || val == POWERVIEW_MODULE){
        	    url = "/product/" + product + "/" + menu + "_zoom.html";
            }
			else if (val == NXP_ISP_MODULE)
			{
			    if(ptz == 1){
            	    url = "/product/" + product + "/" + menu + "_zoom.html";
            	}
            	else{
            	    url = "/product/" + product + "/" + menu + "_ti.html";
            	}
			}
			else if (val == HI3519_ISP_MODULE)
			{
				var is_ptz_micom = readSetupByKey("SYS_IS_TRUEN_PTZ_MICOM");
			    if(is_ptz_micom != 0){
            	    url = "/product/" + product + "/" + menu + "_zoom.html";
            	}
            	else{
                    if(ptz == 1)
                    {
						if(readSetupByKey("SYS_USE_POLYGONMASK") == 1){
							url = "/product/" + product + "/" + menu + "_polygon.html"; // TODO: check
						} else {
							url = "/product/" + product + "/" + menu + "_zoom.html";
						}
                    }
                    else
                    {
            	        url = "/product/" + product + "/" + menu + "_ti.html";
                    }
            	}
			}
			else if (val == HI_ISP_MODULE || val == HI3519A_ISP_MODULE)
			{
                var is_ptz_micom = readSetupByKey("SYS_IS_TRUEN_PTZ_MICOM");
				var mosaic = readSetupByKey("SYS_USE_MOSAIC_MASK");

                if(is_ptz_micom != 0){
					if(mosaic == 1) {
						url = "/product/" + product + "/" + menu + "_mosaic.html";
					} else {
						url = "/product/" + product + "/" + menu + "_zoom.html";
					}
                }
                else {
                    if(ptz == 1){
                        url = "/product/" + product + "/" + menu + "_zoom.html";
                    }
                    else{
                        if(readSetupByKey("SYS_IS_SEMOCON_SPECIFIC") == 1){
                            url = "/product/" + product + "/" + menu + "_zoom.html";
                        }
                        else{
                            url = "/product/" + product + "/" + menu + "_ti.html";
                        }
                    }
                }
			}
            else if (val == S5LM55_ISP_MODULE)
            {
                if (ptz == 1)
                {
					if(readSetupByKey("SYS_USE_POLYGONMASK") == 1){
						url = "/product/" + product + "/" + menu + "_polygon.html"; // TODO: check
                    } else {
                        url = "/product/" + product + "/" + menu + "_zoom.html"; // TODO: check
                    }
                }
                else
                {
                    url = "/product/" + product + "/" + menu + "_ti.html"; // TODO: check
                }
            }
			else if (val == CV2_ISP_MODULE)
            {
				if(is_ptz_micom != 0){
					if(readSetupByKey("SYS_USE_POLYGONMASK") == 1){
						url = "/product/" + product + "/" + menu + "_polygon.html"; // TODO: check
					} else {
						url = "/product/" + product + "/" + menu + "_zoom.html"; // TODO: check
					}
				} else {
					if (ptz == 1)
					{
						url = "/product/" + product + "/" + menu + "_zoom.html"; // TODO: check
					}
					else
					{
						url = "/product/" + product + "/" + menu + "_ti.html"; // TODO: check
					}
				}
            }
			else if(val == AMBA_ISP_MODULE)
			{
				url = "/product/" + product + "/" + menu + "_ti.html";
			}
			else if(readSetupByKey("SYS_ISIPCAM") == 0 && ((readSetupByKey("SYS_BOARDID") & 0xF000) == 0x3000 || (readSetupByKey("SYS_BOARDID") & 0xF000) == 0xB000))
			{
        	    url = "/product/" + product + "/" + menu + "_ti.html";
			}
        }
        if(menu == "imageircontrol"){
			var is_ptz_micom = readSetupByKey("SYS_IS_TRUEN_PTZ_MICOM");
			var module_type = readSetupByKey("SYS_MODULE_TYPE");
			var sys_ftcamera = readSetupByKey("SYS_FTCAMERA");
			var sys_ftcamera_ir4 = readSetupByKey("SYS_FTCAMERA_IR4");
			var sys_ftcamera_nline = readSetupByKey("SYS_FTCAMERA_NLINE");
			var sys_ftcamera_hwversion = readSetupByKey("SYS_FTCAMERA_HWVERSION");

            if(readSetupByKey("SYS_IS_CTECNEW") == 1){
            }
            else if(readSetupByKey("SYS_CYNIX_XIR") == 1){
            	   url = "/product/" + product + "/" + menu + "_xir.html";
            }
            else if(sys_ftcamera == 1 && (readSetupByKey("SYS_BOARDID") & 0x000F) == 0x0002 || (readSetupByKey("SYS_BOARDID") & 0xF00F) == 0xB004){
                if(sys_ftcamera_ir4 != 0){
					if((sys_ftcamera_ir4 == 4 && sys_ftcamera_hwversion == 1) || (sys_ftcamera_ir4 == 2 && sys_ftcamera_nline == 1)) 
					{
						url = "/product/" + product + "/" + menu + "_fir_laser.html";
					}
					else{
						url = "/product/" + product + "/" + menu + "_fir4.html";
					}
        	    }
        	    else{
            	    url = "/product/" + product + "/" + menu + "_fir.html";
        	    }
        	}
			else if(module_type == HI3519_ISP_MODULE)
			{
				if(is_ptz_micom != 0)
				{
					if(sys_ftcamera == 1 && sys_ftcamera_ir4 != 0)
					{
						url = "/product/" + product + "/" + menu + "_fir4.html";
					}
					else{
						url = "/product/" + product + "/" + menu + "_fir.html";
					}
				}
				else
				{
           	    	url = "/product/" + product + "/" + menu + "_nxpisp.html";
				}
			}
            else if(module_type == S5LM55_ISP_MODULE)
            {
                url = "/product/" + product + "/" + menu + "_nxpisp.html";
            }
			else if(module_type == CV2_ISP_MODULE)
			{
				if(is_ptz_micom != 0)
				{
					if(sys_ftcamera == 1 && sys_ftcamera_ir4 != 0)
					{
						if((sys_ftcamera_ir4 == 4 && sys_ftcamera_hwversion == 1) || (sys_ftcamera_ir4 == 2 && sys_ftcamera_nline == 1)) 
						{
							url = "/product/" + product + "/" + menu + "_fir_laser.html";
						} 
						else {
							url = "/product/" + product + "/" + menu + "_fir4.html";
						}
					}
					else{
						url = "/product/" + product + "/" + menu + "_fir.html";
					}
				}
				else
				{
           	    	url = "/product/" + product + "/" + menu + "_nxpisp.html";
				}
			}
            if(readSetupByKey("SYS_IS_HOUSINGSPECIFIC") == 1){
                var val = readSetupByKey("SYS_MODULE_TYPE");
                if(val == NEXT_MODULE || val == SONYIS_MODULE){
            	    url = "/product/" + product + "/" + menu + "_housingeagle.html";
                }
                else{
            	    url = "/product/" + product + "/" + menu + "_housing.html";
        	    }
        	}
            if(module_type == NXP_ISP_MODULE || module_type == HI_ISP_MODULE){
           	    url = "/product/" + product + "/" + menu + "_nxpisp.html";
            }
			if(module_type == HI3519A_ISP_MODULE && (readSetupByKey("SYS_BOARDID") & 0xF00F) == 0xB003){
                url = "/product/" + product + "/" + menu + "_nxpisp.html";
            }
        }
        
        if(menu == "videoinformation"){
			if(parseInt(readSetupByKey("VID_MAX_STREAM_OF_ONE_CHANNEL")) == 2)
			{
        	    url = "/product/" + product + "/" + menu + "_2sub.html";
			} 
			else if(readSetupByKey("SYS_MODE") != 0){ // decoder system
        	    url = "/product/" + product + "/" + menu + "_dec.html";
            }
        }

		if(menu == "videoencode"){
			if(parseInt(readSetupByKey("VID_MAX_STREAM_OF_ONE_CHANNEL")) == 2)
			{
        	    url = "/product/" + product + "/" + menu + "_2sub.html";
			}
        }

		if(menu == "videoaudio") {
			if((parseInt(boardid) & 0xF000) == 0x3000 || (parseInt(boardid) & 0xF000) == 0xB000)
				url = "/product/" + product + "/" + menu + "_3500.html";
		}
		
		if(menu == "networkddns"){
			if(parseInt(readSetupByKey("SYS_COMMAX_DDNS")) == 1)
			{
        	    url = "/product/" + product + "/" + menu + "_commax.html";
			}
        }

		if(menu == "ptzpositionosd"){
			if(parseInt(readSetupByKey("SYS_IS_PVOSD_EXTENSION")) == 1)
			{
        	    url = "/product/" + product + "/" + menu + "_ex.html";
			}
        }

        if(menu == "ptztracking"){
            if(parseInt(readSetupByKey("SYS_IS_OBJECT_DETECT")) == 1)
            {
                url = "/product/" + product + "/" + menu + "_od.html";
            }
        }
        
		if(menu == "eventnotification") {
			if(boardid == 0x3003)
				url = "/product/" + product + "/" + menu + "_3500d.html";
    	}
        if(menu == "systemsystemid") {
            if(readSetupByKey("SYS_OSD_PREVIEW") == 1) {
                url = "/product/" + product + "/" + menu + "_preview.html";
            }
        }
        if(menu == "networkftp") {
            if(readSetupByKey("SYS_IS_BNB_SPECIFIC") == 1) {
                url = "/product/" + product + "/" + menu + "_bnb.html";
            }
        }
    }

    if (menu == "eventlpr") {
        if(readSetupByKey("SYS_USE_LPR") == 1) {
            url = "/product/" + product + "/" + menu + "2.html";
        } 
       if(readSetupByKey("SYS_USE_GIT_LPR") == 1){
            url = "/product/" + product + "/" + menu + "git.html";
       }
    }
	if (menu == "eventobjectdetection") {
		if(readSetupByKey("SYS_IS_OBJECT_DETECT") == 1) {
			url = "/product/" + product + "/" + menu + ".html";
		}
	}
	if((readSetupByKey("SYS_IS_HATTLELAND_SPEC")) == 1){
		$(".LAN_686").attr('class', 'LAN_031');
	}

	contents = ajaxGet(url);
	$("#TSetupContents").html(contents);

	// set
	$("a#" + menu).parent().parent().parent(".menuWrap").addClass("on");
	$("a#" + menu).addClass("on");
	$("a#" + menu).children("img").attr("src", "/resources/img/btn45_N.gif");
}

function T_LoadSetupItem(name)
{
	contents = ajaxGet("/setup-item/" + name + ".html");
	$("#TSetupItemList").append(contents);
}

function T_LiveInit(product)
{
	alert("T_LiveInit");
}

function T_SetupInit(menu)
{
	//loadModel();
	loadSetupTemplate(PRODUCT);
	
	if(!isSupportMenu(menu)) {
		alert("TODO: 지원하지 않는 페이지일 경우 redirect 해야 한다.");
	}
	T_SelectMenu(PRODUCT, menu);
    initLoading();
	if(startSetup() == undefined) {
		initLanguage();
		beautify();
	}

	if(SUPPORT_MULTI_DEVICE_DETECTION_CAM_TYPE == support_multi_device_type) {
		$("[name=subcam-text]").text(GetLan(1060));
		$("[name=maincam-text]").text(GetLan(1064)); 		
	}
	else if(SUPPORT_MULTI_DEVICE_PTZ_TYPE == support_multi_device_type) {
		$("[name=subcam-text]").text(GetLan(1064));
		$("[name=maincam-text]").text(GetLan(1060));
	}
	else {
		;
	}
	
  	contents = ajaxGet("/resources/oem/title.html");
  	if(contents){
        document.title = contents; 
//	    $("#page_title").html(contents);
    }
    else{
        document.title = readSetupByKey("SYS_SYSTEMID");
        if(document.title == ""){
            document.title = "Video Surveillance"; 
        }
    }

  	contents = ajaxGet("/resources/oem/url.html");
  	if(!contents){
		contents = ajaxGet("/bitmap/oem/logo.gif");
		if (!contents){
			contents = '<h1><img src="/resources/oem/logo.gif"/></h1>';
		} else {
			contents = '<h1><img src="/bitmap/oem/logo.gif"/></h1>';
		}
    }
	if((readSetupByKey("SYS_IS_HATTLELAND_SPEC")) == 1){
		contents3 = '<h3><img src="/resources/oem/logo2.gif"/></h3>';
		document.getElementById('logoarea1').style.display = "";
		$("#logoarea1").html(contents3);
	} else {
		document.getElementById('logoarea1').style.display = "none";
	}
	if(readSetupByKey("SYS_IS_SYSPRO_SPECIFIC") != 1) {
		$("#logoarea").html(contents);
	}
    if(parseInt(readSetupByKey("SYS_IS_HAPCHEON_SPECIFIC")) == 1){
	    document.getElementById('logout').style.display = "";
    }
}


function T_Term()
{
    terminateSetup();
}

function T_Init()
{
    initalizeSetup();
}

function initLoading()
{
//	$("#PopupMessage").html("").hide();
	$("#PopupMessage").html("<img src=\"/resources/img/loading.gif\"></img>").hide();
}

function showLoading()
{
	var popup = $("#PopupMessage");

	width = popup.width();
	height = popup.height();
	
	popup.css("position","absolute")
		.css("z-index","10001")
		.css("top","30%")
		.css("left","50%")
		.css("margin-left", -1 * width / 2)
		.css("margin-top", -1 * height / 2);

	popup.show();
}

function hideLoadingDelay(delay)
{
	setTimeout(function() { $("#PopupMessage").hide(); }, delay);
}


/*****************************************************************************/

function GetVideoAlgorithm(boardid, sysmode, sub, force_detection_cam, cb)
{
    if (cb == undefined)
    {
        if (sysmode == 1)
        {
            return parseInt(readSetupByKey("VID_CURDECALGORITHM"));
        }
        else
        {
            if (((boardid & 0xffff) == 0xB001) || (force_detection_cam == 1))
            {
                if      (sub == 0) return parseInt(readSetupByKey("VID_ALGORITHM"));
                else if (sub == 1) return parseInt(readSetupByKey("VI1_ALGORITHM"));
                else if (sub == 2) return parseInt(readSetupByKey("VI2_ALGORITHM"));
                else if (sub == 3) return parseInt(readSetupByKey("VI3_ALGORITHM"));
                else if (sub == 4) return parseInt(readSetupByKey("VID_DUALALGORITHM"));
                else if (sub == 5) return parseInt(readSetupByKey("VI1_DUALALGORITHM"));
                else if (sub == 6) return parseInt(readSetupByKey("VI2_DUALALGORITHM"));
                else if (sub == 7) return parseInt(readSetupByKey("VI3_DUALALGORITHM"));
                else if (sub == 8) return parseInt(readSetupByKey("VI4_ALGORITHM"));
                else if (sub == 9) return parseInt(readSetupByKey("VI4_DUALALGORITHM"));
                else               return parseInt(readSetupByKey("VID_ALGORITHM"));
            }
            else
            {
                if      (sub == 0) return parseInt(readSetupByKey("VID_ALGORITHM"));
                else if (sub == 1) return parseInt(readSetupByKey("VID_DUALALGORITHM"));
                else if (sub == 2) return parseInt(readSetupByKey("VID_TRIPLEALGORITHM1"));
                else if (sub == 3) return parseInt(readSetupByKey("VID_TRIPLEALGORITHM2"));
                else               return parseInt(readSetupByKey("VID_ALGORITHM"));
            }
        }
    }
    else 
    {
        if (sysmode == 1)
        {
            readSetupByKeyAsync("VID_CURDECALGORITHM", cb);
        }
        else
        {
            if (((boardid & 0xffff) == 0xB001) || (force_detection_cam == 1))
            {
                if      (sub == 0) readSetupByKeyAsync("VID_ALGORITHM", cb);
                else if (sub == 1) readSetupByKeyAsync("VI1_ALGORITHM", cb);
                else if (sub == 2) readSetupByKeyAsync("VI2_ALGORITHM", cb);
                else if (sub == 3) readSetupByKeyAsync("VI3_ALGORITHM", cb);
                else if (sub == 4) readSetupByKeyAsync("VID_DUALALGORITHM", cb);
                else if (sub == 5) readSetupByKeyAsync("VI1_DUALALGORITHM", cb);
                else if (sub == 6) readSetupByKeyAsync("VI2_DUALALGORITHM", cb);
                else if (sub == 7) readSetupByKeyAsync("VI3_DUALALGORITHM", cb);
                else if (sub == 8) readSetupByKeyAsync("VI4_ALGORITHM", cb);
                else if (sub == 9) readSetupByKeyAsync("VI4_DUALALGORITHM", cb);
                else               readSetupByKeyAsync("VID_ALGORITHM", cb);
            }
            else
            {
                if      (sub == 0) readSetupByKeyAsync("VID_ALGORITHM", cb);
                else if (sub == 1) readSetupByKeyAsync("VID_DUALALGORITHM", cb);
                else if (sub == 2) readSetupByKeyAsync("VID_TRIPLEALGORITHM1", cb);
                else if (sub == 3) readSetupByKeyAsync("VID_TRIPLEALGORITHM2", cb);
                else               readSetupByKeyAsync("VID_ALGORITHM", cb);
            }
        }
    }
}

