/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/* NAVIGATION */

$(document).ready(function () {
    // Add smooth scrolling to all links
    $("a").on('click', function (event) {

        // Make sure this.hash has a value before overriding default behavior
        if (this.hash !== "") {
            // Prevent default anchor click behavior
            event.preventDefault();

            // Store hash
            var hash = this.hash;

            // Using jQuery's animate() method to add smooth page scroll
            // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
            $('html, body').animate({
                scrollTop: $(hash).offset().top
            }, 800, function () {

                // Add hash (#) to URL when done scrolling (default click behavior)
                window.location.hash = hash;
            });
        } // End if
    });
});

$(window).scroll(function () {
    if ($(window).width() > 960) {
        if ($(this).scrollTop() > 1) {
            $('.navbar').addClass("navbar-shrink");
//            $(".navbar-brand>img").css("width", "80%");
//            $(".navbar-brand").css("margin-top", "-10px");
//            $(".navbar-right").css("margin-top", "7px");
//            $(".pageNav .nav>li>a").css("color", "#ffffff");
//            $(".shoppingCartBoxProduct").css("border-left", "1px solid #ffffff");
        } else {
            $('.navbar').removeClass("navbar-shrink");
//            $(".navbar-brand>img").css("width", "100%");
//            $(".navbar-brand").css("margin-top", "-25px");
//            $(".navbar-right").css("margin-top", "0px");
//            $(".pageNav .nav>li>a").css("color", "#414042");
//            $(".shoppingCartBoxProduct").css("border-left", "1px solid #06438f");
        }
    } else {
        $(".navbar-fixed-top").css("position", "absolute");
    }

});

$(".faq-section__accordion .faq-section__title").on("click", function () {
    if ($(this).hasClass("active")) {
        $(this).removeClass("active");
        $(this).siblings(".faq-section__content").slideUp(400);
        $(".faq-section__accordion > .faq-section__title i").removeClass("faq-section__opened").addClass("faq-section__closed");
    } else {
        $(".faq-section__accordion > .faq-section__title i").removeClass("faq-section__opened").addClass("faq-section__closed");
        $(this).find("i").removeClass("faq-section__closed").addClass("faq-section__opened");
        $(".faq-section__accordion > .faq-section__title").removeClass("active");
        $(this).addClass("active");
        $(".faq-section__content").slideUp(400);
        $(this).siblings(".faq-section__content").slideDown(400);
    }
});

$(".faq-section__accordion2 .faq-section__title2").on("click", function () {
    if ($(this).hasClass("active")) {
        $(this).removeClass("active");
        $(this).siblings(".faq-section__content2").slideUp(400);
        $(".faq-section__accordion2 > .faq-section__title2 i").removeClass("faq-section__opened2").addClass("faq-section__closed2");
    } else {
        $(".faq-section__accordion2 > .faq-section__title2 i").removeClass("faq-section__opened2").addClass("faq-section__closed2");
        $(this).find("i").removeClass("faq-section__closed2").addClass("faq-section__opened2");
        $(".faq-section__accordion2 > .faq-section__title2").removeClass("active");
        $(this).addClass("active");
        $(".faq-section__content2").slideUp(400);
        $(this).siblings(".faq-section__content2").slideDown(400);
    }
});

$.fn.equalHeights = function () {
    var max_height = 0;

    $(this).each(function () {
        max_height = Math.max($(this).height(), max_height);
    });
    $(this).each(function () {
        $(this).height(max_height);        
    });
};

function openMainNav() {
    document.getElementById("mobileSidepanel").style.width = "100%";
}

function closeMainNav() {
    document.getElementById("mobileSidepanel").style.width = "0";
}

function openNav() {
    document.getElementById("mySidepanel").style.width = "250px";
}

function closeNav() {
    document.getElementById("mySidepanel").style.width = "0";
}


/* RETINA */

$('body').find('img[retina]').each(function () {
    var origImg = $(this);
    var imgSRC = origImg.attr("src");
    var imgSRCSET = "";

    imgSRC = imgSRC.replace(".png", "");

    imgSRCSET = imgSRC + ".png 1x, " + imgSRC + "@2x.png 2x, " + imgSRC + "@3x.png 3x";
    origImg.attr("srcset", imgSRCSET);
});


///* CRITERY SORT */
//var tmp = [];
//$("input[name='Subcategory']").change(function () {
//
//    var checked = "CategoryID = '" + $(this).val() + "'";
//
//    if ($(this).is(':checked')) {
//        tmp.push(checked);
//    } else {
//        tmp.splice($.inArray(checked, tmp), 1);
//    }
//
//    var CategoryID = $("#CategoryID").val();
//    var OrderID = $("#OrderID").val();
//    var SubsubcategoriesArray = tmp;
//
//    $.post("/application/ajax/portal/setSubsubcategory.php", {
//        SubsubcategoriesArray: SubsubcategoriesArray,
//        CategoryID: CategoryID,
//        OrderID: OrderID
//    }, function (data, status) {
//        if (status == "success") {
//            $("#filterProductBox").html(data);
//        }
//    });
//
//});





function increaseValue() {
    var value = parseInt(document.getElementById('number').value, 10);
    value = isNaN(value) ? 0 : value;
    value++;
    document.getElementById('number').value = value;
}

function decreaseValue() {
    var value = parseInt(document.getElementById('number').value, 10);
    value = isNaN(value) ? 0 : value;
    value < 1 ? value = 1 : '';
    value--;
    document.getElementById('number').value = value;
}

function removeItem(Counter, ID) {

    $.post("/application/ajax/shop/deleteOrderProduct.php", {
        OrderID: ID,
    }, function (data, status) {
        if (status == "success") {
            $("#cartItem" + Counter).remove();
            calculateFinalPrice();
            calculateShippingPrice();
            location.reload();
        }
    });
}



