if (window.addEventListener) { window.addEventListener("message", onMessage, false); } else if (window.attachEvent) { window.attachEvent("onmessage", onMessage, false); } function onMessage(event) { var allowedOrigins = [ 'https://www.developer003-cooksongold.com', 'https://www.developer003-cookson-clal.com', 'https://www.cooksongold.com', 'https://www.cookson-clal.com', 'https://www.cookson-clal.es', 'https://www.cookson-clal.de', 'https://www.cookson-clal.it', 'https://www.cookson-clal.com', 'https://www.cooksondental.com', 'https://amdevuiapp.azurewebsites.net', 'https://amportal.cooksongold.com' ]; if (!allowedOrigins.includes(event.origin)){ console.log("domain not allowed:" + event.origin); return; } //console.log("parent message handler2"); //console.log("event.origin:" + event.origin); //console.log("*event.data.payload:" + JSON.stringify(event.data.payload)); //console.log("*event:" + event); //console.log("*event.data:", event.data); var data = event.data; if (data.func == "parent_add_to_basket") { add_to_basket(data.payload); } if (data.func == "parent_fetch_price") { fetch_price(data.payload); } if (data.func == "parent_get_auth_token") { get_auth_token(); } if (data.func == "parent_bulk_fetch_price") { parent_bulk_fetch_price(data.payload); } if (data.func == "ga4_click") { window.dataLayer.push({ 'event' : 'click', 'eventCategory' : 'app3d', 'eventLabel': data.message, }); console.log('send ga4 message:' + data.message + ", v1.5" + ",payload:none"); } if(data.func == "parent_add_to_basket"){ itemprice = data.price; if(quantity > 1){ itemprice = (data.price / data.quantity).toFixed(2); } dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: 'add_to_cart', eventCategory : 'app3d', ecommerce: { currency: data.currency, value: data.price, items: [ { index: 1, item_id: data.product_code, price: itemprice, quantity: data.quantity } ] } }); console.log('send ga4 message:' + data.func + ', v1.7, product_id:' + data.product_code); } } function parent_bulk_fetch_price(in_payload){ console.log("::::::::1:::::::"); console.log("::::::::2:::::::" + in_payload); post_data = ""; in_payload.forEach(function(item){ post_data += "product_prefix[]=" + item.product_prefix + "&"; post_data += "product_suffix[]=" + item.product_suffix + "&"; //post_data += "product_prefix[]=" + "AAA&"; //post_data += "product_suffix[]=" + "600&"; post_data += "material_id[]=" + item.material_id + "&"; post_data += "weight[]=" + item.weight + "&"; post_data += "quantity[]=" + "1&"; //post_data += "quantity[]=" + item.quantity + "&"; }); console.log(post_data); $.ajax({ url: "/AMAccess_bulk_fetch_price.jsp", type: "POST", data: post_data, success: function (data) { new_data = JSON.parse(data); var message = { 'func': 'child_bulk_fetch_price', 'payload': new_data }; iframe = document.getElementById("childframe"); iframe.contentWindow.postMessage(message, "https://amportal.cooksongold.com"); console.log("::::::::3:::::::"); }, fail: function (data) { } }); return false; } function add_to_basket(payload) { console.log("add_to_basket"); $.ajax({ url: "/AMAccess_add_to_basket.jsp", type: "POST", data: payload }) .done(function(data) { new_data = JSON.parse(data); var message = { func: "child_add_to_basket", payload: new_data }; iframe = document.getElementById("childframe"); iframe.contentWindow.postMessage(message, "https://amportal.cooksongold.com"); var temp = $.parseJSON(data); $(".my_bkt_qty").html(temp.basketlines); $(".my_bkt_link").attr("href", "/your_basket.jsp"); $("#bkt_qty").html(temp.basketlines); console.log("basketlines:" + temp.basketlines); }) .fail(function() { var message = { "func": "child_add_to_basket", "payload": { "status": "failed", "error_code": "102", "error_desciption": "There was a communication failure." } }; iframe = document.getElementById("childframe"); iframe.contentWindow.postMessage(message, "https://amportal.cooksongold.com"); }); } function fetch_price(payload) { console.log("fetch_price"); $.ajax({ url: "/AMAccess_fetch_price.jsp", type: "POST", data: payload }) .done(function(data) { var message = { func: "child_fetch_price", payload: data }; iframe = document.getElementById("childframe"); iframe.contentWindow.postMessage(message, "https://amportal.cooksongold.com"); }) .fail(function() { var message = { "func": "child_fetch_price", "payload": { "status": "failed", "error_code": "102", "error_desciption": "There was a communication failure." } }; iframe = document.getElementById("childframe"); iframe.contentWindow.postMessage(message, "https://amportal.cooksongold.com"); }); } function get_auth_token() { console.log("get_auth_token_1"); $.ajax({ url: "/AMAccess_federated_auth_v2.jsp", context: document.body, type: "GET" }).done(function(data) { console.log("get_auth_token_2:" + data); var message = { 'func': 'child_new_auth_token', 'payload': { 'JWTToken': "" + data } }; iframe = document.getElementById("childframe"); iframe.contentWindow.postMessage(message, "https://amportal.cooksongold.com"); console.log("get_auth_token_3 message to child"); }).fail(function() { var message = { "func": "child_new_auth_token", "payload": { "status": "failed", "error_code": "103", "error_desciption": "There was a communication failure." } }; iframe = document.getElementById("childframe"); iframe.contentWindow.postMessage(message, "https://amportal.cooksongold.com"); }); } function sub_toggle_vat(){ console.log("vat2"); if($("#vat_toggle_x").prop("checked")){ console.log("vat OFFFF1"); //$("#vat_toggle_x").removeAttr("checked"); //$("#vat_toggle_x").prop('checked', true); $("#vat_toggle_x").attr('checked','checked'); console.log("vat OFFFF2"); }else{ console.log("vat on1"); $("#vat_toggle_x").removeAttr("checked"); console.log("vat on2"); } console.log("vat3"); console.log("--------------"); sub2_toggle_vat(""); } function sub2_toggle_vat(extraparam){ console.log("vat4"); $.ajax({ url: '/vattoggle.jsp?format=ajax' + extraparam, context: document.body, type: "GET" }).done(function(data) { console.log("vat4.1"); var json = $.parseJSON(data); console.log("vat4.2"); if(json.vat == 'on'){ var message = { 'func': 'child_vat', 'payload': { 'vatOn': true } }; iframe = document.getElementById('childframe'); iframe.contentWindow.postMessage(message, "https://amportal.cooksongold.com"); }else{ var message = { 'func': 'child_vat', 'payload': { 'vatOn': false } }; iframe = document.getElementById('childframe'); iframe.contentWindow.postMessage(message, "https://amportal.cooksongold.com"); } }); } function sub_change_currency(mycurrency){ console.log('change_currency2'); str = "cur=" + mycurrency + "&format=ajax"; $.ajax({ url: '/currency.jsp', context: document.body, type: "GET", data : str }).done(function(data) { var json = $.parseJSON(data.trim()); if(json.status == 'ok'){ $('#current_currency').removeClass(); $('#current_currency').addClass('flag_' + json.currency); $('#current_currency').html(json.currencysymb + ' ' + json.currency); var message = { 'func': 'child_currency', 'payload': { 'currencyCode': "" + json.currency, 'currencySymbol': "" + json.currencysymb } }; iframe = document.getElementById('childframe'); iframe.contentWindow.postMessage(message, "https://amportal.cooksongold.com"); } }); } function am_is_ie(){ user_agent = navigator.userAgent; return(user_agent.indexOf("MSIE ") > -1 || user_agent.indexOf("Trident/") > -1); } function am_check_ie(){ if(am_is_ie()){ alert('null'); return false; }else{ return true; } }