$(function(){
    $('#change-form')
    .jqTransform()
    .validate({
        submitHandler: function(form) {
            if (validateCaptcha() === false) {
                console.debug("captche incorrect");
                document.getElementById("captcha_error").style.display = "block";
                return false;
            }
            $(form).ajaxSubmit({
                success: function() {
                    $('#change-form').hide();
                    $('#page-wrap').append("<p class='thanks'>Děkujeme za odeslání emailu.</p>")
                }
            });
        }
    });
});

$("#captcha_errorInput").keydown(function() {
    document.getElementById("captcha_error").style.display = "none";
});

function validateCaptcha() {
    var captchaValue = document.getElementById("captcha_code").value;
    var response = $.ajax({
        url: "validate.php?captcha_code=" + captchaValue,
        async: false
    }).responseText;

    var result = true;
    if (response != "OK") {
        result = false;
    }

    return result;
}

function reloadCaptcha() {
    document.getElementById("captcha_error").style.display = "none";
    document.getElementById("captcha_code").value = "";
    document.getElementById('captcha').src = 'securimage/securimage_show.php?' + Math.random();
    return false;
}