//第1頁
RemoteMqttGroupFirstPage.prototype._InitialRemoteMqttGroupObject_ = function(that)
{
	var number = Object.keys(Global_RemoteMqttGroup);
	if(number.length == 0)
	{
		number=1;
	}
	else if(number.length>0)
	{
		number = Math.max.apply(null,number)+1;
	}
	if(typeof(Global_RemoteMqttGroup[number]) == "undefined")
	{
		Global_RemoteMqttGroup[number] = new Object();
		Global_RemoteMqttGroup[number]["Name"] = $(that).closest(".Row").find(".Column").eq(2).find(".Text").val();			
		Global_RemoteMqttGroup[number]["DataMessage"] = "";
	}
}
//第2頁
RemoteMqttGroupSecondPage.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]));
	}
	RemoteMqttGroupSecondPage.prototype._SetInitDataSetting_(number);
	//ModuleType = DataSettingSelect.val();
	//RemoteMqttGroupSecondPage.prototype._ChangeInputModuleType_(ModuleType, that);
}
RemoteMqttGroupSecondPage.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(0).find(".Textarea");
	TextArea.val(Global_RemoteMqttGroup[number]["DataMessage"]);
}


RemoteMqttGroupSecondPage.prototype._ChangeInputModuleType_ = function(ModuleType, that)
{
	RemoteMqttGroupSecondPage.prototype._LoadModuleNumber_(ModuleType, that);
	RemoteMqttGroupSecondPage.prototype._LoadModuleName_(ModuleType, that);
	
	RemoteMqttGroupSecondPage.prototype._LoadInputVariableAttribute_(ModuleType);
	var ModuleNumber=$(that).closest(".Form").find(".Row").eq(0).find("table").find("tr").eq(1).find("Select").val();
	RemoteMqttGroupSecondPage.prototype._ChangeVariableAttribute_(ModuleType, that);
	/*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_();*/
}
RemoteMqttGroupSecondPage.prototype._LoadModuleNumber_ = function(ModuleType, that)
{
	var ModuleVariable=RemoteMqttGroupSecondPage.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)
	{
		return 0;
	}
	for(var i=0; i<ModuleNumber.length; i++)
	{
		Select.append($("<option></option>").attr("value",ModuleNumber[i]).text(ModuleNumber[i]));
	}
	Class=null;
}
RemoteMqttGroupSecondPage.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;
}
RemoteMqttGroupSecondPage.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=PidOperationSecondPage.prototype._GetVariableIndexPrefixWord_(that);
	RemoteMqttGroupSecondPage.prototype._LoadVariableName_(ModuleType, ModuleNumber, VariableIndex, that);
	
	RemoteMqttGroupSecondPage.prototype._HideErrorMessage_();
	RemoteMqttGroupSecondPage.prototype._ShowInputErrorMessage_();
	RemoteMqttGroupSecondPage.prototype._ShowOutputErrorMessage_();
}

