//更新頁
MqttModuleUpdatePage.prototype._SaveGlobalVariable_ = function(Row)
{
	var Class = new StepFlow();
	Class.LockAllUi();
	var Index = 0, Length = Row.length;
	Row.eq(Index).find(".Button").click();
	var SetInterval = setInterval(function(){
		if(Row.eq(Index).find(".Message").is(":visible") == true && 
		   Row.eq(Index).find(".Message").css("color") == "rgb(34, 119, 0)")
		{
			Index++;
			Row.eq(Index).find(".Button").click();
			if(Index == Length)
			{
				clearInterval(SetInterval);
				MqttModuleUpdatePage.prototype._ShowFirstPage_();
				Class.UnLockAllUi();
			}
		}
	},500);
}

MqttModuleUpdatePage.prototype._IsSaveGlobalVariable_ = function()
{
	var Return = false;
	var Class 		 = new LoadFile();
	var RemoteBroker = Class.ReadFile("./File/IoT_Platform/RemoteBroker.json", new Object());
	
	if(JSON.stringify(RemoteBroker)	== JSON.stringify(Global_RemoteBroker))
	{
		Return = true;
	}
	return Return;
}

MqttModuleUpdatePage.prototype._ShowFirstPage_ = function()
{
	$("#Main_Center-Content").find(".Body").hide();
	$("#Main_Center-Content").find(".Body").eq(1).show();
	MqttModuleFirstPage.prototype._Initialization_();
}

MqttModuleUpdatePage.prototype._Initialization_ = function()
{
	$("#Main_Center-Content").find(".Body").hide();
	$("#Main_Center-Content").find(".Body").eq(0).show();

	if(MqttModuleUpdatePage.prototype._IsSaveGlobalVariable_() == true)
	{
		MqttModuleUpdatePage.prototype._ShowFirstPage_();
		return 0;
	}
	var Row = $("#Main_Center-Content").find(".Body").eq(0).find(".Table").find(".Row");
	MqttModuleUpdatePage.prototype._SaveGlobalVariable_(Row);
}




















//第1頁
MqttModuleFirstPage.prototype._CheckMqttModuleQuantity_ = function()
{
	var Return = true;
	var AllNumber = MqttModuleFirstPage.prototype._GetAllNumber_();
	if(AllNumber.length >= 200)
	{
		Return = false;
	}
	return Return;
}

MqttModuleFirstPage.prototype._LoadLanName_ = function()
{
	var Class = new LoadFile();
	var Lan = Class.ReadFile("./File/LAN.json");
	var LanDevice = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row").eq(0).find(".Select");
	var CopyDialog = $(".Dialog").eq(0).find(".Row").eq(0).find(".Select");
	var RemoveDialog = $(".Dialog").eq(1).find(".Row").eq(0).find(".Select");
	LanDevice.empty();  CopyDialog.empty(); RemoveDialog.empty();
	for(var i = 0; i < Lan.length; i++)
	{
		LanDevice.append($("<option></option>").attr("value",Lan[i]).text(Lan[i]));
		CopyDialog.append($("<option></option>").attr("value",Lan[i]).text(Lan[i]));
		RemoveDialog.append($("<option></option>").attr("value",Lan[i]).text(Lan[i]));
	}
	Class = null;
}

MqttModuleFirstPage.prototype._ChangeLanDevice_ = function(that)
{
	MqttModuleFirstPage.prototype._RedisplayUi_();
}

MqttModuleFirstPage.prototype._GetDeviceName_ = function()
{
	var Return;
	var Element = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row").eq(0).find(".Select");
	Return = Element.val();
	return Return;
}

MqttModuleFirstPage.prototype._GetAllDeviceName_ = function()
{
	var Return 	= new Array();
	var Element = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row").eq(0).find(".Select");
	var Size = Element.find("option").size();
	for(var i = 0; i < Size; i++)
	{
		Return.push(Element.find("option").eq(i).val());
	}
	return Return;
}

MqttModuleFirstPage.prototype._GetAllNumber_ = function()
{
	var AllDeviceName = MqttModuleFirstPage.prototype._GetAllDeviceName_();
	var ObjectKey = new Array();
	for(var i = 0; i<AllDeviceName.length; i++)
	{
		if(typeof(Global_MqttModule[AllDeviceName[i]]) == "object")
		{
			ObjectKey = ObjectKey.concat(Object.keys(Global_MqttModule[AllDeviceName[i]]));
		}
	}
	ObjectKey = ObjectKey.sort(function (a, b) {return a - b});
	return ObjectKey;
}

MqttModuleFirstPage.prototype._CreateMqttSettingObject_ = function(number)
{
	Global_MqttSetting[number] 						= new Object();
	Global_MqttSetting[number]["MqttConnection"]	= new Array("Broker (Local)");
}

MqttModuleFirstPage.prototype._DeleteRepeatNumber_ = function(number)
{
	for(var i = 0; i < Global_MqttIndex.length; i++)
	{
		if(number == Global_MqttIndex[i])
		{
			Global_MqttIndex.splice(i,1);
		}
	}
}

MqttModuleFirstPage.prototype._BuildMqttModuleAllObject_ = function(that)
{
	var number = $(that).closest(".Row").find(".Column").eq(1).find(".Select").val();
	MqttModuleFirstPage.prototype._DeleteRepeatNumber_(number);
	var ModuleName = $(that).closest(".Row").find(".Column").eq(2).find(".Text").val();
	MqttModuleFirstPage.prototype._BuildMqttModuleObject_(ModuleName, number);
	MqttModuleFirstPage.prototype._CreateMqttSettingObject_(number);
}

MqttModuleFirstPage.prototype._BuildMqttModuleObject_ = function(ModuleName, number)
{
	var DeviceName = MqttModuleFirstPage.prototype._GetDeviceName_();
	if(typeof(Global_MqttModule[DeviceName][number]) == "undefined"){
		Global_MqttModule[DeviceName][number] = new Object();
	}
	Global_MqttModule[DeviceName][number]["ModuleName"] = ModuleName;
}

MqttModuleFirstPage.prototype._ShowTable_ = function(ObjectKey, DisplayNumber)
{
	var DeviceName = MqttModuleFirstPage.prototype._GetDeviceName_();
	$("#ModuleTable").find(".DynamicRow").remove();
	var html = $(".Dynamic").html();
	var PageNumber = $("#ModuleToolbar").find(".Text").val();
	var Start = (PageNumber-1)*DisplayNumber;
	if((ObjectKey.length-Start) > DisplayNumber){
		var End = Start + DisplayNumber;
	}
	else{
		var End = ObjectKey.length;
	}
	var index = 0;
	for(var i = Start; i < End && End != 0; i++)
	{
		$("#ModuleTable").append(html);
		$("#ModuleTable").find(".DynamicRow").eq(index).find(".Column").eq(1).text(ObjectKey[i]);
		$("#ModuleTable").find(".DynamicRow").eq(index).find(".Column").eq(2).text(Global_MqttModule[DeviceName][ObjectKey[i]]["ModuleName"]);
		index++;
	}
}

