//共用



MqttToIoTCloudCommon.prototype._GetMqttJsonGroupName_ = function()
{
	var Return=new Array();
	var Class = new LoadFile();
	var JSON = Class.ReadFile("./File/IoT_Platform/MqttJsonGroup.json", new Object());
	var Index=Object.keys(JSON);
	for(var i=0; i<Index.length; i++)
	{
		if(JSON[Index[i]]["Name"] != "")
		{
			Return.push(JSON[Index[i]]["Name"]);
		}
	}
	return Return;
}
MqttToIoTCloudFirstPage.prototype._AddNewRow_ = function()
{
	MqttToIoTCloudFirstPage.prototype._CheckGroupName_();
	var Id = "#GroupTable";
	var ShowErrorMessage = $(Id).find(".Row").eq(0).find(".Message").is(":visible");
	if(ShowErrorMessage == true)	{return 0;}
	MqttToIoTCloudFirstPage.prototype._InitialRemoteMqttGroupObject_();
	MqttToIoTCloudFirstPage.prototype._RedisplayUi_();
	MqttToIoTCloudFirstPage.prototype._AutomaticGroupName_();
}
MqttToIoTCloudFirstPage.prototype._CheckGroupName_ = function(that)
{
	var Id = "#GroupTable";
	$(Id).find(".Message").hide();
	var Row=$(Id).find(".Row");
	var Name = Row.eq(1).find(".Text").val();
	var Class = new Type();
	if(Class.EnglishDigitalAndBottomline(Name) == false)
	{
		Row.eq(0).find(".Message").eq(0).show();
	}
	Class = null;
	if(MqttToIoTCloudFirstPage.prototype._IsNameRepeat_(Name) == true)
	{
		Row.eq(0).find(".Message").eq(1).show();
	}
}
MqttToIoTCloudFirstPage.prototype._AutomaticGroupName_ = function()
{
	var Table = $("#Main_Center-Content").find(".Body").eq(1).find(".Table").eq(0);
	var Index = 1;
	var object = jQuery.parseJSON(JSON.stringify(Global_RemoteMqttGroup));
	var number = Object.keys(object);
	var Name, NameArray=new Array();
	for(var i=0; i<number.length; i++)
	{
		NameArray.push(object[number[i]]["Name"]);
	}
	while(true)
	{
		Name = "Name"+Index;
		if(NameArray.indexOf(Name) == -1)
		{
			break;
		}
		Index++;
	}
	Table.find(".Row").eq(1).find(".Text").val(Name);
}

MqttToIoTCloudFirstPage.prototype._IsNameRepeat_ = function(Name)
{
	var Return=false;
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row");
	var CurrentlyNumber = Row.eq(0).find(".Text").val();
	var number = Object.keys(Global_RemoteMqttGroup);
	for(var i=0; i<number.length; i++)
	{
		if(Name == Global_RemoteMqttGroup[number[i]]["Name"])
		{
			Return=true;
			break;
		}
	}
	return Return;
}
MqttToIoTCloudFirstPage.prototype._InitialRemoteMqttGroupObject_ = function(that)
{
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find("#ModuleTable").eq(0).find(".Row");
	var number = $(that).closest(".Row").find(".Column").eq(2).find(".Select").val();
	if(typeof(Global_RemoteMqttGroup[number]) == "undefined")
	{
		Global_RemoteMqttGroup[number] = new Object();
		Global_RemoteMqttGroup[number]["Name"] = Row.find(".Column").eq(3).find(".Text").val();			
		Global_RemoteMqttGroup[number]["DataMessage"] = "";
		Global_RemoteMqttGroup[number]["ConnectionGroup"] = "";
		Global_RemoteMqttGroup[number]["TransformMqttCustom"] = true;
	}
}
MqttToIoTCloudCommon.prototype._GetAzureName_ = function()
{
	var Return=new Array();
	var Class = new LoadFile();
	var JSON = Class.ReadFile("./File/IoT_Platform/Microsoft_Azure_Platform.json", new Object());
	var Index=Object.keys(JSON);
	for(var i=0; i<Index.length; i++)
	{
		if(JSON[Index[i]]["Name"] != "")
		{
			Return.push(JSON[Index[i]]["Name"]+" (Azure)");
		}
	}
	return Return;
}

MqttToIoTCloudCommon.prototype._GetConnectionName_ = function()
{
	var Return = new Array();
	var MqttJsonGroupName = MqttToIoTCloudCommon.prototype._GetMqttJsonGroupName_();
	var AzureName = MqttToIoTCloudCommon.prototype._GetAzureName_();
	Return = MqttJsonGroupName.concat(AzureName);
	return Return;
}




















//更新頁
MqttToIoTCloudUpdatePage.prototype._SaveGlobalVariable_ = function(Row)
{
	var Class = new StepFlow();
	Class.LockAllUi();
	var ssocket = io.connect(), Index=0, Length=Row.length;
	Row.eq(Index).find(".Button").click();
	ssocket.emit("BackgroundExecution");
	ssocket.on("BackgroundExecution", function (Return) {
		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();
			ssocket.emit("BackgroundExecution");
			if(Index==Length)
			{
				ssocket.emit("SocketClose");
				MqttToIoTCloudUpdatePage.prototype._ShowFirstPage_();
				Class.UnLockAllUi();
			}
		}
		else if(Index<Length)
		{
			ssocket.emit("BackgroundExecution");
		}
	});
}

