var bCaptchaValid = true;

var CaptchaEmpty = "Le code de securite est vide.";
var CaptchaDifferent = "Le code de securite est different.";
var CaptchaOK = "Le code de securite est correct.";

jQuery(document).ready(function () {
    if (document.getElementById('captcha_code')) {
        document.getElementById('captcha_code').onkeyup = function () {
            var oField = this;

            if (oField.value.length == 5) {
                jQuery.ajax({
                    type: 'GET',
                    url: '/ajax_commands/check_captcha_code.php',
                    data: 'captcha=' + oField.value + '&random_number=' + (new Date()).getDate(),
                    success: function (msg) {
                        if (msg == 'true') {
                            bCaptchaValid = true;
                            jQuery("#captcha_error_container").slideUp(1000);
                            jQuery("#captcha_row").hide();
                            jQuery("#captcha_ok_message").html(CaptchaOK);
                            jQuery("#captcha_ok").show();
                        }
                        else {
                            bCaptchaValid = false;

                            jQuery("#captcha_error_message").html(CaptchaDifferent);
                            jQuery("#captcha_error_container").slideDown(1000);
                            jQuery("#captcha_error_container").css('backgroundColor', 'yellow');
                            jQuery("#captcha_error_container").css('color', 'red');
                            jQuery("#captcha_error_container").css('fontWeight', 'bold');
                        }
                    }
                });
            }
        }
    }
});


function validateCaptchaField(oField) {
    var bFieldValid = true;

    if (oField.value == '') {
        bFieldValid = false;
        jQuery("#captcha_error_message").html(CaptchaEmpty);
        jQuery("#captcha_error_container").css('backgroundColor', 'yellow');
        jQuery("#captcha_error_container").css('color', 'red');
        jQuery("#captcha_error_container").css('fontWeight', 'bold');
        jQuery("#captcha_error_container").slideDown(1000);
    }

    return bFieldValid;
}

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

function getUrlParameter(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}
