$(document).ready(function() {
    
	$('#su_form').validate();
	
    //if submit button is clicked
    $('#su_submit').click(function () {       
         
    	if($('#su_form').valid()){
	        //Get the data from all the fields
	        var name = $('#su_name').val();
	        var email = $('#su_email').val();
	        
	
	        //organize the data properly
	        var data = 'name=' + encodeURIComponent(name) + '&email=' + encodeURIComponent(email);
	         
	        //disabled all the text fields
	        $('#su_submit').css('display','none');
	         
	        //start the ajax
	        $.ajax({
	            //this is the php file that processes the data and send mail
	            url: "/employees/subscribe",
	             
	            //GET method is used
	            type: "GET",
	 
	            //pass the data        
	            data: data,    
	             
	            //Do not cache the page
	            cache: false,
	             
	            //success
	            success: function (data) {             
	                //if process.php returned 1/true (send mail success)
	                if (data.error == undefined) {                 
	                	$('#su_result').html('You have been subscribed!');
	                	$('#su_name').val('');
	        	        $('#su_email').val('');
	                     
	                //if process.php returned 0/false (send mail failed)
	                } else {
	                	$('#su_submit').css('display','inline');
	                	alert(data.error);              
	                }
	            },
	            error:(function() { 
	            	$('#su_submit').css('display','inline');
	            	alert('Sorry, unexpected error. Please try again later.');
	            })
	
	        });
	         
	        //cancel the submit button default behaviours
	        return false;
    	}
	    });
}); 
