//Gift Modal Functions

function modalOnLoad() {
	var savedOptions = EGW_getLsWithExpiry("effective_apps_egw_options_LS");
	if (savedOptions === null) {
		window.effectiveAppsEGWGWStatus = false;
		return;
	}

	var giftOptions = JSON.parse(savedOptions);
	if (giftOptions["effectiveAppsEgwGiftMessage"] !== undefined && 'False' === 'True') {
		document.getElementById("effectiveAppsEgwGiftMessage").checked = giftOptions["effectiveAppsEgwGiftMessage"];
		window.effectiveAppsEGWGMStatus = giftOptions["effectiveAppsEgwGiftMessage"];
	}
	if (giftOptions["effectiveAppsEgwGiftMessageText"] !== undefined && 'False' === 'True') {
		document.getElementById("effectiveAppsEgwGiftMessageText").value = giftOptions["effectiveAppsEgwGiftMessageText"];
        updateRemainingCharacters();
        window.effectiveAppsEGWGMStatus = giftOptions["effectiveAppsEgwGiftMessage"];
	}
	if (giftOptions["effectiveAppsEgwGiftReceipt"] !== undefined && 'False' === 'True') {
		document.getElementById("effectiveAppsEgwGiftReceipt").checked = giftOptions["effectiveAppsEgwGiftReceipt"];
		window.effectiveAppsEGWGRStatus = giftOptions["effectiveAppsEgwGiftReceipt"];
	}
	if (giftOptions["effectiveAppsEgwGiftWrap"] !== undefined && 'True' === 'True') {
		document.getElementById("effectiveAppsEgwGiftWrap").checked = giftOptions["effectiveAppsEgwGiftWrap"];
		window.effectiveAppsEGWGWStatus = giftOptions["effectiveAppsEgwGiftWrap"];
		if (giftOptions.hasOwnProperty("effectiveAppsEgwGiftWrapSelectedVariantId") && giftOptions["effectiveAppsEgwGiftWrap"] === true) {
		    setTimeout(function() {
		        document.querySelector('[gw-variant-id="'+giftOptions["effectiveAppsEgwGiftWrapSelectedVariantId"]+'"]').click();
		    }, 50);
		}
	}
}

function validateAndSaveGiftOptions() {
	var previousOptions = EGW_getLsWithExpiry("effective_apps_egw_options_LS");
	var giftOptions = {};
	var giftMessageOptions = {};
	var giftWrapOptions = {};
	var giftReceiptOptions = {};
	if ('False' === 'True')
		giftMessageOptions = updateGiftMessageStatus();

	if ('False' === 'True')
		giftReceiptOptions = updateGiftReceiptStatus();

	if ('True' === 'True')
		giftWrapOptions = updateGiftWrapStatus();

	var giftOptions = Object.assign({}, giftMessageOptions, giftWrapOptions, giftReceiptOptions);
	submitCartUpdates(giftOptions);
	if (previousOptions !== null) {
		previousOptions = JSON.parse(previousOptions);
		if (previousOptions["effectiveAppsEgwGiftWrap"] !== giftOptions["effectiveAppsEgwGiftWrap"]) {
			window.effectiveAppsEGWGWStatusChanged = true;
		}
	} else if (giftOptions["effectiveAppsEgwGiftWrap"] === true) {
		window.effectiveAppsEGWGWStatusChanged = true;
	}

	EGW_setLsWithExpiry("effective_apps_egw_options_LS", JSON.stringify(giftOptions), 600000);
}

function updateGiftMessageStatus() {
	var giftMessageStatus = document.getElementById('effectiveAppsEgwGiftMessage').checked;
	var giftMessageText = document.getElementById('effectiveAppsEgwGiftMessageText').value;
	if (giftMessageText.length < 1 && giftMessageStatus === true) {
		document.getElementById('effectiveAppsEgwGiftMessageText').style.outline = "none !important";
		document.getElementById('effectiveAppsEgwGiftMessageText').style.border = "1px solid red";
		document.getElementById('effectiveAppsEgwGiftMessageText').setAttribute('aria-invalid', 'true');
		throw "Empty Gift Message Text!";
	} else {
		document.getElementById('effectiveAppsEgwGiftMessageText').style.border = "1px solid";
		document.getElementById('effectiveAppsEgwGiftMessageText').removeAttribute('aria-invalid');
	}

	if (window.effectiveAppsEGWGMStatus !== giftMessageStatus) {
		if (giftMessageStatus === true) {
			window.effectiveAppsEGWGMStatus = true;
			window.effectiveAppsEGWGMLastAction = 'add';
		} else {
			window.effectiveAppsEGWGMStatus = false;
			window.effectiveAppsEGWGMLastAction = 'remove';
		}
	}

	return {
		"effectiveAppsEgwGiftMessage": giftMessageStatus,
		"effectiveAppsEgwGiftMessageText": giftMessageStatus ? giftMessageText : ""
	};
}

function updateGiftReceiptStatus() {
	var giftReceiptStatus = document.getElementById('effectiveAppsEgwGiftReceipt').checked;
	if (window.effectiveAppsEGWGRStatus !== giftReceiptStatus) {
		if (giftReceiptStatus === true) {
			window.effectiveAppsEGWGRStatus = true;
		} else {
			window.effectiveAppsEGWGRStatus = false;
		}
	}

	return {
		"effectiveAppsEgwGiftReceipt": giftReceiptStatus
	};
}

function updateGiftWrapStatus() {
    var selectedGiftWrapOptionVariantId = null;
    if (window.effectiveAppsGwOptions["gift_wrap_options"].length > 1) {
        selectedGiftWrapOptionVariantId = jQuery("input[name=effective_apps_gw_option]:checked").val();
    }

	var giftWrapStatus = document.getElementById('effectiveAppsEgwGiftWrap').checked;
	if (giftWrapStatus !== window.effectiveAppsEGWGWStatus) {
		if (giftWrapStatus === true) {
			window.effectiveAppsEGWGWStatus = true;
			window.effectiveAppsEGWGWLastAction = 'add';
		} else {
			window.effectiveAppsEGWGWStatus = false;
			window.effectiveAppsEGWGWLastAction = 'remove';
		}
	}

    if (selectedGiftWrapOptionVariantId !== null) {
        if (selectedGiftWrapOptionVariantId !== window.effectiveAppsSelectedGwVariantId) {
            window.effectiveAppsEGWGWLastAction = 'add';
        }

        return {
            "effectiveAppsEgwGiftWrap": giftWrapStatus,
            "effectiveAppsEgwGiftWrapSelectedVariantId": selectedGiftWrapOptionVariantId
        };
	}
	else {
        return {
            "effectiveAppsEgwGiftWrap": giftWrapStatus
        };
	}
}

function updateRemainingCharacters() {
	document.getElementById('effectiveAppsEgwGiftMessageText').style.border = "1px solid";
	document.getElementById('effectiveAppsEgwGiftMessageText').removeAttribute('aria-invalid');
	window.effectiveAppsEGWGMStatus = undefined;
	var remainingCharactersElement = document.getElementById('effectiveAppsEgwGiftMessageRC');
	var remainingCharacters = parseInt(document.getElementById('effectiveAppsEgwGiftMessageText').maxLength) - parseInt(document.getElementById('effectiveAppsEgwGiftMessageText').textLength);
	remainingCharactersElement.innerText = `Remaining characters: ` + remainingCharacters.toString();
}

function EGW_setLsWithExpiry(key, value, ttl) {
    value = value.toString();
	const now = new Date()
	const item = {
		value: value,
		expiry: now.getTime() + ttl,
	}
	localStorage.setItem(key, JSON.stringify(item))
}

function EGW_getLsWithExpiry(key) {
	const itemStr = localStorage.getItem(key)
	if (!itemStr) {
		return null
	}

	const item = JSON.parse(itemStr)
	const now = new Date()
	if (now.getTime() > item.expiry) {
		localStorage.removeItem(key)
		return null
	}

	return item.value.toString();
}

