//第1頁
AsciiModuleFirstPage.prototype._GetGlobal_AsciiModuleObjectKey_ = function(DeviceName)
{
	var Return = new Array();
	if(typeof(Global_AsciiModule[DeviceName]) == "object")
	{
		Return = Object.keys(Global_AsciiModule[DeviceName]);
	}
	return Return;
}

AsciiModuleFirstPage.prototype._ArrayComplement_ = 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;
}

AsciiModuleFirstPage.prototype._LoadComPortName_ = function()
{
	var Class = new LoadFile();  var LockComPortDevice = new Array();
	var ComPort = Class.ReadFile("./File/ComPortInterfaceSetting.json");
	ComPort = Object.keys(ComPort);
	for(var i=0; i<ComPort.length; i++)
	{
		if(typeof(Global_RtuModule[ComPort[i]]) != "object"){
			continue;
		}
		var array = Object.keys(Global_RtuModule[ComPort[i]]);
		if(array.length){
			LockComPortDevice.push(ComPort[i]);
		}
	}
	ComPort = AsciiModuleFirstPage.prototype._ArrayComplement_(ComPort, LockComPortDevice);
	var ComPortDevice = $("#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");
	ComPortDevice.empty();	CopyDialog.empty();	RemoveDialog.empty();
	for(var i=0; i<ComPort.length; i++){
		ComPortDevice.append($("<option></option>").attr("value",ComPort[i]).text(ComPort[i]));
		CopyDialog.append($("<option></option>").attr("value",ComPort[i]).text(ComPort[i]));
		RemoveDialog.append($("<option></option>").attr("value",ComPort[i]).text(ComPort[i]));
	}
	Class = null;
}

AsciiModuleFirstPage.prototype._ChangeComPortDevice_ = function(that)
{
	AsciiModuleFirstPage.prototype._RedisplayUi_();
}

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

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

AsciiModuleFirstPage.prototype._GetAllNumber_ = function()
{
	var AllDeviceName = AsciiModuleFirstPage.prototype._GetAllDeviceName_();
	var ObjectKey = new Array();
	for(var i=0; i<AllDeviceName.length; i++)
	{
		if(typeof(Global_AsciiModule[AllDeviceName[i]]) == "object")
		{
			ObjectKey = ObjectKey.concat(Object.keys(Global_AsciiModule[AllDeviceName[i]]));
		}
	}
	return ObjectKey;
}

AsciiModuleFirstPage.prototype._CreateAsciiSettingObject_ = function(number)
{
	var DeviceName = AsciiModuleFirstPage.prototype._GetDeviceName_();
	if(typeof(Global_AsciiModule[DeviceName]) != "object"){
		return 0;
	}
	Global_AsciiSetting[number] = new Object();
	Global_AsciiSetting[number]["SlaveId"] = AsciiModuleFirstPage.prototype._InitializationSlaveId_(DeviceName);
	Global_AsciiSetting[number]["Timeout"] = 500;
}

AsciiModuleFirstPage.prototype._CreateModuleFunction_ = function(number)
{
	var DeviceName = AsciiModuleFirstPage.prototype._GetDeviceName_();
	if(typeof(Global_AsciiModule[DeviceName]) != "object")	{return 0;}
	Global_AsciiModule[DeviceName][number]["ASCII"] = 1;
}

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

AsciiModuleFirstPage.prototype._BuildAsciiModuleAllObject_ = function(that)
{
	var number = $(that).closest(".Row").find(".Column").eq(1).find(".Select").val();
	AsciiModuleFirstPage.prototype._DeleteRepeatNumber_(number);
	var ModuleName = $(that).closest(".Row").find(".Column").eq(2).find(".Text").val();
	AsciiModuleFirstPage.prototype._BuildAsciiModuleObject_(ModuleName, number);
	AsciiModuleFirstPage.prototype._CreateModuleFunction_(number);
	AsciiModuleFirstPage.prototype._CreateAsciiSettingObject_(number);
}

AsciiModuleFirstPage.prototype._BuildAsciiModuleObject_ = function(ModuleName, number)
{
	var DeviceName = AsciiModuleFirstPage.prototype._GetDeviceName_();
	if(DeviceName == null){
		return 0;
	}
	if(typeof(Global_AsciiModule[DeviceName][number]) == "undefined"){
		Global_AsciiModule[DeviceName][number] = new Object();
	}
	Global_AsciiModule[DeviceName][number]["ModuleName"] = ModuleName;
}

AsciiModuleFirstPage.prototype._InitializationSlaveId_ = function(DeviceName)
{
	var SlaveId=1;  var SlaveIdArray=new Array();
	var ObjKey = AsciiModuleFirstPage.prototype._GetGlobal_AsciiModuleObjectKey_(DeviceName);
	for(var i=0; i<ObjKey.length; i++)
	{
		if(typeof(Global_AsciiSetting[ObjKey[i]]["SlaveId"]) != "undefined")
		{
			SlaveIdArray.push(Global_AsciiSetting[ObjKey[i]]["SlaveId"]);
		}
	}
	while(true)
	{
		if(SlaveIdArray.indexOf(SlaveId) == -1){
			break;
		}
		SlaveId++;
	}
	return SlaveId;
}

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

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

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

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

AsciiModuleFirstPage.prototype._ModuleAddNewRow_ = function(that)
{
	var DeviceName = AsciiModuleFirstPage.prototype._GetDeviceName_();
	var SlaveId = AsciiModuleFirstPage.prototype._InitializationSlaveId_(DeviceName);
	if(SlaveId>254)	{return 0;}
	var Id = "#ModuleTable";
	var ShowErrorMessage = $(Id).find(".Row").eq(0).find(".Message").is(":visible");
	if(ShowErrorMessage == true)	{return 0;}
	AsciiModuleFirstPage.prototype._BuildAsciiModuleAllObject_(that);
	AsciiModuleFirstPage.prototype._RedisplayUi_();
}

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

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

AsciiModuleFirstPage.prototype._CopyGlobalVariable_ = function(DeviceName, CopyToDeviceName, number)
{
	var SlaveId;
	var ObjectKey = AsciiModuleFirstPage.prototype._GetAllNumber_();
	var Index = ObjectKey.length+Global_AsciiIndex.length+1;
	var CopyNumber =  parseInt($(".Dialog").eq(0).find(".DialogTable").find(".Row").eq(1).find(".Text").val());
	for(j=0; j<CopyNumber; j++)
	{
		SlaveId = AsciiModuleFirstPage.prototype._InitializationSlaveId_(CopyToDeviceName);
		if(SlaveId > 254)	{break;}
		if(typeof(Global_AsciiModule[DeviceName][number]) != "undefined"){
			Global_AsciiModule[CopyToDeviceName][Index] = jQuery.parseJSON(JSON.stringify(Global_AsciiModule[DeviceName][number]));
		}
		if(typeof(Global_AsciiSetting[number]) != "undefined"){
			Global_AsciiSetting[Index] = jQuery.parseJSON(JSON.stringify(Global_AsciiSetting[number]));
			Global_AsciiSetting[Index]["SlaveId"] = SlaveId;
		}
		if(typeof(Global_AsciiVariable[number]) != "undefined"){
			Global_AsciiVariable[Index] = jQuery.parseJSON(JSON.stringify(Global_AsciiVariable[number]));
		}
		AsciiModuleSecondPage.prototype._SynchronizeOtherServiceData_(Index);
		Index++;
	}
}

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

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

AsciiModuleFirstPage.prototype._RemoveGlobalVariable_ = function(DeviceName, number)
{
	Global_AsciiModule[DeviceName][number] = null;	delete Global_AsciiModule[DeviceName][number];
	Global_AsciiVariable[number] = null;			delete Global_AsciiVariable[number];
	Global_AsciiSetting[number] = null;				delete Global_AsciiSetting[number];
	Global_AsciiIndex.push(number);
	Global_AsciiIndex = Global_AsciiIndex.sort(function (a, b) {return a - b});
	Global_AsciiToOpcuaVariable[number] = null;		delete Global_AsciiToOpcuaVariable[number];
	Global_AsciiToMqttSetting[number] = null;		delete Global_AsciiToMqttSetting[number];
	Global_AsciiToMqttVariable[number] = null;		delete Global_AsciiToMqttVariable[number];
	Global_AsciiToMqttJsonVariable[number] = null;	delete Global_AsciiToMqttJsonVariable[number];
	var Class=new ClearService();
	Class.DeleteModuleClearService("MASCII", number);
	Class=null;
}

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

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

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

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