RemoteMqttGroupSecondPage.prototype._LoadModuleName_ = function(ModuleType, that)
{
	var ModuleVariable=RemoteMqttGroupSecondPage.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;
}
RemoteMqttGroupSecondPage.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]));
	}
}
RemoteMqttGroupSecondPage.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;
}
RemoteMqttGroupSecondPage.prototype._ChangeVariableType_ = function(ModuleType, that)
{
	RemoteMqttGroupSecondPage.prototype._ChangeVariableAttribute_(ModuleType, that);
}
RemoteMqttGroupSecondPage.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 = RemoteMqttGroupSecondPage.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"]));
		}
	}
}
RemoteMqttGroupSecondPage.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();
	
	var Data = ModuleType + "_No." + ModuleNumber + "_" + ModuleName + "_" + VariableAttribute + "." + Variable;
	var TextArea = InputForm.find(".Row").eq(0).find(".Textarea");
	var OldData = TextArea.val();
	var NewData = OldData + Data;
	TextArea.val(NewData);
}
RemoteMqttGroupSecondPage.prototype._ConfirmButton_ = function(that)
{
	$(that).closest(".Form").find(".Message").hide();
	var Element = $("#Main_Center-Content").find(".Body").eq(2).find(".Message");
	for(var i=0; i<Element.length; i++)
	{
		if(Element.eq(i).is(":visible") == true){
			$(that).closest(".Row").find(".Message").show();
			return 0;
		}
	}
	RemoteMqttGroupSecondPage.prototype._SaveGlobalVariable_();
	$("#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();
	RemoteMqttGroupFirstPage.prototype._RedisplayUi_();
	/*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 OldGroupName = Global_RemoteMqttGroup[number]["Name"];
	var NewGroupName = ContentSettings.eq(1).find(".Text").val();
	RemoteMqttGroupSecondPage.prototype._ModifyGlobalVariable_(OldGroupName, NewGroupName);
	RemoteMqttGroupSecondPage.prototype._ModifyRemoteMqttGroupObject_(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();
	RemoteMqttGroupFirstPage.prototype._RedisplayUi_();
	var Class = new Path();
	Class.LoadPathHtml($("#Left-List-RemoteMqttGroup").text(), 1, "main_PathRemoteMqttGroup_click(this)");
	Class = null;*/
}
RemoteMqttGroupSecondPage.prototype._SaveGlobalVariable_ = function()
{
	var RemoteMqttGroupClientSettings = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0).find(".Row");
	var number = RemoteMqttGroupClientSettings.eq(0).find(".Text").val();
	var ModuleName = RemoteMqttGroupClientSettings.eq(1).find(".Text").val();
	var InputForm = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(1);
	var TextArea = InputForm.find(".Row").eq(0).find(".Textarea");
	var TextAreaData = TextArea.val();
	Global_RemoteMqttGroup[number]["Name"] = ModuleName;
	Global_RemoteMqttGroup[number]["DataMessage"] = TextAreaData;
	
}

























RemoteMqttGroupUpdatePage.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");
				RemoteMqttGroupUpdatePage.prototype._ShowFirstPage_();
				Class.UnLockAllUi();
			}
		}
		else if(Index<Length)
		{
			ssocket.emit("BackgroundExecution");
		}
	});
}

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

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

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

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




















//第1頁
RemoteMqttGroupFirstPage.prototype._Initialization_ = function()
{
	RemoteMqttGroupFirstPage.prototype._RedisplayUi_();
}

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

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

RemoteMqttGroupFirstPage.prototype._SaveStart_ = function(that, ssocket)
{
	ssocket.emit("BackgroundExecution");
	ssocket.on("BackgroundExecution", function (Return) {
	if(Return == true)
	{
		var Callback0 = function(){RemoteMqttGroupFirstPage.prototype._SaveFinished_(that, ssocket);}
		
		var JsonString1 = JSON.stringify(Global_RtuToMqttJsonVariable);
		var Callback1 = function(){RemoteMqttGroupFirstPage.prototype._TransmissionJson_(ssocket, "SaveRtuToMqttJsonVariable", JsonString1, Callback0);}
		
		var JsonString2 = JSON.stringify(Global_TcpToMqttJsonVariable);
		var Callback2 = function(){RemoteMqttGroupFirstPage.prototype._TransmissionJson_(ssocket, "SaveTcpToMqttJsonVariable", JsonString2, Callback1);}
		
		var JsonString3 = JSON.stringify(Global_AsciiToMqttJsonVariable);
		var Callback3 = function(){RemoteMqttGroupFirstPage.prototype._TransmissionJson_(ssocket, "SaveAsciiToMqttJsonVariable", JsonString3, Callback2);}
		
		var JsonString4 = JSON.stringify(Global_RemoteMqttGroup);
		
		$(that).closest('.Row').find(".Message").hide();
		$(that).closest('.Row').find(".Loading").show();
		RemoteMqttGroupFirstPage.prototype._TransmissionJson_(ssocket, "SaveRemoteMqttGroup", JsonString4, Callback3);
	}
	});
}

RemoteMqttGroupFirstPage.prototype._SaveButton_ = function(that)
{
	$(that).closest('.Row').find(".Loading").hide();
	$(that).closest('.Row').find(".Message").hide();
	var ssocket = io.connect();
	RemoteMqttGroupFirstPage.prototype._SaveStart_(that, ssocket);
}

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

