//更新頁
IftttConditionTriggerUpdatePage.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");
				IftttConditionTriggerUpdatePage.prototype._ShowFirstPage_();
				Class.UnLockAllUi();
			}
		}
		else if(Index<Length)
		{
			ssocket.emit("BackgroundExecution");
		}
	});
}

IftttConditionTriggerUpdatePage.prototype._IsSaveGlobalVariable_ = function()
{
	var Return=false;
	var Class = new Judgment();
	if(Class.IsSaveRtuMasterModuleGlobalVariable() == true &&
	   Class.IsSaveTcpMasterModuleGlobalVariable() == true &&
	   Class.IsSaveAsciiMasterModuleGlobalVariable() == true)
	{
		Return=true;
	}
	return Return;
}

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

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

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




















//第1頁
IftttConditionTriggerFirstPage.prototype._ShowStatus_ = function(Column, Enable)
{
	Column.find(".Label").hide();
	if(Enable == true)
	{
		Column.find(".Label").eq(0).show();
	}
	else if(Enable == false)
	{
		Column.find(".Label").eq(1).show();
	}
}

IftttConditionTriggerFirstPage.prototype._ShowTable_ = function(number, DisplayNumber)
{
	var TableId="#EventTable";    var GlobalVariable=Global_IftttConditionTriggerSetting;
	var html = $(".Dynamic").html();
	var PageNumber = $("#Toolbar").find(".Text").val();
	var Start = (PageNumber-1)*DisplayNumber;
	$(TableId).find(".DynamicRow").remove();
	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++)
	{
		$(TableId).append(html);
		$(TableId).find(".DynamicRow").eq(index).find(".Column").eq(0).text(number[i]);
		$(TableId).find(".DynamicRow").eq(index).find(".Column").eq(2).text(GlobalVariable[number[i]]["EventName"]);
		$(TableId).find(".DynamicRow").eq(index).find(".Column").eq(3).text(GlobalVariable[number[i]]["Key"]);
		var Column=$(TableId).find(".DynamicRow").eq(index).find(".Column").eq(5);
		var Enable=GlobalVariable[number[i]]["Enable"];
		IftttConditionTriggerFirstPage.prototype._ShowStatus_(Column, Enable);
		index++;
	}
}

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

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

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

IftttConditionTriggerFirstPage.prototype._RedisplayUi_ = function()
{
	var GlobalVariable=Global_IftttConditionTriggerSetting;
	var DisplayNumber = 10;
	var number = Object.keys(GlobalVariable);
	IftttConditionTriggerFirstPage.prototype._UpdateTableToolbar_(number, DisplayNumber);
	IftttConditionTriggerFirstPage.prototype._ShowTable_(number, DisplayNumber);
	var Table=$("#Main_Center-Content").find(".Body").eq(1).find(".Table").eq(0);
	Table.find(".Row").eq(0).find(":checkbox").prop("checked",false);
}

IftttConditionTriggerFirstPage.prototype._RemoveGlobalVariable_ = function(number)
{
	Global_IftttConditionTriggerSetting[number]  = null;    delete Global_IftttConditionTriggerSetting[number];
	Global_IftttConditionTriggerVariable[number] = null;    delete Global_IftttConditionTriggerVariable[number];
}

IftttConditionTriggerFirstPage.prototype._RemoveTickedEvent_ = function()
{
	var Id = "#EventTable";
	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();
		IftttConditionTriggerFirstPage.prototype._RemoveGlobalVariable_(number);
	}
}

IftttConditionTriggerFirstPage.prototype._DeleteEvent_ = function()
{
	IftttConditionTriggerFirstPage.prototype._RemoveTickedEvent_();
	IftttConditionTriggerFirstPage.prototype._RedisplayUi_();
}

IftttConditionTriggerFirstPage.prototype._IsRowDataCorrect_ = function()
{
	var Return=true;
	var Id = "#EventTable";
	var MessageError=$(Id).find(".Row").eq(0).find(".Message").is(":visible");
	if(MessageError == true)
	{
		Return=false;
	}
	return Return;
}

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

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

