var commentHttp;

function submitComment(which, id) {	
	//alert("submitComment which: "+which+" id: "+id);
	//var dName = document.commentForm.dName.value;
	var dName = document.forms["commentForm"].elements["dName"].value;
	//var msg = document.commentForm.myComment.value;
	var msg = document.forms["commentForm"].elements["myComment"].value;

	document.getElementById("commentMsg").innerHTML="";
	if (msg.length==0) { 
		document.getElementById("commentMsg").innerHTML="What are you want to comment?";
		return;
	}
	if (dName.length==0) { 
		document.getElementById("commentMsg").innerHTML="Who are you?";
		return;
	}
	if (which.length==0) { 
		document.getElementById("commentMsg").innerHTML="Error: No comment type";
		return;
	}

	//if(debug){
		/*if (!memberLogin) {
			document.getElementById("commentMsg").innerHTML ="Please log in to post your comment";
			return;
		}*/
	/*}else{
		var uEmail = getCookie("userEmail")
		if (uEmail.length==0)
		{ 
			document.getElementById("commentMsg").innerHTML="Please log in to post your comment"
			return
		}
	}*/

	commentHttp = GetXmlHttpObject();

	if (!commentHttp) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var url="postComment.php";

	var params = "dName="+dName;
	params += "&myComment="+encodeURIComponent(msg);
	params += "&commentType="+which;
	params += "&id="+id;
	params += "&rand="+getCurrMsec();
	
	url += "?" + params;
	//if(debug)
		//url=url+"&forumMode=1";
	//alert(url);
	//document.getElementById("box_debug").innerHTML = url;
	commentHttp.onreadystatechange = handlePostComment;
	commentHttp.open("POST", url, true);	
	commentHttp.send(null);
	/*commentHttp.setrequestheader("content-length", params.length);
	commentHttp.setRequestHeader("Content-Type", 'application/x-www-form-urlencoded');
	commentHttp.setRequestHeader("Connection", "close");
	commentHttp.send(params);*/
} 

function handlePostComment() { 	
	if (commentHttp.readyState==4 || commentHttp.readyState=="complete") { 		
		//alert(commentHttp.responseText);
		var res_array = commentHttp.responseText.split("|");
		if (res_array[0] == "0") {
			document.getElementById("commentMsg").innerHTML=res_array[1];
		} else {
			document.getElementById("commentMsg").innerHTML="<br>Thanks for your comment!";
			document.commentForm.reset();
			//alert("res_array[1]: "+res_array[1]+", res_array[2]: "+res_array[2]);
			showComment(res_array[1], res_array[2], true);
			if (res_array[3] != undefined) {
				setCCPoint(res_array[3]);
			}
		}
	} 
} 

function showComment(which, id, afterPost) {
	//alert('showComment: '+which+' '+id);
	if (!afterPost) {	
		document.getElementById("commentMsg").innerHTML="";
	}
	if (which == null) {
		document.getElementById("commentMsg").innerHTML="#Error in loading comment page";
		return;
	}
	if (which.length==0) { 
		document.getElementById("commentMsg").innerHTML="#Error in loading comment page";
		return;
	}
	
	/*commentHttp = GetXmlHttpObject();
	if (!commentHttp) {
		alert ("Browser does not support HTTP Request");
		return;
	} */

	var uName;
	//if(debug){
		uName = memberName;
	/*}else{
		uEmail = getCookie("userEmail");
		uName = getCookie("userName");
	}*/

	var url = 'Components/comment_box/box.php';
	url += '?type='+which;
	url += '&typeid='+id;
	url += '&quantity=5';
	url += '&submit=true';
	/*var url="comment_1024.php";
	url=url+"?commentType="+which;
	url=url+"&id="+id;
	url=url+"&userName="+uName;
	if (afterPost)
		url=url+"&afterPost=1";*/
	url=url+"&rand="+getCurrMsec();
	if (which == "comic")
		ajaxinclude(url, 'comic_comment_content');	
	else if (which == "game")
		ajaxinclude(url, 'game_comment_content');
	else if (which == "activity")
		ajaxinclude(url, 'activity_comment_content');
	
	//if(debug)
		//url=url+"&forumMode=1";
	/*if (screen.width >= 1024) {	
		url = url + "&screen=1024";		
	}*/
	
	/*commentHttp.onreadystatechange=handleShowComment
	commentHttp.open("GET",url,true)
	commentHttp.send(null)*/
}

function handleShowComment() {	
	if (commentHttp.readyState==4 || commentHttp.readyState=="complete") { 
		//alert(commentHttp.responseText);
		
		/*var res_array = commentHttp.responseText.split("|");
		alert('handleShowComment '+res_array[3]);
		if (res_array[0] == "0") {
			document.getElementById("commentMsg").innerHTML="Error in showing comment: "+res_array[2];
		} else {			
			//document.getElementById("comment_content").innerHTML=res_array[2];
			ajaxinclude(res_array[3], 'comment_content');
		}*/
		clearPreloadPage();
	} 
} 