{"version":3,"file":"../../../javascript/pages/warranty.js","names":["$","debugHeight","headerHeight","headerBuffer","addCount","header","addProduct","identifier","options","isSpecial","isLoopback","lastProduct","fieldMap","field","required","title","code","wrap","quantity","batchNumber","purchasedFrom","purchasedOnM","purchasedOnY","invoiceNumber","coastalPlaced","product","class","html","appendTo","hasOwnProperty","fieldInput","find","label","prev","length","applyErrorClasses","is","prop","val","attr","replace","text","name","window","tippy","animation","arrow","theme","interactive","toggleForm","removeClass","addClass","checkReplace","document","querySelector","classList","remove","setAttribute","el","value","quantityControl","Astro","QuantityControl","mode","getAttribute","id","init","quantityControls","push","animate","scrollTop","offset","top","hide","show","elementWrap","labelText","slice","append","getProductName","split","on","e","outerHeight","css","trigger","productCode","String","coastalProducts","indexOf","Date","this","closest","preventDefault","postData","each","product_identifier","i","postIdentifier","product_product_code","product_product_title","product_quantity_purchased","product_batch_number","product_purchased_from","product_purchased_on_month","product_purchased_on_year","product_invoice_number","product_coastal_placed","product_product_isSpecial","contact_estimated_completion_month","contact_estimated_completion_year"],"sources":["pages/warranty.js"],"mappings":"AAAAA,EAAE,WACE,IAGIC,EAAaC,EAAcC,EAH3BC,EAAW,EAEXC,EAASL,EAAE,iBAwFf,SAASM,EAAWC,EAAYC,EAASC,EAAWC,GAChD,IAsDIC,EAtDAC,EAAW,CACPH,UAAW,CACPI,MAAO,uCACPC,UAAU,GAEdC,MAAO,CACHF,MAAO,mCACPC,UAAU,GAEdE,KAAM,CACFH,MAAO,kCACPI,KAAM,6BACNH,UAAU,GAEdI,SAAU,CACNL,MAAO,wCACPI,KAAM,mCACNH,UAAU,GAEdK,YAAa,CACTN,MAAO,kCACPI,KAAM,6BACNH,UAAU,GAEdM,cAAe,CACXP,MAAO,oCACPI,KAAM,+BACNH,UAAU,GAEdO,aAAc,CACVR,MAAO,wCACPI,KAAM,6BACNH,UAAU,GAEdQ,aAAc,CACVT,MAAO,uCACPI,KAAM,6BACNH,UAAU,GAEdS,cAAe,CACXV,MAAO,oCACPI,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nC,EAAE,4CAExBA,EAAE,uCAAuCsC,IAAIwD,SAASe,oCAElDf,SAASgB,kCAAkC5E,QAC3CC,EAAkBnC,EAAE,4CAExBA,EAAE,sCAAsCsC,IAAIwD,SAASgB,mCA8L7D","ignoreList":[],"sourceRoot":"../../../../../resources/javascript","sourcesContent":["$(function() {\n    var addCount = 0;\n\n    var header = $('header.header'),\n        debugHeight, headerHeight, headerBuffer, productHeight;\n\n    $(window)\n        .on('resize', function(e) {\n            headerHeight = header.outerHeight();\n            debugHeight = $('#admin-bar').outerHeight() + $('#debug-bar').outerHeight();\n            // Header isn't included as a buffer if it's mobile\n            headerBuffer = header.css('position') == 'fixed'\n                ? headerHeight + debugHeight\n                : debugHeight\n        })\n        .trigger('resize');\n\n    $(window)\n        .on('scroll', function(e) {\n            scrollTop = $(document).scrollTop();\n        });\n\n    $('#js-add-product')\n        .on('click', function() {\n            var productCode = String($('#js-warranty-products option:selected').val()) || false;\n            $('#js-select-error').text('');\n\n            if(!productCode){\n                $('#js-select-error').text('Please select a product from the dropdown');\n                return;\n            }\n\n            if (coastalProducts.indexOf(productCode) > -1) {\n                $('#js-select-error')\n                    .text(productCode + ' - ' + getProductName() + ' has been added to the registration but please note this product is already covered by the 10 year Coastal Warranty.');\n            }\n\n            addProduct((+ new Date()), {code: productCode, name: getProductName()});\n        });\n\n    $('a.add-special')\n        .on('click', function(){\n            addProduct((+ new Date()), {}, 'true');\n        });\n\n    $('#js-products')\n        .on('click', '.product .remove', function(){\n            $(this).closest('.product').remove();\n            toggleForm();\n            return false;\n        })\n        .on('click', '.product .js-add-more', function(e){\n            e.preventDefault();\n\n            $('html, body')\n                .animate({\n                    scrollTop: $('#js-products-select').offset().top - headerBuffer\n                }, 500);\n        });\n\n    if (typeof postData != 'undefined'){\n        $.each(postData.product_identifier, function(i, postIdentifier) {\n            addProduct(\n                postIdentifier,\n                {\n                    code: postData.product_product_code[postIdentifier],\n                    name: postData.product_product_title[postIdentifier],\n                    quantity: postData.product_quantity_purchased[postIdentifier],\n                    batchNumber: postData.product_batch_number[postIdentifier],\n                    purchasedFrom: postData.product_purchased_from[postIdentifier],\n                    purchasedOnM: postData.product_purchased_on_month[postIdentifier],\n                    purchasedOnY: postData.product_purchased_on_year[postIdentifier],\n                    invoiceNumber: postData.product_invoice_number[postIdentifier],\n                    coastalPlaced: typeof postData.product_coastal_placed != 'undefined' && typeof postData.product_coastal_placed[postIdentifier] != 'undefined'\n                },\n                postData.product_product_isSpecial[postIdentifier],\n                true\n            );\n        });\n\n        if(!postData.contact_estimated_completion_month.length){\n            applyErrorClasses($('#contact_estimated_completion_date_wrap'));\n        }\n        $('#contact_estimated_completion_month').val(postData.contact_estimated_completion_month);\n\n        if(!postData.contact_estimated_completion_year.length){\n            applyErrorClasses($('#contact_estimated_completion_date_wrap'));\n        }\n        $('#contact_estimated_completion_year').val(postData.contact_estimated_completion_year);\n    }\n\n    function addProduct(identifier, options, isSpecial, isLoopback) {\n        var fieldMap = {\n                isSpecial: {\n                    field: '[name^=\"product_product_isSpecial[\"]',\n                    required: false\n                },\n                title: {\n                    field: '[name^=\"product_product_title[\"]',\n                    required: false\n                },\n                code: {\n                    field: '[name^=\"product_product_code[\"]',\n                    wrap: '.product_product_code_wrap',\n                    required: true\n                },\n                quantity: {\n                    field: '[name^=\"product_quantity_purchased[\"]',\n                    wrap: '.product_quantity_purchased_wrap',\n                    required: true\n                },\n                batchNumber: {\n                    field: '[name^=\"product_batch_number[\"]',\n                    wrap: '.product_batch_number_wrap',\n                    required: false\n                },\n                purchasedFrom: {\n                    field: '[name^=\"product_purchased_from[\"]',\n                    wrap: '.product_purchased_from_wrap',\n                    required: true\n                },\n                purchasedOnM: {\n                    field: '[name^=\"product_purchased_on_month[\"]',\n                    wrap: '.product_purchased_on_wrap',\n                    required: true\n                },\n                purchasedOnY: {\n                    field: '[name^=\"product_purchased_on_year[\"]',\n                    wrap: '.product_purchased_on_wrap',\n                    required: true\n                },\n                invoiceNumber: {\n                    field: '[name^=\"product_invoice_number[\"]',\n                    wrap: '.product_invoice_number_wrap',\n                    required: true\n                },\n                coastalPlaced: {\n                    field: '[name^=\"product_coastal_placed[\"]',\n                    wrap: '.product_coastal_placed_wrap',\n                    required: false\n                }\n            },\n            product = $('<div/>', {\n                class: 'product',\n                html: $('.product__template').html()\n            }),\n            lastProduct;\n\n        product.appendTo('#js-products');\n\n        lastProduct = $('.product:last');\n\n        for (var field in fieldMap) {\n            if (fieldMap.hasOwnProperty(field)) {\n                var fieldInput = lastProduct.find(fieldMap[field].field),\n                    label = fieldInput.prev('label');\n\n                if ((typeof options[field] != 'undefined')\n                    && !options[field].length\n                    && isLoopback\n                    && fieldMap[field].required\n                ) {\n                    applyErrorClasses(lastProduct.find(fieldMap[field].wrap));\n                }\n\n                if (fieldInput.is(':checkbox')) {\n                    fieldInput.prop('checked', options[field] ? true : false);\n                } else {\n                    fieldInput.val(options[field] || '')\n                }\n\n                fieldInput\n                    .attr('name', fieldInput.attr('name').replace(']', identifier + ']'));\n\n                if (fieldInput.attr('id')) {\n                    fieldInput\n                        .attr('id', fieldInput.attr('id') + '_' + identifier);\n                }\n\n                if (label.attr('for')) {\n                    label\n                        .attr('for', label.attr('for') + '_' + identifier);\n                }\n            }\n        }\n\n        isSpecial = isSpecial === 'true';\n\n        lastProduct\n            .find(fieldMap.code.field)\n            .prop('readonly', !isSpecial);\n        lastProduct\n            .find('.form__section-title__title')\n            .text(isSpecial ? 'CUSTOMISED PRODUCT' : options.name);\n        lastProduct\n            .find(fieldMap.title.field)\n            .val(isSpecial ? '' : options.name);\n        lastProduct\n            .find(fieldMap.isSpecial.field)\n            .val(isSpecial || false);\n        lastProduct\n            .find('[name^=\"product_identifier[\"]')\n            .val(identifier);\n\n        window.tippy('#js-products .fa-info-circle', {\n            animation: 'fade',\n            arrow: true,\n            theme: 'astro',\n            interactive: true\n        });\n\n        toggleForm();\n\n        // Support input replacement\n        product.find('.js-checkreplace--dummy').removeClass('js-checkreplace--dummy').addClass('js-checkreplace--ready');\n        var checkReplace = document.querySelector('.js-checkreplace--ready');\n        checkReplace.classList.remove('js-checkreplace--ready');\n        checkReplace.setAttribute('check-replace', true);\n\n        // Support quantity controls\n        addCount++;\n\n        product.find('.js-quantity-control--dummy').removeClass('js-quantity-control--dummy').addClass('js-quantity-control--ready');\n        var el = document.querySelector('.js-quantity-control--ready');\n        el.classList.remove('js-quantity-control--ready');\n        el.querySelector('input').value = 1;\n\n        var quantityControl = new Astro.QuantityControl({\n            el: el,\n            mode: el.getAttribute('quantity-control'),\n            id: 'quantity-control-' + addCount\n        });\n        quantityControl.init();\n\n        Astro.quantityControls.push(quantityControl);\n\n        if (!isLoopback) {\n            $('html, body')\n                .animate({\n                    scrollTop: lastProduct.offset().top - (headerBuffer + 10)\n                }, 500);\n        }\n    }\n\n    function toggleForm() {\n        if ($('#js-products .product').length) {\n            $('#js-warranty-form').removeClass('d3r-hidden');\n            $('#js-buffer').hide();\n        } else {\n            $('#js-warranty-form').addClass('d3r-hidden');\n            $('#js-buffer').show();\n        }\n    }\n\n    // Because Product inputs are coded manually - error classes are not\n    // automatically added\n    function applyErrorClasses(elementWrap) {\n        var label = $(elementWrap).find('label');\n        var labelText = $(label).text().slice(0, -2); // Removes asterisk\n\n        $(elementWrap).addClass('field-error');\n        $(label).addClass('error');\n\n        if (!$(elementWrap).find('p.error').length){\n            $(elementWrap).append(\n                $('<p/>', {\n                    class: 'error',\n                    text: labelText + ' is a required field'\n                })\n            );\n        }\n    }\n\n    function getProductName() {\n        return $('#js-warranty-products option:selected')\n            .text()\n            .split(' - ')[1];\n    }\n});\n"]}