﻿function getNewsTitle(param1, param2, param3) {
	try {
		setNewsTitle("<p><font size='+2' color='#999999'><b>資料讀取中‧‧‧</b></font></p>");
		if (param1==null) {
			var currentTime = new Date()
			var month = currentTime.getMonth() + 1
			if (month < 10) {
				month = "0" + month
			};
			var day = currentTime.getDate()
			if (day < 10) {
				day = "0" + day
			};
			var year = currentTime.getFullYear()
			var args1 = year + month + day;
		} else {
			var args1 = param1;
		};
		
		if (param2==null) {
			var args2 = "headline";
		} else {
			var args2 = param2;
		};
		
		if (param3==null) {
			var args3 = "0";
		} else {
			var args3 = param3;
		};
		
		var url = "page_xml/getNewsTitle.asp";
		ajaxFetchNewsTitle(url, args1, args2, args3, 1);
	}
	catch(e) {
		alert(e);
	}
}

function getXmlHttpObject() {
	var req = null;
	try {
		// Firefox, Opera 8.0+, Safari
		req = new XMLHttpRequest();
	}
	catch(e) {
		// Internet Explorer
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return req;
}

// retrieves data from server via ajax
function ajaxFetchNewsTitle(url, param1, param2, param3, isXML) {
	req = getXmlHttpObject();
	if(req == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}

	req.onreadystatechange = function() {
		if(req.readyState == 4) {
			if(req.status == 200) {
				try {
					if(isXML && (xml = req.responseXML) != null) {
						var colNodes = xml.getElementsByTagName("Col");
						if((nodeCount = colNodes.length) > 0) {
							var titlelist = "";
							var currentSection;

							titlelist = "<form>"
							titlelist = titlelist + "<label>標題："
							titlelist = titlelist + "<select name='nid' id='nid'>"
							for(var i = 0; i < nodeCount; i++) {
								var colNode = colNodes[i];
								var gDate = colNode.getElementsByTagName("CPubDate")[0].childNodes[0].nodeValue;
								var gSection = colNode.getElementsByTagName("CSec")[0].childNodes[0].nodeValue;
								var gTitle = colNode.getElementsByTagName("CTitle")[0].childNodes[0].nodeValue;
								gTitle = gTitle.replace(/\+/g, " ");
								try {
									gTitle = unescape(decodeURI(gTitle));
								} catch(e) {
									alert(e);
								};
								var gID = colNode.getElementsByTagName("CNID")[0].childNodes[0].nodeValue;
								titlelist = titlelist + "<OPTION VALUE='" + gID + "'"
								if (gID==param3) {
									titlelist = titlelist + " selected"
								}
								titlelist = titlelist + ">(" + gSection + ")  " + gTitle + "&nbsp;&nbsp;&nbsp;&nbsp;</OPTION>"
							}
							titlelist = titlelist + "</SELECT>"
							titlelist = titlelist + "<input type='button' value=' 閱 讀 ' onclick='checkvalue(this);'>"
							titlelist = titlelist + "</label>"
							titlelist = titlelist + "</form>"
							setNewsTitle(titlelist);

							if (param3!="0") {
								checkvalue(param3);
							};
						} else {
							setNewsTitle("<p>對不起，系統找不到相關資料！</p>");
						}
					} else {
						setNewsTitle("<p>資料格式錯誤！</p>");
					}
				}
				catch(e) {
					alert(e);
				}
			} else {
				// fetched the wrong page or network error...
				var err = "Error: "+req.status;
				alert(err);
				setNewsTitle("<p>網絡讀取錯誤！</p>");
			}
		}
	}
	req.open("GET", url + "?type=" + escape(param2) + "&qdate=" + escape(param1), true);
	req.send(null);
}

function setNewsTitle(data) {
	document.getElementById("newstitle").innerHTML = data;
	document.getElementById("newscontent").innerHTML = "";
}

function checkvalue(param) {
	if (document.getElementById('nid')==null) {
		//alert("null");
	} else {
		getNewsDetails(document.getElementById('nid').value);
	};
}

function getNewsDetails(param) {
	try {
		setNewsContent("<p><font size='+2' color='#999999'><b>資料讀取中‧‧‧</b></font></p>");
		var url = "page_xml/getNewsDetails.asp";
		ajaxFetchNewsDetails(url, param, 1, 0);
	}
	catch(e) {
		alert(e);
	}
}

// retrieves data from server via ajax
function ajaxFetchNewsDetails(url, param, isXML, isPost) {
	req = getXmlHttpObject();
	if(req == null) {
		alert("Your browser does not support AJAX!");
		return;
	}

	req.onreadystatechange = function() {
		if(req.readyState == 4) {
			if(req.status == 200) {
				if(isXML && (xml = req.responseXML) != null) {
					var getNodeText = function(nodeName) {
						var nodes = xml.getElementsByTagName(nodeName);
						if(nodes.length > 0) {
							return nodes[0].childNodes[0].nodeValue;
						}
						else {
							return null;
						}
					}
					
					var str_nid = getNodeText("NID");
					var str_npage = getNodeText("NPage");
					var str_Date = getNodeText("CPubDate");
					var str_Section = getNodeText("CSec");
					
					var str_Title = getNodeText("CTitle");
					str_Title = str_Title.replace(/\+/g, " ");
					try {
						str_Title = unescape(decodeURI(str_Title));
					} catch(e) {
						alert(e);
					};

					var str_Content = getNodeText("CContent");
					str_Content = str_Content.replace(/\+/g, " ");
					try {
						str_Content = unescape(decodeURI(str_Content));
					} catch(e) {
						alert(e);
					}
					
					var html_pic = "";
					if (str_Section=="要聞"){
						html_pic = "<font size='+2'><b>要聞</b></font><br />"
					} else if (str_Section=="新聞" || str_Section=="新聞/世事"){
						html_pic = "<img src='icon/am_news.jpg' alt='新聞' width='300' border='0'><br />"
					} else if (str_Section=="世事"){
						html_pic = "<img src='icon/am_notes.jpg' alt='世事' width='300' border='0'><br />"
					} else if (str_Section=="財經" || str_Section=="投資"){
						html_pic = "<img src='icon/am_finance.jpg' alt='財經/投資' width='300' border='0'><br />"
					} else if (str_Section=="地產" || str_Section=="易搜盤"){
						html_pic = "<img src='icon/am_property.jpg' alt='地產' width='300' border='0'><br />"
					} else if (str_Section=="健康"){
						html_pic = "<img src='icon/am_health.jpg' alt='健康' width='300' border='0'><br />"
					} else if (str_Section=="時尚"){
						html_pic = "<img src='icon/am_ol.jpg' alt='時尚' width='300' border='0'><br />"
					} else if (str_Section=="新科玩"){
						html_pic = "<img src='icon/am_it.jpg' alt='新科玩' width='300' border='0'><br />"
					} else if (str_Section=="體育"){
						html_pic = "<img src='icon/am_sports.jpg' alt='體育' width='300' border='0'><br />"
					} else if (str_Section=="遊樂"){
						html_pic = "<img src='icon/am_walker.jpg' alt='遊樂' width='300' border='0'><br />"
					} else if (str_Section=="小歇"){
						html_pic = "<img src='icon/am_break.jpg' alt='小歇' width='300' border='0'><br />"
					} else if (str_Section=="娛樂"){
						html_pic = "<img src='icon/am_ent.jpg' alt='娛樂' width='300' border='0'><br />"
					} else if (str_Section=="amLife" || str_Section=="Life"){
						html_pic = "<img src='icon/am_amlife.jpg' alt='amLife' height='50' border='0'><br />"
					} else if (str_Section=="feature" || str_Section=="amTaste" || str_Section=="Taste"){
						html_pic = "<img src='icon/am_feature.jpg' alt='Feature' height='50' border='0'>"
						html_pic = html_pic + "<img src='icon/am_Taste.jpg' alt='amTaste' height='50' border='0'>"
						html_pic = html_pic + "<br />"
					};

					var html = "<p><b><u>" + str_Title + "</u></b></p><p>" + str_Content + "</p>" + "<p><font color='blue'>(圖片及原文請查看印刷版第" + str_npage + "頁)</font></p>";
					setNewsContent(html);
				}
				else {
					setNewsContent("<p>資料格式錯誤！</p>");
				}
			}
			else {
				// fetched the wrong page or network error...
				var err = "Error: " + req.status;
				alert(err);
				setNewsContent("<p>網絡讀取錯誤！</p>");
			}
		}
	}
	req.open("GET", url + "?code=" + escape(param), true);
	req.send(null);
}

function setNewsContent(data) {
	document.getElementById("newscontent").innerHTML = data;
}