// JavaScript Document

function ajaxChangeNewsType(area, targetID, nid) {
	var http_request = false;
	var url = "ajax_getnewstype.php";
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
	  http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
	  try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (e) {
		try {
		  http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	  }
	}
	
	if (!http_request) {
	  alert('Giving up :( Cannot create an XMLHTTP instance');
	  return false;
	}
	// 定義事件處理函數為 alterContents()
	http_request.onreadystatechange = function() { genNewsContent(area, targetID, http_request); };
	
	// show loading icon
	var target = document.getElementById(targetID);
	if(area == "INDEX") {
			target.innerHTML = "<table width=\"500\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td height=\"100\" valign=\"middle\" align=\"center\"><img src=\"images/loading.gif\" alt=\"LOADING ...\" /></td></tr></table>";
	} else if(area == "LEFT") {
			target.innerHTML = "<table width=\"215\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td height=\"100\" valign=\"middle\" align=\"center\"><img src=\"images/loading.gif\" alt=\"LOADING ...\" /></td></tr></table>";
	}
	
	// IE 6.x 和 Firefox 1.5.x 皆要 encodeURI()
	
	url = url + "?nid=" + nid + "&amp;seed=" + Date();
	http_request.open('GET', url, true);
	http_request.send(null);
}
	
function genNewsContent(area, targetID, http_request) {
	if (http_request.readyState == 4) {
      if (http_request.status == 200) {
        var xmldoc = http_request.responseXML;
        var nodes = xmldoc.getElementsByTagName("news");
        var target = document.getElementById(targetID);
		var outputContent = "";
		
		// clear target content
		target.innerHTML = "";		
		
		if(area == "INDEX") {
			outputContent += "<table width=\"500\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">";
			// add news
			for(var i=0; i<nodes.length; i++) {
			  newsId = nodes[i].getAttribute("id");
			  newsTypeId = nodes[i].getAttribute("newsTypeId");
			  intDate = nodes[i].getAttribute("intDate");
			  caption = nodes[i].firstChild.nodeValue;
			  
			  outputContent += "<tr><td width=\"120\" height=\"20\">"+intDate+"</td><td width=\"380\" height=\"20\"><a href=\"javascript:popup('popupnews.php?newsTypeId="+newsTypeId+"&newsId="+newsId+"', 500, 800);\">"+caption+"</a></td></tr>\n<tr><td colspan=\"2\"><img src=\"images/n/a_tline.jpg\" width=\"505\" height=\"7\"></td></tr>\n";
			}
			if(nodes.length == 0) {
				outputContent += "<tr><td align=\"center\" valign=\"middle\" height=\"100\">No Records Found</td></tr>";
			}
			//outputContent += "<tr><td height=\"26\" colspan=\"2\" align=\"right\"><a href=\"#\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('999','','images/n/morea.jpg',1)\"><img src=\"images/n/more.jpg\" name=\"Image24\" width=\"56\" height=\"18\" border=\"0\" id=\"Image24\" /></a></td></tr>";
			outputContent += "</table>";
		} else if(area == "LEFT") {			
			outputContent += "<table width=\"215\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";			
			// add news
			for(var i=0; i<nodes.length; i++) {
			  newsId = nodes[i].getAttribute("id");
			  newsTypeId = nodes[i].getAttribute("newsTypeId");
			  intDate = nodes[i].getAttribute("intDate2");
			  caption = nodes[i].firstChild.nodeValue;
			  
			  outputContent += "<tr><td width=\"80\" height=\"20\" valign=\"top\">"+intDate+"</td><td width=\"135\" height=\"20\" align=\"left\" valign=\"top\"><a href=\"javascript:popup('popupnews.php?newsTypeId="+newsTypeId+"&newsId="+newsId+"', 500, 800);\">"+caption+"</a></td></tr>\n<tr><td colspan=\"2\"><img src=\"images/n/dd_l.jpg\" width=\"215\" height=\"5\" /></td></tr>\n";
			}
			if(nodes.length == 0) {
				outputContent += "<tr><td align=\"center\" valign=\"middle\" height=\"100\">No Records Found</td></tr>";
			}
			//outputContent += "<tr><td height=\"26\" colspan=\"2\" align=\"right\"><a href=\"#\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('999','','images/n/morea.jpg',1)\"><img src=\"images/n/more.jpg\" name=\"Image24\" width=\"56\" height=\"18\" border=\"0\" id=\"Image24\" /></a></td></tr>";
			outputContent += "</table>";
		}
				
		// set target content
		target.innerHTML = outputContent;	
		
      } else {
        alert('There was a problem with the request.');
      }
    }
}