jQuery.extend({
	/**
	 * Intercepts downloads to show capture form
	 */
	capture : function(object) {
		
		if(window.formUp != true){
			window.formUp = true;
			$.get('/call-back/call-back-form.php',function(data){
											  
				
				var captureForm = data;
				var clickedLink = object;
			
				var config = {
					'title'			:	"",
					'content'		:	captureForm,
					'closeable'		:	false
				};
				
				$.popup(config, [{
					'label'			:	'Submit',
					'action'		:	function() {
						var formData = $('#call_back_form').serialize();
						$.post("/call-back/form_validation.php", formData, function(response){	

							// Remove any errors
							$('.form_row').removeClass('invalid_row');
							$('#call_back_form').find('p.error').remove();
							
							if(response.status == "error") {
								$.each(response.errors, function(key){
									$('#' + key).parents('.form_row').addClass('invalid_row');
								});
		
								$('#call_back_form').prepend(response.feedback);
							
							}else{
	
								//window.location = "/index.php";
								$('#call_back_form').html("Thank you. Your details have been submitted.<br />We will contact you as soon as possible.<br />If you don't receive a callback within 3 hours (normal office hours GMT), please let us know by emailing us at <a href=\"customerservices@skopos.co.uk\">customerservices@skopos.co.uk</a> or calling 01924 436666");
								$('#popup_buttons button.small_button').remove();
								$('#popup_buttons button.cancel_form span').html("Close");
								$('#popup_buttons').addClass("close");
							
							}
							
							
						}, 'json');
						
					},
					'closePopup'	:	false	
					},
					{'label'		:	'Cancel',
					'buttonClass'	:	'cancel_form'}
					]
				);														   
			
			
			});
	
			return false;
		}
		
	}
	
});