RemoteMqttGroupFirstPage.prototype._RemoveRtuToRemoteMqttGroupName_ = function(GroupName)
{
	var object = jQuery.parseJSON(JSON.stringify(Global_RtuToMqttJsonVariable));
	var number=Object.keys(object);
	for(var i=0; i<number.length; i++)
	{
		var Variable = Object.keys(object[number[i]]);
		for(var j=0; j<Variable.length; j++)
		{
			if(GroupName == object[number[i]][Variable[j]]["ConnectionGroup"])
			{
				Global_RtuToMqttJsonVariable[number[i]][Variable[j]]["ConnectionGroup"] = "";
			}
		}
	}
}

RemoteMqttGroupFirstPage.prototype._RemoveTcpToRemoteMqttGroupName_ = function(GroupName)
{
	var object = jQuery.parseJSON(JSON.stringify(Global_TcpToMqttJsonVariable));
	var number=Object.keys(object);
	for(var i=0; i<number.length; i++)
	{
		var Variable = Object.keys(object[number[i]]);
		for(var j=0; j<Variable.length; j++)
		{
			if(GroupName == object[number[i]][Variable[j]]["ConnectionGroup"])
			{
				Global_TcpToMqttJsonVariable[number[i]][Variable[j]]["ConnectionGroup"] = "";
			}
		}
	}
}

RemoteMqttGroupFirstPage.prototype._RemoveAsciiToRemoteMqttGroupName_ = function(GroupName)
{
	var object = jQuery.parseJSON(JSON.stringify(Global_AsciiToMqttJsonVariable));
	var number=Object.keys(object);
	for(var i=0; i<number.length; i++)
	{
		var Variable = Object.keys(object[number[i]]);
		for(var j=0; j<Variable.length; j++)
		{
			if(GroupName == object[number[i]][Variable[j]]["ConnectionGroup"])
			{
				Global_AsciiToMqttJsonVariable[number[i]][Variable[j]]["ConnectionGroup"] = "";
			}
		}
	}
}

RemoteMqttGroupFirstPage.prototype._RemoveGroup_ = function(number)
{
	var GroupName = Global_RemoteMqttGroup[number]["Name"];
	Global_RemoteMqttGroup[number] = null;	delete Global_RemoteMqttGroup[number];
	
	RemoteMqttGroupFirstPage.prototype._RemoveRtuToRemoteMqttGroupName_(GroupName);
	RemoteMqttGroupFirstPage.prototype._RemoveTcpToRemoteMqttGroupName_(GroupName);
	RemoteMqttGroupFirstPage.prototype._RemoveAsciiToRemoteMqttGroupName_(GroupName);
}



RemoteMqttGroupFirstPage.prototype._ChangeAllCheckbox_ = function(that)
{
	var TableId = $(that).closest(".Table").attr("id");
	var Bool = $(that).closest(".Row").find(":checkbox").prop("checked");
	var Element = $("#"+TableId).find(".DynamicRow");
	Element.prop("checked",Bool);
	for(var i=0; i<Element.length; i++)
	{
		if(Element.eq(i).find(":checkbox").prop("checked") != Bool)
		{
			Element.eq(i).find(":checkbox").click();
		}
	}
}

RemoteMqttGroupFirstPage.prototype._GetAllNumber_ = function()
{
	var ObjectKey = Object.keys(Global_RemoteMqttGroup);
	return ObjectKey;
}

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

RemoteMqttGroupFirstPage.prototype._ShowTable_ = function(ObjectKey, DisplayNumber)
{
	$("#GroupTable").find(".DynamicRow").remove();
	var html = $(".Dynamic").html();
	var PageNumber = $("#GroupToolbar").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++)
	{
		$("#GroupTable").append(html);
		$("#GroupTable").find(".DynamicRow").eq(index).find(".Column").eq(0).text(ObjectKey[i]);
		$("#GroupTable").find(".DynamicRow").eq(index).find(".Column").eq(2).text(Global_RemoteMqttGroup[ObjectKey[i]]["Name"]);
		index++;
	}
}

RemoteMqttGroupFirstPage.prototype._UpdateTableToolbar_ = function(ObjectKey, DisplayNumber)
{
	var NumberOfPages = Math.ceil(ObjectKey.length/DisplayNumber);
	var Id = "#GroupToolbar";
	$(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);
	}
}

