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

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

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

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

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






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

EipModuleFirstPage.prototype._ChannelTick_ = function(ModuleName, number)
{
	var Return;
	Return = Global_EipProductSpecification[ModuleName]["VariableName"].length;
	return Return
}

EipModuleFirstPage.prototype._CreateEipSettingObject_ = function(number)
{
	var ModuleName = $("#ModuleTable").find(".Row").eq(1).find(".Column").eq(2).find(".Select").val();
	var ModeType = Global_ConnectionParameters[ModuleName]["Mode"][0];
	Global_EipSetting[number] = new Object();
	Global_EipSetting[number]["IP"] = "0.0.0.0";
	Global_EipSetting[number]["ModeType"] = ModeType;
	Global_EipSetting[number]["NickName"]=$("#ModuleTable").find(".Row").eq(1).find(".Column").eq(2).find(".Select").val();	
}

EipModuleFirstPage.prototype._CreateEipVariableObject_ = function(ChannelTick, number)
{
	var Return;
	var Prefix, SystemVariableName;
	var VariableType, VariableAttribute;
	var ChannelSwitch;
	var DeviceName = EipModuleFirstPage.prototype._GetDeviceName_();
	var ModuleName = Global_EipModule[DeviceName][number]["ModuleName"];
	Global_EipVariable[number] = new Object();

	for (var i = 0; i < ChannelTick; i++)
	{
		VariableType = Global_EipProductSpecification[ModuleName]["VariableType"][i];
		ChannelSwitch = Global_EipProductSpecification[ModuleName]["VariableName"][i].substring(2,4);
		VariableAttribute = Global_EipProductSpecification[ModuleName]["VariableAttribute"][i];
		SystemVariableName = EipModuleSecondPage.prototype._EipGetSystemVariableName_(VariableType, VariableAttribute, ChannelSwitch);
		Global_EipVariable[number][SystemVariableName] = new Object();
		Global_EipVariable[number][SystemVariableName]["Description"] = "";
		Global_EipVariable[number][SystemVariableName]["Alias"] = "";
		Global_EipVariable[number][SystemVariableName]["VariableName"] = Global_EipProductSpecification[ModuleName]["VariableName"][i];
	}
}

EipModuleFirstPage.prototype._CreateModuleFunction_ = function(ChannelIndex, number)
{
	var DeviceName = EipModuleFirstPage.prototype._GetDeviceName_();
	Global_EipModule[DeviceName][number]["ModuleName"]=$("#ModuleTable").find(".Row").eq(1).find(".Column").eq(2).find(".Select").val();
}

EipModuleFirstPage.prototype._BuildEipModuleAllObject_ = function(that)
{
	var number = $(that).closest(".Row").find(".Column").eq(1).find(".Select").val();
	EipModuleFirstPage.prototype._DeleteRepeatNumber_(number);
	var ModuleName = $("#ModuleTable").find(".Row").eq(1).find(".Column").eq(2).find(".Select").val();
	EipModuleFirstPage.prototype._BuildEipModuleObject_(ModuleName, number);
	var DeviceName = EipModuleFirstPage.prototype._GetDeviceName_();
	var ChannelTick = EipModuleFirstPage.prototype._ChannelTick_(ModuleName, number);
	EipModuleFirstPage.prototype._CreateModuleFunction_(ChannelTick, number);
	EipModuleFirstPage.prototype._CreateEipSettingObject_(number);
	EipModuleFirstPage.prototype._CreateEipVariableObject_(ChannelTick, number);
	EipModuleSecondPage.prototype._EipToOpcuaVariableSetting_(ChannelTick, number);
	EipModuleSecondPage.prototype._EipToMqttVariableSetting_(ChannelTick, number);
}

EipModuleFirstPage.prototype._BuildEipModuleObject_ = function(ModuleName, number)
{
	var DeviceName = EipModuleFirstPage.prototype._GetDeviceName_();
	if(typeof(Global_EipModule[DeviceName][number]) == "undefined"){
		Global_EipModule[DeviceName][number] = new Object();
	}
	Global_EipModule[DeviceName][number]["ModuleName"] = ModuleName;
}

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

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

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

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

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