MqttModuleFirstPage.prototype._UpdateNumber_ = function(ObjectKey)
{
	var AllNumber = MqttModuleFirstPage.prototype._GetAllNumber_();
	AllNumber = AllNumber.sort(function (a, b) {return a - b});
	if(AllNumber.length == 0)   {var MaxNumber = 0}
	else                        {var MaxNumber = AllNumber[AllNumber.length-1];}
	for(var i=0; i<Global_MqttIndex.length; i++)
	{
		if(parseInt(Global_MqttIndex[i])>parseInt(MaxNumber)){
			MqttModuleFirstPage.prototype._DeleteRepeatNumber_(Global_MqttIndex[i]);
			i--;
		}
	}
	var DynamicRowLength = ObjectKey.length;
	var TemporaryIndexLength = Global_MqttIndex.length;
	var Id = "#Number";
	$(Id).empty();
	for(var i = 0; i < TemporaryIndexLength; i++)
	{
		$(Id).append($("<option></option>").attr("value", Global_MqttIndex[i]).text(Global_MqttIndex[i]));
	}
	$(Id).append($("<option></option>").attr("value", DynamicRowLength+TemporaryIndexLength+1).text(DynamicRowLength+TemporaryIndexLength+1));
}

MqttModuleFirstPage.prototype._UpdateTableToolbar_ = function(ObjectKey, DisplayNumber)
{
	var NumberOfPages = Math.ceil(ObjectKey.length/DisplayNumber);
	if(NumberOfPages == 0) {NumberOfPages = 1;}
	var Id = "#ModuleToolbar";
	$(Id).find("label").eq(1).text(NumberOfPages);
	var CurrentlyPage = $(Id).find(".Text").val();
	CurrentlyPage = parseInt(CurrentlyPage);
	if(CurrentlyPage > NumberOfPages)
	{
		$(Id).find(".Text").val(NumberOfPages);
	}
	else if(CurrentlyPage<=0 || isNaN(CurrentlyPage)==true)
	{
		$(Id).find(".Text").val(1);
	}
	else
	{
		$(Id).find(".Text").val(CurrentlyPage);
	}
}

MqttModuleFirstPage.prototype._RedisplayUi_ = function()
{
	var DisplayNumber = 20;//RTU 20 T CP 40 ASCII 20
	var DeviceName = MqttModuleFirstPage.prototype._GetDeviceName_();
	var ObjectKey = MqttModuleFirstPage.prototype._GetAllNumber_();
	MqttModuleFirstPage.prototype._UpdateNumber_(ObjectKey);
	ObjectKey = Object.keys(Global_MqttModule[DeviceName]);
	MqttModuleFirstPage.prototype._UpdateTableToolbar_(ObjectKey, DisplayNumber);
	MqttModuleFirstPage.prototype._ShowTable_(ObjectKey, DisplayNumber);
	var Id = "#ModuleTable";
	$(Id).find(".Row").eq(0).find(":checkbox").prop("checked",false);
}

MqttModuleFirstPage.prototype._ModuleAddNewRow_ = function(that)
{
	var Id = "#ModuleTable";
	var ShowErrorMessage = $(Id).find(".Row").eq(0).find(".Message").is(":visible");
	if(ShowErrorMessage == true)    {return 0;}
	var OK = MqttModuleFirstPage.prototype._CheckMqttModuleQuantity_();
	if(OK == false) {return 0;}
	MqttModuleFirstPage.prototype._BuildMqttModuleAllObject_(that);
	MqttModuleFirstPage.prototype._RedisplayUi_();
}

MqttModuleFirstPage.prototype._ShowPreviousPage_ = function()
{
	var Id = "#ModuleToolbar";
	var CurrentlyPage = $(Id).find(".Text").val();
	$(Id).find(".Text").val(parseInt(CurrentlyPage)-1);
	MqttModuleFirstPage.prototype._RedisplayUi_();
}

MqttModuleFirstPage.prototype._ShowNextPage_ = function()
{
	var Id = "#ModuleToolbar";
	var CurrentlyPage = $(Id).find(".Text").val();
	$(Id).find(".Text").val(parseInt(CurrentlyPage)+1);
	MqttModuleFirstPage.prototype._RedisplayUi_();
}

MqttModuleFirstPage.prototype._CopyGlobalVariable_ = function(DeviceName, CopyToDeviceName, number)
{
	var ObjectKey = MqttModuleFirstPage.prototype._GetAllNumber_();
	var Index = ObjectKey.length+Global_MqttIndex.length+1;
	var CopyNumber =  parseInt($(".Dialog").eq(0).find(".DialogTable").find(".Row").eq(1).find(".Text").val());
	for(j = 0; j < CopyNumber; j++)
	{
		var OK = MqttModuleFirstPage.prototype._CheckMqttModuleQuantity_();
		if(OK == false)     {break;}
		
		if(typeof(Global_MqttModule[DeviceName][number]) != "undefined"){
			Global_MqttModule[CopyToDeviceName][Index] = jQuery.parseJSON(JSON.stringify(Global_MqttModule[DeviceName][number]));
		}
		if(typeof(Global_MqttSetting[number]) != "undefined"){
			Global_MqttSetting[Index] = jQuery.parseJSON(JSON.stringify(Global_MqttSetting[number]));
		}
		if(typeof(Global_MqttVariable[number]) != "undefined"){
			Global_MqttVariable[Index] = jQuery.parseJSON(JSON.stringify(Global_MqttVariable[number]));
		}
		MqttModuleSecondPage.prototype._SynchronizeOtherServiceData_(Index);
		Index++;
	}
}

MqttModuleFirstPage.prototype._CopyTickedModule_ = function()
{
	var DeviceName = MqttModuleFirstPage.prototype._GetDeviceName_();
	var CopyToDeviceName = $(".Dialog").eq(0).find(".Row").find(".Select").val();
	var Id = "#ModuleTable";
	var element = $(Id).find(".DynamicRow").find(":checked");
	var CheckedLength = element.length;
	for(var i = 0; i < CheckedLength; i++)
	{
		number = parseInt(element.eq(i).closest(".DynamicRow").find(".Column").eq(1).text());
		MqttModuleFirstPage.prototype._CopyGlobalVariable_(DeviceName, CopyToDeviceName, number);
	}
}

MqttModuleFirstPage.prototype._CopyModule_ = function()
{
	MqttModuleFirstPage.prototype._CopyTickedModule_();
	MqttModuleFirstPage.prototype._RedisplayUi_();
	MqttModuleFirstPage.prototype._CopyDialogDisabled_();
}

MqttModuleFirstPage.prototype._RemoveGlobalVariable_ = function(DeviceName, number)
{
	Global_MqttModule[DeviceName][number] = null;   delete Global_MqttModule[DeviceName][number];
	Global_MqttVariable[number] = null;             delete Global_MqttVariable[number];
	Global_MqttSetting[number] = null;              delete Global_MqttSetting[number];
	Global_MqttIndex.push(number);
	Global_MqttIndex = Global_MqttIndex.sort(function (a, b) {return a - b});
	Global_MqttToOpcuaVariable[number] = null;		delete Global_MqttToOpcuaVariable[number];
}

MqttModuleFirstPage.prototype._RemoveTickedModule_ = function()
{
	var DeviceName = MqttModuleFirstPage.prototype._GetDeviceName_();
	var Id = "#ModuleTable";
	var element = $(Id).find(".DynamicRow").find(":checked");
	var CheckedLength = element.length;
	for(var i = 0; i < CheckedLength; i++)
	{
		number = element.eq(i).closest(".DynamicRow").find(".Column").eq(1).text();
		MqttModuleFirstPage.prototype._RemoveGlobalVariable_(DeviceName, number);
	}
}

MqttModuleFirstPage.prototype._DeleteModule_ = function()
{
	MqttModuleFirstPage.prototype._RemoveTickedModule_();
	MqttModuleFirstPage.prototype._RedisplayUi_();
}