MqttToIoTCloudUpdatePage.prototype._IsSaveGlobalVariable_ = function()
{
var Return=false;
	var Class = new LoadFile();
	var Rtu_Index = Class.ReadFile("./File/RTU_Module/Index.json", new Array());
	var Rtu_Module = Class.ReadFile("./File/RTU_Module/Module.json", new Object());
	var Rtu_Variable = Class.ReadFile("./File/RTU_Module/RtuVariable.json", new Object());
	var Rtu_Setting = Class.ReadFile("./File/RTU_Module/RtuSetting.json", new Object());
	
	var Tcp_Index = Class.ReadFile("./File/TCP_Module/Index.json", new Array());
	var Tcp_Module = Class.ReadFile("./File/TCP_Module/Module.json", new Object());
	var Tcp_Variable = Class.ReadFile("./File/TCP_Module/TcpVariable.json", new Object());
	var Tcp_Setting = Class.ReadFile("./File/TCP_Module/TcpSetting.json", new Object());

	var Ascii_Index = Class.ReadFile("./File/ASCII_Module/Index.json", new Array());
	var Ascii_Module = Class.ReadFile("./File/ASCII_Module/Module.json", new Object());
	var Ascii_Variable = Class.ReadFile("./File/ASCII_Module/AsciiVariable.json", new Object());
	var Ascii_Setting = Class.ReadFile("./File/ASCII_Module/AsciiSetting.json", new Object());

	var Mqtt_Index = Class.ReadFile("./File/MQTT_Module/Index.json", new Array());
	var Mqtt_Module = Class.ReadFile("./File/MQTT_Module/Module.json", new Object());
	var Mqtt_Variable = Class.ReadFile("./File/MQTT_Module/MqttVariable.json", new Object());
	var Mqtt_Setting = Class.ReadFile("./File/MQTT_Module/MqttSetting.json", new Object());

	var Eip_Index = Class.ReadFile("./File/EIP_Module/Index.json", new Array());
	var Eip_Module = Class.ReadFile("./File/EIP_Module/Module.json", new Object());
	var Eip_Variable = Class.ReadFile("./File/EIP_Module/EipVariable.json", new Object());
	var Eip_Setting = Class.ReadFile("./File/EIP_Module/EipSetting.json", new Object());

	var Register_Index = Class.ReadFile("./File/Register_Module/Index.json", new Array());
	var Register_Module = Class.ReadFile("./File/Register_Module/Module.json", new Object());
	var Register_Variable = Class.ReadFile("./File/Register_Module/RegisterVariable.json", new Object());
	var Register_Setting = Class.ReadFile("./File/Register_Module/RegisterSetting.json", new Object());

	var RemoteMqttGroup = Class.ReadFile("./File/RemoteMQTTGroup/RemoteMqttGroup.json", new Object());

	var MqttJsonGroup = Class.ReadFile("./File/IoT_Platform/MqttJsonGroup.json", new Object());
	var AzurePlatform = Class.ReadFile("./File/IoT_Platform/Microsoft_Azure_Platform.json", new Object());
	if(JSON.stringify(Rtu_Index)==JSON.stringify(Global_RtuIndex) &&
	   JSON.stringify(Rtu_Module)==JSON.stringify(Global_RtuModule) &&
	   JSON.stringify(Rtu_Variable)==JSON.stringify(Global_RtuVariable) &&
	   JSON.stringify(Rtu_Setting)==JSON.stringify(Global_RtuSetting) &&

	   JSON.stringify(Tcp_Index)==JSON.stringify(Global_TcpIndex) &&
	   JSON.stringify(Tcp_Module)==JSON.stringify(Global_TcpModule) &&
	   JSON.stringify(Tcp_Variable)==JSON.stringify(Global_TcpVariable) &&
	   JSON.stringify(Tcp_Setting)==JSON.stringify(Global_TcpSetting) &&

	   JSON.stringify(Ascii_Index)==JSON.stringify(Global_AsciiIndex) &&
	   JSON.stringify(Ascii_Module)==JSON.stringify(Global_AsciiModule) &&
	   JSON.stringify(Ascii_Variable)==JSON.stringify(Global_AsciiVariable) &&
	   JSON.stringify(Ascii_Setting)==JSON.stringify(Global_AsciiSetting) &&

	   JSON.stringify(Mqtt_Index)==JSON.stringify(Global_MqttIndex) &&
	   JSON.stringify(Mqtt_Module)==JSON.stringify(Global_MqttModule) &&
	   JSON.stringify(Mqtt_Variable)==JSON.stringify(Global_MqttVariable) &&
	   JSON.stringify(Mqtt_Setting)==JSON.stringify(Global_MqttSetting) &&

	   JSON.stringify(Eip_Index)==JSON.stringify(Global_EipIndex) &&
	   JSON.stringify(Eip_Module)==JSON.stringify(Global_EipModule) &&
	   JSON.stringify(Eip_Variable)==JSON.stringify(Global_EipVariable) &&
	   JSON.stringify(Eip_Setting)==JSON.stringify(Global_EipSetting) &&

	   JSON.stringify(Register_Index)==JSON.stringify(Global_RegisterIndex) &&
	   JSON.stringify(Register_Module)==JSON.stringify(Global_RegisterModule) &&
	   JSON.stringify(Register_Variable)==JSON.stringify(Global_RegisterVariable) &&
	   JSON.stringify(Register_Setting)==JSON.stringify(Global_RegisterSetting) &&

	   JSON.stringify(MqttJsonGroup)==JSON.stringify(Global_MqttJsonGroup) &&
	   JSON.stringify(AzurePlatform)==JSON.stringify(Global_AzurePlatform) &&

	   JSON.stringify(RemoteMqttGroup)==JSON.stringify(Global_RemoteMqttGroup))
	{
		Return=true;
	}
	return Return;

	/*var Return=false;
	var Class = new LoadFile();
	var RemoteMqttGroup = Class.ReadFile("./File/RemoteMQTTGroup/RemoteMqttGroup.json", new Array());
	if(JSON.stringify(RemoteMqttGroup)==JSON.stringify(Global_RemoteMqttGroup))
	{
		Return=true;
	}
	return Return;*/
}

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

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

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




















//第1頁
MqttToIoTCloudFirstPage.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=MqttToIoTCloudFirstPage.prototype._CheckMqttToIoTCloudQuantity_();
	if(OK == false)	{return 0;}
	MqttToIoTCloudFirstPage.prototype._BuildTcpModuleAllObject_(that);
	MqttToIoTCloudFirstPage.prototype._RedisplayUi_();
}
MqttToIoTCloudFirstPage.prototype._BuildTcpModuleAllObject_ = function(that)
{
	var number = $(that).closest(".Row").find(".Column").eq(2).find(".Select").val();
	MqttToIoTCloudFirstPage.prototype._DeleteRepeatNumber_(number);
	var ModuleName = $(that).closest(".Row").find(".Column").eq(2).find(".Text").val();
	MqttToIoTCloudFirstPage.prototype._InitialRemoteMqttGroupObject_(that);
}
MqttToIoTCloudFirstPage.prototype._DeleteRepeatNumber_ = function(number)
{
	for(var i=0; i<Global_MqttGroupCloudIndex.length; i++)
	{
		if(number == Global_MqttGroupCloudIndex[i])
		{
			Global_MqttGroupCloudIndex.splice(i,1);
		}
	}
}
MqttToIoTCloudFirstPage.prototype._BuildTcpModuleObject_ = function(ModuleName, number)
{
	var DeviceName = TcpModuleFirstPage.prototype._GetDeviceName_();
	if(typeof(Global_TcpModule[DeviceName][number]) == "undefined"){
		Global_TcpModule[DeviceName][number] = new Object();
	}
	Global_TcpModule[DeviceName][number]["ModuleName"] = ModuleName;
}


MqttToIoTCloudFirstPage.prototype._CopyModule_ = function()
{
	MqttToIoTCloudFirstPage.prototype._CopyTickedModule_();
	MqttToIoTCloudFirstPage.prototype._RedisplayUi_();
	MqttToIoTCloudFirstPage.prototype._CopyDialogDisabled_();
}
MqttToIoTCloudFirstPage.prototype._CopyTickedModule_ = function()
{
	var Id = "#ModuleTable";
	var element = $(Id).find(".DynamicRow").find(".Column").eq(0).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());
		MqttToIoTCloudFirstPage.prototype._CopyGlobalVariable_(number);
	}
}
MqttToIoTCloudFirstPage.prototype._CopyDialogDisabled_ = function()
{
	$(".Dialog").eq(0).find(".Message").hide();
	var Class = new Dialog();
	Class.CommonEnabled();
	Class = null;

	$("#Main_Center-Content").find(".Body").eq(0).css('pointer-events', "visible");
	$("#Main_Center-Content").find(".Body").eq(0).css('opacity', 1);

	$(".Dialog").eq(0).css('display', "none");
	$(".Dialog").eq(0).find(".DialogTable").find(".Row").find(".Column").eq(1).find(".Text").val("");
}