EipModuleFirstPage.prototype._ShowTable_ = function(ObjectKey, DisplayNumber)
{
	var DeviceName = EipModuleFirstPage.prototype._GetDeviceName_();
	var ProductSpecification = $("#ModuleTable").find(".Row").eq(1).find(".Column").eq(2).find(".Select").val();
	$("#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_EipModule[DeviceName][ObjectKey[i]]["ModuleName"]);
		$("#ModuleTable").find(".DynamicRow").eq(index).find(".Column").eq(3).text(Global_EipSetting[ObjectKey[i]]["NickName"]);
		index++;
	}
}

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

	$(".Dialog").eq(0).css('display', "block");
}

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

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

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

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

EipModuleFirstPage.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;
}
/*拿到全部設備名稱*/
EipModuleFirstPage.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;
}

EipModuleFirstPage.prototype._GetAllNumber_ = function()
{
	var AllDeviceName = EipModuleFirstPage.prototype._GetAllDeviceName_();
	var ObjectKey = new Array();
	for(var i=0; i<AllDeviceName.length; i++)
	{
		ObjectKey = ObjectKey.concat(Object.keys(Global_EipModule[AllDeviceName[i]]));
	}
	return ObjectKey;
}

EipModuleFirstPage.prototype._CopyGlobalVariable_ = function(DeviceName, CopyToDeviceName, number)
{
	var ObjectKey = EipModuleFirstPage.prototype._GetAllNumber_();
	var Index = ObjectKey.length+Global_EipIndex.length+1;
	var CopyNumber =  parseInt($(".Dialog").eq(0).find(".DialogTable").find(".Row").eq(1).find(".Text").val());
	for(j=0; j<CopyNumber; j++)
	{
		var OK=EipModuleFirstPage.prototype._CheckEipModuleQuantity_();
		if(OK==false)		{break;}
		
		if(typeof(Global_EipModule[DeviceName][number]) != "undefined"){
			Global_EipModule[CopyToDeviceName][Index] = jQuery.parseJSON(JSON.stringify(Global_EipModule[DeviceName][number]));
		}
		if(typeof(Global_EipSetting[number]) != "undefined"){
			Global_EipSetting[Index] = jQuery.parseJSON(JSON.stringify(Global_EipSetting[number]));
		}	
		if(typeof(Global_EipVariable[number]) != "undefined"){
			Global_EipVariable[Index] = jQuery.parseJSON(JSON.stringify(Global_EipVariable[number]));
		}
		if(typeof(Global_EipToOpcuaVariable[number]) != "undefined"){
			Global_EipToOpcuaVariable[Index] = jQuery.parseJSON(JSON.stringify(Global_EipToOpcuaVariable[number]));
		}
		if(typeof(Global_EipToMqttVariable[number]) != "undefined"){
			Global_EipToMqttVariable[Index] = jQuery.parseJSON(JSON.stringify(Global_EipToMqttVariable[number]));
		}
		Index++;
	}
}

EipModuleFirstPage.prototype._CopyTickedModule_ = function()
{
	var DeviceName = EipModuleFirstPage.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());
		EipModuleFirstPage.prototype._CopyGlobalVariable_(DeviceName, CopyToDeviceName, number);
	}
}

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

EipModuleFirstPage.prototype._RemoveGlobalVariable_ = function(DeviceName, number)
{
	Global_EipModule[DeviceName][number] = null;	delete Global_EipModule[DeviceName][number];
	Global_EipSetting[number] = null;				delete Global_EipSetting[number];
	Global_EipVariable[number] = null;				delete Global_EipVariable[number];
	Global_EipIndex.push(number);
	Global_EipIndex = Global_EipIndex.sort(function (a, b) {return a - b});

	Global_EipToOpcuaVariable[number] = null;		delete Global_EipToOpcuaVariable[number];
	Global_EipToMqttSetting[number] = null;			delete Global_EipToMqttSetting[number];
	Global_EipToMqttVariable[number] = null;		delete Global_EipToMqttVariable[number];
}

