var index = 0; 
var data  = '';
var timeSign;
function showTags( pageno)
{
	if (!pageno) pageno = 1;
	index = 0;
	var link = "system.php?sys=word&action=manage&channelid="+channelid+"&pageno="+pageno;
	AJAX.request( link,
	{
		method		: 'get',
		onComplete	: function(transport, txt)
		{
			var json = evaljson(txt);
			if ( json.status == 200)
			{
				clearTable();
				data = json;
				timeSign = setInterval("createTables();",100);			
				pg.Total    = json.total;
				pg.PageSize = json.pagesize;
				pg.PageButtonNumber = 7;
				$('pg_pager').innerHTML = pg.ShowPagerBar();
			}
			else
			{
				alert( lang[json.message]);
			}
			doOk();
		}
	});
}
function createTables()
{
	if(data.message.length == index)
	{
		clearInterval(timeSign);
		data = '';
		return ;
	}
	else
	{
		var _tr = $('mytable').insertRow(index+1);
		_tr.id      = 'kid_'+data.message[index].id;
		var a   = _tr.insertCell(0);
		a.className = 'tablerowl';
		a.innerHTML = "<input type='checkbox' disabled id='ids_"+data.message[index].id+"' value='"+data.message[index].id+"'>";
		var e   = _tr.insertCell(1);
		e.className = 'tablerowl';
		e.innerHTML = data.message[index].word;
		var c   = _tr.insertCell(2);
		c.className = 'tablerow';
		c.align     = 'center';
		c.innerHTML = data.message[index].rank;
		var d   = _tr.insertCell(3);
		d.className = 'tablerow';
		d.align     = 'center';
		d.innerHTML = "<span onclick='editTags("+data.message[index].id+");' style='cursor:pointer;'>编辑</span> | <span onclick='delTags("+data.message[index].id+");' style='cursor:pointer;'>删除</span>";
		index++;
	}
}
function delTags( kid)
{
	var link = "system.php?sys=word&action=update&channelid="+channelid;
	AJAX.request( link,
	{
		method		: 'post',
		parameters	: "type=1&kid="+kid,
		onComplete	: function( json)
		{
			if( json.status == 200)
			{
				showTags( pg.CurrentPage);
				
			}
		}
	});
}
function addTags()
{
	var name = $('name').value;
	var rank = $('rank').value;
	var kid  = $('kid').value;
	if ( !name || !rank)
	{
		alert('请添加完整数据');
		return false;
	}
	$('name').value = '';
	$('rank').value = '';
	var type = $('addTag').value == '添加关键词' ? 2 : 3;
	var link = "system.php?sys=keyword&action=update&channelid="+channelid;
	AJAX.request( link,
	{
		method		: 'post',
		parameters	: "type="+type+"&name="+name+"&rank="+rank+"&kid="+kid,
		onComplete	: function( json)
		{
			$('addTag').value = '添加关键词';
			if( json.status == 200)
			{
				if( type == 2 )
				{
					alert("关键词汇添加成功") ;
				}
				else
				{
					clearTable();
					showTags( pg.CurrentPage);
				}
			}
			else if ( json.status == 403)
			{
				clearTable();
				showTags( pg.CurrentPage);
			}
			else
			{
				alert( lang[json.message]);
			}
		}
	});
}

function editTags( kid)
{
	var obj  = $('kid_'+kid);
	$('name').value = obj.cells[2].innerText;
	$('rank').value = obj.cells[4].innerText; 
	$('kid').value  = kid; 
	$('addTag').value = '编辑关键词汇';
	$('rank').focus(); 
}

function clearTable()
{
	for( var i=0; i< $('mytable').rows.length; i++)
	{
		var id  = $('mytable').rows[i].id;
		if( id.substr(0,3) == 'kid')
		{
			$('mytable').deleteRow(i);
			i--;
		}
	}
}
Pager.prototype.goPrePage = function()
{
	this.CurrentPage--;
	showTags( this.CurrentPage);
}
Pager.prototype.goLastPage = function()
{
	this.CurrentPage = this.PageCount;
	showTags( this.CurrentPage);
}
Pager.prototype.goPage = function(n)
{
	this.CurrentPage = n;
	showTags( n);
}
Pager.prototype.goNextPage = function()
{
	this.CurrentPage++;
	showTags( this.CurrentPage);
}