MqttModuleFirstPage.prototype._ClearAllModule_ = function(that)
{
	var DeviceName = $(that).closest(".Dialog").find(".Row").find(".Select").val();
	var number = Object.keys(Global_MqttModule[DeviceName]);
	for(var i = 0; i < number.length; i++)
	{
		MqttModuleFirstPage.prototype._RemoveGlobalVariable_(DeviceName, number[i]);
	}
	MqttModuleFirstPage.prototype._RedisplayUi_();
	MqttModuleFirstPage.prototype._ClearDialogDisabled_();
}

MqttModuleFirstPage.prototype._IsTickedModule_ = function()
{
	var Return=false;
	var Id = "#ModuleTable";
	if($(Id).find(".DynamicRow").find(":checked").length >0)
	{
		Return=true;
	}
	return Return;
}

MqttModuleFirstPage.prototype._InitializationGlobalVariable_ = function()
{
	var AllDeviceName = MqttModuleFirstPage.prototype._GetAllDeviceName_();
	for(var i = 0; i < AllDeviceName.length; i++)
	{
		if(typeof(Global_MqttModule[AllDeviceName[i]]) != "object")
		{
			Global_MqttModule[AllDeviceName[i]] = new Object();
		}
	}
}

MqttModuleFirstPage.prototype._Initialization_ = function()
{
	MqttModuleFirstPage.prototype._LoadLanName_();
	MqttModuleFirstPage.prototype._InitializationGlobalVariable_();
	MqttModuleFirstPage.prototype._RedisplayUi_();
}

MqttModuleFirstPage.prototype._CheckModuleName_ = function(that)
{
	var DeviceName = MqttModuleFirstPage.prototype._GetDeviceName_();
	var Id = "#ModuleTable";
	$(Id).find(".Row").eq(0).find(".Message").hide();
	var Class = new Type();
	var Name = $(that).closest(".Column").find(".Text").val();
	if(Name == "")
	{
		$(Id).find(".Row").eq(0).find(".Message").eq(0).show();
	}
	else if(Class.CheckSpecialSymbols(Name) == true || Name.split("")[0] == "-" || Name.split("")[0] == "_")
	{
		$(Id).find(".Row").eq(0).find(".Message").eq(1).show();
	}
	else if(Class.CheckChineseLanguage(Name) == true || Name.split("")[0] == " ")
	{
		$(Id).find(".Row").eq(0).find(".Message").eq(1).show();
	}
	Class = null;
}

MqttModuleFirstPage.prototype._CopyDialogEnabled_ = function(Event)
{
	var DeviceName = MqttModuleFirstPage.prototype._GetDeviceName_();
	$(".Dialog").eq(0).find(".Row").find(".Select").val(DeviceName);
	$(".Dialog").eq(0).find(".Row").find(".Text").val("");
	var MouseX = (Event.pageX-250)+"px";
	var MouseY = (Event.pageY-350)+"px";
	$(".Dialog").eq(0).css('left', MouseX);
	$(".Dialog").eq(0).css('top', MouseY);
	var Class = new Dialog();
	Class.CommonDisabled();
	Class = null;
	$("#Main_Center-Content").find(".Body").eq(1).css('pointer-events', "none");
	$("#Main_Center-Content").find(".Body").eq(1).css('opacity', 0.5);
	$(".Dialog").eq(0).css('display', "block");
}

MqttModuleFirstPage.prototype._CopyDialogDisabled_ = function()
{
	$(".Dialog").eq(0).find(".Message").hide();
	var Class = new Dialog();
	Class.CommonEnabled();
	Class = null;
	$("#Main_Center-Content").find(".Body").eq(1).css('pointer-events', "visible");
	$("#Main_Center-Content").find(".Body").eq(1).css('opacity', 1);
	$(".Dialog").eq(0).css('display', "none");
	$(".Dialog").eq(0).find(".DialogTable").find(".Row").find(".Column").eq(1).find(".Text").val("");
}

MqttModuleFirstPage.prototype._ClearDialogEnabled_ = function(Event)
{   
	var MouseX = (Event.pageX-250)+"px";
	var MouseY = (Event.pageY-350)+"px";
	$(".Dialog").eq(1).css('left', MouseX);
	$(".Dialog").eq(1).css('top', MouseY);
	var Class = new Dialog();
	Class.CommonDisabled();
	Class = null;
	$("#Main_Center-Content").find(".Body").eq(1).css('pointer-events', "none");
	$("#Main_Center-Content").find(".Body").eq(1).css('opacity', 0.5);
	$(".Dialog").eq(1).css('display', "block");
}

MqttModuleFirstPage.prototype._ClearDialogDisabled_ = function()
{
	var Class = new Dialog();
	Class.CommonEnabled();
	Class = null;
	$("#Main_Center-Content").find(".Body").eq(1).css('pointer-events', "visible");
	$("#Main_Center-Content").find(".Body").eq(1).css('opacity', 1);
	$(".Dialog").eq(1).css('display', "none");
	$(".Dialog").eq(1).find(".DialogTable").find(".Row").find(".Column").eq(1).find(".Text").val("");
}

MqttModuleFirstPage.prototype._TransmissionJson_ = function(ssocket, Command, string, Callback)
{
	var Length = 20000, index = 0;  var array = new Array("Start","");
	ssocket.emit(Command, array);   
	ssocket.on(Command, function (Return) {
		if(Return == "Start" || Return == "Send")
		{
			if(string.length > (index+Length)){
				array = new Array("Send", string.substring(index, index+Length));
			}
			else if(string.length <= (index+Length)){
				array = new Array("End", string.substring(index, string.length));
			}
			index = index + Length;
			ssocket.emit(Command, array);   
		}
		else if(Return == "End")
		{
			Callback();
		}
	});
}

MqttModuleFirstPage.prototype._EditButton_ = function(that)
{
	$("#Main_Center-Content").find(".Body").eq(2).find(".Message").hide();
	$("#Main_Center-Content").find(".Body").eq(1).hide();
	$("#Main_Center-Content").find(".Body").eq(2).show();
	$("#Main_Center-Content").css("border-left-width", "0px");
	$("#Main_Center-Content").css("width", "100%");
	$("#Main_Left-List").hide();
	var number = parseInt($(that).closest(".DynamicRow").find(".Column").eq(1).text());
	var Name = $(that).closest(".DynamicRow").find(".Column").eq(2).text();
	Name = $("#Main_Center-Content").find(".Body").eq(2).find(".FormTitle").eq(1).text();
	var Class = new Path();
	Class.LoadPathHtml(Name, 2);
	Class = null;
	MqttModuleSecondPage.prototype._HideColumn_();
	MqttModuleSecondPage.prototype._SetInitialValue_(number);
	MqttModuleSecondPage.prototype._LoadBroker_(number);
}

MqttModuleFirstPage.prototype._SaveModuleSetting_ = function(Result)
{
	var Result_1={"Input":null}, Result_2={"Input":null}, Result_3={"Input":null}, Result_4={"Input":null};
	var Class = new Transmission();
	Class.TransmissionString("SaveMqttVariable", JSON.stringify(Global_MqttVariable), Result_1);
	Class.TransmissionString("SaveMqttModule", JSON.stringify(Global_MqttModule), Result_2);
	Class.TransmissionString("SaveMqttSetting", JSON.stringify(Global_MqttSetting), Result_3);
	Class.TransmissionString("SaveMqttIndex", JSON.stringify(Global_MqttIndex), Result_4);
	var SetInterval = setInterval(function()
	{
		if(Result_1["Input"]==true && Result_2["Input"]==true && Result_3["Input"]==true && Result_4["Input"]==true)
		{
			Result["Input"] = true;
			Class = null; delete Class;
			clearInterval(SetInterval);
		}
	},500);
}

