var page = 1;
var startPage = 1;
var pageStart = 0;
var pageEnd = 0;
var boardPages = 4;
var record_per_page = 3; ////// records_per_page

function replaceHTML(str) 
{ 
	var replace=new Array("\n", "&lt;P&gt;", "&lt;p&gt;", "&amp;lt;p&amp;gt;", "&amp;lt;P&amp;gt;"); 
	var by=new Array("<BR>", "", "", "", ""); 
	for (var i=0; i<replace.length; i++) { 
		str = str.replace(replace[i], by[i]); 
	} 
	return str; 
} 

function list_record(startNo, pageNo, channel_id, group_id)
{
	page = pageNo;
	startPage = startNo;

	reset_record_Messages();
	list_record_Messages(blog_id, pageNo, channel_id, group_id);
}

function list_record_Messages(blog_id, pageNo, channel_id, group_id)
{
	var html = "";
	var objPanel = document.getElementById("list_panel");		
	//loading
	objPanel.innerHTML = "<div align=\"center\"><img src='../images/map_loading.gif' /></div>";
	var xmlHttp = getXmlHttp();
	if(!xmlHttp) return false;
	
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			var xmlDoc	= xmlHttp.responseXML.documentElement;
			//alert (xmlDoc);
			var records	= xmlDoc.getElementsByTagName("record");				

			
			objPanel.innerHTML = "";

			if(startPage > 1)
			{
				pageStart = (startPage * boardPages) - boardPages;
				pageEnd = (startPage * boardPages);
			}
			else
			{
				pageStart	= 0;
				pageEnd = boardPages;
			}

			if(startPage > 1)
			{
				//html = "<a class=\"blue\" href=\"JavaScript:list_record(" + (startPage - 1) + ", " + pageStart + ", " + channel_id + ", " + group_id + ")\">&lt;&lt;</a> ";
				html = "<a class=\"blue\" href=\"JavaScript:list_record(" + (startPage - 1) + ", " + pageStart + ", " + channel_id + ", " + group_id + ")\">&lt;&lt;</a> ";
			}

			for(var i = pageStart; i < pageEnd; i++)
			{
				if((i + 1) == page)
				{
					html += "<a class=\"blue\">" + (i + 1) + "</a>  ";
				}
				else
				{
					if(i < xmlDoc.getAttribute("total_page"))
					{
						html += "<a class=\"blue\" href=\"JavaScript:list_record(" + startPage + ", " + (i + 1) + ", " + channel_id + ", " + group_id + ")\">" + (i + 1) + "</a> ";
					}
				}
			}

			if(startPage < (Math.ceil(xmlDoc.getAttribute("total_page") / boardPages)))
			{
				html += "<a class=\"pageturn\" href=\"JavaScript:list_record(" + (startPage + 1) + ", " + (pageEnd + 1) + ", " + channel_id + ", " + group_id + ")\">&gt;&gt;</a> ";
			}

			var objTopTable = new _IC_Table("top_table_search");
			var objTTable = objTopTable.getTableObj();
				objTTable.setAttribute("border", "0");
				objTTable.setAttribute("width", "100%"); ///// page number navigation
				objTTable.setAttribute("cellPadding", "2");
				objTTable.setAttribute("cellSpacing", "0")
			var objTBody = objTopTable.getTableBodyObj();

			var tRow = addRow();
				tRow.setAttribute("id", "1");
			var tCol1 = addCol();
				tCol1.setAttribute("width", "10%");
				tCol1.setAttribute("noWrap", "true");
			var record_first = parseInt((pageNo*record_per_page)-record_per_page+1);
			var record_last = parseInt(record_first+record_per_page-1);
			if (xmlDoc.getAttribute("total_records")<record_last)
			{
				record_last = xmlDoc.getAttribute("total_records");
			}
//				tCol1.innerHTML = record_first+"-"+record_last+" (共有 " + xmlDoc.getAttribute("total_records")+")";
			var tCol2 = addCol();
				tCol2.setAttribute("width", "90%");
				tCol2.setAttribute("noWrap", "true");
				tCol2.setAttribute("align", "right");
//				tCol2.innerHTML = html;
			tRow.appendChild(tCol1);
			tRow.appendChild(tCol2);

			objTBody.appendChild(tRow);
			objTTable.appendChild(objTBody);

			var objBlogTable = new _IC_Table("table_search_message");
			var objTable = objBlogTable.getTableObj();
				objTable.setAttribute("border", "0");
//				objTable.setAttribute("height", "400");
				objTable.setAttribute("width", "100%");
				objTable.setAttribute("valign", "top");
				objTable.setAttribute("cellPadding", "0");
				objTable.setAttribute("cellSpacing", "0");
			var objBody = objBlogTable.getTableBodyObj();
			
			var row1 = addRow();
				row1.setAttribute("id", "1");
			var col1 = addCol();
				col1.appendChild(objTTable);

				row1.appendChild(col1);
				objBody.appendChild(row1);

			if(records.length > 0)
			{

				for(var i = 0; i < records.length; i++)
				{
					
					var sub_objVideoTable = new _IC_Table("sub_search_video_table_" + (i + 1));
					var sub_objVTable = sub_objVideoTable.getTableObj();
						sub_objVTable.setAttribute("border", "0");
						sub_objVTable.setAttribute("width", "100%");
						sub_objVTable.setAttribute("cellPadding", "1");
						sub_objVTable.setAttribute("cellSpacing", "0");
					var sub_objVBody = sub_objVideoTable.getTableBodyObj();

					var sub_tRow = addRow();
						sub_tRow.setAttribute("id", parseInt(records.length+1));
					var sub_tCol1 = addCol();
						sub_tCol1.setAttribute("width", "100%");
						sub_tCol1.setAttribute("noWrap", "true");
						sub_tCol1.innerHTML="<br><br><br>hello<br><br>";
					
					
					sub_tRow.appendChild(sub_tCol1);
					sub_objVBody.appendChild(sub_tRow);
				    sub_objVTable.appendChild(sub_objVBody);
					
					
					
					
					var objVideoTable = new _IC_Table("search_video_table_" + (i + 1));
					var objVTable = objVideoTable.getTableObj();
						objVTable.setAttribute("border", "0");
						objVTable.setAttribute("width", "100%");
						objVTable.setAttribute("cellPadding", "0");
						objVTable.setAttribute("cellSpacing", "0");
					var objVBody = objVideoTable.getTableBodyObj();

					var tRow = addRow();
						tRow.setAttribute("id", parseInt(records.length+1));
					var tCol1 = addCol();
						tCol1.setAttribute("width", "10%");
						tCol1.setAttribute("noWrap", "true");
						
						var video_rating ="";
						for (var vr=1; vr<=getXMLValue(records.item(i).getElementsByTagName("video_rating"));vr++)
						{
							video_rating += "<img src=\"images/star_rating.jpg\" width=\"11\" height=\"11\">"
						}
						
						for (var vr=1; vr<=(5-getXMLValue(records.item(i).getElementsByTagName("video_rating")));vr++)
						{
							video_rating += "<img src=\"images/star_rating_grey.jpg\" width=\"11\" height=\"11\">"
						}	

//						tCol1.innerHTML="<table width=\"410\" border=\"1\" cellspacing=\"2\" cellpadding=\"3\"><tr><td align=\"right\"><img src=\"images/gx_bullet01.gif\" border=\"0\"></td><td><span class=\"centitle\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\" class=\"cennews\">"+getXMLValue(records.item(i).getElementsByTagName("title"))+"</a></span> &nbsp;&nbsp;<a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\"><img src=\"images/tvicon.gif\" border=\"0\"></a></td><td><div class=\"centitle\">"+getXMLValue(records.item(i).getElementsByTagName("date"))+"</div></td><td align=\"center\" rowspan=\"3\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\"><img src=\""+getXMLValue(records.item(i).getElementsByTagName("picture"))+"\" border=\"0\" class=\"picborder\" width=\"140\" height=\"105\"></a></td></tr><tr><td colspan=\"3\"><div class=\"centxt\" style=\"margin-left:22px;margin-right:30px;\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\" class=\"cencontent\">"+getXMLValue(records.item(i).getElementsByTagName("content"))+"</a></p></td></tr><tr><td colspan=\"3\"><div style=\"margin-left:17px;\">上載："+getXMLValue(records.item(i).getElementsByTagName("video_created_date"))+"│觀看："+ getXMLValue(records.item(i).getElementsByTagName("video_hit"))+"│人氣：<span class=\"style60\">"+video_rating+"</span></div></td></tr><tr><td align=\"center\" colspan=\"4\"><img src=\"images/gx_b07.gif\" border=\"0\"></td></tr><tr><td align=\"center\" colspan=\"5\"><img src=\"images/spacer.gif\" border=\"0\" height=\"2\" width=\"10\"></td></tr><table>";
//						tColl.innerHTML="<table width=\"412\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"90\" rowspan=\"2\" valign=\"top\"><img src=\"images/sa_pic.jpg\" width=\"80\" height=\"60\"></td><td width=\"230\" valign=\"top\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\" class=\"blue_big\">"+getXMLValue(records.item(i).getElementsByTagName("title"))+"</a></td><td valign=\"top\"><div align=\"right\">"+getXMLValue(records.item(i).getElementsByTagName("date"))+"</div></td></tr><tr><td colspan=\"2\" valign=\"top\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\" class=\"cencontent\">"+getXMLValue(records.item(i).getElementsByTagName("content"))+"</a></td></tr><tr><td colspan=\"3\" valign=\"top\"><img src=\"images/spacer(1).gif\" width=\"5\" height=\"5\"></td></tr> <tr><td colspan=\"3\" valign=\"top\">上載: "+getXMLValue(records.item(i).getElementsByTagName("video_created_date"))+"│觀看："+ getXMLValue(records.item(i).getElementsByTagName("video_hit"))+"│人氣：<span class=\"style60\">"+video_rating+"</span></td></tr><tr><td colspan=\"3\" valign=\"top\"><img src=\"images/spacer(1).gif\" width=\"10\" height=\"10\"></td></tr><tr><td colspan=\"3\" valign=\"top\" bgcolor=\"#005AB5\"><img src=\"images/spacer(1).gif\" width=\"20\" height=\"1\"></td></tr><tr><td colspan=\"3\" valign=\"top\"><img src=\"images/spacer(1).gif\" width=\"10\" height=\"10\"></td></tr></table>";
//						tCol1.innerHTML="<table width=\"412\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"90\" rowspan=\"2\" valign=\"top\"><img src="+getXMLValue(records.item(i).getElementsByTagName("picture"))+" width=\"80\" height=\"60\"></td><td width=\"230\" valign=\"top\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\" class=\"blue_big\">"+getXMLValue(records.item(i).getElementsByTagName("title"))+"</a></td><td valign=\"top\"><div align=\"right\">"+getXMLValue(records.item(i).getElementsByTagName("date"))+"</div></td></tr><tr><td colspan=\"2\" valign=\"top\">"+getXMLValue(records.item(i).getElementsByTagName("content"))+"</td></tr><tr><td colspan=\"3\" valign=\"top\"><img src=\"images/spacer(1).gif\" width=\"5\" height=\"5\"></td></tr> <tr><td colspan=\"3\" valign=\"top\">上載: "+getXMLValue(records.item(i).getElementsByTagName("video_created_date"))+"│觀看："+ getXMLValue(records.item(i).getElementsByTagName("video_hit"))+"│人氣：<span class=\"style60\">"+video_rating+"</span></td></tr><tr><td colspan=\"3\" valign=\"top\"><img src=\"images/spacer(1).gif\" width=\"10\" height=\"10\"></td></tr>";										


						if (i<2)
						{
							tCol1.innerHTML="<table width=\"412\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"90\" rowspan=\"2\" valign=\"top\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\"><img src="+getXMLValue(records.item(i).getElementsByTagName("picture"))+" width=\"80\" height=\"60\" border=0></a></td><td width=\"230\" valign=\"top\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\" class=\"blue_big\">"+getXMLValue(records.item(i).getElementsByTagName("title"))+"</a></td><td valign=\"top\"><div align=\"right\">"+getXMLValue(records.item(i).getElementsByTagName("date"))+"</div></td></tr><tr><td colspan=\"2\" valign=\"top\">"+replaceHTML(getXMLValue(records.item(i).getElementsByTagName("content")))+"</td></tr><tr><td colspan=\"3\" valign=\"top\"><img src=\"images/spacer(1).gif\" width=\"5\" height=\"5\"></td></tr> <tr><td colspan=\"3\" valign=\"top\">上載: "+getXMLValue(records.item(i).getElementsByTagName("video_created_date"))+"│觀看："+ getXMLValue(records.item(i).getElementsByTagName("video_hit"))+"│人氣：<span class=\"style60\">"+video_rating+"</span></td></tr><tr><td colspan=\"3\" valign=\"top\"><img src=\"images/spacer(1).gif\" width=\"10\" height=\"10\"></td></tr><tr><td colspan=\"3\" valign=\"top\" bgcolor=\"#005AB5\"><img src=\"images/spacer(1).gif\" width=\"20\" height=\"1\"></td></tr><tr><td colspan=\"3\" valign=\"top\"><img src=\"images/spacer(1).gif\" width=\"10\" height=\"10\"></td></tr></table>";
						} else {
							tCol1.innerHTML="<table width=\"412\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"90\" rowspan=\"2\" valign=\"top\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\"><img src="+getXMLValue(records.item(i).getElementsByTagName("picture"))+" width=\"80\" height=\"60\" border=0></a></td><td width=\"230\" valign=\"top\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\" class=\"blue_big\">"+getXMLValue(records.item(i).getElementsByTagName("title"))+"</a></td><td valign=\"top\"><div align=\"right\">"+getXMLValue(records.item(i).getElementsByTagName("date"))+"</div></td></tr><tr><td colspan=\"2\" valign=\"top\">"+replaceHTML(getXMLValue(records.item(i).getElementsByTagName("content")))+"</td></tr><tr><td colspan=\"3\" valign=\"top\"><img src=\"images/spacer(1).gif\" width=\"5\" height=\"5\"></td></tr> <tr><td colspan=\"3\" valign=\"top\">上載: "+getXMLValue(records.item(i).getElementsByTagName("video_created_date"))+"│觀看："+ getXMLValue(records.item(i).getElementsByTagName("video_hit"))+"│人氣：<span class=\"style60\">"+video_rating+"</span></td></tr></table>";
						}

				
						/*if (i%2==0)
						{
						
							tCol1.innerHTML="<table width=\"410\" border=\"1\" cellspacing=\"2\" cellpadding=\"3\"><tr><!-- td rowspan=\"4\" valign=\"top\"><img src=\"images/gx_b06a.gif\" border=\"0\"></td --><td align=\"right\"><img src=\"images/gx_bullet01.gif\" border=\"0\"></td><td><span class=\"centitle\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\" class=\"cennews\">"+getXMLValue(records.item(i).getElementsByTagName("title"))+"</a></span> &nbsp;&nbsp;<a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\"><img src=\"images/tvicon.gif\" border=\"0\"></a></td><td><div class=\"centitle\">"+getXMLValue(records.item(i).getElementsByTagName("date"))+"</div></td><td align=\"center\" rowspan=\"3\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\"><img src=\""+getXMLValue(records.item(i).getElementsByTagName("picture"))+"\" border=\"0\" class=\"picborder\" width=\"140\" height=\"105\"></a></td></tr><tr><td colspan=\"3\"><div class=\"centxt\" style=\"margin-left:22px;margin-right:30px;\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\" class=\"cencontent\">"+getXMLValue(records.item(i).getElementsByTagName("content"))+"</a></p></td></tr><tr><td colspan=\"3\"><div style=\"margin-left:17px;\">上載："+getXMLValue(records.item(i).getElementsByTagName("video_created_date"))+"│觀看："+ getXMLValue(records.item(i).getElementsByTagName("video_hit"))+"│人氣：<span class=\"style60\">"+video_rating+"</span></div></td></tr><tr><td align=\"center\" colspan=\"4\"><img src=\"images/gx_b07.gif\" border=\"0\"></td></tr><tr><td align=\"center\" colspan=\"5\"><img src=\"images/spacer.gif\" border=\"0\" height=\"2\" width=\"10\"></td></tr><table>";
						} else {

							tCol1.innerHTML="<table width=\"410\" border=\"1\" cellspacing=\"2\" cellpadding=\"3\"><tr><!-- td rowspan=\"4\" valign=\"top\"><img src=\"images/gx_b06b.gif\" border=\"0\"></td --><td align=\"center\" rowspan=\"3\" width=\"200\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\"><img src=\""+getXMLValue(records.item(i).getElementsByTagName("picture"))+"\" border=\"0\" class=\"picborder\" width=\"140\" height=\"105\"></a></td><td align=\"right\"><img src=\"images/gx_bullet01.gif\" border=\"0\"></td><td width=\"370\"><span class=\"centitle\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\" class=\"cennews\">"+getXMLValue(records.item(i).getElementsByTagName("title"))+"</a></span>&nbsp;&nbsp;<a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\"><img src=\"images/tvicon.gif\" border=\"0\"></a></td><td><div class=\"centitle\">"+getXMLValue(records.item(i).getElementsByTagName("date"))+"</div></td></tr><tr><td colspan=\"3\"><div class=\"centxt\" style=\"margin-left:22px;margin-right:30px;\"><a href=\"../news_video/index.php?video_id="+records.item(i).getAttribute("id")+"\" class=\"cencontent\">"+getXMLValue(records.item(i).getElementsByTagName("content"))+"</a></p></td></tr><tr><td colspan=\"3\"><div style=\"margin-left:17px;\">上載："+getXMLValue(records.item(i).getElementsByTagName("video_created_date"))+"│觀看："+ getXMLValue(records.item(i).getElementsByTagName("video_hit"))+"│人氣：<span class=\"style60\">"+video_rating+"</span></div></td></tr><tr><td align=\"center\" colspan=\"4\"><img src=\"images/gx_b07.gif\" border=\"0\"></td></tr><tr><td align=\"center\" colspan=\"5\"><img src=\"images/spacer.gif\" border=\"0\" height=\"2\" width=\"10\"></td></tr></table>";

						} */

						//tCol1.appendChild(sub_objVTable);
					
					
					tRow.appendChild(tCol1);
					objVBody.appendChild(tRow);
				    objVTable.appendChild(objVBody);
					

						var row = addRow();
							row.setAttribute("id", (i + 1));
						var col = addCol();
							col.appendChild(objVTable);

						row.appendChild(col);
						objBody.appendChild(row);
				
					
				
					
				}
				
			var objTopTable = new _IC_Table("top_table_search_2");
			var objTTable = objTopTable.getTableObj();
				objTTable.setAttribute("border", "0");
				objTTable.setAttribute("width", "100%"); ///// page number navigation
				objTTable.setAttribute("cellPadding", "2");
				objTTable.setAttribute("cellSpacing", "0")
			var objTBody = objTopTable.getTableBodyObj();
				
			var tRow = addRow();
				tRow.setAttribute("id", parseInt(records.length+1));
			var tCol1 = addCol();
					tCol1.setAttribute("width", "10%");
					tCol1.setAttribute("noWrap", "true");
			var record_first = parseInt((pageNo*record_per_page)-record_per_page+1);
			var record_last = parseInt(record_first+record_per_page-1);
			if (xmlDoc.getAttribute("total_records")<record_last)
			{
				record_last = xmlDoc.getAttribute("total_records");
			}
//					tCol1.innerHTML = record_first+"-"+record_last+" (共有 " + xmlDoc.getAttribute("total_records")+")";
			var tCol2 = addCol();
					tCol2.setAttribute("width", "100%");
					tCol2.setAttribute("noWrap", "true");
					tCol2.setAttribute("align", "right");
					tCol2.innerHTML = html;
				tRow.appendChild(tCol1);
				tRow.appendChild(tCol2);

				objTBody.appendChild(tRow);
				objTTable.appendChild(objTBody);	
				
			var row1 = addRow();
				row1.setAttribute("id", parseInt(records.length+1));
			var col1 = addCol();
				col1.appendChild(objTTable);

				row1.appendChild(col1);
				objBody.appendChild(row1);
			}
			else
			{
				var row = addRow();
					row.setAttribute("id", "1");
				var col = addCol();
					col.setAttribute("align", "left");
					col.innerHTML = "<font color=\"#FF0000\">沒有相關資料！</font>";
				
				row.appendChild(col);
				objBody.appendChild(row);
			}
			objTable.appendChild(objBody);

			objPanel.appendChild(objTable);
			
			
			//var required_div_height = window.document.getElementById('list_panel').offsetHeight;
			//alert(window.document.getElementById('main_table').offsetHeight+"<----->"+window.document.getElementById('list_panel').offsetHeight);
			//window.document.getElementById('main_table').height = 1000;
			//alert(window.document.getElementById('main_table').height+"<----->"+window.document.getElementById('list_panel').offsetHeight);
			///objPanel.offsetHeight =1000;
			///alert("here-->"+objPanel.offsetHeight);

			//window.document.getElementById('main_table').body.offsetHeight =1000;
			//alert(window.document.getElementById('main_table').offsetHeight);

			
		}
	}

	
	xmlHttp.open("GET", "ajax/list_record_news.php?page=" + pageNo + "&category=" + group_id, true);
	/////document.write("ajax/list_record.php?page=" + pageNo + "&category=" + group_id);
	xmlHttp.send(null);
}

function reset_record_Messages()
{
	var objTable = document.getElementById("table_search_message");

	if(objTable != null)
	{
		for(var r = 0; r < objTable.rows.length; r++)
		{
			if(objTable.rows[r].getAttribute("id") > 0)
			{
				objTable.deleteRow(r);
			}
		}

		for(var r = 0; r < objTable.rows.length; r++)
		{
			if(objTable.rows[r].getAttribute("id") > 0)
			{
				reset_record_Messages();
			}
		}
	}
}