RemoteMqttGroupFirstPage.prototype._RedisplayUi_ = function()
{
	var DisplayNumber = 5;
	var ObjectKey = RemoteMqttGroupFirstPage.prototype._GetAllNumber_();
	RemoteMqttGroupFirstPage.prototype._UpdateTableToolbar_(ObjectKey, DisplayNumber);
	RemoteMqttGroupFirstPage.prototype._ShowTable_(ObjectKey, DisplayNumber);
	var Id = "#GroupTable";
	$(Id).find(".Row").eq(0).find(":checkbox").prop("checked",false);
}

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

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

RemoteMqttGroupFirstPage.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(RemoteMqttGroupFirstPage.prototype._IsNameRepeat_(Name) == true)
	{
		Row.eq(0).find(".Message").eq(1).show();
	}
}

RemoteMqttGroupFirstPage.prototype._EditButton_ = function(that)
{
	$("#Main_Center-Content").find(".Body").eq(2).find(".Message").hide();
	var number = parseInt($(that).closest(".DynamicRow").find(".Column").eq(0).text());
	$("#Main_Center-Content").find(".Body").eq(1).hide();
	var Name=$(that).closest(".DynamicRow").find(".Column").eq(2).text();

	$("#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();
	
	Name = $("#Main_Center-Content").find(".Body").eq(2).find(".FormTitle").eq(1).text();

	var Class = new Path();
	Class.LoadPathHtml(Name, 2);
	Class = null;
	RemoteMqttGroupSecondPage.prototype._SetInitialValue_(number);
}

RemoteMqttGroupFirstPage.prototype._AddNewRow_ = function(that)
{
	RemoteMqttGroupFirstPage.prototype._CheckGroupName_();
	var Id = "#GroupTable";
	var ShowErrorMessage = $(Id).find(".Row").eq(0).find(".Message").is(":visible");
	if(ShowErrorMessage == true)	{return 0;}
	RemoteMqttGroupFirstPage.prototype._InitialRemoteMqttGroupObject_(that);
	RemoteMqttGroupFirstPage.prototype._RedisplayUi_();
	RemoteMqttGroupFirstPage.prototype._AutomaticGroupName_();
}

RemoteMqttGroupFirstPage.prototype._DeleteRow_ = function(that)
{
	var Id = "#GroupTable";
	var element = $(Id).find(".DynamicRow").find(":checked");
	var CheckedLength = element.length;
	for(var i=0; i<CheckedLength; i++)
	{
		var number = element.eq(i).closest(".DynamicRow").find(".Column").eq(0).text();
		RemoteMqttGroupFirstPage.prototype._RemoveGroup_(number);
	}
	RemoteMqttGroupFirstPage.prototype._RedisplayUi_();
}

RemoteMqttGroupFirstPage.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;
}

























































RemoteMqttGroupSecondPage.prototype._CheckGroupName_ = function(that)
{
	$(that).closest(".Row").find(".Message").hide();
	var Name = $(that).closest(".Row").find(".Text").val();
	var Class = new Type();
	if(Class.EnglishDigitalAndBottomline(Name) == false)
	{
		$(that).closest(".Row").find(".Message").eq(0).show();
	}
	Class = null;
	if(RemoteMqttGroupSecondPage.prototype._IsNameRepeat_(Name) == true)
	{
		$(that).closest(".Row").find(".Message").eq(1).show();
	}
}

RemoteMqttGroupSecondPage.prototype._IsNameRepeat_ = function(Name)
{
	var Return=false;
	var Row = $("#Main_Center-Content").find(".Body").eq(2).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(number[i] == CurrentlyNumber)
		{
			continue;
		}	
		if(Name == Global_RemoteMqttGroup[number[i]]["Name"])
		{
			Return=true;
			break;
		}
	}
	return Return;
}

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

RemoteMqttGroupSecondPage.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;
}



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



RemoteMqttGroupSecondPage.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-RemoteMqttGroup").text(), 1, "main_PathRemoteMqttGroup_click(this)");
	Class = null;
}