//
function EGW_runLogic() {
    try {
        var modalCancelButtonColorCSS = '';
        if (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) ||
            navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i) ||
            navigator.userAgent.match(/Windows Phone/i)) {
            window.EFFECTIVE_APPS_EGW_IS_MOBILE = true;
            modalCancelButtonColorCSS = '.effective-apps-egw-gw-swatch{opacity:.8;width:40px;height:40px;background-position:center center;background-size: cover;background-color:gray;display:inline-block !important;margin:10px}.effective-apps-egw-gw-swatch:hover{opacity:1}.radio-img>input{display:none}.radio-img>.effective-apps-egw-gw-swatch{cursor:pointer;border:2px solid #000}.radio-img>input:checked+.effective-apps-egw-gw-swatch{border:2px solid green} .tingle-modal__close { background-color: #3498db !important; } .tingle-modal-box { overflow-y: auto; height: 100% !important; background-color: #FFFFFF !important; } #effectiveAppsEgwGiftMessageRC { width: 100%; } .effective-apps-egw-modal .sectioned, .x-ws .effective-apps-egw-modal .x-ws-span6 { width: auto !important; }';
        } else {
            window.EFFECTIVE_APPS_EGW_IS_MOBILE = false;
            modalCancelButtonColorCSS = '.effective-apps-egw-gw-swatch{opacity:.8;width:40px;height:40px;background-position:center center;background-size: cover;background-color:gray;display:inline-block !important;margin:10px}.effective-apps-egw-gw-swatch:hover{opacity:1}.radio-img>input{display:none}.radio-img>.effective-apps-egw-gw-swatch{cursor:pointer;border:2px solid #000}.radio-img>input:checked+.effective-apps-egw-gw-swatch{border:2px solid green} .tingle-modal-box { border-top-left-radius: 25px !important; border-top-right-radius: 25px !important; border-bottom-left-radius: 25px !important; border-bottom-right-radius: 25px !important; } .tingle-modal-box__content { border-top-left-radius: 25px !important; border-top-right-radius: 25px !important; } .tingle-modal-box__footer { border-bottom-left-radius: 25px !important; border-bottom-right-radius: 25px !important; }';
        }

        var customModalFont = `Default`;
        var customModalFontCss = '';
        if (customModalFont !== '' && customModalFont.toLowerCase() !== 'default') {
            customModalFontCss = ".tingle-modal-box h1 { font-family: " + customModalFont + " !important; } .checkbox-text { font-family: " + customModalFont + " !important; } .tingle-modal-box h2 { font-family: " + customModalFont + " !important; } #effectiveAppsEgwGiftMessageLbl { font-family: " + customModalFont + " !important; } .smallsize { font-family: " + customModalFont + " !important; } #effectiveAppsEgwGiftMessageRC { font-family: " + customModalFont + " !important; } #effectiveAppsEgwGiftReceiptLbl { font-family: " + customModalFont + " !important; } .egwSaveGiftOptionsBtn  { font-family: " + customModalFont + " !important; } #effectiveAppsEgwGiftMessageText { font-family: " + customModalFont + " !important; } #effectiveAppsEgwGiftWrapLbl { font-family: " + customModalFont + " !important; } #effectiveAppsEgwGiftWrapPrice { font-family: " + customModalFont + " !important; }";
        }

        window.effectiveAppsGwLabelText = `Gift Wrap`;
        window.effectiveAppsSelectedGwVariantId = '40403376930886';
        window.effectiveAppsSelectedGmVariantId = '0';
        var savedOptions = EGW_getLsWithExpiry("effective_apps_egw_options_LS");
        if (savedOptions !== null) {
            var giftOptions = JSON.parse(savedOptions);
            if (giftOptions.hasOwnProperty("effectiveAppsEgwGiftWrapSelectedVariantId")) {
                window.effectiveAppsSelectedGwVariantId = giftOptions["effectiveAppsEgwGiftWrapSelectedVariantId"];
            }
        }

        window.effectiveAppsGwOptions = JSON.parse(`{"gift_wrap_options": [{"gw_title": "Gift Wrap", "gw_price": 4.0, "gw_explanation": "Add a beautiful gift wrap to surprise and delight.", "gw_variant_id": "40403376930886", "gw_img": "https://cdn.shopify.com/s/files/1/0395/1797/8787/t/6/assets/gift_wrap.png"}]}`);
        window.effectiveAppsEgwSp = '';
        window.effectiveAppsAllowedProducts = window.effectiveAppsEgwSp.split(',');
        window.EFFECTIVE_APPS_EGW_CUSTOM_CSS = ``;
        if (window.EGW_THEME_NAME.indexOf('xtra') > -1) {
            window.EFFECTIVE_APPS_EGW_CUSTOM_CSS += '[for="effectiveAppsEGWATC"] {display:inline !important;} [for="effectiveAppsEGW"] * {display:inline !important;} .effective-apps-egw-modal span {display: inline !important;} .tingle-modal__closeIcon svg {max-height: 35px; max-width: 35px;}';
        }

        if (window.EGW_THEME_NAME.indexOf('testament') > -1) {
            window.EFFECTIVE_APPS_EGW_CUSTOM_CSS += '.effectiveAppsEGWImage { width: unset !important; height: unset !important; } .effectiveAppsEGWImage { width: unset !important; height: unset !important; }';
        }

        if (window.EGW_THEME_NAME.indexOf('kalles') > -1 && location.pathname.indexOf('/cart') > -1) {
            window.EFFECTIVE_APPS_EGW_CUSTOM_CSS += '[for="effectiveAppsEGW"] * { text-align: right !important;}';
        }

        if (window.EGW_THEME_NAME.indexOf("vision") > -1) {
            window.EFFECTIVE_APPS_EGW_CUSTOM_CSS += '[for="effectiveAppsEGW"] {display:block !important;}';
        }

        if (window.EGW_THEME_NAME.indexOf("ella") > -1 && window.EGW_THEME_NAME.indexOf('shella') === -1) {
            window.EFFECTIVE_APPS_EGW_CUSTOM_CSS += '#effectiveAppsEGW { display: unset !important; border: unset !important; clip: unset !important; height: unset !important; overflow: unset !important; padding: unset !important; position: unset !important; width: unset !important; box-sizing: unset !important; } [for="effectiveAppsEGW"] img { display: inline !important;} #effectiveAppsEGWATC { display: unset !important; border: unset !important; clip: unset !important; height: unset !important; overflow: unset !important; padding: unset !important; position: unset !important; width: unset !important; box-sizing: unset !important; } [for="effectiveAppsEGWATC"] img { display: inline !important;}';
        }

        if (window.EGW_THEME_NAME.indexOf('local') > -1) {
            window.EFFECTIVE_APPS_EGW_CUSTOM_CSS += '[for="effectiveAppsEGWATC"] * {display:inline !important;} [for="effectiveAppsEGW"] * {display:inline !important;}';
        }

        jQuery('head').append('<style> ' + customModalFontCss + modalCancelButtonColorCSS + window.EFFECTIVE_APPS_EGW_CUSTOM_CSS + ' .cd-cart { z-index: 111111110 !important; } .cd-cart-overlay { z-index: 11111111 !important; } .egwSaveGiftOptionsBtn { background-color: #3498db !important; } .egwCancelBtn { background-color: #3498db !important; } .egw-modal { z-index:111111111111; } .tingle-modal-box__footer { background-color: #FFFFFF !important; } .tingle-modal-box__content { background-color: #FFFFFF !important;} @keyframes effectiveAppsWiggle{0%{transform:rotate(0)}80%{transform:rotate(0)}85%{transform:rotate(5deg)}95%{transform:rotate(-5deg)}100%{transform:rotate(0)}}div.effectiveAppsWiggle{display:inline-block;animation:effectiveAppsWiggle 1.5s infinite}div.effectiveAppsWiggle:hover{animation:none} div.effectiveAppsBlink{animation:egwBlinker 1.5s step-start infinite}@keyframes egwBlinker{50%{opacity:0}} div.effectiveAppsBlink:hover{animation:none} .effectiveAppsEGWImage { display: inline; } .effectiveAppsEGWATCImage { display: inline; }</style><link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/tingle/0.15.1/tingle.min.css">');
        if (location.pathname.indexOf('thank_you') > -1) {
            localStorage.removeItem('effective_apps_egw_options_LS');
            return;
        }
    }
    catch(err) {

    }

	String.prototype.format = function () {
		a = this;
		for (k in arguments) {
			a = a.replace("{" + k + "}", arguments[k])
		}
		return a
	}
	createGiftModal();
	EGW_attachToCart();
}

function EGW_isElementVisible(element) {
    if (element.getAttribute('egw-override') === 'true' || element.getAttribute('id') === 'cart-form-minicart' || element.getAttribute('id') === 'corner-cowi-cart-summary-card-cta-button' || window.EGW_THEME_NAME.indexOf('palo alto') > -1 || window.EGW_THEME_NAME.indexOf('turbo') > -1 || window.EGW_THEME_NAME.indexOf('aurora') > -1 || element.classList.contains('qsc2-checkout-button') || element.classList.contains('picky-bundle-add-to-cart-button')) {
        return true;
    }

	if (element.offsetWidth === 0 || element.offsetHeight === 0) return false;
	var height = document.documentElement.clientHeight,
		rects = element.getClientRects(),
		on_top = function (r) {
			var x = (r.left + r.right) / 2,
				y = (r.top + r.bottom) / 2;
			var pointElement = document.elementFromPoint(x, y);
			var isParentVisible = (pointElement !== null && (pointElement.parentElement === element))
			return ((pointElement === element) || (isParentVisible));
		};
	for (var i = 0, l = rects.length; i < l; i++) {
		var r = rects[i],
			in_viewport = r.top > 0 ? r.top <= height : (r.bottom > 0 && r.bottom <= height);
		if (in_viewport && on_top(r)) return true;
	}
	return false;
}

function EGW_detectCartNote() {
    var cartNoteSelectors = [
        '*[egw-cn-override="true"]',
        '*[name="note"]:not([disabled])'
    ];

    for (var selector of cartNoteSelectors) {
        var elementSearchResult = document.querySelector(selector);
        if (elementSearchResult !== null) {
            if (elementSearchResult.getAttribute('egw-ignore') === "true") {
                continue;
            }
            else {
                return elementSearchResult;
            }
        }
    }

    return null;
}

