$(document).ready(function()
{
    //Captcha IMG
    $('#xxx_change-image').click(function()
    {
        document.getElementById('xxx_captcha').src = 'http://kamera.mexpo.pl/captcha.php?' + Math.random();
        return false;
    });

    var html = '<textarea id="xxx_question_content" name="xxx_question_content" cols="50" rows="12"></textarea>';
    $('.xxx_textarea').append(html);

    // Location
    $('a.xxx_go_to_question_form').click(function()
    {
        $('html, body').animate(
        {
        scrollTop: $('.xxx_question_form').offset().top
        },
        2000
        );
    });

    //Message
    var href_location = location.href;
    var params = href_location.split("?");

    var success = params[1];
    if(success)
    {
        var infos = success.split("&");
        $.each(infos, function(key, value) { 
            var info = value.split("=");
            if(info[0] == "sucess")
            {
                if(info[1] == "ok")
                    alert('Zapytanie zostało wysłane');
                else if(info[1] == "error")
                    alert('Wystąpił błąd');
                else if(info[1] == "captcha_error")
                    alert('Wpisano błędny kod');
            }
            else
                $('#' + info[0]).val(decodeURI(info[1]));
        });
    }
    // Validation
    $('#xxx_send_question').click(function(e)
    {
        var result = true;
        var email_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        var phone_filter = /\d./;
        
        $('#xxx_question_content_label').remove();
        if($('#xxx_question_content').val() == '')
        {
            $('#xxx_question_content_span').after("<label id='xxx_question_content_label' style='color: red'> Pole wymagane</label>");
            result = false;
        }

        $('#xxx_name_and_surname_label').remove();
        if($('#xxx_name_and_surname').val() == '')
        {
            $('#xxx_name_and_surname_span').after("<label id='xxx_name_and_surname_label' style='color: red'> Pole wymagane</label>");
            result = false;
        }

        $('#xxx_company_name_label').remove();
        if($('#xxx_company_name').val() == '')
        {
            $('#xxx_company_name_span').after("<label id='xxx_company_name_label' style='color: red'> Pole wymagane</label>");
            result = false;
        }

        $('#xxx_email_label').remove();
        if($('#xxx_email').val() == '')
        {
            $('#xxx_email_span').after("<label id='xxx_email_label' style='color: red'> Pole wymagane</label>");
            result = false;
        }
        else if(!email_filter.test($('#xxx_email').val()))
        {
            $('#xxx_email_span').after("<label id='xxx_email_label' style='color: red'> Niepoprawny adres</label>");
            result = false;
        }

        $('#xxx_phone_label').remove();
        if(!phone_filter.test($('#xxx_phone').val()) && $('#xxx_phone').val() != '')
        {
            $('#xxx_phone_span').after("<label id='xxx_phone_label' style='color: red'> Niepoprawny numer</label>");
            result = false;
        }

        //Captcha
        $('#xxx_captcha_text_label').remove();
        if($('#xxx_captcha_text').val() == '')
        {
            $('#xxx_captcha_text').after("<label id='xxx_captcha_text_label' style='color: red'> Pole wymagane</label>");
            result = false;
        }
        return result;
    });
});
