/**
 * 首页登陆操作
 */

//列出某栏目的对应新闻
function listNews( category, element )
{
	var callback = function(result)
	{
		for(var i = 0; i < result.data.length; i++)
		{
			if( i >= 10 )	break; 
			createNews(element, result.data[i]);
		}
	}
	var operation = new Operation("公用.列出栏目新闻");
	operation.category = category;
	operation.offset = 0;
	operation.limit = 10;
	operation.execute(callback);
}

//将新闻的点击数加1
function addHitCount(news)
{
	var operation = new Operation("公用.增加新闻点击数");
	operation.id = news.id;
	operation.execute();
}

//创建一条新闻链接
function createNews(ulElement, news)
{
	var month = news.createDate.getMonth() + 1;
	if (month < 10) { month = "0" + month; }
	var day = news.createDate.getDate();
	if (day < 10) { day = "0" + day; }
		
	var li = document.createElement("li");
	
	var dateString = document.createElement("span");
	dateString.className = "date";
	dateString.innerHTML = month + "月" + day + "日";
		
	var a = document.createElement("a");
	a.newsId = news.id;
	a.categoryId = news.categoryId;
	a.departmentId = news.departmentId;

	if(news.title.length <= 20)
		a.innerHTML = news.title;
	else
		a.innerHTML = news.title.substring(0, 19) + "...";
	
	a.target = "_blank";
	a.title = news.title;
	a.href = "news_view.jsp?id=" + news.id;
	a.onclick = function()
	{
		addHitCount(news);
	};
	
	li.appendChild(dateString);
	li.appendChild(a);
	
	ulElement.appendChild(li);
}

//登陆操作
function doLogin()
{
	var login = $("login").value;
	if($("login").value == null || $("login").value == "")	
	{
		$("login").focus();
		return false;
	}
	var password = $("password").value;
	if($("password").value == null || $("password").value == "")	
	{
		$("password").focus();
		return false;
	}
	
	var callback = function (result)
	{
		if (result.account == null)
		{
			alert("登录失败");
			$("login").focus();
			$("login").select();
			return false;
		}
		
		//ie下无法执行，移入服务器端
		//addAccountUseHistory();
		window.location = "main.jsp";
	}
	
	var error_handler = function (e)
	{
		alert("登录失败: " + e);
	}
	var operation = new Operation("公用.登录");
	operation.login = login;
	operation.password = password;
	operation.execute(callback);
	
	return false;
}

//添加用户使用历史记录
function addAccountUseHistory()
{
	var operation = new Operation("客户.使用历史.添加用户使用历史");
	operation.execute();
}

//解析时间
function parseDate( newsDate )
{
	var year = newsDate.getFullYear();
	var month = newsDate.getMonth() + 1;
	if (month < 10) { month = "0" + month; }
	var day = newsDate.getDate();
	if (day < 10) { day = "0" + day; }
	return year + "年" + month + "月" + day + "日";
}