EipModuleFirstPage.prototype._RemoveTickedModule_ = function()
{
	var DeviceName = EipModuleFirstPage.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();
		EipModuleFirstPage.prototype._RemoveGlobalVariable_(DeviceName, number);
	}
}

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

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

EipModuleFirstPage.prototype._IsTickedModule_ = function()
{
	var Return=false;
	var Id = "#ModuleTable";
	if($(Id).find(".DynamicRow").find(":checked").length >0)
	{
		Return=true;
	}
	return Return;
}
/*讀取檔案名稱*/
EipModuleFirstPage.prototype._LoadLanName_ = function()
{
	var Class = new LoadFile();
	var Lan = Class.ReadFile("./File/LAN.json");
	var Module = Object.keys(Global_EipProductSpecification);
	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");
	var ModuleName = $("#Main_Center-Content").find(".Body").eq(0).find(".Table").eq(0).find(".Row").eq(1).find(".Column").eq(2).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]));
	}
	for(var i=0; i<Module.length; i++)
	{
		ModuleName.append($("<option></option>").attr("value",Module[i]).text(Module[i]));
	}
	Class = null;
}
/*初始化全域變數*/
EipModuleFirstPage.prototype._InitializationGlobalVariable_ = function()
{
	var AllDeviceName = EipModuleFirstPage.prototype._GetAllDeviceName_();
	for(var i=0; i<AllDeviceName.length; i++)
	{
		if(typeof(Global_EipModule[AllDeviceName[i]])!="object")
		{
			Global_EipModule[AllDeviceName[i]] = new Object();
		}
	}
}

EipModuleFirstPage.prototype._Initialization_ = function()
{
	EipModuleFirstPage.prototype._LoadLanName_();
	EipModuleFirstPage.prototype._InitializationGlobalVariable_();
	EipModuleFirstPage.prototype._RedisplayUi_();
}
/*檢查設備是否超過限制*/
EipModuleFirstPage.prototype._CheckEipModuleQuantity_ = function()
{
	var Return=true;
	var AllNumber = EipModuleFirstPage.prototype._GetAllNumber_();
	if(AllNumber.length >= 50)
	{
		Return=false;
	}
	return Return;
}

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

EipModuleFirstPage.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("SaveEipVariable", JSON.stringify(Global_EipVariable), Result_1);
	Class.TransmissionString("SaveEipModule", JSON.stringify(Global_EipModule), Result_2);
	Class.TransmissionString("SaveEipSetting", JSON.stringify(Global_EipSetting), Result_3);
	Class.TransmissionString("SaveEipIndex", JSON.stringify(Global_EipIndex), 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;
		}
	},500);
}

EipModuleFirstPage.prototype._SaveConvertSetting_ = function(Result)
{
	var Result_1={"Input":null}, Result_2={"Input":null}, Result_3={"Input":null};
	var Class = new Transmission();
	Class.TransmissionString("SaveEipToOpcuaVariable", JSON.stringify(Global_EipToOpcuaVariable), Result_1);
	Class.TransmissionString("SaveEipToMqttSetting", JSON.stringify(Global_EipToMqttSetting), Result_2);
	Class.TransmissionString("SaveEipToMqttVariable", JSON.stringify(Global_EipToMqttVariable), Result_3);
	var SetInterval = setInterval(function()
	{
		if(Result_1["Input"]==true && Result_2["Input"]==true && Result_3["Input"]==true)
		{
			clearInterval(SetInterval);
			Result["Input"] = true;
		}
	},500);
}

EipModuleFirstPage.prototype._SaveDataLayerAndServiceLayerVariables_ = function(Result)
{
	var Result_1={"Input":null}, Result_2={"Input":null};

	EipModuleFirstPage.prototype._SaveModuleSetting_(Result_1);
	EipModuleFirstPage.prototype._SaveConvertSetting_(Result_2);

	var SetInterval = setInterval(function()
	{
		if(Result_1["Input"]==true && Result_2["Input"]==true)
		{
			clearInterval(SetInterval);
			Result["Input"] = true;
		}
	},500);
}

