//-------------------------------------
// class ajaxCommentSystem
// by: Jason Lombardozzi
// Description:
//-------------------------------------
function ajaxCommentSystem( boardUrl )
{
	this.boardUrl = boardUrl;
}
	//-------------------------------------
	// ajaxCommentSystem::fetchCommentSystemHtml
	// Description:
	//-------------------------------------
	ajaxCommentSystem.prototype.writeItemHtmlTag = function( itemGuid )
	{
		var uniqueId = Math.round(Math.random()*1000000);
		document.writeln("<p id='ajaxCommentSystemTag-"+uniqueId+"'></p>");
		do_request_function = function()
		{
			//----------------------------------
			// Ignore unless we're ready to go
			//----------------------------------
			if ( !ajaxItemCommentCount.readystate_ready_and_ok() )
			{
				return;
			}
			//----------------------------------
			// INIT
			//----------------------------------
			var responseTxt = ajaxItemCommentCount.xmlhandler.responseText;
			if( responseTxt == null )
			{
				return;
			}
			document.getElementById("ajaxCommentSystemTag-" + uniqueId).innerHTML = responseTxt;
		};
		var ajaxItemCommentCount = new ajax_request();
		ajaxItemCommentCount.onreadystatechange( do_request_function );
		ajaxItemCommentCount.process( this.boardUrl+'/index.php?act=xmlout&do=fetchRssItemCommentHtml&itemGuid='+escape(itemGuid)+'&containerId='+escape(uniqueId) );
		return false;
	};

//-------------------------------------
// toggleIpsCommentBox
// Description:
//-------------------------------------
function toggleIpsCommentBox( id )
{
	if( !id ) return;
	if( open = document.getElementById("ipsCommentBox-"+id) )
	{
		if( open.style.display == 'none' )
		{
			open.style.display = '';
		}
		else
		{
			open.style.display = 'none';
		}
	}
};
//-------------------------------------
// submitComment
// Description:
//-------------------------------------
function submitComment( formId )
{
	if( !formId ) return;
	var formElement = document.getElementById("ipsCommentBox-form-"+formId);
	if( !formElement ) return;
	var queryString = '';

	for( i=0; i<formElement.childNodes.length; i++ )
	{
		if( formElement.childNodes[i].nodeName.toLowerCase() == 'input' || formElement.childNodes[i].nodeName.toLowerCase() == 'textarea' )
		{
			queryString = queryString + formElement.childNodes[i].name + '=' + escape(formElement.childNodes[i].value) + '&';
		}
	}

	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( !ajaxSubmitForm.readystate_ready_and_ok() )
		{
			return;
		}
		//----------------------------------
		// INIT
		//----------------------------------
		var responseTxt = ajaxSubmitForm.xmlhandler.responseText;

		if( responseTxt == null )
		{
			return;
		}

		// Close comment box
		var listBox = document.getElementById( "ipsCommentBox-list-"+formId );
		listBox.style.display = 'none';
		// Close reply box
		var replyBox = document.getElementById( "ipsCommentBox-reply-"+formId );
		replyBox.style.display = 'none';
		// Append the new post
		var list = document.getElementById("ipsCommentBox-list-" + formId);
		var listItem = document.createElement('li');
		listItem.innerHTML = responseTxt;
		list.childNodes[3].insertBefore( listItem, list.childNodes[3].firstChild );
		// Update counter
		var counterElement = document.getElementById("ipsCommentBox-count-" + formId );
		var regex  = new RegExp( '([0-9]+) comment\\(s\\)' );
		var results = counterElement.innerHTML.match( regex );
		counterElement.innerHTML = "<a href=\"javascript:toggleIpsCommentBox('list-"+formId+"');\">" + (parseInt(results[1])+1) + " comment(s)</a>";

		//if( results[1] == 0 )
		//{alert(1);
	//		counterElement.innerHTML = "<a href=\"javascript:toggleIpsCommentBox('list-"+formId+"');\">" + counterElement.innerHTML.replace( results[1], parseInt(results[1])+1 ) + "</a>";
		//}
		//else
		//{alert(2);
		//	counterElement.innerHTML = counterElement.innerHTML.replace( results[1], parseInt(results[1])+1 );
		//}
		// Empty post textarea

		var textAreas = document.getElementsByTagName('textarea');
		for( var i=0; i<textAreas.length;i++ )
		{
			if( textAreas.name='Post' )
			{
				textAreas[i].value= '';
			}
		}
	};

	var ajaxSubmitForm = new ajax_request();
	ajaxSubmitForm.onreadystatechange( do_request_function );
	ajaxSubmitForm.process( boardUrl + '/index.php?' + queryString );
};

// Fix XSS issue with www.
boardUrl = (location.href.match( /^(http:\/\/www\.)/ ) != null ) ? boardUrl.replace( /^(http:\/\/)/, "http://www."  ) : boardUrl;
var commentSystem = new ajaxCommentSystem( boardUrl );