//第1頁
RemoteBrokerFirstPage.prototype._AutomaticBrokerName_ = function()
{
	var Table = $("#Main_Center-Content").find(".Body").eq(0).find(".Table").eq(0);
	var Index = 1;
	var object = jQuery.parseJSON(JSON.stringify(Global_RemoteBroker));
	var number = Object.keys(object);
	var Name = "Name"+Index;
	for(var i=0; i<number.length; i++)
	{
		if(Name == object[number[i]]["Name"])
		{
			Index++;
			Name = "Name"+Index;
			i=0;
			continue;
		}
	}
	Table.find(".Row").eq(1).find(".Text").val(Name);
}

RemoteBrokerFirstPage.prototype._BuildRemoteBrokerObject_ = function(that)
{
	var number = Object.keys(Global_RemoteBroker);
	if(number.length == 0)
	{
		number=1;
	}
	else if(number.length>0)
	{
		number = Math.max.apply(null,number)+1;
	}
	if(typeof(Global_RemoteBroker[number]) == "undefined")
	{
		Global_RemoteBroker[number] = new Object();
		Global_RemoteBroker[number]["Name"] = $(that).closest(".Row").find(".Column").eq(2).find(".Text").val();
		Global_RemoteBroker[number]["IP"] = "127.0.0.1";
		Global_RemoteBroker[number]["Port"] = 1883;
		Global_RemoteBroker[number]["Keepalive"] = 60;
		Global_RemoteBroker[number]["SSL/TLS"] = false;
		Global_RemoteBroker[number]["CaCertFile"] = "";
		Global_RemoteBroker[number]["CertFile"] = "";
		Global_RemoteBroker[number]["KeyFile"] = "";
		Global_RemoteBroker[number]["AnonymousLogin"] = true;
		Global_RemoteBroker[number]["Account"] = "";
		Global_RemoteBroker[number]["Password"] = "";
	}
}

RemoteBrokerFirstPage.prototype._ShowTable_ = function(number, DisplayNumber)
{
	$("#BrokerTable").find(".DynamicRow").remove();
	var html = $(".Dynamic").html();
	var PageNumber = $("#Toolbar").find(".Text").val();
	var Start = (PageNumber-1)*DisplayNumber;
	if((number.length-Start)>DisplayNumber){
		var End = Start + DisplayNumber;
	}
	else{
		var End = number.length;
	}
	var index = 0;
	for(var i=Start; i<End && End!=0; i++)
	{
		$("#BrokerTable").append(html);
		$("#BrokerTable").find(".DynamicRow").eq(index).find(".Column").eq(0).text(number[i]);
		$("#BrokerTable").find(".DynamicRow").eq(index).find(".Column").eq(2).text(Global_RemoteBroker[number[i]]["Name"]);
		$("#BrokerTable").find(".DynamicRow").eq(index).find(".Column").eq(3).text(Global_RemoteBroker[number[i]]["IP"]);
		$("#BrokerTable").find(".DynamicRow").eq(index).find(".Column").eq(4).text(Global_RemoteBroker[number[i]]["Port"]);
		index++;
	}
}

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

RemoteBrokerFirstPage.prototype._RedisplayUi_ = function()
{
	var DisplayNumber = 10;//RTU 20 TCP 40 ASCII 20
	var number = Object.keys(Global_RemoteBroker);
	RemoteBrokerFirstPage.prototype._UpdateTableToolbar_(number, DisplayNumber);
	RemoteBrokerFirstPage.prototype._ShowTable_(number, DisplayNumber);
	var Id = "#BrokerTable";
	$(Id).find(".Row").eq(0).find(":checkbox").prop("checked",false);
}

RemoteBrokerFirstPage.prototype._AddNewRow_ = function(that)
{
	RemoteBrokerFirstPage.prototype._CheckBrokerName_();
	var Id = "#BrokerTable";
	var ShowErrorMessage = $(Id).find(".Row").eq(0).find(".Message").is(":visible");
	if(ShowErrorMessage == true)	{return 0;}
	RemoteBrokerFirstPage.prototype._BuildRemoteBrokerObject_(that);
	RemoteBrokerFirstPage.prototype._RedisplayUi_();
	RemoteBrokerFirstPage.prototype._AutomaticBrokerName_();
}

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

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

