/* Create a new xmlHttpRequest object to talk to the Web server */
var xmlHttp = false;
try {
  xmlHttp = new ActiveXObject("Msxml2.xmlHttp");
} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.xmlHttp");
  } catch (e2) {
    xmlHttp = false;
  }
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
}

function submitComment() {
	var newsId = encodeURIComponent(document.getElementById("articleId").value);
	var commentContent = document.getElementById("commentContent").value;
	// Only go on if there are values for both fields
	if(commentContent.length>500||commentContent.length<10){
		alert('评论的长度必须大于10小于500');
		return;
	}
	commentContent = encodeURIComponent(commentContent);
	if ((newsId == null) || (newsId == ""))
		return;
	if ((commentContent == null) || (commentContent == ""))
		return;
	// Build the URL to connect to
	var url = "/article/submitComment?timeStamp=" + new Date().getTime();
	// Open a connection to the server
	xmlHttp.open("post",url,true);
	// Setup a function for the server to run when it's done
	var	query = "newsId=" + newsId
			+ "&commentContent=" + commentContent;
	//query=encodeURI(query);
	//query=encodeURI(query);
	xmlHttp.onreadystatechange = updateComments;
	xmlHttp.setRequestHeader("content-length",query.length);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.setRequestHeader("Connection", "close");
	// Send the request
	xmlHttp.send(query);
}

function updateComments() {
	if (xmlHttp.readyState == 4) {
		var response = xmlHttp.responseText;
		if(response == 'NOT_AUTHORIZE'){
			window.location = "/auth/login.faces";
		} else if(response == 'FAIL'){
			alert('error');
		} else {
			if(!window.location.href.endWith('#comments')){
				window.location.href= window.location.href + "#comments";
				window.location.reload();	
			} else {
				window.location.reload();
			}
			//window.location.reload(true);
		}
	}
}

