/*
@description JS for blog comments

@author	Sally Edgington
@created 2009-03-18
*/

jQuery(document).ready(function(){

	//show or hide comments form on blog page
	jQuery('.comments-form-container.hideform').hide();
	jQuery('#add-comments li.post-comment a').click(function(event){
		jQuery('#add-comments li.post-comment').hide();
		jQuery('.comments-form-container.hideform').removeClass('active');
		jQuery(this).parent().addClass('active');
		jQuery('.comments-form-container.hideform').show();
		event.preventDefault();
	});
	
	//use ajax to submit blog comment form

	jQuery('#comments-form').submit(function(eventObj){
		//validate
		if(jQuery('#comment_name').val() == ""){
			alert('Please provide your name');
			return false;
		}else if(jQuery('#comment_text').val() == ""){
			alert('Please provide your comment');
			return false;
		}else if(jQuery('#comment_email').val() == "" || !(/^([a-zA-Z0-9-_\+\']+\.)*[a-zA-Z0-9-_\+\']+@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]+$/).test(jQuery('#comment_email').val())) {
			alert('Please provide your email address');
			return false;
		}else{
			//submit form
			jQuery.post( jQuery('#comments-form').attr('action'), { comment_name: jQuery('#comment_name').val(), comment_jobtitle: jQuery('#comment_jobtitle').val(), comment_text: jQuery('#comment_text').val(), comment_ht_email: jQuery('#comment_ht_email').val(),  comment_email: jQuery('#comment_email').val(), comment_article_id: jQuery('#comment_article_id').val() }, function(){
				//callback
				tb_show("", "/applications/comments/complete.rm?is_ajax=1&id=" + jQuery('#comment_article_id').val() + "&height=200&width=300");
				sIFR.rollback("#TB_ajaxContent h1");
				applySifr();
				
				if(!jQuery('#comment_name').hasClass("prepopulated")){
					jQuery('#comment_name').val('');
				}
				jQuery('#comment_jobtitle').val('');
				jQuery('#comment_text').val('');
				jQuery('#comment_email').val('');
			});
			return false;
		}
	});

});