function setInputDecimalNF(e) {
    this.inputDecimalValue = e
}

function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function calculateItemTotal(Counter, OrderID) {
    var SingleCounter = $("#Quantity" + Counter).val();
    var TotalPrice = $("#ProductPrice_" + Counter).val();

    var Sum = parseFloat(TotalPrice.replace(",", ".")) * SingleCounter;
    var TotalSum = (parseFloat(Sum)).toFixed(2);
    var TotalSum = TotalSum.toString().replace(/\./g, ',') + " €";
    var TotalSumInput = Sum;


    $.post("/application/ajax/shop/changeOrderProductQuantity.php", {
        OrderProductID: OrderID,
        Quantity: SingleCounter
    }, function (data, status) {
        if (status == "success") {
            $("#TotalPriceLabel_" + Counter).text(TotalSum);
            $("#SingleItemTotalPrice_" + Counter).val(TotalSumInput);

            calculateFinalPrice();
            calculateShippingPrice();

        }
    });

}

function calculateFinalPrice() {
    var NumberOfProducts = $("#NumberOfItems").val();
    var ShippingPrice = $("#ShippingPrice").val();
    for (var e, t = $("#NumberOfItems").val(), r = ($("#PromoCodeValueHidden").val(), 0), a = 0; t >= a; a++)
        e = parseFloat($("#SingleItemTotalPrice_" + a).val()), isNaN(e) && (e = 0), r += e;
    var Total = (r.toFixed(2)).toString().replace(/\./g, ',') + " €";


    $("#OrderFinalPriceLabelWithOutShipping").html(Total);
    $("#OrderFinalPriceInput").val(r);

}

function calculateShippingPrice() {
    var FreeShippingPrice = $("#DefaultFreeShippingPrice").val();
    var OrderFinalPriceInput = $("#OrderFinalPriceInput").val();
    var DefaultShippingPrice = $("#DefaultShippingPrice").val();
    var ShippingStatus = FreeShippingPrice - OrderFinalPriceInput;
    var ShippingStatus = ShippingStatus.toFixed(2);
    var EmptyShipping = "0,00 €";
    //var DefaultShipping = parseFloat(DefaultShippingPrice.replace(".", ","));
    var DefaultShipping = Math.round(DefaultShippingPrice * 100) / 100;
    DefaultShipping = (DefaultShipping.toFixed(2)).toString().replace(/\./g, ',') + " €";

    var FreeShippingPrice = FreeShippingPrice.toString().replace(/\./g, ',');
    var FreeShippingStatus = ShippingStatus.toString().replace(/\./g, ',');

    if (ShippingStatus < 0) {
        var Shipping = "Ker vaše naročilo presega znesek " + FreeShippingPrice + " €, je dostava brezplačna";
        $("#FreeShippingText").html(Shipping);
        $("#ShippingPrice").val(0);
        $("#OrderShippingPrice").html(EmptyShipping);
        var Total = parseFloat(OrderFinalPriceInput);
        $("#OrderFinalPriceInput").val(Total);
        var Total = (Total.toFixed(2)).toString().replace(/\./g, ',') + " €";
    } else {
        var Shipping = "Do brezplačne dostave vas loči še: " + FreeShippingStatus + " €";
        $("#FreeShippingText").html(Shipping);
        $("#OrderShippingPrice").html(DefaultShipping);
        $("#ShippingPrice").val(DefaultShippingPrice);
        var Total = parseFloat(OrderFinalPriceInput) + parseFloat(DefaultShippingPrice);
        $("#OrderFinalPriceInput").val(Total);
        var Total = (Total.toFixed(2)).toString().replace(/\./g, ',') + " €";
    }
    $("#FreeShippingPrice").val(ShippingStatus);
    $("#OrderFinalPriceLabel").html(Total);


}

function changeDeliveryType(Single) {
    var Total = Single.value;
    var Price = (Total = Total.split("-"))[1];
    var ShippingType = Total[0];
    var OrderID = $("#OrderID").val();

    $.post("/application/ajax/shop/changeDelivery.php", {
        ShippingType: ShippingType,
        OrderID: OrderID
    }, function (data, status) {
        if (status == "success") {
            location.reload();
        }
    });

}

function isEmail(email) {
    var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return regex.test(email);
}

$(".formStep1").submit(function (e) {
    var Firstname = $("#Firstname").val();
    var Lastname = $("#Lastname").val();
    var Address = $("#Address").val();
    var Post = $("#Post").val();
    var PostNumber = $("#PostNumber").val();
    var Email = $("#Email").val();
    var Phone = $("#Phone").val();

    if (Firstname == "") {
        $("#Firstname").addClass("missingDataInput");
    } else {
        $("#Firstname").removeClass("missingDataInput");
    }
    if (Lastname == "") {
        $("#Lastname").addClass("missingDataInput");
    } else {
        $("#Lastname").removeClass("missingDataInput");
    }
    if (Address == "") {
        $("#Address").addClass("missingDataInput");
    } else {
        $("#Address").removeClass("missingDataInput");
    }
    if (Post == "") {
        $("#Post").addClass("missingDataInput");
    } else {
        $("#Post").removeClass("missingDataInput");
    }
    if (PostNumber == "") {
        $("#PostNumber").addClass("missingDataInput");
    } else {
        $("#PostNumber").removeClass("missingDataInput");
    }
    if (Email == "" || (!isEmail(Email))) {
        $("#Email").addClass("missingDataInput");
    } else {
        $("#Email").removeClass("missingDataInput");
    }
    if (Phone == "") {
        $("#Phone").addClass("missingDataInput");
    } else {
        $("#Phone").removeClass("missingDataInput");
    }

    if (Firstname != "" && Lastname != "" && Address != "" && Post != "" && PostNumber != "" && Email != "" && Phone != "" && isEmail(Email)) {
        console.log("success");
    } else {
        e.preventDefault();
        return  false;
    }


})


