//更新頁
RtuToOpcuaUpdatePage.prototype._SaveGlobalVariable_ = function(Row)
{
	var Class = new StepFlow();
	Class.LockAllUi();
	var ssocket = io.connect(), Index=0, Length=Row.length;
	Row.eq(Index).find(".Button").click();
	ssocket.emit("BackgroundExecution");
	ssocket.on("BackgroundExecution", function (Return) {
		if(Row.eq(Index).find(".Message").is(":visible") == true && 
		   Row.eq(Index).find(".Message").css("color") == "rgb(34, 119, 0)")//成功
		{
			Index++;
			Row.eq(Index).find(".Button").click();
			ssocket.emit("BackgroundExecution");
			if(Index==Length)
			{
				ssocket.emit("SocketClose");
				RtuToOpcuaUpdatePage.prototype._ShowFirstPage_();
				Class.UnLockAllUi();
			}
		}
		else if(Index<Length)
		{
			ssocket.emit("BackgroundExecution");
		}
	});
}

RtuToOpcuaUpdatePage.prototype._IsSaveGlobalVariable_ = function()
{
	var Return=false;
	var Class=new Judgment();
	var Bool_1=Class.IsSaveRtuMasterModuleGlobalVariable();
	var Bool_2=Class.IsSaveOpcUaLocalServerGlobalVariable();
	if(Bool_1==true && Bool_2==true)
	{
		Return = true;
	}
	return Return;
}

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

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

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




















//第1頁
RtuToOpcuaFirstPage.prototype._ChangeCheckbox_ = function(that)
{
	var Bool = $(that).closest(".DynamicRow").find(".Column").eq(3).find(":checkbox").prop("checked");
	var number = $(that).closest(".DynamicRow").find(".Column").eq(0).text();
	if((typeof(Global_RtuToOpcuaVariable[number])!="object") || (typeof(Global_RtuScalingToOpcuaVariable[number])!="object")){
		return;
	}
	var SystemVariable = Object.keys(Global_RtuVariable[number]);
	for(var i=0; i<SystemVariable.length; i++)
	{
		Global_RtuToOpcuaVariable[number][SystemVariable[i]]["TransformOpcUa"] = Bool;
	}
	var SystemVariable = Object.keys(Global_RtuScalingVariable[number]);
	for(var i=0; i<SystemVariable.length; i++)
	{
		if(Global_RtuScalingVariable[number][SystemVariable[i]]["TransformScaling"] == true)
		{
			Global_RtuScalingToOpcuaVariable[number][SystemVariable[i]]["TransformOpcUa"] = Bool;
		}
	}
	
	RtuToOpcuaSecondPage.prototype._SetAllRtuBitwiseToOpcuaBoolenValue_(number, Bool);
}

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

RtuToOpcuaFirstPage.prototype._IsAllTransformOpcUa_ = function(number)
{
	var Return=false;
	if(typeof(Global_RtuToOpcuaVariable[number]) != "object")
	{
		return Return;
	}
	var SystemVariable=Object.keys(Global_RtuToOpcuaVariable[number]);
	if(SystemVariable.length==0)
	{
		return Return;
	}
	for(var i=0; i<SystemVariable.length; i++)
	{
		if(Global_RtuToOpcuaVariable[number][SystemVariable[i]]["TransformOpcUa"]==false)
		{
			return Return;
		}
	}
	Return=true;
	return Return;
}
RtuToOpcuaFirstPage.prototype._IsAllTransformScalingOpcUa_ = function(number)
{
	var Return=false;
	if(typeof(Global_RtuScalingToOpcuaVariable[number]) != "object")
	{
		return Return;
	}
	var SystemVariable=Object.keys(Global_RtuScalingToOpcuaVariable[number]);
	if(SystemVariable.length==0)
	{
		var Keys=Object.keys(Global_RtuVariable[number]);
		for(var i=0; i<Keys.length; i++)
		{
			if((Keys[i].split("-")[0] == "DO") || (Keys[i].split("-")[0] == "DI"))
			{
				Return=true;
			}
			else
			{
				Return=false;
			}
			return Return;
		}
	}
	for(var i=0; i<SystemVariable.length; i++)
	{
		if(Global_RtuScalingVariable[number][SystemVariable[i]]["TransformScaling"] == true)
		{
			if(Global_RtuScalingToOpcuaVariable[number][SystemVariable[i]]["TransformOpcUa"]==false)
			{
				return Return;
			}
		}
	}
	Return=true;
	return Return;
}