IftttConditionTriggerFirstPage.prototype._SaveStart_ = function(that, ssocket)
{
	ssocket.emit("BackgroundExecution");
	ssocket.on("BackgroundExecution", function (Return) {
	if(Return == true)
	{
		var Class=new Transmission();
		var Callback0 = function(){IftttConditionTriggerFirstPage.prototype._SaveFinished_(that, ssocket);}
		
		var JsonString1 = JSON.stringify(Global_IftttConditionTriggerSetting);
		var Callback1=function(){Class.TransmissionJson(ssocket, "SaveIftttConditionTriggerSetting", JsonString1, Callback0);}
		
		var JsonString2 = JSON.stringify(Global_IftttConditionTriggerVariable);
		$(that).closest('.Row').find(".Message").hide();
		$(that).closest('.Row').find(".Loading").show();
		Class.TransmissionJson(ssocket, "SaveIftttConditionTriggerVariable", JsonString2, Callback1);
	}
	});
}

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

IftttConditionTriggerFirstPage.prototype._AddNewObject_ = function()
{
	var number = Object.keys(Global_IftttConditionTriggerVariable);
	if(number.length == 0){
		number=1;
	}
	else if(number.length>0){
		number = Math.max.apply(null,number)+1;
	}
	$("#Main_Center-Content").find(".Body").eq(2).find(".FormTitle").eq(0).text();
	$("#Main_Center-Content").find(".Body").eq(2).find(".Message").hide();
	$("#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;
	IftttConditionTriggerSecondPage.prototype._Initialization_(number);
}

IftttConditionTriggerFirstPage.prototype._EditButton_ = function(that)
{
	$("#Main_Center-Content").find(".Body").eq(2).find(".Message").hide();
	var number = $(that).closest(".DynamicRow").find(".Column").eq(0).text();
	$("#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;
	IftttConditionTriggerSecondPage.prototype._Initialization_(number);
}

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




















//第2頁
IftttConditionTriggerSecondPage.prototype._SetContentFormValue_ = function(number)
{
	var Variable, EventName, Key, Enable;
	var Form=$("#Main_Center-Content").find(".Body").eq(2).find(".Form");
	if(typeof(Global_IftttConditionTriggerSetting[number]) != "object")
	{
		EventName="";
		Key="";
		Enable=true;
	}
	else if(typeof(Global_IftttConditionTriggerSetting[number]) == "object")
	{
		Variable=Global_IftttConditionTriggerSetting;	
		EventName=Variable[number]["EventName"];
		Key=Variable[number]["Key"];
		Enable=Variable[number]["Enable"];
	}
	Form.eq(0).find(".Row").eq(0).find(".Text").val(number);
	Form.eq(0).find(".Row").eq(1).find(".Text").val(EventName);
	Form.eq(0).find(".Row").eq(2).find(".Text").val(Key);
	Form.eq(0).find(".Row").eq(3).find(":checkbox").prop("checked",Enable);
}

IftttConditionTriggerSecondPage.prototype._SetConditionTableValue_ = function()
{
	var DynamicRow=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(1).find(".DynamicRow");
	DynamicRow.remove();
	
	var Index=$("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0).find(".Row").eq(0).find(".Text").val();
	var Variable=Global_IftttConditionTriggerVariable;
	
	if(typeof(Variable[Index]) != "object")    {return 0;}
	
	var Length=Variable[Index]["IndexValue"].length;
	for(var i=0; i<Length; i++)
	{
		var object=new Object();
		object["ModuleType"]   =Variable[Index]["IndexValue"][i].split("/")[0];
		object["ModuleIndex"]  =Variable[Index]["IndexValue"][i].split("/")[1];
		object["VariableIndex"]=Variable[Index]["IndexValue"][i].split("/")[2];
		object["Condition"]    =Variable[Index]["Condition"][i];
		object["DefineMessage"]=Variable[Index]["DefineMessage"][i];
		IftttConditionTriggerSecondPage.prototype._AddConditionRowHtml_();
		IftttConditionTriggerSecondPage.prototype._SetConditionRowValue_(object);
	}
}

IftttConditionTriggerSecondPage.prototype._SetConditionRowValue_ = function(object)
{
	var ModuleType=object["ModuleType"];
	var ModuleIndex=object["ModuleIndex"];
	var VariableIndex=object["VariableIndex"];
	var Condition=object["Condition"];
	var DefineMessage=object["DefineMessage"];
	
	var Body=$("#Main_Center-Content").find(".Body").eq(2);
	var Table=Body.find(".Table").eq(1);
	var DynamicRow=Table.find(".DynamicRow");
	var LastDynamicRow=DynamicRow.eq(DynamicRow.length-1);

	var Column1=LastDynamicRow.find(".Column").eq(1);
	var Column2=LastDynamicRow.find(".Column").eq(2);
	var Column3=LastDynamicRow.find(".Column").eq(3);
	var Column4=LastDynamicRow.find(".Column").eq(4);

	IftttConditionTriggerSecondPage.prototype._SetModuleColumnValue_(ModuleType, ModuleIndex, Column1);
	IftttConditionTriggerSecondPage.prototype._SetVariableColumnValue_(ModuleType, ModuleIndex, VariableIndex, Column2);
	IftttConditionTriggerSecondPage.prototype._SetConditionColumnValue_(VariableIndex, Condition, Column3);
	IftttConditionTriggerSecondPage.prototype._SetDefineMessageColumnValue_(DefineMessage, Column4);
}

IftttConditionTriggerSecondPage.prototype._SetModuleColumnValue_ = function(ModuleType, ModuleIndex, Column1)
{
	var ModuleTypeText=Column1.find(".Text").eq(0);
	var ModuleNameText=Column1.find(".Text").eq(1);

	var Class=new GetValue();
	var GlobalModule=Class.GetGlobalModule(ModuleType);
	
	var ModuleType_Text=IftttConditionTriggerSecondPage.prototype._GetConditionSettingModuleTypeText_(ModuleType);
	$(ModuleTypeText).attr("ModuleType",ModuleType);
	$(ModuleTypeText).val(ModuleType_Text);

	var ModuleName=Class.GetModuleName(GlobalModule, ModuleIndex);
	ModuleName="No."+ModuleIndex+" "+ModuleName;
	$(ModuleNameText).attr("ModuleIndex",ModuleIndex);
	$(ModuleNameText).val(ModuleName);
}

IftttConditionTriggerSecondPage.prototype._SetVariableColumnValue_ = function(ModuleType, ModuleIndex, VariableIndex, Column2)
{	
	var VariableNameText=Column2.find(".Text").eq(0);
	var VariableAttributeText=Column2.find(".Text").eq(1);
	var VariableDataTypeText=Column2.find(".Text").eq(2);

	var Class=new GetValue();
	var GlobalVariable=Class.GetGlobalVariable(ModuleType);

	var VariableName=Class.GetModuleVariableName(GlobalVariable, ModuleIndex, VariableIndex);
	$(VariableNameText).attr("VariableIndex",VariableIndex);
	VariableNameText.val(VariableName);
	
	var Attribute=VariableIndex.split("-")[0];
	var ReadWrite=GetValue.prototype._GetModuleVariableReadWrite_(Attribute);
	var VariableAttribute=IftttConditionTriggerSecondPage.prototype._GetConditionSettingVariableAttributeText_(ReadWrite)
	VariableAttributeText.val(VariableAttribute);
	VariableDataTypeText.val(VariableIndex.split("-")[1]);
}

IftttConditionTriggerSecondPage.prototype._SetConditionColumnValue_ = function(VariableIndex, Condition, Column3)
{
	var ConditionText=Column3.find(".Text").eq(0);
	var Signal=VariableIndex.split("-")[0];
	if(Signal == "DI" || Signal == "DO")
	{
		$(ConditionText).attr("Condition",Condition);
		var Status=IftttConditionTriggerSecondPage.prototype._GetConditionSettingDigitalStatusText_(Condition);
		$(ConditionText).val(Status);
	}
	else if(Signal == "AI" || Signal == "AO")
	{
		$(ConditionText).attr("Condition",Condition);
		$(ConditionText).val(Condition);
	}
}

IftttConditionTriggerSecondPage.prototype._SetDefineMessageColumnValue_ = function(DefineMessage, Column4)
{
	var DefineMessageText=Column4.find(".Text").eq(0);
	$(DefineMessageText).val(DefineMessage);
}

IftttConditionTriggerSecondPage.prototype._Initialization_ = function(number)
{
	IftttConditionTriggerSecondPage.prototype._LoadModuleType_();
	IftttConditionTriggerSecondPage.prototype._LoadModuleName_();
	IftttConditionTriggerSecondPage.prototype._LoadModuleVariable_();
	IftttConditionTriggerSecondPage.prototype._SwitchFormula_();
	IftttConditionTriggerSecondPage.prototype._SetContentFormValue_(number);
	IftttConditionTriggerSecondPage.prototype._SetConditionTableValue_();
	IftttConditionTriggerSecondPage.prototype._ShowFormErrorMessage_();
	IftttConditionTriggerSecondPage.prototype._ShowTableErrorMessage_();
	var Deadband = $("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0).find(".Row").eq(1).find(".Column").eq(2).find(".Text");
	Deadband.val(1);
}

IftttConditionTriggerSecondPage.prototype._LoadModuleType_ = function()
{
	var Class=new LoadFile();
	var ModuleType = Class.ReadFile("./File/IFTTT_Notify/ModuleType.json");
	var Value = ModuleType["Value"];
	var Text = ModuleType["Text"];
	var Table = $("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0);
	
	var ModuleTypeSelect = Table.find("tr").eq(0).find(".Select");
	ModuleTypeSelect.empty();
	for(var i=0; i<Value.length; i++)
	{
		ModuleTypeSelect.append($("<option></option>").attr("value",Value[i]).text(Text[i]));
	}
}

IftttConditionTriggerSecondPage.prototype._LoadModuleName_ = function()
{
	var Class=new GetValue(), ModuleName, CombinationModuleName=new Array();
	var Table = $("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0);
	var ModuleTypeSelect = Table.find("tr").eq(0).find(".Select");
	var ModuleNameSelect = Table.find("tr").eq(1).find(".Select");
	var ModuleType=ModuleTypeSelect.val();
	var GlobalModule=Class.GetGlobalModule(ModuleType);
	var ModuleNumber=Class.GetModuleAllNumber(GlobalModule);
	ModuleNameSelect.empty();
	for(var i=0; i<ModuleNumber.length; i++)
	{
		ModuleName=Class.GetModuleName(GlobalModule, ModuleNumber[i]);
		CombinationModuleName.push("No."+ModuleNumber[i]+" "+ModuleName);
	}
	if(CombinationModuleName.length == 0)
	{
		return 0;
	}
	for(var i=0; i<CombinationModuleName.length; i++)
	{
		ModuleNameSelect.append($("<option></option>").attr("value",ModuleNumber[i]).text(CombinationModuleName[i]));
	}
}

IftttConditionTriggerSecondPage.prototype._LoadModuleVariable_ = function()
{
	var Class=new GetValue(), ModuleName, CombinationModuleName=new Array();
	var Table = $("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0);
	var ModuleType = Table.find("tr").eq(0).find(".Select").val();
	var ModuleIndex = Table.find("tr").eq(1).find(".Select").val();
	var ReadWrite  = Table.find("tr").eq(2).find(".Select").val();
	var VariableNameSelect=Table.find("tr").eq(3).find(".Select");
	VariableNameSelect.empty();
	var AllVariableIndex=Class.GetModuleAllVariableIndex(ModuleType, ModuleIndex);
	AllVariableIndex=IftttConditionTriggerSecondPage.prototype._FilterVariableArray_(ReadWrite, AllVariableIndex);
	var VariableName=Class.GetModuleAllVariableName(ModuleType, ModuleIndex, AllVariableIndex);
	for(var i=0; i<AllVariableIndex.length; i++)
	{
		var CombinationVariableName=VariableName[i]+" ("+IftttConditionTriggerSecondPage.prototype._ShortenDataTypeName_(AllVariableIndex[i].split("-")[1])+")";
		VariableNameSelect.append($("<option></option>").attr("value",AllVariableIndex[i]).text(CombinationVariableName));
	}
}

IftttConditionTriggerSecondPage.prototype._FilterVariableArray_ = function(ReadWrite, VariableArray)
{
	for(var i=0; i<VariableArray.length; i++){
		if(ReadWrite=="Read")
		{
			if(!(VariableArray[i].split("-")[0].indexOf("DI") != -1 ||
				 VariableArray[i].split("-")[0].indexOf("AI") != -1))
			{
				VariableArray.splice(i,1);	i--;
			}
		}
		else if(ReadWrite=="ReadWrite")
		{
			if(!(VariableArray[i].split("-")[0].indexOf("DO") != -1 ||
				 VariableArray[i].split("-")[0].indexOf("AO") != -1))
			{
				VariableArray.splice(i,1);	i--;
			}
		}
		else if(ReadWrite=="Write")
		{
			VariableArray.splice(i,1);	i--;
		}
	}
	return VariableArray;
}

IftttConditionTriggerSecondPage.prototype._ShowAllFormulaColumn_ = function()
{
	var Table=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0);
	var TitleRow=Table.find(".Row").eq(0);
	var Row=Table.find(".Row").eq(1);
	
	TitleRow.find(".TitleColumn").show();
	Row.find(".Column").show();
}

IftttConditionTriggerSecondPage.prototype._ShowDigitalFormulaColumn_ = function()
{
	var Table=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0);
	var TitleRow=Table.find(".Row").eq(0);
	var Row=Table.find(".Row").eq(1);

	TitleRow.find(".TitleColumn").hide();
	TitleRow.find(".TitleColumn").eq(0).show();
	TitleRow.find(".TitleColumn").eq(3).show();
	Row.find(".Column").hide();
	Row.find(".Column").eq(0).show();
	Row.find(".Column").eq(3).show();
}

IftttConditionTriggerSecondPage.prototype._ShowAnalogFormulaColumn_ = function()
{
	var Table=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0);
	var TitleRow=Table.find(".Row").eq(0);
	var Row=Table.find(".Row").eq(1);

	TitleRow.find(".TitleColumn").hide();
	TitleRow.find(".TitleColumn").eq(0).show();
	TitleRow.find(".TitleColumn").eq(2).show();
	Row.find(".Column").hide();
	Row.find(".Column").eq(0).show();
	Row.find(".Column").eq(2).show();
}

IftttConditionTriggerSecondPage.prototype._ShowFormErrorMessage_ = function()
{	
	var Form=$("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0);
	Form.find(".Message").hide();
	var EventNameText=Form.find(".Row").eq(1).find(".Text");
	var KeyText=Form.find(".Row").eq(2).find(".Text");
	
	var EventName=EventNameText.val();
	var Key=KeyText.val();

	if(EventName=="") {EventNameText.closest(".Row").find(".Message").show();}
	if(Key=="")       {KeyText.closest(".Row").find(".Message").show();}
}

IftttConditionTriggerSecondPage.prototype._ShowTableErrorMessage_ = function()
{		
	var Table=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0);
	Table.find(".Message").hide();
	var ModuleTypeSelect       =Table.find(".Row").eq(1).find("tr").eq(0).find(".Select");
	var ModuleNameSelect       =Table.find(".Row").eq(1).find("tr").eq(1).find(".Select");
	var VariableAttributeSelect=Table.find(".Row").eq(1).find("tr").eq(2).find(".Select");
	var VariableNameSelect     =Table.find(".Row").eq(1).find("tr").eq(3).find(".Select");

	var ModuleType       =ModuleTypeSelect.val();
	var ModuleIndex      =ModuleNameSelect.val();
	var VariableAttribute=VariableAttributeSelect.val();
	var VariableIndex    =VariableNameSelect.val();

	if(ModuleType=="" || ModuleType==null)               {ModuleTypeSelect.closest("td").find(".Message").show();}
	if(ModuleIndex=="" || ModuleIndex==null)             {ModuleNameSelect.closest("td").find(".Message").show();}
	if(VariableAttribute=="" || VariableAttribute==null) {VariableAttributeSelect.closest("td").find(".Message").show();}
	if(VariableIndex=="" || VariableIndex==null)         {VariableNameSelect.closest("td").find(".Message").show();}
}

IftttConditionTriggerSecondPage.prototype._SwitchFormula_ = function()
{
	IftttConditionTriggerSecondPage.prototype._ShowAllFormulaColumn_();
	
	var Table=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0);
	var AnalogDigital=Table.find("tr").eq(3).find(".Select").val();
	
	if(AnalogDigital ==null || AnalogDigital == "")    {return 0;}
	
	AnalogDigital=AnalogDigital.split("-")[0];

	if(AnalogDigital == "DO" || AnalogDigital == "DI")
	{
		IftttConditionTriggerSecondPage.prototype._ShowDigitalFormulaColumn_();
	}
	else if(AnalogDigital == "AO" || AnalogDigital == "AI")
	{
		IftttConditionTriggerSecondPage.prototype._ShowAnalogFormulaColumn_();
	}
}

IftttConditionTriggerSecondPage.prototype._ShortenDataTypeName_ = function(DataType)
{
	var Return;
	if(DataType == "Unsigned Short")		{Return="UShort";}
	else if(DataType == "Unsigned Long")	{Return="ULong";}
	else if(DataType == "Unsigned Int64")	{Return="UInt64";}
	else									{Return=DataType;}
	return Return;
}

IftttConditionTriggerSecondPage.prototype._ChangeModuleType_ = function()
{
	IftttConditionTriggerSecondPage.prototype._LoadModuleName_();
	IftttConditionTriggerSecondPage.prototype._LoadModuleVariable_();
	IftttConditionTriggerSecondPage.prototype._SwitchFormula_();
	IftttConditionTriggerSecondPage.prototype._ShowTableErrorMessage_();
}

IftttConditionTriggerSecondPage.prototype._ChangeModuleName_ = function()
{
	IftttConditionTriggerSecondPage.prototype._LoadModuleVariable_();
	IftttConditionTriggerSecondPage.prototype._SwitchFormula_();
	IftttConditionTriggerSecondPage.prototype._ShowTableErrorMessage_();
}

IftttConditionTriggerSecondPage.prototype._ChangeVariableAttribute_ = function()
{
	IftttConditionTriggerSecondPage.prototype._LoadModuleVariable_();
	IftttConditionTriggerSecondPage.prototype._SwitchFormula_();
	IftttConditionTriggerSecondPage.prototype._ShowTableErrorMessage_();
}

IftttConditionTriggerSecondPage.prototype._ChangeVariableName_ = function()
{
	IftttConditionTriggerSecondPage.prototype._SwitchFormula_();
	IftttConditionTriggerSecondPage.prototype._ShowTableErrorMessage_();
}

IftttConditionTriggerSecondPage.prototype._IsRowDataCorrect_ = function()
{
	var Return=true;
	var Table=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0);
	var MessageError=Table.find(".Message").is(":visible");
	if(MessageError == true)
	{
		Return=false;
	}
	return Return;
}

IftttConditionTriggerSecondPage.prototype._IsContentSettingDataCorrect_ = function()
{
	var Return=true;
	var Form=$("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0);
	var MessageError=Form.find(".Message").is(":visible");
	if(MessageError == true)
	{
		Return=false;
	}
	return Return;
}

IftttConditionTriggerSecondPage.prototype._IsConditionTableDataCorrect_ = function()
{
	var Return=true;
	var TableError=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(1).find(".Message").is(":visible");
	if(TableError == true)
	{
		Return=false;
	}
	return Return;
}

IftttConditionTriggerSecondPage.prototype._IsTotalDataCorrect_ = function()
{
	var Return=true;
	var ConditionTableDataCorrect=IftttConditionTriggerSecondPage.prototype._IsConditionTableDataCorrect_();
	var ContentSettingDataCorrect=IftttConditionTriggerSecondPage.prototype._IsContentSettingDataCorrect_();
	if(ContentSettingDataCorrect == false || ConditionTableDataCorrect == false)
	{
		Return=false;
	}
	return Return;
}

IftttConditionTriggerSecondPage.prototype._CheckDefineMessage_ = function(that)
{
	var Message=$(that).closest(".Column").find(".Message");
	Message.hide();
	var DefineMessage=$(that).val();
	var Class = new Type();
	if(DefineMessage == "")
	{
		Message.show();
	}
	else if(Class.CheckChineseLanguage(DefineMessage) == true)
	{
		Message.show();
	}
}

IftttConditionTriggerSecondPage.prototype._CheckEventName_ = function(that)
{
	var Message=$(that).closest(".Row").find(".Message");
	Message.hide();
	var EventName=$(that).val();
	var Class = new Type();
	if(EventName == "")
	{
		Message.show();
	}
}

IftttConditionTriggerSecondPage.prototype._CheckKey_ = function(that)
{
	var Message=$(that).closest(".Row").find(".Message");
	Message.hide();
	var Key=$(that).val();
	var Class = new Type();
	if(Key == "")
	{
		Message.show();
	}
}

IftttConditionTriggerSecondPage.prototype._CheckDeadband_ = function(that)
{
	var DeadBand=$(that).val();
	DeadBand = parseFloat(DeadBand);
	var Class = new Type();
	if(Class.CheckFloat(DeadBand) == false)
	{
		$(that).val(0);
	}
	else if(DeadBand < 0)
	{
		$(that).val(0);
	}
}

IftttConditionTriggerSecondPage.prototype._BuildNewIftttConditionTriggerSettingObject_ = function()
{
	var Form  =$("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0);
	var number=Form.find(".Row").eq(0).find(".Text").val();
	if(typeof(Global_IftttConditionTriggerSetting[number]) == "undefined")
	{
		Global_IftttConditionTriggerSetting[number] = new Object();
		Global_IftttConditionTriggerSetting[number]["EventName"] = "";
		Global_IftttConditionTriggerSetting[number]["Key"] = "";
		Global_IftttConditionTriggerSetting[number]["Enable"] = true;
	}
}

IftttConditionTriggerSecondPage.prototype._BuildNewIftttConditionTriggerVariableObject_ = function()
{
	var Form  =$("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0);
	var number=Form.find(".Row").eq(0).find(".Text").val();
	if(typeof(Global_IftttConditionTriggerVariable[number]) == "undefined")
	{
		Global_IftttConditionTriggerVariable[number] = new Object();
		Global_IftttConditionTriggerVariable[number]["IndexValue"] = new Array();
//		Global_IftttConditionTriggerVariable[number]["RuntimeVariableName"] = new Array();
		Global_IftttConditionTriggerVariable[number]["Condition"] = new Array();
		Global_IftttConditionTriggerVariable[number]["DefineMessage"] = new Array();
	}
}

IftttConditionTriggerSecondPage.prototype._AddNewRow_ = function(that)
{
	$(that).closest(".Row").find(".Message").hide();
	if(IftttConditionTriggerSecondPage.prototype._IsRowDataCorrect_() == false)
	{
		$(that).closest(".Row").find(".Message").show();
		return 0;
	}
	var Table=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0);
	var ModuleTypeSelect       =Table.find(".Row").eq(1).find("tr").eq(0).find(".Select");
	var ModuleNameSelect       =Table.find(".Row").eq(1).find("tr").eq(1).find(".Select");
	var VariableAttributeSelect=Table.find(".Row").eq(1).find("tr").eq(2).find(".Select");
	var VariableNameSelect     =Table.find(".Row").eq(1).find("tr").eq(3).find(".Select");
	var object=new Object();
	object["ModuleType"]       =ModuleTypeSelect.val();
	object["ModuleIndex"]      =ModuleNameSelect.val();
	object["VariableIndex"]    =VariableNameSelect.val();
	
	var Signal=object["VariableIndex"].split("-")[0];
	object["Condition"]=IftttConditionTriggerSecondPage.prototype._GetCondition_(Signal);
	var Class=new GetValue();
	object["DefineMessage"]=Class.GetRuntimeVariableName(object["ModuleType"], object["ModuleIndex"], object["VariableIndex"]);
	IftttConditionTriggerSecondPage.prototype._AddConditionRowHtml_();
	IftttConditionTriggerSecondPage.prototype._SetConditionRowValue_(object);
}

IftttConditionTriggerSecondPage.prototype._AddConditionRowHtml_ = function()
{
	var Html;
	var Body=$("#Main_Center-Content").find(".Body").eq(2);
	var Table=Body.find(".Table").eq(1);
	Html = Body.find(".Dynamic").html();
	Table.append(Html);
}

IftttConditionTriggerSecondPage.prototype._RemoveRow_ = function(that)
{
	$(that).closest(".Row").find(".Message").hide();
	var Table=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(1);
	var Element=$(Table).find(".DynamicRow").find(":checked");
	
	if(Element.length == 0)
	{
		$(that).closest(".Row").find(".Message").show();
		return 0;
	}
	Element.closest(".DynamicRow").remove();
	Table.find(".Row").eq(0).find(":checkbox").prop("checked",false);
}

IftttConditionTriggerSecondPage.prototype._GetCondition_ = function(Signal)
{
	var Return;
	var Row=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0).find(".Row").eq(1);
	var AnalogDeadband =parseFloat(Row.find(".Column").eq(2).find(".Text").eq(0).val());
	var DigitalStatus  =Row.find(".Column").eq(3).find(".Select").val();
	if(Signal=="DI" || Signal=="DO")
	{
		Return=DigitalStatus;
	}
	else if(Signal=="AI" || Signal=="AO")
	{
		Return="Deadband="+AnalogDeadband;
	}
	return Return;
}

IftttConditionTriggerSecondPage.prototype._GetConditionSettingModuleTypeText_ = function(ModuleType)
{
	var Return;
	var Table=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0);
	var ModuleTypeSelect=Table.find(".Row").eq(1).find("tr").eq(0).find(".Select");
	var Condition='option[value="'+ModuleType+'"]';
	Return=ModuleTypeSelect.find(Condition).text();
	return Return;
}

IftttConditionTriggerSecondPage.prototype._GetConditionSettingVariableAttributeText_ = function(ReadWrite)
{
	var Return;
	var Table=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0);
	var VariableAttributeSelect=Table.find(".Row").eq(1).find("tr").eq(2).find(".Select");
	var Condition='option[value="'+ReadWrite+'"]';
	Return=VariableAttributeSelect.find(Condition).text();
	return Return;
}

IftttConditionTriggerSecondPage.prototype._GetConditionSettingDigitalStatusText_ = function(Status)
{
	var Return;
	var Table=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(0);
	var DigitalStatusSelect=Table.find(".Row").eq(1).find(".Column").eq(3).find(".Select");
	var Condition='option[value="'+Status+'"]';
	Return=DigitalStatusSelect.find(Condition).text();
	return Return;
}

IftttConditionTriggerSecondPage.prototype._ModifyIftttConditionTriggerSettingObject_ = function()
{
	var Form= $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0);
	var Index=Form.find(".Row").eq(0).find(".Text").val();
	var EventName=Form.find(".Row").eq(1).find(".Text").val();
	var Key=Form.find(".Row").eq(2).find(".Text").val();
	var Enable=Form.find(".Row").eq(3).find(":checkbox").prop("checked");
	
	Global_IftttConditionTriggerSetting[Index]["EventName"] = EventName;
	Global_IftttConditionTriggerSetting[Index]["Key"] = Key;
	Global_IftttConditionTriggerSetting[Index]["Enable"] = Enable;
}