MqttModuleFirstPage.prototype._SaveConvertSetting_ = function(Result)
{
	var Result_1={"Input":null}, Result_2={"Input":null}, Result_3={"Input":null};
	var Class = new Transmission();
	Class.TransmissionString("SaveMqttToOpcuaVariable", JSON.stringify(Global_MqttToOpcuaVariable), Result_1);
	Class.TransmissionString("SaveMqttToMysqlVariable", JSON.stringify(Global_MqttToMysqlVariable), Result_2);
	Class.TransmissionString("SaveMqttToMsSqlVariable", JSON.stringify(Global_MqttToMsSqlVariable), Result_3);
	var SetInterval = setInterval(function()
	{
		if(Result_1["Input"]==true && Result_2["Input"]==true && Result_3["Input"]==true)
		{
			Result["Input"] = true;
			Class = null; delete Class;
			clearInterval(SetInterval);
		}
	},500);
}

MqttModuleFirstPage.prototype._SaveDataLayerAndServiceLayerVariables_ = function(Result)
{
	var RReturn = $.Deferred();
	var Result_1={"Input":null}, Result_2={"Input":null};
	MqttModuleFirstPage.prototype._SaveModuleSetting_(Result_1);
	MqttModuleFirstPage.prototype._SaveConvertSetting_(Result_2);
	var SetInterval = setInterval(function()
	{
		if(Result_1["Input"]==true && Result_2["Input"]==true)
		{
			Result["Input"] = true;
			clearInterval(SetInterval);
			RReturn.resolve(true);
		}
	},500);
	return RReturn.promise();
}

MqttModuleFirstPage.prototype._SaveButton_ = function(that)
{
	var Result={"Input":null};
	$(that).closest('.Row').find(".Loading").show();
	$(that).closest('.Row').find(".Message").hide();
	MqttModuleFirstPage.prototype._SaveDataLayerAndServiceLayerVariables_(Result);
	var SetInterval = setInterval(function()
	{
		if(Result["Input"] == true)
		{
				$(that).closest('.Row').find(".Loading").hide();
				$(that).closest('.Row').find(".Message").eq(0).show();
				clearInterval(SetInterval);
		}
	},500);
}




















//第2頁
MqttModuleSecondPage.prototype._CheckModuleName_ = function(that)
{
	var Id = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0);
	$(Id).find(".Row").eq(1).find(".Message").hide();
	var Class = new Type();
	var Name = $(that).closest(".Row").find(".Text").val();
	if(Name == ""){
		$(Id).find(".Row").eq(1).find(".Message").eq(0).show();
	}
	else if(Class.CheckSpecialSymbols(Name) == true || Name.split("")[0] == "-" || Name.split("")[0] == "_")
	{
		$(Id).find(".Row").eq(1).find(".Message").eq(1).show();
	}
	else if(Class.CheckChineseLanguage(Name) == true || Name.split("")[0] == " ")
	{
		$(Id).find(".Row").eq(1).find(".Message").eq(1).show();
	}
	Class = null;
}

MqttModuleSecondPage.prototype._CheckMqttConnection_ = function()
{
	var MqttConnection = false;
	var Table = $("#Main_Center-Content").find(".Body").eq(2).find(".BrokerTable");
	var Tr    = Table.find(".BrokerTr");
	for(var i = 0; i < Tr.length; i++)
	{
		var Bool = Tr.eq(i).find(".BrokerTd").eq(0).find(".Checkbox").prop("checked");
		if(Bool == true)
		{
			MqttConnection = true;
			break;
		}
	}
	if(MqttConnection == false)
	{
		$("#Main_Center-Content").find(".Body").eq(2).find(".Form").find(".Row").eq(2).find(".Message").show();
	}else{
		$("#Main_Center-Content").find(".Body").eq(2).find(".Form").find(".Row").eq(2).find(".Message").hide();
	}
}

MqttModuleSecondPage.prototype._CheckTopic_ = function(that)
{
	$(that).closest(".DynamicRow").find(".Column").eq(5).find(".Message").hide();
	$(that).closest(".DynamicRow").find(".Column").eq(7).find(".Message").hide();
	var Class=new Type();
	var SubscribeTopic = $(that).closest(".DynamicRow").find(".Column").eq(5).find(".Text").val();
	var PublishTopic = $(that).closest(".DynamicRow").find(".Column").eq(7).find(".Text").val();
	var PublishTopicDisabled = $(that).closest(".DynamicRow").find(".Column").eq(7).find(".Text").attr('disabled');
	if(Class.CheckMqttTopic(SubscribeTopic) == false)
	{
		$(that).closest(".DynamicRow").find(".Column").eq(5).find(".Message").eq(0).show();
	}
	else if(SubscribeTopic == PublishTopic)
	{
		$(that).closest(".DynamicRow").find(".Column").eq(5).find(".Message").eq(1).show();
	}
	if(typeof(PublishTopicDisabled)=="undefined" && Class.CheckMqttTopic(PublishTopic) == false)
	{
		$(that).closest(".DynamicRow").find(".Column").eq(7).find(".Message").eq(0).show();
	}
	else if(typeof(PublishTopicDisabled)=="undefined" && SubscribeTopic == PublishTopic)
	{
		$(that).closest(".DynamicRow").find(".Column").eq(7).find(".Message").eq(1).show();
	}
}

MqttModuleSecondPage.prototype._CheckDataNumber_ = function(that)
{
	var Class  = new Type();
	var Length = $(that).val();
	Length = parseInt(Length);
	if(Class.CheckPositiveIntegers(Length) == false){
		$(that).val(1);
		Class = null;
		return 0;
	}
	Length = Class.CheckMqttDataModelMaxQuantity(Length);
	$(that).val(Length);
	Class = null;
}

MqttModuleSecondPage.prototype._CheckVariableName_ = function()
{
	var CurrentlyName;
	var AllName = MqttModuleSecondPage.prototype._GetTableVariableName_();
	var Element = $("#VariableTable").find(".DynamicRow");
	for(var i = 0; i < Element.length; i++)
	{
		Element.eq(i).find(".Message").hide();
		CurrentlyName = Element.eq(i).find(".Text").val();
		if(MqttModuleSecondPage.prototype._IsNameFormat_(CurrentlyName) == false)
		{
			 Element.eq(i).find(".Message").eq(1).show();
		}
		else if(MqttModuleSecondPage.prototype._IsNameRepeat_(AllName, CurrentlyName) == true)
		{
			 Element.eq(i).find(".Message").eq(0).show();
		}
	}
}

MqttModuleSecondPage.prototype._SetFormInitialValue_ = function(number)
{
	var DeviceName = MqttModuleFirstPage.prototype._GetDeviceName_();
	var ContentSettings = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0).find(".Row");
	ContentSettings.eq(0).find(".Text").val(number);
	ContentSettings.eq(1).find(".Text").val(Global_MqttModule[DeviceName][number]["ModuleName"]);
}