RtuToOpcuaFirstPage.prototype._IsAllTransformBitwiseOpcUa_ = function(number)
{
	var Return=false;
	if(typeof(Global_RtuBitwiseToOpcuaVariable[number]) != "object")
	{
		return Return;
	}
	var SystemVariable=Object.keys(Global_RtuBitwiseToOpcuaVariable[number]);
	if(SystemVariable.length==0)
	{
		var Keys=Object.keys(Global_RtuVariable[number]);
		for(var i=0; i<Keys.length; i++)
		{
			if((Keys[i].split("-")[0] == "DO") || (Keys[i].split("-")[0] == "DI"))
			{
				Return=true;
			}
			else
			{
				Return=false;
			}
			return Return;
		}
	}
	for(var i=0; i<SystemVariable.length; i++)
	{
		Index = Object.keys(Global_RtuBitwiseToOpcuaVariable[number][SystemVariable[i]]);
		for(var j=0; j<Index.length; j++)
		{
			if(Index[j].indexOf("BitTagName-") != -1)
			{
				if(Global_RtuBitwiseToOpcuaVariable[number][SystemVariable[i]][Index[j]]==false)
				return Return;
			}
		}
	}
	Return=true;
	return Return;
}

RtuToOpcuaFirstPage.prototype._IsAllTransformCombineOpcUa_ = function(Bool_IO, Bool_Scaling, Bool_Bitwise)
{
	var Return=false;
	if(Bool_IO == true && Bool_Scaling == true && Bool_Bitwise == true)
		Return = true;
	return Return;
}

RtuToOpcuaFirstPage.prototype._GetAllModuleData_ = function()
{
	var Return = new Object();
	var DeviceName = Object.keys(Global_RtuModule);
	for(var i=0; i<DeviceName.length; i++)
	{
		var nmuber = Object.keys(Global_RtuModule[DeviceName[i]]);
		for(var j=0; j<nmuber.length; j++)
		{
			Return[nmuber[j]] = new Object();
			Return[nmuber[j]] = jQuery.parseJSON(JSON.stringify(Global_RtuModule[DeviceName[i]][nmuber[j]]));
		}
	}
	return Return;
}

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

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

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

RtuToOpcuaFirstPage.prototype._GetAllNumber_ = function()
{
	var AllDeviceName = RtuToOpcuaFirstPage.prototype._GetAllDeviceName_();
	var ObjectKey = new Array();
	for(var i=0; i<AllDeviceName.length; i++)
	{
		if(typeof(Global_RtuModule[AllDeviceName[i]]) == "object")
		{
			ObjectKey = ObjectKey.concat(Object.keys(Global_RtuModule[AllDeviceName[i]]));
		}
	}
	ObjectKey = ObjectKey.sort(function (a, b) {return a - b});
	return ObjectKey;
}

RtuToOpcuaFirstPage.prototype._ShowTable_ = function(ObjectKey, DisplayNumber)
{
	var AllModuleData = RtuToOpcuaFirstPage.prototype._GetAllModuleData_();
	$("#ModuleTable").find(".DynamicRow").remove();
	var html = $(".Dynamic").html();
	var PageNumber = $("#ModuleToolbar").find(".Text").val();
	var Start = (PageNumber-1)*DisplayNumber;
	if((ObjectKey.length-Start)>DisplayNumber){
		var End = Start + DisplayNumber;
	}
	else{
		var End = ObjectKey.length;
	}
	var index = 0;
	for(var i=Start; i<End && End!=0; i++)
	{
		$("#ModuleTable").append(html);
		$("#ModuleTable").find(".DynamicRow").eq(index).find(".Column").eq(0).text(ObjectKey[i]);
		$("#ModuleTable").find(".DynamicRow").eq(index).find(".Column").eq(1).text(AllModuleData[ObjectKey[i]]["ModuleName"]);
		//var Bool = RtuToOpcuaFirstPage.prototype._IsAllTransformOpcUa_(ObjectKey[i]);
		
		var Bool_IO = RtuToOpcuaFirstPage.prototype._IsAllTransformOpcUa_(ObjectKey[i]);
		var Bool_Scling = RtuToOpcuaFirstPage.prototype._IsAllTransformScalingOpcUa_(ObjectKey[i]);
		var Bool_BitWise = RtuToOpcuaFirstPage.prototype._IsAllTransformBitwiseOpcUa_(ObjectKey[i]);
		var Bool = RtuToOpcuaFirstPage.prototype._IsAllTransformCombineOpcUa_(Bool_IO, Bool_Scling, Bool_BitWise);
		
		$("#ModuleTable").find(".DynamicRow").eq(index).find(".Column").eq(3).find(":checkbox").prop("checked",Bool);
		index++;
	}
}

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

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

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

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

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

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

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

