
$(document).ready(function(){ 

	// Handle default field value
	jQuery('input#email').focus( function() {
		if(jQuery(this).val() === 'Your Email Address') {jQuery(this).val('');}
	});
	
	jQuery('input#email').blur( function() {
		if(jQuery(this).val() === '') {jQuery(this).val('Your Email Address');}
	});
	
	
	// 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  
     $('#mailinglistform').submit(function (event) {  
     	event.preventDefault();
         //Get the data from all the fields   
         var email 		= $('input[name=email]');   
         var category 	= $('input[name=category]');    
         var campaign 	= $('input[name=campaign]');   
         var cinput 	= $('input[name=cinput]');  
         //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 (cinput.val()=='' ) {  
             cinput.addClass('hightlight');  
             return false;  
         } else cinput.removeClass('hightlight');
         */  
         var emailaddy = email.val();
         emailaddy.replace('@','-')
         //organize the data properly  
         var data = 'email=' + email.val() + '&category=' + category.val() + '&campaign=' + campaign.val() + '&cinput=' + cinput.val();  
         
         //disabled all the text fields  
         $('input#email').attr('disabled','true');  
           
         //show the loading sign  
         //$('.loading').show();  
         //$("#captcha").replaceWith(waitimg);
         //start the ajax           	
         
         //console.log('Doing the ajax bit: '+siteurl+"mailinglist/newreg");

         $.ajax({  
             //this is the php file that processes the data and send mail  
             url: siteurl+"mailinglist/newreg",   
             //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:
                 $("#theform").replaceWith(html);
             } ,
             
             error: function (stuff) {
             	alert( "Sorry, an error occurred. I was unable to store your email submission at this time." );
             }
             
         });  
           
         //cancel the submit button default behaviours  
         return false;  
     });
	

});