function EGW_detectATCButton() {
    var atcButtonSelectors = [
        '*[egw-atc-override="true"]',
        '*[data-pf-type="ProductATC"]:not([disabled])',
        '*[class*="cowlendar-btn"]:not([disabled])',
        '*[class*="add AddtoCart"]:not([disabled])',
        '*[class*="barracuda-cart-addToCartButton"]:not([disabled])',
        '[upsell-hooked-add-to-cart-btn="true"]:not([disabled])',
        '[id="new-form-atc"]:not([disabled])',
        '*[class*="engoj-btn-addtocart"]:not([disabled])',
	    'input[name="add"]:not([disabled])',
        'button[name="add"]:not([class*="product-bar__submit"]):not([class*="btn--small"]):not([disabled])',
        'button[id="add"]:not([disabled])',
        'input[id="add"]:not([disabled])',
        'button[id="addToCart"]:not([disabled])',
        'input[id="addToCart"]:not([disabled])',
        'input[name="AddToCart"]:not([disabled])',
        'button[name="AddToCart"]:not([disabled])',
        'input[id="AddToCart"]:not([disabled])',
        'button[id="AddToCart"]:not([disabled])',
        'button[class*="button--addToCart"]:not([disabled])',
        '*[class*="product-form--atc-button"]:not([disabled])',
        '*[class*="product__add-to-cart"]:not([disabled])',
        '*[id="shopify_add_to_cart"]:not([disabled])',
        '*[class*="product-form--add-to-cart"]:not([disabled])',
        '*[class*="ProductForm__AddToCart"]:not([disabled])',
        '*[class*="add-to-cart"]:not(div):not(img):not([disabled])',
        '*[class*="add_to_cart"]:not([class*="complementary-atc"]):not([disabled])',
        '*[class*="addtocart"]:not([disabled])',
        '*[class*="AddToCart"]:not([disabled])',
        '*[data-action="add-to-cart"]:not([disabled])',
        '*[data-add-to-cart]:not([disabled])',
        '*[name="add"]:not([class*="product-bar__submit"]):not([class*="btn--small"]):not([disabled])',
        '*[id="add-to-cart"]:not([disabled])',
        '*[data-lhi="trans_buynow"]:not([disabled])',
        '*[class*="add_to_cart_button"]:not([disabled])',
        '*[data-add-to-cart-text]:not([disabled])',
        '*[class*="btn-cart"]:not([disabled])',
        '*[data-add-cart]:not([disabled])',
        '*[class*="product-buy-buttons--cta"]:not([disabled])',
        '*[data-aid="product-form-submit-button"]:not([disabled])',
        '*[class*="lh-add_to_cart"]:not([disabled])',
        '*[data-text-add-to-cart]:not([disabled])',
        '*[class*="single_add_to_cart_button"]:not([disabled])',
        '*[data-text*="Add To Cart"]:not([disabled])',
        '*[id="adbtn"]:not([disabled])',
        '*[class*="addToCart"]:not([disabled])',
        '*[data-add-button]:not([disabled])',
        '*[data-buy-button]:not([disabled])',
        'buy-buttons:not([disabled])',
        '*[data-product-add]:not([disabled])',
        '.quantity-submit-row__submit [class*="button"]:not([disabled])',
        '*[class*="f8pr-buy-button"]:not([disabled])'
    ];

    for (var selector of atcButtonSelectors) {
        var elementSearchResult = document.querySelector(selector);
        if ((elementSearchResult !== null) && (elementSearchResult.getAttribute('egw-ignore') === "true")) {
            continue;
        }

        if (elementSearchResult !== null && elementSearchResult.innerText.toLowerCase().indexOf('sold out') === -1) {
            return elementSearchResult;
        }
    }

    return null;
}

function generateCartGiftOptionsNote(currentGiftOptions) {
    let result = "";
    if (currentGiftOptions === undefined) {
        currentGiftOptions = JSON.parse(EGW_getLsWithExpiry("effective_apps_egw_rc_options_LS"));
        if (currentGiftOptions === null) {
            return "";
        }
    }

    if (currentGiftOptions.hasOwnProperty("effectiveAppsEgwGiftMessage") && currentGiftOptions["effectiveAppsEgwGiftMessage"] === true) {
        result += "Gift Message: " + currentGiftOptions["effectiveAppsEgwGiftMessageText"] + ", ";
    }

    if (currentGiftOptions.hasOwnProperty("effectiveAppsEgwGiftWrap") && currentGiftOptions["effectiveAppsEgwGiftWrap"] === true) {
        if (window.effectiveAppsGwOptions["gift_wrap_options"].length > 1) {
            for (var giftOption of window.effectiveAppsGwOptions["gift_wrap_options"]) {
                if (currentGiftOptions["effectiveAppsEgwGiftWrapSelectedVariantId"] === giftOption["gw_variant_id"]) {
                    result += giftOption["gw_title"]+ ": ✓, ";
                }
            }
        }
        else {
            result += window.effectiveAppsGwLabelText + ": ✓, ";
        }
    }

    if (currentGiftOptions.hasOwnProperty("effectiveAppsEgwGiftReceipt") && currentGiftOptions["effectiveAppsEgwGiftReceipt"] === true) {
        result += "Gift Receipt: ✓, ";
    }

    return result.slice(0, -2);
}

function EGW_listenForCartChanges() {
    var giftMessageProductFound = false;
    if ('' === '') {
        giftMessageProductFound = true;
    }

    var giftWrapProductFound = false;
    var currentGiftOptions = JSON.parse(EGW_getLsWithExpiry("effective_apps_egw_options_LS"));

    jQuery.getJSON('/cart.js').done(function(data) {
        for (var item of data['items']) {
            if (item["product_id"].toString() === '7049038528582') {
                giftWrapProductFound = true;
            }

            if (item["product_id"].toString() === 'None') {
                giftMessageProductFound = true;
            }
        }

        if (giftMessageProductFound === false && currentGiftOptions["effectiveAppsEgwGiftMessage"] === true) {
        	currentGiftOptions["effectiveAppsEgwGiftMessage"] = false;
        	if ("1" === "1") {
        	    jQuery.post('/cart/update.js',{attributes: {'Gift Message': null} }, null, "json");
        	}
        	else {
        	    let giftOptionsNote = generateCartGiftOptionsNote(currentGiftOptions);
        	    jQuery.post('/cart/update.js',{note: giftOptionsNote }, null, "json");
        	    let cartNoteElement = EGW_detectCartNote();
        	    if (cartNoteElement !== null) {
        	        cartNoteElement.value = giftOptionsNote;
        	    }
        	}

        	EGW_setLsWithExpiry("effective_apps_egw_options_LS", JSON.stringify(currentGiftOptions), 600000);
        }

        if (giftWrapProductFound === false && currentGiftOptions["effectiveAppsEgwGiftWrap"] === true) {
        	var attributesDict = {};
        	if (window.effectiveAppsGwOptions["gift_wrap_options"].length > 1) {
                for (var giftOption of window.effectiveAppsGwOptions["gift_wrap_options"]) {
                    attributesDict[giftOption["gw_title"]] = null;
                }
            }
            else {
                attributesDict[window.effectiveAppsGwLabelText] = null;
            }

        	currentGiftOptions["effectiveAppsEgwGiftWrap"] = false;
        	if (currentGiftOptions.hasOwnProperty("effectiveAppsEgwGiftWrapSelectedVariantId")) {
                delete currentGiftOptions["effectiveAppsEgwGiftWrapSelectedVariantId"]
        	}

            if ("1" === "1") {
                jQuery.post('/cart/update.js', {attributes: attributesDict }, null, "json");
            }
        	else {
                let giftOptionsNote = generateCartGiftOptionsNote(currentGiftOptions);
                jQuery.post('/cart/update.js',{note: giftOptionsNote }, null, "json");
                let cartNoteElement = EGW_detectCartNote();
        	    if (cartNoteElement !== null) {
        	        cartNoteElement.value = giftOptionsNote;
        	    }
            }

        	EGW_setLsWithExpiry("effective_apps_egw_options_LS", JSON.stringify(currentGiftOptions), 600000);
        }

        if (false === shouldGiftOptionsCBBeTicked(currentGiftOptions)) {
            if (document.getElementById('effectiveAppsEGWATC') !== null) {
                document.getElementById('effectiveAppsEGWATC').checked = false;
            }

            if (document.getElementById('effectiveAppsEGW') !== null) {
                document.getElementById('effectiveAppsEGW').checked = false;
            }

            clearInterval(window.EFFECTIVE_APPS_EGW_CART_CHANGE_LISTENER);
        }

	});
}

function shouldGiftOptionsCBBeTicked(currentGiftOptions=null) {
    if (currentGiftOptions === null) {
        currentGiftOptions = JSON.parse(EGW_getLsWithExpiry("effective_apps_egw_options_LS"));
        if (currentGiftOptions === null) {
            return false;
        }
    }

    return ((currentGiftOptions.hasOwnProperty("effectiveAppsEgwGiftMessage") && currentGiftOptions["effectiveAppsEgwGiftMessage"] === true) ||
    (currentGiftOptions.hasOwnProperty("effectiveAppsEgwGiftWrap") && currentGiftOptions["effectiveAppsEgwGiftWrap"] === true) ||
    (currentGiftOptions.hasOwnProperty("effectiveAppsEgwGiftReceipt") && currentGiftOptions["effectiveAppsEgwGiftReceipt"] === true)
    );
}

