﻿
var evtSubscribers = {};
function subscribe(evt, func) {
    var arr = evtSubscribers[evt];
    if (!arr) {
        arr = [];
    }
    arr.push(func);
    evtSubscribers[evt] = arr;
}

function unsubscribe(evt, func) {
    var arr = evtSubscribers[evt];
    if (arr) {
        var index = -1;
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == func)
                index = i;
        }
        if (index > -1)
            arr.splice(index, 1);
    }
}

function triggerSubscription(evt) {
    var arr = evtSubscribers[evt];
    if (arr) {
        for (var i = 0; i < arr.length; i++)
            if (typeof arr[i] == "function")
                arr[i]();
}
}

function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
    var newnumber = Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
    return newnumber;
}

function getParameterByName(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 decodeURIComponent(results[1]);
}


function setFontSize(size, obj) {
    if (jQuery != undefined) {
        $('body').css('font-size', size);
        $('#header .font-size a').removeClass('current');
        $(obj).addClass('current');
        $.cookie('fontSize', size + "," + $(obj).attr('id'));
    }
}

function parseNLCurrency(c) {
    return parseFloat(c.replace(/\u20AC/, '').replace(/\./g, '').replace(',', '.'));
}

function toNLCurrency(f, forZero) {
    if (forZero && f == 0) return forZero;
    var s = "" + roundNumber(f, 2);
    s = s.replace('.', ',');
    var index = s.lastIndexOf(',');
    if (index < 0) { index = s.length; s += ",00"; }
    index -= 3;
    while (index > 0) {
        s = s.substr(0, index) + "." + s.substr(index);
        index -= 3;
    }
    return "€ " + s;
}


function setupSlider(jObj,defaultValue) {
    if (!jObj) return;
    if (!defaultValue||defaultValue<1000||defaultValue>16500) {
        defaultValue = 8750;
    }
    jObj.slider({
        value: defaultValue,
        min: 1000,
        max: 16500,
        step: 1937.5,
        slide: function (event, ui) {
            // doesnot work in ie
            //$(this).closest('tr').prev('tr').find("input.premie-amount").val('€ ' + ui.value).focus().blur();

            var $input = $(this).closest('tr').prev('tr').find("input.premie-amount").val(toNLCurrency(Math.round(ui.value)));
            $input.trigger("focus");
        }
    });
    jObj.closest('tr').prev('tr').find("input.premie-amount").change(function () {
        var $this = $(this);
       
        var $slider = $this.closest('tr').next('tr').find("span.slider-in");
        var sliderMin = $slider.slider("option", "min");
        var sliderMax = $slider.slider("option", "max");
        var sliderValue = parseFloat($this.val().replace('€', '').replace(' ', ''));

        if (sliderValue > 0) {
            if (sliderValue >= sliderMax) {
                $slider.slider("option", "value", sliderMax);
            } else if (sliderValue <= sliderMin) {
                $slider.slider("option", "value", sliderMin);
            } else {
                $slider.slider("option", "value", sliderValue);
            }
        }
    });
}

function updateSummary(unCompletedSpan, completedSpan, priceSpan, completedWishSpan) {
    $.getJSON("/c-rootsite/Builtin_service",
                    { action: "queryCountNPrice" },
                    function (data) {
                        if (data.Total) {
                            unCompletedSpan.text(data.Total - data.Completed);
                            completedSpan.text(data.Completed);
                            priceSpan.text(data.Price);

                            completedWishSpan.text(data.Completed > 1 ? "wensen" : "wens");

                            if (data.Ratio && data.Ratio != -1) {
                                if ($('#ratio').length > 0) {
                                    $('#ratio').text(data.Ratio + '%');
                                    $('div.bar-in').width(140 * parseFloat($('#ratio').text()) / 100);
                                }
                            }
                        }
                    });
}

function calculatePremie(inputData, callback) {
    $.getJSON('/c-rootsite/builtin_service',
        {
            action: 'calculatePremie',
            data: jlUtil.getJSON(inputData)
        },
        function (data) {
            callback && callback(data);
        });
}