MqttToIoTCloudFirstPage.prototype._CopyGlobalVariable_ = function(number)
{
	var SlaveId;
	var ObjectKey = MqttToIoTCloudFirstPage.prototype._GetAllNumber_();
	var Index = ObjectKey.length+Global_MqttGroupCloudIndex.length+1;
	var CopyNumber =  parseInt($(".Dialog").eq(0).find(".DialogTable").find(".Row").eq(0).find(".Text").val());
	for(j=0; j<CopyNumber; j++)
	{
		if(SlaveId > 254)	{break;}
		var OK=MqttToIoTCloudFirstPage.prototype._CheckMqttToIoTCloudQuantity_();
		if(OK==false)		{break;}
		
		if(typeof(Global_RemoteMqttGroup[number]) != "undefined"){
			Global_RemoteMqttGroup[Index] = jQuery.parseJSON(JSON.stringify(Global_RemoteMqttGroup[number]));
		}
		Index++;
	}
}
MqttToIoTCloudFirstPage.prototype._CheckMqttToIoTCloudQuantity_ = function()
{
	var Return=true;
	var AllNumber=MqttToIoTCloudFirstPage.prototype._GetAllNumber_();
	if(AllNumber.length >= 100)
	{
		Return=false;
	}
	return Return;
}
MqttToIoTCloudFirstPage.prototype._DeleteModule_ = function()
{
	MqttToIoTCloudFirstPage.prototype._RemoveTickedModule_();
	MqttToIoTCloudFirstPage.prototype._RedisplayUi_();
}
MqttToIoTCloudFirstPage.prototype._RemoveTickedModule_ = function()
{
	var Id = "#ModuleTable";
	var DynamicRow = $(Id).find(".DynamicRow");
	var Checked = false;
	var number = 0;

	for(var i=0; i<DynamicRow.length; i++)
	{
		Checked = DynamicRow.eq(i).find(".Checkbox").eq(0).prop("checked");
		if( Checked == true )
		{
			number = DynamicRow.eq(i).find(".Column").eq(1).text();
			MqttToIoTCloudFirstPage.prototype._RemoveGlobalVariable_(number);
		}
	}
}
MqttToIoTCloudFirstPage.prototype._RemoveGlobalVariable_ = function(number)
{
	Global_RemoteMqttGroup[number] = null;	delete Global_RemoteMqttGroup[number];
	Global_MqttGroupCloudIndex.push(number);
	Global_MqttGroupCloudIndex = Global_MqttGroupCloudIndex.sort(function (a, b) {return a - b});
	/*Global_TcpVariable[number] = null;				delete Global_TcpVariable[number];
	Global_TcpScalingVariable[number] = null;       delete Global_TcpScalingVariable[number];
	Global_TcpBitwiseVariable[number] = null;       delete Global_TcpBitwiseVariable[number];
	Global_TcpSetting[number] = null;				delete Global_TcpSetting[number];
	Global_TcpIndex.push(number);
	Global_TcpIndex = Global_TcpIndex.sort(function (a, b) {return a - b});
	Global_TcpToOpcuaVariable[number] = null;		delete Global_TcpToOpcuaVariable[number];
	Global_TcpScalingToOpcuaVariable[number] = null;    delete Global_TcpScalingToOpcuaVariable[number];
	Global_TcpBitwiseToOpcuaVariable[number] = null;    delete Global_TcpBitwiseToOpcuaVariable[number];
	Global_TcpToMqttSetting[number] = null;			    delete Global_TcpToMqttSetting[number];
	Global_TcpToMqttVariable[number] = null;		    delete Global_TcpToMqttVariable[number];
	Global_TcpToMqttJsonVariable[number] = null;	    delete Global_TcpToMqttJsonVariable[number];
	Global_TcpToMsSqlVariable[number] = null;	delete Global_TcpToMsSqlVariable[number];
    Global_TcpToMysqlConnectionVariable[number] = null;	delete Global_TcpToMysqlConnectionVariable[number];
	Global_TcpToLocalDataLoggerVariable[number] = null;	delete Global_TcpToLocalDataLoggerVariable[number];
	var Class=new ClearService();
	Class.DeleteModuleClearService("MTCP", number);
	Class.DeleteModuleClearService("MTCP Scaling", number);*/
	Class=null;
}


















MqttToIoTCloudFirstPage.prototype._SetConnectionName_ = function(that)
{
	var number = $(that).closest(".DynamicRow").find(".Column").eq(1).text();
	if(typeof(Global_RemoteMqttGroup) != "object")	{return 0;}
	
	var Button = $(that).closest(".Column").find(".Button");
	var Combobox =  $(that).closest(".Column").find(".Select");
	var SystemVariable = Object.keys(Global_RemoteMqttGroup);
	var object=new Object();
	for(var i=0; i<SystemVariable.length; i++)
	{
		Global_RemoteMqttGroup[number]["ConnectionGroup"] = Combobox.val();
	}
	Button.attr('disabled', true);
}

MqttToIoTCloudFirstPage.prototype._ChangeConnectionName_ = function(that)
{
	var number = $(that).closest(".DynamicRow").find(".Column").eq(0).text();
	if(typeof(Global_RemoteMqttGroup) != "object")	{return 0;}
	
	var Button = $(that).closest(".Column").find(".Button");
	var Combobox =  $(that).closest(".Column").find(".Select");
	var SystemVariable = Object.keys(Global_RemoteMqttGroup);
	var object=new Object();
	for(var i=0; i<SystemVariable.length; i++)
	{
		var ConnectionGroup=Global_RemoteMqttGroup["ConnectionGroup"]
		object[ConnectionGroup]=new Object();
	}
	var array=Object.keys(object);
	if(array.length == 1 && array[0] == Combobox.val()){
		Button.attr('disabled', true);
	}
	else{
		Button.attr('disabled', false);
	}
}

MqttToIoTCloudFirstPage.prototype._LoadConnectionName_ = function(index, number)
{
	var Button = $("#ModuleTable").find(".DynamicRow").eq(index).find(".Column").eq(4).find(".Button");
	var Combobox = $("#ModuleTable").find(".DynamicRow").eq(index).find(".Column").eq(4).find(".Select");
	var ConnectionName = MqttToIoTCloudCommon.prototype._GetConnectionName_();
	for(var i=0; i<ConnectionName.length; i++){
		Combobox.append($("<option></option>").attr("value",ConnectionName[i]).text(ConnectionName[i]));
	}
	Combobox.append($("<option></option>").attr("disabled",true).text("(Groups)"));
	Combobox.val("");
	if(typeof(Global_RemoteMqttGroup[number]) !="object"){
		return 0;
	}
	MqttToIoTCloudFirstPage.prototype._ApplyButton_(index, number, Combobox);
}