function EGW_detectCheckoutButton(isCartPage=false) {
	var resultElement = null;
	var checkoutButtonSelectors = [
	    '*[egw-co-override="true"]',
	    '*[class*="_buttonsWrapper"] *[class*="picky-bundle-add-to-cart-button"]:not([disabled])',
	    '*[id="cart-form-minicart"]:not([disabled])',
	    '*[class*="qsc2-checkout-button"]:not([disabled])',
	    'button[form="cart-form"][name="checkout"]:not([disabled])',
	    'button[form="cartForm"][name="checkout"]:not([disabled])',
	    'button[form="cart-drawer-form"][name="checkout"][class*="js-cart-drawer-submit-btn"]:not([disabled])',
	    'aside .link-btn:not([class*="mobile-only"]) button:not([disabled])',
	    '*[class="gokwik-checkout"]:not([disabled])',
	    '*[id="corner-cowi-cart-summary-card-cta-button"]:not([disabled])',
	    '*[name*="checkout-route-"]:not([disabled])',
	    '*[data-ocu-checkout="true"]:not([disabled])',
	    '*[class*="rebuy-cart__checkout-button"]',
        '*[id="cart_checkout"]:not([disabled])',
		'*[name="checkout"]:not([disabled]):not([class*="product-cart-message__checkout-button"]):not([class*="product-message__btn"])',
		'*[name="goto_pp"]:not([disabled])',
		'*[name="goto_gc"]:not([disabled])',
		'*[value*="Checkout"]',
		'*[value*="Check out"]',
		'*[href*="/checkout"]:not([href*="front_end/login"]):not([href*="/manage/subscription/"]):not([href*="/admin/settings/checkout"]):not([href*="checkout-sandbox"]):not([href*="asset"]):not(link)',
        '*[href*="/checkout"]:not([href*="asset"]):not(link)',
		'*[onclick*="/checkout"]',
		'*[class*=satcb-cs-checkout-btn]',
		'*[class*="evm_upsell_checkout"]',
        '*[name="qbk-checkout"]',
        '*[class*="ymq-fake-checkout-btn"]:not([disabled])',
        '*[name="checkout-exemptify"]:not([disabled])',
        '*[id="checkout"]:not([disabled])',
        '*[data-cart-checkout-button]',
        '*[class*="cart-button-checkout"]',
        '*[class*="Cart__Checkout"]',
        '*[class*="checkout__fbtn"]',
        '*[name="ddcheckout"]:not([disabled])',
        '*[class*="btnCheckout"]:not([disabled])',
        '*[id="mu-checkout-button"]:not([disabled])',
        '*[id="cart-checkout"]:not([disabled])',
        '*[data-href="/checkout"]:not([disabled])',
        '*[aria-label="Checkout"]:not([disabled])',
        '*[aria-label="checkout"]:not([disabled])',
	];
	var cartFormSelectors = [
		'input[type="submit"]:not([disabled])',
		'input[type="button"]:not([disabled])'
	];

	for (var selector of checkoutButtonSelectors) {
		var elementSearchResult = document.querySelectorAll(selector);
		if (elementSearchResult.length > 1 && window.EGW_THEME_NAME.indexOf('dawn') === -1) {
		    if (selector === '*[id="cart-form-minicart"]:not([disabled])') {
		        return elementSearchResult[0];
		    }

		    if (elementSearchResult[1].getAttribute('egw-ignore') === "true") {
		        continue;
		    }

            if (window.EGW_THEME_NAME.indexOf('shapes') > -1 && isCartPage === true) {
		        return elementSearchResult[0];
		    }

		    if (elementSearchResult[1].hasOwnProperty('href')) {
		        if (elementSearchResult[1].getAttribute('href').indexOf("/front_end/login") > -1) {
		            continue;
		        }
		    }

			return elementSearchResult[1];
		} else if (elementSearchResult.length > 0) {
		    if (elementSearchResult[0].getAttribute('egw-ignore') === "true") {
		        continue;
		    }
		    if (elementSearchResult[0].hasOwnProperty('href')) {
		        if (elementSearchResult[0].getAttribute('href').indexOf("/front_end/login") > -1) {
		            continue;
		        }
		    }

            if (window.EGW_THEME_NAME.indexOf('dawn') > -1 && isCartPage === true && elementSearchResult.length > 1) {
                return elementSearchResult[1];
            }

			return elementSearchResult[0];
		}
	}

	var cartFormElement = document.querySelector('form[action="/cart"]');
	if (cartFormElement !== null) {
		for (var selector of cartFormSelectors) {
			var element = cartFormElement.querySelector(selector);
			if (element !== null) {
				return element;
			}
		}
	}

	return null;
}

function EGW_waitForCartDrawer() {
	if (document.getElementById('effectiveAppsEGW') !== null) {
		return;
	}

    var atcButtonElement = null;
    if ('True' === 'True' && location.pathname.indexOf('/products') > -1) {
        atcButtonElement = EGW_detectATCButton();
    }

    if (atcButtonElement !== null) {
        if (window.effectiveAppsEgwSp.length > 0) {
            if (window.effectiveAppsAllowedProducts.indexOf(ShopifyAnalytics.meta.product.id.toString()) > -1) {
                EGW_attachToATC(atcButtonElement);
            }
        }
        else {
            EGW_attachToATC(atcButtonElement);
        }
    }

	var checkoutButtonElement = EGW_detectCheckoutButton();
	if ('True' === 'False' || checkoutButtonElement === null) {
		return;
	}

	if (EGW_isElementVisible(checkoutButtonElement) === true) {
		if (window.effectiveAppsEgwSp.length > 0) {
            jQuery.getJSON('/cart.js').done(function(data) {
                for (var item of data['items']) {
                    if (window.effectiveAppsAllowedProducts.indexOf(item["product_id"].toString()) > -1) {
                        EGW_attachToCart(true);
                        break;
                    }
                }
            });
        }
        else {
            EGW_attachToCart(true);
        }
	}
}

function EGW_attachToATC(atcElementToAttachTo) {
    if (document.getElementById('effectiveAppsEGWATC') !== null) {
        return;
    }
	if (null === atcElementToAttachTo) {
		return;
	}

    var insertionType = 'afterbegin';
	var elementContainer = atcElementToAttachTo.parentElement;
	var checkboxElement = document.createElement('p');
	checkboxElement.innerHTML = `<div style="display: inline-block;" class=""><label style="display:inline; float:none" for="effectiveAppsEGWATC"><input style="-webkit-appearance: checkbox; float:none; vertical-align: middle;" type="checkbox" id="effectiveAppsEGWATC" name="effectiveAppsEGWATC" onchange="window.effectiveAppsEGWModal.open();"/> <span><img class="effectiveAppsEGWImage" alt="Gift options checkbox image" src="https://cdn.shopify.com/s/files/1/0395/1797/8787/t/6/assets/black_gift_box.png" /> <strong style="color: black; font-size: 14px;">Add gift options</strong></span></label></div>`;
	if (window.EGW_THEME_NAME.indexOf("brooklyn") > -1 || window.EGW_THEME_NAME.indexOf("minimal") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("blockshop") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("corano") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("pipeline") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("kingdom") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("m&j website") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("sleek") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("motion") > -1) {
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("enterprise") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("fastor") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("broadcast") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("blum") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("north") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("plantza") > -1) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("xtra") > -1) {
        elementContainer = atcElementToAttachTo
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("split") > -1) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("be yours") > -1) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("eva") > -1) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("local") > -1) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("yuva") > -1) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("meroxio") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("reformation") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("andaman") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("vegist") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("beyond") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("trademark") > -1 || window.EGW_THEME_NAME.indexOf("avone") > -1) {
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("minimog") > -1) {
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("woodstock") > -1) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("decorks") > -1) {
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("flex") > -1) {
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("simple") > -1) {
        elementContainer = atcElementToAttachTo.parentElement;
        checkboxElement.innerHTML = checkboxElement.innerHTML.replace('display: inline-block;', 'display: block;');
    }

    if (window.EGW_THEME_NAME.indexOf("booster-6.0.5") > -1) {
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("boost") > -1 && window.EGW_THEME_NAME.indexOf("booster-6.0.5") === -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("turbo") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("icon") > -1) {
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("empire") > -1) {
        insertionType = 'beforebegin';
    }

    if (atcElementToAttachTo.getAttribute('data-pf-type') === 'ProductATC') {
        insertionType = 'beforebegin';
        elementContainer = atcElementToAttachTo;
    }

    if (window.EGW_THEME_NAME.indexOf("flex") > -1) {
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("august") > -1) {
        insertionType = 'beforebegin';
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("emerge") > -1) {
        insertionType = 'beforebegin';
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("parallax") > -1) {
        insertionType = 'beforebegin';
        elementContainer = atcElementToAttachTo.parentElement.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("kalles") > -1) {
        insertionType = 'beforebegin';
        elementContainer = atcElementToAttachTo.parentElement.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("warehouse") > -1) {
        insertionType = 'beforebegin';
    }

    if (window.EGW_THEME_NAME.indexOf("drop") > -1) {
        insertionType = "beforebegin";
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("themekit template") > -1) {
        insertionType = "beforebegin";
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("atlantic") > -1) {
        insertionType = "beforebegin";
        elementContainer = atcElementToAttachTo;
    }

    if (window.EGW_THEME_NAME.indexOf("canopy") > -1) {
        insertionType = "beforebegin";
        elementContainer = atcElementToAttachTo.parentElement.parentElement;
    }

	elementContainer.insertAdjacentHTML(insertionType, checkboxElement.innerHTML);
	if (shouldGiftOptionsCBBeTicked() === true) {
        document.getElementById('effectiveAppsEGWATC').checked = true;
        if (window.EFFECTIVE_APPS_EGW_CART_CHANGE_LISTENER === undefined) {
            window.EFFECTIVE_APPS_EGW_CART_CHANGE_LISTENER = setInterval(EGW_listenForCartChanges, 1000);
        }
    }
    else {
        document.getElementById('effectiveAppsEGWATC').checked = false;
    }

	window.effectiveAppsEGWSuccessfullyAttachedToATC = true;
}

function EGW_attachToCart(isCartDrawer = false) {
    if (document.getElementById('effectiveAppsEGW') !== null) {
        return;
    }

	var isCartPage = ShopifyAnalytics.meta.page.pageType === "cart" || location.pathname.indexOf('/cart') === 0;
	if (isCartDrawer === false && isCartPage === false) {
		window.EFFECTIVE_APPS_EGW_CART_LISTENER = setInterval(EGW_waitForCartDrawer, 500);
		return;
	}

	if (window.effectiveAppsEgwSp.length > 0) {
        jQuery.getJSON('/cart.js').done(function(data) {
            for (var item of data['items']) {
                if (window.effectiveAppsAllowedProducts.indexOf(item["product_id"].toString()) > -1) {
                    EGW_attachToCartPage(isCartPage);
                    break;
                }
            }
        });
    }
    else {
        EGW_attachToCartPage(isCartPage);
    }
}

