//	Requires jquery.blockUI.js and jquery.form.js.

//	Removes all white space from a string and returns the new string.
function white_space(data){
	return (data).replace(/^\s*|\s*$/g,'');
}

$(function(){
	//	Processes the Comment Form for Article Comments.
	
		//	pre-submit callback
		function preSubmit(formData, jqForm, options){
			var authorName = $("#author_name").val();
			var authorEmail = $("#author_email").val();
			var comment = $("#comment_content").val();
			var captchaText = $("#captcha_text").val();
			var errorMsg = '';
			
			if(authorName == ''){
				errorMsg = errorMsg + "Please enter your Name.\n";
			}
			
			if(authorEmail == ''){
				errorMsg = errorMsg + "Please enter your Email.\n";
			}
			/*else{
				$.ajax({
					type: "POST",
					url: "/processors/proc.emailValidator.php",
					data: "emailAddress=" + authorEmail,
					success: function(data){
						if(white_space(data) != 'true'){
							errorMsg = errorMsg + "Your email address is not valid.\n";
						}
					},
					async:false
				});
			}*/
			
			if(captchaText == ''){
				errorMsg = errorMsg + "Please enter the text provided in the image.\n";
			}
			else{
				$.ajax({
					type: "POST",
					url: "/processors/proc.captchaText.php",
					data: "captchaText=" + captchaText,
					success: function(data){
						if(white_space(data) != 'true'){
							errorMsg = errorMsg + "Invalid text entered.  Click the image to reset the text value.\n";
						}
					},
					async:false
				});
			}
			
			if(comment == ''){
				errorMsg = errorMsg + "Please enter your Comment.\n";
			}
			
			if(errorMsg != ''){
				alert(errorMsg);
				return false;
			}
			$.blockUI({ css: { 
	            border: 'none', 
	            padding: '15px', 
	            backgroundColor: '#000', 
	            '-webkit-border-radius': '10px', 
	            '-moz-border-radius': '10px', 
	            opacity: '.5', 
	            color: '#fff' 
	        } }); 
			return true;
		}
		//	post-submit callback
		function postSubmit(responseText, statusText){
			$.unblockUI();
			$('#frmComment').resetForm();
			alert(white_space(responseText));
		}
		$("#frmComment").ajaxForm({beforeSubmit:preSubmit, success:postSubmit, url:"/processors/proc.saveRPLComment.php", type:"post"});
	

});