AsciiModuleFirstPage.prototype._AutomaticConfigurationComPortName_ = function()
{
	var AllDeviceName = AsciiModuleFirstPage.prototype._GetAllDeviceName_();
	var Select = $("#Main_Center-Content").find(".Body").eq(0).find(".Form").eq(0).find(".Row").eq(0).find(".Select");
	for(var i=0; i<AllDeviceName.length; i++)
	{
		var ObjectKey = Object.keys(Global_AsciiModule[AllDeviceName[i]]);
		if(ObjectKey.length > 0)
		{
			Select.val(AllDeviceName[i]);
			break;
		}
	}
}

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

AsciiModuleFirstPage.prototype._Initialization_ = function()
{
	AsciiModuleFirstPage.prototype._LoadComPortName_();
	AsciiModuleFirstPage.prototype._InitializationGlobalVariable_();
	AsciiModuleFirstPage.prototype._AutomaticConfigurationComPortName_();
	AsciiModuleFirstPage.prototype._RedisplayUi_();
}

AsciiModuleFirstPage.prototype._CheckModuleName_ = function(that)
{
	var DeviceName = AsciiModuleFirstPage.prototype._GetDeviceName_();
	var Id = "#ModuleTable";
	$(Id).find(".Row").eq(0).find(".Message").hide();
	var Class = new Type();
	var Name = $(that).closest(".Column").find(".Text").val();
	if(Name == "")
	{
		$(Id).find(".Row").eq(0).find(".Message").eq(0).show();
	}
	else if(Class.CheckSpecialSymbols(Name) == true)
	{
		$(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;
}

AsciiModuleFirstPage.prototype._CopyDialogEnabled_ = function(Event)
{
	var DeviceName = AsciiModuleFirstPage.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");
}

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

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

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

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

AsciiModuleFirstPage.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;
	AsciiModuleSecondPage.prototype._SetInitialValue_(number);
	$("#MappingTable").find(".Tab").eq(1).click();
}

AsciiModuleFirstPage.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("SaveAsciiVariable", JSON.stringify(Global_AsciiVariable), Result_1);
	Class.TransmissionString("SaveAsciiModule", JSON.stringify(Global_AsciiModule), Result_2);
	Class.TransmissionString("SaveAsciiSetting", JSON.stringify(Global_AsciiSetting), Result_3);
	Class.TransmissionString("SaveAsciiIndex", JSON.stringify(Global_AsciiIndex), Result_4);
	var SetInterval = setInterval(function()
	{
		if(Result_1["Input"]==true && Result_2["Input"]==true && Result_3["Input"]==true && Result_4["Input"]==true)
		{
			clearInterval(SetInterval);
			Result["Input"] = true;
			Class = null; delete Class;
		}
	},500);
}

AsciiModuleFirstPage.prototype._SaveConvertSetting_ = 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("SaveAsciiToOpcuaVariable", JSON.stringify(Global_AsciiToOpcuaVariable), Result_1);
	Class.TransmissionString("SaveAsciiToMqttSetting", JSON.stringify(Global_AsciiToMqttSetting), Result_2);
	Class.TransmissionString("SaveAsciiToMqttVariable", JSON.stringify(Global_AsciiToMqttVariable), Result_3);
	Class.TransmissionString("SaveAsciiToMqttJsonVariable", JSON.stringify(Global_AsciiToMqttJsonVariable), Result_4);
	var SetInterval = setInterval(function()
	{
		if(Result_1["Input"]==true && Result_2["Input"]==true && Result_3["Input"]==true && Result_4["Input"]==true)
		{
			clearInterval(SetInterval);
			Result["Input"] = true;
			Class = null; delete Class;
		}
	},500);
}

AsciiModuleFirstPage.prototype._SaveAdvancedSetting_ = function(Result)
{
	var Result_1={"Input":null}, Result_2={"Input":null}, Result_3={"Input":null};
	var Class = new Transmission();
	Class.TransmissionString("SavePidOperationVariable", JSON.stringify(Global_PidOperation), Result_1);
	Class.TransmissionString("SaveIftttConditionTriggerSetting", JSON.stringify(Global_IftttConditionTriggerSetting), Result_2);
	Class.TransmissionString("SaveIftttConditionTriggerVariable", JSON.stringify(Global_IftttConditionTriggerVariable), Result_3);
	var SetInterval = setInterval(function()
	{
		if(Result_1["Input"]==true && Result_2["Input"]==true && Result_3["Input"]==true)
		{
			clearInterval(SetInterval);
			Result["Input"] = true;
			Class = null; delete Class;
		}
	},500);
}

AsciiModuleFirstPage.prototype._SaveAsciiDataLayerAndServiceLayerVariables_ = function(Result)
{
	var Result_1={"Input":null}, Result_2={"Input":null}, Result_3={"Input":null}, Result_4={"Input":null};
	AsciiModuleFirstPage.prototype._SaveModuleSetting_(Result_1);
	AsciiModuleFirstPage.prototype._SaveConvertSetting_(Result_2);
	AsciiModuleFirstPage.prototype._SaveAdvancedSetting_(Result_3);
	var SetInterval_1 = setInterval(function()
	{
		if(Result_1["Input"]==true && Result_2["Input"]==true && Result_3["Input"]==true)
		{
			clearInterval(SetInterval_1);
			Result["Input"] = true;
		}
	},500);
}


AsciiModuleFirstPage.prototype._SaveDataLayerAndServiceLayerVariables_ = function(Result)
{
	var Result_1={"Input":null}, Result_2={"Input":null};
	RtuModuleFirstPage.prototype._SaveRtuDataLayerAndServiceLayerVariables_(Result_1);
	var SetInterval_1 = setInterval(function()
	{
		if(Result_1["Input"]==true)
		{
			AsciiModuleFirstPage.prototype._SaveAsciiDataLayerAndServiceLayerVariables_(Result_2);
			clearInterval(SetInterval_1);
		}
	},500);
	var SetInterval_2 = setInterval(function()
	{
		if(Result_2["Input"]==true)
		{
			Result["Input"] = true;
			clearInterval(SetInterval_2);
		}
	},500);
}

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










//第2頁
AsciiModuleSecondPage.prototype._GetDataModelTableOldData_ = function(DataModelId)
{
	DataModelId = DataModelId +"Table";
	var Return = new Object();
	var length  = $("#"+DataModelId).find(".DynamicRow").length;
	var Address, Variable, DataType, Swap, Description;

	for(var i=0; i<length; i++)
	{
		Address = parseInt($("#"+DataModelId).find(".DynamicRow").eq(i).find(".Column").eq(0).text());
		Return[Address] = new Object();
		Variable = $("#"+DataModelId).find(".DynamicRow").eq(i).find(".Column").eq(1).find(":text").val();
		Return[Address]["VariableName"] = Variable;
		DataType= $("#"+DataModelId).find(".DynamicRow").eq(i).find(".Column").eq(2).text();
		Return[Address]["DataType"] = DataType;
		Swap    = $("#"+DataModelId).find(".DynamicRow").eq(i).find(".Column").eq(3).find(".Checkbox").prop("checked");
		Return[Address]["Swap"] = Swap;
		Description = $("#"+DataModelId).find(".DynamicRow").eq(i).find(".Column").eq(4).find(":text").val();
		Return[Address]["Description"] = Description;
	}
	return Return;
}

AsciiModuleSecondPage.prototype._DataModelTableAddNewRow_ = function(DataModelId, Data)
{
	var html="", TempHtml="";
	if(DataModelId == "CoilStatus" || DataModelId == "InputStatus")
	{
		TempHtml = $(".DioDynamic").html();
	}
	else if(DataModelId == "HoldingRegisters" || DataModelId == "InputRegisters")
	{
		TempHtml = $(".AioDynamic").html();
	}
	var ObjectKey = Object.keys(Data);
	for(var i=0; i<ObjectKey.length; i++)
	{
		html = html + TempHtml;
	}
	DataModelId = DataModelId + "Table";
	$("#"+DataModelId).append(html);
}

