jQuery(document).ready(function(){
    
    var origAction = jQuery('#contactform').attr('action');
	
	jQuery('#contactform').submit(function(){
	
		var action = jQuery(this).attr('action');
		
		jQuery("#message").slideUp(750,function() {
		jQuery('#message').hide();
		
 		jQuery('#contactform #submit')
			.after('<img src="http://www.haveyougotit.nhs.uk/wp-content/themes/haveyougotit/images/ajax-loader.gif" class="loader" />')
			.attr('disabled','disabled');
		
		jQuery.post(action, { 
			name: jQuery('#name').val(),
			email: jQuery('#email').val(),
			phone: jQuery('#phone').val(),
			comments: jQuery('#comments').val(),
			month: jQuery('#month').val(),
			day: jQuery('#day').val(),
			year: jQuery('#year').val(),
			address: jQuery('#address').val(),
			verify: jQuery('#verify').val()
		},
			function(data){
                if ( data == 'less than 16' || data == 'more than 25')
                {
                    jQuery('#contactform').attr('action', origAction + '?ignore_age');
                    
                    var _id = '';
                    if ( data == 'less than 16' ) {
                        _id = '#less16';
                    } else {
                        _id = '#more25'
                    }
                    
                    jQuery.fancybox( jQuery(_id).html(), {
                        height : 100,
                        width : 400,
                        autoDimensions : false,
                        'onComplete'	:	function() {
                            jQuery("#fancybox-wrap a.cancel").click(function() {
                                jQuery('#contactform').attr('action', origAction);
                                    jQuery.fancybox.close();
                                    return false;
                            });
                            
                            jQuery('#fancybox-wrap a.continue').click( function() {
                                jQuery('#contactform').trigger('submit');
                                return false;
                            });
                        },
                        onClosed : function() { jQuery('#contactform').attr('action', origAction); }
                    });
                    
                    jQuery('#contactform img.loader').fadeOut('slow',function(){jQuery(this).remove()});
                    jQuery('#contactform #submit').attr('disabled','');
                }
				else
                {
                    jQuery.fancybox.close();
                    jQuery('#contactform').attr('action', origAction);
                    document.getElementById('message').innerHTML = data;
                    jQuery('#message').slideDown('slow');
                    jQuery('#contactform img.loader').fadeOut('slow',function(){jQuery(this).remove()});
                    jQuery('#contactform #submit').attr('disabled',''); 
                    jQuery('#contactform img.loader').fadeOut('slow',function(){jQuery(this).remove()});
                    if(data.match('success') != null) jQuery('#contactform').slideUp('slow');
                }
			}
		);
		
		});
		
		return false; 
	
	});
	
});
