var ENGLISH = 0;
var CHINESE = 1;
var logTypeEn = ["Error", "Alarm", "User Operation"];
var logTypeCn = ["错误", "告警", "用户操作"];
var logType = [logTypeEn, logTypeCn];

var totalPageNum;
var rowNum;
var curPage = 1;

var FIRST = 1;
var PREV = 2;
var NEXT = 3;
var LAST = 4;
var PAGE = 5;

$(function(){
	$("#startTime").datepick({
		 maxDate: 0 ,  
	        dateFormat:'yyyy-mm-dd',
	        showSpeed:'fast'
	});
	$("#endTime").datepick({
	 maxDate: 0 ,  
	        dateFormat:'yyyy-mm-dd',
	        showSpeed:'fast'
	});
	});

var option = {
				minWidth: 250,
				minHeight: 100,
				close: false
			}
$(document).ready(function()
{
 	GetQuery();
});

function Assignment(data)
{
	$("#TotalPageNum").html(totalPageNum);
	if(rowNum == 0)
	{
		$("#CurrentPageNum").html(0);
		$("#PromptInfo").html(data);
		$("#PromptInfo").modal(option);
		setTimeout("$.modal.close()", 2000);
		$("#Operation").val("0");
	}
	else
	{
		$("#CurrentPageNum").html(curPage);
	}
	if(curPage == 1)
	{
		$("#first").attr("disabled", true);
		$("#prev").attr("disabled", true);
	}
	else
	{
		$("#first").attr("disabled", false);
		$("#prev").attr("disabled", false);
	}
	if((curPage ==  totalPageNum) || (totalPageNum == 0))
	{
		$("#next").attr("disabled", true);
		$("#last").attr("disabled", true);	
	}
	else
	{
		$("#next").attr("disabled", false);
		$("#last").attr("disabled", false);
	}
	for(var i = 0; i < rowNum; i++)
	{
		var vTr = $("#table_info_alarm").find("tr").eq(i + 1);
		vTr.css("display", "");
		rowData = data[i].split(",");
		vTr.find("td").eq(0).text(rowData[0]);
		vTr.find("td").eq(1).text(logType[language][parseInt(rowData[1], 10)]);
		vTr.find("td").eq(2).text(rowData[3]);		
	}
	for(var i = rowNum ; i < 20; i++)
	{
		$("#table_info_alarm").find("tr").eq(i + 1).css("display", "none");
	}
	$("#inputPageNum").val("");
}

function paramCheck()
{
	var obj = $("#inputPageNum");
	var patten = /^[1-9][0-9]*$/;
	if(!patten.test(obj.val()))
	{
		if(language == ENGLISH)
		{
			alert("Page number should be nonzero decimal integer!");
		}
		else
		{
			alert("页码应该输入非零的十进制数！");
		}
		setTimeout(function () {obj.focus()}, 0);
		setTimeout(function () {obj.select()}, 0);
		return false; 
	}
	if(parseInt(obj.val(), 10) > totalPageNum)
	{
		if(language == ENGLISH)
		{
			alert("Can not exceed the total number of pages!");
		}
		else
		{
			alert("不能超出总页数！");
		}
		return false;
	}
	return true;
}

function FilterLog()
{
	GetInformation(FIRST);
}

function deleteLog()
{
	var confirmPrompt;	
	if(language == ENGLISH)
	{
		confirmPrompt = "Are you sure to delete all logs?";
	}
	else if(language == CHINESE)
	{
		confirmPrompt = "确定要删除所有日志?";
	}
	if(window.confirm(confirmPrompt))
	{
		$("#Operation").val("1");
		GetInformation(FIRST);
	}	
}

function GetInformation(Operation)
{
	switch(Operation)
	{
		case FIRST:
			curPage = 1;
			GetQuery();
			break;
		case PREV:
			if(curPage > 1)
			{
				curPage--;
			}
			GetQuery();
			break;
		case NEXT:
			if(curPage < totalPageNum)
			{
				curPage++;
			}	
			GetQuery();
			break;
		case LAST:	
			curPage = totalPageNum;
			GetQuery();
			break;
		case PAGE:
			if(paramCheck() == false)
			{	
				return;
			}
			curPage = parseInt($("#inputPageNum").val(), 10);
			GetQuery();
			break;
		default:
			break;
	}
}

function GetQuery()
{
	var data;
	var xmlhttp = getHTTPObject();
	queryString = "curPage=" + curPage + "&startTime=" + $("#startTime").val() + "&endTime=" + $("#endTime").val() + "&logType=" + $("#logType").val() + "&OperationType=" + $("#Operation").val()+"";
	xmlhttp.open("POST", "/goform/syslogGet", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", queryString.length);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			var response = xmlhttp.responseText.slice(7, -8);
			var responseArr = response.split("||");
			if(parseInt(responseArr[1], 10) == 0)
			{
				rowNum = 0;
				data = responseArr[0];
			}
			else
			{
				data = responseArr[0].split("&");
				rowNum = data.length;				 
			}
			totalPageNum = parseInt(responseArr[1], 10);
			Assignment(data);		
		}
	}
	xmlhttp.send(queryString);
	return true;
}

function getHTTPObject()
{
	var aHttpObject = false;
	try
	{
		aHttpObject = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			aHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			aHttpObject = false;
		}
	}
	if ((!aHttpObject) && (typeof XMLHttpRequest != "undefined"))
	{
		try
		{
			aHttpObject = new XMLHttpRequest();
		}
		catch(e)
		{
			aHttpObject = false;
		}
	}
	return aHttpObject;
}