MqttModuleSecondPage.prototype._SetInitialValue_ = function(number)
{
	MqttModuleSecondPage.prototype._ClearForm_();
	MqttModuleSecondPage.prototype._ClearTable_();
	MqttModuleSecondPage.prototype._SetFormInitialValue_(number);
	var ContentSettings = $(".Form").eq(1).find(".Row");
	ContentSettings.eq(0).find(".Text").val(number);
	if(typeof(Global_MqttVariable[number]) == "undefined"){
		return 0;
	}
	MqttModuleSecondPage.prototype._SetVariableTableInitialValue_(number);
}

MqttModuleSecondPage.prototype._SetBrokerValue_ = function(number, Table, RemoteNumber)
{
	var obj = {Index:0};  var BrokerName;
	var Tr 	= Table.find(".BrokerTr");
	var BrokerName = Global_MqttSetting[number]["MqttConnection"];
	MqttModuleSecondPage.prototype._SetLocalBrokerName_(Tr, obj, BrokerName);
	MqttModuleSecondPage.prototype._SetRemoteBrokerName_(Tr, obj, RemoteNumber, BrokerName);
	MqttModuleSecondPage.prototype._CheckMqttConnection_();
}

MqttModuleSecondPage.prototype._SetLocalBrokerName_ = function(Tr, obj, BrokerName)
{
	var array = new Array("Broker (Local)");
	var Name  = MqttModuleSecondPage.prototype._ArrayInteraction_(BrokerName, array);
	if(Name.length > 0)
	{
		Tr.eq(obj.Index).find(".BrokerTd").eq(0).find(".Checkbox").prop("checked",true);
	}
	Tr.eq(obj.Index).find(".BrokerTd").eq(1).text("Broker (Local)");
}

MqttModuleSecondPage.prototype._SetRemoteBrokerName_ = function(Tr, obj, RemoteNumber, BrokerName)
{
	for(var i = 0; i < RemoteNumber.length; i++)
	{
		obj.Index = obj.Index+1;
		var RemoteBroker = Global_RemoteBroker[RemoteNumber[i]]["Name"];
		var array = new Array(RemoteBroker+" (Remote)");
		Name = MqttModuleSecondPage.prototype._ArrayInteraction_(BrokerName, array);
		if(Name.length > 0)
		{
			Tr.eq(obj.Index).find(".BrokerTd").eq(0).find(".Checkbox").prop("checked",true);
		}
		Tr.eq(obj.Index).find(".BrokerTd").eq(1).text(Global_RemoteBroker[RemoteNumber[i]]["Name"]+" (Remote)");
	}
}

MqttModuleSecondPage.prototype._SetVariableTableRow_ = function(Data)
{
	var DynamicRow = $("#VariableTable").find(".DynamicRow");
	var VariableIndex = Object.keys(Data);
	var Class=new Algorithm();
	VariableIndex=VariableIndex.sort(Class.StringNumberMixedSort());
	Class=new GetValue();
	for(var i = 0; i < VariableIndex.length; i++)
	{
		DynamicRow.eq(i).find(".Column").eq(0).text(VariableIndex[i]);
		DynamicRow.eq(i).find(".Column").eq(2).find(".Text").val(Data[VariableIndex[i]]["VariableName"]);
		DynamicRow.eq(i).find(".Column").eq(3).find(".Select").val(Class.GetModuleVariableReadWrite(VariableIndex[i].split("-")[0]));
		DynamicRow.eq(i).find(".Column").eq(4).text(VariableIndex[i].split("-")[1]);
		DynamicRow.eq(i).find(".Column").eq(5).find(".Text").val(Data[VariableIndex[i]]["SubscribeTopic"]);
		DynamicRow.eq(i).find(".Column").eq(6).find(".Select").val(Data[VariableIndex[i]]["SubscribeQoS"]);
		MqttModuleSecondPage.prototype._DecideColumnEnable_(Data[VariableIndex[i]]["PublishTopic"], DynamicRow.eq(i));
		DynamicRow.eq(i).find(".Column").eq(7).find(".Text").val(Data[VariableIndex[i]]["PublishTopic"]);
		DynamicRow.eq(i).find(".Column").eq(8).find(".Select").val(Data[VariableIndex[i]]["PublishQoS"]);
		DynamicRow.eq(i).find(".Column").eq(9).find(".Text").val(Data[VariableIndex[i]]["Description"]);
		DynamicRow.eq(i).find(".Column").eq(10).find(":checkbox").prop("checked", Data[VariableIndex[i]]["Retain"]);
	}
}

MqttModuleSecondPage.prototype._SetVariableTableInitialValue_ = function(number)
{
	var VariableIndex=Object.keys(Global_MqttVariable[number]);
	MqttModuleSecondPage.prototype._VariableTableAddNewRow_(VariableIndex.length);
	MqttModuleSecondPage.prototype._SetVariableTableRow_(Global_MqttVariable[number]);
}

MqttModuleSecondPage.prototype._GetTableVariableName_ = function()
{
	var Length, Element, AllName = new Array();
	Element = $("#VariableTable").find(".DynamicRow");
	Length = Element.length;
	for(var i = 0; i < Length; i++)
	{
		AllName.push(Element.eq(i).find(".Text").val());
	}
	var Class=new Algorithm();
	AllName = AllName.sort(Class.StringNumberMixedSort());
	return AllName;
}

MqttModuleSecondPage.prototype._GetMqttConnection_ = function()
{
	var Return = new Array();  var Bool;
	var BrokerTable = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0).find(".Row").eq(2).find(".BrokerTable");
	var BrokerTr = BrokerTable.find(".BrokerTr");
	for(var i = 0; i < BrokerTr.length; i++)
	{
		Bool = BrokerTr.eq(i).find(".BrokerTd").eq(0).find(".Checkbox").prop("checked");
		if(Bool == true)
		{
			Return.push(BrokerTr.eq(i).find(".BrokerTd").eq(1).text());
		}
	}
	return Return;
}

MqttModuleSecondPage.prototype._GetSettingFormValue_ = function()
{
	var object = new Object();
	object["LastAddress"]	= $("#VariableTable").find(".DynamicRow").last().find(".Column").eq(0).text().split("-")[2];
	object["AllAddress"] 	= $("#VariableTable").find(".DynamicRow").length;
	object["Attribute"] 	= $("#SettingForm").find(".Row").eq(0).find(":selected").val();
	object["DataType"] 		= $("#SettingForm").find(".Row").eq(1).find(":selected").text();
	object["DataNumber"]	= parseInt($("#SettingForm").find(".Row").eq(2).find(".Text").val());
	return object;
}

MqttModuleSecondPage.prototype._GetVariableTableRowData_ = function()
{
	var Return=new Object();
	var DynamicRow=$("#VariableTable").find(".DynamicRow");
	for(var i=0; i<DynamicRow.length; i++)
	{
		var VariableIndex =DynamicRow.eq(i).find(".Column").eq(0).text();
		Return[VariableIndex]=new Object();
		var VariableName  =DynamicRow.eq(i).find(".Column").eq(2).find(".Text").val();
		var SubscribeTopic=DynamicRow.eq(i).find(".Column").eq(5).find(".Text").val();
		var SubscribeQoS  =DynamicRow.eq(i).find(".Column").eq(6).find(".Select").val();
		var PublishTopic  =DynamicRow.eq(i).find(".Column").eq(7).find(".Text").val();
		var PublishQoS    =DynamicRow.eq(i).find(".Column").eq(8).find(".Select").val();
		var Description   =DynamicRow.eq(i).find(".Column").eq(9).find(".Text").val();
		var Retain        =DynamicRow.eq(i).find(".Column").eq(10).find(":checkbox").prop("checked");
		Return[VariableIndex]["VariableName"]  =VariableName;
		Return[VariableIndex]["SubscribeTopic"]=SubscribeTopic;
		Return[VariableIndex]["SubscribeQoS"]  =SubscribeQoS;
		Return[VariableIndex]["PublishTopic"]  =PublishTopic;
		Return[VariableIndex]["PublishQoS"]    =PublishQoS;
		Return[VariableIndex]["Description"]   =Description;
		Return[VariableIndex]["Retain"]        =Retain;
	}
	return Return;
}