RemoteMqttGroupSecondPage.prototype._ModifyRemoteMqttGroupObject_ = function(number)
{
	var MqttClientSettings = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0).find(".Row");
	MqttClientSettings.eq(0).find(".Text").val(number);
	Global_RemoteMqttGroup[number]["Name"]           = MqttClientSettings.eq(1).find(".Text").val();
	Global_RemoteMqttGroup[number]["ScanRate"]       = parseInt(MqttClientSettings.eq(2).find(".Text").val());
	Global_RemoteMqttGroup[number]["DeadBand"]       = parseFloat(MqttClientSettings.eq(3).find(".Text").val());
	Global_RemoteMqttGroup[number]["WillTopic"]      = MqttClientSettings.eq(4).find(".Text").val();
	Global_RemoteMqttGroup[number]["Will"]           = MqttClientSettings.eq(5).find(".Text").val();
	Global_RemoteMqttGroup[number]["MqttConnection"] = RemoteMqttGroupSecondPage.prototype._GetMqttConnection_();

	var PublishAndSubscribe = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(1).find(".Row");
	Global_RemoteMqttGroup[number]["PublishTopic"]   = PublishAndSubscribe.eq(0).find(".Text").val();
	Global_RemoteMqttGroup[number]["PublishQos"]     = parseInt(PublishAndSubscribe.eq(1).find(".Select").val());
	Global_RemoteMqttGroup[number]["SubscribeTopic"] = PublishAndSubscribe.eq(2).find(".Text").val();
	Global_RemoteMqttGroup[number]["SubscribeQos"]   = parseInt(PublishAndSubscribe.eq(3).find(".Select").val());
	Global_RemoteMqttGroup[number]["Retain"]         = parseInt(PublishAndSubscribe.eq(4).find(".Select").val());
}

RemoteMqttGroupSecondPage.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(6);
	var Table = Row.find(".BrokerTable");
	Table.empty();

	var RemoteNumber = Object.keys(Global_RemoteBroker);
	RemoteMqttGroupSecondPage.prototype._AddBrokerHtml_(Table, RemoteNumber);
	RemoteMqttGroupSecondPage.prototype._SetBrokerValue_(number, Table, RemoteNumber);
}

RemoteMqttGroupSecondPage.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);
	}
/*	for(var i=0; i<AzureNumber.length; i++)
	{
		$(Table).append(Html);
	}*/
}

RemoteMqttGroupSecondPage.prototype._SetBrokerValue_ = function(number, Table, RemoteNumber)
{
	var obj={Index:0};  var BrokerName;
	var Tr = Table.find(".BrokerTr");
	var BrokerName = Global_RemoteMqttGroup[number]["MqttConnection"];
	RemoteMqttGroupSecondPage.prototype._SetLocalBrokerName_(Tr, obj, BrokerName);
	RemoteMqttGroupSecondPage.prototype._SetRemoteBrokerName_(Tr, obj, RemoteNumber, BrokerName);
//	RemoteMqttGroupSecondPage.prototype._SetAzureBrokerName_(Tr, obj, AzureNumber, BrokerName);
	if(Tr.find(".BrokerTd").find(":checked").length == 0)
	{
		Tr.closest(".Row").find(".Message").show();
	}
}

RemoteMqttGroupSecondPage.prototype._SetLocalBrokerName_ = function(Tr, obj, BrokerName)
{
	var array = new Array("Broker (Local)");
	var Name = RemoteMqttGroupSecondPage.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)");
}

RemoteMqttGroupSecondPage.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 = RemoteMqttGroupSecondPage.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)");
	}
}

/*RemoteMqttGroupSecondPage.prototype._SetAzureBrokerName_ = function(Tr, obj, AzureNumber, BrokerName)
{
	for(var i=0; i<AzureNumber.length; i++)
	{
		obj.Index=obj.Index+1;
		var AzureBroker = Global_AzurePlatform[AzureNumber[i]]["Name"];
		var array = new Array(AzureBroker+" (Azure)");
		Name = RemoteMqttGroupSecondPage.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_AzurePlatform[AzureNumber[i]]["Name"]+" (Azure)");
	}
}*/

RemoteMqttGroupSecondPage.prototype._CheckScanRate_ = function(that)
{
	var ScanRate = $(that).closest(".RightColumn").find(".Text").val();
	var Class = new Type();
	ScanRate = parseInt(ScanRate);
	if(Class.CheckPositiveIntegers(ScanRate) == false)
	{
		$(that).closest(".RightColumn").find(".Text").val(1000);
		return 0;
	}
	if(ScanRate < 100)
	{
		$(that).closest(".RightColumn").find(".Text").val(1000);
		return 0;
	}
	$(that).closest(".RightColumn").find(".Text").val(ScanRate);
}

