var showform = false;
var showreply = false;
var shownewreplies = false;

function Set_Cookie(name, value, expires, path, domain, secure) {
	var today = new Date();
	today.setTime(today.getTime());

	if (expires) {
		expires = expires * 86400000;
	}
	var expires_date = new Date(today.getTime() + (expires));

	document.cookie = name + "=" +escape(value) +
	((expires) ? ";expires=" + expires_date.toGMTString() : "") + 
	((path) ? ";path=" + path : "") + 
	((domain) ? ";domain=" + domain : "") +
	((secure) ? ";secure" : "");
}

function Get_Cookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length))) {
		return null;
	}
	if (start == -1) return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

function Delete_Cookie(name, path, domain) {
	if (Get_Cookie(name)) document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function HttpRequest(prc, url) {
	var http_request = false;
	
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
        	try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (http_request) {
		http_request.onreadystatechange = function() { 
			eval(prc + '(http_request)');
		};   
		http_request.open('GET', url, true);
		http_request.send(null);
	} else {
		//alert('連接伺服器失敗, 請稍後重試。');
		return false;
	}
}

function SubmitReply() {
	var poster_name = document.reply_form.name.value;
	if (poster_name == '') {
		alert('請輸入您的名稱!');
		document.reply_form.name.focus();
	} else if (document.reply_form.comment.value == '') {
		alert('請輸入您的回覆內容!');
		document.reply_form.comment.focus();
	} else {
		Set_Cookie('mobile01blog_visitor_name', poster_name, 30);
		document.reply_form.submit_comment.disabled = true;
		document.reply_form.submit();
	}
}

function ReplyCheckResult(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var mesg = http_request.responseText;
			if (mesg == 1) {
				var header, form, element0, element1, element2, element3;
				element0 = document.createDocumentFragment();
				header = document.createElement("div");
				header.className = "replyheader";
				header.appendChild(document.createTextNode('回應此篇文章'));
				element0.appendChild(header);
				
				form = document.createElement("form");
				form.setAttribute("name", "reply_form");
				form.setAttribute("id", "reply_form");
				form.setAttribute("target", "_self");
				form.setAttribute("action", document.location.href.replace(/article/, 'reply'));
				form.setAttribute("method", "post");
				
				element1 = document.createElement("div");
				element1.setAttribute("id", "reply_method");
				element1.appendChild(document.createTextNode('請輸入您的名稱與意見內容'));
				element1.className = "replyfield";
				
				form.appendChild(element1);
				
				var formdata = new Array();
				formdata[0] = new Array("您的名稱", "name", "text", "50", "30");
				formdata[1] = new Array("電郵位址", "email", "text", "50", "50");
				formdata[2] = new Array("意見內容", "comment", "textarea", "56", "10");
				formdata[3] = new Array("", "reply_id", "hidden", "0", "0");
				formdata[4] = new Array("", "submit_comment", "submit");
				
				poster_name = Get_Cookie('mobile01blog_visitor_name');
				
				for (i=0; i<formdata.length; i++){
					element1 = document.createElement("div");
					element1.className = "replyfield";
					
					if (formdata[i][2] == "text") {
						element1.appendChild(document.createTextNode(formdata[i][0] + ': '));						
						element2 = document.createElement("input");
						element2.setAttribute("name", formdata[i][1]);
						element2.setAttribute("id", formdata[i][1]);
						element2.setAttribute("type", "text");
						element2.setAttribute("size", formdata[i][3]);
						element2.setAttribute("maxlength", formdata[i][4]);
						if (poster_name && formdata[i][1] == "name") {
							element2.setAttribute("value", poster_name);
						}
					}
					if (formdata[i][2] == "textarea") {
						element3 = document.createElement("br");
						element1.appendChild(document.createTextNode(formdata[i][0] + ': '));
						element1.appendChild(element3);
						element2 = document.createElement("textarea");
						element2.setAttribute("name", formdata[i][1]);
						element2.setAttribute("id", formdata[i][1]);
						element2.setAttribute("cols", formdata[i][3]);
						element2.setAttribute("rows", formdata[i][4]);
					}
					if (formdata[i][2] == "hidden") {
						element2 = document.createElement("input");
						element2.setAttribute("name", formdata[i][1]);
						element2.setAttribute("id", formdata[i][1]);
						element2.setAttribute("type", "hidden");
						element2.setAttribute("value", formdata[i][0]);
					}
					if (formdata[i][2] == "submit") {
						element1.appendChild(document.createTextNode('*注意: HTML或BBCode標籤無效 '));
						element2 = document.createElement("input");
						element2.setAttribute("name", formdata[i][1]);
						element2.setAttribute("id", formdata[i][1]);
						element2.setAttribute("type", "button");
						element2.onclick = SubmitReply;
						element2.setAttribute("value", "確定送出");
					}
					element1.appendChild(element2);
					form.appendChild(element1);
				}
				element0.appendChild(form);				
				
				document.getElementById("article_reply").appendChild(element0);
				document.getElementById('article_reply').style.display = "block";
			} else {
				//alert('此Blog未開放回應。');
			}
		} else {
			//alert('回應功能無效。');
		}
	}
}

