//第1頁
RegisterModuleFirstPage.prototype._Initialization_ = function()
{
	RegisterModuleFirstPage.prototype._InitializationGlobalVariable_();
	RegisterModuleFirstPage.prototype._RedisplayUi_();
}
RegisterModuleFirstPage.prototype._LoadLanName_ = function()
{
	var Class = new LoadFile();
	var Lan = Class.ReadFile("./File/LAN.json");
	var LanDevice = $("#Main_Center-Content").find(".Body").eq(0).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;
}
RegisterModuleFirstPage.prototype._InitializationGlobalVariable_ = function()
{
	if(typeof(Global_RegisterModule)!="object")
	{
		Global_RegisterModule= new Object();
	}
}
RegisterModuleFirstPage.prototype._GetAllDeviceName_ = function()
{
	var Return=new Array();
	var Element = $("#Main_Center-Content").find(".Body").eq(0).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;
}
RegisterModuleFirstPage.prototype._RedisplayUi_ = function()
{
	var DisplayNumber = 20; //RTU 20 TCP 40 ASCII 20 Register 8,100
	var ObjectKey = RegisterModuleFirstPage.prototype._GetAllNumber_();
	RegisterModuleFirstPage.prototype._UpdateNumber_(ObjectKey);
	ObjectKey = Object.keys(Global_RegisterModule);
	RegisterModuleFirstPage.prototype._UpdateTableToolbar_(ObjectKey, DisplayNumber);
	RegisterModuleFirstPage.prototype._ShowTable_(ObjectKey, DisplayNumber);
	var Id = "#ModuleTable";
	$(Id).find(".Row").eq(0).find(":checkbox").prop("checked",false);
}
RegisterModuleFirstPage.prototype._GetAllNumber_ = function()
{
	var ObjectKey = new Array();
	ObjectKey = ObjectKey.concat(Object.keys(Global_RegisterModule));
	return ObjectKey;
}
RegisterModuleFirstPage.prototype._UpdateNumber_ = function(ObjectKey)
{
	var AllNumber=RegisterModuleFirstPage.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_RegisterIndex.length; i++)
	{
		if(parseInt(Global_RegisterIndex[i])>parseInt(MaxNumber)){
			RegisterModuleFirstPage.prototype._DeleteRepeatNumber_(Global_RegisterIndex[i]);
			i--;
		}
	}
	var DynamicRowLength = ObjectKey.length;
	var TemporaryIndexLength = Global_RegisterIndex.length;
	var Id = "#Number";
	$(Id).empty();
	for(var i=0; i<TemporaryIndexLength; i++)
	{
		$(Id).append($("<option></option>").attr("value", Global_RegisterIndex[i]).text(Global_RegisterIndex[i]));
	}
	$(Id).append($("<option></option>").attr("value", DynamicRowLength+TemporaryIndexLength+1).text(DynamicRowLength+TemporaryIndexLength+1));
}
RegisterModuleFirstPage.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);
	}
}
RegisterModuleFirstPage.prototype._DeleteRepeatNumber_ = function(number)
{
	for(var i=0; i<Global_RegisterIndex.length; i++)
	{
		if(number == Global_RegisterIndex[i])
		{
			Global_RegisterIndex.splice(i,1);
		}
	}
}

