
$(document).ready(function(){ 

	// validate an email address format
	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i);
		return pattern.test(emailAddress);
	}

	//if submit button is clicked  
     $('#comment_form').submit(function (event) {  
     	event.preventDefault();
         //Get the data from all the fields   
         var email 		= $('input[name=comment_email]');   
         var name 		= $('input[name=comment_username]');    
         var link 		= $('input[name=comment_userlink]');   
         var cinput 	= $('textarea[name=comment_content]');  
         var page 		= $('input[name=p]');    
         //Simple validation to make sure user entered something  
         //If error found, add hightlight class to the text field  
         if (email.val()=='' || !isValidEmailAddress(email.val()) ) {  
     		 alert('The email address you entered does not appear to be valid. Please check and try again.');
             email.addClass('hightlight'); 
             return false;  
         } else email.removeClass('hightlight'); 
         
         if (name.val()=='' ) {  
     		 alert('Please enter your name in the "Your Name" field.');
             name.addClass('hightlight'); 
             return false;  
         } else name.removeClass('hightlight');
         
         if (cinput.val()=='' ) {  
     		 alert('Please enter something in the "Comment" field.');
             cinput.addClass('hightlight'); 
             return false;  
         } else cinput.removeClass('hightlight');
         
         /*
         if (cinput.val()=='' ) {  
             cinput.addClass('hightlight');  
             return false;  
         } else cinput.removeClass('hightlight');
         */  
         var emailaddy = email.val();
         emailaddy.replace('@','-')
         //organize the data properly  
         var data = 'comment_email=' + email.val() + '&comment_username=' + name.val() + '&comment_userlink=' + link.val() + '&comment_content=' + cinput.val() + '&page_id=' + page.val();  
         
         //disabled all the text fields  
         $('input#comment_email').attr('disabled','true');  
         $('input#comment_userlink').attr('disabled','true');  
         $('input#comment_username').attr('disabled','true');  
         $('input#comment_input').attr('disabled','true');  
           
         //show the loading sign  
         //$('.loading').show();  
         //$("#captcha").replaceWith(waitimg);
         //start the ajax           	
         
         //console.log('Doing the ajax bit: '+siteurl+"comment/newcomment");
		
         $.ajax({  
             //this is the php file that processes the data and send mail  
             url: siteurl+"comment/newcomment",   
             //GET method is used  
             type: "POST",  
   
             //pass the data           
             data: data,       
               
             //Do not cache the page  
             cache: false,  
             
             //success  
             success: function (html) {   
             	
         	//console.log('Ajax returned OK');
                 //Error messaging is handled by the responding server, 
                 // so here we can just utput the result:
                 $("#comment_form").replaceWith(html);
             } ,
             
             error: function (stuff) {
             	alert( "Sorry, an error occurred. I was unable to store your comment sumission at this time." );
             	//console.log('ouch, didn\'t work: '+stuff.responseText);
             }
             
         });  
           
         //cancel the submit button default behaviours  
         return false;  
     });
	

});