MqttModuleSecondPage.prototype._GetMaxAddress_ = function(CurrentData)
{
	var VariableIndex=Object.keys(CurrentData);
	var Return=new Array();
	for(var i=0; i<VariableIndex.length; i++)
	{
		Return.push(parseInt(VariableIndex[i].split("-")[2]));
	}
	Return=Return.sort(function(a, b){return b - a});
	return Return[0];
}

MqttModuleSecondPage.prototype._GetStartAddress_ = function(CurrentData)
{
	var Return=1;
	var MaxAddress=MqttModuleSecondPage.prototype._GetMaxAddress_(CurrentData);
	if(typeof(MaxAddress) == "undefined")
	{
		MaxAddress=0;
	}
	Return=MaxAddress+1;
	return Return;
}

MqttModuleSecondPage.prototype._GetPublishTopic_ = function(VariableName)
{
	var Row = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0).find(".Row");
	var number = parseInt(Row.eq(0).find(".Text").val());
	var ModuleName = Row.eq(1).find(".Text").val();
	var ReadWrite=$("#SettingForm").find(".Row").eq(0).find(".Select").val();
	var Return="";
	if(ReadWrite == "ReadWrite")
	{
		Return="/MQTT_No."+number+"_"+ModuleName+"/"+VariableName+"/Publish";
	}
	return Return;
}

MqttModuleSecondPage.prototype._GetSubscribeTopic_ = function(VariableName)
{
	var Row = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0).find(".Row");
	var number = parseInt(Row.eq(0).find(".Text").val());
	var ModuleName = Row.eq(1).find(".Text").val();
	return "/MQTT_No."+number+"_"+ModuleName+"/"+VariableName+"/Subscribe";
}

MqttModuleSecondPage.prototype._ClearTable_ = function()
{
	$("#VariableTable").find(".DynamicRow").remove();
}

MqttModuleSecondPage.prototype._RemoveTickedVariable_ = function()
{
	var Id = "#VariableTable";
	var element = $(Id).find(".Checkbox:checked");
	element.closest(".DynamicRow").remove();
}

MqttModuleSecondPage.prototype._ClearForm_ = function()
{
	$("#SettingForm").find(".Row").eq(0).find(".Select").val("Read");
	$("#SettingForm").find(".Row").eq(1).find(".Select").val("Bool");
	$("#SettingForm").find(".Row").eq(2).find(".Text").val(1);
	$("#VariableTable").find(".Row").find(".TitleColumn").eq(10).find(":checkbox").attr('checked', false);
	MqttModuleSecondPage.prototype._HideColumn_();
	$("#SettingForm").find(".Message").hide();
}

MqttModuleSecondPage.prototype._DeleteButton_ = function()
{
	MqttModuleSecondPage.prototype._RemoveTickedVariable_();
}

MqttModuleSecondPage.prototype._DeleteTableButton_ = function()
{
	var button = $('#SettingForm').find(".Button").offset();
	var MouseX = (button.left-30)+"px";
	var MouseY = (button.top-93)+"px";
	$(".Dialog").eq(2).css('left', MouseX);
	$(".Dialog").eq(2).css('top', MouseY);
	var Class = new Dialog();
	Class.CommonDisabled();
	Class = null;
	$("#Main_Center-Content").find(".Body").eq(2).css('pointer-events', "none");
	$("#Main_Center-Content").find(".Body").eq(2).css('opacity', 0.5);
	$(".Dialog").eq(2).css('display', "block");
}

MqttModuleSecondPage.prototype._DeleteTable_ = function()
{
	MqttModuleSecondPage.prototype._ClearTable_();
	MqttModuleSecondPage.prototype._NotDeleteTableButton_();
}

MqttModuleSecondPage.prototype._NotDeleteTableButton_ = function()
{
	$(".Dialog").eq(2).find(".Message").hide();
	var Class = new Dialog();
	Class.CommonEnabled();
	Class = null;
	$("#Main_Center-Content").find(".Body").eq(2).css('pointer-events', "visible");
	$("#Main_Center-Content").find(".Body").eq(2).css('opacity', 1);
	$(".Dialog").eq(2).css('display', "none");
}

MqttModuleSecondPage.prototype._ShowColumn_ = function()
{
	$("#VariableTable").find(".Row").find(".TitleColumn").eq(6).show();
	$("#VariableTable").find(".Row").find(".TitleColumn").eq(8).show();
	$("#VariableTable").find(".Row").find(".TitleColumn").eq(10).show();
	var DynamicRow = $("#VariableTable").find(".DynamicRow");
	for(var i = 0; i < DynamicRow.length; i++)
	{
		DynamicRow.eq(i).find(".Column").eq(6).show();
		DynamicRow.eq(i).find(".Column").eq(8).show();
		DynamicRow.eq(i).find(".Column").eq(10).show();
	}
}

MqttModuleSecondPage.prototype._HideColumn_ = function()
{
	$("#VariableTable").find(".Row").find(".TitleColumn").eq(6).hide();
	$("#VariableTable").find(".Row").find(".TitleColumn").eq(8).hide();
	$("#VariableTable").find(".Row").find(".TitleColumn").eq(10).hide();
	var DynamicRow = $("#VariableTable").find(".DynamicRow");
	for(var i = 0; i < DynamicRow.length; i++)
	{
		DynamicRow.eq(i).find(".Column").eq(6).hide();
		DynamicRow.eq(i).find(".Column").eq(8).hide();
		DynamicRow.eq(i).find(".Column").eq(10).hide();
	}
}

MqttModuleSecondPage.prototype._InsertVariableTableRowData_ = function(CurrentData)
{
	var Class=new GetValue();
	var ReadWrite =$("#SettingForm").find(".Row").eq(0).find(".Select").val();
	var DataType  =$("#SettingForm").find(".Row").eq(1).find(".Select").val();
	var DataNumber=$("#SettingForm").find(".Row").eq(2).find(".Text").val();
	var Start=MqttModuleSecondPage.prototype._GetStartAddress_(CurrentData);
	var End=Start+parseInt(DataNumber);
	for(var i=Start; i<End; i++)
	{
		var VariableIndex=Class.GetModuleVariableAttribute(ReadWrite, DataType)+"-"+DataType+"-"+i;
		CurrentData[VariableIndex]=new Object();
		CurrentData[VariableIndex]["VariableName"]  ="Tag"+i;
		CurrentData[VariableIndex]["SubscribeTopic"]=MqttModuleSecondPage.prototype._GetSubscribeTopic_(CurrentData[VariableIndex]["VariableName"]);
		CurrentData[VariableIndex]["SubscribeQoS"]  =2;
		CurrentData[VariableIndex]["PublishTopic"]  =MqttModuleSecondPage.prototype._GetPublishTopic_(CurrentData[VariableIndex]["VariableName"]);
		CurrentData[VariableIndex]["PublishQoS"]    =2;
		CurrentData[VariableIndex]["Description"]   ="";
		CurrentData[VariableIndex]["Retain"]        =false;
	}
	return CurrentData;
}