MqttToIoTCloudFirstPage.prototype._ApplyButton_ = function(index, number, Combobox)
{
	var Button = $("#ModuleTable").find(".DynamicRow").eq(index).find(".Column").eq(4).find(".Button");
	var SystemVariable=Object.keys(Global_RemoteMqttGroup);
	var object=new Object();
	for(var i=0; i<SystemVariable.length; i++){
		var ConnectionGroup=Global_RemoteMqttGroup[number]["ConnectionGroup"];
		object[ConnectionGroup]=new Object();
	}
	var array=Object.keys(object);
	if(array.length == 1 && array[0] != ""){
		Combobox.val(array[0]);
		Button.attr('disabled', true);
	}
	else if(array.length == 1 && array[0] == ""){
		Combobox.val(array[0]);
		Button.attr('disabled', false);
	}
	else if(array.length >1){
		Combobox.val("(Groups)");
		Button.attr('disabled', true);
	}
}
MqttToIoTCloudFirstPage.prototype._ConnectChangeCheckbox_ = function(that)
{
	var Bool = $(that).closest(".DynamicRow").find(":checkbox").eq(1).prop("checked");
	var number = $(that).closest(".DynamicRow").find(".Column").eq(1).text();
	if(typeof(Global_RemoteMqttGroup[number])!="object"){
		return;
	}
	Global_RemoteMqttGroup[number]["TransformMqttCustom"] = Bool;
}
MqttToIoTCloudFirstPage.prototype._ChangeCheckbox_ = function(that)
{
	var Bool = $(that).closest(".DynamicRow").find(".Column").eq(5).find(":checkbox").prop("checked");
	var number = $(that).closest(".DynamicRow").find(".Column").eq(1).text();
	if(typeof(Global_RemoteMqttGroup[number])!="object"){
		return;
	}
	Global_RemoteMqttGroup[number]["TransformMqttCustom"] = Bool;
}
MqttToIoTCloudFirstPage.prototype._IsTickedModule_ = function()
{
	var Return=false;
	var Id = "#ModuleTable";
	if($(Id).find(".DynamicRow").find(":checked").length >0)
	{
		Return=true;
	}
	return Return;
}
MqttToIoTCloudFirstPage.prototype._CopyDialogEnabled_ = function(Event)
{
	var DeviceName = MqttToIoTCloudFirstPage.prototype._GetDeviceName_();
	$(".Dialog").eq(0).find(".Row").find(".Select").val(DeviceName);
	$(".Dialog").eq(0).find(".Row").find(".Text").val("1");
	
    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(0).css('pointer-events', "none");
	$("#Main_Center-Content").find(".Body").eq(0).css('opacity', 0.5);

	$(".Dialog").eq(0).css('display', "block");
}
MqttToIoTCloudFirstPage.prototype._GetDeviceName_ = function()
{
	var Return;
	var Element = $("#Main_Center-Content").find(".Body").eq(0).find(".Form").eq(0).find(".Row").eq(0).find(".Select");
	Return = Element.val();
	return Return;
}


MqttToIoTCloudFirstPage.prototype._ChangeAllCheckbox_ = function(that)
{
	var ModuleTableId = $(that).closest(".Table").attr("id");
	var Bool = $(that).closest(".Row").find(":checkbox").eq(0).prop("checked");
	var Element = $("#"+ModuleTableId).find(".DynamicRow");
	for(var i=0; i<Element.length; i++)
	{
		if(Element.eq(i).find(":checkbox").eq(0).prop("checked") != Bool)
		{
			Element.eq(i).find(":checkbox").eq(0).prop("checked",Bool);
			var that = Element.eq(i).find(":checkbox").eq(0);
		}
	}
}
MqttToIoTCloudFirstPage.prototype._ConnectChangeAllCheckbox_ = function(that)
{
	var ModuleTableId = $(that).closest(".Table").attr("id");
	var Bool = $(that).closest(".Row").find(":checkbox").eq(1).prop("checked");
	var Element = $("#"+ModuleTableId).find(".DynamicRow");
	for(var i=0; i<Element.length; i++)
	{
		if(Element.eq(i).find(":checkbox").eq(1).prop("checked") != Bool)
		{
			Element.eq(i).find(":checkbox").eq(1).prop("checked",Bool);
			var that = Element.eq(i).find(":checkbox").eq(1);
			MqttToIoTCloudFirstPage.prototype._ConnectChangeCheckbox_(that);
		}
	}
}
MqttToIoTCloudFirstPage.prototype._IsAllTransformMqttJson_ = function(number)
{
	var Return=false;
	if(typeof(Global_RemoteMqttGroup[number]) != "object")
	{
		return Return;
	}
	var SystemVariable=Object.keys(Global_RemoteMqttGroup[number]);
	if(SystemVariable.length==0)
	{
		return Return;
	}
	for(var i=0; i<SystemVariable.length; i++)
	{
		if(Global_RemoteMqttGroup[number]["TransformMqttCustom"]==false)
		{
			return Return;
		}
	}
	Return=true;
	return Return;
}

MqttToIoTCloudFirstPage.prototype._GetAllModuleData_ = function()
{
	var Return = new Object();

	var nmuber = Object.keys(Global_RemoteMqttGroup);
	for(var i=0; i<nmuber.length; i++)
	{
		Return[i + 1] = new Object();
		Return[i + 1] = jQuery.parseJSON(JSON.stringify(Global_RemoteMqttGroup[nmuber[i]]["Name"]));
	}
	return Return;
}

MqttToIoTCloudFirstPage.prototype._GetAllDeviceName_ = function()
{
	var Return=Object.keys(Global_RtuModule);
	return Return;
}

MqttToIoTCloudFirstPage.prototype._GetAllNumber_ = function()
{
	var ObjectKey = new Array();
	if(typeof(Global_RemoteMqttGroup) == "object")
	{
		ObjectKey = ObjectKey.concat(Object.keys(Global_RemoteMqttGroup));
	}
	ObjectKey = ObjectKey.sort(function (a, b) {return a - b});
	return ObjectKey;
}

MqttToIoTCloudFirstPage.prototype._ShowTable_ = function(ObjectKey, DisplayNumber)
{
	var AllModuleData = MqttToIoTCloudFirstPage.prototype._GetAllModuleData_();
	$("#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(AllModuleData[i + 1]);
		MqttToIoTCloudFirstPage.prototype._LoadConnectionName_(index, ObjectKey[i]);
		var Bool = MqttToIoTCloudFirstPage.prototype._IsAllTransformMqttJson_(ObjectKey[i]);
		$("#ModuleTable").find(".DynamicRow").eq(index).find(".Column").eq(5).find(":checkbox").prop("checked",Bool);
		index++;
	}
}

MqttToIoTCloudFirstPage.prototype._UpdateTableToolbar_ = function(ObjectKey, DisplayNumber)
{	
	var NumberOfPages = Math.ceil(ObjectKey.length/DisplayNumber);
	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);
	}
}

MqttToIoTCloudFirstPage.prototype._RedisplayUi_ = function()
{
	var DisplayNumber = 20;//RTU 20 TCP 40 ASCII 20
	var ObjectKey = MqttToIoTCloudFirstPage.prototype._GetAllNumber_();
	MqttToIoTCloudFirstPage.prototype._UpdateNumber_(ObjectKey);
	MqttToIoTCloudFirstPage.prototype._UpdateTableToolbar_(ObjectKey, DisplayNumber);
	MqttToIoTCloudFirstPage.prototype._ShowTable_(ObjectKey, DisplayNumber);
	var Id = "#ModuleTable";
	$(Id).find(".Row").eq(0).find(":checkbox").prop("checked",false);
}

MqttToIoTCloudFirstPage.prototype._UpdateNumber_ = function(ObjectKey)
{
	var AllNumber=MqttToIoTCloudFirstPage.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_MqttGroupCloudIndex.length; i++)
	{
		if(parseInt(Global_MqttGroupCloudIndex[i])>parseInt(MaxNumber)){
			MqttToIoTCloudFirstPage.prototype._DeleteRepeatNumber_(Global_MqttGroupCloudIndex[i]);
			i--;
		}
	}
	var DynamicRowLength = ObjectKey.length;
	var TemporaryIndexLength = Global_MqttGroupCloudIndex.length;
	var Id = "#Number";
	$(Id).empty();
	for(var i=0; i<TemporaryIndexLength; i++)
	{
		$(Id).append($("<option></option>").attr("value", Global_MqttGroupCloudIndex[i]).text(Global_MqttGroupCloudIndex[i]));
	}
	$(Id).append($("<option></option>").attr("value", DynamicRowLength+TemporaryIndexLength+1).text(DynamicRowLength+TemporaryIndexLength+1));
}


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

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

MqttToIoTCloudFirstPage.prototype._Initialization_ = function()
{
	MqttToIoTCloudFirstPage.prototype._RedisplayUi_();
}