AsciiModuleSecondPage.prototype._SetDataModelTableNewRowValue_ = function(DataModelId, Data)
{
	DataModelId = DataModelId + "Table";
	var ObjectKey = Object.keys(Data);
	for(var i=0; i<ObjectKey.length; i++)
	{
		$("#"+DataModelId).find(".DynamicRow").eq(i).find(".Column").eq(0).text(ObjectKey[i]);
		$("#"+DataModelId).find(".DynamicRow").eq(i).find(".Column").eq(1).find(":text").val(Data[ObjectKey[i]]["VariableName"]);
		$("#"+DataModelId).find(".DynamicRow").eq(i).find(".Column").eq(2).text(Data[ObjectKey[i]]["DataType"]);
		$("#"+DataModelId).find(".DynamicRow").eq(i).find(".Column").eq(3).find(".Checkbox").prop("checked", Data[ObjectKey[i]]["Swap"]);
		$("#"+DataModelId).find(".DynamicRow").eq(i).find(".Column").eq(4).find(":text").val(Data[ObjectKey[i]]["Description"]);
	}
}

AsciiModuleSecondPage.prototype._GetTotalTableOldData_ = function(DataModelId)
{
	var object = new Object(), StartAddress, DataNumber, DataType;
	var TotalTableId = DataModelId + "TotalTable";
	var Element = $("#"+TotalTableId).find(".DynamicRow");
	var Length = Element.length;
	for(var i=0; i<Length; i++)
	{
		StartAddress=parseInt(Element.eq(i).find(".Row").eq(0).find("label").text());
		DataNumber=Element.eq(i).find(".Row").eq(1).find("label").text();
		DataType=Element.eq(i).find(".Row").eq(2).find("label").text();
		object[StartAddress]=new Object();
		object[StartAddress]["DataNumber"] = DataNumber;
		object[StartAddress]["DataType"] = DataType;
	}
	return object;
}

AsciiModuleSecondPage.prototype._TotalTableAddNewRow_ = function(DataModelId, Data)
{
	var html="", TempHtml="", TotalTableId = DataModelId + "TotalTable";
	if(DataModelId == "CoilStatus" || DataModelId == "InputStatus")
	{
		TempHtml = $(".TotalTableDynamic").html();
	}
	else if(DataModelId == "HoldingRegisters" || DataModelId == "InputRegisters")
	{
		TempHtml = $(".TotalTableDynamic").html();
	}
	var ObjectKey = Object.keys(Data);
	for(var i=0; i<ObjectKey.length; i++)
	{
		html = html + TempHtml;
	}
	$("#"+TotalTableId).append(html);
}

AsciiModuleSecondPage.prototype._AddTotalTableData_ = function(StartAddress, DataNumber, DataType, Data)
{
	if(typeof(Data[StartAddress]) == "undefined"){
		Data[StartAddress] = new Object();
		Data[StartAddress]["DataNumber"] = DataNumber;
		Data[StartAddress]["DataType"] = DataType;
	}
	return Data;
}

AsciiModuleSecondPage.prototype._SetTotalTableNewRowValue_ = function(DataModelId, Data)
{
	var TotalTableId = DataModelId + "TotalTable";
	var Element = $("#"+TotalTableId).find(".DynamicRow");
	var ObjectKey = Object.keys(Data);
	for(var i=0; i<ObjectKey.length; i++)
	{
		Element.eq(i).find(".Row").eq(0).find("label").text(ObjectKey[i]);
		Element.eq(i).find(".Row").eq(0).find(":text").val(ObjectKey[i]);
		Element.eq(i).find(".Row").eq(1).find("label").text(Data[ObjectKey[i]]["DataNumber"]);
		Element.eq(i).find(".Row").eq(1).find(":text").val(Data[ObjectKey[i]]["DataNumber"]);
		Element.eq(i).find(".Row").eq(2).find("label").text(Data[ObjectKey[i]]["DataType"]);
	}
}

AsciiModuleSecondPage.prototype._GetItemTableValue_ = function(that)
{
	var Return = new Object();
	var Element = $(that).closest(".DynamicRow").find(".Row");
	var LabelStartAddress = parseInt($(that).closest(".DynamicRow").find(".Row").eq(0).find("label").text());
	var TextStartAddress = parseInt($(that).closest(".DynamicRow").find(".Row").eq(0).find(":text").val());
	var LabelNumber = parseInt($(that).closest(".DynamicRow").find(".Row").eq(1).find("label").text());
	var TextNumber = parseInt($(that).closest(".DynamicRow").find(".Row").eq(1).find(":text").val());
	var DataType = $(that).closest(".DynamicRow").find(".Row").eq(2).find("label").text();
	var DataModel = $(that).closest(".Table").attr("id").replace("TotalTable", "")
	Return["DataModel"] = DataModel;
	Return["LabelStartAddress"] = LabelStartAddress;
	Return["TextStartAddress"] = TextStartAddress;
	Return["LabelNumber"] = LabelNumber;
	Return["TextNumber"] = TextNumber;
	Return["DataType"] = DataType;
	return Return;
}

AsciiModuleSecondPage.prototype._GetSettingFormValue_ = function()
{
	var DataModel, StartAddress, DataNumber, DataType, object=new Object();
	object["DataModel"] = $("#SettingForm").find(".Row").eq(0).find(".Select").val();
	object["StartAddress"] = parseInt($("#SettingForm").find(".Row").eq(1).find(".Text").val());
	object["DataNumber"] = parseInt($("#SettingForm").find(".Row").eq(2).find(".Text").val());
	if(object["DataModel"] == "CoilStatus" || object["DataModel"] == "InputStatus"){
		object["DataType"] = "Bool";
	}
	else{
		object["DataType"] = $("#SettingForm").find(".Row").eq(3).find(".Select").val();
	}
	return object;
}

AsciiModuleSecondPage.prototype._GetGridNumber_ = function(DataType)
{
	var Grid=0;
	if(DataType == "Bool" || DataType == "Short" || DataType == "Unsigned Short")	{Grid = 1;}
	else if(DataType == "Long" ||DataType == "Unsigned Long" ||DataType == "Float")	{Grid = 2;}
	else if(DataType == "Double")													{Grid = 4;}
	return Grid;
}

AsciiModuleSecondPage.prototype._DeleteDataModelTableData_ = function(Data, DeleteAddress)
{
	for(var i=0; i<DeleteAddress.length; i++)
	{
		Data[DeleteAddress[i]] = null;
		delete Data[DeleteAddress[i]];
	}
	return Data;
}

AsciiModuleSecondPage.prototype._AddDataModelTableData_ = function(Data, AddAddress, DataType)
{
	for(var i=0; i<AddAddress.length; i++)
	{
		Data[AddAddress[i]] = AddAddress[i];
		Data[AddAddress[i]] = new Object();
		Data[AddAddress[i]]["VariableName"] = "Tag"+AddAddress[i];
		Data[AddAddress[i]]["DataType"] = DataType;
		Data[AddAddress[i]]["Swap"] = false;
		Data[AddAddress[i]]["Description"] = "";
	}
	return Data;
}

AsciiModuleSecondPage.prototype._GenerateNeedAddAddress_ = function(StartAddress, DataNumber, DataType)
{
	var Return=new Array();
	var Grid = AsciiModuleSecondPage.prototype._GetGridNumber_(DataType);
	for(var i=0; i<DataNumber; i++)
	{
		Return.push(StartAddress+Grid*i);
	}
	return Return;
}

AsciiModuleSecondPage.prototype._GenerateNeedDeleteAddress_ = function(StartAddress, DataNumber, DataType)
{
	var Return=new Array();
	var Grid = AsciiModuleSecondPage.prototype._GetGridNumber_(DataType);
	for(var i=0; i<DataNumber; i++)
	{
		Return.push(StartAddress+Grid*i);
	}
	return Return;
}

AsciiModuleSecondPage.prototype._CompareDataModelTableNeedDeleteAddress_ = function(that)
{
	var Return=new Array(), LabelArray=new Array(), TextArray=new Array();
	var obj = AsciiModuleSecondPage.prototype._GetItemTableValue_(that);
	var Grid = AsciiModuleSecondPage.prototype._GetGridNumber_(obj["DataType"]);
	
	for(var i=0; i<obj["LabelNumber"]; i++){
		LabelArray.push(obj["LabelStartAddress"]);
		obj["LabelStartAddress"] = obj["LabelStartAddress"]+Grid;
	}
	for(var i=0; i<obj["TextNumber"]; i++){
		TextArray.push(obj["TextStartAddress"]);
		obj["TextStartAddress"] = obj["TextStartAddress"]+Grid;
	}
	Return = AsciiModuleSecondPage.prototype._ArrayComplement_(LabelArray, TextArray);
	return Return;
}