function EGW_attachToCartPage(isCartPage) {
    if ('True' === 'False') {
        return;
    }

    var checkboxElement = document.createElement('p');
    var insertionType = "beforeelement";
	checkboxElement.innerHTML = `<label style="display:inline; float:none" for="effectiveAppsEGW"><input style="-webkit-appearance: checkbox; float:none; vertical-align: middle;" type="checkbox" id="effectiveAppsEGW" name="effectiveAppsEGW" onchange="window.effectiveAppsEGWModal.open();"/> <span><img class="effectiveAppsEGWImage" alt="Gift options checkbox image" src="https://cdn.shopify.com/s/files/1/0395/1797/8787/t/6/assets/black_gift_box.png" /> <strong style="color: black; font-size: 14px;">Add gift options</strong></span></label>`;
    if ('' !== '' && '' !== 'openModal') {
        checkboxElement.innerHTML = '<div class="">' + checkboxElement.innerHTML + '</div>';
    }
	var checkoutButtonElement = EGW_detectCheckoutButton(isCartPage);
	if (null === checkoutButtonElement) {
		return;
	}

	let elementContainer = checkoutButtonElement.parentElement;
	if (window.EGW_THEME_NAME.indexOf('debut') > -1 || window.EGW_THEME_NAME.indexOf('express') > -1) {
		elementContainer = checkoutButtonElement.parentElement.parentElement;
	}

	if (window.EGW_THEME_NAME.indexOf('dawn') > -1 || window.EGW_THEME_NAME.indexOf('taste') > -1 || window.EGW_THEME_NAME.indexOf('studio') > -1 || window.EGW_THEME_NAME.indexOf('craft') > -1 || window.EGW_THEME_NAME.indexOf('crave') > -1 || window.EGW_THEME_NAME.indexOf('sense') > -1) {
	    if (isCartPage !== true) {
	        elementContainer = checkoutButtonElement.parentElement.parentElement;
	    }
        else {
            insertionType = "beforebegin";
        }
    }

    if (window.EGW_THEME_NAME.indexOf('maker') > -1) {
        elementContainer = checkoutButtonElement.parentElement.parentElement;
        checkboxElement.innerHTML = '<div style="width: 100%;">' + checkboxElement.innerHTML + '</div>';
    }

    if (window.EGW_THEME_NAME.indexOf("simple") > -1) {
        elementContainer = checkoutButtonElement.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("drop") > -1 || window.EGW_THEME_NAME.indexOf('shrine') > -1 || window.EGW_THEME_NAME.indexOf("district") > -1 || window.EGW_THEME_NAME.indexOf("aurora") > -1) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("modular") > -1) {
        insertionType = "beforebegin";
        elementContainer = checkoutButtonElement.parentElement.parentElement;
        checkboxElement.innerHTML = checkboxElement.innerHTML.replace('vertical-align: middle;', 'vertical-align: middle; display: inline;');
    }

    if (window.EGW_THEME_NAME.indexOf("impact") > -1 && isCartPage !== true) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("sleek") > -1 && isCartPage !== true) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("blum") > -1 && isCartPage !== true) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("vision") > -1 && isCartPage !== true) {
        elementContainer = checkoutButtonElement.parentElement.parentElement;
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("local") > -1 && isCartPage !== true) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("atlantic") > -1) {
        insertionType = "beforebegin";
        if (isCartPage === false) {
            elementContainer = checkoutButtonElement.parentElement.parentElement;
        }
    }

    if (window.EGW_THEME_NAME.indexOf("capital") > -1 && isCartPage === true) {
        insertionType = "beforebegin";
        checkboxElement.innerHTML = checkboxElement.innerHTML.replace('display:inline', 'display:block');
    }

    if (window.EGW_THEME_NAME.indexOf("kalles") > -1 && isCartPage === true) {
        insertionType = "beforebegin";
    }

    if (location.pathname.indexOf('apps/giftregistry') > -1) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("xtra") > -1 && isCartPage === true) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("envy") > -1 && isCartPage === true) {
        insertionType = "beforebegin";
    }

    if (checkoutButtonElement.classList.contains('satcb-cs-checkout-btn')) {
        insertionType = "beforebegin";
    }

    if (checkoutButtonElement.classList.contains('picky-bundle-add-to-cart-button')) {
        insertionType = "beforebegin";
    }

    if (checkoutButtonElement.getAttribute('id') === 'corner-cowi-cart-summary-card-cta-button') {
        insertionType = "beforebegin";
    }

    if (checkoutButtonElement.classList.contains('cd-cart-checkout-button')) {
        insertionType = "beforebegin";
    }

    if (checkoutButtonElement.classList.contains('rebuy-cart__checkout-button')) {
        insertionType = "beforebegin";
    }

    if (checkoutButtonElement.getAttribute('data-ocu-checkout') !== null) {
        insertionType = "beforebegin";
        elementContainer = checkoutButtonElement.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("label") > -1 && isCartPage === true) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("editions") > -1 && isCartPage === true) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("themekit template") > -1) {
        insertionType = "beforebegin";
        elementContainer = checkoutButtonElement;
    }

    if (window.EGW_THEME_NAME.indexOf("shapes") > -1) {
        insertionType = "beforebegin";
        elementContainer = checkoutButtonElement.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("blockshop") > -1) {
        insertionType = "beforebegin";
        elementContainer = checkoutButtonElement.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("be yours") > -1) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("motion") > -1 && isCartPage === false) {
        elementContainer = checkoutButtonElement;
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("fastlane") > -1) {
        elementContainer = checkoutButtonElement;
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("ella") > -1 && window.EGW_THEME_NAME.indexOf('shella') === -1) {
        insertionType = "beforebegin";
        elementContainer = checkoutButtonElement.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("plantza") > -1) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("emerge") > -1) {
        elementContainer = checkoutButtonElement.parentElement.parentElement;
    }

    if (window.EGW_THEME_NAME.indexOf("showcase") > -1 && isCartPage !== true) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("symmetry") > -1) {
        insertionType = "beforebegin";
    }

    if (window.EGW_THEME_NAME.indexOf("warehouse") > -1) {
        insertionType = 'beforebegin';
        if (isCartPage === true) {
            elementContainer = checkoutButtonElement;
        }
    }

    if (checkoutButtonElement.getAttribute('class') !== null && checkoutButtonElement.getAttribute('class').indexOf("evm_upsell_checkout") > -1) {
        insertionType = "beforebegin";
    }

    if (checkoutButtonElement.getAttribute('id') !== null && checkoutButtonElement.getAttribute('id').indexOf("mu-checkout-button") > -1) {
        elementContainer = checkoutButtonElement;
        insertionType = "beforebegin";
    }

    if (document.getElementById('effectiveAppsEGW') !== null && document.getElementById('effectiveAppsEGW') !== undefined) {
        return;
    }

	if (insertionType === "beforebegin") {
        elementContainer.insertAdjacentHTML('beforebegin', checkboxElement.innerHTML);
    }
    else {
	    elementContainer.innerHTML = checkboxElement.innerHTML + elementContainer.innerHTML;
    }

	if (shouldGiftOptionsCBBeTicked() === true) {
        document.getElementById('effectiveAppsEGW').checked = true;
        if (window.EFFECTIVE_APPS_EGW_CART_CHANGE_LISTENER === undefined) {
            window.EFFECTIVE_APPS_EGW_CART_CHANGE_LISTENER = setInterval(EGW_listenForCartChanges, 1000);
        }
    }
    else {
        document.getElementById('effectiveAppsEGW').checked = false;
    }

	if ('' === 'openModal' && location.pathname.indexOf('/cart') === 0 && shouldGiftOptionsCBBeTicked() === false && EGW_getLsWithExpiry("effective_apps_egw_awareness_modal_opened") === null) {
	    document.getElementById('effectiveAppsEGW').click();
	    EGW_setLsWithExpiry("effective_apps_egw_awareness_modal_opened", 'true', 300000);
    }

	window.effectiveAppsEGWSuccessfullyAttachedToCart = true;
}