(function ($) {
    $.fn.gallery = function (options) {
        var defaults = {};

        var options = $.extend(defaults, options);
        if ($.gallerified) return this;
        $.gallerified = true;

        return this.each(function () {
            var $w7 = $(this);
            var $w3 = $w7.next('.widget3');
            var $cust = $w7.nextAll('.cust-div:first');
            var $chk_cust = $cust.find(':checkbox');
            var $input_cust = $cust.find('textarea');
            var $gallery_mask = $w7.find('.gallery-mask');
            var count = $(this).find('.widget7-middle ul li').length;
            var index = 0;
            var step = 146;
            $w7.find('span.previous').click(function () {
                if (index > 0) {
                    $w7.find('.widget7-middle').animate({ marginLeft: "+=" + step + "px" }, 300);
                    index--;
                }

            });

            $w7.find('span.next').click(function () {
                if (index < count - 3) {
                    $w7.find('.widget7-middle').animate({ marginLeft: "-=" + step + "px" }, 300);
                    index++;
                }
            });


            $w7.find('a.show-detail').click(function () {
                $w3.find('.name').text($(this).closest('li').find('.name').text());
                $w3.find('img').attr('src', $(this).closest('li').find('img').attr('src'));
                $w3.find('p.intro').text($(this).closest('li').find('input[name="intro"]:hidden').val());
                if ($w3.find('h2').length > 0)
                    $w3.find('h2').text($(this).closest('li').find('input[name="group"]:hidden').val());
                $w3.find('input.uuid:hidden').val($(this).closest('li').find('input[name="uuid"]:hidden').val());
                $w7.hide(); $w3.show(); $cust.hide();
            });

            $w3.find('a.add-product').click(function () {
                $(this).hide().nextAll('a.remove-product').show().prevAll('a.back-to-overview').hide().prevAll('span.added-message').show().closest('.widget3-middle').addClass('added').find('span.transparence-pic').show();
                if (options.added) { options.added($w3.find('input.uuid').val(), $w7); }
            });

            $w3.find('a.remove-product').click(function () {
                $(this).hide().prevAll('a.add-product').show().prevAll('a.back-to-overview').show().prevAll('span.added-message').hide().closest('.widget3-middle').removeClass('added').find('span.transparence-pic').hide();
                //$w3.hide(); $w7.show();
                if (options.removed) { options.removed($w3.find('input.uuid').val(), $w7); $cust.show(); }
            });

            $w3.find('a.back-to-overview').click(function () {
                $w3.hide(); $w7.show(); $cust.show();
            });

            $chk_cust.removeAttr('checked').bind('click change', function () {
                var chk = $(this).is(':checked');
                $gallery_mask.toggle(chk);
                if (chk) {
                    $cust.find('textarea').removeAttr('readonly');
                } else {
                    $cust.find('textarea').attr('readonly', 'readonly');
                }
            });

            var products = options.products;
            var custProducts = options.custProducts;
            var uuid;
            $w7.find('input[name="uuid"]:hidden').each(function () {
                uuid = $(this).val();
                if (products.indexOf(uuid) > -1) {
                    $("#product-" + uuid).find('a.show-detail')
                                         .click()
                                         .closest('.wish-product')
                                         .next('.widget3')
                                         .find('span.added-message')
                                         .show()
                                         .nextAll('a.back-to-overview')
                                         .hide()
                                         .nextAll('a.add-product')
                                         .hide()
                                         .nextAll('a.remove-product')
                                         .show()
                                         .closest('.widget3-middle')
                                         .addClass('added')
                                         .find('span.transparence-pic').show();
                    $cust.hide();
                    $w3.find('input.uuid:hidden').val(uuid);
                    if (options.init) { options.init(uuid, $w7); }
                    return false;
                }
            });

            for (var key in custProducts) {
                if ('cust_' + key == $chk_cust.attr('id')) {
                    $cust.find('textarea').val(custProducts[key]);
                    $chk_cust.attr('checked', 'checked').change().add($input_cust).attr('disabled', 'disabled');
                    $cust.find('.added,.tips').show();
                    $cust.find('.add').hide();
                    if (options.init) { options.init(uuid, $w7); }
                }
            }

            $cust.find('.add').click(function () {
                $chk_cust.add($input_cust).attr('disabled', 'disabled');
                $cust.find('.added,.tips').show();
                $(this).hide();
                var id = $chk_cust.attr('id').substring(5);
                var text = $cust.find('textarea').val();
                if (options.added) { options.added(id, $w7, text, true); }
            });

            $cust.find('.tips a').click(function () {
                $chk_cust.add($input_cust).removeAttr('disabled');
                $cust.find('.add').show();
                $cust.find('.added,.tips').hide();
                var id = $chk_cust.attr('id').substring(5);
                if (options.removed) { options.removed(id, $w7, true); }
            });
        });
    };

    $.fn.gallerySelect = function (uuid) {
        if ($.gallerified) {
            if (GetProducts().indexOf(uuid) < 0 && !this.is(":hidden")) {
                var hid = this.find('input:hidden[value="' + uuid + '"]');
                if (hid.length > 0)
                    hid.closest('li').find('a.show-detail').click();
            }
        }
        return this;
    }
})(jQuery);