function showSearch() {
    $(".searchInput").css("width", "300px");
    $(".searchInput").css("border-bottom", "1px solid #ffffff");
    $("#searchShow").hide();
    $("#searchSubmit").show();
}

function submitSearch() {
    var String = $("#searchInput").val();
    var BASE_URL = $("#BASE_URL").val();

    location.href = BASE_URL + "/iskalnik/" + String;
}

function sendDemandModal() {
    $('.modalNew').toggleClass('is-visible');
}

function sendFormContact() {
    var Name = $("#ContactFormName").val();
    var Company = $("#ContactFormCompany").val();
    var Email = $("#ContactFormEmail").val();
    var Phone = $("#ContactFormPhone").val();
    var ContactFormMessage = $("#ContactFormMessage").val();
    var Validation = $("#ValidationInput").val();


    if (Name == "") {
        $("#ContactFormName").addClass("missingDataInput");
    } else {
        $("#ContactFormName").removeClass("missingDataInput");
    }
    if (Email == "" || (!isEmail(Email))) {
        $("#ContactFormEmail").addClass("missingDataInput");
    } else {
        $("#ContactFormEmail").removeClass("missingDataInput");
    }
    if (ContactFormMessage == "") {
        $("#ContactFormMessage").addClass("missingDataInput");
    } else {
        $("#ContactFormMessage").removeClass("missingDataInput");
    }

    if (Name != "" && Email != "" && ContactFormMessage != "" && Validation == "" && isEmail(Email)) {
        $.post("/application/ajax/portal/sendFormContact.php", {
            Name: Name,
            Company: Company,
            Email: Email,
            Phone: Phone,
            Message: ContactFormMessage
        }, function (data, status) {
            if (status == "success") {
                $('#demandModal').modal('toggle');
                $(".buttonDemands").hide();
                $("#textDemand").hide();
                $("#demandsSuccess").slideDown();
            }
        });
    }
}


function setSubsubcategoryMobile(ID) {
    var CategoryID = $("#CategoryID").val();
    var SubCategoryID = ID;
    document.getElementById("mySidepanel").style.width = "0";

    $.post("/application/ajax/portal/setSubsubcategoryMobile.php", {
        SubCategoryID: SubCategoryID,
        CategoryID: CategoryID,
    }, function (data, status) {
        if (status == "success") {
            $("#mySidepanel").width()
            $("#filterProductBox").html(data);
        }
    });
}

//function changeOrderBrand(ID) {
//    $(".orderText").removeClass("orderTextActive");
//    $("#orderText" + ID).addClass("orderTextActive");
//    $("#OrderID").val(ID);
//
//    var OrderID = $("#OrderID").val();
//    var BrandID = $("#BrandID").val();
//
//    $.post("/application/ajax/portal/setOrderBrands.php", {
//        OrderID: OrderID,
//        BrandID: BrandID
//    }, function (data, status) {
//        if (status == "success") {
//            $("#filterProductBox").html(data);
//        }
//    });
//}


function itemsInCart() {
    $.post("/application/ajax/shop/itemsInCart.php", {
    }, function (data, status) {
        if (status == "success") {
            $("#cart-sidebar-container").html(data);
        }
    });
}

function showSearchInput() {
    $(".searchContainer").slideDown();
    $(".showSearchInput").hide();
    $(".hideSearchInput").show();
}

function hideSearchInput() {
    $(".searchContainer").slideUp();
    $(".showSearchInput").show();
    $(".hideSearchInput").hide();
}


function searchProduct() {
    var Query = $("#Query").val();
    if(Query.length >= 2){
        $.post("/application/ajax/portal/searchProduct.php", {
        Query: Query
    }, function (data, status) {
        if (status == "success") {
            $("#searchResultsBox").html(data);
        }
    });
    }
}

function selectDemandProduct(ProductID) {
    $("#DemandProductID").val(ProductID);
}

function sendDemand() {

    var ProductID = $("#DemandProductID").val();
    var Name = $("#Name").val();
    var Email = $("#Email").val();
    var Phone = $("#Phone").val();
    var Company = $("#Company").val();
    var Description = $("#Description").val();

    if (Name == "") {
        $("#Name").addClass("missingData");
    } else {
        $("#Name").removeClass("missingData");
    }

    if (Email == "" || (!isEmail(Email))) {
        $("#Email").addClass("missingData");
    } else {
        $("#Email").removeClass("missingData");
    }

    if (Description == "") {
        $("#Description").addClass("missingData");
    } else {
        $("#Description").removeClass("missingData");
    }

    if (Name != '' && Email != '' && Description != '' && isEmail(Email)) {
        $.post("/application/ajax/portal/sendPromoCodeDemand.php", {
            ProductID: ProductID,
            Name: Name,
            Email: Email,
            Phone: Phone,
            Company: Company,
            Description: Description
        }, function (data, status) {
            if (status == "success") {
                $(".demandsStatus").fadeIn();
                $('#myModal').delay(2000).fadeOut(0);
            }
        });
    }


}


function showCategories() {
    $(".productsSidebar section").slideDown();
    $(".productSidebarShowMore img").css("transform", "rotate(270deg)");
}

function reservationstep2() {
    var Date = $("#inlinePicker").val();
    var ProductID = $("#ProductID").val();
    var LastID = $("#LastID").val();
    var BASE_URL = $("#BASE_URL").val();



    $.post("/application/ajax/reservation/reservationNextDay.php", {
        Date: Date,
        ProductID: ProductID
    }, function (data, status) {
        if (status == "success") {
            location.href = BASE_URL + "/najem-plovila-korak-3/" + LastID;
        }
    });


}


$("#gdpr-cookietype-analytics").click(function () {

});

