function getUrlParameter(sParam) {
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            return sParameterName[1];
        }
    }
}

$('document').ready(function () {



    $("#order_select").change(function (e) {
        $("#order_form").trigger("submit");
    });


    $("#contact_datahandler_c").click(function (e) {
        $(this).parent().parent().removeClass("has-error");
        $("#contact_datahandler_error").hide();
    });





        $(".js-recommend-button").click(function (e) {
            var variant_id=$(this).data('variant_id');
            var price=$(this).data('price');

        $.ajax({
            'method': 'post',
            'url': '/ajax/get_recommend_layer',
            data: {
                'variant_id': variant_id
            },
            'dataType': 'json'
        }).done(function (resp) {

            if (resp.html != undefined) {
                $("#layers_div").html(resp.html);
                showPopup("recommend");
                recommInit();
                $("#recommend_price").val(price);

            }

            });
        });


    $(".js-question-button").click(function (e) {
        var variant_id=$(this).data('variant_id');

        $.ajax({
            'method': 'post',
            'url': '/ajax/get_question_layer',
            data: {
                'variant_id': variant_id
            },
            'dataType': 'json'
        }).done(function (resp) {

            if (resp.html != undefined) {
                $("#layers_div").html(resp.html);
                showPopup("question");
                questionInit();
            }

        });
    });



    $(".js-reg-layer-close").click(function (e) {

       $(".js-reg-layer-input").each(function (i, v) {
           $(v).val("");
           $(v).trigger("blur");
       });
       $("#layer_street_name").empty();
        $("#layer_street_name_error").hide();
        $("#layer_aszf_error").hide();
        $("#layer_aszf_error").parent().removeClass("has-error");
    });

    $("#contact-send").click(function (e) {

        var hiba=false;


        if ($("#contact_datahandler_c").is(":checked")) {
           $("#contact_datahandler").val(1);
        }
        else{
            $("#contact_datahandler").val("");
        }



            $.ajax({
                'method': 'post',
                'url': '/ajax/contactSend',
                data: {

                    'data': $("#contact_form").serialize()
                },
                'dataType': 'json'
            }).done(function (resp) {

                    if (resp.ok == 1) {
                        $("#contact_name").val("");
                        $("#contact_email").val("");
                        $("#contact_phone").val("");
                        $("#contact_subject").val("");
                        $("#contact_message").val("");
                        $("#contact_datahandler").attr("checked", false);
                        $("#message_title").html("Köszönjük!");
                        $("#message_text").html("Üzenetét továbbítottuk kollégánk számára, aki keresni fogja Önt a megadott elérhetőségen.");
                        showPopup("message");
                    }
                    else {
                    $.each(resp.errors, function (index, item) {
                        $("#" + index + "_error").html(item);
                        $("#" + index + "_error").show();
                        $("#" + index + "_error").parent().addClass("has-error");
                    });
                }

            });

    });


    $(".form-control").blur(function (e) {
        if (($(this).attr("type") != "checkbox") && ($(this).attr("type") != "radio")) {
            $(this).parent().find("span").hide();
            $(this).parent().removeClass("has-error");
        }
    });

    $(".js-newsletter-checkbox").click(function (e) {
        $(this).parent().parent().removeClass("has-error");
    });

    $(".js-checkbox").change(function (e) {
        if ($(this).is(':checked')) {
            $(".js-checkbox-error-span").hide();
            $(this).parent().parent().removeClass("has-error");
        }
    });



    $('[data-isint="1"]').keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .				// Allow: Ctrl+A						// Allow: home, end, left, right, down, up
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 192, 51]) !== -1 || (e.keyCode == 65 && e.ctrlKey === true) || (e.keyCode >= 35 && e.keyCode <= 40)) {
            // let it happen, don't do anything
            return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });


    $("#set_cookie_button").click(function (e) {

        var marketing_cookie=0;




        $.ajax({
            'method': 'post',
            'url': '/ajax/set_cookie',
            data: {
                'marketing_cookie': marketing_cookie
            },
            'dataType': 'json'
        }).done(function (resp) {

            if (resp.ok == 1) {
                $("#cookie_layer_div").hide();
            }

        });
    });

    $(".js-add-to-favourite").click(function (e) {

        var id = $(this).data("product-id");
        var self = this;

        $.ajax({
            'method': 'post',
            'url': '/ajax/add_to_favourite',
            data: {
                'id': id
            },
            'dataType': 'json'
        }).done(function (resp) {
            if ($("#kedvencek_oldal").length) {
                location.reload();
            } else {
                if (resp.add == 1) {
                    $(".fav_img[data-favid='" + id + "']").each(function (i, v) {
                        $(v).attr('src', $("#baseUrl").val() + '/public/img/icons/red/hearth-full.svg');
                    });
                } else {
                    $(".fav_img[data-favid='" + id + "']").each(function (i, v) {
                        $(v).attr('src', $("#baseUrl").val() + '/public/img/icons/red/hearth-empty.svg');
                    });
                }
            }
        });
    });


    $("#kereses").keypress(function (e) {
        if (e.keyCode==13){
            document.location.href="kereses_lista/?keresendo="+$("#kereses").val();
        }
    });

    $("#kereses").keyup(function (e) {
        keres();
    });

    $("#osszes_talalat_link").click(function (e) {
        $("#osszes_talalat_link").attr("href", "kereses_lista/?keresendo="+$("#kereses").val());
    });



});