RegisterModuleFirstPage.prototype._ShowTable_ = function(ObjectKey, DisplayNumber)
{
	$("#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_RegisterModule[ObjectKey[i]]["ModuleName"]);
		index++;
	}
}
RegisterModuleFirstPage.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 = RegisterModuleFirstPage.prototype._CheckRegisterModuleQuantity_();
	if(OK == false)	{return 0;}
	RegisterModuleFirstPage.prototype._BuildRegisterModuleAllObject_(that);
	RegisterModuleFirstPage.prototype._RedisplayUi_();
}
RegisterModuleFirstPage.prototype._CheckRegisterModuleQuantity_ = function()
{
	var Return=true;
	var AllNumber = RegisterModuleFirstPage.prototype._GetAllNumber_();
	if(AllNumber.length >= 8)
	{
		Return=false;
	}
	return Return;
}
RegisterModuleFirstPage.prototype._BuildRegisterModuleAllObject_ = function(that)
{
	var number = $(that).closest(".Row").find(".Column").eq(1).find(".Select").val();
	RegisterModuleFirstPage.prototype._DeleteRepeatNumber_(number);
	var ModuleName = $(that).closest(".Row").find(".Column").eq(2).find(".Text").val();
	RegisterModuleFirstPage.prototype._BuildRegisterModuleObject_(ModuleName, number);
}
RegisterModuleFirstPage.prototype._BuildRegisterModuleObject_ = function(ModuleName, number)
{
	if(typeof(Global_RegisterModule[number]) == "undefined"){
		Global_RegisterModule[number] = new Object();
	}
	Global_RegisterModule[number]["ModuleName"] = ModuleName;
}
RegisterModuleFirstPage.prototype._CheckModuleName_ = function(that)
{
	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_ExcludeBottomLineBrackets(Name) == true)
	{
		$(Id).find(".Row").eq(0).find(".Message").eq(1).show();
	}
	else if(Class.CheckChineseLanguage(Name) == true)
	{
		$(Id).find(".Row").eq(0).find(".Message").eq(1).show();
	}
	Class = null;
}
RegisterModuleFirstPage.prototype._IsTickedModule_ = function()
{
	var Return=false;
	var Id = "#ModuleTable";
	if($(Id).find(".DynamicRow").find(":checked").length >0)
	{
		Return=true;
	}
	return Return;
}
RegisterModuleFirstPage.prototype._CopyDialogEnabled_ = function(Event)
{
	$(".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");
}
RegisterModuleFirstPage.prototype._CopyModule_ = function()
{
	RegisterModuleFirstPage.prototype._CopyTickedModule_();
	RegisterModuleFirstPage.prototype._RedisplayUi_();
	RegisterModuleFirstPage.prototype._CopyDialogDisabled_();
}
RegisterModuleFirstPage.prototype._CopyTickedModule_ = function()
{
	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());
		RegisterModuleFirstPage.prototype._CopyGlobalVariable_(number);
	}
}
RegisterModuleFirstPage.prototype._CopyGlobalVariable_ = function(number)
{
	var SlaveId;
	var ObjectKey = RegisterModuleFirstPage.prototype._GetAllNumber_();
	var Index = ObjectKey.length + Global_RegisterIndex.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=RegisterModuleFirstPage.prototype._CheckRegisterModuleQuantity_();
		if(OK==false)		{break;}
		
		if(typeof(Global_RegisterModule[number]) != "undefined"){
			Global_RegisterModule[Index] = jQuery.parseJSON(JSON.stringify(Global_RegisterModule[number]));
		}
		if(typeof(Global_RegisterSetting[number]) != "undefined"){
			Global_RegisterSetting[Index] = jQuery.parseJSON(JSON.stringify(Global_RegisterSetting[number]));
		}
		if(typeof(Global_RegisterVariable[number]) != "undefined"){
			Global_RegisterVariable[Index] = jQuery.parseJSON(JSON.stringify(Global_RegisterVariable[number]));
		}
		RegisterModuleSecondPage.prototype._SynchronizeOtherServiceData_(Index);
		Index++;
	}
}
RegisterModuleFirstPage.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("");
}
RegisterModuleSecondPage.prototype._SynchronizeOtherServiceData_ = function(number)
{
	if(typeof(Global_RegisterVariable[number]) != "object")	{return 0;}
	var OldObject=jQuery.parseJSON(JSON.stringify(Global_RegisterToOpcuaVariable));
	var object = RegisterModuleSecondPage.prototype._CreateVariableEmptyObject_(number);
	var RegisterToOpcua = RegisterModuleSecondPage.prototype._InitialRegisterToOpcuaEmptyObject_(object, number);
	RegisterModuleSecondPage.prototype._RetainRegisterToOpcuaData_(TcpToOpcua, number);
	var Class=new SynchronizeService();
	Class.ChangeVariableSynchronizationService("MTCP", OldObject, object, number);
	Class=null;
}
RegisterModuleSecondPage.prototype._CreateVariableEmptyObject_ = function(number)
{
	var Return=new Object();
	var object=jQuery.parseJSON(JSON.stringify(Global_RegisterVariable[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;
}
RegisterModuleFirstPage.prototype._DeleteModule_ = function()
{
	RegisterModuleFirstPage.prototype._RemoveTickedModule_();
	RegisterModuleFirstPage.prototype._RedisplayUi_();
}
RegisterModuleFirstPage.prototype._RemoveTickedModule_ = function()
{
	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();
		RegisterModuleFirstPage.prototype._RemoveGlobalVariable_(number);
	}
}
RegisterModuleFirstPage.prototype._RemoveGlobalVariable_ = function(number)
{
	Global_RegisterModule[number] = null;						delete Global_RegisterModule[number];
	Global_RegisterVariable[number] = null;						delete Global_RegisterVariable[number];
	Global_RegisterSetting[number] = null;						delete Global_RegisterSetting[number];
	Global_RegisterIndex.push(number);
	Global_RegisterIndex = Global_RegisterIndex.sort(function (a, b) {return a - b});
	Global_RegisterToOpcuaVariable[number] = null;				delete Global_RegisterToOpcuaVariable[number];
	//Global_RegisterToMqttSetting[number] = null;			    delete Global_RegisterToMqttSetting[number];
	//Global_RegisterToMqttVariable[number] = null;		    	delete Global_RegisterToMqttVariable[number];
	var Class=new ClearService();
	Class.DeleteModuleClearService("MTCP", number);
	Class=null;
}
RegisterModuleFirstPage.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(0).css('pointer-events', "none");
	$("#Main_Center-Content").find(".Body").eq(0).css('opacity', 0.5);

	$(".Dialog").eq(1).css('display', "block");
}
RegisterModuleFirstPage.prototype._ClearAllModule_ = function(that)
{
	var number = Object.keys(Global_RegisterModule);
	for(var i=0; i<number.length; i++)
	{
		RegisterModuleFirstPage.prototype._RemoveGlobalVariable_(number[i]);
	}
	RegisterModuleFirstPage.prototype._RedisplayUi_();
	RegisterModuleFirstPage.prototype._ClearDialogDisabled_();
}
RegisterModuleFirstPage.prototype._ClearDialogDisabled_ = function()
{
	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(1).css('display', "none");
	$(".Dialog").eq(1).find(".DialogTable").find(".Row").find(".Column").eq(1).find(".Text").val("");
}
RegisterModuleFirstPage.prototype._ShowPreviousPage_ = function()
{
	var Id = "#ModuleToolbar";
	var CurrentlyPage = $(Id).find(".Text").val();
	$(Id).find(".Text").val(parseInt(CurrentlyPage)-1);
	RegisterModuleFirstPage.prototype._RedisplayUi_();
}
RegisterModuleFirstPage.prototype._ShowNextPage_ = function()
{
	var Id = "#ModuleToolbar";
	var CurrentlyPage = $(Id).find(".Text").val();
	$(Id).find(".Text").val(parseInt(CurrentlyPage)+1);
	RegisterModuleFirstPage.prototype._RedisplayUi_();
}
RegisterModuleFirstPage.prototype._EditButton_ = function(that)
{
	$("#Main_Center-Content").find(".Body").eq(1).find(".Message").hide();
	var number = parseInt($(that).closest(".DynamicRow").find(".Column").eq(1).text());
	$("#Main_Center-Content").find(".Body").eq(0).hide();
	$("#Main_Center-Content").find(".Body").eq(1).show();
	var Name = $("#Main_Center-Content").find(".Body").eq(1).find(".FormTitle").eq(0).text();
	var Class = new Path();
	Class.LoadPathHtml(Name, 2);
	Class = null;
	RegisterModuleSecondPage.prototype._SetInitialValue_(number);
}
RegisterModuleSecondPage.prototype._SetInitialValue_ = function(number)
{
	RegisterModuleSecondPage.prototype._ClearForm_();
	var ContentSettings = $(".Form").eq(0).find(".Row");
	ContentSettings.eq(0).find(".Text").val(number);
	RegisterModuleSecondPage.prototype._SetFormInitialValue_(number);
	if(typeof(Global_RegisterVariable[number]) == "undefined"){
		return 0;
	}
}
RegisterModuleSecondPage.prototype._ClearForm_ = function()
{
	$("#VariableTable").find(".DynamicRow").remove();
}
RegisterModuleSecondPage.prototype._SetFormInitialValue_ = function(number)
{
	var ContentSettings = $(".Form").eq(0).find(".Row");
	ContentSettings.eq(0).find(".Text").val(number);
	ContentSettings.eq(1).find(".Text").val(Global_RegisterModule[number]["ModuleName"]);
	if(typeof(Global_RegisterVariable[number]) == "undefined")
	{
		Global_RegisterVariable[number] = new Object();
	}
	var ObjectKey = Object.keys(Global_RegisterVariable[number]);
	var RowIndex = ObjectKey.length;
	RegisterModuleSecondPage.prototype._CreateRowTable_(RowIndex);
	RegisterModuleSecondPage.prototype._AddRowVariableTable_(ObjectKey, number);
}
RegisterModuleSecondPage.prototype._CreateRowTable_ = function(index)
{
	var html = $(".Dynamic").eq(1).html();
	for(var i = 0; i < index; i++)
	{
		$("#VariableTable").append(html);
	}
}
RegisterModuleSecondPage.prototype._AddRowVariableTable_ = function(ObjectKey, number)
{
	for(var i = 0; i < ObjectKey.length; i++)
	{
		$("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(2).text(Global_RegisterVariable[number][ObjectKey[i]]["Number"]);
		$("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(3).find(".Text").val(Global_RegisterVariable[number][ObjectKey[i]]["VariableName"]);
		$("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(5).text(Global_RegisterVariable[number][ObjectKey[i]]["DataType"]);
		$("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(6).find(".Text").val(Global_RegisterVariable[number][ObjectKey[i]]["Description"]);
	}
}


RegisterModuleSecondPage.prototype._ProduceTable_ = function(that)
{
	var Obj = RegisterModuleSecondPage.prototype._GetSettingFormValue_();
	$(that).closest(".Row").find(".Message").hide();
	$(that).closest(".Row").find(".Message").eq(1).show();
	setTimeout(function(){
		RegisterModuleSecondPage.prototype._CreateTable_();
		$(that).closest(".Row").find(".Message").eq(1).hide();
	}, 500);
}
RegisterModuleSecondPage.prototype._GetSettingFormValue_ = function()
{
	var DataAttribute, DataType, DataModel, DataCounter, object=new Object();
	var ContentSettings = $("#SettingForm").find(".Row");
	object["DataAttribute"] = ContentSettings.eq(0).find(".Select").val();
	object["DataType"] = ContentSettings.eq(1).find(".Select").val();
	object["DataCounter"] = ContentSettings.eq(2).find(".Text").val();
	return object;
}
RegisterModuleSecondPage.prototype._CreateTable_ = function(that)
{
	var NowIndex = $("#VariableTable").find(".DynamicRow").length;
	var html = $(".Dynamic").eq(1).html();
	var Counter = $("#SettingForm").find(".Row").eq(2).find(".Text").val();
	var Max = parseInt(NowIndex) + parseInt(Counter);
	if(Max > 100)
	{
		Counter = 100 - NowIndex;
	}
	var Start = RegisterModuleSecondPage.prototype._GetStartAddress_();
	var End = RegisterModuleSecondPage.prototype._GetStartAddress_() + parseInt(Counter);
	for(var i = 0;i<Counter;i++)
	{
		$("#VariableTable").append(html);
	}
	var object= new Object();
	var ContentSettings = $("#SettingForm").find(".Row");
	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();
	if(ReadWrite == "ReadWrite")
	{
		ReadWrite = "AO";
	}
	else
	{
		ReadWrite = "AI";
	}
	for (var i = Start; i < End; i++)
	{
		var VariableIndex = ReadWrite + "-" + DataType + "-" + i;
		object[VariableIndex] = new Object();
		object[VariableIndex]["Number"] = i;
		object[VariableIndex]["DataAttribute"] = ContentSettings.eq(0).find(".Select").val();
		object[VariableIndex]["DataType"] = ContentSettings.eq(1).find(".Select").val();
	}
	var ObjectKey = Object.keys(object);
	for (var i = 0; i < Counter; i++)
	{
		var Index = i + NowIndex;
		$("#VariableTable").find(".DynamicRow").eq(Index).find(".Column").eq(2).text(object[ObjectKey[i]]["Number"]);
		var TagIndex = parseInt(Start) + parseInt(i) - 1;
		$("#VariableTable").find(".DynamicRow").eq(Index).find(".Column").eq(3).find(".Text").val("Tag" + TagIndex);
		$("#VariableTable").find(".DynamicRow").eq(Index).find(".Column").eq(5).text(object[ObjectKey[i]]["DataType"]);
	}
}
RegisterModuleSecondPage.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;
}

RegisterModuleSecondPage.prototype._GetStartAddress_ = function(CurrentData)
{
	var Return = 1, VariableIndex = new Array(), MaxAddress = 0;
	var DynamicRow = $("#VariableTable").find(".DynamicRow");
	if(DynamicRow.length==0)
	{
		return Return;
	}
	for(var i=0; i<DynamicRow.length; i++)
	{
		VariableIndex[i] = $("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(2).text();
	}
	MaxAddress = VariableIndex.sort(function(a, b){return b - a});
	Return = parseInt(MaxAddress[0]) + 1;
	return Return;
}
RegisterModuleSecondPage.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];
}
RegisterModuleSecondPage.prototype._NoConfirmButton_ = function()
{
	$("#Main_Center-Content").find(".Body").eq(1).hide();
	$("#Main_Center-Content").find(".Body").eq(0).show();
	var Class = new Path();
	Class.LoadPathHtml($("#Left-List-Register").text(), 1, "main_PathRegisterModule_click(this)");
	Class = null;
}
RegisterModuleSecondPage.prototype._ConfirmButton_ = function(that)
{
	$(that).closest(".Form").find(".Message").hide();
	$("#SettingForm").find(".Message").hide();
	var Element = $("#Main_Center-Content").find(".Body").eq(1).find(".Message");
	for(var i=0; i<Element.length; i++)
	{
		if(Element.eq(i).is(":visible") == true && Element.eq(i).css("color") != "rgb(34, 119, 0)"){
			$(that).closest(".Form").find(".Message").eq(0).show();
			return 0;
		}
	}
	var ContentSettings = $(".Form").eq(0).find(".Row");
	var number = parseInt(ContentSettings.eq(0).find(".Text").val());
	var ModuleName = ContentSettings.eq(1).find(".Text").val();	
	RegisterModuleFirstPage.prototype._BuildRegisterModuleObject_(ModuleName, number);
	RegisterModuleSecondPage.prototype._BuildRegisterSettingObject_(number);
	RegisterModuleSecondPage.prototype._BuildRegisterVariableObject_(number);
	RegisterModuleSecondPage.prototype._SynchronizeOtherServiceData_(number);
	$("#Main_Center-Content").find(".Body").eq(1).hide();
	$("#Main_Center-Content").find(".Body").eq(0).show();
	RegisterModuleFirstPage.prototype._RedisplayUi_();
}
RegisterModuleSecondPage.prototype._SynchronizeOtherServiceData_ = function(number)
{
	if(typeof(Global_RegisterVariable[number]) != "object") {return 0;}
	var object = RegisterModuleSecondPage.prototype._CreateVariableEmptyObject_(number);
	var RegisterToOpcua = RegisterModuleSecondPage.prototype._InitialRegisterToOpcuaEmptyObject_(object, number);
	RegisterModuleSecondPage.prototype._RetainRegisterToOpcuaData_(RegisterToOpcua, number);
}
RegisterModuleSecondPage.prototype._InitialRegisterToOpcuaEmptyObject_ = 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;
}
RegisterModuleSecondPage.prototype._RetainRegisterToOpcuaData_ = function(object, number)
{
	if(typeof(Global_RegisterToOpcuaVariable[number]) != "object"){
		Global_RegisterToOpcuaVariable[number] = new Object();
	}
	var NewSystemVariable = Object.keys(object[number]);
	var OldSystemVariable = Object.keys(Global_RegisterToOpcuaVariable[number]);
	var SameVariable 	  = RegisterModuleSecondPage.prototype._ArrayInteraction_(NewSystemVariable, OldSystemVariable);
	for(var i = 0; i < SameVariable.length; i++)
	{
		object[number][SameVariable[i]] = Global_RegisterToOpcuaVariable[number][SameVariable[i]];
	}
	Global_RegisterToOpcuaVariable[number] 	= jQuery.parseJSON(JSON.stringify(object[number]));
}
RegisterModuleSecondPage.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;
}
RegisterModuleSecondPage.prototype._CheckModuleName_ = function(that)
{
	$(that).closest(".Row").find(".Message").hide();
	var Name = $(that).closest(".Row").find(".Text").val();
	var Class = new Type();
	if(Name == ""){
		$(that).closest(".Row").find(".Message").eq(0).show();
	}
	else if(Class.CheckSpecialSymbols_ExcludeBottomLineBrackets(Name) == true)
	{
		$(that).closest(".Row").find(".Message").eq(1).show();
	}
	else if(Class.CheckChineseLanguage(Name) == true)
	{
		$(that).closest(".Row").find(".Message").eq(1).show();
	}
	Class = null;
}
RegisterModuleSecondPage.prototype._BuildRegisterSettingObject_ = function(number)
{

}
RegisterModuleSecondPage.prototype._BuildRegisterVariableObject_ = function(number)
{
	var RowIndex = $("#VariableTable").find(".DynamicRow").length;
	var object= new Object();
	for(var i=0;i<RowIndex;i++)
	{
		var DataNumber = $("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(2).text();
		var VariableName = $("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(3).find(".Text").val();
		var DataAttribute = $("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(4).find(".Select").val();
		var DataType = $("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(5).text();
		var Description = $("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(6).find(".Text").val();
		var Flag = RegisterModuleSecondPage.prototype._GetObjFlag_(DataAttribute, DataType, DataNumber);
		
		object[Flag] = new Object();
		object[Flag]["Number"] = DataNumber;
		object[Flag]["VariableName"] = VariableName;
		object[Flag]["DataAttribute"] = DataAttribute;
		object[Flag]["DataType"] = DataType;
		object[Flag]["Description"] = Description;
	}
	Global_RegisterVariable[number] = object;
}
RegisterModuleSecondPage.prototype._GetObjFlag_ = function(DataAttribute, DataType, DataNumber)
{
	var Return;
	if(DataAttribute == "ReadWrite")
	{
		if(DataType == "Bool")
		{
			DataAttribute = "DO";
		}
		else
		{
			DataAttribute = "AO";
		}
	}
	else
	{
		if(DataType == "Bool")
		{
			DataAttribute = "DI";
		}
		else
		{
			DataAttribute = "AI";
		}
	}
	Return = DataAttribute + "-" + DataType + "-" + DataNumber;
	return Return;
}
RegisterModuleSecondPage.prototype._DeleteButton_ = function(that)
{
	var Id = "#VariableTable";
	var element = $(Id).find(".Checkbox:checked");
	element.closest(".DynamicRow").remove();
}
RegisterModuleSecondPage.prototype._CheckDataNumber_ = function(that)
{
	var Class = new Type();
	var Length = $(that).val();
	if(Class.CheckPositiveIntegers(Length) == false){
		$(that).val(1);
		Class = null;
		return 0;
	}
	var TableId=$(that).closest(".Table").attr("id");
	var FormId=$(that).closest(".Form").attr("id");
	if(typeof(TableId) != "undefined")
	{
		var DataType=TableId.replace("TotalTable", "");;
	}
	else if(typeof(FormId) != "undefined")
	{
		var DataType=$("#SettingForm").find(".Row").eq(0).find(".Select").val();
	}
	Length=Class.CheckModbusDataModelMaxQuantity(DataType, parseInt(Length));
	$(that).val(Length);
	Class = null;
}
RegisterModuleSecondPage.prototype._CheckVariableName_ = function(that)
{
	var CurrentlyName;
	var AllName = RegisterModuleSecondPage.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(RegisterModuleSecondPage.prototype._IsNameFormat_(CurrentlyName) == false)
		{
			 Element.eq(i).find(".Message").eq(1).show();
		}
		else if(RegisterModuleSecondPage.prototype._IsNameRepeat_(AllName, CurrentlyName) == true)
		{
			 Element.eq(i).find(".Message").eq(0).show();
		}
	}
}
RegisterModuleSecondPage.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;
}
RegisterModuleSecondPage.prototype._IsNameFormat_ = function(Name)
{
	var Return = true, Class = new Type();
	if(Class.CheckChineseLanguage(Name) == true ||
	   Class.CheckSpecialSymbols(Name) 	== true ||
	   Name == "")
	{
		Return = false;
	}
	return Return;
}
RegisterModuleSecondPage.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;
}
RegisterModuleFirstPage.prototype._SaveButton_ = function(that)
{
	var Result={"Input":null};
	$(that).closest('.Row').find(".Loading").show();
	$(that).closest('.Row').find(".Message").hide();
	RegisterModuleFirstPage.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);
}
RegisterModuleFirstPage.prototype._SaveDataLayerAndServiceLayerVariables_ = function(Result)
{
	var RReturn = $.Deferred();
	var Result_1={"Input":null}, Result_2={"Input":null}, Result_3={"Input":null}, Result_4={"Input":null};
	RegisterModuleFirstPage.prototype._SaveModuleSetting_(Result_1);
	RegisterModuleFirstPage.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();
}
RegisterModuleFirstPage.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("SaveRegisterIndex", JSON.stringify(Global_RegisterIndex), Result_1);
	Class.TransmissionString("SaveRegisterModule", JSON.stringify(Global_RegisterModule), Result_2);
	Class.TransmissionString("SaveRegisterVariable", JSON.stringify(Global_RegisterVariable), Result_3);
	Class.TransmissionString("SaveRegisterSetting", JSON.stringify(Global_RegisterSetting), 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);
}
RegisterModuleFirstPage.prototype._SaveConvertSetting_ = function(Result)
{
	var Result_1={"Input":null};
	var Class = new Transmission();
	Class.TransmissionString("SaveRegisterToOpcuaVariable", JSON.stringify(Global_RegisterToOpcuaVariable), Result_1);
	var SetInterval = setInterval(function()
	{
		if(Result_1["Input"]==true)
		{
			Result["Input"] = true;
			Class = null; delete Class;
			clearInterval(SetInterval);
		}
	},500);
}
RegisterModuleSecondPage.prototype._DeleteTableButton_ = function(Event)
{
	var button = $('#SettingForm').find(".Button").offset();
	var MouseX = (Event.pageX-250)+"px";
	var MouseY = (Event.pageY-350)+"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");
}