/*
@description JS for  comments
@author DC
@created 2010-02
*/

$(function(){
	$('#comments-form').submit(function(eventObj){
		//validate
		if($('#comment_name').val() == ""){
			alert('Please provide your name');
			return false;
		}else if($('#comment_text').val() == ""){
			alert('Please provide your comment');
			return false;
		}else if($('#comment_email').val() == "" || !(/^([a-zA-Z0-9-_\+\']+\.)*[a-zA-Z0-9-_\+\']+@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]+$/).test($('#comment_email').val())) {
			alert('Please provide your email address');
			return false;
		}else{
			$('.comments-form-wrapper .submit').attr("disabled","disabled");
			//replace new lines with brs before posting
			var commenttext = $('#comment_text').val().replace(/\n/g,'<br />');
			//submit form
			$.post($('#comments-form').attr('action'), { 
				comment_name: $('#comment_name').val(), 
				comment_jobtitle: $('#comment_jobtitle').val(), 
				comment_organisation: $('#comment_organisation').val(), 
				comment_text: $('#comment_text').val(), 
				comment_ht_email: $('#comment_ht_email').val(),  
				comment_email: $('#comment_email').val(), 
				comment_article_id: $('#comment_article_id').val()
			}, function(){
				$('#comments .comments-container ul').append('<li><p class="quote">'+commenttext+'</p><p class="author"><strong>'+ $('#comment_name').val()+'</strong> '+ $('#comment_jobtitle').val() +' '+$('#comment_organisation').val()+'<br /><span class="date">Today</span></p></li>');
				/*
				//callback
				tb_show("", "/applications/comments/complete.rm?is_ajax=1&id=" + $('#comment_article_id').val() + "&height=200&width=300");
				sIFR.rollback("#TB_ajaxContent h1");
				applySifr();
				*/
				
				if(!$('#comment_name').hasClass("prepopulated")){
					$('#comment_name').val('');
				}
				if(!$('#comment_email').hasClass("prepopulated")){
					$('#comment_email').val('');
				}
				$('#comment_jobtitle').val('');
				$('#comment_text').val('');
				$('.comments-form-wrapper .submit').removeAttr("disabled");
			});
			return false;
		}
	});

});