function toggleSideCartOverlay(eventType='openModal') {
    try {
        if (eventType === 'openModal') {
            try {
                document.querySelectorAll('[class*="tingle-modal"]')[0].setAttribute('style', 'z-index: 111111111111;');
                document.querySelector('[class*="tingle-modal-box"]').setAttribute('aria-live', 'polite');
                document.querySelector('[class*="tingle-modal-box__content"] h2').setAttribute('tabindex', '0');
                document.querySelector('[class*="tingle-modal-box__content"] h2').setAttribute('autofocus', 'true');
                document.querySelector('[class*="tingle-modal-box__content"] h2').focus();
                document.querySelector('[class*="tingle-modal-box__content"] h2').click();
                document.querySelector('[class*="tingle-modal__close"]').setAttribute('role', 'button');
            }
            catch(err) {
            }

            if (window.EGW_THEME_NAME.indexOf("brooklyn") > -1 && typeof timber !== 'undefined' && window.effectiveAppsEGWSuccessfullyAttachedToATC !== true) {
                timber.RightDrawer.close(new Event(1));
            }
            else if (window.EGW_THEME_NAME.indexOf("express") > -1 || window.EGW_THEME_NAME.indexOf("prestige") > -1 || window.EGW_THEME_NAME.indexOf("debutify") > -1 || window.EGW_THEME_NAME.indexOf("lalicious") > -1) {
                for (var i=0; i < 10; i++) {
                    try {
                        document.querySelector('[tabindex="-1"]').setAttribute('tabindex', '');
                    }
                    catch (err) {
                        break;
                    }
                }
            }
            else if (document.body.getAttribute('class').indexOf('js-drawer-open-right') > -1) {
                document.body.setAttribute('class', document.body.getAttribute('class').replace('js-drawer-open-right', ''));
                window.RecoverBodyJsDrawerCartNeeded = true;
            }

            if (document.getElementsByTagName('html')[0].getAttribute('class').indexOf('js-drawer-open-right') > -1) {
                document.getElementsByTagName('html')[0].setAttribute('class', document.getElementsByTagName('html')[0].getAttribute('class').replace('js-drawer-open-right', ''));
                window.RecoverHeadJsDrawerCartNeeded = true;
            }

            if (document.querySelector('[class*=mfp-wrap]') !== null) {
                if (document.querySelector('[class*=mfp-wrap]').getAttribute("tabindex") !== null) {
                    window.RecoverDrawerCartTabIndex = document.querySelector('[class*=mfp-wrap]').getAttribute("tabindex");
                    document.querySelector('[class*=mfp-wrap]').setAttribute("tabindex", '');
                }
            }

            if (document.getElementById("CartDrawer") !== null) {
                if (document.getElementById("CartDrawer").getAttribute("tabindex") !== null) {
                    window.RecoverDrawerCartTabIndex2 = document.getElementById("CartDrawer").getAttribute("tabindex");
                    document.getElementById("CartDrawer").setAttribute("tabindex", '');
                }
            }

            if (document.querySelector("[data-section-id='cart-drawer']") !== null) {
                if (document.querySelector("[data-section-id='cart-drawer']").getAttribute("tabindex") !== null) {
                    window.RecoverDrawerCartTabIndex3 = document.querySelector("[data-section-id='cart-drawer']").getAttribute("tabindex");
                    document.querySelector("[data-section-id='cart-drawer']").setAttribute("tabindex", '');
                }
            }

            if (document.querySelector("[class*='drawer--visible']") !== null) {
                document.querySelector("[class*='drawer--visible']").setAttribute("recover-drawer-visibility-4", 'true');
                window.RecoverDrawerCart4 = true;
                document.querySelector("[class*='drawer--visible']").classList.remove('drawer--visible');
            }

            if (document.querySelector("[data-sidebar-drawer-animation-state='open']") !== null) {
                document.querySelector("[data-sidebar-drawer-animation-state='open']").setAttribute("recover-drawer-visibility-5", 'true');
                window.RecoverDrawerCart5 = true;
                document.querySelector("[data-sidebar-drawer-animation-state='open']").removeAttribute("data-sidebar-drawer-animation-state");
            }

            if (window.EGW_THEME_NAME.indexOf("impulse") > -1 && location.pathname.indexOf('/cart') === -1 && document.getElementById("CartDrawer") !== null && document.getElementById("CartDrawer").classList.contains("drawer--is-open") === true) {
                window.RecoverDrawerCart6 = true;
            }

            if (window.EGW_THEME_NAME.indexOf("retina") > -1 && location.pathname.indexOf('/cart') === -1 && document.querySelector('[data-animation-state]') !== null && document.querySelector('[data-animation-state]').getAttribute("data-animation-state") === "open") {
                document.querySelector('[data-cart-drawer-close]').click();
                window.RecoverDrawerCart7 = true;
            }

            if (window.EGW_THEME_NAME.indexOf("modular") > -1 && location.pathname.indexOf('/cart') === -1 && document.querySelector('body') !== null && document.querySelector('body').getAttribute("class").indexOf("cart--is-visible") > -1) {
                document.querySelector("body").classList.remove('cart--is-visible');
                window.RecoverDrawerCart8 = true;
            }

            if (document.querySelector('[class*="slidecarthq-overlay open"]') !== null) {
                document.querySelector('[class*="slidecarthq-overlay open"]').setAttribute("recover-drawer-visibility-9-1", 'true');
                document.querySelector('[class*="slidecarthq-overlay open"]').classList.remove('open');
                document.querySelector('[class*="slidecarthq "]').setAttribute("recover-drawer-visibility-9-2", 'true');
                document.querySelector('[class*="slidecarthq "]').classList.remove('open');
                window.RecoverDrawerCart9 = true;
            }

            if (window.EGW_THEME_NAME.indexOf("stiletto") > -1 && location.pathname.indexOf('/cart') === -1 && document.querySelector('[class="quick-cart__wrapper active"]') !== null) {
                document.querySelector('[class="quick-cart__wrapper active"]').setAttribute("recover-drawer-visibility-10", 'true');
                document.querySelector('[class="quick-cart__wrapper active"]').classList.remove('active');
                window.RecoverDrawerCart10 = true;
            }

            if (window.EGW_THEME_NAME.indexOf("fastlane") > -1 && location.pathname.indexOf('/cart') === -1) {
                document.querySelector('html').classList.remove('open_mobile_cart');
                window.RecoverDrawerCart11 = true;
            }

            if (document.getElementById('corner-cowi-open-wrapper') !== null) {
                document.getElementById('corner-cowi-open-wrapper').style.setProperty('z-index', '2147483646', 'important');
            }
        }
        else {
            if (window.EGW_THEME_NAME.indexOf("brooklyn") > -1 && typeof timber !== 'undefined' && window.effectiveAppsEGWSuccessfullyAttachedToATC !== true && location.pathname.indexOf('cart') === -1) {
                timber.RightDrawer.open();
            }
            else if (window.RecoverBodyJsDrawerCartNeeded === true) {
                document.body.setAttribute('class', document.body.getAttribute('class') + ' js-drawer-open-right');
                window.RecoverBodyJsDrawerCartNeeded = false;
            }

            if (window.RecoverHeadJsDrawerCartNeeded === true) {
                document.getElementsByTagName('html')[0].setAttribute('class', document.getElementsByTagName('html')[0].getAttribute('class') + ' js-drawer-open-right');
                window.RecoverHeadJsDrawerCartNeeded = false;
            }

            if (window.RecoverDrawerCartTabIndex !== undefined) {
                document.querySelector('[class*=mfp-wrap]').setAttribute("tabindex", window.RecoverDrawerCartTabIndex);
                window.RecoverDrawerCartTabIndex = undefined;
            }

            if (window.RecoverDrawerCartTabIndex2 !== undefined) {
                document.getElementById("CartDrawer").setAttribute("tabindex", window.RecoverDrawerCartTabIndex2);
                window.RecoverDrawerCartTabIndex2 = undefined;
            }

            if (window.RecoverDrawerCartTabIndex3 !== undefined) {
                document.querySelector("[data-section-id='cart-drawer']").setAttribute("tabindex", window.RecoverDrawerCartTabIndex3);
                window.RecoverDrawerCartTabIndex3 = undefined;
            }

            if (window.RecoverDrawerCart4 !== undefined) {
                document.querySelector("[recover-drawer-visibility-4='true']").classList.add('drawer--visible');
            }

            if (window.RecoverDrawerCart5 !== undefined) {
                document.querySelector("[recover-drawer-visibility-5='true']").setAttribute('data-sidebar-drawer-animation-state', 'open');
            }

            if (window.RecoverDrawerCart6 !== undefined) {
                setTimeout(function() {
                    document.querySelector('[class="cart-link"]').click();
                }, 750);
                window.RecoverDrawerCart6 = undefined;
            }

            if (window.RecoverDrawerCart7 !== undefined) {
                setTimeout(function() {
                    document.querySelector('[data-cart-drawer-open]').click();
                }, 250);
                window.RecoverDrawerCart7 = undefined;
            }

            if (window.RecoverDrawerCart8 !== undefined) {
                document.querySelector("body").classList.add('cart--is-visible');
                window.RecoverDrawerCart8 = undefined;
            }

            if (window.RecoverDrawerCart9 !== undefined) {
                document.querySelector('[recover-drawer-visibility-9-1="true"]').classList.add('open');
                document.querySelector('[recover-drawer-visibility-9-1="true"]').removeAttribute('recover-drawer-visibility-9-1');
                document.querySelector('[recover-drawer-visibility-9-2="true"]').classList.add('open');
                document.querySelector('[recover-drawer-visibility-9-2="true"]').removeAttribute('recover-drawer-visibility-9-2');
                window.RecoverDrawerCart9 = undefined;
            }

            if (window.RecoverDrawerCart10 !== undefined) {
                document.querySelector('[recover-drawer-visibility-10="true"]').classList.add('active');
                document.querySelector('[recover-drawer-visibility-10="true"]').removeAttribute('recover-drawer-visibility-10');
                window.RecoverDrawerCart10 = undefined;
            }

            if (window.RecoverDrawerCart11 !== undefined) {
                document.querySelector('html').classList.add('open_mobile_cart')
                window.RecoverDrawerCart11 = undefined;
            }
        }
    }
    catch (err) {

    }
}

// ---- cart options ----