MqttToIoTCloudFirstPage.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();
		}
	});
}

MqttToIoTCloudFirstPage.prototype._SaveFinished_ = function(that, ssocket)
{
	$(that).closest('.Row').find(".Loading").hide();
	$(that).closest('.Row').find(".Message").eq(1).show();
	ssocket.emit("SaveFinished");
	ssocket.on("SaveFinished", function (Return) {
		if(Return == true)
		{
			$(that).closest('.Row').find(".Message").hide();
			$(that).closest('.Row').find(".Message").eq(0).show();
		}
		else
		{
			$(that).closest('.Row').find(".Message").hide();
			$(that).closest('.Row').find(".Message").eq(2).show();
		}
		ssocket.emit("SocketClose");
	});
}

MqttToIoTCloudFirstPage.prototype._SaveStart_ = function(that, ssocket)
{
	ssocket.emit("BackgroundExecution");
	ssocket.on("BackgroundExecution", function (Return) {
	if(Return == true)
	{
		var Callback0 = function(){MqttToIoTCloudFirstPage.prototype._SaveFinished_(that, ssocket);}
		var JsonString = JSON.stringify(Global_MqttToIoTCloudVariable);
		$(that).closest('.Row').find(".Message").hide();
		$(that).closest('.Row').find(".Loading").show();
		MqttToIoTCloudFirstPage.prototype._TransmissionJson_(ssocket, "SaveMqttToIoTCloudVariable", JsonString, Callback0);
	}
	});
}

MqttToIoTCloudFirstPage.prototype._EditButton_ = function(that)
{
	$(that).closest(".Column").find(".Message").show();
	var ssocket = io.connect();
	ssocket.emit("BackgroundExecution");
	ssocket.on("BackgroundExecution", function (Return) {
	if(Return == true)
	{
		$("#Main_Center-Content").find(".Body").eq(2).find(".Message").hide();
		var number = parseInt($(that).closest(".DynamicRow").find(".Column").eq(1).text());
		$("#Main_Center-Content").find(".Body").eq(1).hide();
		MqttToIoTCloudSecondPage.prototype._HideColumn_();
		$("#Main_Center-Content").find(".Body").eq(2).show();
		var Name = $(that).closest(".DynamicRow").find(".Column").eq(2).text();
		
		var Class = new Path();
		Class.LoadPathHtml(Name, 2);
		Class = null;
		MqttToIoTCloudSecondPage.prototype._SetInitialValue_(number);
		$(that).closest(".Column").find(".Message").hide();
	}
	});
}

MqttToIoTCloudFirstPage.prototype._SaveButton_ = function(that)
{
	var Result={"Input":null};
	$(that).closest('.Row').find(".Loading").show();
	$(that).closest('.Row').find(".Message").hide();
	MqttToIoTCloudFirstPage.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);
}
MqttToIoTCloudFirstPage.prototype._SaveDataLayerAndServiceLayerVariables_ = function(Result)
{
	var RReturn = $.Deferred();
	var Result_1 = {"Input":null};
	MqttToIoTCloudFirstPage.prototype._SaveModuleSetting_(Result_1);
	//RemoteMqttGroupFirstPage.prototype._SaveConvertSetting_(Result_2);
	//RemoteMqttGroupFirstPage.prototype._SaveLoggerSetting_(Result_3);
	//RemoteMqttGroupFirstPage.prototype._SaveAdvancedSetting_(Result_4);
	var SetInterval = setInterval(function()
	{
		if(Result_1["Input"] == true)
		{
			Result["Input"] = true;
			clearInterval(SetInterval);
			RReturn.resolve(true);
		}
	},500);
	return RReturn.promise();
}
MqttToIoTCloudFirstPage.prototype._SaveModuleSetting_ = function(Result)
{
	var Result_1 = {"Input":null}, Result_2 = {"Input":null};
	var Class = new Transmission();
	Class.TransmissionString("SaveRemoteMqttGroup", JSON.stringify(Global_RemoteMqttGroup), Result_1);
	Class.TransmissionString("SaveMqttGroupCloudIndex", JSON.stringify(Global_MqttGroupCloudIndex), Result_2);
	//Class.TransmissionString("SaveTcpModule", JSON.stringify(Global_TcpModule), Result_2);
	//Class.TransmissionString("SaveTcpVariable", JSON.stringify(Global_TcpVariable), Result_3);
	//Class.TransmissionString("SaveTcpSetting", JSON.stringify(Global_TcpSetting), Result_4);
	//Class.TransmissionString("SaveTcpScalingVariable", JSON.stringify(Global_TcpScalingVariable), Result_5);
	//Class.TransmissionString("SaveTcpBitwiseVariable", JSON.stringify(Global_TcpBitwiseVariable), Result_6);
	var SetInterval = setInterval(function()
	{
		if(Result_1["Input"] == true && Result_2["Input"] == true)
		{
			Result["Input"] = true;
			Class = null; delete Class;
			clearInterval(SetInterval);
		}
	},500);
}


















//第2頁
MqttToIoTCloudSecondPage.prototype._LoadConnectionName_ = function(Index, ConnectionGroup)
{
	var Combobox = $("#VariableTable").find(".DynamicRow").eq(Index).find(".Column").eq(5).find(".Select");
	var ConnectionName = MqttToIoTCloudCommon.prototype._GetConnectionName_();
	for(var i=0; i<ConnectionName.length; i++)
	{
		Combobox.append($("<option></option>").attr("value",ConnectionName[i]).text(ConnectionName[i]));
	}
	Combobox.val(ConnectionGroup);
}

MqttToIoTCloudSecondPage.prototype._VariableTableAddNewRow_ = function(number)
{
	if(typeof(Global_MqttToIoTCloudVariable[number]) != "object")	{return 0;}
	var html="", TempHtml = $("#Main_Center-Content").find(".Body").eq(2).find(".Dynamic").html();
	var SystemVariable = Object.keys(Global_MqttToIoTCloudVariable[number]);
	for(var i=0; i<SystemVariable.length; i++)
	{
		html = html + TempHtml;
	}
	$("#VariableTable").append(html);
}

MqttToIoTCloudSecondPage.prototype._SetVariableTableNewRowValue_ = function(number)
{
	if(typeof(Global_MqttToIoTCloudVariable[number]) != "object")	{return 0;}
	var Variable = Global_RtuVariable[number];
	var Class=new Algorithm();
	var SystemVariable = Object.keys(Global_MqttToIoTCloudVariable[number]).sort(Class.StringNumberMixedSort());
	var Row = $("#VariableTable").find(".DynamicRow");
	for(var i=0; i<SystemVariable.length; i++)
	{
		Row.eq(i).find(".Column").eq(0).text(SystemVariable[i]);
		Row.eq(i).find(".Column").eq(1).find(".Text").val(Variable[SystemVariable[i]]["VariableName"]);
		Row.eq(i).find(".Column").eq(2).find(".Text").val(Global_MqttToIoTCloudVariable[number][SystemVariable[i]]["Alias"]);
		var ReadWriteType = MqttToIoTCloudSecondPage.prototype._GetReadWriteType_(SystemVariable[i].split("-")[0]);
		Row.eq(i).find(".Column").eq(3).find(".Select").val(ReadWriteType);
		Row.eq(i).find(".Column").eq(4).text(SystemVariable[i].split("-")[1]);
		MqttToIoTCloudSecondPage.prototype._LoadConnectionName_(i, Global_MqttToIoTCloudVariable[number][SystemVariable[i]]["ConnectionGroup"]);
		var Bool = Global_MqttToIoTCloudVariable[number][SystemVariable[i]]["TransformMqttJson"];
		Row.eq(i).find(".Column").eq(6).find(".Checkbox").prop("checked",Bool);
	}
}