EipModuleFirstPage.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=EipModuleFirstPage.prototype._CheckEipModuleQuantity_();
	if(OK == false)	{return 0;}
	EipModuleFirstPage.prototype._BuildEipModuleAllObject_(that);
	EipModuleFirstPage.prototype._RedisplayUi_();
}

EipModuleFirstPage.prototype._EditButton_ = function(that)
{
	$("#DIVariableTable").find(".DynamicRow").remove();
	$("#DOVariableTable").find(".DynamicRow").remove();
	$("#AIVariableTable").find(".DynamicRow").remove();
	$("#AOVariableTable").find(".DynamicRow").remove();
	$("#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;
	EipModuleSecondPage.prototype._SetInitialValue_(number);
	$("#MappingTable").find(".Tab").eq(1).click();
	var DeviceName = EipModuleFirstPage.prototype._GetDeviceName_();
	var ModuleName = $("#ModuleTable").find(".Row").eq(1).find(".Column").eq(2).find(".Select").val();
	var Channel = parseInt(Global_EipSetting[number]["ModeType"].split("-")[0]);
	var DeviceName = EipModuleFirstPage.prototype._GetDeviceName_();
	var VariableName = Global_EipModule[DeviceName][number]["ModuleName"];
	var VariableModule = Object.keys(Global_EipVariable[number]);
	for(var i = 0; i<Channel;i++)
	{
		var NameType = Global_EipProductSpecification[VariableName]["VariableName"][i].substr(0,2);
		EipModuleSecondPage.prototype._CreatAndJoinFormSetting_(NameType, i, number);
	}
}
EipModuleSecondPage.prototype._CreatAndJoinFormSetting_ = function(NameType, channelIndex, number)
{
	var html = ""
    , TempHtml = $("#Main_Center-Content").find(".Body").eq(1).find(".Dynamic").html();
    html = html + TempHtml;
    var Name = '#' + NameType + 'VariableTable';
    $(Name).append(html);
    var TypeIndex = $(Name).find(".DynamicRow").length;
    var DeviceName = EipModuleFirstPage.prototype._GetDeviceName_();
	var VariableName = Global_EipModule[DeviceName][number]["ModuleName"];
	var VariableModule = Object.keys(Global_EipVariable[number]);
	var channel = VariableModule[channelIndex].split("-")[2];
	$(Name).find(".DynamicRow").eq(TypeIndex - 1).find(".Column").eq(1).text(channel);
	$(Name).find(".DynamicRow").eq(TypeIndex - 1).find(".Column").eq(2).find(".Text").val(Global_EipVariable[number][VariableModule[channelIndex]]["VariableName"]);
	$(Name).find(".DynamicRow").eq(TypeIndex - 1).find(".Column").eq(3).find(".Select").val(Global_EipProductSpecification[VariableName]["VariableAttribute"][channelIndex]);
	$(Name).find(".DynamicRow").eq(TypeIndex - 1).find(".Column").eq(4).text(Global_EipProductSpecification[VariableName]["VariableType"][channelIndex]);
	$(Name).find(".DynamicRow").eq(TypeIndex - 1).find(".Column").eq(5).find(".Text").val(Global_EipVariable[number][VariableModule[channelIndex]]["Description"]);
}


EipModuleFirstPage.prototype._SaveButton_ = function(that)
{
	var Result={"Input":null};
	$(that).closest('.Row').find(".Loading").show();
	$(that).closest('.Row').find(".Message").hide();
	EipModuleFirstPage.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);
}




