AsciiModuleSecondPage.prototype._CompareDataModelTableNeedAddAddress_ = function(that)
{
	var Return=new Array(), LabelArray=new Array(), TextArray=new Array();
	var obj = AsciiModuleSecondPage.prototype._GetItemTableValue_(that);
	var Grid = AsciiModuleSecondPage.prototype._GetGridNumber_(obj["DataType"]);
	for(var i=0; i<obj["LabelNumber"]; i++){
		LabelArray.push(obj["LabelStartAddress"]);
		obj["LabelStartAddress"] = obj["LabelStartAddress"]+Grid;
	}
	for(var i=0; i<obj["TextNumber"]; i++){
		TextArray.push(obj["TextStartAddress"]);
		obj["TextStartAddress"] = obj["TextStartAddress"]+Grid;
	}
	Return = AsciiModuleSecondPage.prototype._ArrayComplement_(TextArray, LabelArray);
	return Return;
}

AsciiModuleSecondPage.prototype._ItemTableCss_ = function(Mode, that)
{
	if(Mode=="Edit"){
		$(that).closest(".DynamicRow").find(".Row").eq(0).find("label").hide();
		$(that).closest(".DynamicRow").find(".Row").eq(0).find(":text").show();
		$(that).closest(".DynamicRow").find(".Row").eq(1).find("label").hide();	
		$(that).closest(".DynamicRow").find(".Row").eq(1).find(":text").show();
		$(that).closest(".DynamicRow").find(".Row").eq(3).find(":button").show();
		$(that).closest(".DynamicRow").find(".Row").eq(3).find(":button").show();
		$(that).closest(".DynamicRow").find(".Row").eq(4).show();
		$(that).closest(".DynamicRow").find(".Message").hide();
		$(that).closest(".DynamicRow").find(".Message").eq(0).show();
		$(that).hide();
	}
	else if(Mode=="Save" || Mode=="Cancel"){
		$(that).closest(".DynamicRow").find(".Row").eq(0).find("label").show();
		$(that).closest(".DynamicRow").find(".Row").eq(0).find(":text").hide();
		$(that).closest(".DynamicRow").find(".Row").eq(1).find("label").show();
		$(that).closest(".DynamicRow").find(".Row").eq(1).find(":text").hide();
		$(that).closest(".DynamicRow").find(".Row").eq(3).find(":button").hide();
		$(that).closest(".DynamicRow").find(".Row").eq(3).find(":button").hide();
		$(that).closest(".DynamicRow").find(".Row").eq(4).hide();
		$(that).closest(".DynamicRow").find(".Message").hide();
		$(that).closest(".DynamicRow").find(".Row").eq(3).find(":button").eq(1).show();
	}
}

AsciiModuleSecondPage.prototype._OkButton_ = function(that)
{
	var Obj=AsciiModuleSecondPage.prototype._GetItemTableValue_(that);
	var Bool = AsciiModuleSecondPage.prototype._CheckZoomAddressCollision_(that);
	if(Bool == true){
		$(that).closest(".DynamicRow").find(".Message").hide();
		$(that).closest(".DynamicRow").find(".Message").eq(1).show();
		return 0;
	}
	var DataModelId=$(that).closest(".Table").attr("id").replace("TotalTable", "");
	var Data=AsciiModuleSecondPage.prototype._GetDataModelTableOldData_(DataModelId);
	$("#"+DataModelId+"Table").find(".DynamicRow").remove();
	
	var DeleteAddress = AsciiModuleSecondPage.prototype._CompareDataModelTableNeedDeleteAddress_(that);
	var AddAddress = AsciiModuleSecondPage.prototype._CompareDataModelTableNeedAddAddress_(that);
	Data = AsciiModuleSecondPage.prototype._DeleteDataModelTableData_(Data, DeleteAddress);
	Data = AsciiModuleSecondPage.prototype._AddDataModelTableData_(Data, AddAddress, Obj["DataType"]);

	AsciiModuleSecondPage.prototype._DataModelTableAddNewRow_(DataModelId, Data);
	AsciiModuleSecondPage.prototype._SetDataModelTableNewRowValue_(DataModelId, Data);

	var Address=parseInt($(that).closest(".DynamicRow").find(".Row").eq(0).find(":text").val());
	$(that).closest(".DynamicRow").find(".Row").eq(0).find("label").text(Address);
	var number=parseInt($(that).closest(".DynamicRow").find(".Row").eq(1).find(":text").val());
	$(that).closest(".DynamicRow").find(".Row").eq(1).find("label").text(number);
	
	AsciiModuleSecondPage.prototype._ItemTableCss_("Save", that);
}

AsciiModuleSecondPage.prototype._CancelButton_ = function(that)
{	
	var StartAddress = parseInt($(that).closest(".DynamicRow").find(".Row").eq(0).find("label").text());
	$(that).closest(".DynamicRow").find(".Row").eq(0).find(":text").val(StartAddress);

	var number = parseInt($(that).closest(".DynamicRow").find(".Row").eq(1).find("label").text());
	$(that).closest(".DynamicRow").find(".Row").eq(1).find(":text").val(number);
	
	AsciiModuleSecondPage.prototype._ItemTableCss_("Cancel", that);
}

AsciiModuleSecondPage.prototype._EditButton_ = function(that)
{
	AsciiModuleSecondPage.prototype._ItemTableCss_("Edit", that);
}
//快速刪除
AsciiModuleSecondPage.prototype._DeleteButton_ = function(that)
{
	var StartAddress = parseInt($(that).closest(".DynamicRow").find(".Row").eq(0).find("label").text());
	var number = parseInt($(that).closest(".DynamicRow").find(".Row").eq(1).find("label").text());
	var Address,Index;
	var TableId = $(that).closest(".Table").attr("id");
	TableId = TableId.replace("TotalTable", "") + "Table";
	var element = $("#"+TableId).find(".DynamicRow");
	for(var i=0; i<element.length; i++)
	{
		Address = parseInt(element.eq(i).find(".Column").eq(0).text());
		if(StartAddress == Address)
		{
			Index = i;
			break;
		}
	}
	for(var i=0; i<number; i++)
	{
		element.eq(Index+i).remove();
	}
	$(that).closest(".DynamicRow").remove();
}

AsciiModuleSecondPage.prototype._CreateTable_ = function(DataModelId, that)
{
	var Obj=AsciiModuleSecondPage.prototype._GetSettingFormValue_();
	
	var Data = AsciiModuleSecondPage.prototype._GetTotalTableOldData_(DataModelId);
	$("#"+DataModelId+"TotalTable").find(".DynamicRow").remove();
	Data = AsciiModuleSecondPage.prototype._AddTotalTableData_(Obj["StartAddress"], Obj["DataNumber"], Obj["DataType"], Data);
	AsciiModuleSecondPage.prototype._TotalTableAddNewRow_(DataModelId, Data);
	AsciiModuleSecondPage.prototype._SetTotalTableNewRowValue_(DataModelId, Data);

	Data = AsciiModuleSecondPage.prototype._GetDataModelTableOldData_(DataModelId);
	$("#"+DataModelId+"Table").find(".DynamicRow").remove();
	var InsertAddress=AsciiModuleSecondPage.prototype._GenerateNeedAddAddress_(Obj["StartAddress"], Obj["DataNumber"], Obj["DataType"]);
	Data = AsciiModuleSecondPage.prototype._AddDataModelTableData_(Data, InsertAddress, Obj["DataType"]);
	AsciiModuleSecondPage.prototype._DataModelTableAddNewRow_(DataModelId, Data);
	AsciiModuleSecondPage.prototype._SetDataModelTableNewRowValue_(DataModelId, Data);
	
	$(that).closest(".Row").find(".Message").eq(0).show();
	$(that).closest(".Row").find(".Message").eq(1).hide();
}

