var billing_same = true; var customer_shipping_country = null; var customer_shipping_region = null; task().ready(function(){ // Validation validate = new INZU_liveValidate('form_main'); if ( !logged_in ) { validate.fields.terms = {}; validate.fields.terms["isChecked"] = null; validate.fields.terms["END_check"] = null; } // Customer shipping details if ( shipping != "" ) { shipping = JSON.parse(shipping); customer_shipping_country = shipping.locations[0].country; customer_shipping_region = shipping.locations[0].region; if ( mail_check > 0 ) { document.getElementById('shipping_section').style.display = "block"; } } if ( customer_shipping_country && mail_check > 0 ) set_shipping_country(customer_shipping_country); // Customer billing details if ( customer_billing_region != "" ) { selectOptionValue(document.getElementById('billing_region'), customer_billing_region); document.getElementById('billing_same_element').style.display = "none"; } // Set customer shipping country task("#shipping_country").change(function(e){ set_shipping_country(e.value); }); // Select customer shipping address task(".shipping_location").change(function(e){ if ( shipping.locations[e.value].country ) { // Defualt address may be empty! select_shipping_address(e.value); } }); // Billing same as shipping task("#billing_same").change(function(e){ billing_shipping_same(); }); // Saved card task("#saved_card").change(function(e){ use_saved_card(e.checked); }); // Use temp address task("#shipping_location_temp").change(function(){ temporary_shipping_address(true); }); // Edit saved addresses task("#edit_addresses").click(function(e){ edit_addresses(); }); // Remove saved address task(".remove_address").click(function(e){ remove_address(e); }); // Create / save new customer shipping address if ( logged_in ) { toggle_create_addrress_validation(false); // No validation unless option is selected task('input[name="save_address"]').click(function(e) { save_create_shipping_address(e); }); } // Safe place authorisation task("#package_safe").click(function(e){ if ( e.checked === true) { task("#shipping_delivery_instructions").obj().parentElement.style.display = "none"; } else { task("#shipping_delivery_instructions").obj().parentElement.style.display = "block"; } }); }); // SET SHIPPING COUNTRY function set_shipping_country(val){ // Select value (for logged in users) selectOptionValue(document.getElementById('shipping_country'), val); // Set billing loc if ( billing_same ) { var element = document.getElementById('billing_region'); element.value = ( val == "UK" ) ? "GB" : val; } // Get shipping values var http = getHTTPObject(); http.open("GET", "/shipping/fetch/zone?country_code=" + val, true); http.onreadystatechange = function ajaxResponses() { if ( http.readyState == 4 ) { var country = http.responseText; var shippingError = 0; var tableID; var ship_to; var matchShipping; var newTotal = 0; var newTotal_units = 0; var products = document.getElementById('output_items').getElementsByTagName('tr'); for ( var i = 0; i < products.length; i++ ) { tableID = products[i].id; ship_to = tableID.split(","); if ( tableID != "null,digital" && tableID.search("s6") == -1 ) { matchShipping = tableID.search(country); if ( ( matchShipping == -1 && ship_to[0] != val ) || val == "" ) { shippingError++; var rows = products[i].getElementsByTagName("td"); for ( p = 0; p < rows.length; p++ ) { if ( rows[p].getAttribute("data-highlight") == "true" ) rows[p].className = "ship_fail"; } } else { var item_price = products[i].getAttribute("data-price").split("."); newTotal += parseInt(item_price[0]); newTotal_units += parseInt(item_price[1]); rows = products[i].getElementsByTagName("td"); for ( p = 0; p < rows.length; p++ ) { if ( rows[p].getAttribute("data-highlight") == "true" ) rows[p].className = "ship_ok"; } } } } if ( shippingError > 0 ) alert("The highlighted items do not ship to this location.\n\nThese items will be removed if you proceed."); // Shipping form if ( shippingError >= mail_check ) { document.getElementById('order_shipping').style.display = "none"; toggle_shipping_validation(false); } else { get_shipping_total(val, country); document.getElementById('order_shipping').className = "displayTable"; document.getElementById('shipping_section').style.display = "block"; get_region_list(val); toggle_shipping_validation(true); } newTotal_units = ("0" + newTotal_units).slice(-2); document.getElementById('total_price').innerHTML = newTotal + "." + newTotal_units; } } http.send(null); // Set hidden field as text value var shippping_country = document.getElementById("shipping_country"); for (var i = 0; i < shippping_country .options.length; i++) { if (shippping_country.options[i].value == val) { document.getElementById("shipping_country_full").value = shippping_country.options[i].text; } } } // GET SHIPPING TOTAL function get_shipping_total(country, val) { var http = getHTTPObject(); http.open("GET", "/shipping/fetch/cost?basket=" + basket + "&token=" + token + "&shipping_zone=" + val + "&loc=" + loc + "&country=" + country , true); http.onreadystatechange = function ajaxResponse() { if ( http.readyState == 4 ) { var shipping = http.responseText.split(","); if ( shipping == "0.00" ) { document.getElementById('shipping_currency').innerHTML = ""; shipping = "No charge"; } else { document.getElementById('shipping_currency').innerHTML = currency_symbol; } document.getElementById('shipping_amount').innerHTML = shipping; } } http.send(null); } // GET REGION LIST function get_region_list(country) { var http = getHTTPObject(); http.open("GET", "/shipping/fetch/regions?country=" + country, true); http.onreadystatechange = function ajaxResponse() { if ( http.readyState == 4 && http.response != "") { var result = JSON.parse(http.response); for (var i = region.options.length - 1 ; i >= 0 ; i--) { region.remove(i); } for (var i = result.regions.length - 1 ; i >= 0 ; i--) { var opt = document.createElement('option'); for (a in result.regions[i]) { opt.value = a; opt.innerHTML = result.regions[i][a]; } region.appendChild(opt); } if ( customer_shipping_region ) { selectOptionText(region, customer_shipping_region); } } } http.send(null); var zip_desc = "Postal code"; if ( country == "US" ) { zip_desc = "Zip code"; } else if ( country == "UK" ) { zip_desc = "Post code"; } else if ( country == "AU" ) { zip_desc = "Post code"; } else if ( country == "CA" ) { zip_desc = "Postal code"; } document.getElementById("zip_desc").innerHTML = zip_desc; region.disabled = false; } // SELECT SAVED ADDRESS function select_shipping_address(key){ document.getElementsByName('shipping_location')[key].checked = true; // Incase choice selected by a function if ( key !== "temp" ) { // Save address option was disabled for temp address document.getElementById('save_address').disabled = false; task("#shipping_country_element").obj().style.display = "none"; } // Hide create address field / deselect create address option document.getElementById('save_address_new').checked = false; document.getElementById('create_address').style.display = "none"; if ( typeof shipping === "object" ) { for ( value in shipping.locations[key] ) { if ( value != "title" ) { if ( value == "region" ) { customer_shipping_region = shipping.locations[key][value]; selectOptionText(task("#region" ).obj(), customer_shipping_region); } else if ( value == "country" ) { customer_shipping_country = shipping.locations[key][value]; set_shipping_country(customer_shipping_country); } else { task("#shipping_" + value).obj().value = shipping.locations[key][value]; } } } } } // SHIPPING ADDRESS TEMP function temporary_shipping_address(){ var save_options = document.getElementsByName('save_address'); for (var i = 0; i < save_options.length; i++) { save_options[i].checked = false; } document.getElementById('save_address').disabled = true; document.getElementById('create_address').style.display = "none"; task("#shipping_country_element").obj().style.display = "block"; toggle_create_addrress_validation(false); } // EDIT ADDRESSES function edit_addresses(){ task(".removeable_address").classToggle('remove_address_active'); task(".remove_address").showToggle('flex'); } // REMOVE ADDRESSES function remove_address(e){ let key = e.dataset.key; document.getElementById('removeable_address_' + key).style.display = 'none'; document.getElementById('remove_address_' + key).value = 'true'; select_shipping_address(0); } // SAVE/CREATE SHIPPING ADDRESS function save_create_shipping_address(e){ if ( e.id == "save_address_new") { task("#create_address").obj().style.display = "block"; task("#shipping_country_element").obj().style.display = "block"; var shipping_locations = document.getElementsByName('shipping_location'); for (var i = 0; i < shipping_locations.length; i++) { shipping_locations[i].checked = false; } toggle_create_addrress_validation(true); } else { task("#create_address").obj().style.display = "none"; task("#shipping_country_element").obj().style.display = "none"; toggle_create_addrress_validation(false); } } // TOGGLE CREATE ADDRESS VALIDATION function toggle_create_addrress_validation(state) { if ( state ) { validate.fields.shipping_title = {}; validate.fields.shipping_title["emptyCheck"] = "Address title required"; validate.fields.shipping_title["END_check"] = null; } else { validate.fields.shipping_title = {}; validate.fields.shipping_title["END_check"] = null; } } // TOGGLE SHIPPING VALIDATION function toggle_shipping_validation(required) { if (required) { validate.ignore = []; } else { validate.ignore.push('shipping_country'); } } // BILLING SHIPPING SAME function billing_shipping_same(){ billing_same = false; document.getElementById('billing_same_element').style.display = "none"; document.getElementById('billing_region_element').style.display = "block"; } // SAVED CARD function use_saved_card(checked){ if ( digital_check > 0 ) { if ( !checked ) { document.getElementById('billing_region_element').style.display = "block"; document.getElementById('billing_region').options[0].selected = true; } else { document.getElementById('billing_region_element').style.display = "none"; selectOptionValue(document.getElementById('billing_region'), customer_billing_region); } } } // VALIDATE FORM function form_validate(required) { if ( mail_check < 1 ) toggle_shipping_validation(false); validate.submit() }