RemoteBrokerFirstPage.prototype._RemoveConversionTransferModbusGlobalVariable_ = function(DeviceName)
{
	var deviceName = DeviceName+" (Remote)";
	var number = Object.keys(Global_RtuToMqttSetting);
	for(var i=0; i<number.length; i++){
		var array = Global_RtuToMqttSetting[number[i]]["MqttConnection"];
		Global_RtuToMqttSetting[number[i]]["MqttConnection"] = RemoteBrokerFirstPage.prototype._ArrayComplement_(array, deviceName);
	}
	var number = Object.keys(Global_TcpToMqttSetting);
	for(var i=0; i<number.length; i++){
		var array = Global_TcpToMqttSetting[number[i]]["MqttConnection"];
		Global_TcpToMqttSetting[number[i]]["MqttConnection"] = RemoteBrokerFirstPage.prototype._ArrayComplement_(array, deviceName);
	}
	var number = Object.keys(Global_AsciiToMqttSetting);
	for(var i=0; i<number.length; i++){
		var array = Global_AsciiToMqttSetting[number[i]]["MqttConnection"];
		Global_AsciiToMqttSetting[number[i]]["MqttConnection"] = RemoteBrokerFirstPage.prototype._ArrayComplement_(array, deviceName);
	}
}

RemoteBrokerFirstPage.prototype._RemoveConversionTransferGlobalVariable_ = function(DeviceName)
{
	RemoteBrokerFirstPage.prototype._RemoveConversionTransferModbusGlobalVariable_(DeviceName);
	var deviceName = DeviceName+" (Remote)";
	var number = Object.keys(Global_MqttSetting);
	for(var i=0; i<number.length; i++){
		var array = Global_MqttSetting[number[i]]["MqttConnection"];
		Global_MqttSetting[number[i]]["MqttConnection"] = RemoteBrokerFirstPage.prototype._ArrayComplement_(array, deviceName);
	}
	var number = Object.keys(Global_EipToMqttSetting);
	for(var i=0; i<number.length; i++){
		var array = Global_EipToMqttSetting[number[i]]["MqttConnection"];
		Global_EipToMqttSetting[number[i]]["MqttConnection"] = RemoteBrokerFirstPage.prototype._ArrayComplement_(array, deviceName);
	}
}

RemoteBrokerFirstPage.prototype._RemoveMqttGroupConnectionGlobalVariable_ = function(DeviceName)
{
	var deviceName = DeviceName+" (Remote)";
	
	var number = Object.keys(Global_MqttJsonGroup);
	for(var i=0; i<number.length; i++){
		var array = Global_MqttJsonGroup[number[i]]["MqttConnection"];
		Global_MqttJsonGroup[number[i]]["MqttConnection"] = RemoteBrokerFirstPage.prototype._ArrayComplement_(array, deviceName);
	}
}

RemoteBrokerFirstPage.prototype._RemoveGlobalVariable_ = function(DeviceName, number)
{
	Global_RemoteBroker[number] = null;	delete Global_RemoteBroker[number];
	RemoteBrokerFirstPage.prototype._RemoveConversionTransferGlobalVariable_(DeviceName);
	RemoteBrokerFirstPage.prototype._RemoveMqttGroupConnectionGlobalVariable_(DeviceName);
}

RemoteBrokerFirstPage.prototype._RemoveTickedBroker_ = function()
{
	var Id = "#BrokerTable";
	var element = $(Id).find(".DynamicRow").find(":checked");
	var CheckedLength = element.length;
	for(var i=0; i<CheckedLength; i++)
	{
		var number = element.eq(i).closest(".DynamicRow").find(".Column").eq(0).text();
		var DeviceName = element.eq(i).closest(".DynamicRow").find(".Column").eq(2).text();
		RemoteBrokerFirstPage.prototype._RemoveGlobalVariable_(DeviceName, number);
	}
}