AsciiModuleSecondPage.prototype._ProduceTable_ = function(that)
{
	var Obj=AsciiModuleSecondPage.prototype._GetSettingFormValue_();
	var Bool = AsciiModuleSecondPage.prototype._CheckAddressCollision_(Obj["DataModel"], Obj["StartAddress"], Obj["DataNumber"], Obj["DataType"]);
	if(Bool == true){
		$(that).closest(".Row").find(".Message").hide();
		$(that).closest(".Row").find(".Message").eq(2).show();
		return 0;
	}
	$(that).closest(".Row").find(".Message").hide();
	$(that).closest(".Row").find(".Message").eq(1).show();
	
	
	var DataModelId = $(that).closest(".Form").find(".Row").eq(0).find(".Select").val();

	setTimeout(function(){
		AsciiModuleSecondPage.prototype._CreateTable_(DataModelId, that);
	}, 500);
}

AsciiModuleSecondPage.prototype._ClearAllCss_ = function(that)
{
	$(that).parent("div").find(".Tab").css("border-right-color", "#FFFFFF");
	$(that).parent("div").find(".Tab").css("border-left-color", "#FFFFFF");
	$(that).parent("div").find(".Tab").css("border-top-color", "#FFFFFF");
	$(that).parent("div").find(".Tab").css("background-color", "#FFFFFF");
}

AsciiModuleSecondPage.prototype._ChangeCurrentlyTabCss_ = function(that)
{
	$(that).css("border-right-color", "#395d8b");
	$(that).css("border-left-color", "#395d8b");
	$(that).css("border-top-color", "#395d8b");
	$(that).css("background-color", "#E5F1FF");
}

AsciiModuleSecondPage.prototype._HideAllTotalTable_ = function()
{
	$("#CoilStatusTotalTable").css("display", "none");
	$("#InputStatusTotalTable").css("display", "none");
	$("#HoldingRegistersTotalTable").css("display", "none");
	$("#InputRegistersTotalTable").css("display", "none");
}

AsciiModuleSecondPage.prototype._ShowAllTotalTable_ = function()
{
	$("#CoilStatusTotalTable").css("display", "table");
	$("#InputStatusTotalTable").css("display", "table");
	$("#HoldingRegistersTotalTable").css("display", "table");
	$("#InputRegistersTotalTable").css("display", "table");
}

AsciiModuleSecondPage.prototype._HideAllDataModelForm_ = function()
{
	$("#CoilStatusFormTitle").css("display", "none");
	$("#CoilStatusForm").css("display", "none");
	$("#CoilStatusTable").css("display", "none");
	
	$("#InputStatusFormTitle").css("display", "none");
	$("#InputStatusForm").css("display", "none");
	$("#InputStatusTable").css("display", "none");
	
	$("#HoldingRegistersFormTitle").css("display", "none");
	$("#HoldingRegistersForm").css("display", "none");
	$("#HoldingRegistersTable").css("display", "none");
	
	$("#InputRegistersFormTitle").css("display", "none");
	$("#InputRegistersForm").css("display", "none");
	$("#InputRegistersTable").css("display", "none");
}

AsciiModuleSecondPage.prototype._ShowAllDataModelForm_ = function()
{	
	$("#CoilStatusFormTitle").css("display", "table");
	$("#CoilStatusForm").css("display", "table");
	$("#CoilStatusTable").css("display", "table");
	
	$("#InputStatusFormTitle").css("display", "table");
	$("#InputStatusForm").css("display", "table");
	$("#InputStatusTable").css("display", "table");
	
	$("#HoldingRegistersFormTitle").css("display", "table");
	$("#HoldingRegistersForm").css("display", "table");
	$("#HoldingRegistersTable").css("display", "table");
	
	$("#InputRegistersFormTitle").css("display", "table");
	$("#InputRegistersForm").css("display", "table");
	$("#InputRegistersTable").css("display", "table");
}

AsciiModuleSecondPage.prototype._CheckDataModelTable_ = function()
{
	var Return=true;
	if($("#CoilStatusTable").find(".Message").is(":visible") == true ||
	   $("#InputStatusTable").find(".Message").is(":visible") == true ||
	   $("#HoldingRegistersTable").find(".Message").is(":visible") == true ||
	   $("#InputRegistersTable").find(".Message").is(":visible") == true )
	{
		Return=false;
	}
	return Return;
}

AsciiModuleSecondPage.prototype._CheckTotalTable_ = function()
{
	var Return=true;
	if($("#CoilStatusTotalTable").find(".Message").is(":visible") == true ||
	   $("#InputStatusTotalTable").find(".Message").is(":visible") == true ||
	   $("#HoldingRegistersTotalTable").find(".Message").is(":visible") == true ||
	   $("#InputRegistersTotalTable").find(".Message").is(":visible") == true )
	{
		Return=false;
	}
	return Return;
}

AsciiModuleSecondPage.prototype._ChangeModbusTab_ = function(Mode, that)
{
	if(Mode=="AddressTable"){
		if(AsciiModuleSecondPage.prototype._CheckDataModelTable_() == false){
			return 0;
		}
	}
	else if(Mode=="NameSetting"){
		if(AsciiModuleSecondPage.prototype._CheckTotalTable_() == false){
			return 0;
		}
	}
	AsciiModuleSecondPage.prototype._ClearAllCss_(that);
	AsciiModuleSecondPage.prototype._ChangeCurrentlyTabCss_(that);
	if(Mode=="AddressTable"){
		AsciiModuleSecondPage.prototype._HideAllDataModelForm_();
		AsciiModuleSecondPage.prototype._ShowAllTotalTable_();
	}
	else if(Mode=="NameSetting"){
		AsciiModuleSecondPage.prototype._ShowAllDataModelForm_();
		AsciiModuleSecondPage.prototype._HideAllTotalTable_();
	}
}

AsciiModuleSecondPage.prototype._SelectDataModelCombobox_ = function()
{
	var value = $("#SettingForm").find(".Row").eq(0).find(".Select").val();
	if(value=="CoilStatus" || value=="InputStatus")
	{
		$("#SettingForm").find(".Row").eq(3).hide();
	}
	else if(value=="HoldingRegisters" || value=="InputRegisters")
	{
		$("#SettingForm").find(".Row").eq(3).show();
	}
}

AsciiModuleSecondPage.prototype._InitialSettingFormValue_ = function()
{
	$("#SettingForm").find(".Row").eq(1).find(".Text").val(0);
	$("#SettingForm").find(".Row").eq(2).find(".Text").val(1);
}

AsciiModuleSecondPage.prototype._CheckZoomAddressCollision_ = function(that)
{
	var Return=false, Grid, StartAddress, number, DataType, MaxAddress, Address=new Array(), ExistAddress=new Array();
	var Obj=AsciiModuleSecondPage.prototype._GetItemTableValue_(that);
	StartAddress=parseInt(Obj["TextStartAddress"]);
	Grid = AsciiModuleSecondPage.prototype._GetGridNumber_(Obj["DataType"]);
	MaxAddress=StartAddress+Grid*parseInt(Obj["TextNumber"]);
	if(MaxAddress > 65536){
		return true;
	}
	for(var i=StartAddress; i<MaxAddress; i++){
		Address.push(i);
	}
	ExistAddress=AsciiModuleSecondPage.prototype._GetExistAddress_(that);
	if(AsciiModuleSecondPage.prototype._ArrayInteraction_(Address, ExistAddress).length >0){
		Return=true;
	}
	return Return;
}

AsciiModuleSecondPage.prototype._CheckAddressCollision_ = function(DataModel, StartAddress, DataNumber, DataType)
{
	var Return=false, Address=new Array();
	var Grid = AsciiModuleSecondPage.prototype._GetGridNumber_(DataType);
	var MaxAddress=StartAddress+Grid*parseInt(DataNumber);
	if(MaxAddress > 65536){
		return true;
	}
	for(var i=StartAddress; i<MaxAddress; i++){
		Address.push(i);
	}
	var ExistAddress=new Array();
	var element = $("#"+DataModel+"Table").find(".DynamicRow");
	for(var i=0; i<element.length; i++){
		var address = parseInt(element.eq(i).find(".Column").eq(0).text());
		var dataType = element.eq(i).find(".Column").eq(2).text();
		Grid=AsciiModuleSecondPage.prototype._GetGridNumber_(dataType);
		for(var j=0; j<Grid; j++)
		{
			ExistAddress.push(address);
			address++;
		}
	}
	if(AsciiModuleSecondPage.prototype._ArrayInteraction_(Address, ExistAddress).length >0){
		Return=true;
	}
	return Return;
}

