
var GetCommentUrl = "getCommentData.php";
var SendCommentUrl = "sendCommentData.php";
var DeleteCommentUrl = "deleteComment.php";
//var lastIndex = 0;

//window.onload = initJavaScript;

function delete_comment(commId, artId) {
	var answer = confirm('Czy na pewno usun±æ ten komentarz?');
	if(answer) {
		param = 'i=' + commId;
		httpDeleteComment.open("POST", DeleteCommentUrl, true);
		httpDeleteComment.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		httpDeleteComment.send(param);
		httpDeleteComment.onreadystatechange = function () {
			if (httpDeleteComment.readyState == 4) {
				if (httpDeleteComment.status == 200) {
					receiveComments(artId); //refreshes the chat after a new comment has been added (this makes it more responsive)
				}
			}
		};
	}
	else return false;
	
	return true;
}

function initJavaScript(artId) {
	//document.forms['chatForm'].elements['chatbarText'].setAttribute('autocomplete','off'); //this non standard attribute prevents firefox' autofill function to clash with this script
	//checkStatus();
	receiveComments(artId); //initiates the first data query
}

//initiates the first data query
function receiveComments(artId) {
	var obj = document.getElementById("comments");
	param = 'i=' + artId;
	httpReceiveComments.open("POST",GetCommentUrl, true);
	httpReceiveComments.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	httpReceiveComments.send(param);
	
	httpReceiveComments.onreadystatechange = function () {
		if (httpReceiveComments.readyState == 4) {
			if (httpReceiveComments.status == 200) {
				//alert('index: ' + lastIndex + '\nresponse: ' + httpReceiveComments.responseText);
				obj.innerHTML = httpReceiveComments.responseText;
		    }
		}
	}; 
}

//function handlehhttpReceiveComments() {}

//stores a new comment on the server
function sendComment(artId) {
	//currentAuthor = document.getElementById("author_id").value;
	//currentArticle = document.getElementById("article_id").value;
	currentComment = document.getElementById("comment").value;
	//window.alert("art id = " + currentArticle + "; comment = " + currentComment);
	param = 'a=' + artId + '&c=' + currentComment;
	//window.alert(param);
	
	httpSendComment.open("POST", SendCommentUrl, true);
	httpSendComment.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	httpSendComment.send(param);
	
	httpSendComment.onreadystatechange = function () {
		if (httpSendComment.readyState == 4) {
			if (httpSendComment.status == 200) {
				//obj = document.getElementById("comments");
				//obj.innerHTML = "<table><tr><td> - " + httpSendComment.responseText; + "</td></tr></table>";
				receiveComments(artId); //refreshes the chat after a new comment has been added (this makes it more responsive)
			}
		}
	};
  	
  	document.getElementById("comment").value = '';
  	document.getElementById("comment").select();
  	checkStatus();
}

//function handlehhttpSendComment() {}

function checkStatus() {
	currentComment = document.getElementById("comment");
	oSubmit = document.getElementById("submit_comment");
	if (currentComment.value != '') {
		oSubmit.disabled = false;
	}
	else {
		oSubmit.disabled = true;
	}
}

function getHttpObject() {
	
    var xmlhttp;
    
	try { // Firefox, Opera 8.0+, Safari; create a javascript instance of the object.
		xmlhttp=new XMLHttpRequest();
	}
	catch (e) {
		try { // Internet Explorer
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); //If the Javascript version is greater than 5
		}
		catch (e) {
			try {
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); //If not, then use the older active x object.
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
    
	return xmlhttp;
}

// initiates the two objects for sending and receiving data
var httpReceiveComments = getHttpObject();
var httpSendComment = getHttpObject();
var httpDeleteComment = getHttpObject();