RemoteBrokerFirstPage.prototype._DeleteBroker_ = function()
{
	RemoteBrokerFirstPage.prototype._RemoveTickedBroker_();
	RemoteBrokerFirstPage.prototype._RedisplayUi_();
}

RemoteBrokerFirstPage.prototype._IsNameRepeat_ = function(Name)
{
	var Return=false;
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row");
	var CurrentlyNumber = Row.eq(0).find(".Text").val();
	var number = Object.keys(Global_RemoteBroker);
	for(var i=0; i<number.length; i++)
	{
		if(Name == Global_RemoteBroker[number[i]]["Name"])
		{
			Return=true;
			break;
		}
	}
	return Return;
}

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

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

RemoteBrokerFirstPage.prototype._CheckBrokerName_ = function()
{
	var Id = "#BrokerTable";
	$(Id).find(".Message").hide();
	var Row=$(Id).find(".Row");
	var Name = Row.eq(1).find(".Text").val();
	var Class = new Type();
	if(Class.EnglishDigitalAndBottomline(Name) == false)
	{
		Row.eq(0).find(".Message").eq(0).show();
	}
	Class = null;
	if(RemoteBrokerFirstPage.prototype._IsNameRepeat_(Name) == true)
	{
		Row.eq(0).find(".Message").eq(1).show();
	}
}

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

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

RemoteBrokerFirstPage.prototype._SaveFinished_ = function(that, ssocket)
{
	$(that).closest('.Row').find(".Loading").hide();
	$(that).closest('.Row').find(".Message").eq(1).show();
	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");
	});
}

RemoteBrokerFirstPage.prototype._SaveStart_ = function(that, ssocket)
{	
	ssocket.emit("BackgroundExecution");
	ssocket.on("BackgroundExecution", function (Return) {
	if(Return == true)
	{
		var array = new Array(Global_RemoteBroker);
		ssocket.emit("SaveRemoteBrokerSetting", array);		
		var Callback0 = function(){RemoteBrokerFirstPage.prototype._SaveFinished_(that, ssocket);}
		var JsonString1 = JSON.stringify(Global_RtuToMqttSetting);
		var Callback1 = function(){RemoteBrokerFirstPage.prototype._TransmissionJson_(ssocket, "SaveRtuToMqttSetting", JsonString1, Callback0);}
		
		var JsonString2 = JSON.stringify(Global_TcpToMqttSetting);
		var Callback2 = function(){RemoteBrokerFirstPage.prototype._TransmissionJson_(ssocket, "SaveTcpToMqttSetting", JsonString2, Callback1);}
		
		var JsonString3 = JSON.stringify(Global_AsciiToMqttSetting);
		var Callback3 = function(){RemoteBrokerFirstPage.prototype._TransmissionJson_(ssocket, "SaveAsciiToMqttSetting", JsonString3, Callback2);}

		var JsonString4 = JSON.stringify(Global_MqttSetting);
		var Callback4 = function(){RemoteBrokerFirstPage.prototype._TransmissionJson_(ssocket, "SaveMqttSetting", JsonString4, Callback3);}

		var JsonString5 = JSON.stringify(Global_MqttJsonGroup);
		$(that).closest('.Row').find(".Message").hide();
		$(that).closest('.Row').find(".Loading").show();
		RemoteBrokerFirstPage.prototype._TransmissionJson_(ssocket, "SaveMqttJsonGroup", JsonString5, Callback4);
	}
	});
}

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

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




















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

RemoteBrokerSecondPage.prototype._CheckIP_ = function(that)
{
	var IP = $(that).closest(".Row").find(".Text").val();
	if(IP == "")
	{
		$(that).closest(".Row").find(".Text").val("127.0.0.1");
	}
	Class = null;
}