AsciiModuleSecondPage.prototype._CheckStartAddress_ = function(that)
{
	var Class = new Type();
	var StatAddress = $(that).val();
	if(Class.CheckPositiveIntegersAndZero(StatAddress) == false){
		$(that).val(0);
		Class = null;
		return 0;
	}
	if(!(StatAddress >=0 && StatAddress <=65535)){
		$(that).val(0);
		Class = null;
		return 0;
	}
	StatAddress = parseInt(StatAddress);
	$(that).val(StatAddress);
	Class = null;
}

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

AsciiModuleSecondPage.prototype._CheckPollingTimeout_ = function(that)
{
	var Class = new Type();
	var PollingTimeout = $(that).val();
	PollingTimeout = parseInt(PollingTimeout);
	if(Class.CheckPositiveIntegers(PollingTimeout) == false || 
	   PollingTimeout < 20){
		$(that).val(20);
		Class = null;
		return 0;
	}
	$(that).val(PollingTimeout);
	Class = null;
}

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

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

AsciiModuleSecondPage.prototype._CheckSlaveId_ = function(that)
{
	var SlaveId = $(that).closest(".Row").find(".Text").val();
 	var number = parseInt($(that).closest(".Form").find(".Row").eq(0).find(".Text").val());
 	$(that).closest(".Row").find(".Message").hide();

 	var Class = new Type();
 	if(Class.CheckPositiveIntegers(SlaveId) == false ||
       !(parseInt(SlaveId)>0 && parseInt(SlaveId)<255))
	{
		$(that).closest(".Row").find(".Message").eq(0).show();
		$(that).closest(".Row").find(".Text").val(SlaveId);
  		Class = null;  return 0;
 	}
 	Class = null;
 	var DeviceName;
	var AllDeviceName = Object.keys(Global_AsciiSetting);
 	for(var i=0; i<AllDeviceName.length; i++)
 	{
		if(typeof(Global_AsciiModule[AllDeviceName[i]]) == "object" && 
		   typeof(Global_AsciiModule[AllDeviceName[i]][number]) == "object" )
  		{
			DeviceName = AllDeviceName[i];
   			break;
		}
 	}

 	var ObjKey = AsciiModuleFirstPage.prototype._GetGlobal_AsciiModuleObjectKey_(DeviceName);
 	for(var i=0; i<ObjKey.length; i++)
 	{
		if(Global_AsciiSetting[ObjKey[i]]["SlaveId"] == SlaveId && number != ObjKey[i])
		{
			$(that).closest(".Row").find(".Message").eq(1).show();
			$(that).closest(".Row").find(".Text").val(SlaveId);
			break;
		}
	}
}

AsciiModuleSecondPage.prototype._GetTableVariableValue_ = function(that)
{
	var Length, Element, AllName = new Array();
	Element =$(that).closest(".Table").find(".DynamicRow");
	Length = Element.length;
	for(var i=0; i<Length; i++)
	{
		AllName.push(Element.eq(i).find(".Text").val());
	}
	AllName = AllName.sort(function (a, b) {return a - b});
	return AllName;
}

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

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

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

AsciiModuleSecondPage.prototype._ArrayComplement_ = 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;
}

AsciiModuleSecondPage.prototype._ClearForm_ = function()
{
	$("#SettingForm").find(".Row").eq(0).find(".Select").val("CoilStatus");
	$("#SettingForm").find(".Row").eq(1).find(".Text").val(0);
	$("#SettingForm").find(".Row").eq(2).find(".Text").val(1);
	$("#SettingForm").find(".Row").eq(3).find(".Select").val("Short");
	$("#SettingForm").find(".Row").eq(3).hide();
	$("#SettingForm").find(".Message").hide();
}

AsciiModuleSecondPage.prototype._ClearDataModelTable_ = function()
{
	var array = new Array("InputRegistersTable","HoldingRegistersTable","InputStatusTable","CoilStatusTable");
	for(var i=0; i<array.length; i++)
	{
		$("#"+array[i]).find(".DynamicRow").remove();
	}
}

AsciiModuleSecondPage.prototype._ClearTotalTable_ = function()
{
	var array = new Array("CoilStatusTotalTable","InputStatusTotalTable","HoldingRegistersTotalTable","InputRegistersTotalTable");
	for(var i=0; i<array.length; i++)
	{
		$("#"+array[i]).find(".DynamicRow").remove();
	}
}

AsciiModuleSecondPage.prototype._ConvertedIntoTotalTableUiData_ = function(Data)
{
	var Temp=new Array(), StartAddress, DataCount, Return=new Object();
	var Address = Object.keys(Data);
	for(var i=0; i<Address.length; i++)
	{
		Temp.push(parseInt(Address[i]));
		if(i==Address.length-1 || Data[Address[i]]["ModbusGroupNumber"] != Data[Address[i+1]]["ModbusGroupNumber"])
		{
			StartAddress = Math.min.apply(Math,Temp);
			DataCount = Temp.length;
			DataType = Data[Address[i]]["DataType"];
			Return = AsciiModuleSecondPage.prototype._AddTotalTableData_(StartAddress, DataCount, DataType, Return);
			Temp=new Array();
		}
	}
	return Return;
}

AsciiModuleSecondPage.prototype._GetTotalTableNeedData_ = function(number)
{
	var Return=new Object(); Return["CoilStatus"]=new Object(); Return["InputStatus"]=new Object(); Return["HoldingRegisters"]=new Object(); Return["InputRegisters"]=new Object();
	var SystemVariable = Object.keys(Global_AsciiVariable[number]);
	var object, Model, Address, DataType;
	for(var i=0; i<SystemVariable.length; i++)
	{
		Address = parseInt(Global_AsciiVariable[number][SystemVariable[i]]["Address"]);
		object=new Object();
		object[Address] = new Object();
		object[Address]["DataType"] = SystemVariable[i].split("-")[1];
		object[Address]["ModbusGroupNumber"] = parseInt(Global_AsciiVariable[number][SystemVariable[i]]["ModbusGroupNumber"]);
		
		if(SystemVariable[i].split("-")[0] == "DO")			{Model="CoilStatus";}
		else if(SystemVariable[i].split("-")[0] == "DI")	{Model="InputStatus";}
		else if(SystemVariable[i].split("-")[0] == "AO")	{Model="HoldingRegisters";}
		else if(SystemVariable[i].split("-")[0] == "AI")	{Model="InputRegisters";}
		Return[Model][Address] = new Object();
		Return[Model][Address] = object[Address];
	}
	return Return;
}

AsciiModuleSecondPage.prototype._GetDataModelTableUiData_ = function(number)
{
	var DataModel=new Object(); DataModel["CoilStatus"]=new Object(); DataModel["InputStatus"]=new Object(); DataModel["HoldingRegisters"]=new Object(); DataModel["InputRegisters"]=new Object();
	var Model, object, SystemVariable = Object.keys(Global_AsciiVariable[number]);
	for(var i=0; i<SystemVariable.length; i++)
	{
		var Address = parseInt(Global_AsciiVariable[number][SystemVariable[i]]["Address"]);
		object=new Object();
		object[Address] = new Object();
		object[Address]["VariableName"] = Global_AsciiVariable[number][SystemVariable[i]]["VariableName"];
		object[Address]["DataType"] = SystemVariable[i].split("-")[1];
		object[Address]["Swap"] = Global_AsciiVariable[number][SystemVariable[i]]["Swap"];
		object[Address]["Description"] = Global_AsciiVariable[number][SystemVariable[i]]["Description"];

		if(SystemVariable[i].split("-")[0] == "DO")		{Model="CoilStatus";}
		else if(SystemVariable[i].split("-")[0] == "DI")	{Model="InputStatus";}
		else if(SystemVariable[i].split("-")[0] == "AO")	{Model="HoldingRegisters";}
		else if(SystemVariable[i].split("-")[0] == "AI")	{Model="InputRegisters";}
		DataModel[Model][Address] = new Object();
		DataModel[Model][Address] = object[Address];
	}
	return DataModel;
}