function submitCartUpdates(giftOptions) {
    var attributesUpdates = {};
    var quantityUpdates = {};
    var quantityUpdatesItems = [];

    if ('False' === 'True') {
        if (giftOptions.hasOwnProperty('effectiveAppsEgwGiftMessage') && giftOptions['effectiveAppsEgwGiftMessage'] === true) {
            attributesUpdates['Gift Message'] = giftOptions['effectiveAppsEgwGiftMessageText'];
            if (parseFloat('0') > 0 && window.effectiveAppsSelectedGmVariantId.length > 1) {
                quantityUpdates[parseInt(window.effectiveAppsSelectedGmVariantId)] = 1;
                quantityUpdatesItems.push(parseInt(window.effectiveAppsSelectedGmVariantId));
            }
        }
        else {
            attributesUpdates['Gift Message'] = null;
            if (window.effectiveAppsSelectedGmVariantId.length > 1) {
                quantityUpdates[parseInt(window.effectiveAppsSelectedGmVariantId)] = 0;
            }
        }
    }

    if ('False' === 'True') {
        if (giftOptions.hasOwnProperty('effectiveAppsEgwGiftReceipt') && giftOptions['effectiveAppsEgwGiftReceipt'] === true) {
            attributesUpdates['Gift Receipt'] = "✓";
        }
        else {
            attributesUpdates['Gift Receipt'] = null;
        }
    }

    if ('True' === 'True') {
        if (giftOptions.hasOwnProperty('effectiveAppsEgwGiftWrap') && giftOptions['effectiveAppsEgwGiftWrap'] === true) {
            if (window.effectiveAppsGwOptions["gift_wrap_options"].length > 1) {
                for (var giftOption of window.effectiveAppsGwOptions["gift_wrap_options"]) {
                    if (giftOptions["effectiveAppsEgwGiftWrapSelectedVariantId"] === giftOption["gw_variant_id"]) {
                        attributesUpdates[giftOption["gw_title"]] = "✓";
                        quantityUpdates[parseInt(giftOptions["effectiveAppsEgwGiftWrapSelectedVariantId"])] = 1;
                        quantityUpdatesItems.push(parseInt(giftOptions["effectiveAppsEgwGiftWrapSelectedVariantId"]));
                    }
                    else {
                        quantityUpdates[parseInt(giftOption["gw_variant_id"])] = 0;
                        attributesUpdates[giftOption["gw_title"]] = null;
                    }
                }
            }
            else {
                attributesUpdates[window.effectiveAppsGwLabelText] = "✓";
                quantityUpdates[parseInt(window.effectiveAppsSelectedGwVariantId)] = 1;
                quantityUpdatesItems.push(parseInt(window.effectiveAppsSelectedGwVariantId));
            }
        }
        else {
            attributesUpdates[window.effectiveAppsGwLabelText] = null;
            if (window.effectiveAppsGwOptions["gift_wrap_options"].length > 1) {
                for (var giftOption of window.effectiveAppsGwOptions["gift_wrap_options"]) {
                    attributesUpdates[giftOption["gw_title"]] = null;
                    quantityUpdates[parseInt(giftOption["gw_variant_id"])] = 0;
                }
            }
            else {
                attributesUpdates[window.effectiveAppsGwLabelText] = null;
                if ('40403376930886'.length > 1) {
                    quantityUpdates[parseInt(window.effectiveAppsSelectedGwVariantId)] = 0;
                }
            }
        }
    }

    if ("1" === "1") {
        jQuery.post('/cart/update.js',
                    {
                        attributes: attributesUpdates,
                        updates: quantityUpdates
                    }, null, "json"
        ).always(function() {
            var didAttributesApplied = true;
            var itemsList = [];
            jQuery.getJSON('/cart.js').done(function(data) {
                for (var attribute in attributesUpdates) {
                    if (attributesUpdates[attribute] !== null && data['attributes'][attribute] !== attributesUpdates[attribute]) {
                        didAttributesApplied = false;
                        break;
                    }
                }
                for (var item in data['items']) {
                    itemsList.push(data['items'][item]['id']);
                }

                if (didAttributesApplied === false || arraysComparison(quantityUpdatesItems, itemsList) === false) {
                    submitCartUpdates(giftOptions)
                }
                else {
                    if (window.effectiveAppsEGWGWLastAction !== undefined || (window.effectiveAppsEGWGMLastAction !== undefined && parseFloat('0') > 0) ) {
                        if (1 === 1) {
                            location.reload();
                        };
                    }
                }
            });
        });
    }
    else {
        let giftOptionsNote = generateCartGiftOptionsNote(giftOptions);
        jQuery.post('/cart/update.js',
                    {
                        note: giftOptionsNote,
                        updates: quantityUpdates
                    }, null, "json"
        ).always(function() {
            var didNoteApplied = true;
            var itemsList = [];
            jQuery.getJSON('/cart.js').done(function(data) {
                if (data['note'].indexOf(giftOptionsNote) === -1) {
                    didNoteApplied = false;
                }

                for (var item in data['items']) {
                    itemsList.push(data['items'][item]['id']);
                }

                if (didNoteApplied === false || arraysComparison(quantityUpdatesItems, itemsList) === false) {
                    submitCartUpdates(giftOptions)
                }
                else {
                    if (window.effectiveAppsEGWGWLastAction !== undefined || (window.effectiveAppsEGWGMLastAction !== undefined && parseFloat('0') > 0) ) {
                        if (1 === 1) {
                            location.reload();
                        }
                    }
                }
            });
        });
    }
}

function arraysComparison(arr, arr2){
  return arr.every(i => arr2.includes(i));
}

function encode(r) {
  return r.replace(/[\x26\x0A\x3c\x3e\x22\x27]/g, function(r) {
    return "&#" + r.charCodeAt(0) + ";";
  });
}

