PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/order.html

https://gitlab.com/vsailakshmi/Web-Order-Application
HTML | 362 lines | 307 code | 55 blank | 0 comment | 0 complexity | 07a64b4ee5db807ab75f7427be9b9ea4 MD5 | raw file
  1. <html>
  2. <head>
  3. <link rel="stylesheet" type="text/css" href="style.css"/>
  4. <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js"></script>
  5. <script type="text/javascript" src="/socket.io/socket.io.js"></script>
  6. <script type="text/javascript" src="/js/dimmer.js"></script>
  7. <script type="text/javascript" src="/js/searchbox.js"></script>
  8. <script type="text/javascript" src="/js/dialog.js"></script>
  9. <script type="text/javascript" src="/js/confirmation.js"></script>
  10. <script>
  11. $(document).ready(function()
  12. {
  13. var id;
  14. var modified = false;
  15. var new_customer = false;
  16. var alternate_address = false;
  17. var socket = io.connect('localhost/db');
  18. initializeDimmer();
  19. initializeSearchbox();
  20. initializeDialog();
  21. initializeConfirmation();
  22. //Hide the delivery form (this will be made visible if we need to specify a secondary address for delivery)
  23. $("#delivery").hide();
  24. //Delay for a moment to display the empty form, then open the search form.
  25. setTimeout(searchForCustomer, 50);
  26. function searchForCustomer()
  27. {
  28. openSearchbox("Enter customer's phone number:", instantSearch, receiveSearchResult);
  29. enableDimmer();
  30. };
  31. //Called by the search dialog on keypresses to act as instant search
  32. function instantSearch()
  33. {
  34. socket.emit('search_customer_by_phone_number', {phone_number: getSearchboxQuery()});
  35. }
  36. function clearForm()
  37. {
  38. $("#input-name").val("");
  39. $("#input-phonenumber").val("");
  40. $("#input-streetaddress").val("");
  41. $("#input-city").val("");
  42. $("#input-zipcode").val("");
  43. $("#input-state").val("");
  44. }
  45. //Called by the search dialog upon close
  46. function receiveSearchResult(customer_id)
  47. {
  48. if (customer_id !== -1)
  49. {
  50. socket.emit('search_by_customer_id', {id: customer_id});
  51. //store the customer_id so we can use it later
  52. id = customer_id;
  53. }
  54. else
  55. {
  56. //We have a new customer, so let's make the prompt proper
  57. new_customer = true;
  58. clearForm();
  59. $("#continue").html("Continue (Save New Customer)");
  60. }
  61. disableDimmer();
  62. }
  63. function specifyAlternateAddress()
  64. {
  65. //make that we're using an alternate address
  66. alternate_address = true;
  67. $("#delivery").slideDown("slow").show("slow");
  68. }
  69. function validateCustomerInfo()
  70. {
  71. var valid = true;
  72. if ($("#input-name").val() == "")
  73. {
  74. valid = false;
  75. }
  76. if ($("#input-phonenumber").val() == "")
  77. {
  78. valid = false;
  79. }
  80. if ($("#input-streetaddress").val() == "")
  81. {
  82. valid = false;
  83. }
  84. if ($("#input-city").val() == "")
  85. {
  86. valid = false;
  87. }
  88. if ($("#input-zipcode").val() == "")
  89. {
  90. valid = false;
  91. }
  92. if ($("#input-state").val() == "")
  93. {
  94. valid = false;
  95. }
  96. return valid;
  97. }
  98. function validateDeliveryAddress()
  99. {
  100. var valid = true;
  101. if ($("#input-altstreetaddress").val() == "")
  102. {
  103. valid = false;
  104. }
  105. if ($("#input-altcity").val() == "")
  106. {
  107. valid = false;
  108. }
  109. if ($("#input-altzipcode").val() == "")
  110. {
  111. valid = false;
  112. }
  113. if ($("#input-altstate").val() == "")
  114. {
  115. valid = false;
  116. }
  117. return valid;
  118. }
  119. function saveOrder()
  120. {
  121. var order = {};
  122. order.customer_id = id;
  123. order.delivery_status = 1;
  124. if (alternate_address == true)
  125. {
  126. order.delivery_street_address = $("#input-altstreetaddress").val();
  127. order.delivery_city = $("#input-altcity").val();
  128. order.delivery_zip_code = $("#input-altzipcode").val();
  129. order.delivery_state = $("#input-altstate").val();
  130. }
  131. else
  132. {
  133. order.delivery_street_address = $("#input-streetaddress").val();
  134. order.delivery_city = $("#input-city").val();
  135. order.delivery_zip_code = $("#input-zipcode").val();
  136. order.delivery_state = $("#input-state").val();
  137. }
  138. order.driver_id = 0;
  139. socket.emit("create_order_information", order);
  140. openConfirmation("The order has been placed successfully.", returnToMenu);
  141. }
  142. function returnToMenu()
  143. {
  144. window.location.href = "/";
  145. }
  146. function saveCustomer()
  147. {
  148. var customer_info = {};
  149. customer_info.name = $("#input-name").val();
  150. customer_info.phone_number = $("#input-phonenumber").val();
  151. customer_info.street_address = $("#input-streetaddress").val();
  152. customer_info.city = $("#input-city").val();
  153. customer_info.zip_code = $("#input-zipcode").val();
  154. customer_info.state = $("#input-state").val();
  155. socket.emit("create_customer_information", customer_info);
  156. }
  157. function saveChanges()
  158. {
  159. var customer_info = {};
  160. customer_info.name = $("#input-name").val();
  161. customer_info.phone_number = $("#input-phonenumber").val();
  162. customer_info.street_address = $("#input-streetaddress").val();
  163. customer_info.city = $("#input-city").val();
  164. customer_info.zip_code = $("#input-zipcode").val();
  165. customer_info.state = $("#input-state").val();
  166. customer_info.id = id;
  167. socket.emit("update_customer_information", customer_info);
  168. }
  169. $("#searchbutton").click(function()
  170. {
  171. //Reset the new customer check (this will be set if the null result is clicked)
  172. new_customer = false;
  173. //Also, let's reset the prompt
  174. $("#continue").html("Continue");
  175. //begin a search!
  176. searchForCustomer();
  177. });
  178. //Show the the prompt, then decide whether or not to show the delivery address specifier
  179. $("#continue").click(function()
  180. {
  181. //is our form info valid? If not, let's perster the user.
  182. if (!validateCustomerInfo())
  183. {
  184. enableDimmer();
  185. openConfirmation("Please enter all required fields.", function()
  186. {
  187. disableDimmer();
  188. });
  189. //bail.
  190. return;
  191. }
  192. $("#information").slideUp("slow").hide("slow");
  193. openDialog("Will the customer be delivering to their home address?", saveOrder, specifyAlternateAddress);
  194. //store our updated values locally before pushing
  195. name = $("#input-name").val();
  196. phone_number = $("#input-phonenumber").val();
  197. street_address = $("#input-streetaddress").val();
  198. city = $("#input-city").val();
  199. zip_code = $("#input-zipcode").val();
  200. state = $("#input-state").val();
  201. //if we've got a new customer, let's write them to the database
  202. if (new_customer == true)
  203. {
  204. saveCustomer();
  205. }
  206. //if we've modified anything, let's save our changes
  207. if (modified == true && new_customer == false)
  208. {
  209. saveChanges();
  210. }
  211. });
  212. $("#delivery > #confirm").click(function()
  213. {
  214. //is our form info valid? If not, let's perster the user.
  215. if (!validateDeliveryAddress())
  216. {
  217. enableDimmer();
  218. openConfirmation("Please enter all required fields.", function()
  219. {
  220. disableDimmer();
  221. });
  222. //bail.
  223. return;
  224. }
  225. $("#delivery").hide("slow");
  226. saveOrder();
  227. });
  228. socket.on('customer_phone_number_search_results', function (data)
  229. {
  230. clearSearchboxResults();
  231. for (var i in data)
  232. {
  233. var gadget = data[i];
  234. addSearchboxResult(gadget.name, gadget.id);
  235. }
  236. });
  237. socket.on('customer_information_result', function (data)
  238. {
  239. if (data !== [])
  240. {
  241. var name = data[0].name;
  242. var phone_number = data[0].phone_number;
  243. var street_address = data[0].street_address;
  244. var city = data[0].city;
  245. var zip_code = data[0].zip_code;
  246. var state = data[0].state;
  247. //Populate the inputs with data from the database
  248. $("#input-name").val(name);
  249. $("#input-phonenumber").val(phone_number);
  250. $("#input-streetaddress").val(street_address);
  251. $("#input-city").val(city);
  252. $("#input-zipcode").val(zip_code);
  253. $("#input-state").val(state);
  254. }
  255. });
  256. socket.on('customer_creation_result', function (data)
  257. {
  258. id = data.insertId;
  259. });
  260. //If anything is typed into the input fields, change button to mention that data will be saved
  261. $("td > input").keyup(function()
  262. {
  263. modified = true;
  264. if (new_customer != true)
  265. {
  266. $("#continue").html("Continue (Save Changes)");
  267. }
  268. });
  269. });
  270. </script>
  271. </head>
  272. <body>
  273. <div id="home" class="form">
  274. <h2 style="text-align: center;"><a href="/">Back to Menu</a></h1>
  275. </div>
  276. <div id="menu" class="form">
  277. <div id="searchbutton" class="button" style="margin-left: auto; margin-right: auto;">Search</div>
  278. </div>
  279. <div id="information" class="form">
  280. <h1 style="text-align: center;">Customer Information</h1>
  281. <table style="margin-left: auto; margin-right: auto;">
  282. <tr><td><span style="font-weight: bold;">Basic Information</span><br/></td></tr>
  283. <tr><td><span>Name</span></td><td><input id="input-name" type="text"/></td></tr>
  284. <tr><td><span>Phone Number</span></td><td><input id="input-phonenumber" maxlength="10" type="text"/></td></tr>
  285. <tr><td><span style="font-weight: bold;">Address</span><br/></td></tr>
  286. <tr><td><span>Street Address</span></td><td><input id="input-streetaddress" type="text"/></td></tr>
  287. <tr><td><span>City</span></td><td><input id="input-city" type="text"/></td></tr>
  288. <tr><td><span>Zip Code</span></td><td><input id="input-zipcode" type="text"/></td></tr>
  289. <tr><td><span>State</span></td><td><input id="input-state" type="text"/></td></tr>
  290. </table>
  291. <br/>
  292. <div id="continue" class="button" style="margin-left: auto; margin-right: auto;">Continue</div>
  293. </div>
  294. <div id="delivery" class="form">
  295. <h1 style="text-align: center;">Delivery Address</h1>
  296. <table style="margin-left: auto; margin-right: auto;">
  297. <tr><td><span style="font-weight: bold;">Address</span><br/></td></tr>
  298. <tr><td><span>Street Address</span></td><td><input id="input-altstreetaddress" type="text"/></td></tr>
  299. <tr><td><span>City</span></td><td><input id="input-altcity" type="text"/></td></tr>
  300. <tr><td><span>Zip Code</span></td><td><input id="input-altzipcode" type="text"/></td></tr>
  301. <tr><td><span>State</span></td><td><input id="input-altstate" type="text"/></td></tr>
  302. </table>
  303. <br/>
  304. <div id="confirm" class="button" style="margin-left: auto; margin-right: auto;">Confirm</div>
  305. </div>
  306. </body>
  307. </html>