(function ($) {
    $.fn.wishQuestion = function (options) {
        var defaults = {};
        var options = $.extend(defaults, options);
        if ($.questionSetup) return this;
        $.questionSetup = true;

        this.each(function () {
            var w = $(this);
            var uuid = $(this).find(':checkbox, :radio').attr('name');
            if (options.wishes[uuid]) {
                $(this).removeClass('status-unchecked')
                    .addClass('status-checked')
                    .find(':checkbox, :radio')
                    .val(options.wishes[uuid].Answer.split(','));
                $(this).find('input,textarea').attr('disabled', 'disabled');
                $(this).find('input[name^="lastInput_"]').val(options.wishes[uuid].OptionLastInput);
                $(this).find('textarea[name^="extra_"]').val(options.wishes[uuid].AdditionalComment);
                $(this).find('a.add').hide();
                $(this).find('a.added,span.tips').show();
                if (options.initSelect) {
                    options.initSelect(uuid, w);
                }
            }

            $(this).find('a.add').click(function () {
                var answer = "";
                w.find(":checkbox:checked, :radio:checked").each(function () { answer += $(this).val() + ","; });
                if (answer.length > 0)
                    answer = answer.substr(0, answer.length - 1);
                else
                    return;

                var lastInput = w.find("input[name^='lastInput_']");
                var lastCheck = lastInput.closest('p').find(':checkbox:checked, :radio:checked');
                lastCheck.click(function () {
                    if (!$(this).is(":checked")) {
                        lastInput.unbind('focus').closest('p').removeClass('error').find('label.error').remove();
                        lastInput.val('');
                    }
                });

                if (($.trim(lastInput.val()) == "" || $.trim(lastInput.val()) == "U dient nog antwoord te geven.") && lastCheck.length > 0) {
                    if (!lastInput.closest('p').hasClass('error')) {
                        lastInput.val("U dient nog antwoord te geven.").closest('p').addClass('error').append('<label class="error">!</label>');
                        lastInput.focus(function () { $(this).val(''); });
                    }
                    return;
                } else {
                    lastInput.unbind('focus').closest('p').removeClass('error').find('label.error').remove();
                    if (lastInput.closest('p').find(':checkbox:checked, :radio:checked').length == 0)
                        lastInput.val('');
                }

                var wish = {
                    Answer: answer,
                    OptionLastInput: w.find('input[name^="lastInput_"]').val(),
                    AdditionalComment: w.find('textarea[name^="extra_"]').val()
                }
                if (options.added) {
                    options.added(uuid, wish, w);
                    //PushWish(uuid, wish);
                }
                w.find('input,textarea').attr('disabled', 'disabled');
                $(this).hide();
                w.removeClass('status-unchecked').addClass('status-checked').find('a.added,span.tips').show();
            });

            $(this).find('span.tips a').click(function () {
                w.removeClass('status-checked').addClass('status-unchecked').find('input,textarea').removeAttr('disabled');
                //w.find(':text,textarea').val('');
                //w.find(':checkbox, :radio').val([]);
                w.find('a.added,span.tips').hide();
                w.find('a.add').show();
                if (options.removed) {
                    options.removed(uuid, w);
                    //   RemoveWish(uuid);
                }
            });
        });
    }
})(jQuery);

