TimeSetting.prototype._ShowUi_ = function()
{
	$("#Calendar").datepicker();
	TimeSetting.prototype._SetCalendar_();
	TimeSetting.prototype._SetTime_();
	TimeSetting.prototype._InitializationManuallySetTime_();
	TimeSetting.prototype._SetNtpData_();
	TimeSetting.prototype._CheckDateError_();
};

TimeSetting.prototype._SetCalendar_ = function()
{
	var ssocket = io.connect();
	ssocket.emit('GetDate');
	ssocket.on("GetDate", function (Return) 
	{
		$("#Calendar").datepicker("setDate", Return);
		ssocket.emit("SocketClose");
	});
};

TimeSetting.prototype._SetTime_ = function()
{
	var ssocket = io.connect();
	var TimeSetInterval = setInterval(function(){
		if(TimeSetting.prototype._IsUsedTimeSetting_() == false)
		{
			ssocket.emit("SocketClose");
			clearInterval(TimeSetInterval);
			return 0;
		}
		ssocket.emit('GetTime');
		ssocket.on("GetTime", function (Return) {
			Return=Return.split(":");
			var Hour=Return[0];
			var Minute=Return[1];
			var Second=Return[2];
			var Text=$("#CalendarTime").find(".Text");
			Text.eq(0).val(Hour);
			Text.eq(1).val(Minute);
			Text.eq(2).val(Second);
		});
	}, 1000);
};

TimeSetting.prototype._IsUsedTimeSetting_ = function()
{
	var Return=true;
	if(!($("#Top-Menu-SystemSetting").css("border-bottom-color") == "rgb(57, 93, 139)" &&
		 $("#Left-List-TimeSetting").css("border-left-color") == "rgb(57, 93, 139)")){
		Return=false;
	}
	return Return;
}

TimeSetting.prototype._InitializationManuallySetTime_ = function()
{
	var Text=$("#Main_Center-Content").find(".Form").eq(3).find(".Row").eq(0).find(".Text");
	var Today=new Date();
	var Year=Today.getFullYear();
	var Month=Today.getMonth()+1;
	var Day=Today.getDate();
	var Hours=Today.getHours();
	var Minutes=Today.getMinutes();
	var Seconds=Today.getSeconds();
	Text.eq(0).val(Year);
	Text.eq(1).val(Month);
	Text.eq(2).val(Day);
	Text.eq(3).val(Hours);
	Text.eq(4).val(Minutes);
	Text.eq(5).val(Seconds);
};

TimeSetting.prototype._SaveManuallySetTime_ = function(that)
{
	$(that).closest('.Row').find(".Message").hide();
	$(that).closest('.Row').find(".Loading").show();
	var array = TimeSetting.prototype._CollectManuallySetTimeData_();
	var ssocket = io.connect();
	ssocket.emit('SaveManuallySetTime', array);
	ssocket.on("SaveManuallySetTime", function (Return) {
		if(Return==true)
		{
			TimeSetting.prototype._SetNtpData_();
			$(that).closest('.Row').find(".Loading").hide();
			$(that).closest('.Row').find(".Message").eq(0).show();
		}
		else
		{
			$(that).closest('.Row').find(".Loading").hide();
			$(that).closest('.Row').find(".Message").eq(1).show();
		}
		ssocket.emit("SocketClose");
	});
};

TimeSetting.prototype._CollectManuallySetTimeData_ = function()
{
	var Text=$("#Main_Center-Content").find(".Form").eq(3).find(".Row").eq(0).find(".Text");
	var Year=Text.eq(0).val();
	var Month=Text.eq(1).val();
	var Day=Text.eq(2).val();
	var Hours=Text.eq(3).val();
	var Minutes=Text.eq(4).val();
	var Seconds=Text.eq(5).val();
	var TimeZone=$("#Main_Center-Content").find(".Form").eq(3).find(".Row").eq(2).find(".Select").val();
	var array=new Array(TimeZone, Year+"-"+Month+"-"+Day+" "+Hours+":"+Minutes+":"+Seconds);
	return array;
};

TimeSetting.prototype._SetNtpData_ = function()
{
	var ssocket = io.connect();
	ssocket.emit('GetNtpData');
	ssocket.on("GetNtpData", function (Return) {
	
	var Row=$("#Main_Center-Content").find(".Form").eq(1).find(".Row");
		if(Return.length == 3)
		{
			TimeSetting.prototype._SetCalendar_();
			TimeSetting.prototype._SetNtpServer_(Return[1], Row);
			TimeSetting.prototype._SetTimeZone_(Return[2], Row);
			TimeSetting.prototype._SetNtpStatus_(Return[0]);
		}
		ssocket.emit("SocketClose");
	});
};