IftttConditionTriggerSecondPage.prototype._ModifyIftttConditionTriggerVariableObject_ = function()
{
//	var IndexValueArray=new Array(), RuntimeVariableNameArray=new Array(), ConditionArray=new Array(), DefineMessageArray=new Array(), Class=new GetValue();
	var IndexValueArray=new Array(), ConditionArray=new Array(), DefineMessageArray=new Array(), Class=new GetValue();
	var Form= $("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0);
	var Index=Form.find(".Row").eq(0).find(".Text").val();
	var Table=$("#Main_Center-Content").find(".Body").eq(2).find(".Table").eq(1);
	var DynamicRow=Table.find(".DynamicRow");
	for(var i=0; i<DynamicRow.length; i++)
	{
		var ModuleType   =DynamicRow.eq(i).find(".Column").eq(1).find(".Text").eq(0).attr("ModuleType");
		var ModuleIndex  =DynamicRow.eq(i).find(".Column").eq(1).find(".Text").eq(1).attr("ModuleIndex");
		var VariableIndex=DynamicRow.eq(i).find(".Column").eq(2).find(".Text").eq(0).attr("VariableIndex");
		var Condition    =DynamicRow.eq(i).find(".Column").eq(3).find(".Text").eq(0).attr("Condition");
		var DefineMessage=DynamicRow.eq(i).find(".Column").eq(4).find(".Text").eq(0).val();
		var IndexValue   =ModuleType+"/"+ModuleIndex+"/"+VariableIndex;
		IndexValueArray.push(IndexValue);
//		RuntimeVariableNameArray.push(Class.GetRuntimeVariableName(ModuleType, ModuleIndex, VariableIndex));
		ConditionArray.push(Condition);
		DefineMessageArray.push(DefineMessage);
	}
	Global_IftttConditionTriggerVariable[Index]["IndexValue"] = IndexValueArray;
//	Global_IftttConditionTriggerVariable[Index]["RuntimeVariableName"] = RuntimeVariableNameArray;
	Global_IftttConditionTriggerVariable[Index]["Condition"] = ConditionArray;
	Global_IftttConditionTriggerVariable[Index]["DefineMessage"] = DefineMessageArray;
}

IftttConditionTriggerSecondPage.prototype._ConfirmButton_ = function(that)
{
	$(that).closest(".Form").find(".Message").hide();
	if(IftttConditionTriggerSecondPage.prototype._IsTotalDataCorrect_() == false)
	{
		$(that).closest(".Form").find(".Message").show()
		return 0;
	}
	IftttConditionTriggerSecondPage.prototype._BuildNewIftttConditionTriggerSettingObject_();
	IftttConditionTriggerSecondPage.prototype._BuildNewIftttConditionTriggerVariableObject_();
	IftttConditionTriggerSecondPage.prototype._ModifyIftttConditionTriggerSettingObject_();
	IftttConditionTriggerSecondPage.prototype._ModifyIftttConditionTriggerVariableObject_();
	IftttConditionTriggerSecondPage.prototype._NoConfirmButton_();
	IftttConditionTriggerFirstPage.prototype._RedisplayUi_();
}

IftttConditionTriggerSecondPage.prototype._NoConfirmButton_ = function()
{
	var number=$("#Main_Center-Content").find(".Body").eq(2).find(".Form").eq(0).find(".Row").eq(0).find(".Text").val();
	$("#Main_Center-Content").find(".Body").eq(2).hide();
	$("#Main_Center-Content").find(".Body").eq(1).show();
	var Class = new Path();
	Class.LoadPathHtml($("#Left-List-IftttConditionTrigger").text(), 1, "main_PathIftttConditionTrigger_click(this)");
	Class = null;
}