(function ($) {
    $.fn.staticQuestion = function (options) {
        var defaults = {};
        var options = $.extend(defaults, options);

        this.each(function () {
            var $w = $(this);
            var uuid = $w.find(':checkbox, :radio').attr('name');
            var inputA = $w.find('textarea[name^="InputA_"]');
            var inputA1 = $w.find('input[name^="InputA1_"]');
            var inputA2 = $w.find('input[name^="InputA2_"]');
            var inputA3 = $w.find('input[name^="InputA3_"]');
            var inputB = $w.find('input[name^="InputB_"]');
            var inputD = $w.find('textarea[name^="InputD_"]');
            var inputE = $w.find('textarea[name^="InputE_"]');

            if (options.staticQuestions[uuid]) {
                $w.removeClass('status-unchecked')
                    .addClass('status-checked')
                    .find(':checkbox, :radio')
                    .val(options.staticQuestions[uuid].Answer.split(','));
                $w.find('input,textarea').attr('disabled', 'disabled');
                inputA.val(options.staticQuestions[uuid].inputA);
                inputD.val(options.staticQuestions[uuid].inputD);
                inputE.val(options.staticQuestions[uuid].inputE);
                inputA1.val(options.staticQuestions[uuid].inputA1);
                inputA2.val(options.staticQuestions[uuid].inputA2);
                inputA3.val(options.staticQuestions[uuid].inputA3);
                inputB.val(options.staticQuestions[uuid].inputB);
                $w.find('a.add').hide();
                $w.find('a.added,span.tips').show();
                $w.find('span.tips').css('display', 'inline');
                if (options.initSelect) {
                    options.initSelect(uuid, $w);
                }
            }

            $w.find('a.add').click(function () {
                var answer = "";
                $w.find(":checkbox:checked, :radio:checked").each(function () { answer += $(this).val() + ","; });
                if (answer.length > 0)
                    answer = answer.substr(0, answer.length - 1);
                else
                    return;

                if (inputA.length > 0 && (inputA.val() == "" || inputA.val() == "U dient nog antwoord te geven.") && inputA.closest('p').find(':checkbox:checked').length > 0) {
                    if (!inputA.closest('p').hasClass('error')) {
                        inputA.val("U dient nog antwoord te geven.").closest('p').addClass('error');
                        inputA.focus(function () { $(this).val(''); });
                        inputA.closest('p').find(':checkbox').one('click', function () {
                            if (!$(this).is(':checked')) {
                                inputA.val('').unbind('focus').closest('p').removeClass('error');
                            }
                        });
                    }
                    return;
                }

                if (inputD.length > 0 && (inputD.val() == "" || inputD.val() == "U dient nog antwoord te geven.") && inputD.closest('p').find(':checkbox:checked').length > 0) {
                    if (!inputD.closest('p').hasClass('error')) {
                        inputD.val("U dient nog antwoord te geven.").closest('p').addClass('error');
                        inputD.focus(function () { $(this).val(''); });
                        inputD.closest('p').find(':checkbox').one('click', function () {
                            if (!$(this).is(':checked')) {
                                inputD.val('').unbind('focus').closest('p').removeClass('error');
                            }
                        });
                    }
                    return;
                }

                if (inputE.length > 0 && (inputE.val() == "" || inputE.val() == "U dient nog antwoord te geven.") && inputE.closest('p').find(':checkbox:checked').length > 0) {
                    if (!inputE.closest('p').hasClass('error')) {
                        inputE.val("U dient nog antwoord te geven.").closest('p').addClass('error');
                        inputE.focus(function () { $(this).val(''); });
                        inputE.closest('p').find(':checkbox').one('click', function () {
                            if (!$(this).is(':checked')) {
                                inputE.val('').unbind('focus').closest('p').removeClass('error');
                            }
                        });
                    }
                    return;
                }

                if (inputA1.length > 0 && inputA2.length > 0 && inputA3.length > 0 && (inputA1.val() == "" && inputA2.val() == "" && inputA3.val() == "") && inputA1.closest('p').find(':radio:checked').length > 0) {
                    if (!inputA1.closest('p').hasClass('error')) {
                        inputA1.closest('p').addClass('error').closest('div').find(':radio').one('click', function () {
                            if (w.find(':radio:checked').val() != 'A') {
                                inputA1.val('').closest('p').removeClass('error');
                                inputA2.val('');
                                inputA3.val('');
                            }
                        });
                    }
                    return;
                }

                if (inputB.length > 0 && inputB.val() == "" && inputB.closest('p').find(':radio:checked').length > 0) {
                    if (!inputB.closest('p').hasClass('error')) {
                        inputB.closest('p').addClass('error').closest('div').find(':radio').one('click', function () {
                            if (w.find(':radio:checked').val() != 'B') {
                                inputB.val('').closest('p').removeClass('error');
                            }
                        });
                    }
                    return;
                }

                $w.find('p').removeClass('error');

                var staticQuestion = {
                    Answer: answer,
                    inputA: inputA ? inputA.val() : "",
                    inputD: inputD ? inputD.val() : "",
                    inputA1: inputA1 ? inputA1.val() : "",
                    inputA2: inputA2 ? inputA2.val() : "",
                    inputA3: inputA3 ? inputA3.val() : "",
                    inputB: inputB ? inputB.val() : "",
                    inputE: inputE ? inputE.val() : ""
                }

                if (options.added) {
                    options.added(uuid, staticQuestion, $w);
                    //PushWish(uuid, wish);
                }
                $w.find('input,textarea').attr('disabled', 'disabled');
                $(this).hide();
                $w.removeClass('status-unchecked').addClass('status-checked').find('a.added,span.tips').show();
                $w.find('span.tips').css('display', "inline");
            });

            $w.find('span.tips a').click(function () {
                $w.removeClass('status-checked').addClass('status-unchecked').find('input,textarea').removeAttr('disabled');
                //w.find(':text,textarea').val('');
                //$w.find(':checkbox, :radio').val([]);
                $w.find('a.added,span.tips').hide();
                $w.find('a.add').show();
                if (options.removed) {
                    options.removed(uuid, $w);
                    //   RemoveWish(uuid);
                }
            });
        });
    }
})(jQuery);