RemoteBrokerSecondPage.prototype._CheckPort_ = function(that)
{
	var Port = $(that).closest(".Row").find(".Text").val();
	Port = parseInt(Port);
	$(that).closest(".Row").find(".Text").val(Port);
	var Class = new Type();
	if(Class.CheckPositiveIntegers(Port) == false)
	{
		$(that).closest(".Row").find(".Text").val(1883);
	}
	Class = null;
}

RemoteBrokerSecondPage.prototype._CheckKeepAlive_ = function(that)
{
	var Name = $(that).closest(".Row").find(".Text").val();
	Name = parseInt(Name);
	$(that).closest(".Row").find(".Text").val(Name);
	var Class = new Type();
	if(Class.CheckPositiveIntegers(Name) == false)
	{
		$(that).closest(".Row").find(".Text").val(60);
	}
	Class = null;
	if(parseInt(Name) < 5)
	{
		$(that).closest(".Row").find(".Text").val(60);
	}
}

RemoteBrokerSecondPage.prototype._CheckAccount_ = function(that)
{	
	if($(that).val() == "")
	{
		$(that).closest(".RightColumn").find(".Message").show();
		return 0;
	}
	$(that).closest(".RightColumn").find(".Message").hide();
};

RemoteBrokerSecondPage.prototype._CheckPassword_ = function(that)
{	
	if($(that).val() == "")
	{
		$(that).closest(".RightColumn").find(".Message").show();
		return 0;
	}
	$(that).closest(".RightColumn").find(".Message").hide();
};

RemoteBrokerSecondPage.prototype._CheckTrustedCertificate_ = function()
{
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row");
	Row.eq(6).find(".Message").hide();
	var TrustedCertificate = Row.eq(6).find(".Select").val();
	if(TrustedCertificate == "")
	{
		Row.eq(6).find(".Message").show();
	}
};

RemoteBrokerSecondPage.prototype._CheckCertificatePrivateKey_ = function()
{
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row");
	Row.eq(7).find(".Message").hide();
	Row.eq(8).find(".Message").hide();
	var Certificate = Row.eq(7).find(".Select").val();
	var PrivateKey = Row.eq(8).find(".Select").val();
	if(Certificate==null)
	{
		Certificate = "";
	}
	if(PrivateKey == null)
	{
		PrivateKey = "";
	}
	if(!(Certificate != "" && PrivateKey != ""))
	{
		if(Certificate == "" && PrivateKey == "")
		{
			return;
		}
		else if(Certificate == "")
		{
			Row.eq(7).find(".Message").show();
		}
		else if(PrivateKey == "")
		{
			Row.eq(8).find(".Message").show();
		}
	}
};

RemoteBrokerSecondPage.prototype._CheckKeepAlive_ = function(that)
{
	var Name = $(that).closest(".Row").find(".Text").val();
	Name = parseInt(Name);
	$(that).closest(".Row").find(".Text").val(Name);
	var Class = new Type();
	if(Class.CheckPositiveIntegers(Name) == false)
	{
		$(that).closest(".Row").find(".Text").val(60);
	}
	Class = null;
	if(parseInt(Name) < 5)
	{
		$(that).closest(".Row").find(".Text").val(60);
	}
}

RemoteBrokerSecondPage.prototype._IsNameRepeat_ = function(Name)
{
	var Return=false;
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row");
	var CurrentlyNumber = Row.eq(0).find(".Text").val();
	var number = Object.keys(Global_RemoteBroker);
	for(var i=0; i<number.length; i++)
	{
		if(number[i] == CurrentlyNumber)
		{
			continue;
		}		
		if(Name == Global_RemoteBroker[number[i]]["Name"])
		{
			Return=true;
			break;
		}
	}
	return Return;
}