/// ------
function createGiftModal() {
	window.effectiveAppsEGWModal = new tingle.modal({
		footer: !window.EFFECTIVE_APPS_EGW_IS_MOBILE,
		closeMethods: ['overlay', 'button', 'escape'],
		closeLabel: `Save gift options`,
		cssClass: ['egw-modal'],
		onOpen: function () {
			toggleSideCartOverlay('openModal');
			modalOnLoad();
		},
		beforeClose: function() {
		    try {
                validateAndSaveGiftOptions();
                return true;
            }
            catch (err) {
                return false;
            }
        },
		onClose: function () {
			toggleSideCartOverlay('closeModal');
			if (window.effectiveAppsEGWGMStatus === true || window.effectiveAppsEGWGRStatus === true || window.effectiveAppsEGWGWStatus === true) {
				if (document.getElementById('effectiveAppsEGW') !== null) {
				    document.getElementById('effectiveAppsEGW').checked = true;
                }
                if (document.getElementById('effectiveAppsEGWATC') !== null) {
				    document.getElementById('effectiveAppsEGWATC').checked = true;
                }

                if (window.EFFECTIVE_APPS_EGW_CART_CHANGE_LISTENER === undefined) {
                    window.EFFECTIVE_APPS_EGW_CART_CHANGE_LISTENER = setInterval(EGW_listenForCartChanges, 1000);
                }
			} else {
				if (document.getElementById('effectiveAppsEGW') !== null) {
				    document.getElementById('effectiveAppsEGW').checked = false;
                }
                if (document.getElementById('effectiveAppsEGWATC') !== null) {
				    document.getElementById('effectiveAppsEGWATC').checked = false;
                }
			}
		}
	});
	var giftMessageHtml = `<div class="effective-apps-egw-modal minispacing"> <div class="sectioned leftlabel bold declaration"> <div class="minispacing"><label for="effectiveAppsEgwGiftMessage" class="bold"><input class="effective-apps-egw-cb" id="effectiveAppsEgwGiftMessage" name="effectiveAppsEgwGiftMessage" style="-webkit-appearance: checkbox; display: inline-block;" type="checkbox"> <span class="checkbox-text"> <span id="effectiveAppsEgwGiftMessageLbl">Gift Message</span><span class="smallsize shade-color">Add a personal note to the recipient of this gift</span> </span></label> </div></div></div><textarea aria-label="Gift Message" tabindex="-1" onclick="if (false === document.getElementById('effectiveAppsEgwGiftMessage').checked) { document.getElementById('effectiveAppsEgwGiftMessage').checked = true; }" onkeyup="updateRemainingCharacters();" class="effective-apps-gm-textarea" maxlength="250" name="effectiveAppsEgwGiftMessageText" id="effectiveAppsEgwGiftMessageText" placeholder="Add a personal gift message" rows="4"></textarea> <div id="effectiveAppsEgwGiftMessageRC" name="effectiveAppsEgwGiftMessageRC" class="sectionedcolumn sectioned righttext endingspan smallsize shade-color">Remaining characters: 250 </div>`;
	var giftWrapHtml = `<label for="effectiveAppsEgwGiftWrap" class="bold"><input class="effective-apps-egw-cb" id="effectiveAppsEgwGiftWrap" name="effectiveAppsEgwGiftWrap" style="-webkit-appearance: checkbox; display: inline-block;" type="checkbox"> <span class="checkbox-text"> <span id="effectiveAppsEgwGiftWrapLbl">Gift Wrap</span> - $4.00<span class="smallsize shade-color">Add a beautiful gift wrap to surprise and delight.</span> </span></label>`;
    if (window.effectiveAppsGwOptions["gift_wrap_options"].length > 1) {
        var giftOptionsHtml = "";
        var isFirst = true;
        var firstGwTitle = "", firstGwPrice = "", firstGwExp = "";
        for (var giftOption of window.effectiveAppsGwOptions["gift_wrap_options"]) {
            var gw_title = giftOption["gw_title"];
            var gw_price = giftOption["gw_price"];
            var gw_explanation = giftOption["gw_explanation"];
            var gw_variant_id = giftOption["gw_variant_id"];
            var gw_img = giftOption["gw_img"];
            var firstSelected = "";
            if (isFirst === true) {
                firstGwTitle = gw_title;
                firstGwPrice = " - $" + gw_price.toString();
                if (parseFloat(gw_price) === 0) {
                    firstGwPrice = "";
                }
                firstGwExp = gw_explanation;
                var firstSelected = "checked";
                isFirst = false;
            }

            if (gw_explanation.indexOf('<br') === -1) {
                gw_explanation = encode(gw_explanation);
            }

            giftOptionsHtml += '<label gw-variant-id='+ gw_variant_id +' style="display: inline-block !important;" class="radio-img"><input ' + firstSelected + ' type="radio" onclick="selectGwOption(`' + gw_variant_id + '`, `' + gw_img + '`, `' + encode(gw_title) +'`, `' + gw_price + '`, `'+ gw_explanation + '`)" name="effective_apps_gw_option" value="' + gw_variant_id + '"><div class="effective-apps-egw-gw-swatch" style="background-image: url(' + gw_img + ')"></div></label>';
        }

        giftWrapHtml = '<label for="effectiveAppsEgwGiftWrap" class="bold"><input class="effective-apps-egw-cb" id="effectiveAppsEgwGiftWrap" name="effectiveAppsEgwGiftWrap" style="-webkit-appearance: checkbox; display: inline-block;" type="checkbox"> <span class="checkbox-text"> <span id="effectiveAppsEgwGiftWrapLbl">' + firstGwTitle + '</span><span id="effectiveAppsEgwGiftWrapPrice">' + firstGwPrice + '</span><span class="smallsize shade-color" id="effectiveAppsEgwGiftWrapExp">' + firstGwExp + '</span><div>' + giftOptionsHtml+ '</div></span></label>';
    }
	var giftReceiptHtml = `<span class="bold declaration"> <div class="minispacing"><label for="effectiveAppsEgwGiftReceipt" class="bold"><input class="effective-apps-egw-cb" id="effectiveAppsEgwGiftReceipt" name="effectiveAppsEgwGiftReceipt" style="-webkit-appearance: checkbox; display: inline-block;" type="checkbox"> <span class="checkbox-text"> <span id="effectiveAppsEgwGiftReceiptLbl">Gift Receipt</span><span class="smallsize shade-color">Hide prices from the receipt</span> </span></label> </div></span>`;
	var sectioned = "sectioned";
	var sectionedColumn = "sectionedcolumn";
	var sectionedStyle = "";
	if (window.EFFECTIVE_APPS_EGW_IS_MOBILE === true) {
		sectioned = "";
		sectionedColumn = "";
		sectionedStyle = 'style="padding-top: 15px;"';
	}
	var firstSection = '<div class="' + sectioned + '"><div class="innersection topminispacing">{0}</div></div>';
	var secondSection = '<div ' + sectionedStyle + ' class="' + sectionedColumn + ' ' + sectioned + ' endingspan"> <div class="innersection minispacing topminispacing"> <span class="bold declaration"> <div class="minispacing"> {0} <div> <img alt="Modal image" id="effectiveAppsEgwModalImage" width="333" height="190" src="https://cdn.shopify.com/s/files/1/0448/4143/5300/t/3/assets/2.png" style="object-fit: contain;"> </div></div></span> </div></div>';

	if ('False' === 'True') {
		firstSection = firstSection.format(giftMessageHtml);
		if ('False' === 'True' && 'True' === 'False') {
			secondSection = secondSection.format(giftReceiptHtml);
		} else if ('False' === 'False' && 'True' === 'True') {
			secondSection = secondSection.format(giftWrapHtml);
		} else if ('False' === 'True' && 'True' === 'True') {
			secondSection = secondSection.format(giftReceiptHtml + giftWrapHtml);
		} else {
			secondSection = secondSection.format('');
		}
	} else if ('True' === 'True' && 'False' === 'False') {
		firstSection = firstSection.format(giftWrapHtml);
		secondSection = secondSection.format('');
	} else if ('True' === 'False' && 'False' === 'True') {
		firstSection = firstSection.format(giftReceiptHtml);
		secondSection = secondSection.format('');
	} else if ('True' === 'True' && 'False' === 'True') {
		firstSection = firstSection.format(giftReceiptHtml + giftWrapHtml);
		secondSection = secondSection.format('');
	}

	var modalContentHtml = `<style>.effective-apps-gm-textarea{background-color: white; -moz-user-select: text; -webkit-user-select: text; -ms-user-select: text;background-color: "white"; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; font-family: Arial, sans-serif; font-size: 13px; border: 1px solid; -webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, .5), 0 1px 0 rgba(0, 0, 0, .07) inset; -moz-box-shadow: 0 1px 0 rgba(255, 255, 255, .5), 0 1px 0 rgba(0, 0, 0, .07) inset; box-shadow: 0 1px 0 rgba(255, 255, 255, .5), 0 1px 0 rgba(0, 0, 0, .07) inset; outline: 0; outline-width: 0; height: auto; width: 100%; padding: 5px 7px; resize: vertical}.effective-apps-gm-textarea:focus{border-color: blue; -webkit-box-shadow: 0 0 3px rgba(228, 121, 17, .5), 0 1px 0 rgba(0, 0, 0, .07) inset; -moz-box-shadow: 0 0 3px rgba(228, 121, 17, .5), 0 1px 0 rgba(0, 0, 0, .07) inset; box-shadow: 0 0 3px rgba(228, 121, 17, .5), 0 1px 0 rgba(0, 0, 0, .07) inset}.effective-apps-gm-textarea:-webkit-input-placeholder{color: #888}.effective-apps-gm-textarea:-moz-placeholder{color: #888}.effective-apps-gm-textarea:-ms-input-placeholder{color: #888}.effective-apps-egw-cb{margin: 0; padding: 0; height: 13px; width: 13px; vertical-align: bottom; position: relative; top: 0; overflow: hidden}.effective-apps-egw-modal{width: 100%; font-size: 13px; line-height: 19px; color: #333; font-family: Arial, sans-serif; padding: 0; margin: 0; background-color="#000000"}.effective-apps-egw-modal:after, .effective-apps-egw-modal:before{display: table; content: ""; line-height: 0; font-size: 0}.effective-apps-egw-modal:after{clear: both}.effective-apps-egw-modal{float: right!important; margin-left: 2.53165%!important; margin-right: 0!important}.effective-apps-egw-modal .sectioned, .x-ws .effective-apps-egw-modal .x-ws-span6{width: 48.68608%}td.sectioned, th.sectioned{width: 52.43%; float: none!important}.effective-apps-egw-modal .sectioned div.sectioned{width: auto !important;}td.sectioned{float: none; margin-right: 0}div.sectioned{margin-right: 2.53165%; float: left; _float: left!important; min-height: 1px; overflow: visible}.innersection{margin-bottom: 22px}.innersection:last-child{margin-bottom: 0}.topminispacing{margin-top: 6px!important}hr.topminispacing{padding-top: 6px; margin-top: 0!important}.bold{font-weight: 700!important}.sectionedcolumn.endingspan{margin-right: 0; float: right}.smallsize{font-size: 11px; line-height: 1.465}.shade-color{color: #888888; display: block; }.minispacing{margin-bottom: 6px!important}.leftlabel{text-align: left!important}.sectionedcolumn.endingspan{margin-right: 0; float: right}.bold{font-weight: 700!important}.righttext{text-align: right!important}.declaration form{margin-bottom: 9px}.declaration:last-child form:last-child{margin-bottom: 0}label .checkbox-text{position: relative; top: 3px; left: 5px}@-moz-document url-prefix(){label .checkbox-text{top: 4px}}.smallsize{font-size: 12px; line-height: 1.5}</style><h2>Gift Options</h2> <div class="effective-apps-egw-modal"> ` + firstSection + secondSection + '</div>';
	window.effectiveAppsEGWModal.setContent(modalContentHtml);
	if (false === window.EFFECTIVE_APPS_EGW_IS_MOBILE) {
        window.effectiveAppsEGWModal.addFooterBtn(`Save gift options`, 'tingle-btn tingle-btn--primary egwSaveGiftOptionsBtn', function () {
            window.effectiveAppsEGWModal.close();
        });
	}
}

function selectGwOption(gwVariantId, gwImg, gwTitle, gwPrice, gwExplanation) {
    document.getElementById('effectiveAppsEgwGiftWrapLbl').innerText = gwTitle;
    if (gwExplanation.indexOf('<br') > -1) {
        document.getElementById('effectiveAppsEgwGiftWrapExp').innerHTML = gwExplanation;
    }
    else {
        document.getElementById('effectiveAppsEgwGiftWrapExp').innerText = gwExplanation;
    }

    if (gwImg !== '' && gwImg !== undefined) {
        document.getElementById('effectiveAppsEgwModalImage').src = gwImg;
    }
    document.getElementById('effectiveAppsEgwGiftWrap').checked = true;
    if (parseFloat(gwPrice) === 0) {
        gwPrice = "";
    }
    else {
        gwPrice = " - $" + gwPrice.toString();
    }

    document.getElementById('effectiveAppsEgwGiftWrapPrice').innerText = gwPrice;
}

function EGW_loadScript(url, success) {
	var script = document.createElement('script');
	script.src = url;
	var head = document.getElementsByTagName('head')[0],
		done = false;
	head.appendChild(script);
	script.onload = script.onreadystatechange = function () {
		if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
			done = true;
			success();
			script.onload = script.onreadystatechange = null;
			head.removeChild(script);
		}
	};
}

function EGW_getShopifyThemeName() {
    try {
        if (window.EGW_THEME_OVERRIDE !== undefined) {
            return window.EGW_THEME_OVERRIDE;
        }

        if (window.BOOMR !== undefined && window.BOOMR.themeName !== undefined && window.BOOMR.themeName.length > 0) {
            return window.BOOMR.themeName.toLowerCase();
        }
        else {
            return Shopify.theme.name.toLowerCase();
        }
    }
    catch (err) {
        return "";
    }
}

function EGW_mainLoader() {
    if (window.EGW_THEME_NAME_OVERRIDE === undefined) {
        window.EGW_THEME_NAME = EGW_getShopifyThemeName();
    }
    else {
        window.EGW_THEME_NAME = window.EGW_THEME_NAME_OVERRIDE;
    }

    if (window.EGW_THEME_NAME.indexOf('prestige') > -1 || window.EGW_THEME_NAME.indexOf('fashionopolism') > -1 || window.EGW_THEME_NAME.indexOf('mr parker') > -1) {
        setTimeout(EGW_main, 1200);
    }
    else {
        EGW_main();
    }
}

function EGW_main() {
    console.log('%c------ Super Gift Options by Effective Apps is Initializing (Pop Up) ------', 'color: cyan');
    console.log('%c------ Contact us at support@effectify.co for help and questions about the app ------', 'color: cyan');
	if (window.EGW_SCRIPT_INJECTED === undefined) {
	    window.EGW_SCRIPT_INJECTED = true;
		if (typeof jQuery === 'undefined') {
            EGW_loadScript('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js',
                function () {
                    if (typeof tingle === 'undefined') {
                        EGW_loadScript("https://cdnjs.cloudflare.com/ajax/libs/tingle/0.15.1/tingle.min.js", EGW_runLogic);
                    }
                    else {
                        EGW_runLogic();
                    }
                });
        }
        else {
            if (typeof tingle === 'undefined') {
                EGW_loadScript("https://cdnjs.cloudflare.com/ajax/libs/tingle/0.15.1/tingle.min.js", EGW_runLogic);
            }
            else {
                EGW_runLogic();
            }
        }
	}
}

EGW_mainLoader();