function ViewRepliesResult(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;			
			var nodes = xmldoc.getElementsByTagName("reply")[0].getElementsByTagName('item');			
			var len = nodes.length;
			if (len) {
				var header, footer, element0, element1, element2;
				element0 = document.createDocumentFragment();
				header = document.createElement("div");
				header.className = "commentheader";
				header.appendChild(document.createTextNode('文章回應一覽'));
				element0.appendChild(header);
				
				for (i=0; i<len; i++){
					comment_id = nodes[i].getElementsByTagName("commentid").item(0).firstChild.nodeValue;
					icon = nodes[i].getElementsByTagName("icon").item(0).firstChild.nodeValue;
					date = nodes[i].getElementsByTagName("date").item(0).firstChild.nodeValue;
					name = nodes[i].getElementsByTagName("name").item(0).firstChild.nodeValue;
					pid = nodes[i].getElementsByTagName("pid").item(0).firstChild.nodeValue;
					
					element1 = document.createElement("div");
					element1.className = "commenttext";
					
					element2 = document.createElement("p");
					element2.setAttribute("id", "comment_"+i);
					
					element8 = document.createElement("div");
					element8.setAttribute("align", "right");
					element8.setAttribute("className", "replycomment"); // IE
					element8.setAttribute("class", "replycomment");
					element8.appendChild(document.createTextNode("» "));
					
					element9 = document.createElement("a");
					element9.setAttribute("href", "#reply");
					element9.setAttribute("id", "setreply_" + i);
					element9.appendChild(document.createTextNode("回應"));
					
					element8.appendChild(element9);
					
					element6 = document.createElement("a");
					element6.setAttribute("name", comment_id);
					element1.appendChild(element6);
					
					if (icon) {
						element3 = document.createElement("img");
						element3.setAttribute("src", icon);
						element3.setAttribute("width", 30);
						element3.setAttribute("height", 30);
						element3.setAttribute("border", 0);
						element3.setAttribute("align", "absmiddle");
						element3.setAttribute("alt", name);
						element1.appendChild(element3);
					}
					
					element4 = document.createElement("span");
					element4.setAttribute("id", "name_" + comment_id);
					element4.appendChild(document.createTextNode(name));
					
					element1.appendChild(document.createTextNode(' '));
					element1.appendChild(element4);
					element1.appendChild(document.createTextNode(' 於 ' + date));
					if (pid != 0) {						
						element7 = document.createElement("a");
						element7.setAttribute("href", "#" + pid);
						element7.setAttribute("ClassName", "replyname"); // IE
						element7.setAttribute("Class", "replyname");
						
						element5 = document.createElement("span");
						element5.setAttribute("id", "reply_" + i);
						element5.setAttribute("title", pid);
						element5.setAttribute("ClassName", "replyname"); // IE
						element5.setAttribute("Class", "replyname");
						
						element7.appendChild(element5);
						
						element1.appendChild(document.createTextNode(' 回覆 '));
						element1.appendChild(element7);
					}
					element1.appendChild(element2);
					element1.appendChild(element8);
					element0.appendChild(element1);
					
				}
				footer = document.createElement("div");
				footer.className = "commentfooter";
				footer.appendChild(document.createTextNode('共 ' + len + ' 則回應'));				
				element0.appendChild(footer);
				
				document.getElementById("article_comment").appendChild(element0);
				document.getElementById('article_comment').style.display = "block";				
				
				var reg = new RegExp("\n","g");
				for (i=0; i<len; i++){
					var comment = nodes[i].getElementsByTagName("comment").item(0).firstChild.nodeValue;
					var comment_id = nodes[i].getElementsByTagName("commentid").item(0).firstChild.nodeValue;
					document.getElementById("comment_"+i).innerHTML = comment.replace(reg, '<br />');
					if (document.getElementById("reply_"+i)) {
						var rid = document.getElementById("reply_"+i).title;
						var uname = document.getElementById("name_"+rid).innerHTML;
						document.getElementById("reply_"+i).innerHTML = uname;
						document.getElementById("reply_"+i).title = uname;
					}
					makeEvent(document.getElementById("setreply_"+i), PostReply, comment_id, "click");
				}
				
			} else {
				//alert('此文章尚無回應。');
			}
		} else {
			//alert('檢視功能無效。');
		}
	}
}