EipModuleSecondPage.prototype._ChangeModeSwitch_ = function(that)
{
	$("#DIVariableTable").find(".DynamicRow").remove();
	$("#DOVariableTable").find(".DynamicRow").remove();
	$("#AIVariableTable").find(".DynamicRow").remove();
	$("#AOVariableTable").find(".DynamicRow").remove();
	$("#VariableTable").find(".DynamicRow").remove();
	var ModeSwitch = $(".Form").eq(1).find(".Row").eq(4).find(".Select").val();
	var number = $(".Form").eq(1).find(".Row").eq(0).find(".Text").val();
	var Channel = parseInt(ModeSwitch.split("-")[0]); 	
	var DeviceName = EipModuleFirstPage.prototype._GetDeviceName_();
	var VariableName = Global_EipModule[DeviceName][number]["ModuleName"];
	var VariableModule = Object.keys(Global_EipVariable[number]);
	for(var i = 0; i<Channel;i++)
	{
		var NameType = Global_EipProductSpecification[VariableName]["VariableName"][i].substr(0,2);
		EipModuleSecondPage.prototype._CreatAndJoinFormSetting_(NameType, i, number);
	}
}

EipModuleSecondPage.prototype._ModifyProductSpecificationsValue_ = function(ModuleName, Channel)
{
	var DisplayLenth = Global_EipProductSpecification[ModuleName]["Display"].length;
	for(var i=0; i<DisplayLenth; i++)
	{
		Global_EipProductSpecification[ModuleName]["Display"][i] = false;
	}
	for(var i=0; i<Channel; i++)
	{
		Global_EipProductSpecification[ModuleName]["Display"][i] = true;
	}
}

EipModuleSecondPage.prototype._SaveChangeChannelValue_ = function(number)
{
	var Type = new Array();
	var NameType;
	Type = ["DI","DO","AI","AO"];
	for(var i=0; i<4; i++)
	{
		NameType = '#' + Type[i] + 'VariableTable';
		var ChannelLength = $(NameType).find(".DynamicRow").length;
		for(var j=0; j<ChannelLength; j++)
		{
			var RegisterGroup = Type[i];
			var RegisterChannelIndex = $(NameType).find(".DynamicRow").eq(j).find(".Column").eq(1).text();
			var RegisterName = $(NameType).find(".DynamicRow").eq(j).find(".Column").eq(2).find(".Text").val();
			var RegisterDataType = $(NameType).find(".DynamicRow").eq(j).find(".Column").eq(4).text();
			var RegisterDescription = $(NameType).find(".DynamicRow").eq(j).find(".Column").eq(5).find(".Text").val();
			var RegisterVariable = RegisterGroup + '-' + RegisterDataType + '-' + RegisterChannelIndex;
			Global_EipVariable[number][RegisterVariable]["Description"] = RegisterDescription;
			Global_EipVariable[number][RegisterVariable]["VariableName"] = RegisterName;
		}
	}
	var ModeSwitch = $(".Form").eq(1).find(".Row").eq(4).find(".Select").val();
	Global_EipSetting[number]["ModeType"] = ModeSwitch;
}

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

EipModuleSecondPage.prototype._CheckIP_ = function(that)
{
	$(that).closest(".Row").find(".Message").hide();
	var Element = $(that).closest(".Row").find(".Text");
	var IP = Element.eq(0).val()+"."+Element.eq(1).val()+"."+Element.eq(2).val()+"."+Element.eq(3).val();
	var Class = new Type();
	if(Class.CheckIp(IP) == false)
	{
		$(that).closest(".Row").find(".Message").show();
		return 0;
	}
}

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

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

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

EipModuleSecondPage.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 = ContentSettings.eq(0).find(".Text").eq(0).val();
	var IP = ContentSettings.eq(3).find(".Text").eq(0).val() + "." + ContentSettings.eq(3).find(".Text").eq(1).val() + "." + ContentSettings.eq(3).find(".Text").eq(2).val() + "." + ContentSettings.eq(3).find(".Text").eq(3).val();
	if (typeof (Global_EipSetting[number]) == "undefined") {
        Global_EipSetting[number] = new Object();
    }
    Global_EipSetting[number]["NickName"] = ContentSettings.eq(2).find(".Text").eq(0).val();
    Global_EipSetting[number]["IP"] = IP;
	$("#Main_Center-Content").find(".Body").eq(1).hide();
	$("#Main_Center-Content").find(".Body").eq(0).show();
	EipModuleFirstPage.prototype._RedisplayUi_();
	
	EipModuleSecondPage.prototype._SaveChangeChannelValue_(number);
	var Class = new Path();
	Class.LoadPathHtml($("#Left-List-EIP").text(), 1, "main_PathEipModule_click(this)");
	Class = null;
}