MqttModuleSecondPage.prototype._IsNameFormat_ = function(Name)
{
	var Return = true, Class = new Type();
	if(Class.CheckChineseLanguage(Name) == true ||
	   Class.CheckSpecialSymbols(Name) 	== true ||
	   Name == "")
	{
		Return = false;
	}
	return Return;
}

MqttModuleSecondPage.prototype._IsNameRepeat_ = function(AllName, Name)
{
	var Return=false, Index=1;
	for(var i=0;i<=AllName.length;i++)
	{
		if (Index > 2)
		{
			Return=true;
			break;
		}
		if (Name==AllName[i] && Name!="")
		{
			Index++;
		}
	}
	return Return;
}

MqttModuleSecondPage.prototype._LoadBroker_ = function(number)
{
	var Body = $("#Main_Center-Content").find(".Body").eq(2);
	var Html = Body.find(".MqttConnectionDynamic").html();
	var Form = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0);
	var Row  = Form.find(".Row").eq(2);
	var Table= Row.find(".BrokerTable");
	Table.empty();
	var RemoteNumber = Object.keys(Global_RemoteBroker);
	MqttModuleSecondPage.prototype._AddBrokerHtml_(Table, RemoteNumber);
	MqttModuleSecondPage.prototype._SetBrokerValue_(number, Table, RemoteNumber);
}

MqttModuleSecondPage.prototype._AddBrokerHtml_ = function(Table, RemoteNumber)
{
	var Html = $("#Main_Center-Content").find(".Body").eq(2).find(".MqttConnectionDynamic").html();
	$(Table).append(Html);
	for(var i = 0; i < RemoteNumber.length; i++)
	{
		$(Table).append(Html);
	}
}

MqttModuleSecondPage.prototype._BuildMqttVariableObject_ = function(number)
{
	Global_MqttVariable[number] = new Object();
	var GlobalVariable=Global_MqttVariable[number];
	var DynamicRow=$("#VariableTable").find(".DynamicRow");
	for(var i=0; i<DynamicRow.length; i++)
	{
		var VariableIndex =DynamicRow.eq(i).find(".Column").eq(0).text();
		var VariableName  =DynamicRow.eq(i).find(".Column").eq(2).find(".Text").val();
		var SubscribeTopic=DynamicRow.eq(i).find(".Column").eq(5).find(".Text").val();
		var SubscribeQoS  =DynamicRow.eq(i).find(".Column").eq(6).find(".Select").val();
		var PublishTopic  =DynamicRow.eq(i).find(".Column").eq(7).find(".Text").val();
		var PublishQoS    =DynamicRow.eq(i).find(".Column").eq(8).find(".Select").val();
		var Description   =DynamicRow.eq(i).find(".Column").eq(9).find(".Text").val();
		var Retain        =DynamicRow.eq(i).find(".Column").eq(10).find(":checkbox").prop("checked");
		GlobalVariable[VariableIndex]=new Object();
		GlobalVariable[VariableIndex]["VariableName"]  =VariableName;
		GlobalVariable[VariableIndex]["SubscribeTopic"]=SubscribeTopic;
		GlobalVariable[VariableIndex]["SubscribeQoS"]  =SubscribeQoS;
		GlobalVariable[VariableIndex]["PublishTopic"]  =PublishTopic;
		GlobalVariable[VariableIndex]["PublishQoS"]    =PublishQoS;
		GlobalVariable[VariableIndex]["Description"]   =Description;
		GlobalVariable[VariableIndex]["Retain"]        =Retain;
	}
}

MqttModuleSecondPage.prototype._BuildMqttSettingObject_ = function(number)
{
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row");
	Global_MqttSetting[number]["MqttConnection"] = MqttModuleSecondPage.prototype._GetMqttConnection_();
}

MqttModuleSecondPage.prototype._ProduceTable_ = function(that)
{
	var Obj = MqttModuleSecondPage.prototype._GetSettingFormValue_();
	$(that).closest(".Row").find(".Message").hide();
	$(that).closest(".Row").find(".Message").eq(1).show();
	var CheckDisplay = $("#VariableTable").find(".Row").eq(0).find(".TitleColumn").eq(6).css("display");
	var Column=$("#Main_Center-Content").find(".Body").eq(2).find(".Dynamic").find(".DynamicRow").find(".Column");
    if (CheckDisplay == "table-cell") 
    {
        Column.eq(6).css("display", "table-cell");
        Column.eq(8).css("display", "table-cell");
        Column.eq(10).css("display", "table-cell");
    } else {
        Column.eq(6).css("display", "none");
        Column.eq(8).css("display", "none");
        Column.eq(10).css("display", "none");
    }

	setTimeout(function(){
		MqttModuleSecondPage.prototype._CreateTable_();
		$(that).closest(".Row").find(".Message").hide();
	}, 500);
}

MqttModuleSecondPage.prototype._CreateTable_ = function()
{
	var CurrentData=MqttModuleSecondPage.prototype._GetVariableTableRowData_();
	CurrentData=MqttModuleSecondPage.prototype._InsertVariableTableRowData_(CurrentData);
	var DataNumber=$("#SettingForm").find(".Row").eq(2).find(".Text").val();
	MqttModuleSecondPage.prototype._VariableTableAddNewRow_(DataNumber);
	MqttModuleSecondPage.prototype._SetVariableTableRow_(CurrentData);
	MqttModuleSecondPage.prototype._CheckVariableName_();
}

MqttModuleSecondPage.prototype._VariableTableAddNewRow_ = function(RowNumber)
{
	var html = "",TempHtml = $("#Main_Center-Content").find(".Body").eq(2).find(".Dynamic").html();	
    for (var i = 0; i < RowNumber; i++) {
        html = html + TempHtml;
    }
    $("#VariableTable").append(html);
}

MqttModuleSecondPage.prototype._DecideColumnEnable_ = function(PublishTopic, DynamicRow)
{
	if(PublishTopic == "")
	{
		DynamicRow.find(".Column").eq(7).find(".Text").attr('disabled', true);
		DynamicRow.find(".Column").eq(8).find(".Select").attr('disabled', true);
	}
	else
	{
		DynamicRow.find(".Column").eq(7).find(".Text").attr('disabled', false);
		DynamicRow.find(".Column").eq(8).find(".Select").attr('disabled', false);
	}
}

MqttModuleSecondPage.prototype._ArrayInteraction_ = function(FirstArray, SecondArray)
{
	var Return = new Array();
	for(var i = 0; i < FirstArray.length; i++){
		if(SecondArray.indexOf(FirstArray[i]) != -1){
			Return.push(FirstArray[i]);
		}
	}
	return Return;
}

MqttModuleSecondPage.prototype._RetainCheckbox_ = function(that)
{
	var DataModelId = $(that).closest(".Table").attr("id");
	var Bool = $(that).closest(".Row").find(":checkbox").eq(0).prop("checked");
	$("#"+ DataModelId).find(".DynamicRow").find(".RetainCheckbox").prop("checked",Bool);
}