AsciiModuleSecondPage.prototype._SetFormInitialValue_ = function(number)
{
	var DeviceName=AsciiModuleFirstPage.prototype._GetDeviceName_();
	var ContentSettings = $(".Form").eq(1).find(".Row");
	ContentSettings.eq(0).find(".Text").val(number);
	ContentSettings.eq(1).find(".Text").val(Global_AsciiModule[DeviceName][number]["ModuleName"]);
	ContentSettings.eq(2).find(".Text").val(Global_AsciiSetting[number]["SlaveId"]);
	ContentSettings.eq(3).find(".Text").val(Global_AsciiSetting[number]["Timeout"]);
}

AsciiModuleSecondPage.prototype._SetDataModelTableInitialValue_ = function(number)
{
	var Data = AsciiModuleSecondPage.prototype._GetDataModelTableUiData_(number);
	AsciiModuleSecondPage.prototype._DataModelTableAddNewRow_("CoilStatus", Data["CoilStatus"]);
	AsciiModuleSecondPage.prototype._SetDataModelTableNewRowValue_("CoilStatus", Data["CoilStatus"]);
	AsciiModuleSecondPage.prototype._DataModelTableAddNewRow_("InputStatus", Data["InputStatus"]);
	AsciiModuleSecondPage.prototype._SetDataModelTableNewRowValue_("InputStatus", Data["InputStatus"]);
	AsciiModuleSecondPage.prototype._DataModelTableAddNewRow_("HoldingRegisters", Data["HoldingRegisters"]);
	AsciiModuleSecondPage.prototype._SetDataModelTableNewRowValue_("HoldingRegisters", Data["HoldingRegisters"]);
	AsciiModuleSecondPage.prototype._DataModelTableAddNewRow_("InputRegisters", Data["InputRegisters"]);
	AsciiModuleSecondPage.prototype._SetDataModelTableNewRowValue_("InputRegisters", Data["InputRegisters"]);
}

AsciiModuleSecondPage.prototype._SetTotalTableInitialValue_ = function(number)
{	
	var Data=AsciiModuleSecondPage.prototype._GetTotalTableNeedData_(number);
	var data = AsciiModuleSecondPage.prototype._ConvertedIntoTotalTableUiData_(Data["CoilStatus"]);
	AsciiModuleSecondPage.prototype._TotalTableAddNewRow_("CoilStatus", data);
	AsciiModuleSecondPage.prototype._SetTotalTableNewRowValue_("CoilStatus", data);
	
	data = AsciiModuleSecondPage.prototype._ConvertedIntoTotalTableUiData_(Data["InputStatus"]);
	AsciiModuleSecondPage.prototype._TotalTableAddNewRow_("InputStatus", data);
	AsciiModuleSecondPage.prototype._SetTotalTableNewRowValue_("InputStatus", data);
	
	data = AsciiModuleSecondPage.prototype._ConvertedIntoTotalTableUiData_(Data["HoldingRegisters"]);
	AsciiModuleSecondPage.prototype._TotalTableAddNewRow_("HoldingRegisters", data);
	AsciiModuleSecondPage.prototype._SetTotalTableNewRowValue_("HoldingRegisters", data);

	data = AsciiModuleSecondPage.prototype._ConvertedIntoTotalTableUiData_(Data["InputRegisters"]);
	AsciiModuleSecondPage.prototype._TotalTableAddNewRow_("InputRegisters", data);
	AsciiModuleSecondPage.prototype._SetTotalTableNewRowValue_("InputRegisters", data);
}

AsciiModuleSecondPage.prototype._SetInitialValue_ = function(number)
{
	AsciiModuleSecondPage.prototype._ClearForm_();
	AsciiModuleSecondPage.prototype._ClearDataModelTable_();
	AsciiModuleSecondPage.prototype._ClearTotalTable_();
	var ContentSettings = $(".Form").eq(1).find(".Row");
	ContentSettings.eq(0).find(".Text").val(number);
	if(typeof(Global_AsciiSetting[number]) == "undefined"){
		ContentSettings.eq(1).find(".Text").val(number);
		return 0;
	}
	AsciiModuleSecondPage.prototype._SetFormInitialValue_(number);
	if(typeof(Global_AsciiVariable[number]) == "undefined"){
		return 0;
	}
	AsciiModuleSecondPage.prototype._SetDataModelTableInitialValue_(number);
	AsciiModuleSecondPage.prototype._SetTotalTableInitialValue_(number);
}