EipModuleSecondPage.prototype._NoConfirmButton_ = function(that)
{
	$("#Main_Center-Content").find(".Body").eq(1).hide();
	$("#Main_Center-Content").find(".Body").eq(0).show();
	$("#VariableTable").find(".DynamicRow").remove();
	var Class = new Path();
	Class.LoadPathHtml($("#Left-List-EIP").text(), 1, "main_PathEipModule_click(this)");
	Class = null;
}

EipModuleSecondPage.prototype._SetFormInitialValue_ = function(number)
{
	var DeviceName=EipModuleFirstPage.prototype._GetDeviceName_();
	var ContentSettings = $(".Form").eq(1).find(".Row");
	var ModeSwitchSetting = ContentSettings.eq(4).find(".Select");
	var ModuleName = Global_EipModule[DeviceName][number]["ModuleName"];
	ContentSettings.eq(0).find(".Text").val(number);
	ContentSettings.eq(1).find(".Text").val(Global_EipModule[DeviceName][number]["ModuleName"]);
	ContentSettings.eq(2).find(".Text").val(Global_EipSetting[number]["NickName"]);
	ContentSettings.eq(3).find(".Text").eq(0).val(Global_EipSetting[number]["IP"].split(".")[0]);
	ContentSettings.eq(3).find(".Text").eq(1).val(Global_EipSetting[number]["IP"].split(".")[1]);
	ContentSettings.eq(3).find(".Text").eq(2).val(Global_EipSetting[number]["IP"].split(".")[2]);
	ContentSettings.eq(3).find(".Text").eq(3).val(Global_EipSetting[number]["IP"].split(".")[3]);		
	var Module = Global_ConnectionParameters[ModuleName]["Mode"];
	ModeSwitchSetting.empty();
	for (var i = 0; i < Module.length; i++) {
        ModeSwitchSetting.append($("<option></option>").attr("value",Module[i]).text(Module[i]));
    }
	ModeSwitchSetting.val(Global_EipSetting[number]["ModeType"]);
}

EipModuleSecondPage.prototype._EipToOpcuaVariableSetting_ = function(ChannelTick, number)
{
	var Return;
	var Prefix, SystemVariableName;
	var VariableType, VariableAttribute;
	var ChannelSwitch;
	var DeviceName = EipModuleFirstPage.prototype._GetDeviceName_();
	var ModuleName = Global_EipModule[DeviceName][number]["ModuleName"];
	Global_EipToOpcuaVariable[number] = new Object();

	for (var i = 0; i < ChannelTick; i++)
	{
		VariableType = Global_EipProductSpecification[ModuleName]["VariableType"][i];
		VariableAttribute = Global_EipProductSpecification[ModuleName]["VariableAttribute"][i];
		ChannelSwitch = Global_EipProductSpecification[ModuleName]["VariableName"][i].substring(2,4);
		SystemVariableName = EipModuleSecondPage.prototype._EipGetSystemVariableName_(VariableType, VariableAttribute, ChannelSwitch);
		Global_EipToOpcuaVariable[number][SystemVariableName] = new Object();
		Global_EipToOpcuaVariable[number][SystemVariableName]["TransformOpcUa"] = false;
	}
}

