var leasing_form_submitting = false;
var leasing_form_used_buy = true;

function check_errors()
{
	var valid = true;

	if(!$("#FORM_FIELD_FIO").val())
	{
		valid = false;
		$("#FORM_FIELD_FIO_wrapper").addClass("error").attr("data-error", "Необходимо указать Ваши ФИО");
	}
	if(!$("#FORM_FIELD_PHONE").val())
	{
		valid = false;
		$("#FORM_FIELD_PHONE_wrapper").addClass("error").attr("data-error", "Необходимо указать номер Вашего телефона");
	}
	if(!$("#FORM_FIELD_EMAIL").val())
	{
		valid = false;
		$("#FORM_FIELD_EMAIL_wrapper").addClass("error").attr("data-error", "Необходимо указать Ваш адрес E-mail");
	}
	else
	{
		if(!is_email($("#FORM_FIELD_EMAIL").val()))
		{
			valid = false;
			$("#FORM_FIELD_EMAIL_wrapper").addClass("error").attr("data-error", "Пожалуйста, укажите правильный адрес E-mail");
		}
	}

	return valid;
}

function form_leasing_request_submit()
{
	var accept = $("#policy").is(':checked');
	if(!leasing_form_submitting && accept)
	{
		if(check_errors())
		{
			leasing_form_submitting = true;
			$("#send_button_text").css("display", "none");
			$("#send_button_spinner").css("display", "block");

			$("#form_leasing_request").find("input").removeClass("error");

			var formData = new FormData($("#form_leasing_request")[0]);
			formData.append("form", "FORM_LEASING_REQUESTS");
			formData.append("FORM_FIELD_PAGE_NAME", document.title);
			formData.append("FORM_FIELD_PAGE_URL", window.location.href);
			formData.append("FORM_FIELD_BRAND", $("#FORM_FIELD_BRAND").val() ? $("#FORM_FIELD_BRAND").val() : "");
			formData.append("FORM_FIELD_MODEL", $("#FORM_FIELD_MODEL").val() ? $("#FORM_FIELD_MODEL").val() : "");
			formData.append("FORM_FIELD_MODIFICATION", $("#FORM_FIELD_MODIFICATION").val() ? $("#FORM_FIELD_MODIFICATION").val() : "");
			formData.append("FORM_FIELD_PRICE", $("#FORM_FIELD_PRICE").val() ? $("#FORM_FIELD_PRICE").val() : "");
			formData.append("FORM_FIELD_PREPAID", $("#FORM_FIELD_PREPAID").val() ? $("#FORM_FIELD_PREPAID").val() : "");
			formData.append("FORM_FIELD_TERM", $("#FORM_FIELD_TERM").val() ? $("#FORM_FIELD_TERM").val() : "");
			formData.append("FORM_FIELD_REDEMPTION", $("#FORM_FIELD_REDEMPTION").val() ? $("#FORM_FIELD_REDEMPTION").val() : "");
			formData.append("FORM_FIELD_IS_IZT", $("#FORM_FIELD_IS_IZT").val() !== "" ? $("#FORM_FIELD_IS_IZT").val() : "");

			grecaptcha.ready(function() 
			{
				grecaptcha.execute($("meta[name=recaptcha_site_key]").attr("content"), { action: 'submit' }).then(function(token)
				{
					formData.append("recaptcha_token", token);

					$.ajax(
					{
						url: "/api/forms/", type: 'POST', success: function(response)
						{
							if(response.status === "complete")
							{
								$("#form_leasing_info").css("display", "none");
								$("#form_leasing_process").css("display","none");
								$("#form_leasing_success").css("display","flex");

								$("#send_button_text").css("display", "block");
								$("#send_button_spinner").css("display", "none");

								leasing_form_submitting = false;

								try
								{
									ym(66905533,'reachGoal','leasingform')
								}
								catch(e)
								{
									console.error(e);
								}
							}
							else if(response.status === "error")
							{
								for(var i in response.errors)
								{
									$("#form_leasing_request").find("*[name="+response.errors[i]+"]").addClass("error");
								}

								if(response.message === "recaptcha_error")
								{
									$("#form_leasing_info").css("display", "none");
									$("#form_leasing_process").css("display","none");
									$("#form_leasing_fail").css("display","flex").parent().css("backgroundColor", "#2C2D2E");	
								}

								$("#send_button_text").css("display", "block");
								$("#send_button_spinner").css("display", "none");

								leasing_form_submitting = false;
							}
						}, data: formData,
						dataType: "json",
						cache: false, contentType: false, processData: false
					});
				});
			});
		}
	}
}

$(function()
{
	$("#form_leasing_request").find("input").on("keypress", function()
	{
		$(this).parent().removeClass("error").attr("data-error", null);
	});

	$("#form_leasing_send_button").on("click", function()
	{
		form_leasing_request_submit();
	});

	$("#form_leasing_request").on("submit", function(event)
	{
		event.preventDefault();
		form_leasing_request_submit();
	});

	$("#policy").on("change", function()
	{
		if($(this).is(':checked'))
		{
			$("#leasing_send_button").attr("disabled", false);
		}
		else
		{
			$("#leasing_send_button").attr("disabled", true);
		}
	});
});