(function ($) {
    $.fn.wishType = function () {
        var wishType = GetWishType();
        if (wishType && wishType != "both") {
            this.find('span.tips').show();
            this.find('a.add').hide();
            this.find('a.added').show();
            this.find('input').val([wishType]).attr('disabled', 'disabled');
            this.find('select').attr('disabled', 'disabled');
            this.prev('div.title').removeClass('uncomplete').addClass('completed').find('.determine').text('Wens is ingevuld').next('.icon-check-green').show();
        }
    }
})(jQuery);


/* html encode */
String.prototype.htmlencode = function () {
    return this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}

document.write('<script src="/template/rootsite/BinaryResource/Assets/js/jquery.bt.min.js"' + ' type="text/javascript"><' + '/script>');
$(function () {
    if ($.cookie('fontSize')) {
        var arr = $.cookie('fontSize').split(',');
        setFontSize(arr[0], document.getElementById(arr[1]));
    }

    $('span.icon-question').bt({
        positions: ['bottom'],
        contentSelector: "$(this).children().html()",
        spikeGirth: 0,
        spikeLength: 0,
        shadowColor: '#8A8474',
        width: '400px'
    });

    $("input:text").keypress(function (e) {
        var keycode = (e.keyCode == undefined) ? 0 : e.keyCode;
        var witch = (e.which == undefined) ? 0 : e.which;
        var charcode = (e.charCode == undefined) ? 0 : e.charCode;
        var currKey = (keycode || witch || charcode);
        ////keyCode:{'<':60,'>':62,'/':47}
        if (currKey == 60 || currKey == 62 || currKey == 47) {
            e.stopPropagation();
            e.preventDefault();
        }
    });

    $("dd.description a").each(function () {
        var height = $(this).height();
        var parentHeight = 57;
        if (parentHeight > height) {
            $(this).css("margin-top", ((parentHeight - height) / 2) + "px");
        }
        else if (parentHeight < height) {
            $(this).parent().prev().find("img").css("margin-top", ((height - parentHeight) / 2) + "px");
        }

    });
});

$(function () {
    $('img[notsave]').bind("contextmenu", function (e) { return false; })
});