EipModuleSecondPage.prototype._EipToMqttVariableSetting_ = function(ChannelTick, number)
{
	var Return, Prefix, SystemVariableName, ChannelSwitch, VariableType, VariableAttribute;
	var DeviceName = EipModuleFirstPage.prototype._GetDeviceName_();
	var ModuleName = Global_EipModule[DeviceName][number]["ModuleName"];
	Global_EipToMqttVariable[number] = new Object();

	for (var i = 0; i < ChannelTick; i++)
	{
		VariableType = Global_EipProductSpecification[ModuleName]["VariableType"][i];
		VariableAttribute = Global_EipProductSpecification[ModuleName]["VariableAttribute"][i];
		ChannelSwitch = Global_EipProductSpecification[ModuleName]["VariableName"][i].substring(2,4);

		SystemVariableName = EipModuleSecondPage.prototype._EipGetSystemVariableName_(VariableType, VariableAttribute, ChannelSwitch);
		Global_EipToMqttVariable[number][SystemVariableName] = new Object();			
		if (VariableAttribute=='ReadWrite')
		{
			Global_EipToMqttVariable[number][SystemVariableName]["SubscribeTopic"] = '/EIP_No.' + number + '_' + ModuleName + '/' + SystemVariableName.split("-",1) + '/' + Global_EipVariable[number][SystemVariableName]["VariableName"] + '/Subscribe';
		}
		Global_EipToMqttVariable[number][SystemVariableName]["SubscribeQos"] = 2;
		Global_EipToMqttVariable[number][SystemVariableName]["PublishTopic"] =   '/EIP_No.' + number + '_' + ModuleName + '/' + SystemVariableName.split("-",1) + '/' + Global_EipVariable[number][SystemVariableName]["VariableName"] + '/Publish';
		Global_EipToMqttVariable[number][SystemVariableName]["PublishQos"] = 2;
		Global_EipToMqttVariable[number][SystemVariableName]["Retain"] = false;
		Global_EipToMqttVariable[number][SystemVariableName]["TransformMqtt"] = false;	
	}
}

EipModuleSecondPage.prototype._SetVariableTableRow_ = function(ChannelTick, number)
{
	var DeviceName = EipModuleFirstPage.prototype._GetDeviceName_();
	var VariableName = Global_EipModule[DeviceName][number]["ModuleName"];
	var VariableModule = Object.keys(Global_EipVariable[number]);
	for (var i = 0; i < ChannelTick; i++) 
	{
		$("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(1).find(".Text").val(Global_EipProductSpecification[VariableName]["VariableName"][i]);
		$("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(3).find(".Select").val(Global_EipProductSpecification[VariableName]["VariableAttribute"][i]);
		$("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(4).text(Global_EipProductSpecification[VariableName]["VariableType"][i]);
		$("#VariableTable").find(".DynamicRow").eq(i).find(".Column").eq(5).find(".Text").val(Global_EipVariable[number][VariableModule[i]]["Description"]);
	}
}

EipModuleSecondPage.prototype._CreatAddNewRow_ = function(ChannelTick)
{
	var html = ""
    , TempHtml = $("#Main_Center-Content").find(".Body").eq(1).find(".Dynamic").html();
    for (var i = 0; i < ChannelTick; i++) {
        html = html + TempHtml;
    }
    $("#VariableTable").append(html);
}

EipModuleSecondPage.prototype._ModuleChannelNumber_ = function(number)
{
	var Return;
 	var DeviceName = EipModuleFirstPage.prototype._GetDeviceName_();
	var VariableName = Global_EipModule[DeviceName][number]["ModuleName"];
	var DisplayLenth = Global_EipProductSpecification[VariableName]["Display"].length;
	
	for(var i=0; i < DisplayLenth; i++)
	{
		if(Global_EipProductSpecification[VariableName]["Display"][i]==false)
		{
			Return = i; return Return;
		}
	}

	Return = DisplayLenth;
	return Return;
}

EipModuleSecondPage.prototype._EipGetSystemVariableName_ = function(VariableType, VariableAttribute, Index)
{
	var Return = '';

	if(VariableType == "Bool" && VariableAttribute == 'ReadWrite')
		Return = Return + 'DO'+ '-' + VariableType + '-' + Index;
	else if(VariableType == "Bool" && VariableAttribute == 'Read')
		Return = Return + 'DI'+ '-' + VariableType + '-' + Index;
	else if(VariableType == "Float" && VariableAttribute == 'ReadWrite')
		Return = Return + 'AO'+ '-' + VariableType + '-' + Index;
	else if(VariableType == "Float" && VariableAttribute == 'Read')
		Return = Return + 'AI' + '-' + VariableType + '-' + Index;
	return Return;
}