RemoteMqttGroupSecondPage.prototype._CheckDeadBand_ = function(that)
{
	var DeadBand = $(that).closest(".RightColumn").find(".Text").val();
	var Class = new Type();
	DeadBand = parseFloat(DeadBand);
	if(Class.CheckFloat(DeadBand) == false)
	{
		$(that).closest(".RightColumn").find(".Text").val(0);
		return 0;
	}
	if(DeadBand < 0)
	{
		$(that).closest(".RightColumn").find(".Text").val(0);
		return 0;
	}
	DeadBand = parseFloat(DeadBand.toFixed(4));
	$(that).closest(".RightColumn").find(".Text").val(DeadBand);
}

RemoteMqttGroupSecondPage.prototype._CheckMqttConnection_ = function(that)
{
	$(that).closest(".RightColumn").find(".Message").hide();
	var MqttConnection=false;
	var Table = $(that).closest(".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)
	{
		$(that).closest(".RightColumn").find(".Message").show();
	}
}

RemoteMqttGroupSecondPage.prototype._CheckTopic_ = function(that)
{
	$(that).closest(".Form").find(".Message").hide();
	var Row = $(that).closest(".Form").find(".Row");
	var PublishTopic = Row.eq(0).find(".Text").val();
	var SubscribeTopic = Row.eq(2).find(".Text").val();
	if(PublishTopic=="" && SubscribeTopic=="")
	{
		Row.eq(0).find(".Message").show();
		Row.eq(2).find(".Message").show();
	}
}

RemoteMqttGroupSecondPage.prototype._ModifyGlobalVariable_ = function(OldGroupName, NewGroupName)
{
	RemoteMqttGroupSecondPage.prototype._ModifyRtuToRemoteMqttGroupName_(OldGroupName, NewGroupName);
	RemoteMqttGroupSecondPage.prototype._ModifyTcpToRemoteMqttGroupName_(OldGroupName, NewGroupName);
	RemoteMqttGroupSecondPage.prototype._ModifyAsciiToRemoteMqttGroupName_(OldGroupName, NewGroupName);
}

RemoteMqttGroupSecondPage.prototype._ModifyRtuToRemoteMqttGroupName_ = function(OldGroupName, NewGroupName)
{
	var object = jQuery.parseJSON(JSON.stringify(Global_RtuToMqttJsonVariable));
	var number=Object.keys(object);
	for(var i=0; i<number.length; i++)
	{
		var Variable = Object.keys(object[number[i]]);
		for(var j=0; j<Variable.length; j++)
		{
			if(OldGroupName == object[number[i]][Variable[j]]["ConnectionGroup"])
			{
				Global_RtuToMqttJsonVariable[number[i]][Variable[j]]["ConnectionGroup"] = NewGroupName;
			}
		}
	}
}

RemoteMqttGroupSecondPage.prototype._ModifyTcpToRemoteMqttGroupName_ = function(OldGroupName, NewGroupName)
{
	var object = jQuery.parseJSON(JSON.stringify(Global_TcpToMqttJsonVariable));
	var number=Object.keys(object);
	for(var i=0; i<number.length; i++)
	{
		var Variable = Object.keys(object[number[i]]);
		for(var j=0; j<Variable.length; j++)
		{
			if(OldGroupName == object[number[i]][Variable[j]]["ConnectionGroup"])
			{
				Global_TcpToMqttJsonVariable[number[i]][Variable[j]]["ConnectionGroup"] = NewGroupName;
			}
		}
	}
}

RemoteMqttGroupSecondPage.prototype._ModifyAsciiToRemoteMqttGroupName_ = function(OldGroupName, NewGroupName)
{
	var object = jQuery.parseJSON(JSON.stringify(Global_AsciiToMqttJsonVariable));
	var number=Object.keys(object);
	for(var i=0; i<number.length; i++)
	{
		var Variable = Object.keys(object[number[i]]);
		for(var j=0; j<Variable.length; j++)
		{
			if(OldGroupName == object[number[i]][Variable[j]]["ConnectionGroup"])
			{
				Global_AsciiToMqttJsonVariable[number[i]][Variable[j]]["ConnectionGroup"] = NewGroupName;
			}
		}
	}
}