msg_noComment = 'Wpisz co najmniej 5 znaków.';
msg_thanksForComment = 'Komentarz został dodany.';

function postAComment(comment, type, id) {
	showDiv('postACommentStatus');
	element = getElem('postACommentStatus');
	
	if(type == 'USER'){
		getElem('postACommentContainer').style.height = '25px';
	}
	
	if (comment.length < 5) {
		element.style.padding = '3px 0 3px 0';
		element.style.background = '';
		element.innerHTML = '<span class="error">'+msg_noComment+'</span>';
	}
	else {
		element.style.padding = '0';
		element.style.background = '';
		document.postACommentForm.comment.disabled = true;
		document.postACommentForm.postACommentBtn.disabled = true;
		element.innerHTML = '<img src="/g/awLoading.gif" border="0" />';
		oConnector.SendPOSTCommand('postAComment', null, 'comment='+escape(encodeURI(comment))+'&type='+type+'&id='+id, postACommentCallBack);
	}
}

function changePACFieldsAvailability(){
	document.postACommentForm.comment.disabled = !document.postACommentForm.comment.disabled;
	document.postACommentForm.postACommentBtn.disabled = !document.postACommentForm.postACommentBtn.disabled;
}

function postACommentCallBack(mXml) {
	element = getElem('postACommentStatus');

	var oNode = mXml.SelectSingleNode('connector/postAComment');
	if (oNode == null) {
		element.innerHTML = '<span class="error">Serwer przeciążony.</span>';
		changePACFieldsAvailability();
		return;
	}
	
	var postACommentStatus	= oNode.attributes.getNamedItem('status').value;
	var type = oNode.attributes.getNamedItem('type').value;
		
	if (postACommentStatus == "wrongInputData") {
		//wrong data;
		element.innerHTML = '';
		changePACFieldsAvailability();	
	}
	else if(postACommentStatus == "commentTooShort") {
		element.style.padding = '3px 0 3px 0';
		element.style.background = '';
		element.innerHTML = '<span class="error">'+msg_noComment+'</span>';
		changePACFieldsAvailability();	
	}
	else if(postACommentStatus == "commentSent") {
		linkElem = getElem('postACommentStatus');
		
		linkElem.style.background = 'url(/g/status/successSmall.gif) no-repeat';
		linkElem.style.padding = '3px 0 3px 23px';
		
		linkElem.innerHTML = '<span class="green" >' + msg_thanksForComment + '</span>';
		
		commentArea = getElem('commentArea');
		commentArea.value = '';
		changePACFieldsAvailability();
		
		var commentsCount = oNode.attributes.getNamedItem('count').value;
		
		getElem('commentsCountId').innerHTML = commentsCount;
		
		getCommentsPage(type, oNode.attributes.getNamedItem('id').value, 1, commentsCount);
	}
}

function getCommentsPage(type, id, pageNo, count) {
	getElem('comments').innerHTML = '<div class="comLoading"><img src="/g/awLoading.gif"/></div>';
	oConnector.SendCommand( 'getCommentsPage', 'type='+type+'&id='+id+'&pageNo='+pageNo+'&count='+count, getCommentsPageCallBack );
}

function getCommentsPageCallBack(mXml) {
	var oNode = mXml.SelectSingleNode('connector/html');
	if (oNode == null) {
		getElem('comments').innerHTML = '<span class="error">Serwer przeciążony.</span>';
		return;
	}
	else {
		var text = oNode.firstChild.nodeValue;		
		//hideDiv('notificationsLoading');
		getElem('comments').innerHTML = text;	
	}
}