MqttModuleSecondPage.prototype._SynchronizeOtherServiceData_ = function(number)
{
	if(typeof(Global_MqttVariable[number]) != "object") {return 0;}
	var object = MqttModuleSecondPage.prototype._CreateVariableEmptyObject_(number);
	var MqttToOpcua = MqttModuleSecondPage.prototype._InitialMqttToOpcuaEmptyObject_(object, number);
	MqttModuleSecondPage.prototype._RetainMqttToOpcuaData_(MqttToOpcua, number);
	var MqttToMysql = MqttModuleSecondPage.prototype._InitialMqttToMysqlEmptyObject_(object, number);
	MqttModuleSecondPage.prototype._RetainMqttToMysqlData_(MqttToMysql, number);
	var MqttToMsSql = MqttModuleSecondPage.prototype._InitialMqttToMsSqlEmptyObject_(object, number);
	MqttModuleSecondPage.prototype._RetainMqttToMsSqlData_(MqttToMsSql, number);
}

MqttModuleSecondPage.prototype._InitialMqttToOpcuaEmptyObject_ = function(object, number)
{
	object = jQuery.parseJSON(JSON.stringify(object));
	var SystemVariable = Object.keys(object[number]);
	for(var i = 0; i < SystemVariable.length; i++)
	{
		object[number][SystemVariable[i]]["TransformOpcUa"] = false;
	}
	return object;
}

MqttModuleSecondPage.prototype._RetainMqttToOpcuaData_ = function(object, number)
{
	if(typeof(Global_MqttToOpcuaVariable[number]) != "object"){
		Global_MqttToOpcuaVariable[number] = new Object();
	}
	var NewSystemVariable = Object.keys(object[number]);
	var OldSystemVariable = Object.keys(Global_MqttToOpcuaVariable[number]);
	var SameVariable 	  = MqttModuleSecondPage.prototype._ArrayInteraction_(NewSystemVariable, OldSystemVariable);
	for(var i = 0; i < SameVariable.length; i++)
	{
		object[number][SameVariable[i]] = Global_MqttToOpcuaVariable[number][SameVariable[i]];
	}
	Global_MqttToOpcuaVariable[number] 	= jQuery.parseJSON(JSON.stringify(object[number]));
}

MqttModuleSecondPage.prototype._InitialMqttToMysqlEmptyObject_ = function(object, number)
{
	object = jQuery.parseJSON(JSON.stringify(object));
	var SystemVariable = Object.keys(object[number]);
	for(var i = 0; i < SystemVariable.length; i++)
	{
		object[number][SystemVariable[i]]["DatabaseIndex"]  = "";
		object[number][SystemVariable[i]]["DatabaseType"]   = "Remote";
		object[number][SystemVariable[i]]["DatabaseRecord"] = false;
	}
	return object;
}

MqttModuleSecondPage.prototype._RetainMqttToMysqlData_ = function(object, number)
{
	if(typeof(Global_MqttToMysqlVariable[number]) != "object"){
		Global_MqttToMysqlVariable[number] = new Object();
	}
	var NewSystemVariable = Object.keys(object[number]);
	var OldSystemVariable = Object.keys(Global_MqttToMysqlVariable[number]);
	var SameVariable 	  = MqttModuleSecondPage.prototype._ArrayInteraction_(NewSystemVariable, OldSystemVariable);
	for(var i = 0; i < SameVariable.length; i++)
	{
		object[number][SameVariable[i]] = Global_MqttToMysqlVariable[number][SameVariable[i]];
	}
	Global_MqttToMysqlVariable[number] 	= jQuery.parseJSON(JSON.stringify(object[number]));
}

MqttModuleSecondPage.prototype._InitialMqttToMsSqlEmptyObject_ = function(object, number)
{
	object = jQuery.parseJSON(JSON.stringify(object));
	var SystemVariable = Object.keys(object[number]);
	for(var i = 0; i < SystemVariable.length; i++)
	{
		object[number][SystemVariable[i]]["DatabaseIndex"]  = "";
		object[number][SystemVariable[i]]["DatabaseType"]   = "Remote";
		object[number][SystemVariable[i]]["DatabaseRecord"] = false;
	}
	return object;
}

MqttModuleSecondPage.prototype._RetainMqttToMsSqlData_ = function(object, number)
{
	if(typeof(Global_MqttToMsSqlVariable[number]) != "object"){
		Global_MqttToMsSqlVariable[number] = new Object();
	}
	var NewSystemVariable = Object.keys(object[number]);
	var OldSystemVariable = Object.keys(Global_MqttToMsSqlVariable[number]);
	var SameVariable 	  = MqttModuleSecondPage.prototype._ArrayInteraction_(NewSystemVariable, OldSystemVariable);
	for(var i = 0; i < SameVariable.length; i++)
	{
		object[number][SameVariable[i]] = Global_MqttToMsSqlVariable[number][SameVariable[i]];
	}
	Global_MqttToMsSqlVariable[number] 	= jQuery.parseJSON(JSON.stringify(object[number]));
}

MqttModuleSecondPage.prototype._CreateVariableEmptyObject_ = function(number)
{
	var Return = new Object();
	var object = jQuery.parseJSON(JSON.stringify(Global_MqttVariable[number]));
	var SystemVariable = Object.keys(object);
	for(var i = 0; i < SystemVariable.length; i++)
	{
		object[SystemVariable[i]] = null;			delete object[SystemVariable[i]];
		object[SystemVariable[i]] = new Object();
	}
	Return[number] = new Object();
	Return[number] = object;
	return Return;
}

MqttModuleSecondPage.prototype._ConfirmButton_ = function(that)
{
	var MessageError = $("#Main_Center-Content").find(".Body").eq(2).find("#VariableTable").find(".Message").is(":visible");
	if(MessageError == true)
	{
		$(that).closest(".Row").find(".Message").show();
		return 0;
	}
	var ContentSettings = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0).find(".Row");
	var number = parseInt(ContentSettings.eq(0).find(".Text").val());
	var ModuleName = ContentSettings.eq(1).find(".Text").val();
	MqttModuleFirstPage.prototype._BuildMqttModuleObject_(ModuleName, number);
	MqttModuleSecondPage.prototype._BuildMqttSettingObject_(number);
	MqttModuleSecondPage.prototype._BuildMqttVariableObject_(number);
	MqttModuleSecondPage.prototype._SynchronizeOtherServiceData_(number);
	$("#Main_Center-Content").find(".Body").eq(2).find(".FormTitle").eq(0).text("");
	$("#Main_Center-Content").find(".Body").eq(2).hide();
	$("#Main_Center-Content").find(".Body").eq(1).show();
	$("#Main_Center-Content").css("border-left-width", "1px");
	$("#Main_Center-Content").css("width", "795px");
	$("#Main_Left-List").show();
	MqttModuleFirstPage.prototype._RedisplayUi_();
	var Class = new Path();
	Class.LoadPathHtml($("#Left-List-MQTT").text(), 1, "main_PathMqttModule_click(this)");
	Class = null;
}

MqttModuleSecondPage.prototype._NoConfirmButton_ = function()
{
	$("#Main_Center-Content").find(".Body").eq(2).find(".FormTitle").eq(0).text("");
	$("#Main_Center-Content").find(".Body").eq(2).hide();
	$("#Main_Center-Content").find(".Body").eq(1).show();
	$("#Main_Center-Content").css("border-left-width", "1px");
	$("#Main_Center-Content").css("width", "795px");
	$("#Main_Left-List").show();
	var Class = new Path();
	Class.LoadPathHtml($("#Left-List-MQTT").text(), 1, "main_PathMqttModule_click(this)");
	Class = null;
}