MqttToIoTCloudSecondPage.prototype._GetModuleName_ = function(number)
{
	var Return="";
	var DeviceName = Object.keys(Global_RtuModule);
	for(var i=0; i<DeviceName.length; i++)
	{
		if(typeof(Global_RtuModule[DeviceName[i]][number]) == "object")
		{
			Return = Global_RtuModule[DeviceName[i]][number]["ModuleName"];
			break;
		}
	}
	return Return;
}

MqttToIoTCloudSecondPage.prototype._ClearTable_ = function(number)
{
	$("#VariableTable").find(".Row").eq(0).find(".Checkbox").prop("checked",false);
	$("#VariableTable").find(".DynamicRow").remove();
}

MqttToIoTCloudSecondPage.prototype._SetVariableTableInitialValue_ = function(number)
{
	MqttToIoTCloudSecondPage.prototype._VariableTableAddNewRow_(number);
	MqttToIoTCloudSecondPage.prototype._SetVariableTableNewRowValue_(number);
}

MqttToIoTCloudSecondPage.prototype._SetFormInitialValue_ = function(number)
{
	var RemoteMqttGroupClientSettings = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0).find(".Row");
	RemoteMqttGroupClientSettings.eq(0).find(".Text").val(number);
	RemoteMqttGroupClientSettings.eq(1).find(".Text").val(Global_RemoteMqttGroup[number]["Name"]);

	var Class=new LoadFile();
	var ModuleType = Class.ReadFile("./File/RemoteMQTTGroup/ModuleType.json");
	var Value = ModuleType["Value"];
	var Text = ModuleType["Text"];
	var DataSettingForm = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(1);
	var DataSettingSelect = DataSettingForm.find(".Row").eq(0).find("table").find("tr").eq(0).find(".Select");
	DataSettingSelect.empty();
	DataSettingForm.find(".Row").eq(0).find("table").find("tr").find(".Select").empty();;
	for(var i=0; i<Value.length; i++)
	{
		DataSettingSelect.append($("<option></option>").attr("value",Value[i]).text(Text[i]));
	}
	MqttToIoTCloudSecondPage.prototype._SetInitDataSetting_(number);
	//ModuleType = DataSettingSelect.val();
	//RemoteMqttGroupSecondPage.prototype._ChangeInputModuleType_(ModuleType, that);
}
MqttToIoTCloudSecondPage.prototype._ChangeInputModuleType_ = function(ModuleType, that)
{
	
	MqttToIoTCloudSecondPage.prototype._LoadModuleNumber_(ModuleType, that);
	MqttToIoTCloudSecondPage.prototype._LoadModuleName_(ModuleType, that);
	
	MqttToIoTCloudSecondPage.prototype._LoadInputVariableAttribute_(ModuleType);
	var ModuleNumber=$(that).closest(".Form").find(".Row").eq(0).find("table").find("tr").eq(1).find("Select").val();
	MqttToIoTCloudSecondPage.prototype._ChangeVariableAttribute_(ModuleType, that);
	MqttToIoTCloudSecondPage.prototype._HideErrorMessage_();
	MqttToIoTCloudSecondPage.prototype._ShowInputErrorMessage_();
	/*PidOperationSecondPage.prototype._LoadVariableType_(ModuleType, that);
	
	var ModuleNumber=$(that).closest(".Form").find(".Row").eq(0).find("table").find("tr").eq(1).find("Select").val();
	var VariableIndex=PidOperationSecondPage.prototype._GetVariableIndexPrefixWord_(that);
	PidOperationSecondPage.prototype._LoadVariableName_(ModuleType, ModuleNumber, VariableIndex, that);
	
	PidOperationSecondPage.prototype._HideErrorMessage_();
	PidOperationSecondPage.prototype._ShowInputErrorMessage_();
	PidOperationSecondPage.prototype._ShowOutputErrorMessage_();*/
}
MqttToIoTCloudSecondPage.prototype._HideErrorMessage_ = function()
{
	var Body=$("#Main_Center-Content").find(".Body").eq(2);
	var Form=Body.find(".Form");
	Form.eq(1).find(".Message").hide();
	Form.eq(2).find(".Message").hide();
}
MqttToIoTCloudSecondPage.prototype._LoadModuleNumber_ = function(ModuleType, that)
{
	var ModuleVariable=MqttToIoTCloudSecondPage.prototype._GetModuleVariable_(ModuleType);
	var Class=new GetValue();
	var ModuleNumber=Class.GetModuleAllNumber(ModuleVariable);

	var Tr=$(that).closest(".Form").find(".Row").eq(0).find("table").find("tr");
	var Select = Tr.eq(1).find(".Select");
	Select.empty();
	if(ModuleNumber.length == 0)
	{
		var InputForm = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(1);
		{
			InputForm.find(".Row").eq(0).find("tr").eq(1).find(".Message").val();
		}
		return 0;
	}
	for(var i=0; i<ModuleNumber.length; i++)
	{
		Select.append($("<option></option>").attr("value",ModuleNumber[i]).text(ModuleNumber[i]));
	}
	Class=null;
}
MqttToIoTCloudSecondPage.prototype._GetModuleVariable_ = function(Module)
{
	var Internel = new Object();
	Internel["LAN"] = Global_RegisterModule;
	var Return;
	if(Module=="MRTU" || Module=="MRTU Scaling")			{Return=Global_RtuModule;}
	else if(Module=="MTCP"|| Module=="MTCP Scaling")		{Return=Global_TcpModule;}
	else if(Module=="MASCII"|| Module=="MASCII Scaling")	{Return=Global_AsciiModule;}
	else if(Module=="EIP")	{Return=Global_EipModule;}
	else if(Module=="MQTT")	{Return=Global_MqttModule;}
	else if(Module=="Internel")	{Return=Internel;}
	return Return;
}