RemoteBrokerSecondPage.prototype._ClearForm_ = function()
{
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row");
	Row.eq(0).find(".Text").val("");
	Row.eq(1).find(".Text").val("");
	Row.eq(2).find(".Text").val("");
	Row.eq(3).find(".Text").val("");
	Row.eq(4).find(".Text").val("");
	Row.eq(5).find(":checkbox").prop("checked", false);
	Row.eq(6).find(".Text").val("");
	Row.eq(7).find(".Text").val("");
	Row.eq(8).find(".Text").val("");
	Row.eq(9).find(":checkbox").prop("checked", false);
	Row.eq(10).find(".Text").val("");
	Row.eq(11).find(".Text").val("");
	$("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Message").hide();
}

RemoteBrokerSecondPage.prototype._GetSettingFormValue_ = function()
{
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row");
	var number = Row.eq(0).find(".Text").val();
	Global_RemoteBroker[number]["Name"]           = Row.eq(1).find(".Text").val();
	Global_RemoteBroker[number]["IP"]             = Row.eq(2).find(".Text").val();
	Global_RemoteBroker[number]["Port"]           = parseInt(Row.eq(3).find(".Text").val());
	Global_RemoteBroker[number]["Keepalive"]      = parseInt(Row.eq(4).find(".Text").val());
	Global_RemoteBroker[number]["SSL/TLS"]        = Row.eq(5).find(":checkbox").prop("checked");
	Global_RemoteBroker[number]["CaCertFile"]     = Row.eq(6).find(".Select").val();
	Global_RemoteBroker[number]["CertFile"]       = Row.eq(7).find(".Select").val();
	Global_RemoteBroker[number]["KeyFile"]        = Row.eq(8).find(".Select").val();
	Global_RemoteBroker[number]["AnonymousLogin"] = Row.eq(9).find(":checkbox").prop("checked");
	Global_RemoteBroker[number]["Account"]        = Row.eq(10).find(".Text").val();
	Global_RemoteBroker[number]["Password"]       = Row.eq(11).find(".Text").val();
}

RemoteBrokerSecondPage.prototype._SetFormInitialValue_ = function(number)
{
	var Name=Global_RemoteBroker[number]["Name"];
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row");
	Row.eq(0).find(".Text").val(number);
	Row.eq(1).find(".Text").val(Name);
	Row.eq(2).find(".Text").val(Global_RemoteBroker[number]["IP"]);
	Row.eq(3).find(".Text").val(Global_RemoteBroker[number]["Port"]);
	Row.eq(4).find(".Text").val(Global_RemoteBroker[number]["Keepalive"]);
	Row.eq(5).find(":checkbox").prop("checked", Global_RemoteBroker[number]["SSL/TLS"]);
	Row.eq(6).find(".Text").val(Global_RemoteBroker[number]["CaCertFile"]);
	Row.eq(7).find(".Text").val(Global_RemoteBroker[number]["CertFile"]);
	Row.eq(8).find(".Text").val(Global_RemoteBroker[number]["KeyFile"]);
	Row.eq(9).find(":checkbox").prop("checked", Global_RemoteBroker[number]["AnonymousLogin"]);
	Row.eq(10).find(".Text").val(Global_RemoteBroker[number]["Account"]);
	Row.eq(11).find(".Text").val(Global_RemoteBroker[number]["Password"]);
	RemoteBrokerSecondPage.prototype._ChangeAnonymousLogin_();
	RemoteBrokerSecondPage.prototype._ChangeSslTls_();
}

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

RemoteBrokerSecondPage.prototype._ConfirmButton_ = function(that)
{
	var Message = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Message");
	for(var i=0; i<Message.length; i++)
	{
		if(Message.eq(i).is(":visible") == true ){
			$(that).closest(".Form").find(".Message").eq(1).show();
			return 0;
		}
	}
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row");
	var number = Row.eq(0).find(".Text").val();
	var OldDeviceName = Global_RemoteBroker[number]["Name"]
	var NewDeviceName = Row.eq(1).find(".Text").val();
	RemoteBrokerFirstPage.prototype._ModifyConversionTransferGlobalVariable_(OldDeviceName, NewDeviceName);
	RemoteBrokerFirstPage.prototype._ModifyMqttGroupConnectionGlobalVariable_(OldDeviceName, NewDeviceName);
	RemoteBrokerSecondPage.prototype._GetSettingFormValue_();
	$("#Main_Center-Content").find(".Body").eq(1).hide();
	$("#Main_Center-Content").find(".Body").eq(0).show();
	RemoteBrokerFirstPage.prototype._RedisplayUi_();
	var Class = new Path();
	Class.LoadPathHtml($("#Left-List-RemoteBroker").text(), 1, "main_PathRemoteBroker_click(this)");
	Class = null;
}