$(document).ready(function () {
    (function () {
        var showChar = 400;
        var ellipsestext = "...";

        $(".truncate").each(function () {
            var content = $(this).html();
            if (content.length > showChar) {
                var c = content.substr(0, showChar);
                var h = content;
                var html =
                        '<div class="truncate-text" style="display:block">' +
                        c +
                        '<span class="moreellipses">' +
                        ellipsestext +
                        '<br><a href="" class="moreless more">Pokaži več <img src="https://www.shop.enterosgel.si/library/images/icons/next.svg"></a></span></span></div><div class="truncate-text" style="display:none">' +
                        h +
                        '<br><a href="" class="moreless less">Skrij <img src="https://www.shop.enterosgel.si/library/images/icons/next.svg"></a></span></div>';

                $(this).html(html);
            }
        });

        $(".moreless").click(function () {
            var thisEl = $(this);
            var cT = thisEl.closest(".truncate-text");
            var tX = ".truncate-text";

            if (thisEl.hasClass("less")) {
                cT.prev(tX).slideToggle();
                cT.slideToggle();
            } else {
                cT.slideToggle();
                cT.next(tX).slideToggle();
            }
            return false;
        });
        /* end iffe */
    })();

    /* end ready */
});

function savePromoEmail() {
    var Email = $("#Email").val();
    var Terms = $("#Terms").prop('checked');

    if (Email != '' && (isEmail(Email)) && Terms != false) {
        $.post("/application/ajax/portal/savePromoEmail.php", {
            Email: Email
        }, function (data, status) {
            if (status == "success") {
                $(".missingData").slideUp();
                $(".successMessage").slideDown().delay(1000).slideUp();
                setTimeout(function () {
                    $('#myModal').modal('hide');
                }, 2000);
            }
        }
        );
    } else {
        $('.missingData').slideDown('slow');
        $(".successMessage").slideUp();
    }
}

function openmodal() {
    $("#myModal").modal('show');
}

function sendPromoCodeDemand() {
    var Email = $("#Email").val();
    var Terms = $("#Terms").prop('checked');

    if (Email != '' && (isEmail(Email)) && Terms != false) {
        $.post("/application/ajax/portal/sendPromoCodeDemand.php", {
            Email: Email
        }, function (data, status) {
            if (status == "success") {
                $(".missingData").slideUp();
                $(".successMessage").slideDown('fast').delay(2500).slideUp();
                setTimeout(function () {
                    $('#myModal').modal('hide');
                }, 3000);
            }
        }
        );
    } else {
        $('.missingData').slideDown('slow');
        $(".successMessage").slideUp();
    }
}


function sendContactMessage() {
    var Status1;
    var Status2;
    $('input.requiredInput, textarea.requiredInput').each(function () {
        if (!$(this).val()) {
            Status1 = 0;
            $(this).addClass("requiredInputCheck");
        } else {
            $(this).removeClass("requiredInputCheck");
        }
    });

    var Email = $("#Email").val();

    if (!isEmail(Email)) {
        Status2 = 0;
    } else {
        Status2 = 1;
    }


    var BasicData = []
    $('#SendMessageForm input, textarea').each(
            function (index) {
                var input = $(this);
                BasicData.push(input.val());
            }
    );

    if (Status1 != "0" && Status2 != "0") {
        $.post(BASE_URL + "/send_message", {
            BasicData: BasicData
        }, function (data, status) {
            if (status == "success") {
                $("#CompleteOrderSuccess").slideDown().delay(2000).slideUp();
                $("input").val("");
                $("textarea").val("");
            }
        });
    }
}

function changeActive(Class) {
    if(Class == "notActive1") {
        $(".activeBox1").show();
        $(".active1").show();
        $(".activeBox1Border").show();
        $(".active2").hide();
        $(".active3").hide();
        $(".notActive1").hide();
        $(".notActive2").show();
        $(".notActive3").show();
        $(".notActive4").show();
        $(".notActive5").show();
        $(".activeBox2").hide();
        $(".activeBox3").hide();
        $(".activeBox4").hide();
        $(".activeBox5").hide();
        $(".activeBox2Border").hide();
        $(".activeBox3Border").hide();
        $(".activeBox4Border").hide();
        $(".activeBox5Border").hide();
    }else if(Class == "notActive2") {
        $(".activeBox2").show();
        $(".active2").show();
        $(".activeBox2Border").show();
        $(".active1").hide();
        $(".active3").hide();
        $(".active4").hide();
        $(".active5").hide();
        $(".notActive2").hide();
        $(".notActive1").show();
        $(".notActive3").show();
        $(".notActive4").show();
        $(".notActive5").show();
        $(".activeBox1").hide();
        $(".activeBox3").hide();
        $(".activeBox4").hide();
        $(".activeBox5").hide();
        $(".activeBox1Border").hide();
        $(".activeBox3Border").hide();
        $(".activeBox4Border").hide();
        $(".activeBox5Border").hide();
    
    }else if(Class == "notActive3"){
        $(".activeBox3").show();
        $(".active3").show();
        $(".activeBox3Border").show();
        $(".active1").hide();
        $(".active2").hide();
        $(".active4").hide();
        $(".active5").hide();
        $(".notActive3").hide();
        $(".notActive1").show();
        $(".notActive2").show();
        $(".notActive4").show();
        $(".notActive5").show();
        $(".activeBox1").hide();
        $(".activeBox2").hide();
        $(".activeBox4").hide();
        $(".activeBox5").hide();
        $(".activeBox1Border").hide();
        $(".activeBox2Border").hide();
        $(".activeBox4Border").hide();
        $(".activeBox5Border").hide();
    }else if(Class == "notActive4"){
        $(".activeBox4").show();
        $(".active4").show();
        $(".activeBox4Border").show();
        $(".active1").hide();
        $(".active2").hide();
        $(".active3").hide();
        $(".active5").hide();
        $(".notActive4").hide();
        $(".notActive1").show();
        $(".notActive2").show();
        $(".notActive3").show();
        $(".notActive5").show();
        $(".activeBox1").hide();
        $(".activeBox2").hide();
        $(".activeBox3").hide();
        $(".activeBox5").hide();
        $(".activeBox1Border").hide();
        $(".activeBox2Border").hide();
        $(".activeBox3Border").hide();
        $(".activeBox5Border").hide();
    }
    else if(Class == "notActive5"){
        $(".activeBox5").show();
        $(".active5").show();
        $(".activeBox5Border").show();
        $(".active1").hide();
        $(".active2").hide();
        $(".active3").hide();
        $(".active4").hide();
        $(".notActive5").hide();
        $(".notActive1").show();
        $(".notActive2").show();
        $(".notActive3").show();
        $(".notActive4").show();
        $(".activeBox1").hide();
        $(".activeBox2").hide();
        $(".activeBox3").hide();
        $(".activeBox4").hide();
        $(".activeBox1Border").hide();
        $(".activeBox2Border").hide();
        $(".activeBox3Border").hide();
        $(".activeBox4Border").hide();
    }
} 