AsciiModuleSecondPage.prototype._CreateVariableEmptyObject_ = function(number)
{
	var Return=new Object();
	var object=jQuery.parseJSON(JSON.stringify(Global_AsciiVariable[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;
}

AsciiModuleSecondPage.prototype._InitialAsciiToOpcuaEmptyObject_ = 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;
}

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

AsciiModuleSecondPage.prototype._InitialAsciiToMqttVariableEmptyObject_ = function(object, number)
{
	var AllDeviceName=GetValue.prototype._GetModuleAllDeviceName_(Global_AsciiModule);
	//var AllDeviceName=AsciiModuleFirstPage.prototype._GetAllDeviceName_();
	for(var i=0; i<AllDeviceName.length; i++){
		if(typeof(Global_AsciiModule[AllDeviceName[i]][number]) == "object"){
			var DeviceName = AllDeviceName[i];
			break;
		}
	}
	object = jQuery.parseJSON(JSON.stringify(object));
	var SystemVariable = Object.keys(object[number]);
	for(var i=0; i<SystemVariable.length; i++)
	{
		var IO = SystemVariable[i].split("-")[0];
		var Variable = Global_AsciiVariable[number][SystemVariable[i]]["VariableName"];
		var ModuleName = Global_AsciiModule[DeviceName][number]["ModuleName"];
		if(IO == "DO" ||IO == "AO"){
			object[number][SystemVariable[i]]["SubscribeTopic"]="/MASCII_No."+number+"_"+ModuleName+"/"+AsciiModuleSecondPage.prototype._GetDataModel_(IO)+"/"+Variable+"/Subscribe";
		}
		object[number][SystemVariable[i]]["PublishTopic"] = "/MASCII_No."+number+"_"+ModuleName+"/"+AsciiModuleSecondPage.prototype._GetDataModel_(IO)+"/"+Variable+"/Publish";
		object[number][SystemVariable[i]]["SubscribeQos"]=2;
		object[number][SystemVariable[i]]["PublishQos"]=2;
		object[number][SystemVariable[i]]["Retain"]=false;
		object[number][SystemVariable[i]]["TransformMqtt"]=false;
	}
	return object;
}

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

AsciiModuleSecondPage.prototype._InitialAsciiToMqttJsonVariableEmptyObject_ = function(object, number)
{
	var AllDeviceName=AsciiModuleFirstPage.prototype._GetAllDeviceName_();
	for(var i=0; i<AllDeviceName.length; i++){
		if(typeof(Global_AsciiModule[AllDeviceName[i]][number]) == "object"){
			var DeviceName = AllDeviceName[i];
			break;
		}
	}	
	object = jQuery.parseJSON(JSON.stringify(object));
	var SystemVariable = Object.keys(object[number]);
	for(var i=0; i<SystemVariable.length; i++)
	{
		object[number][SystemVariable[i]]["ConnectionGroup"]="";
		object[number][SystemVariable[i]]["TransformMqttJson"]=false;
		object[number][SystemVariable[i]]["Alias"]=Global_AsciiVariable[number][SystemVariable[i]]["VariableName"];
	}
	return object;
}

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

AsciiModuleSecondPage.prototype._SynchronizeOtherServiceData_ = function(number)
{
	if(typeof(Global_AsciiVariable[number]) != "object")	{return 0;}
	var OldObject=jQuery.parseJSON(JSON.stringify(Global_AsciiToOpcuaVariable));
	var object = AsciiModuleSecondPage.prototype._CreateVariableEmptyObject_(number);
	var AsciiToOpcua = AsciiModuleSecondPage.prototype._InitialAsciiToOpcuaEmptyObject_(object, number);
	AsciiModuleSecondPage.prototype._RetainAsciiToOpcuaData_(AsciiToOpcua, number);

	var AsciiToMqtt = AsciiModuleSecondPage.prototype._InitialAsciiToMqttVariableEmptyObject_(object, number);
	AsciiModuleSecondPage.prototype._RetainAsciiToMqttVariableData_(AsciiToMqtt, number);
	
	var AsciiToMqttJson = AsciiModuleSecondPage.prototype._InitialAsciiToMqttJsonVariableEmptyObject_(object, number);
	AsciiModuleSecondPage.prototype._RetainAsciiToMqttJsonVariableData_(AsciiToMqttJson, number);
	
	var Class=new SynchronizeService();
	Class.ChangeVariableSynchronizationService("MASCII", OldObject, object, number);
	Class=null;
}

AsciiModuleSecondPage.prototype._BuildAsciiSettingObject_ = function(number)
{
	var ContentSettings = $(".Form").eq(1).find(".Row");
	var SlaveId = parseInt(ContentSettings.eq(2).find(".Text").val());
	var Timeout = parseInt(ContentSettings.eq(3).find(".Text").val());
	if(typeof(Global_AsciiSetting[number]) == "undefined"){
		Global_AsciiSetting[number]=new Object();
	}
	Global_AsciiSetting[number]["SlaveId"] = SlaveId;
	Global_AsciiSetting[number]["Timeout"] = Timeout;
}

AsciiModuleSecondPage.prototype._BuildNewSystemVariableName_ = function(DataModelId, Data)
{
	var Prefix, SystemVariableName, NewSystemVariableName=new Array();
	if(DataModelId == "CoilStatus")				{Prefix="DO";}
	else if(DataModelId == "InputStatus")		{Prefix="DI";}
	else if(DataModelId == "HoldingRegisters")	{Prefix="AO";}
	else if(DataModelId == "InputRegisters")	{Prefix="AI";}
	var ObjKey=Object.keys(Data);
	for(var j=0; j<ObjKey.length; j++)
	{
		SystemVariableName = Prefix+"-"+Data[ObjKey[j]]["DataType"]+"-"+ObjKey[j];
		NewSystemVariableName.push(SystemVariableName);
	}
	return NewSystemVariableName;
}

AsciiModuleSecondPage.prototype._SetNewAsciiVariableObjectValue_ = function(DataModelId, Data, number)
{
	var Prefix, SystemVariableName;
	if(DataModelId == "CoilStatus")				{Prefix="DO";}
	else if(DataModelId == "InputStatus")		{Prefix="DI";}
	else if(DataModelId == "HoldingRegisters")	{Prefix="AO";}
	else if(DataModelId == "InputRegisters")	{Prefix="AI";}
	var ObjKey=Object.keys(Data);
	for(var j=0; j<ObjKey.length; j++)
	{
		SystemVariableName = Prefix+"-"+Data[ObjKey[j]]["DataType"]+"-"+ObjKey[j];
		if(typeof(Global_AsciiVariable[number][SystemVariableName]) == "undefined"){
			Global_AsciiVariable[number][SystemVariableName]=new Object();
		}
		Global_AsciiVariable[number][SystemVariableName]["ModbusGroupNumber"] = AsciiModuleSecondPage.prototype._GetGroupNumber_(DataModelId, ObjKey[j]);
		Global_AsciiVariable[number][SystemVariableName]["Address"] = parseInt(ObjKey[j]);
		Global_AsciiVariable[number][SystemVariableName]["VariableName"] = Data[ObjKey[j]]["VariableName"];
		Global_AsciiVariable[number][SystemVariableName]["Swap"] = Data[ObjKey[j]]["Swap"];
		Global_AsciiVariable[number][SystemVariableName]["Description"] = Data[ObjKey[j]]["Description"];
	}
}

AsciiModuleSecondPage.prototype._BuildAsciiVariableObject_ = function(number)
{
	var OldSystemVariableName = new Array();
	
	if(typeof(Global_AsciiVariable[number]) == "undefined"){
		Global_AsciiVariable[number]=new Object();
	}
	else{
		OldSystemVariableName = Object.keys(Global_AsciiVariable[number]);
	}

	var NewSystemVariableName=new Array(), Data=new Object();
	var DataModelId = new Array("CoilStatus","InputStatus","HoldingRegisters","InputRegisters");
	for(var i=0; i<DataModelId.length; i++){
		Data = AsciiModuleSecondPage.prototype._GetDataModelTableOldData_(DataModelId[i]);
		AsciiModuleSecondPage.prototype._SetNewAsciiVariableObjectValue_(DataModelId[i], Data, number);
		NewSystemVariableName = NewSystemVariableName.concat(AsciiModuleSecondPage.prototype._BuildNewSystemVariableName_(DataModelId[i], Data));
	}

	var array = AsciiModuleSecondPage.prototype._ArrayComplement_(OldSystemVariableName, NewSystemVariableName);
	for(var i=0; i<array.length; i++){
		Global_AsciiVariable[number][array[i]] = null; delete Global_AsciiVariable[number][array[i]];
	}
}

AsciiModuleSecondPage.prototype._GetGroupNumber_ = function(DataModelId, CurrentlyAddress)
{
	var Return;
	if(DataModelId == "CoilStatus")				{Return=1000000;}
	else if(DataModelId == "InputStatus")		{Return=2000000;}
	else if(DataModelId == "HoldingRegisters")	{Return=3000000;}
	else if(DataModelId == "InputRegisters")	{Return=4000000;}
	
	var Data = AsciiModuleSecondPage.prototype._GetTotalTableOldData_(DataModelId);
	var Address = Object.keys(Data);
	for(var i=0; i<Address.length; i++)
	{
		var StartAddress =parseInt( Address[i]);
		var DataNumber = parseInt(Data[Address[i]]["DataNumber"]);
		var DataType = Data[Address[i]]["DataType"];
		var MaxAddress = StartAddress + AsciiModuleSecondPage.prototype._GetGridNumber_(DataType) * DataNumber;
		if(CurrentlyAddress>=StartAddress && CurrentlyAddress<MaxAddress)
		{
			Return=Return+(i+1);
			break;
		}
	}
	return Return;
}

AsciiModuleSecondPage.prototype._GetDataModel_ = function(string)
{
	var Return;
	if(string == "DO")		{Return = "Coil_Status";}
	else if(string == "DI")	{Return = "Input_Status";}
	else if(string == "AO")	{Return = "Holding_Registers";}
	else if(string == "AI")	{Return = "Input_Registers";}
	return Return;
}

AsciiModuleSecondPage.prototype._GetExistAddress_ = function(that)
{
	var Return=new Array();
	var element=$(that).closest(".Table").find(".DynamicRow");
	var index=$(that).closest(".DynamicRow").index()-1;
	for(var i=0; i<element.length; i++)
	{
		if(i!=index)
		{
			var StartAddress=parseInt(element.eq(i).find(".Row").eq(0).find("label").text());
			var number=parseInt(element.eq(i).find(".Row").eq(1).find("label").text());
			var DataType=element.eq(i).find(".Row").eq(2).find("label").text();
			var Grid = AsciiModuleSecondPage.prototype._GetGridNumber_(DataType);
			var MaxAddress=StartAddress+Grid*number;
			for(var j=StartAddress; j<MaxAddress; j++)
			{
    			Return.push(j);
   			}
  		}
 	}
	return Return;
}

AsciiModuleSecondPage.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(1).show();
			return 0;
		}
	}
	var ContentSettings = $(".Form").eq(1).find(".Row");
	var number = parseInt(ContentSettings.eq(0).find(".Text").val());
	var ModuleName = ContentSettings.eq(1).find(".Text").val();	
	AsciiModuleFirstPage.prototype._BuildAsciiModuleObject_(ModuleName, number);
	AsciiModuleSecondPage.prototype._BuildAsciiSettingObject_(number);
	AsciiModuleSecondPage.prototype._BuildAsciiVariableObject_(number);
	AsciiModuleSecondPage.prototype._SynchronizeOtherServiceData_(number);
	$("#Main_Center-Content").find(".Body").eq(1).hide();
	$("#Main_Center-Content").find(".Body").eq(0).show();
	AsciiModuleFirstPage.prototype._RedisplayUi_();
	var Class = new Path();
	Class.LoadPathHtml($("#Left-List-ASCII").text(), 1, "main_PathAsciiModule_click(this)");
	Class = null;
}

AsciiModuleSecondPage.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-ASCII").text(), 1, "main_PathAsciiModule_click(this)");
	Class = null;
}