RemoteBrokerSecondPage.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-RemoteBroker").text(), 1, "main_PathRemoteBroker_click(this)");
	Class = null;
}

RemoteBrokerSecondPage.prototype._ChangeAnonymousLogin_ = function()
{
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row");
	var Bool = Row.eq(9).find(":checkbox").prop("checked");
	if(Bool == true)
	{
		Row.eq(10).hide();
		Row.eq(11).hide();
		Row.eq(10).find(".Text").val("");
		Row.eq(11).find(".Text").val("");
	}
	else
	{
		Row.eq(10).show();
		Row.eq(11).show();
		if(Row.eq(10).find(".Text").val() == "")  {Row.eq(10).find(".Message").show();}
		if(Row.eq(11).find(".Text").val() == "")  {Row.eq(11).find(".Message").show();}
	}
}

RemoteBrokerSecondPage.prototype._ChangeSslTls_ = function()
{
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row");
	var Bool = Row.eq(5).find(":checkbox").prop("checked");
	if(Bool == false)
	{
		Row.eq(6).hide();
		Row.eq(7).hide();
		Row.eq(8).hide();

		Row.eq(6).find(".Select").val("");
		Row.eq(7).find(".Select").val("");
		Row.eq(8).find(".Select").val("");

		Row.eq(6).find(".Message").hide();
		Row.eq(7).find(".Message").hide();
		Row.eq(8).find(".Message").hide();
	}
	else
	{

		Row.eq(6).show();
		Row.eq(7).show();
		Row.eq(8).show();
		RemoteBrokerSecondPage.prototype._ShowCertificateDropDownMenu_();
		//RemoteBrokerSecondPage.prototype._CheckTrustedCertificate_();
		//RemoteBrokerSecondPage.prototype._CheckCertificatePrivateKey_();
	}
}
RemoteBrokerSecondPage.prototype._ShowCertificateDropDownMenu_ = function()
{
	var Result = {"Input":null};
	var Path = "/app/Certificates/MQTT";
	var array = new Array(Path);
	var Class = new Transmission();
	Class.TransmissionArray("MQTTCertificateList", array, Result);
	var SetInterval = setInterval(function()
	{
		if(Result["Input"]!=null)
		{
			Class = null; delete Class;
			clearInterval(SetInterval);
			RemoteBrokerSecondPage.prototype._AddCertificateDropDownMenu_(Result["Input"]);
		}
	},500);
}
RemoteBrokerSecondPage.prototype._AddCertificateDropDownMenu_ = function(CertificateList)
{
	var Row = $("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).find(".Row");
	var number = Row.eq(0).find(".Text").val();
	var CaCertFile = ["CaCertFile", "CertFile","KeyFile"];
	for(var i=0;i<CertificateList.length;i++)
	{
		Row.eq(6 + i).find(".Message").hide();
		Row.eq(6 + i).find(".Select").find("option").remove();	
		for(var j=0;j<CertificateList[i].length;j++)
		{
			Row.eq(6 + i).find(".Select").append($("<option></option>").attr("value",CertificateList[i][j]).text(CertificateList[i][j]));
		}
		Row.eq(6 + i).find(".Select").val(Global_RemoteBroker[number][CaCertFile[i]]);
	}
	RemoteBrokerSecondPage.prototype._CheckTrustedCertificate_();
	RemoteBrokerSecondPage.prototype._CheckCertificatePrivateKey_();
}

RemoteBrokerFirstPage.prototype._ModifyConversionTransferGlobalVariable_ = function(OldDeviceName, NewDeviceName)
{
	var OldDeviceName = OldDeviceName+" (Remote)";
	var NewDeviceName = NewDeviceName+" (Remote)";
	number = Object.keys(Global_RtuToMqttSetting);
	for(var i=0; i<number.length; i++){
		var array = Global_RtuToMqttSetting[number[i]]["MqttConnection"];
		array = array.toString().replace(OldDeviceName, NewDeviceName);
		array = array.split(",");
		Global_RtuToMqttSetting[number[i]]["MqttConnection"] = array;
	}
	
	number = Object.keys(Global_TcpToMqttSetting);
	for(var i=0; i<number.length; i++){
		var array = Global_TcpToMqttSetting[number[i]]["MqttConnection"];
		array = array.toString().replace(OldDeviceName, NewDeviceName);
		array = array.split(",");
		Global_TcpToMqttSetting[number[i]]["MqttConnection"] = array;
	}
	
	number = Object.keys(Global_AsciiToMqttSetting);
	for(var i=0; i<number.length; i++){
		var array = Global_AsciiToMqttSetting[number[i]]["MqttConnection"];
		array = array.toString().replace(OldDeviceName, NewDeviceName);
		array = array.split(",");
		Global_AsciiToMqttSetting[number[i]]["MqttConnection"] = array;
	}
}

RemoteBrokerFirstPage.prototype._ModifyMqttGroupConnectionGlobalVariable_ = function(OldDeviceName, NewDeviceName)
{
	var OldDeviceName = OldDeviceName+" (Remote)";
	var NewDeviceName = NewDeviceName+" (Remote)";
	
	var number = Object.keys(Global_MqttJsonGroup);
	for(var i=0; i<number.length; i++){
		var array = Global_MqttJsonGroup[number[i]]["MqttConnection"];
		array = array.toString().replace(OldDeviceName, NewDeviceName);
		array = array.split(",");
		Global_MqttJsonGroup[number[i]]["MqttConnection"] = array;
	}
}

// 遠端Broker 一般設定
RemoteBrokerSecondPage.prototype._RemoteBrokerGeneralSetting_ = function()
{
	var AllDivButtonName = $("#Main_Center-Content").find(".Body").eq(1).find(".FormTitle");
	AllDivButtonName.css("border-right-color", "#FFFFFF");
	AllDivButtonName.css("border-left-color", "#FFFFFF");
	AllDivButtonName.css("border-top-color", "#FFFFFF");
	AllDivButtonName.css("background-color", "#FFFFFF");
	var DivButtonName = $("#Main_Center-Content").find(".Body").eq(1).find(".FormTitle").eq(0);
	DivButtonName.css("border-right-color", "#395d8b");
	DivButtonName.css("border-left-color", "#395d8b");
	DivButtonName.css("border-top-color", "#395d8b");
	DivButtonName.css("background-color", "#E5F1FF");
	$("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).show();
	$("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(1).show();
	$("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(2).hide();
}

// 遠端Broker 憑證設定
RemoteBrokerSecondPage.prototype._RemoteBrokerCertificateSetting_ = function()
{
	var AllDivButtonName = $("#Main_Center-Content").find(".Body").eq(1).find(".FormTitle");
	AllDivButtonName.css("border-right-color", "#FFFFFF");
	AllDivButtonName.css("border-left-color", "#FFFFFF");
	AllDivButtonName.css("border-top-color", "#FFFFFF");
	AllDivButtonName.css("background-color", "#FFFFFF");
	
	var DivButtonName = $("#Main_Center-Content").find(".Body").eq(1).find(".FormTitle").eq(1);
	DivButtonName.css("border-right-color", "#395d8b");
	DivButtonName.css("border-left-color", "#395d8b");
	DivButtonName.css("border-top-color", "#395d8b");
	DivButtonName.css("background-color", "#E5F1FF");
	$("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(0).hide();
	$("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(1).hide();
	$("#Main_Center-Content").find(".Body").eq(1).find(".Form").eq(2).show();
}