
/*
	Comments:
*/

/*
	Show the Comment Toolbar | user-comments.php
*/

function uc_ShowToolbar(i, hType)
{
	var ucTB = E('uctb_'+i);

	ucTB.style.display = (hType == 'in') ? 'block' : 'none';
}

function uc_ShowComment(i)
{
	var ucComm = E('uccomm_'+i);

	ucComm.style.display = (ucComm.style.display != 'block') ? 'block' : 'none';
}


/*
	Add Comment | comment-overview.php

	tableId: main id of target-table, located in the table `comm_table_config`
	pTarget: main id of a record, located in the target-table
*/

function comment_Add(tableId, pTarget)
{
	var postContent = E('postContent_preview');
	var newContent = ajax_encode(postContent.value);

	var newSeq = 0;

	while(E('postItem_'+newSeq))
		newSeq++;
	
	var postParam = "action=comment_add&tableId="+tableId+"&pTarget="+pTarget+"&comment_seq="+newSeq+"&comment_content="+newContent;

	var xmlHttp = createXMLHttpRequest();
	xmlHttp.onreadystatechange = function(){comment_RequestAction(xmlHttp, 'add');};
	xmlHttp.open("POST", WSD_WEBROOT+"users/comment-add.php", true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.setRequestHeader("Content-length", postParam.length);
	xmlHttp.setRequestHeader("Accept-Charset", "UTF-8");
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(postParam);

	return false;
}


/*
	Preview Comment | comment-overview.php

	activated from the preview textarea
*/

function comment_Preview()
{
	var postContent = E('postContent_preview');
	var newContent = ajax_encode(postContent.value);

	var postParam = "action=comment_preview&comment_content="+newContent;

	var xmlHttp = createXMLHttpRequest();
	xmlHttp.onreadystatechange = function(){comment_RequestAction(xmlHttp, 'preview');};
	xmlHttp.open("POST", WSD_WEBROOT+"users/comment-preview.php", true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.setRequestHeader("Content-length", postParam.length);
	xmlHttp.setRequestHeader("Accept-Charset", "UTF-8");
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(postParam);

	return false;
}


/*
	Edit Comment (toggle) | comment-display.php

	i: unique number to identify and modify the div containing the comment
	pId: the main id of the comment, located in `comm_comments`, passed through ajax when submitting changes
*/

function comment_Edit(i, pId)
{
	var postInner = E('post_'+i);

	var editBox = E('postEdit_'+i);
	var postContent = E('postContent_'+i);

	// Edit Mode
	if(postInner.style.display != 'none')
	{
		// switch real/edit:
		postInner.style.display = 'none';
		editBox.style.display = 'block';
		
		// preview text:
		E('postPrev_'+i).style.display = 'none';
	}

	// Preview Mode
	else
	{
		var newContent = ajax_encode(postContent.value);

		var postParam = "action=comment_parse&comment_content="+newContent;

		var xmlHttp = createXMLHttpRequest();
		xmlHttp.onreadystatechange = function(){comment_RequestAction(xmlHttp, 'preview_edit', i);};
		xmlHttp.open("POST", WSD_WEBROOT+"users/comment-parse.php", true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp.setRequestHeader("Content-length", postParam.length);
		xmlHttp.setRequestHeader("Accept-Charset", "UTF-8");
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(postParam);
	}

	return false;
}


/*
	Submit and Save Changes (from edit-toggle) | comment-display.php

	i: unique number to identify and modify the div containing the comment
	pId: the main id of the comment, located in `comm_comments`, passed through ajax when submitting changes
*/

function comment_Submit(i, pId)
{
	if(!E('postEdit_'+i))
	{
		return false;
	}

	var editBox = E('postEdit_'+i);
	var postContent = E('postContent_'+i);

	var newContent = ajax_encode(postContent.value);

	var postParam = "action=comment_edit&pId="+pId+"&comment_content="+newContent;

	var xmlHttp = createXMLHttpRequest();
	xmlHttp.onreadystatechange = function(){comment_RequestAction(xmlHttp, 'edit_finish', i, pId);};
	xmlHttp.open("POST", WSD_WEBROOT+"users/comment-edit.php", true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.setRequestHeader("Content-length", postParam.length);
	xmlHttp.setRequestHeader("Accept-Charset", "UTF-8");
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(postParam);
}


/*
	Delete Comment | comment-display.php
*/

function comment_Delete(i, pId)
{
	var postParam = "action=comment_delete&pId="+pId;

	var xmlHttp = createXMLHttpRequest();
	xmlHttp.onreadystatechange = function(){comment_RequestAction(xmlHttp, 'delete', i, pId);};
	xmlHttp.open("POST", WSD_WEBROOT+"users/comment-delete.php", true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.setRequestHeader("Content-length", postParam.length);
	xmlHttp.setRequestHeader("Accept-Charset", "UTF-8");
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(postParam);

	return false;
}


/*
	Create Preview-comment from 'Edit Comment'

	called when AJAX request is successfull
*/

function comment_EditPreview(i, newContent)
{
	comment_UpdateContent(i, newContent);
	
	// preview text:
	E('postPrev_'+i).style.display = 'block';
}


/*
	Create Real-comment from 'Edit Comment'

	called when AJAX request is successfull
*/

function comment_SubmitFinish(i, newContent)
{
	comment_UpdateContent(i, newContent);

	// preview text:
	E('postPrev_'+i).style.display = 'none';
}


/*
	Update HTML Content for either preview or real
*/

function comment_UpdateContent(i, newContent)
{
	var postInner = E('post_'+i);
	var editBox = E('postEdit_'+i);

	postInner.innerHTML = newContent;

	// switch real/edit:
	postInner.style.display = 'block';
	editBox.style.display = 'none';
}


/*
	Create Preview Comment (from ajax request)
*/

function comment_CreatePreview(cContent)
{
	if(E('postItem_preview'))
	{
		E('commHolder').removeChild(E('postItem_preview'));
		E('commHolder').removeChild(E('postSep_preview'));
	}

	var postHolder = E('commHolder');

	postHolder.innerHTML = cContent + postHolder.innerHTML;
}


/*
	Add Real Comment (from ajax request)
*/

function comment_CreateReal(cContent)
{
	if(E('postItem_preview'))
	{
		E('commHolder').removeChild(E('postItem_preview'));
		E('commHolder').removeChild(E('postSep_preview'));
	}

	var postHolder = E('commHolder');

	postHolder.innerHTML = cContent + postHolder.innerHTML;
}


/*
	Comment Actions: Finish the Ajax Request
*/

function comment_RequestAction(xmlHttp, pAction, i, pId)
{
	if(xmlHttp.readyState == 4)
	{
        if(xmlHttp.status == 200)
		{
			var responseStatus = xmlHttp.responseText;

			if(!responseStatus.match(/^good(\:.*)?/gi))
			{
				alert("Error: "+responseStatus);
			}
			else
			{
				if(pAction == "preview")
				{
					cContent = responseStatus.replace(/^good(\:(.*))?/gi, '$2');

					comment_CreatePreview(cContent)
				}
				else if(pAction == "add")
				{
					cContent = responseStatus.replace(/^good(\:(.*))?/gi, '$2');

					comment_CreateReal(cContent)
				}
				else if (pAction == "preview_edit")
				{
					cContent = responseStatus.replace(/^good(\:(.*))?/gi, '$2');

					comment_EditPreview(i, cContent);
				}
				else if(pAction == "edit")
				{
					comment_Edit(i, pId);

					// hide preview message to overwrite the above function:
					E('postPrev_'+i).style.display = 'none';
				}
				else if (pAction == "edit_finish")
				{
					cContent = responseStatus.replace(/^good(\:(.*))?/gi, '$2');

					comment_SubmitFinish(i, cContent);
				}
				else if (pAction == "delete")
				{
					E('postItem_'+i).style.display = 'none';
					E('postSep_'+i).style.display = 'none';
				}
			}
		}
	}
}

function comment_UpdateCharacters()
{
	var cBox = E('postContent_preview');
	var charLeft = E('postCharLeft');
	var charTotal = E('postMaxLeft');

	var maxChar = charTotal.innerHTML;
	var currentChar = cBox.value.replace(/[\n]/gi, '\r\n').length;

	charLeft.innerHTML = parseInt(maxChar) - currentChar;
}

function bb_TogglePane(caller)
{
	var bbcodePane = document.getElementById("bbcodePane");

	if(bbcodePane.style.display != "block")
	{
		bbcodePane.style.display = "block";
	}
	else
	{
		bbcodePane.style.display = "none";
	}
}

function bb_AddCode ( str )
{
	var inputfield = document.getElementById('postContent_preview');
	inputfield.value += str;

	comment_UpdateCharacters();
}

/*
	end: Comments
*/


