Advertisement
DatStorm

Untitled

Mar 5th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var isLoggedIn = false;
  2. var searchBoolean = false;
  3. //Run this function when we have loaded the HTML document
  4. window.onload = function () {
  5.     //Request items from the server. The server expects no request body, so we set it to null
  6.  
  7.  
  8.     //This code is called when the body element has been loaded and the application starts
  9.     updateStockList();
  10.  
  11.  
  12.     /**
  13.      * Laver en ny bruger og disabler knappen så du ikke kan lave flere brugere
  14.      * Du kan derefter logge in.
  15.      */
  16.     $("#create-customer-submit-button").click(function () {
  17.         var login = document.getElementById("login-button");
  18.         var create = document.getElementById("create-customer-submit-button");
  19.         createCustomer();
  20.         create.disabled = true;
  21.         login.disabled = false;
  22.  
  23.     });
  24.  
  25.  
  26.     /**
  27.      * Logger dig ind og fjerner div efter.
  28.      */
  29.     $("#login-button").on("click", function () {
  30.         loginCustomer();
  31.         makeLoginDivDisappear();
  32.  
  33.     });
  34.  
  35.  
  36.     /**
  37.      * Viser din kurv ved tryk og hvis du trykker på den igen slider den up.
  38.      */
  39.     $('a#show-quick-cart').click(function () {
  40.  
  41.         if ($('div#cart').css("display") == 'none') {
  42.             $('div#cart').slideDown(500);
  43.         } else {
  44.             $('div#cart').slideUp(500);
  45.             $("#buy-items-message").text("");
  46.         }
  47.         return false;
  48.     });
  49.  
  50.  
  51.     /**
  52.      * Får login div frem og hvis du trykker på den igen forsvinder den.
  53.      */
  54.     $('a#login').click(function () {
  55.  
  56.         if ($('div.login-div').css("display") == 'none') {
  57.             $('div.login-div').slideDown(500);
  58.         } else {
  59.             $('div.login-div').slideUp(500);
  60.         }
  61.         return false;
  62.     });
  63.  
  64.     //ONLOAD FUNCTIONS
  65.     buyItemsInCart();
  66.     makeSearchEnter();
  67.     myOrdersLogInFirst();
  68.     goHome();
  69.     hideFormerBought();
  70. };
  71.  
  72.  
  73. /**
  74.  * Får login div til at slideup når musen forlader div'en.
  75.  */
  76. function makeLoginDivDisappear() {
  77.     $('div.login-div').mouseleave(function () {
  78.         $('div.login-div').slideUp(500);
  79.     });
  80. }
  81.  
  82.  
  83. /**
  84.  * Når du søger efter noget og trykker på enter bliver search kaldt.
  85.  */
  86. function makeSearchEnter() {
  87.     $(document).on('keyup', "#searchField", function (e) {
  88.         if (e.keyCode == 13) {
  89.             searchForItems();
  90.  
  91.         }
  92.     });
  93. }
  94.  
  95.  
  96. /**
  97.  *  Vores søge funktion, den ser hvilken shop du kigger på nu.
  98.  *  Og søger derefter det og viser det på siden.
  99.  */
  100. function searchForItems() {
  101.     var search = document.getElementById("searchField").value;
  102.     var shop = document.getElementById("mySelect").value;
  103.     console.log(search.length);
  104.     if (search.length != 0){
  105.         $('#searchField').val('');
  106.         $('#searchField').attr("placeholder",'Searching...');
  107.  
  108.     }else {
  109.         $('#searchField').attr("placeholder",'Please type in text');
  110.     }
  111.     $.post("/rest/shop/search", {search: search, shopID: shop}, function (response) {
  112.         var items = JSON.parse(response);
  113.         addItemsToTable(items);
  114.         searchBoolean = true;
  115.         $('#searchField').attr("placeholder",'Done! Enter new Word');
  116.  
  117.     });
  118. }
  119.  
  120.  
  121. /**
  122.  *  Laver en customer og ser om det er udfyldt korrekt.
  123.  *  Giver besked tilbage til brugeren, hvis det ikke lykkes.
  124.  */
  125. function createCustomer() {
  126.     var createCustomerName = document.getElementById("createUserName").value;
  127.     var createCustomerPassword = document.getElementById("createUserPassword").value;
  128.  
  129.     if (!(createCustomerName.length > 0 && createCustomerPassword.length > 0)) {
  130.         errorMessage("You must fill in both username and password!");
  131.     } else {
  132.         $.post("/rest/shop/createCustomer", {
  133.             username: createCustomerName,
  134.             password: createCustomerPassword
  135.         }, function (name) {
  136.             var login = document.getElementById("login-customer-submit-button");
  137.             var create = document.getElementById("create-customer-submit-button");
  138.             if (name == "true") {
  139.                 errorMessage("Your are now a customer. Please Log in");
  140.                 create.disabled = true;
  141.                 login.disabled = false;
  142.             } else {
  143.                 errorMessage("We could not create you. Username is taken");
  144.                 login.disabled = false;
  145.                 create.disabled = false;
  146.             }
  147.         });
  148.     }
  149.  
  150. }
  151.  
  152.  
  153. /**
  154.  * Logger din bruger ind og sætter boolean: isLoggedIn til true.
  155.  */
  156. function loginCustomer() {
  157.     var customerName = document.getElementById("createUserName").value;
  158.     var customerPass = document.getElementById("createUserPassword").value;
  159.  
  160.     if (!(customerName.length > 0 && customerPass.length > 0)) {
  161.         errorMessage("You must fill in both username and password!");
  162.     } else {
  163.         $.post("/rest/shop/loginCustomer", {
  164.             username: customerName,
  165.             password: customerPass
  166.         }, function (name) {
  167.             var login = document.getElementById("login-button");
  168.             var create = document.getElementById("create-customer-submit-button");
  169.             if (name == "true") {
  170.                 errorMessage("You are now logged in.");
  171.                 isLoggedIn = true;
  172.                 create.disabled = true;
  173.                 login.disabled = true;
  174.             } else {
  175.                 errorMessage("We could not log you in. Did you type your username & Password correct?");
  176.             }
  177.  
  178.         });
  179.  
  180.     }
  181. }
  182.  
  183.  
  184. /**
  185.  * Se mine tidligere ordre. Skjuler nuværende items og viser dine købte.
  186.  */
  187. function myOrdersLogInFirst() {
  188.     $(document).on('click', '#my-orders-button', function () {
  189.         if (isLoggedIn == false) {
  190.             alert("You have to log in before you can see your previous orders!");
  191.         } else {
  192.             hideTable();
  193.             getFormerBought();
  194.             showFormerBought();
  195.         }
  196.     });
  197. }
  198.  
  199. /**
  200.  * Til at vise brugeren, hvis det ikke lykkes at logge ind eller create customer.
  201.  */
  202. function errorMessage(name) {
  203.     $("#login-message-error-area").text(name);
  204. }
  205.  
  206. /**
  207.  * Hvis det ikke lykkes at købe tingene i kurven.
  208.  * Eller hvis det lykkes giver den besked.
  209.  */
  210. function buyMessage(text) {
  211.     $("#buy-items-message").text(text);
  212. }
  213.  
  214.  
  215. /**
  216.  * Bliver kaldt når siden loades og laver selecter.
  217.  */
  218. function updateStockList() {
  219.     sendRequest("GET", "rest/shop/testing", null, function (itemsText) {
  220.         //This code is called when the server has sent its data
  221.         var items = JSON.parse(itemsText);
  222.         addItemsToTable(items);
  223.         getShopNames();
  224.     });
  225. }
  226.  
  227. /**
  228.  * Bliver kaldt for at opdatere itemsList.
  229.  */
  230. function updateStockListWithOutSelect() {
  231.     sendRequest("GET", "rest/shop/testing", null, function (itemsText) {
  232.         //This code is called when the server has sent its data
  233.         var items = JSON.parse(itemsText);
  234.         addItemsToTable(items);
  235.     });
  236. }
  237.  
  238. /**
  239.  * Opdaterer itemsList med det shopID som Selecteren har.
  240.  */
  241. function updateStockListWithOtherShopID() {
  242.     var shopID = $('#mySelect').val();
  243.     console.log(shopID);
  244.     sendRequest("GET", "rest/shop/items/" + shopID, null, function (itemsText) {
  245.         //This code is called when the server has sent its data
  246.         var items = JSON.parse(itemsText);
  247.         addItemsToTable(items);
  248.         hideFormerBought();
  249.         showTable();
  250.     });
  251. }
  252.  
  253. /**
  254.  * Funktion der gør det muligt at købe det der er i cart.
  255.  */
  256. function buyItemsInCart() {
  257.     $(document).on("click", "#buy-items-in-shopping-bag", function () {
  258.  
  259.         if (isLoggedIn == false) {
  260.             buyMessage("You need to login to buy your items");
  261.         } else {
  262.             buyMessage("");
  263.             $.post("/rest/shop/buyItemsInCart", null, function (response) {
  264.                 console.log("We got response");
  265.                 buyMessage(response);
  266.                 updateStockListWithOutSelect();
  267.  
  268.                 var shoppingBag = document.getElementById("quickCart-products");
  269.  
  270.                 //FIXME
  271.                 /*
  272.                 var $length = $('#quickCart-products').find('div').length;
  273.                 console.log("length is " + $length);
  274.                 if ($length >= 1) {
  275.                     for(i = 0; i<$length; i++){
  276.                         console.log(i);
  277.                         console.log(shoppingBag);
  278.                         console.log(shoppingBag.childNodes[i]);
  279.                         shoppingBag.removeChild(shoppingBag.childNodes[i]);
  280.                     }
  281.                 }
  282.                 */
  283.  
  284.  
  285.                 var price = document.getElementById("total-price");
  286.                 price.innerHTML = "0 kr.";
  287.                 updateStockListWithOtherShopID();
  288.                 var noItems = document.getElementById("no-items");
  289.                 noItems.style.display = "block";
  290.  
  291.             });
  292.  
  293.         }
  294.     });
  295. }
  296.  
  297.  
  298.  
  299. /**
  300.  * //FIXME
  301.  */
  302. function addToCartIfLoggedIn(itemID, itemStock) {
  303.     if (itemStock > 0) {
  304.         $.post("/rest/shop/addToCart", {itemID: itemID}, function (response) {
  305.             //Do nothing...
  306.  
  307.         });
  308.     }
  309. }
  310.  
  311. function removeFromCartIfLoggedIn(itemID) {
  312.     $.post("/rest/shop/removeFromCart", {itemID: itemID}, function (response) {
  313.         //Do nothing...
  314.     });
  315. }
  316.  
  317. var stockValue;
  318. function addToCartChange() {
  319.  
  320.     $(".addItemButton").click(function () {
  321.  
  322.         var $this = $(this);
  323.  
  324.         var $counter = $this.closest('td').next('.itemsInCart').find('span');
  325.         var $number = parseInt($counter.text());
  326.         var $numberToAdd = 1;
  327.         var $stock = $this.closest('td').prev('td');
  328.         var $stockValue = parseInt($stock.text());
  329.         stockValue = $stockValue;
  330.         eow();
  331.  
  332.         function eow(stockValue) {
  333.  
  334.             if ($number < $stockValue || $stockValue > 0) {
  335.                 $number = $number + $numberToAdd;
  336.                 $stockValue--;
  337.                 $counter.replaceWith("<span class='inputInCart'>" + $number + "</span>");
  338.                 $stock.replaceWith("<td>" + $stockValue + "</td>");
  339.  
  340.  
  341.             } else {
  342.  
  343.                 $number = $stockValue;
  344.             }
  345.         }
  346.  
  347.         price();
  348.  
  349.     });
  350.  
  351.     $(".removeItemButton").click(function () {
  352.  
  353.         var $this = $(this);
  354.  
  355.         var $counter = $this.closest('td').prev('.itemsInCart').find('span');
  356.         var $number = parseInt($counter.text());
  357.         var $numberToRemove = 1;
  358.         var $stock = $this.closest('td').prev('td').prev('td').prev('td');
  359.         var $stockValue = parseInt($stock.text());
  360.  
  361.         if ($number > 0) {
  362.             $number = $number - $numberToRemove;
  363.             $stockValue++;
  364.             $counter.replaceWith("<span class='inputInCart'>" + $number + "</span>")
  365.             $stock.replaceWith("<td>" + $stockValue + "</td>")
  366.         } else {
  367.             $number = 0;
  368.         }
  369.  
  370.         price();
  371.         removeFromQuickCart();
  372.  
  373.         function removeFromQuickCart() {
  374.  
  375.             var $name = $this.closest('td').prev('td').prev('td').prev('td').prev('td').prev('td').prev('td').prev('td');
  376.             var $nameText = $name.text();
  377.             console.log($nameText);
  378.             var $thisName = '.' + $nameText.toString();
  379.             console.log($thisName);
  380.             if ($('#quickCart-products').find($thisName).length != 0) {
  381.  
  382.                 var elements = $('#quickCart-products').find($thisName);
  383.                 elements[0].parentNode.removeChild(elements[0]);
  384.             }
  385.  
  386.             if ($('#quickCart-products').find('p').length === 1) {
  387.  
  388.                 var noItems = document.getElementById("no-items");
  389.                 noItems.style.display = "block";
  390.             }
  391.         }
  392.  
  393.     });
  394.  
  395. }
  396.  
  397.  
  398. /**
  399.  * Moving items to cart from table.
  400.  */
  401. function addToQuickCart(itemName, itemPrice, itemURL) {
  402.  
  403.     // DIV
  404.     var divToAdd = document.createElement("div");
  405.     divToAdd.className = "divProductWithItem";
  406.  
  407.     // IMG
  408.     var image = document.createElement('img');
  409.     image.className = "fishPictures";
  410.     image.src = itemURL;
  411.     image.alt = "FAIL";
  412.     divToAdd.appendChild(image);
  413.  
  414.  
  415.     // NAME
  416.     var para = document.createElement("p");
  417.     para.textContent = itemName;
  418.     para.className = itemName;
  419.     divToAdd.appendChild(para);
  420.  
  421.     // PRICE
  422.     var price = document.createElement("p");
  423.     price.textContent = itemPrice + ' kr.';
  424.     price.className = "itemPriceQuickCart";
  425.     divToAdd.appendChild(price);
  426.  
  427.     $('.quickCart-products').prepend(divToAdd);
  428.  
  429.     var noItems = document.getElementById("no-items");
  430.     noItems.style.display = "none";
  431. }
  432.  
  433. function getShopNames() {
  434.     sendRequest("GET", "rest/shop/getShopNames", null, function (shopNames) {
  435.         var shops = JSON.parse(shopNames);
  436.         viewShops(shops);
  437.     });
  438. }
  439.  
  440.  
  441. function viewShops(shopNames) {
  442.     var scroller = document.getElementById("scroller");
  443.     var select = document.createElement("SELECT");
  444.     for (i = 0; i < shopNames.length; i++) {
  445.         (function (i) {
  446.             var shop = shopNames[i];
  447.  
  448.             select.setAttribute("id", "mySelect");
  449.  
  450.  
  451.             scroller.appendChild(select);
  452.  
  453.             var shopName = shop.shopName;
  454.             var shopID = shop.ShopID;
  455.  
  456.             var option = document.createElement("option");
  457.             option.setAttribute("value", shopID);
  458.  
  459.             if (shop.shopName == "Fish Shop") {
  460.                 option.setAttribute("selected", "selected");
  461.             }
  462.  
  463.             var shoptext = document.createTextNode(shopName);
  464.             option.appendChild(shoptext);
  465.             select.appendChild(option);
  466.         }(i));
  467.     }
  468.     selectList();
  469. }
  470.  
  471.  
  472. function goHome() {
  473.     $(document).on('click', '#home-button', function () {
  474.         showTable();
  475.         hideFormerBought();
  476.         if (searchBoolean == true){
  477.            updateStockListWithOtherShopID();
  478.             searchBoolean = false;
  479.         }
  480.     });
  481.  
  482.     $(document).on('click', '#logo', function () {
  483.  
  484.         showTable();
  485.         hideFormerBought();
  486.         if (searchBoolean == true){
  487.          updateStockListWithOtherShopID();
  488.             searchBoolean = false;
  489.         }
  490.     });
  491.  
  492. }
  493.  
  494. function showFormerBought() {
  495.     var list = document.getElementById("my-orders");
  496.     list.style.display = "table";
  497.     list.style.visibility = "visible";
  498.     list.style.width = "100%";
  499.     list.style.marginTop = "0px";
  500. }
  501.  
  502. function hideFormerBought() {
  503.  
  504.     var list = document.getElementById("my-orders");
  505.     list.style.display = "none";
  506.     list.style.visibility = "hidden";
  507.     list.style.marginTop = "150px";
  508. }
  509.  
  510. function hideTable() {
  511.  
  512.     var table = document.getElementById("table-div");
  513.     table.style.display = "none";
  514.     table.style.visibility = "hidden";
  515. }
  516.  
  517. function showTable() {
  518.     var table = document.getElementById("table-div");
  519.     table.style.display = "table";
  520.     table.style.visibility = "visible";
  521.     table.style.width = "100%";
  522. }
  523.  
  524. function getFormerBought() {
  525.     sendRequest("GET", "rest/shop/itemsBought", null, function (shopNames) {
  526.         var shops = JSON.parse(shopNames);
  527.         viewBoughtItems(shops);
  528.     });
  529. }
  530.  
  531. function viewBoughtItems(items) {
  532.  
  533.     //Get the table body we we can add items to it
  534.     var tableBody = document.getElementById("itemTableBody-my-orders");
  535.     //Remove all contents of the table body (if any exist)
  536.     tableBody.innerHTML = "";
  537.  
  538.  
  539.     var totalAmountForCustomer = 0;
  540.     for (i = 0; i < items.length; i++) {
  541.         //GET ITEMS FROM SERVER WITH ID
  542.         (function (i) {
  543.             var itemFormerBought = items[i];
  544.  
  545.             var tr = document.createElement("tr");
  546.  
  547.             //TIME BOUGHT
  548.             var dateCell = document.createElement("td");
  549.             var timeStamp = new Date(parseInt(itemFormerBought.saleTime));
  550.             var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  551.             var year = timeStamp.getFullYear();
  552.             var month = months[timeStamp.getMonth()];
  553.             var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
  554.             var date = timeStamp.getDate();
  555.             var day = days[timeStamp.getDay()];
  556.             var hours = timeStamp.getHours();
  557.             var minutes = timeStamp.getMinutes();
  558.             var seconds = timeStamp.getSeconds();
  559.             //+ " at " +hours+ ":" + minutes + ":" + seconds
  560.             dateCell.textContent = day + ", " + month + " " + date + ", " + year;
  561.             tr.appendChild(dateCell);
  562.  
  563.  
  564.             // ITEM NAME
  565.             var nameCell = document.createElement("td");
  566.             nameCell.className = "itemName";
  567.             nameCell.textContent = itemFormerBought.item.itemName;
  568.             tr.appendChild(nameCell);
  569.  
  570.             // ITEM URL / PICTURE
  571.             var picturecell = document.createElement("td");
  572.             var image = document.createElement('img');
  573.             image.className = "fishPictures";
  574.             image.src = itemFormerBought.item.itemURL;
  575.             image.alt = "FAIL";
  576.             picturecell.appendChild(image);
  577.             tr.appendChild(picturecell);
  578.  
  579.  
  580.             //ITEM DESCRIPTION
  581.             var htmlItemDescription = itemFormerBought.item.itemDescription;
  582.             var htmlConverter = document.createElement("td");
  583.             htmlConverter.innerHTML = htmlItemDescription;
  584.             tr.appendChild(htmlConverter);
  585.  
  586.  
  587.             // ITEM AMOUNT
  588.             var itemAmount = document.createElement("td");
  589.             var saleAmount = itemFormerBought.saleAmount;
  590.             itemAmount.textContent = saleAmount;
  591.             tr.appendChild(itemAmount);
  592.  
  593.  
  594.             // ITEM PRICE
  595.             var paraItemPrice = document.createElement("td");
  596.             var itemPrice = itemFormerBought.item.itemPrice;
  597.             paraItemPrice.textContent = itemPrice + " kr.";
  598.             tr.appendChild(paraItemPrice);
  599.  
  600.  
  601.             // TOTAL FOR THIS ITEM
  602.             var paraItemTotalAmount = document.createElement("td");
  603.             var totalForThisItem = (parseInt(itemPrice * saleAmount));
  604.             totalAmountForCustomer += totalForThisItem;
  605.             paraItemTotalAmount.textContent = totalForThisItem + " kr.";
  606.             tr.appendChild(paraItemTotalAmount);
  607.  
  608.  
  609.             //Append
  610.             tableBody.appendChild(tr);
  611.  
  612.  
  613.         }(i));
  614.     }
  615.  
  616.     var div = document.getElementById("result-my-orders");
  617.     div.innerHTML = "";
  618.     var totalPricePara = document.createElement("p");
  619.     totalPricePara.textContent = "Total price for all your buys is " + totalAmountForCustomer;
  620.     div.appendChild(totalPricePara);
  621. }
  622.  
  623.  
  624.  
  625. function selectList() {
  626.     $('#mySelect').change(function () {
  627.         var shopID = $(this).val();
  628.         sendRequest("GET", "rest/shop/items/" + shopID, null, function (itemsText) {
  629.             //This code is called when the server has sent its data
  630.             var items = JSON.parse(itemsText);
  631.             addItemsToTable(items);
  632.             hideFormerBought();
  633.             showTable();
  634.         });
  635.  
  636.     });
  637. }
  638.  
  639. function price() {
  640.  
  641.     var totalPriceSpan = document.getElementById("total-price"),
  642.         $totalPriceSpan = totalPriceSpan,
  643.         amount = document.getElementsByClassName("inputInCart"),
  644.         price = document.getElementsByClassName("price"),
  645.         parent = totalPriceSpan.parentNode,
  646.         tempDiv = document.createElement('div');
  647.  
  648.     var totalPrice = 0;
  649.  
  650.     for (var i = 0; i < amount.length; i++) {
  651.  
  652.  
  653.         var amountOfItem = parseInt(amount[i].innerText);
  654.         var priceOfEach = parseInt(price[i].innerText);
  655.  
  656.         var priceOfAll = amountOfItem * priceOfEach;
  657.         var oldString = totalPrice.toString() + "kr.";
  658.  
  659.         totalPrice += priceOfAll;
  660.         var totalPriceString = totalPrice.toString() + "kr.";
  661.     }
  662.  
  663.     tempDiv.innerHTML = "<b class='total-price' id='total-price'>" + totalPriceString + "</b>";
  664.  
  665.     var input = tempDiv.childNodes[0];
  666.  
  667.     parent.replaceChild(input, totalPriceSpan);
  668.  
  669. }
  670.  
  671. function addItemsToTable(items) {
  672.     //Get the table body we we can add items to it
  673.     var tableBody = document.getElementById("itemtablebody");
  674.     //Remove all contents of the table body (if any exist)
  675.     tableBody.innerHTML = "";
  676.  
  677.     //Loop through the items from the server
  678.     for (var i = 0; i < items.length; i++) {
  679.         (function (i) {
  680.             var item = items[i];
  681.             //Create a new line for this item
  682.             var tr = document.createElement("tr");
  683.             tr.id = item.itemID;
  684.  
  685.  
  686.             // ITEM NAME
  687.             var nameCell = document.createElement("td");
  688.             nameCell.className = "itemName";
  689.             nameCell.textContent = item.itemName;
  690.             tr.appendChild(nameCell);
  691.  
  692.             //Item URL / ITEM PICTURE
  693.             var picturecell = document.createElement("td");
  694.             var image = document.createElement('img');
  695.             image.className = "fishPictures";
  696.             image.src = item.itemURL;
  697.             image.alt = "FAIL";
  698.             picturecell.appendChild(image);
  699.             tr.appendChild(picturecell);
  700.  
  701.             // ITEM PRICE
  702.             var priceCell = document.createElement("td");
  703.             priceCell.textContent = item.itemPrice;
  704.             priceCell.className = "price";
  705.             tr.appendChild(priceCell);
  706.  
  707.             //ITEM DESCRIPTION
  708.             var htmlItemDescription = item.itemDescription;
  709.             var htmlConverter = document.createElement("td");
  710.             htmlConverter.innerHTML = htmlItemDescription;
  711.             tr.appendChild(htmlConverter);
  712.  
  713.             // ITEM STOCK
  714.             var itemStock = document.createElement("td");
  715.             itemStock.textContent = item.itemStock;
  716.             tr.appendChild(itemStock);
  717.  
  718.  
  719.             // ADD TO CART
  720.             var addToCart = document.createElement("td");
  721.             var addToCartButton = document.createElement("BUTTON");
  722.             addToCartButton.className = "addItemButton";
  723.             var buttonText = document.createTextNode("  +  ");
  724.             addToCartButton.appendChild(buttonText);
  725.             addToCart.appendChild(addToCartButton);
  726.             tr.appendChild(addToCart);
  727.  
  728.             // ADD TO CART - FUNCTION
  729.             $(addToCartButton).click(function () {
  730.                 addToQuickCart(item.itemName, item.itemPrice, item.itemURL);
  731.                 addToCartIfLoggedIn(item.itemID, item.itemStock);
  732.             });
  733.  
  734.             // IN CART
  735.             var itemsInCart = document.createElement("td");
  736.             itemsInCart.className = "inCart";
  737.             var inputField = document.createElement("span");
  738.             inputField.textContent = "0";
  739.             inputField.className = "inputInCart";
  740.             itemsInCart.appendChild(inputField);
  741.             itemsInCart.className = "itemsInCart";
  742.             tr.appendChild(itemsInCart);
  743.  
  744.             //REMOVE FROM CART
  745.             var removeFromCart = document.createElement("td");
  746.             var removeFromCartButton = document.createElement("BUTTON");
  747.             removeFromCartButton.className = "removeItemButton";
  748.             var buttonText = document.createTextNode("  -  ");
  749.             removeFromCartButton.appendChild(buttonText);
  750.             removeFromCart.appendChild(removeFromCartButton);
  751.             tr.appendChild(removeFromCart);
  752.  
  753.             // REMOVE FROM CART - BUTTON
  754.             $(removeFromCartButton).click(function () {
  755.                 removeFromCartIfLoggedIn(item.itemID);
  756.             });
  757.  
  758.  
  759.             // ITEM ID
  760.             var itemID = document.createElement("td");
  761.             itemID.textContent = item.itemID;
  762.             tr.appendChild(itemID);
  763.  
  764.             tableBody.appendChild(tr);
  765.         })(i);
  766.     }
  767.  
  768.     addToCartChange();
  769.  
  770. }
  771.  
  772.  
  773. /////////////////////////////////////////////////////
  774. // Code from slides
  775. /////////////////////////////////////////////////////
  776.  
  777. /**
  778.  * A function that can add event listeners in any browser
  779.  */
  780. function addEventListener(myNode, eventType, myHandlerFunc) {
  781.     if (myNode.addEventListener)
  782.         myNode.addEventListener(eventType, myHandlerFunc, false);
  783.     else
  784.         myNode.attachEvent("on" + eventType,
  785.             function (event) {
  786.                 myHandlerFunc.call(myNode, event);
  787.             });
  788. }
  789.  
  790. var http;
  791. if (!XMLHttpRequest)
  792.     http = new ActiveXObject("Microsoft.XMLHTTP");
  793. else
  794.     http = new XMLHttpRequest();
  795.  
  796. function sendRequest(httpMethod, url, body, responseHandler) {
  797.     http.open(httpMethod, url);
  798.     if (httpMethod == "POST") {
  799.         http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  800.     }
  801.     http.onreadystatechange = function () {
  802.         if (http.readyState == 4 && http.status == 200) {
  803.             responseHandler(http.responseText);
  804.         }
  805.     };
  806.     http.send(body);
  807. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement