
//COMENTARIOS A NOVIDADES

//FUNCAO AJAX PARA INSERIR UM COMENTARIO E NO FIM ACTUALIZAR A TIMELINE
function addUpdateComment(updateID) {
	var updateID = document.getElementById('comment_update_id_' + updateID).value;
	var comment  = document.getElementById('comment_update_' + updateID).value;
	if (comment == '') {
		return;
	}
	var xmlHttp;
	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlHttp.onreadystatechange = function () {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				//TODO MAIS TARDE MUDAR ISTO PARA UM INSERT CHILD
				document.getElementById('cont_update_comments_' + updateID).innerHTML = document.getElementById('cont_update_comments_' + updateID).innerHTML + xmlHttp.responseText;
				xmlHttp.abort();
			}
			else {
				/*SHOW ERROR MESSAGE*/
				return;
			}
		}
	};

	var params = 'update_id=' + updateID + '&update_comment=' + comment.replace(/&/g, '%26');
	xmlHttp.open("POST",'comentarioupdate.html',true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length.toString());
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	document.getElementById('comment_' + updateID).style.display = 'none';
	document.getElementById('comment_update_' + updateID).value = '';
}

//FUNCAO AJAX PARA MOSTRAR MAIS NOVIDADES NA TIMELINE
function timelineShowMore(page, type, showMoreDivId, userOnly, showComments, userID) {
	document.getElementById(showMoreDivId).innerHTML = 'aguarde...';
	var xmlHttp;
	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlHttp.onreadystatechange = function () {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				document.getElementById('cont_timeline').removeChild(document.getElementById(showMoreDivId));
				document.getElementById('cont_timeline').innerHTML += xmlHttp.responseText;
				xmlHttp.abort();
			}
			else {
				/*SHOW ERROR MESSAGE*/
				return;
			}
		}
	};

	var params = 'page=' + page + '&type=' + type + '&timeline=1' + '&userOnly=' + userOnly + '&showComments=' + showComments + '&userID=' + userID;
	xmlHttp.open("POST",'timeline.html',true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length.toString());
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function deleteUpdate(updateID) {
	//document.getElementById(showMoreDivId).innerHTML = 'aguarde...';
	document.getElementById('cont_timeline').removeChild(document.getElementById('cont_update_' + updateID));
	var xmlHttp;
	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlHttp.onreadystatechange = function () {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				xmlHttp.abort();
			}
			else {
				/*SHOW ERROR MESSAGE*/
				return;
			}
		}
	};

	var params = 'update_id=' + updateID;
	xmlHttp.open("POST",'apagarupdate.html',true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length.toString());
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}