MqttToIoTCloudSecondPage.prototype._ShowInputErrorMessage_ = function()
{
	var Form=$("#Main_Center-Content").find(".Body").eq(2).find(".Form");
	var ModuleType=Form.eq(1).find(".Row").eq(0).find("tr").eq(0).find(".Select").val();
	var ModuleNumber=Form.eq(1).find(".Row").eq(0).find("tr").eq(1).find(".Select").val();
	var ModuleName=Form.eq(1).find(".Row").eq(0).find("tr").eq(2).find(".Text").val();
	
	var VariableAttribute=Form.eq(1).find(".Row").eq(0).find("tr").eq(3).find(".Select").val();
	var TagName=Form.eq(1).find(".Row").eq(0).find("tr").eq(4).find(".Select").val();

	if(ModuleType=="" || ModuleType==null)               {Form.eq(1).find(".Row").eq(0).find("tr").eq(0).find(".Message").show();}
	if(ModuleNumber=="" || ModuleNumber==null)           {Form.eq(1).find(".Row").eq(0).find("tr").eq(1).find(".Message").show();}
	if(ModuleName=="" || ModuleName==null)               {Form.eq(1).find(".Row").eq(0).find("tr").eq(2).find(".Message").show();}
	
	if(VariableAttribute=="" || VariableAttribute==null) {Form.eq(1).find(".Row").eq(0).find("tr").eq(3).find(".Message").show();}
	if(TagName=="" || TagName==null)           {Form.eq(1).find(".Row").eq(0).find("tr").eq(4).find(".Message").show();}
}
MqttToIoTCloudSecondPage.prototype._InputDataSetting_ = function()
{	
	var InputForm = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(1);
	var ModuleType = InputForm.find(".Row").eq(0).find("tr").eq(0).find(".Select").val();
	var ModuleNumber = InputForm.find(".Row").eq(0).find("tr").eq(1).find(".Select").val();
	var ModuleName = InputForm.find(".Row").eq(0).find("tr").eq(2).find(".Text").val();
	var VariableAttribute = InputForm.find(".Row").eq(0).find("tr").eq(3).find(".Select").val();
	var Variable = InputForm.find(".Row").eq(0).find("tr").eq(4).find(".Select").val();

	if(ModuleType == "" || ModuleType == null )	{return 0;}
	if(ModuleNumber == "" || ModuleNumber == null )	{return 0;}
	if(ModuleName == "" || ModuleName == null )	{return 0;}
	if(VariableAttribute == "" || VariableAttribute == null )	{return 0;}
	if(Variable == "" || Variable == null )	{return 0;}
	if(ModuleType == "MRTU Scaling" || ModuleType == "MTCP Scaling")
	{	
		var Data = "$" + ModuleType.split(" ")[0] + "_No." + ModuleNumber + "_" + ModuleName + "_" + VariableAttribute + "_Scale.Scale_" + Variable + "$";
	}
	else
	{
		var Data = "$" + ModuleType + "_No." + ModuleNumber + "_" + ModuleName + "_" + VariableAttribute + "." + Variable + "$";
	}
	var TextArea = InputForm.find(".Row").eq(1).find(".Textarea");
	var OldData = TextArea.val();
	var NewData = OldData + Data;
	TextArea.val(NewData);
}
MqttToIoTCloudSecondPage.prototype._ChangeVariableType_ = function(ModuleType, that)
{
	MqttToIoTCloudSecondPage.prototype._ChangeVariableAttribute_(ModuleType, that);
	MqttToIoTCloudSecondPage.prototype._HideErrorMessage_();
	MqttToIoTCloudSecondPage.prototype._ShowInputErrorMessage_();
}
MqttToIoTCloudSecondPage.prototype._ChangeVariableAttribute_ = function(ModuleType, that)
{	
	var DataSettingForm = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(1);
	var Select = DataSettingForm.find(".Row").eq(0).find("table").find("tr").eq(4).find(".Select");
	var Variable = MqttToIoTCloudSecondPage.prototype._GetGlobalVariable_(ModuleType);
	var ModuleNumber = $(that).closest(".Form").find(".Row").eq(0).find("table").find("tr").eq(1).find("Select").val();
	var VariableType = $(that).closest(".Form").find(".Row").eq(0).find("table").find("tr").eq(3).find("Select").val();
	Select.empty();
	var key = Object.keys(Variable[ModuleNumber]);
	for(var i=0; i<key.length; i++)
	{
		if(key[i].indexOf(VariableType)>=0)
		{
			Select.append($("<option></option>").attr("value",Variable[ModuleNumber][key[i]]["VariableName"]).text(Variable[ModuleNumber][key[i]]["VariableName"]));
		}
	}
}
MqttToIoTCloudSecondPage.prototype._GetGlobalVariable_ = function(Module)
{	
	var Return;
	if(Module=="MRTU")			{Return=Global_RtuVariable;}
	else if(Module=="MTCP")		{Return=Global_TcpVariable;}
	else if(Module=="MASCII")	{Return=Global_AsciiVariable;}
	else if(Module=="MRTU Scaling")	{Return=Global_RtuScalingVariable;}
	else if(Module=="MTCP Scaling")	{Return=Global_TcpScalingVariable;}
	else if(Module=="EIP")	{Return=Global_EipVariable;}
	else if(Module=="MQTT")	{Return=Global_MqttVariable;}
	else if(Module=="Internel")	{Return=Global_RegisterVariable;}
	return Return;
}
MqttToIoTCloudSecondPage.prototype._LoadModuleName_ = function(ModuleType, that)
{
	var ModuleVariable=MqttToIoTCloudSecondPage.prototype._GetModuleVariable_(ModuleType);
	var Select=$(that).closest("table").find("tr").eq(1).find(".Select");
	var ModuleNumber=Select.val();
	var Class=new GetValue();
	var ModuleName=Class.GetModuleName(ModuleVariable, ModuleNumber);
	
	var Text = $(that).closest("table").find("tr").eq(2).find(".Text");
	Text.val(ModuleName);
	Class=null;
}
MqttToIoTCloudSecondPage.prototype._LoadInputVariableAttribute_ = function(ModuleType)
{
	var Class=new LoadFile();
	var VariableAttribute = Class.ReadFile("./File/RemoteMQTTGroup/VariableAttribute.json");

	var InputForm = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(1);
	if(ModuleType =="")
	{
		ModuleType=InputForm.find(".Row").eq(0).find("tr").eq(0).find(".Select").val();
	}
	var Value=VariableAttribute[ModuleType]["Value"];
	var Text=VariableAttribute[ModuleType]["Text"];
	var DataSettingForm = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(1);
	var InputSelect = DataSettingForm.find(".Row").eq(0).find("table").find("tr").eq(3).find(".Select");
	InputSelect.empty();
	for(var i=0; i<Value.length; i++)
	{
		InputSelect.append($("<option></option>").attr("value",Value[i]).text(Text[i]));
	}
}
MqttToIoTCloudSecondPage.prototype._ChangeVariableAttribute_ = function(ModuleType, that)
{	
	var DataSettingForm = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(1);
	var Select = DataSettingForm.find(".Row").eq(0).find("table").find("tr").eq(4).find(".Select");
	var Variable = MqttToIoTCloudSecondPage.prototype._GetGlobalVariable_(ModuleType);
	var ModuleNumber = $(that).closest(".Form").find(".Row").eq(0).find("table").find("tr").eq(1).find("Select").val();
	var VariableType = $(that).closest(".Form").find(".Row").eq(0).find("table").find("tr").eq(3).find("Select").val();
	Select.empty();
	if (ModuleNumber==null)
	{
		return 0;
	}
	var key = Object.keys(Variable[ModuleNumber]);
	for(var i=0; i<key.length; i++)
	{
		if(key[i].indexOf(VariableType)>=0)
		{
			Select.append($("<option></option>").attr("value",Variable[ModuleNumber][key[i]]["VariableName"]).text(Variable[ModuleNumber][key[i]]["VariableName"]));
		}
	}
}
MqttToIoTCloudSecondPage.prototype._GetGlobalVariable_ = function(Module)
{	
	var Return;
	if(Module=="MRTU")			{Return=Global_RtuVariable;}
	else if(Module=="MTCP")		{Return=Global_TcpVariable;}
	else if(Module=="MASCII")	{Return=Global_AsciiVariable;}
	else if(Module=="MRTU Scaling")	{Return=Global_RtuScalingVariable;}
	else if(Module=="MTCP Scaling")	{Return=Global_TcpScalingVariable;}
	else if(Module=="EIP")	{Return=Global_EipVariable;}
	else if(Module=="MQTT")	{Return=Global_MqttVariable;}
	else if(Module=="Internel")	{Return=Global_RegisterVariable;}
	return Return;
}
MqttToIoTCloudSecondPage.prototype._ChangeModuleNumber_ = function(ModuleType, that)
{
	RemoteMqttGroupSecondPage.prototype._LoadModuleName_(ModuleType, that);
	var ModuleNumber=$(that).closest(".Form").find(".Row").eq(0).find("table").find("tr").eq(1).find("Select").val();
	var VariableIndex=MqttToIoTCloudSecondPage.prototype._GetVariableIndexPrefixWord_(that);
	MqttToIoTCloudSecondPage.prototype._LoadVariableName_(ModuleType, ModuleNumber, VariableIndex, that);
	MqttToIoTCloudSecondPage.prototype._ChangeVariableAttribute_(ModuleType, that);
	MqttToIoTCloudSecondPage.prototype._HideErrorMessage_();
	MqttToIoTCloudSecondPage.prototype._ShowInputErrorMessage_();
}
MqttToIoTCloudSecondPage.prototype._GetVariableIndexPrefixWord_ = function(that)
{
	var Return="";
	var VariableAttribute=$(that).closest(".Form").find(".Row").eq(1).find("table").find("tr").eq(0).find("Select").val();
	var VariableType=$(that).closest(".Form").find(".Row").eq(1).find("table").find("tr").eq(1).find("Select").val();

	var Class=new GetValue();
	var DIO_AIO=Class.GetModuleVariableAttribute(VariableAttribute, VariableType);
	Return=DIO_AIO+"-"+VariableType+"-";
	Class=null;
	return Return;
}
MqttToIoTCloudSecondPage.prototype._LoadVariableName_ = function(ModuleType, ModuleNumber, VariableIndex, that)
{
	if(ModuleType == "MRTU Scaling" || ModuleType == "MTCP Scaling")
	{
		MqttToIoTCloudSecondPage.prototype._ScalingLoadVariableName_(ModuleType, ModuleNumber, VariableIndex, that);
	}
	else
	{
		var ModuleVariable=MqttToIoTCloudSecondPage.prototype._GetGlobalVariable_(ModuleType);
		var Class=new GetValue();
		var Variable=Class.GetModuleVariableNameArray(ModuleVariable, ModuleNumber, VariableIndex);

		var Tr=$(that).closest(".Form").find(".Row").eq(1).find("table").find("tr");
		var Select = Tr.eq(2).find(".Select");
		Select.empty();
		for(var i=0; i<Variable.length; i++)
		{
			Select.append($("<option></option>").attr("value",Variable[i]).text(Variable[i]));
		}
	}
}

