$(document).ready(function() { setCartRow(); setDeleteRow(); $("#dialog").hide(); $("form#stockGroupForm").submit(function() { addToCart(); return false; }); // if the response did not have the cart rows, then dont show the // popup dialog, we use this to set the state for the row existance var jsonResponseValid = 0; function addToCart() { if($('#size').val() == "" || $('#colour :selected').text() == "Then Select Colour"|| $('#colour :selected').text() == "Then SELECT Colour"){ window.alert("Please ensure both size and colour options are selected"); return false; } else $.ajax({ type: 'post', url: 'index_xml.cgi' + $('#stockGroupForm').attr('action'), dataType: 'json', data: { stockGroupCode: $('#stockGroupCode').val(), stockCode: $('#stockCode').val(), size: $('#size').val(), colour: $('#colour').val(), quantity: $('#quantity').val() }, success: function(json) { formatMiniCart(json); }, error: function() { redirect(); }, complete: function() { notifyBubble(); } }); $("#cart_button_01").attr("disabled", "disabled"); // disable submit to avoid repeated presses return false; } function removeFromCart(itemUrl) { $.ajax({ type: 'get', url: 'index_xml.cgi' + itemUrl, dataType: 'json', data: {}, success: function(json) { formatMiniCart(json); }, error: function() { redirect(); } }); return false; } function redirect() { window.location=$('#viewCartBtn').attr("href"); } function formatMiniCart(json) { var newRows = ''; var cart_num_items = ''; $.each(json.cart_details, function(index, row) { jsonResponseValid = 1; // we have rows var newRow = "
" + row.quantity; newRow += " x " + row.mini_description.item; newRow += "X
"; var popupRow = "" + row.mini_description.code; popupRow += " " + row.mini_description.detail + "
"; popupRow += "" + "" + row.quantity; popupRow += " - " + row.attribs.size; popupRow += " - " + row.attribs.colour + "
Items: ' + json.cart_num_items + '
'); $('#miniCartSummary').append('Total: ' + json.cart_total + '
'); setCartRow(); setDeleteRow() } function setCartRow(){ $(".cartRow").each(function() { $(this).mouseover(function() { $(this).children(".deleteRow").show(); $(this).next(".popupRow").show(); $(this).css("background-color", "#7593DF"); }); $(this).mouseout(function() { $(this).children(".deleteRow").hide(); $(this).next(".popupRow").hide(); $(this).css("background-color", "#A4B8EA"); }); }); } function setDeleteRow(){ $(".deleteRow") .hide() .click(function() { removeFromCart($(this).attr('href')); return false; }); } function notifyBubble() { if (jsonResponseValid == 1) { var myItem = $('#productRegion h1').text(); // store name of item added var mySize = $('#size').val(); // store size var myColourText = $('#colour :selected').text(); //get text of selected option myColourText = myColourText.split(" "); // split option text to colour and value at var myPrice = myColourText.pop(); // store price value myPrice = myPrice.replace('£',''); myPrice = myPrice.replace('£',''); myPrice = myPrice.replace('£',''); var myColour = myColourText.join(' '); // store colour //var myQty = $('#Xqty').val(); // store the quantity to be displayed - existing + selected var miniCartQty = $('#quantity').val() var myTotal = Math.round((myPrice * miniCartQty)*100)/100 ; $("#miniCart").fadeIn("fast"); // display the mini cart $("#miniCart .miniCartItem").text(myItem); $("#miniCart .miniCartSize strong").text(mySize); $("#miniCart .miniCartColour strong").text(myColour); $("#miniCart .miniCartPrice strong").text(myPrice); $("#miniCart .miniCartQty strong").text(miniCartQty); $("#miniCart .miniCartSubTotal strong").text(myTotal); document.getElementById("stockGroupForm").reset(); $("#cart_button_01").removeAttr("disabled"); // re-enable submit $("#dialog").dialog("open"); } else { redirect(); } } /* if ($("#dialog")){ */ $("#dialog").dialog({ title: "Your Cart Has Been Updated", width: 450, autoOpen: false, buttons: [{ text: "Continue Shopping", id: "continueBtn", click: function() { $(this).dialog("close"); window.location.reload(); } }, { text: "Go to Checkout", id: "checkoutBtn", click: function() { $(this).dialog("close"); window.location=$('#checkoutBtn').attr("href"); } }], modal: true }); /* }*/ });