function sendDemand() {  
    var FirstName = $("#FirstName").val();
    var LastName = $("#LastName").val();
    var Email = $("#Email").val();
    var Phone = $("#Phone").val();
    var Message = $("#Message").val();
    var Terms = $("#Terms").prop('checked');
    
    var Status1;
    var Status2;
    var Status3;


    $('.requiredInput').each(function () {
        if (!$(this).val()) {
            Status1 = 0;
            $(this).addClass("requiredInputCheck");
        }else{
             $(this).removeClass("requiredInputCheck");
        }
    });
    
    if ($("#Terms").is(":checked")) {
        Status2 = 1;
        $("#TermsLabel").removeClass("requiredCheckbox");
    } else {
        $("#TermsLabel").addClass("requiredCheckbox");
        Status2 = 0;
    }
    
    if (!isEmail(Email)) {
        Status3 = 0;
    } else {
        Status3 = 1;
    }
    
     if (Status1 != "0" && Status2 != "0" && Status3 != "0") {
        $.post(BASE_URL + "/index/send_demand", {
            FirstName: FirstName,
            LastName: LastName,
            Email: Email,
            Phone: Phone,
            Message: Message
        }, function (data, status) {
            if (status == "success") {                       
                $(".successMessage").slideDown().delay(3000).slideUp();
                $("#FirstName").val("");
                $("#LastName").val("");
                $("#Email").val("");
                $("#Phone").val("");
                $("#Message").val("");
                $("#Terms").prop('checked', false);
            }
        });
    }
}

function selectCategory() {
    var Selected = $('input[name="category"]:checked').val();
    
    $.post("/application/ajax/portal/selectCategory.php", {
        Selected: Selected
    }, function (data, status) {
        if (status == "success") {
            $("#SubcategoryBody").html(data);    
            $("#SubcategoryBody").show('slow');  
            $("#SubcategoryBody").addClass('shown');
            $("#Circle2").addClass('orange');
        }
    });
}

function sendConfiguratorDemand(ConfiguratorID) {  
    
    var Radio1 = $('input[name="category"]:checked').val();
    var Radio2 = $('input[name="subcategory"]:checked').val();
    var Input1 = $('#Input1').val();
    var Type1 = $('input[name="Type1"]:checked').val();
    var Input2 = $('#Input2').val();
    var Type2 = $('input[name="Type2"]:checked').val();
    var Select1 = $("#Select1 option:selected").val();
    
    var FirstName = $("#FirstName").val();
    var LastName = $("#LastName").val();
    var Email = $("#Email").val();
    var Phone = $("#Phone").val();
    var Message = $("#Message").val();
    var Address = $("#Address").val();
    var Terms = $("#Terms").prop('checked');
        
    var Status1;
    var Status2;
    var Status3;
    var Status4;
    var Status5;
    var Status6;

    $('.requiredInput').each(function () {
        if (!$(this).val()) {
            Status1 = 0;
            $(this).addClass("requiredInputCheck");
        }else{
             $(this).removeClass("requiredInputCheck");
        }
    });
    
    if ($("#Terms").is(":checked")) {
        Status2 = 1;
        $("#TermsLabel").removeClass("requiredCheckbox");
    } else {
        $("#TermsLabel").addClass("requiredCheckbox");
        Status2 = 0;
    }
    
    if ($('input[name="subcategory"]:checked').length > 0) {
        Status4 = 1;
    }else{
        Status4 = 0;
    }
    
    if (!isEmail(Email)) {
        Status3 = 0;
    } else {
        Status3 = 1;
    }
    
    
    if(Input1 == "0"){
        Status5 = 0;
    }else{
        Status5 = 1;
    }
    
    
    if(ConfiguratorID == "1"){
        if(Input2 == "0"){
            Status6 = 0;
        }else{
            Status6 = 1;
        }
    }else{
        Status6 = 1;
    }

    
     if (Status1 != "0" && Status2 != "0" && Status3 != "0" && Status4 != "0" && Status5 != "0" && Status6 != "0" && Select1 != "0") {
        $.post(BASE_URL + "/index/send_configurator_demand", {
            FirstName: FirstName,
            LastName: LastName,
            Email: Email,
            Phone: Phone,
            Message: Message,
            Address: Address,
            Radio1: Radio1,
            Radio2: Radio2,
            Input1: Input1,
            Input2: Input2,
            Type1: Type1,
            Type2: Type2,
            Select1: Select1,
            ConfiguratorID : ConfiguratorID
            
        }, function (data, status) {
            if (status == "success") {                       
                $(".successMessage").slideDown().delay(3000).slideUp();
                $(".failMessage").slideUp();
                $("#FirstName").val("");
                $("#LastName").val("");
                $("#Email").val("");
                $("#Phone").val("");
                $("#Address").val("");
                $("#Input1").val("");
                $("#Input2").val("");
                $("#Terms").prop('checked', false);
            }
        });
    }else{
        $(".failMessage").show();
    }
}