function makeEvent(element, callback, param, eventtype) {
	function local() {
		return callback(param);
	}
	if (element.addEventListener) {
		element.addEventListener(eventtype, local, false);
	} else if (element.attachEvent) {
		element.attachEvent("on"+eventtype, local);
	}
}

function ReplyCheck() {
	if (showform == false) {
		HttpRequest('ReplyCheckResult', '/replycheck.php?url=' + encodeURIComponent(document.location.href));
		showform = true;
	}
}

function ViewReplies() {
	if (showreply == false) {
		HttpRequest('ViewRepliesResult', '/getreply.php?url=' + encodeURIComponent(document.location.href));
		showreply = true;
	}
}

function GetNewRepliesResult(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var reply = http_request.responseText;			
			if (reply) {
				var menu = document.getElementById("sidemenu").innerHTML;
				document.getElementById("sidemenu").innerHTML = menu + reply;
			}
		}
	}
}

function GetNewReplies() {
	if (shownewreplies == false) {
		HttpRequest('GetNewRepliesResult', '/getnewreply.php?url=' + encodeURIComponent(document.location.href));
		shownewreplies = true;
	}
}

function PostReply(rid) {
	document.getElementById("reply_id").value = rid;
	uname = document.getElementById("name_" + rid).innerHTML;
	document.getElementById("reply_method").innerHTML = "您準備回應 " + uname + " 的意見嗎? 請輸入您的名稱與意見內容";
	document.getElementById("name").focus();
}

function addListener(element, event, listener, bubble) {
	if(element.addEventListener) {
		if(typeof(bubble) == "undefined") bubble = false;
		element.addEventListener(event, listener, bubble);
	} else if(this.attachEvent) {
		element.attachEvent("on" + event, listener);
	}
}

addListener(this, "load", function() { ReplyCheck(); ViewReplies(); GetNewReplies(); });
addListener(window, "load", function() { ReplyCheck(); ViewReplies(); GetNewReplies(); });
addListener(document, "load", function() { ReplyCheck(); ViewReplies(); GetNewReplies(); });
addListener(document, "DOMContentLoaded", function() { ReplyCheck(); ViewReplies(); GetNewReplies(); });

document.onload = function() { ReplyCheck(); ViewReplies(); GetNewReplies(); }
window.onload = function() { ReplyCheck(); ViewReplies(); GetNewReplies(); }
this.onload = function() { ReplyCheck(); ViewReplies(); GetNewReplies(); }