/**
 * @author alex
 */
$(document).ready(function()
{
	$("form#callback_form").validate({
										'ajax': 'captcha_check_mini',
										'url' : '/ajax/request_call',
										'ajax_callback' : 'result',
										'fieldErrorsCap' : false,
										'errorCallback' : 'got_error',
										'singleErrors' : false,
										'highlightReset' : { 'border': '1px solid #ABABAB' }
									});

	
	$("form#estimate_form").validate({
										'ajax': 'captcha_check',
										'url' : '/ajax/request_estimate',
										'ajax_callback' : 'result',
										'fieldErrorsCap' : false,
										'errorCallback' : 'got_error_2',
										'singleErrors' : false,
										'highlightReset' : { 'border': '1px solid #ABABAB' }
									});
});

function captcha_check_mini(param)
{
	var code = $('input#captcha_code').val();
	$.post('/ajax/captcha_check', {code: code}, function(success)
	{
		//run custom callback function
		if(success == 1)
		{
			$.post('/ajax/request_call', param, function(data)
			{
				//run custom callback function
				result(data);
			});
			$('#captcha').attr({'src': '/assets/captcha/securimage_show_2.php?sid=' + Math.random()});
			return false;
		}
		else
		{
			$('#captcha').attr({'src': '/assets/captcha/securimage_show_2.php?sid=' + Math.random()});
			$('input#captcha_code').css({'border':'1px solid red'});
			alert('Please make sure you have entered the right code and that you have not been trying to submit this form for too long.');
			return false;	
		}
	});
}

function captcha_check(param)
{
	var code = $('input#captcha_code').val();
	$.post('/ajax/captcha_check', {code: code}, function(success)
	{
		//run custom callback function
		if(success == 1)
		{
			$.post('/ajax/request_estimate', param, function(data)
			{
				//run custom callback function
				result(data);
			});
			$('#captcha').attr({'src': '/assets/captcha/securimage_show.php?sid=' + Math.random()});
			return false;
		}
		else
		{
			$('#captcha').attr({'src': '/assets/captcha/securimage_show.php?sid=' + Math.random()});
			$('input#captcha_code').css({'border':'1px solid red'});
			alert('Please make sure you have entered the right code and that you have not been trying to submit this form for too long.');
			return false;	
		}
	});
}

function result(data)
{
	if(data == "success")
	{
		alert("Thank you for your enquiry, we will endeavour to get back to you as soon as we can.");
	}
	else
	{
		alert("Unfortunately there was an issue with the form and we did not receive your enquiry, please try again.");
	}
}

function got_error()
{
	alert("Please supply us with your name, a valid number and your enquiry.");
}

function got_error_2()
{
	alert("Please supply us with all the required fields which are marked with red borders.");
}