function recommInit(){

    $(".recommend-form-control").blur(function (e) {
        if (($(this).attr("type") != "checkbox") && ($(this).attr("type") != "radio")) {
            $(this).parent().find("span").hide();
            $(this).parent().removeClass("has-error");
        }
    });

    $("#recommend_datahandling").click(function (e) {
            $("#recommend_datahandling_error").hide();
            $(this).parent().parent().removeClass("has-error");
    });


    $("#recommend_send").click(function (e) {

        $.ajax({
            'method': 'post',
            'url': '/ajax/recommendSend',
            data: {

                'data': $("#recommend_form").serialize()
            },
            'dataType': 'json'
        }).done(function (resp) {

            if (resp.ok == 1) {
                $("#recommend_div").hide();
                $("#recommend_ok_div").show();

            }
            else {

                $.each(resp.errors, function (index, item) {
                    $("#" + index + "_error").html(item);
                    $("#" + index + "_error").show();
                    $("#" + index + "_error").parent().addClass("has-error");
                });
            }

        })


    });
}


function questionInit(){

    $(".question-form-control").blur(function (e) {
        if (($(this).attr("type") != "checkbox") && ($(this).attr("type") != "radio")) {
            $(this).parent().find("span").hide();
            $(this).parent().removeClass("has-error");
        }
    });

    $("#question_datahandling").click(function (e) {
        $("#question_datahandling_error").hide();
        $(this).parent().parent().removeClass("has-error");
    });

    $("#question_send").click(function (e) {

        $.ajax({
            'method': 'post',
            'url': '/ajax/questionSend',
            data: {

                'data': $("#question_form").serialize()
            },
            'dataType': 'json'
        }).done(function (resp) {

            if (resp.ok == 1) {
                $("#question_div").hide();
                $("#question_ok_div").show();
            }
            else {

                $.each(resp.errors, function (index, item) {
                    $("#" + index + "_error").html(item);
                    $("#" + index + "_error").show();
                    $("#" + index + "_error").parent().addClass("has-error");
                });
            }

        })


    });
}

function keres(){

    if ($("#kereses").val().length > 2) {
        $.ajax({
            'method': 'post',
            'url': '/ajax/search',
            data: {
                'keresendo': $("#kereses").val()
            },
            'dataType': 'json'
        }).done(function (resp) {

            if (resp.ok) {
                if (resp.html) {



                    $("#search_layer_cont").html(resp.html);
                    // $(".search").addClass("sys");
                    showSearchBox();

                    if (resp.van_talalat) {
                        $(".js-search-footer").show();
                    } else {
                        $(".js-search-footer").hide();
                    }


                }
            }


        });
    }
}