RtuToOpcuaFirstPage.prototype._SaveStart_ = function(that, ssocket)
{
	ssocket.emit("BackgroundExecution");
	ssocket.on("BackgroundExecution", function (Return) {
	if(Return == true)
	{
		var Callback0 = function(){RtuToOpcuaFirstPage.prototype._SaveFinished_(that, ssocket);}
		var JsonString = JSON.stringify(Global_RtuToOpcuaVariable);
		var JsonString_2 = JSON.stringify(Global_RtuScalingToOpcuaVariable);
		var JsonString_3 = JSON.stringify(Global_RtuBitwiseToOpcuaVariable);
		$(that).closest('.Row').find(".Message").hide();
		$(that).closest('.Row').find(".Loading").show();
		$(that).closest('.Row').find(".Message").eq(1).show();
		RtuToOpcuaFirstPage.prototype._TransmissionJson_(ssocket, "SaveRtuToOpcuaVariable", JsonString, Callback0);
		RtuToOpcuaFirstPage.prototype._TransmissionJson_(ssocket, "SaveRtuScalingToOpcuaVariable", JsonString_2, Callback0);
		RtuToOpcuaFirstPage.prototype._TransmissionJson_(ssocket, "SaveRtuBitwiseToOpcuaVariable", JsonString_3, Callback0);
	}
	});
}

RtuToOpcuaFirstPage.prototype._EditButton_ = function(that)
{
	$("#Main_Center-Content").find(".Body").eq(2).find(".Message").hide();
	var number = parseInt($(that).closest(".DynamicRow").find(".Column").eq(0).text());
	Global_RtuToOpcuaModuleNumber = number;
	$("#Main_Center-Content").find(".Body").eq(1).hide();
	$("#Main_Center-Content").find(".Body").eq(2).show();
	var Name = $("#Main_Center-Content").find(".Body").eq(2).find(".FormTitle").eq(0).text();
	var Class = new Path();
	Class.LoadPathHtml(Name, 2);
	Class = null;
	RtuToOpcuaSecondPage.prototype._SetInitialValue_(number);
	$("#Main_Center-Content").find(".MappingTable").find(".Tab").eq(1).click();
}

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




















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

RtuToOpcuaSecondPage.prototype._ScalingVariableTableAddNewRow_ = function(number)
{
	if(typeof(Global_RtuScalingToOpcuaVariable[number]) != "object")	{return 0;}
	var html="", TempHtml = $("#Main_Center-Content").find(".Body").eq(2).find(".Dynamic").html();
	var SystemVariable = Object.keys(Global_RtuScalingToOpcuaVariable[number]);
	for(var i=0; i<SystemVariable.length; i++)
	{
		if(Global_RtuScalingVariable[number][SystemVariable[i]]["TransformScaling"] == true)
		{
			html = html + TempHtml;
		}
	}
	$("#ScalingVariableTable").append(html);
}

RtuToOpcuaSecondPage.prototype._BitwiseVariableTableAddNewRow_ = function(Data)
{
	var html="", TempHtml = $("#Main_Center-Content").find(".Body").eq(2).find(".Dynamic").html();
	for(var i=0; i<Data.length; i++)
	{
		html = html + TempHtml;
	}
	$("#BitwiseVariableTable").append(html);
}

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

RtuToOpcuaSecondPage.prototype._SetScalingVariableTableNewRowValue_ = function(number)
{
	if(typeof(Global_RtuScalingToOpcuaVariable[number]) != "object")	{return 0;}
	var Variable = Global_RtuScalingVariable[number];
	var Class=new Algorithm();
	var SystemVariable = Object.keys(Global_RtuScalingToOpcuaVariable[number]).sort(Class.StringNumberMixedSort());
	var Row = $("#ScalingVariableTable").find(".DynamicRow");
	var j = 0;
	for(var i=0; i<Row.length; i++)
	{
		while( j < SystemVariable.length)
		{
			if(Global_RtuScalingVariable[number][SystemVariable[j]]["TransformScaling"] == true)
			{
				Row.eq(i).find(".Column").eq(0).text(SystemVariable[j]);
				Row.eq(i).find(".Column").eq(1).text(Variable[SystemVariable[j]]["ScaleVariableName"]);
				var ReadWriteType = RtuToOpcuaSecondPage.prototype._GetReadWriteType_(SystemVariable[i].split("-")[0]);
				Row.eq(i).find(".Column").eq(2).find(".Select").val(ReadWriteType);
				Row.eq(i).find(".Column").eq(3).text("Float");
				var Bool = Global_RtuScalingToOpcuaVariable[number][SystemVariable[j]]["TransformOpcUa"];
				Row.eq(i).find(".Column").eq(4).find(".Checkbox").prop("checked",Bool);
				j++;
				break;
			}
			j++;
		}
	}
}