function openNextSection(SectionID) {
    if(SectionID == "SubcategoryBody"){
        $("#Input1section").show("slow");
        $("#Input1section").addClass("shown");
        $("#Circle3").addClass("orange");
    }else if(SectionID == "Input1section"){
        $("#Input2section").show("slow");
        $("#Input2section").addClass("shown");
        $("#Circle4").addClass("orange");
    }else if(SectionID == "Input2section"){
        $("#Input3section").show("slow");
        $("#Input3section").addClass("shown");
        $("#Circle5").addClass("orange");
    }else if(SectionID == "Input3section"){
        $("#Input4section").show("slow");
        $("#Input4section").addClass("shown");
        $("#Circle6").addClass("orange");
    }
}

function send_contract_withdrawal() {
    var Status1;
    var Status2;
    var Email = $("#Email").val();
    var Firstname = $("#Firstname").val();
    var Lastname = $("#Lastname").val();
    var Phone = $("#Phone").val();
    var OrderNumber = $("#OrderNumber").val();
    var Body = $("#Body").val();

    $('input.requiredInput').each(function () {
        if (!$(this).val()) {
            Status1 = 0;
            $(this).addClass("requiredInputCheck");
        } else {
            $(this).removeClass("requiredInputCheck");
        }
    });

    if (!isEmail(Email)) {
        Status2 = 0;
    } else {
        Status2 = 1;
    }

    if (Status1 != "0" || Status2 != "0") {
        $.post(BASE_URL + "/send_contract_withdrawal", {
            Email: Email,
            Firstname: Firstname,
            Lastname: Lastname,
            Phone: Phone,
            OrderNumber: OrderNumber,
            Body: Body
        }, function (data, status) {
            if (status == "success") {
                $(".successMessage").slideDown().delay(3000).slideUp();
                $("#Email").val("");
                $("#Firstname").val("");
                $("#Lastname").val("");
                $("#Phone").val("");
                $("#OrderNumber").val("");
                $("#Body").val("");
            }
        });
    }
}
function openLoginModal() {
    $('#loginModal').toggleClass('is-visible');   
}
function closeLoginModal() {
    $('#loginModal').toggleClass('is-visible');
}

function openProductDemand(ProductUUID) {
    $('#demandModal').toggleClass('is-visible');
    $('#ProductUUID').val(ProductUUID);
}

function sendProductDemand() {  
    var FirstName = $("#FirstName").val();
    var LastName = $("#LastName").val();
    var Email = $("#Email").val();
    var Phone = $("#Phone").val();
    var Message = $("#Message").val();
    var ProductUUID = $("#ProductUUID").val();
    
    var Status1;
    var Status2;


    $('.requiredInput').each(function () {
        if (!$(this).val()) {
            Status1 = 0;
            $(this).addClass("requiredInputCheck");
        }else{
             $(this).removeClass("requiredInputCheck");
        }
    });
    
    
    if (!isEmail(Email)) {
        Status2 = 0;
    } else {
        Status2 = 1;
    }
    
     if (Status1 != "0" && Status2 != "0" ) {
        $.post(BASE_URL + "/index/send_product_demand", {
            FirstName: FirstName,
            LastName: LastName,
            Email: Email,
            Phone: Phone,
            Message: Message,
            ProductUUID: ProductUUID
        }, function (data, status) {
            if (status == "success") {                       
                $(".successMessage").slideDown().delay(2000).slideUp();
                $("#FirstName").val("");
                $("#LastName").val("");
                $("#Email").val("");
                $("#Phone").val("");
                $("#Message").val("");
                setTimeout(function(){ 
                    $('#demandModal').toggleClass('is-visible');; }, 2500);
                
            }
        });
    }
}

function redirectToProductPage(ProductUrl, ProductUUID) { 
	
	$.post(BASE_URL + "/shop_check_quantity", {
        ProductUUID: ProductUUID
    }, function (data, status) {
        if (status == "success") {
			
			//console.log(typeof(data));
			//return;
			
			if(data === "0"){
				
				openProductDemand(ProductUUID);
				
			} else {
				var BASE_URL = $("#BASE_URL").val();
    
				location.href = BASE_URL + "/" + ProductUrl;
			}
		}	
    });
  
}

//var tmp1 = [];
//var tmp2 = [];
//
//$("input[name='Category']").change(function () {
//
//    var checked = $(this).val();
//
//    if ($(this).is(':checked')) {
//        tmp1.push(checked);
//    } else {
//        tmp1.splice($.inArray(checked, tmp1), 1);
//    }
//    $("#SubSubCategories").val(tmp1);
//    
//    var SubCategoryID = $("#SubCategory").val();
//    var CategoryID = $("#Category").val();
//    var OrderID = $("#OrderID").val();
//    var Manufacturer = $("#Manufacturer").val();
//    var CategoriesArray = tmp1;
//  
//    $.post("/application/ajax/shop/setCategory.php", {
//        CategoriesArray: CategoriesArray,
//        CategoryID: CategoryID,
//        SubCategoryID: SubCategoryID,
//        OrderID:OrderID,
//        Manufacturer: Manufacturer
//    }, function (data, status) {
//        if (status == "success") {
//            $("#filterProductBox").html(data);
//        }
//    });
//
//});
//