TimeSetting.prototype._SaveNtpSet_ = function(that)
{
	$(that).closest('.Row').find(".Message").hide();
	$(that).closest('.Row').find(".Loading").show();
	var array=TimeSetting.prototype._CollectNtpData_();
	var ssocket = io.connect();
	ssocket.emit('SaveNtpSet', array);
	ssocket.on("SaveNtpSet", function (Return) {
		if(Return==true)
		{
			TimeSetting.prototype._ShowUi_();
			$(that).closest('.Row').find(".Loading").hide();
			$(that).closest('.Row').find(".Message").eq(0).show();
		}
		else
		{
			$(that).closest('.Row').find(".Loading").hide();
			$(that).closest('.Row').find(".Message").eq(1).show();
		}
		ssocket.emit("SocketClose");
	});
};

TimeSetting.prototype._SetNtpStatus_ = function(Status)
{
	if(Status == "no")
	{
		$("#NtpTimeServer").attr("checked", false);
		$("#CustomizeServer").attr("checked", false);
	}
};

TimeSetting.prototype._SetNtpServer_ = function(NtpServer, Row)
{
	var Floag=false;
	var NtpServerItem=Row.eq(1).find("select option");
	var MaxIndex=Row.eq(1).find("select option:last").index();
	for(var i=0; i<=MaxIndex; i++)
	{
		if(NtpServer == NtpServerItem.eq(i).val())
		{
			Floag=true;
			break;
		}
	}
	if(Floag == true)
	{
		Row.eq(1).find(".Select").val(NtpServer);
		$("#NtpTimeServer").click();
	}
	else
	{
		Row.eq(2).find(".Text").val(NtpServer);
		$("#CustomizeServer").click();
	}
};

TimeSetting.prototype._SetTimeZone_ = function(TimeZone, Row)
{
	Row.eq(3).find(".Select").val(TimeZone);
	var ManuallySetTimeZone=$("#Main_Center-Content").find(".Form").eq(3).find(".Row").eq(2).find(".Select");
	ManuallySetTimeZone.val(TimeZone);
}

TimeSetting.prototype._CollectNtpData_ = function()
{
	var Row=$("#Main_Center-Content").find(".Form").eq(1).find(".Row");
	var NtpServer=Row.eq(1).find(".Select").val();
	var SetManuallyServer=Row.eq(2).find(".Text").val();
	var TimeZone=Row.eq(3).find(".Select").val();
	
	var array=new Array();
	array[1]=TimeZone;
	var RunNtpServer=Row.find("input[type=radio]")[0].checked;
	var RunCustomizeServer=Row.find("input[type=radio]")[1].checked;
	if(RunCustomizeServer==true)
	{
		array[0]=SetManuallyServer;
	}
	else if(RunNtpServer==true)
	{
		array[0]=NtpServer;
	}
	return array;
};

TimeSetting.prototype._ChangeRadio_ = function(that)
{
	$(that).closest(".Form").find(".Row").show();
	var RadioId = $(that).attr("id");
	if(RadioId == "NtpTimeServer")
	{
		$(that).closest(".Form").find(".Row").eq(2).hide();
	}
	else if(RadioId == "CustomizeServer")
	{
		$(that).closest(".Form").find(".Row").eq(1).hide();
	}
};

TimeSetting.prototype._CheckDateError_ = function(that)
{

	var ssocket = io.connect();
	ssocket.emit('GetDateTime');
	ssocket.on("GetDateTime", function (Return) 
	{
		var Today=new Date();
		var PC_Time=parseInt(Today.getTime() / 1000);
		var TimeDifference = Math.abs(PC_Time - Return);
		if(TimeDifference > 86280)
		{
			$(".FormTitle").eq(1).css('display', "block");
		}
	});
	/*
	var Today=new Date();
	var PC_Time=Today.getTime();
	var PC_Year=Today.getFullYear();
	var PC_Month=Today.getMonth()+1;
	var PC_Day=Today.getDate();
	var PC_Hours=Today.getHours();
	var PC_Minutes=Today.getMinutes();
	var PC_Seconds=Today.getSeconds();
	var PC_Date = PC_Month + "/" + PC_Day + "/" + PC_Year;
	var CheckDate, CheckTime;
	var ssocket = io.connect();
	var SystemDate, SystemHour;
	ssocket.emit('GetDate');
	ssocket.on("GetDate", function (Return) 
	{
		SystemDate = Return;
		if(PC_Date==SystemDate)
		CheckDate = true;
		//ssocket.emit("SocketClose");
		//var ssocket = io.connect();
		ssocket.emit('GetTime');
		ssocket.on("GetTime", function (Return) {
			Return = Return.split(":");
			SystemHour = Return[0];
			if(PC_Hours==SystemHour)
				CheckTime = true;


		});
		

	});
*/
};