RtuToOpcuaSecondPage.prototype._SetBitwiseVariableTableNewRowValue_ = function(Data)
{
	var Row = $("#BitwiseVariableTable").find(".DynamicRow");
	for(var i=0; i<Data.length; i++)
	{
		Row.eq(i).find(".Column").eq(0).text(Data[i]["SystemVariable"]);
		Row.eq(i).find(".Column").eq(1).text(Data[i]["VariableName"]);
		var ReadWriteType = RtuToOpcuaSecondPage.prototype._GetReadWriteType_("DI");
		Row.eq(i).find(".Column").eq(2).find(".Select").val(ReadWriteType);
		Row.eq(i).find(".Column").eq(3).text("Bool");
		var Bool = Data[i]["TransformOpcUa"];
		Row.eq(i).find(".Column").eq(4).find(".Checkbox").prop("checked",Bool);
	}
}

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

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

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

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

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

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

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

RtuToOpcuaSecondPage.prototype._SetScalingVariableTableInitialValue_ = function(number)
{
	RtuToOpcuaSecondPage.prototype._ScalingVariableTableAddNewRow_(number);
	RtuToOpcuaSecondPage.prototype._SetScalingVariableTableNewRowValue_(number);
}

RtuToOpcuaSecondPage.prototype._CheckBitwiseGlobalVariableExist_ = function(GlobalVariable, SystemVariable, VariableName)
{
	var Return; 

	if(typeof(GlobalVariable[SystemVariable][VariableName]) != "undefined")
	{
		Return = true;
	}
	return Return;
}

RtuToOpcuaSecondPage.prototype._BitwiseVariablePretreatment_ = function(number)
{
	var Return=new Array(); var BitTotalNumber = 0; var BitNameId = ""; var VariableExist;
	if((typeof(Global_RtuBitwiseToOpcuaVariable[number]) != "object") && (typeof(Global_RtuBitwiseVariable[number]) != "object"))	{return 0;}
	var SystemVariable = Object.keys(Global_RtuBitwiseToOpcuaVariable[number]);
	for(var i=0; i<SystemVariable.length; i++)
	{
		if(Global_RtuBitwiseVariable[number][SystemVariable[i]]["DataType"] == "Short" || Global_RtuBitwiseVariable[number][SystemVariable[i]]["DataType"]== "Unsigned Short") BitTotalNumber = 16;
		else if(Global_RtuBitwiseVariable[number][SystemVariable[i]]["DataType"] == "Long" || Global_RtuBitwiseVariable[number][SystemVariable[i]]["DataType"] == "Unsigned Long")  BitTotalNumber = 32;
		for(var j=0; j<BitTotalNumber; j++)
		{
			BitNameId = "BitTagName-" + j.toString();
			VariableExist = RtuToOpcuaSecondPage.prototype._CheckBitwiseGlobalVariableExist_(Global_RtuBitwiseToOpcuaVariable[number], SystemVariable[i], BitNameId);
			if(VariableExist == true)
			{
				Obj = new Object();
				Obj["SystemVariable"]=SystemVariable[i];
				Obj["VariableName"]=Global_RtuBitwiseVariable[number][SystemVariable[i]][BitNameId];
				Obj["GlobalIndexName"]=BitNameId;
				Obj["TransformOpcUa"]=Global_RtuBitwiseToOpcuaVariable[number][SystemVariable[i]][BitNameId];
				Return.push(Obj);
			}
		}
	}
	return Return;
}

RtuToOpcuaSecondPage.prototype._SetBitwiseVariableTableInitialValue_ = function(number)
{
	Local_RtuToOpcuaBitwiseTotalTag = RtuToOpcuaSecondPage.prototype._BitwiseVariablePretreatment_(number);
	RtuToOpcuaSecondPage.prototype._BitwiseVariableTableAddNewRow_(Local_RtuToOpcuaBitwiseTotalTag);
	RtuToOpcuaSecondPage.prototype._SetBitwiseVariableTableNewRowValue_(Local_RtuToOpcuaBitwiseTotalTag);
}