// Example: /spletna-trgovina?kategorija=siroka-potrosnja&podkategorija=avtomobilizem&proizvajalec=55
// User removeParam(URL, 'tip');
function removeParam(url, param){
	
	// remove all before ?
	var paramsURL = url.split('?')[1];
	var paramsArray = paramsURL.split('&');

	var newArray = [];
	paramsArray.forEach(function(p){
		if(!p.includes(param)){
			newArray.push(p);
//            alert(newArray);
		}
	});
	
	return url.split('?')[0]+ '?' + newArray.join('&');
}

//Filtiriranje po tip - podpodkategorija
	var SubSubCategories = [];
	var SubSubCategoryUrl = "";

	$("input[name='Category']").change(function () {

		var checked = $(this).val();

		var SSCat = $("#SubSubCategories").val();

		if(SSCat !== undefined && SSCat !== ""){
			SubSubCategories = $("#SubSubCategories").val().split(',');
		}

		if ($(this).is(':checked')) {
			SubSubCategories.push(checked);
		} else {
			SubSubCategories.splice($.inArray(checked, SubSubCategories), 1);
		}	
		
		$("#SubSubCategories").val(SubSubCategories);
        
        //checked == 3 je podpodkategorija "koruza"
        
//        if(checked == 3){
//            $("#Fao").toggle();
//        }

		if(SubSubCategories == null || SubSubCategories == ""){
		   SubSubCategoryUrl = "";
		}else{

			SubSubCategoryUrl = "&tip=" + SubSubCategories;
		}        

		newUrl('tip');
        getManufacturers();

	});
    
    //Filtriranje po Filter1
    var Filter1 = [];
	var Filter1Url = "";

	$("input[name='Filter1']").change(function () {

		var checked = $(this).val();

		var Filter1Cat = $("#Filter1").val();
        
		if(Filter1Cat !== undefined && Filter1Cat !== ""){
			Filter1 = $("#Filter1").val().split(',');
		}

		if ($(this).is(':checked')) {
			Filter1.push(checked);
		} else {
			Filter1.splice($.inArray(checked, Filter1), 1);
		}
			
		$("#Filter1").val(Filter1);
        

		if(Filter1 == null || Filter1 == ""){
		   Filter1Url = "";
		}else{
			Filter1Url = "&vrsta=" + Filter1;
		}

		newUrl('vrsta');
        getManufacturers();

	});
    
    //Filtriranje po Filter2
    var Filter2 = [];
	var Filter2Url = "";

	$("input[name='Filter2']").change(function () {

		var checked = $(this).val();

		var Filter2Cat = $("#Filter2").val();
        
		if(Filter2Cat !== undefined && Filter2Cat !== ""){
			Filter2 = $("#Filter2").val().split(',');
		}

		if ($(this).is(':checked')) {
			Filter2.push(checked);
		} else {
			Filter2.splice($.inArray(checked, Filter1), 1);
		}
				
		$("#Filter2").val(Filter2);
        

		if(Filter2 == null || Filter2 == ""){
		   Filter2Url = "";
		}else{
			Filter2Url = "&fao=" + Filter2;
		}      

		newUrl('fao');
        getManufacturers();

	});

//order by filter
function changeOrder(ID) {
    $(".orderText").removeClass("orderTextActive");
    $("#orderText" + ID).addClass("orderTextActive");
    $("#OrderID").val(ID);

	var SSCat = $("#SubSubCategories").val();

	if(SSCat !== undefined && SSCat !== ""){
		SubSubCategories = $("#SubSubCategories").val().split(',');
	}
        
    var Filter1Cat = $("#Filter1").val();
        
	if(Filter1Cat !== undefined && Filter1Cat !== ""){
        Filter1 = $("#Filter1").val().split(',');
    }  
    
    var Filter2Cat = $("#Filter2").val();
        
    if(Filter2Cat !== undefined && Filter2Cat !== ""){
        Filter2 = $("#Filter2").val().split(',');
    }
    
    var SubSubCategoriesArray = SubSubCategories;   
    var Filter1Array = Filter1;
    var Filter2Array = Filter2;
    var SubCategoryID = $("#SubCategory").val();
    var CategoryID = $("#Category").val();
    var OrderID = $("#OrderID").val();
    var Manufacturer = $("#Manufacturer").val();

    $.post("/application/ajax/shop/setOrderCategory.php", {
        CategoryID: CategoryID,
        SubCategoryID: SubCategoryID,
        OrderID:OrderID,
        Manufacturer: Manufacturer,
        SubSubCategoriesArray: SubSubCategoriesArray,
        Filter1Array: Filter1Array,
        Filter2Array: Filter2Array
    }, function (data, status) {
        if (status == "success") {
            $("#filterProductBox").html(data);
        }
    });
}

//funkcija pošlje podatke v setCategory.php, ki vrne seznam produktov
function newUrl(Type) {
    var BASE_URL = $("#BASE_URL").val();
    var CURRENT_URL = $("#CurrentUrl").val();   
    
    var SSCat = $("#SubSubCategories").val();

	if(SSCat !== undefined && SSCat !== ""){
		SubSubCategories = $("#SubSubCategories").val().split(',');
	}
        
    var Filter1Cat = $("#Filter1").val();
        
	if(Filter1Cat !== undefined && Filter1Cat !== ""){
        Filter1 = $("#Filter1").val().split(',');
    }
    
    var Filter2Cat = $("#Filter2").val();
        
	if(Filter2Cat !== undefined && Filter2Cat !== ""){
        Filter2 = $("#Filter2").val().split(',');
    }
                 
    if(Type == 'tip'){
        CURRENT_URL = removeParam(CURRENT_URL, 'tip');     
    }
    if(Type == 'vrsta'){
        CURRENT_URL = removeParam(CURRENT_URL, 'vrsta');
    }  
     if(Type == 'fao'){
        CURRENT_URL = removeParam(CURRENT_URL, 'fao');
    } 
    
    $("#CurrentUrl").val(CURRENT_URL);
    
    var URL = BASE_URL + CURRENT_URL + SubSubCategoryUrl + Filter1Url + Filter2Url;
       
    history.pushState({}, null, URL);
    
    var SubSubCategoriesArray = SubSubCategories;
    var Filter1Array = Filter1;
    var Filter2Array = Filter2;
    var SubCategoryID = $("#SubCategory").val();
    var CategoryID = $("#Category").val();
    var OrderID = $("#OrderID").val();
    var Manufacturer = $("#Manufacturer").val();
    
    $.post("/application/ajax/shop/setCategory.php", {
        CategoryID: CategoryID,
        SubCategoryID: SubCategoryID,
        OrderID:OrderID,
        Manufacturer: Manufacturer,
        SubSubCategoriesArray: SubSubCategoriesArray,
        Filter1Array: Filter1Array,
        Filter2Array: Filter2Array
    }, function (data, status) {
        if (status == "success") {
            $("#filterProductBox").html(data);        
        }
    });
    
}