MqttToIoTCloudSecondPage.prototype._SetInitDataSetting_ = function(number)
{
	var InputForm = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(1);
	//InputForm.find(".Row").eq(0).find("table").find("tr").eq(0).find(".Select").val("");
	InputForm.find(".Row").eq(0).find("table").find("tr").find(".Select").val("");
	InputForm.find(".Row").eq(0).find("table").find("tr").find(".Text").val("");
	var TextArea = InputForm.find(".Row").eq(1).find(".Textarea");
	TextArea.val(Global_RemoteMqttGroup[number]["DataMessage"]);
}
MqttToIoTCloudSecondPage.prototype._SetInitialValue_ = function(number)
{
	MqttToIoTCloudSecondPage.prototype._ClearTable_(number);
	MqttToIoTCloudSecondPage.prototype._SetFormInitialValue_(number);
	MqttToIoTCloudSecondPage.prototype._SetVariableTableInitialValue_(number);
}

MqttToIoTCloudSecondPage.prototype._ModifyMqttToIoTCloudVariableObject_ = function(number)
{
	var InputForm = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(1);
	var TextArea = InputForm.find(".Row").eq(1).find(".Textarea");
	var Name = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0).find(".Row").eq(1).find(".Text");
	Global_RemoteMqttGroup[number]["DataMessage"] = TextArea.val();
	Global_RemoteMqttGroup[number]["Name"] = Name.val();
	//Global_RemoteMqttGroup[number]["TransformMqttJson"] = Bool;
	/*var Row = $("#ModuleTable").find(".DynamicRow");
	for(var i=0; i<Row.length; i++)
	{
		var SystemVariable = Row.eq(i).find(".Column").eq(0).text();
		var Alias = Row.eq(i).find(".Column").eq(2).find(".Text").val();
		var ConnectionGroup = Row.eq(i).find(".Column").eq(5).find(".Select").val();
		if(ConnectionGroup == null)
		{
			ConnectionGroup="";
		};
		var Bool = Row.eq(i).find(":checkbox").prop("checked");
		Global_MqttToIoTCloudVariable[number][SystemVariable]["Alias"] = Alias;
		Global_MqttToIoTCloudVariable[number][SystemVariable]["ConnectionGroup"] = ConnectionGroup;
		Global_MqttToIoTCloudVariable[number][SystemVariable]["TransformMqttJson"] = Bool;
	}*/
}

MqttToIoTCloudSecondPage.prototype._ConfirmButton_ = function(that)
{
	$(that).closest(".Row").find(".Message").hide();
	var Bool=MqttToIoTCloudSecondPage.prototype._AllDataIsCorrect_();
	if(Bool==false)
	{
		$(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());
	MqttToIoTCloudSecondPage.prototype._ModifyMqttToIoTCloudVariableObject_(number);
	$("#Main_Center-Content").find(".Body").eq(2).hide();
	$("#Main_Center-Content").find(".Body").eq(1).show();
	MqttToIoTCloudFirstPage.prototype._RedisplayUi_();
	var Class = new Path();
	Class.LoadPathHtml($("#Left-List-MqttToIoTCloud").text(), 1, "main_PathMqttToIoTCloud_click(this)");
	Class = null;
}

MqttToIoTCloudSecondPage.prototype._NoConfirmButton_ = function()
{
	$("#Main_Center-Content").find(".Body").eq(2).hide();
	$("#Main_Center-Content").find(".Body").eq(1).show();
	var Class = new Path();
	Class.LoadPathHtml($("#Left-List-MqttToIoTCloud").text(), 1, "main_PathMqttToIoTCloud_click(this)");
	Class = null;
}

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

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

MqttToIoTCloudSecondPage.prototype._GetReadWriteType_ = function(string)
{
	var Return;
	if(string == "DO")			{Return = "ReadWrite";}
	else if(string == "DI")		{Return = "Read";}
	else if(string == "AO")		{Return = "ReadWrite";}
	else if(string == "AI")		{Return = "Read";}
	return Return;
}

MqttToIoTCloudSecondPage.prototype._AllDataIsCorrect_ = function()
{
	var Return = true;
	var length = $("#VariableTable").find(".DynamicRow").length;
	for(var i=0; i<length; i++)
	{
		var ShowMessage = $("#VariableTable").find(".DynamicRow").eq(i).find(".Message").eq(0).is(":visible");
		if(ShowMessage == true)
		{
			Return = false;
		}
	}
	return Return;
}

MqttToIoTCloudSecondPage.prototype._CheckAlias_ = function(that)
{
	$(that).closest(".Column").find(".Message").hide();
	var Name = $(that).closest(".Column").find(".Text").val();
	var Class = new Type();
	if(Name == "")
	{
		$(that).closest(".Column").find(".Message").show();
	}
	else if(Class.CheckSpecialSymbols(Name) == true)
	{
		$(that).closest(".Column").find(".Message").show();
	}
	else if(Class.CheckChineseLanguage(Name) == true)
	{
		$(that).closest(".Column").find(".Message").show();
	}
	Class = null;
}

MqttToIoTCloudSecondPage.prototype._SetInitialValue_ = function(number)
{
	MqttToIoTCloudSecondPage.prototype._SetFormInitialValue_(number);
}