RtuToOpcuaSecondPage.prototype._SetFormInitialValue_ = function(number)
{
	var ModuleName=RtuToOpcuaSecondPage.prototype._GetModuleName_(number);
	var ContentSettings = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0).find(".Row");
	ContentSettings.eq(0).find(".Text").val(number);
	ContentSettings.eq(1).find(".Text").val(ModuleName);
}

RtuToOpcuaSecondPage.prototype._SetInitialValue_ = function(number)
{
	RtuToOpcuaSecondPage.prototype._ClearTable_(number);
	RtuToOpcuaSecondPage.prototype._ClearScalingTable_(number);
	RtuToOpcuaSecondPage.prototype._ClearBitwiseTable_(number);
	RtuToOpcuaSecondPage.prototype._SetFormInitialValue_(number);

	RtuToOpcuaSecondPage.prototype._SetVariableInitialValue_(number);
	RtuToOpcuaSecondPage.prototype._SetScalingInitialValue_(number);
	RtuToOpcuaSecondPage.prototype._SetBitwiseInitialValue_(number);
}

RtuToOpcuaSecondPage.prototype._SetVariableInitialValue_ = function(number)
{
	RtuToOpcuaSecondPage.prototype._SetVariableTableInitialValue_(number);
}

RtuToOpcuaSecondPage.prototype._SetScalingInitialValue_ = function(number)
{
	RtuToOpcuaSecondPage.prototype._SetScalingVariableTableInitialValue_(number);
}

RtuToOpcuaSecondPage.prototype._SetBitwiseInitialValue_ = function(number)
{
	RtuToOpcuaSecondPage.prototype._SetBitwiseVariableTableInitialValue_(number);
}

RtuToOpcuaSecondPage.prototype._ModifyRtuToOpcuaVariableObject_ = function(number)
{
	var Row = $("#VariableTable").find(".DynamicRow");
	for(var i=0; i<Row.length; i++)
	{
		var SystemVariable = Row.eq(i).find(".Column").eq(0).text();
		var Bool = Row.eq(i).find(":checkbox").prop("checked");
		Global_RtuToOpcuaVariable[number][SystemVariable]["TransformOpcUa"] = Bool;
	}
}

RtuToOpcuaSecondPage.prototype._ModifyRtuScalingToOpcuaVariableObject_ = function(number)
{
	var Row = $("#ScalingVariableTable").find(".DynamicRow");
	for(var i=0; i<Row.length; i++)
	{
		var SystemVariable = Row.eq(i).find(".Column").eq(0).text();
		var Bool = Row.eq(i).find(":checkbox").prop("checked");
		Global_RtuScalingToOpcuaVariable[number][SystemVariable]["TransformOpcUa"] = Bool;
	}
}

RtuToOpcuaSecondPage.prototype._SetAllRtuBitwiseToOpcuaBoolenValue_  = function(number, Bool)
{
	var ObjectKey = Object.keys(Global_RtuBitwiseToOpcuaVariable[number]);
	for(var i=0; i<ObjectKey.length; i++)
	{
		var Index = Object.keys(Global_RtuBitwiseToOpcuaVariable[number][ObjectKey[i]]);
		for(var j=0; j<Index.length; j++)
		{
			if(Index[j].indexOf("BitTagName-") != -1)
			{
				Global_RtuBitwiseToOpcuaVariable[number][ObjectKey[i]][Index[j]] = Bool;
			}
		}
	}
}

RtuToOpcuaSecondPage.prototype._SetRtuBitwiseToOpcuaBoolenValue_ = function(SystemVariable, IndexName, Bool, number)
{
	var ObjectKey = Object.keys(Global_RtuBitwiseToOpcuaVariable[number]);
	for(var i=0; i<ObjectKey.length; i++)
	{
		if(ObjectKey[i].indexOf(SystemVariable) != -1)
		{
			Global_RtuBitwiseToOpcuaVariable[number][ObjectKey[i]][IndexName]=Bool;
			break;
		}
	}
}