function getManufacturers() {
    var BASE_URL = $("#BASE_URL").val();
    var CURRENT_URL = $("#CurrentUrl").val();   
    
    var SSCat = $("#SubSubCategories").val();

	if(SSCat !== undefined && SSCat !== ""){
		SubSubCategories = $("#SubSubCategories").val().split(',');
	}
        
    var Filter1Cat = $("#Filter1").val();
        
	if(Filter1Cat !== undefined && Filter1Cat !== ""){
        Filter1 = $("#Filter1").val().split(',');
    }
    
    var Filter2Cat = $("#Filter2").val();
        
	if(Filter2Cat !== undefined && Filter2Cat !== ""){
        Filter2 = $("#Filter2").val().split(',');
    }                  
       
    var SubSubCategoriesArray = SubSubCategories;
    var Filter1Array = Filter1;
    var Filter2Array = Filter2;
    var SubCategoryID = $("#SubCategory").val();
    var CategoryID = $("#Category").val();
    var OrderID = $("#OrderID").val();
    var Manufacturer = $("#Manufacturer").val();
    
    $.post("/application/ajax/shop/getManufacturers.php", {
        CategoryID: CategoryID,
        SubCategoryID: SubCategoryID,
        OrderID:OrderID,
        Manufacturer: Manufacturer,
        SubSubCategoriesArray: SubSubCategoriesArray,
        Filter1Array: Filter1Array,
        Filter2Array: Filter2Array,
        BASE_URL: BASE_URL,
        CURRENT_URL: CURRENT_URL
    }, function (data, status) {
        if (status == "success") {
            $("#Manufacturers").html(data);        
        }
    });
    
}

function selectManufacturer(GeneratedUrl){
    
    var SubSubCategories = $('#SubSubCategories').val();
    var Filter1 = $('#Filter1').val();
    var Filter2 = $('#Filter2').val();
//    alert(Filter1);
//    alert(Filter2);
//    alert(SubSubCategories);
    
//    location.href = GeneratedUrl;
    
    if(SubSubCategories == "" && Filter1 == "" && Filter2 == ""){
        location.href = GeneratedUrl;
    }else if(SubSubCategories != "" && Filter1 == "" && Filter2 == ""){
        location.href = GeneratedUrl + "&tip=" + SubSubCategories;
    }else if(SubSubCategories != "" && Filter1 != "" && Filter2 == ""){
        location.href = GeneratedUrl + "&tip=" + SubSubCategories + "&vrsta=" + Filter1;
    }else if(SubSubCategories != "" && Filter1 != "" && Filter2 != ""){
        location.href = GeneratedUrl + "&tip=" + SubSubCategories + "&vrsta=" + Filter1 + "&fao=" + Filter2;
    }else if(SubSubCategories == "" && Filter1 != "" && Filter2 != ""){
        location.href = GeneratedUrl + "&vrsta=" + Filter1 + "&fao=" + Filter2;
    }else if (SubSubCategories == "" && Filter1 == "" && Filter2 != ""){
        location.href = GeneratedUrl + "&fao=" + Filter2;
    }else if (SubSubCategories == "" && Filter1 != "" && Filter2 == ""){
        location.href = GeneratedUrl + "&vrsta=" + Filter1;
    }else if (SubSubCategories != "" && Filter1 == "" && Filter2 != ""){
        location.href = GeneratedUrl + "&tip=" + SubSubCategories + "&fao" + Filter2;
    }
    
//    if (SubSubCategories == "" && Filter1 == ""){
//        location.href = GeneratedUrl;
//    }else if(SubSubCategories != "" && Filter1 == ""){
//        location.href = GeneratedUrl + "&tip=" + SubSubCategories;
//    }else if(SubSubCategories == "" && Filter1 != ""){
//        location.href = GeneratedUrl + "&vrsta=" + Filter1;
//    }else if(SubSubCategories != "" && Filter1 != ""){
//        location.href = GeneratedUrl + "&tip=" + SubSubCategories + "&vrsta=" + Filter1;
//    }

}

//called when key is pressed in textbox
$(".onlyNumber").keypress(function (e) {
    //if the letter is not digit then display error and don't type anything
    if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        return false;
    }
});

function clearInput(Input) {    
    $("#" + Input).val("");
}

function checkInputValue() {
    var Type = $('input[name="Type1"]:checked').val();
    var Input = $("#Input1").val();
    var SubcategoryID = $('input[name="subcategory"]:checked').val();
       
    $.post("/application/ajax/portal/CheckInputValue.php", {
        Type: Type,
        Input: Input,
        SubcategoryID: SubcategoryID
    }, function (data, status) {
        if (status == "success") {
			$("#Input1").val(data);
            console.log("#" + data + "#");
		}	
    });
}



