
/* Function to submit a comment form using AJAX */

function ajaxComment(sLinkroot,sParams)
{
	
	var bError = false;
	
	/* Get form fields */
	var tbl_comment_name 			= Form.Element.getValue('tbl_comment_name');
	var tbl_comment_email 			= Form.Element.getValue('tbl_comment_email');
	var tbl_comment_comment			= Form.Element.getValue('tbl_comment_comment');
	var tbl_comment_x_id			= Form.Element.getValue('tbl_comment_x_id');
	var tbl_comment_taal			= Form.Element.getValue('tbl_comment_taal');
	var securityimage				= Form.Element.getValue('securityimage');

	
	
	/* Hide all error spans */
	$('tbl_comment_name_error').hide();
	$('tbl_comment_email_error').hide();
	$('tbl_comment_comment_error').hide();
	$('securityimage_error').hide();
	
	
	/* Check fields */
	if(tbl_comment_name == '')				{ showError('tbl_comment_name'); bError = true;} 
	else if(!emailCheck(tbl_comment_email))	{ showError('tbl_comment_email'); bError = true;} 
	else if(tbl_comment_comment == '')		{ showError('tbl_comment_comment'); bError = true;} 
	else if(securityimage == '')			{ showError('securityimage'); bError = true;} 
	
	
	/* Only submit if no error was found */
	if(!bError)
	{
	
		/* Build URL */
		var sUrl = sLinkroot+'/index.php?'+sParams+'&tbl_comment_taal='+tbl_comment_taal+'&tbl_comment_x_id='+tbl_comment_x_id+'&tbl_comment_comment='+encodeURIComponent(tbl_comment_comment)+'&tbl_comment_name='+encodeURIComponent(tbl_comment_name)+'&tbl_comment_email='+encodeURIComponent(tbl_comment_email)+'&securityimage='+encodeURIComponent(securityimage);
		
		/* Send AJAX request to page */
		new Ajax.Request(sUrl, 
		{
			method: 'get',
	  		onSuccess: function(oTransport) 
	  			{
	    		checkResponse(oTransport);
	  			}
				});	
	}
	else
	{
		return false;
	}
}



/* Check AJAX response for submitted comment form */

function  checkResponse(oTransport)
{
	var sResponse = oTransport.responseText;
	var bError = false;
	
	var aResponse = sResponse.split("(|)");
	if(aResponse[0] == "CODE_ERROR") { showError('securityimage'); bError = true;  } 
	else if(aResponse[0] == "OK") 
	{
		showComment(aResponse[1],aResponse[2],aResponse[3]);
		
	}
	
}


/* Display error and fail */
function showError(id)
{
	var errorid = id+'_error';
	
	$(errorid).show();
	Form.Element.focus(id);
}


/* Transfer input from form into hidden div */
function showComment(sName,sComment,sDate)
{
	$('newcomment_name').update(sName);
	$('newcomment_comment').update(sComment);
	$('newcomment_date').update(sDate);
	$('newcomment').show();
	$('commentform').hide();
	$('commenttitle').show();
}