RtuToOpcuaSecondPage.prototype._FindGlobalIndexFromLocalRtuToOpcuaBitwiseTotalTag_ = function(SystemVariable, VariableName)
{
	var Return;
	for(var i=0; i<Local_RtuToOpcuaBitwiseTotalTag.length; i++)
	{
		if((Local_RtuToOpcuaBitwiseTotalTag[i]["VariableName"] == VariableName) && (Local_RtuToOpcuaBitwiseTotalTag[i]["SystemVariable"] == SystemVariable))
		{
			Return = Local_RtuToOpcuaBitwiseTotalTag[i]["GlobalIndexName"];
			break;
		}
	}
	return Return;
}

RtuToOpcuaSecondPage.prototype._ModifyRtuBitwiseToOpcuaVariableObject_ = function(number)
{
	var Row = $("#BitwiseVariableTable").find(".DynamicRow");
	for(var i=0; i<Row.length; i++)
	{
		var SystemVariable = Row.eq(i).find(".Column").eq(0).text();
		var VariableName = Row.eq(i).find(".Column").eq(1).text();
		var Bool = Row.eq(i).find(":checkbox").prop("checked");
		var IndexName = RtuToOpcuaSecondPage.prototype._FindGlobalIndexFromLocalRtuToOpcuaBitwiseTotalTag_(SystemVariable, VariableName);
		RtuToOpcuaSecondPage.prototype._SetRtuBitwiseToOpcuaBoolenValue_(SystemVariable, IndexName, Bool, number);
	}
}

RtuToOpcuaSecondPage.prototype._ConfirmButton_ = function()
{
	var ContentSettings = $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0).find(".Row");
	var number = parseInt(ContentSettings.eq(0).find(".Text").val());
	RtuToOpcuaSecondPage.prototype._ModifyRtuToOpcuaVariableObject_(number);
	RtuToOpcuaSecondPage.prototype._ModifyRtuScalingToOpcuaVariableObject_(number);
	RtuToOpcuaSecondPage.prototype._ModifyRtuBitwiseToOpcuaVariableObject_(number);
	$("#Main_Center-Content").find(".Body").eq(2).hide();
	$("#Main_Center-Content").find(".Body").eq(1).show();
	RtuToOpcuaFirstPage.prototype._RedisplayUi_();
	var Class = new Path();
	Class.LoadPathHtml($("#Left-List-RtuToOpcua").text(), 1, "main_PathRtuToOpcua_click(this)");
	Class = null;
}

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

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

RtuToOpcuaSecondPage.prototype._HideVariableTableForm_ = function()
{
	$("#VariableTable").css("display", "none");
};

RtuToOpcuaSecondPage.prototype._ShowVariableTableForm_ = function()
{
	$("#VariableTable").css("display", "table");
};

RtuToOpcuaSecondPage.prototype._HideScalingVariableTableForm_ = function()
{
	$("#ScalingVariableTable").css("display", "none");
};

RtuToOpcuaSecondPage.prototype._ShowScalingVariableTableForm_ = function()
{
	$("#ScalingVariableTable").css("display", "table");
};

RtuToOpcuaSecondPage.prototype._HideBitwiseVariableTableForm_ = function()
{
	$("#BitwiseVariableTable").css("display", "none");
};

RtuToOpcuaSecondPage.prototype._ShowBitwiseVariableTableForm_ = function()
{
	$("#BitwiseVariableTable").css("display", "table");
};

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

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

RtuToOpcuaSecondPage.prototype._ChangeTab_ = function(Mode, that)
{
	RtuToOpcuaSecondPage.prototype._ClearAllCss_(that);
	RtuToOpcuaSecondPage.prototype._ChangeCurrentlyTabCss_(that);

	if(Mode=="ShowRtuScalingToOpcua")
	{
		RtuToOpcuaSecondPage.prototype._HideVariableTableForm_();
		RtuToOpcuaSecondPage.prototype._HideBitwiseVariableTableForm_();
		RtuToOpcuaSecondPage.prototype._ShowScalingVariableTableForm_();
	}
	else if(Mode=="ShowRtuBitwiseToOpcua")
	{
		RtuToOpcuaSecondPage.prototype._HideVariableTableForm_();
		RtuToOpcuaSecondPage.prototype._HideScalingVariableTableForm_();
		RtuToOpcuaSecondPage.prototype._ShowBitwiseVariableTableForm_();
	}
	else if(Mode=="ShowRtuToOpcua")
	{
		RtuToOpcuaSecondPage.prototype._HideScalingVariableTableForm_();
		RtuToOpcuaSecondPage.prototype._HideBitwiseVariableTableForm_();
		RtuToOpcuaSecondPage.prototype._ShowVariableTableForm_();
	}
};