PageRenderTime 27ms CodeModel.GetById 33ms RepoModel.GetById 4ms app.codeStats 1ms

/themes/pos_bstore2/js/order-opc.js

https://gitlab.com/elasa/shop2.elasa.ir
JavaScript | 988 lines | 871 code | 54 blank | 63 comment | 167 complexity | 9ab53705d8999582a8508e4532e112a7 MD5 | raw file
  1. /*
  2. * 2007-2015 PrestaShop
  3. *
  4. * NOTICE OF LICENSE
  5. *
  6. * This source file is subject to the Academic Free License (AFL 3.0)
  7. * that is bundled with this package in the file LICENSE.txt.
  8. * It is also available through the world-wide-web at this URL:
  9. * http://opensource.org/licenses/afl-3.0.php
  10. * If you did not receive a copy of the license and are unable to
  11. * obtain it through the world-wide-web, please send an email
  12. * to license@prestashop.com so we can send you a copy immediately.
  13. *
  14. * DISCLAIMER
  15. *
  16. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  17. * versions in the future. If you wish to customize PrestaShop for your
  18. * needs please refer to http://www.prestashop.com for more information.
  19. *
  20. * @author PrestaShop SA <contact@prestashop.com>
  21. * @copyright 2007-2015 PrestaShop SA
  22. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  23. * International Registered Trademark & Property of PrestaShop SA
  24. */
  25. $(document).ready(function(){
  26. // GUEST CHECKOUT / NEW ACCOUNT MANAGEMENT
  27. if ((typeof isLogged == 'undefined' || !isLogged) || (typeof isGuest !== 'undefined' && isGuest))
  28. {
  29. if (guestCheckoutEnabled && !isLogged)
  30. {
  31. $('#opc_account_choice').show();
  32. $('#opc_account_form, #opc_invoice_address').hide();
  33. $(document).on('click', '#opc_createAccount',function(e){
  34. e.preventDefault();
  35. $('.is_customer_param').show();
  36. $('#opc_account_form').slideDown('slow');
  37. $('#is_new_customer').val('1');
  38. $('#opc_account_choice, #opc_invoice_address').hide();
  39. if (typeof bindUniform !=='undefined')
  40. bindUniform();
  41. });
  42. $(document).on('click', '#opc_guestCheckout', function(e){
  43. e.preventDefault();
  44. $('.is_customer_param').hide();
  45. $('#opc_account_form').slideDown('slow');
  46. $('#is_new_customer').val('0');
  47. $('#opc_account_choice, #opc_invoice_address').hide();
  48. $('#new_account_title').html(txtInstantCheckout);
  49. $('#submitAccount').attr({id : 'submitGuestAccount', name : 'submitGuestAccount'});
  50. if (typeof bindUniform !=='undefined')
  51. bindUniform();
  52. });
  53. }
  54. else if (isGuest)
  55. {
  56. $('.is_customer_param').hide();
  57. $('#opc_account_form').show('slow');
  58. $('#is_new_customer').val('0');
  59. $('#opc_account_choice, #opc_invoice_address').hide();
  60. $('#new_account_title').html(txtInstantCheckout);
  61. }
  62. else
  63. {
  64. $('#opc_account_choice').hide();
  65. $('#is_new_customer').val('1');
  66. $('.is_customer_param, #opc_account_form').show();
  67. $('#opc_invoice_address').hide();
  68. }
  69. // LOGIN FORM
  70. $(document).on('click', '#openLoginFormBlock', function(e){
  71. e.preventDefault();
  72. $('#openNewAccountBlock').show();
  73. $(this).hide();
  74. $('#login_form_content').slideDown('slow');
  75. $('#new_account_form_content').slideUp('slow');
  76. });
  77. // LOGIN FORM SENDING
  78. $(document).on('click', '#SubmitLogin', function(e){
  79. e.preventDefault();
  80. $.ajax({
  81. type: 'POST',
  82. headers: { "cache-control": "no-cache" },
  83. url: authenticationUrl + '?rand=' + new Date().getTime(),
  84. async: false,
  85. cache: false,
  86. dataType : "json",
  87. data: 'SubmitLogin=true&ajax=true&email='+encodeURIComponent($('#login_email').val())+'&passwd='+encodeURIComponent($('#login_passwd').val())+'&token=' + static_token ,
  88. success: function(jsonData)
  89. {
  90. if (jsonData.hasError)
  91. {
  92. var errors = '<b>'+txtThereis+' '+jsonData.errors.length+' '+txtErrors+':</b><ol>';
  93. for(var error in jsonData.errors)
  94. //IE6 bug fix
  95. if(error !== 'indexOf')
  96. errors += '<li>'+jsonData.errors[error]+'</li>';
  97. errors += '</ol>';
  98. $('#opc_login_errors').html(errors).slideDown('slow');
  99. }
  100. else
  101. {
  102. // update token
  103. static_token = jsonData.token;
  104. updateNewAccountToAddressBlock();
  105. }
  106. },
  107. error: function(XMLHttpRequest, textStatus, errorThrown) {
  108. if (textStatus !== 'abort')
  109. {
  110. error = "TECHNICAL ERROR: unable to send login informations \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus;
  111. if (!!$.prototype.fancybox)
  112. $.fancybox.open([
  113. {
  114. type: 'inline',
  115. autoScale: true,
  116. minHeight: 30,
  117. content: '<p class="fancybox-error">' + error + '</p>'
  118. }
  119. ], {
  120. padding: 0
  121. });
  122. else
  123. alert(error);
  124. }
  125. }
  126. });
  127. });
  128. // VALIDATION / CREATION AJAX
  129. $(document).on('click', '#submitAccount, #submitGuestAccount', function(e){
  130. e.preventDefault();
  131. $('#opc_new_account-overlay, #opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeIn('slow')
  132. var callingFile = '';
  133. var params = '';
  134. if (parseInt($('#opc_id_customer').val()) == 0)
  135. {
  136. callingFile = authenticationUrl;
  137. params = 'submitAccount=true&';
  138. }
  139. else
  140. {
  141. callingFile = orderOpcUrl;
  142. params = 'method=editCustomer&';
  143. }
  144. $('#opc_account_form input:visible, #opc_account_form input[type=hidden]').each(function() {
  145. if ($(this).is('input[type=checkbox]'))
  146. {
  147. if ($(this).is(':checked'))
  148. params += encodeURIComponent($(this).attr('name'))+'=1&';
  149. }
  150. else if ($(this).is('input[type=radio]'))
  151. {
  152. if ($(this).is(':checked'))
  153. params += encodeURIComponent($(this).attr('name'))+'='+encodeURIComponent($(this).val())+'&';
  154. }
  155. else
  156. params += encodeURIComponent($(this).attr('name'))+'='+encodeURIComponent($(this).val())+'&';
  157. });
  158. $('#opc_account_form select:visible').each(function() {
  159. params += encodeURIComponent($(this).attr('name'))+'='+encodeURIComponent($(this).val())+'&';
  160. });
  161. params += 'customer_lastname='+encodeURIComponent($('#customer_lastname').val())+'&';
  162. params += 'customer_firstname='+encodeURIComponent($('#customer_firstname').val())+'&';
  163. params += 'alias='+encodeURIComponent($('#alias').val())+'&';
  164. params += 'other='+encodeURIComponent($('#other').val())+'&';
  165. params += 'is_new_customer='+encodeURIComponent($('#is_new_customer').val())+'&';
  166. // Clean the last &
  167. params = params.substr(0, params.length-1);
  168. $.ajax({
  169. type: 'POST',
  170. headers: { "cache-control": "no-cache" },
  171. url: callingFile + '?rand=' + new Date().getTime(),
  172. async: false,
  173. cache: false,
  174. dataType : "json",
  175. data: 'ajax=true&'+params+'&token=' + static_token ,
  176. success: function(jsonData)
  177. {
  178. if (jsonData.hasError)
  179. {
  180. var tmp = '';
  181. var i = 0;
  182. for(var error in jsonData.errors)
  183. //IE6 bug fix
  184. if(error !== 'indexOf')
  185. {
  186. i = i+1;
  187. tmp += '<li>'+jsonData.errors[error]+'</li>';
  188. }
  189. tmp += '</ol>';
  190. var errors = '<b>'+txtThereis+' '+i+' '+txtErrors+':</b><ol>'+tmp;
  191. $('#opc_account_errors').slideUp('fast', function(){
  192. $(this).html(errors).slideDown('slow', function(){
  193. $.scrollTo('#opc_account_errors', 800);
  194. });
  195. });
  196. }
  197. else
  198. {
  199. $('#opc_account_errors').slideUp('slow', function(){
  200. $(this).html('');
  201. });
  202. }
  203. isGuest = parseInt($('#is_new_customer').val()) == 1 ? 0 : 1;
  204. // update addresses id
  205. if(jsonData.id_address_delivery !== undefined && jsonData.id_address_delivery > 0)
  206. $('#opc_id_address_delivery').val(jsonData.id_address_delivery);
  207. if(jsonData.id_address_invoice !== undefined && jsonData.id_address_invoice > 0)
  208. $('#opc_id_address_invoice').val(jsonData.id_address_invoice);
  209. if (jsonData.id_customer !== undefined && jsonData.id_customer !== 0 && jsonData.isSaved)
  210. {
  211. // update token
  212. static_token = jsonData.token;
  213. // It's not a new customer
  214. if ($('#opc_id_customer').val() !== '0')
  215. if (!saveAddress('delivery'))
  216. return false;
  217. // update id_customer
  218. $('#opc_id_customer').val(jsonData.id_customer);
  219. if ($('#invoice_address:checked').length !== 0)
  220. {
  221. if (!saveAddress('invoice'))
  222. return false;
  223. }
  224. // update id_customer
  225. $('#opc_id_customer').val(jsonData.id_customer);
  226. // force to refresh carrier list
  227. if (isGuest)
  228. {
  229. isLogged = 1;
  230. $('#opc_account_saved').fadeIn('slow');
  231. $('#submitAccount').hide();
  232. updateAddressSelection();
  233. }
  234. else
  235. updateNewAccountToAddressBlock();
  236. }
  237. $('#opc_new_account-overlay, #opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeIn('slow');
  238. },
  239. error: function(XMLHttpRequest, textStatus, errorThrown) {
  240. if (textStatus !== 'abort')
  241. {
  242. error = "TECHNICAL ERROR: unable to save account \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus;
  243. if (!!$.prototype.fancybox)
  244. $.fancybox.open([
  245. {
  246. type: 'inline',
  247. autoScale: true,
  248. minHeight: 30,
  249. content: '<p class="fancybox-error">' + error + '</p>'
  250. }
  251. ], {
  252. padding: 0
  253. });
  254. else
  255. alert(error);
  256. }
  257. $('#opc_new_account-overlay, #opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeIn('slow')
  258. }
  259. });
  260. });
  261. }
  262. bindInputs();
  263. $('#opc_account_form input,select,textarea').change(function() {
  264. if ($(this).is(':visible'))
  265. {
  266. $('#opc_account_saved').fadeOut('slow');
  267. $('#submitAccount').show();
  268. }
  269. });
  270. // If the multishipping mode is off assure us the checkbox "I want to specify a delivery address for each products I order." is unchecked.
  271. $('#multishipping_mode_checkbox').attr('checked', false);
  272. // If the multishipping mode is on, check the box "I want to specify a delivery address for each products I order.".
  273. if (typeof(multishipping_mode) !== 'undefined' && multishipping_mode)
  274. {
  275. $('#multishipping_mode_checkbox').click();
  276. $('.addressesAreEquals').hide().find('input').attr('checked', false);
  277. }
  278. if (typeof(open_multishipping_fancybox) !== 'undefined' && open_multishipping_fancybox)
  279. $('#link_multishipping_form').click();
  280. });
  281. function updateCarrierList(json)
  282. {
  283. var html = json.carrier_block;
  284. $('#carrier_area').replaceWith(html);
  285. bindInputs();
  286. /* update hooks for carrier module */
  287. $('#HOOK_BEFORECARRIER').html(json.HOOK_BEFORECARRIER);
  288. }
  289. function updatePaymentMethods(json)
  290. {
  291. $('#HOOK_TOP_PAYMENT').html(json.HOOK_TOP_PAYMENT);
  292. $('#opc_payment_methods-content #HOOK_PAYMENT').html(json.HOOK_PAYMENT);
  293. }
  294. function updatePaymentMethodsDisplay()
  295. {
  296. var checked = '';
  297. if ($('#cgv:checked').length !== 0)
  298. checked = 1;
  299. else
  300. checked = 0;
  301. $('#opc_payment_methods-overlay').fadeIn('slow', function(){
  302. $.ajax({
  303. type: 'POST',
  304. headers: { "cache-control": "no-cache" },
  305. url: orderOpcUrl + '?rand=' + new Date().getTime(),
  306. async: true,
  307. cache: false,
  308. dataType : "json",
  309. data: 'ajax=true&method=updateTOSStatusAndGetPayments&checked=' + checked + '&token=' + static_token,
  310. success: function(json)
  311. {
  312. updatePaymentMethods(json);
  313. if (typeof bindUniform !=='undefined')
  314. bindUniform();
  315. }
  316. });
  317. $(this).fadeOut('slow');
  318. });
  319. }
  320. function updateAddressSelection()
  321. {
  322. var idAddress_delivery = ($('#opc_id_address_delivery').length == 1 ? $('#opc_id_address_delivery').val() : $('#id_address_delivery').val());
  323. var idAddress_invoice = ($('#opc_id_address_invoice').length == 1 ? $('#opc_id_address_invoice').val() : ($('#addressesAreEquals:checked').length == 1 ? idAddress_delivery : ($('#id_address_invoice').length == 1 ? $('#id_address_invoice').val() : idAddress_delivery)));
  324. $('#opc_account-overlay').fadeIn('slow');
  325. $('#opc_delivery_methods-overlay').fadeIn('slow');
  326. $('#opc_payment_methods-overlay').fadeIn('slow');
  327. $.ajax({
  328. type: 'POST',
  329. headers: { "cache-control": "no-cache" },
  330. url: orderOpcUrl + '?rand=' + new Date().getTime(),
  331. async: true,
  332. cache: false,
  333. dataType : "json",
  334. data: 'allow_refresh=1&ajax=true&method=updateAddressesSelected&id_address_delivery=' + idAddress_delivery + '&id_address_invoice=' + idAddress_invoice + '&token=' + static_token,
  335. success: function(jsonData)
  336. {
  337. if (jsonData.hasError)
  338. {
  339. var errors = '';
  340. for(var error in jsonData.errors)
  341. //IE6 bug fix
  342. if(error !== 'indexOf')
  343. errors += $('<div />').html(jsonData.errors[error]).text() + "\n";
  344. if (!!$.prototype.fancybox)
  345. $.fancybox.open([
  346. {
  347. type: 'inline',
  348. autoScale: true,
  349. minHeight: 30,
  350. content: '<p class="fancybox-error">' + errors + '</p>'
  351. }
  352. ], {
  353. padding: 0
  354. });
  355. else
  356. alert(errors);
  357. }
  358. else
  359. {
  360. if (jsonData.refresh)
  361. location.reload();
  362. // Update all product keys with the new address id
  363. $('#cart_summary .address_'+deliveryAddress).each(function() {
  364. $(this)
  365. .removeClass('address_'+deliveryAddress)
  366. .addClass('address_'+idAddress_delivery);
  367. $(this).attr('id', $(this).attr('id').replace(/_\d+$/, '_'+idAddress_delivery));
  368. if ($(this).find('.cart_unit span').length > 0 && $(this).find('.cart_unit span').attr('id').length > 0)
  369. $(this).find('.cart_unit span').attr('id', $(this).find('.cart_unit span').attr('id').replace(/_\d+$/, '_'+idAddress_delivery));
  370. if ($(this).find('.cart_total span').length > 0 && $(this).find('.cart_total span').attr('id').length > 0)
  371. $(this).find('.cart_total span').attr('id', $(this).find('.cart_total span').attr('id').replace(/_\d+$/, '_'+idAddress_delivery));
  372. if ($(this).find('.cart_quantity_input').length > 0 && $(this).find('.cart_quantity_input').attr('name').length > 0)
  373. {
  374. var name = $(this).find('.cart_quantity_input').attr('name')+'_hidden';
  375. $(this).find('.cart_quantity_input').attr('name', $(this).find('.cart_quantity_input').attr('name').replace(/_\d+$/, '_'+idAddress_delivery));
  376. if ($(this).find('[name="' + name + '"]').length > 0)
  377. $(this).find('[name="' + name +'"]').attr('name', name.replace(/_\d+_hidden$/, '_'+idAddress_delivery+'_hidden'));
  378. }
  379. if ($(this).find('.cart_quantity_delete').length > 0 && $(this).find('.cart_quantity_delete').attr('id').length > 0)
  380. {
  381. $(this).find('.cart_quantity_delete')
  382. .attr('id', $(this).find('.cart_quantity_delete').attr('id').replace(/_\d+$/, '_'+idAddress_delivery))
  383. .attr('href', $(this).find('.cart_quantity_delete').attr('href').replace(/id_address_delivery=\d+&/, 'id_address_delivery='+idAddress_delivery+'&'));
  384. }
  385. if ($(this).find('.cart_quantity_down').length > 0 && $(this).find('.cart_quantity_down').attr('id').length > 0)
  386. {
  387. $(this).find('.cart_quantity_down')
  388. .attr('id', $(this).find('.cart_quantity_down').attr('id').replace(/_\d+$/, '_'+idAddress_delivery))
  389. .attr('href', $(this).find('.cart_quantity_down').attr('href').replace(/id_address_delivery=\d+&/, 'id_address_delivery='+idAddress_delivery+'&'));
  390. }
  391. if ($(this).find('.cart_quantity_up').length > 0 && $(this).find('.cart_quantity_up').attr('id').length > 0)
  392. {
  393. $(this).find('.cart_quantity_up')
  394. .attr('id', $(this).find('.cart_quantity_up').attr('id').replace(/_\d+$/, '_'+idAddress_delivery))
  395. .attr('href', $(this).find('.cart_quantity_up').attr('href').replace(/id_address_delivery=\d+&/, 'id_address_delivery='+idAddress_delivery+'&'));
  396. }
  397. });
  398. // Update global var deliveryAddress
  399. deliveryAddress = idAddress_delivery;
  400. if (window.ajaxCart !== undefined)
  401. {
  402. $('.cart_block_list dd, .cart_block_list dt').each(function(){
  403. if (typeof($(this).attr('id')) != 'undefined')
  404. $(this).attr('id', $(this).attr('id').replace(/_\d+$/, '_' + idAddress_delivery));
  405. });
  406. }
  407. updateCarrierList(jsonData.carrier_data);
  408. updatePaymentMethods(jsonData);
  409. updateCartSummary(jsonData.summary);
  410. updateHookShoppingCart(jsonData.HOOK_SHOPPING_CART);
  411. updateHookShoppingCartExtra(jsonData.HOOK_SHOPPING_CART_EXTRA);
  412. if ($('#gift-price').length == 1)
  413. $('#gift-price').html(jsonData.gift_price);
  414. $('#opc_account-overlay, #opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
  415. }
  416. },
  417. error: function(XMLHttpRequest, textStatus, errorThrown) {
  418. if (textStatus !== 'abort')
  419. {
  420. error = "TECHNICAL ERROR: unable to save adresses \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus;
  421. if (!!$.prototype.fancybox)
  422. $.fancybox.open([
  423. {
  424. type: 'inline',
  425. autoScale: true,
  426. minHeight: 30,
  427. content: '<p class="fancybox-error">' + error + '</p>'
  428. }
  429. ], {
  430. padding: 0
  431. });
  432. else
  433. alert(error);
  434. }
  435. $('#opc_account-overlay, #opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
  436. }
  437. });
  438. }
  439. function getCarrierListAndUpdate()
  440. {
  441. $('#opc_delivery_methods-overlay').fadeIn('slow');
  442. $.ajax({
  443. type: 'POST',
  444. headers: { "cache-control": "no-cache" },
  445. url: orderOpcUrl + '?rand=' + new Date().getTime(),
  446. async: true,
  447. cache: false,
  448. dataType : "json",
  449. data: 'ajax=true&method=getCarrierList&token=' + static_token,
  450. success: function(jsonData)
  451. {
  452. if (jsonData.hasError)
  453. {
  454. var errors = '';
  455. for(var error in jsonData.errors)
  456. //IE6 bug fix
  457. if(error !== 'indexOf')
  458. errors += $('<div />').html(jsonData.errors[error]).text() + "\n";
  459. if (!!$.prototype.fancybox)
  460. {
  461. $.fancybox.open([
  462. {
  463. type: 'inline',
  464. autoScale: true,
  465. minHeight: 30,
  466. content: '<p class="fancybox-error">' + errors + '</p>'
  467. }
  468. ], {
  469. padding: 0
  470. });
  471. }
  472. else
  473. {
  474. if (!!$.prototype.fancybox)
  475. $.fancybox.open([
  476. {
  477. type: 'inline',
  478. autoScale: true,
  479. minHeight: 30,
  480. content: '<p class="fancybox-error">' + errors + '</p>'
  481. }
  482. ], {
  483. padding: 0
  484. });
  485. else
  486. alert(errors);
  487. }
  488. }
  489. else
  490. updateCarrierList(jsonData);
  491. $('#opc_delivery_methods-overlay').fadeOut('slow');
  492. }
  493. });
  494. }
  495. function updateCarrierSelectionAndGift()
  496. {
  497. var recyclablePackage = 0;
  498. var gift = 0;
  499. var giftMessage = '';
  500. var delivery_option_radio = $('.delivery_option_radio');
  501. var delivery_option_params = '&';
  502. $.each(delivery_option_radio, function(i) {
  503. if ($(this).prop('checked'))
  504. delivery_option_params += $(delivery_option_radio[i]).attr('name') + '=' + $(delivery_option_radio[i]).val() + '&';
  505. });
  506. if (delivery_option_params == '&')
  507. delivery_option_params = '&delivery_option=&';
  508. if ($('input#recyclable:checked').length)
  509. recyclablePackage = 1;
  510. if ($('input#gift:checked').length)
  511. {
  512. gift = 1;
  513. giftMessage = encodeURIComponent($('#gift_message').val());
  514. }
  515. $('#opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
  516. $.ajax({
  517. type: 'POST',
  518. headers: { "cache-control": "no-cache" },
  519. url: orderOpcUrl + '?rand=' + new Date().getTime(),
  520. async: true,
  521. cache: false,
  522. dataType : "json",
  523. data: 'ajax=true&method=updateCarrierAndGetPayments' + delivery_option_params + 'recyclable=' + recyclablePackage + '&gift=' + gift + '&gift_message=' + giftMessage + '&token=' + static_token ,
  524. success: function(jsonData)
  525. {
  526. if (jsonData.hasError)
  527. {
  528. var errors = '';
  529. for(var error in jsonData.errors)
  530. //IE6 bug fix
  531. if(error !== 'indexOf')
  532. errors += $('<div />').html(jsonData.errors[error]).text() + "\n";
  533. if (!!$.prototype.fancybox)
  534. $.fancybox.open([
  535. {
  536. type: 'inline',
  537. autoScale: true,
  538. minHeight: 30,
  539. content: '<p class="fancybox-error">' + errors + '</p>'
  540. }
  541. ], {
  542. padding: 0
  543. });
  544. else
  545. alert(errors);
  546. }
  547. else
  548. {
  549. updateCartSummary(jsonData.summary);
  550. updatePaymentMethods(jsonData);
  551. updateHookShoppingCart(jsonData.summary.HOOK_SHOPPING_CART);
  552. updateHookShoppingCartExtra(jsonData.summary.HOOK_SHOPPING_CART_EXTRA);
  553. updateCarrierList(jsonData.carrier_data);
  554. $('#opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
  555. refreshDeliveryOptions();
  556. if (typeof bindUniform !=='undefined')
  557. bindUniform();
  558. }
  559. },
  560. error: function(XMLHttpRequest, textStatus, errorThrown) {
  561. if (textStatus !== 'abort')
  562. alert("TECHNICAL ERROR: unable to save carrier \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
  563. $('#opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
  564. }
  565. });
  566. }
  567. function confirmFreeOrder()
  568. {
  569. if ($('#opc_new_account-overlay').length !== 0)
  570. $('#opc_new_account-overlay').fadeIn('slow');
  571. else
  572. $('#opc_account-overlay').fadeIn('slow');
  573. $('#opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
  574. $('#confirmOrder').prop('disabled', 'disabled');
  575. $.ajax({
  576. type: 'POST',
  577. headers: { "cache-control": "no-cache" },
  578. url: orderOpcUrl + '?rand=' + new Date().getTime(),
  579. async: true,
  580. cache: false,
  581. dataType : "html",
  582. data: 'ajax=true&method=makeFreeOrder&token=' + static_token ,
  583. success: function(html)
  584. {
  585. $('#confirmOrder').prop('disabled', false);
  586. var array_split = html.split(':');
  587. if (array_split[0] == 'freeorder')
  588. {
  589. if (isGuest)
  590. document.location.href = guestTrackingUrl+'?id_order='+encodeURIComponent(array_split[1])+'&email='+encodeURIComponent(array_split[2]);
  591. else
  592. document.location.href = historyUrl;
  593. }
  594. },
  595. error: function(XMLHttpRequest, textStatus, errorThrown) {
  596. if (textStatus !== 'abort')
  597. {
  598. error = "TECHNICAL ERROR: unable to confirm the order \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus;
  599. if (!!$.prototype.fancybox)
  600. $.fancybox.open([
  601. {
  602. type: 'inline',
  603. autoScale: true,
  604. minHeight: 30,
  605. content: '<p class="fancybox-error">' + error + '</p>'
  606. }
  607. ], {
  608. padding: 0
  609. });
  610. else
  611. alert(error);
  612. }
  613. }
  614. });
  615. }
  616. function saveAddress(type)
  617. {
  618. if (type !== 'delivery' && type !== 'invoice')
  619. return false;
  620. var params = 'firstname=' + encodeURIComponent($('#firstname' + (type == 'invoice' ? '_invoice' : '')).val()) + '&lastname=' + encodeURIComponent($('#lastname'+(type == 'invoice' ? '_invoice' : '')).val()) + '&';
  621. if ($('#company' + (type == 'invoice' ? '_invoice' : '')).length)
  622. params += 'company=' + encodeURIComponent($('#company' + (type == 'invoice' ? '_invoice' : '')).val()) + '&';
  623. if ($('#vat_number' + (type == 'invoice' ? '_invoice' : '')).length)
  624. params += 'vat_number='+encodeURIComponent($('#vat_number' + (type == 'invoice' ? '_invoice' : '')).val()) + '&';
  625. if ($('#dni'+(type == 'invoice' ? '_invoice' : '')).length)
  626. params += 'dni=' + encodeURIComponent($('#dni' + (type == 'invoice' ? '_invoice' : '')).val())+'&';
  627. params += 'address1=' + encodeURIComponent($('#address1' + (type == 'invoice' ? '_invoice' : '')).val()) + '&';
  628. params += 'address2=' + encodeURIComponent($('#address2' + (type == 'invoice' ? '_invoice' : '')).val()) + '&';
  629. params += 'postcode=' + encodeURIComponent($('#postcode' + (type == 'invoice' ? '_invoice' : '')).val()) + '&';
  630. params += 'city=' + encodeURIComponent($('#city' + (type == 'invoice' ? '_invoice' : '')).val()) + '&';
  631. params += 'id_country=' + parseInt($('#id_country' + (type == 'invoice' ? '_invoice' : '')).val()) + '&';
  632. if ($('#id_state'+(type == 'invoice' ? '_invoice' : '')).length)
  633. params += 'id_state='+encodeURIComponent($('#id_state'+(type == 'invoice' ? '_invoice' : '')).val()) + '&';
  634. params += 'other=' + encodeURIComponent($('#other' + (type == 'invoice' ? '_invoice' : '')).val()) + '&';
  635. params += 'phone=' + encodeURIComponent($('#phone' + (type == 'invoice' ? '_invoice' : '')).val()) + '&';
  636. params += 'phone_mobile=' + encodeURIComponent($('#phone_mobile' + (type == 'invoice' ? '_invoice' : '')).val()) + '&';
  637. params += 'alias=' + encodeURIComponent($('#alias' + (type == 'invoice' ? '_invoice' : '')).val()) + '&';
  638. if (type == 'delivery' && $('#opc_id_address_delivery').val() != undefined && parseInt($('#opc_id_address_delivery').val()) > 0)
  639. params += 'opc_id_address_delivery=' + parseInt($('#opc_id_address_delivery').val()) + '&';
  640. if (type == 'invoice' && $('#opc_id_address_invoice').val() != undefined && parseInt($('#opc_id_address_invoice').val()) > 0)
  641. params += 'opc_id_address_invoice=' + parseInt($('#opc_id_address_invoice').val()) + '&';
  642. // Clean the last &
  643. params = params.substr(0, params.length-1);
  644. var result = false;
  645. $.ajax({
  646. type: 'POST',
  647. headers: { "cache-control": "no-cache" },
  648. url: addressUrl + '?rand=' + new Date().getTime(),
  649. async: false,
  650. cache: false,
  651. dataType : "json",
  652. data: 'ajax=true&submitAddress=true&type='+type+'&'+params+'&token=' + static_token,
  653. success: function(jsonData)
  654. {
  655. if (jsonData.hasError)
  656. {
  657. var tmp = '';
  658. var i = 0;
  659. for(var error in jsonData.errors)
  660. //IE6 bug fix
  661. if(error !== 'indexOf')
  662. {
  663. i = i+1;
  664. tmp += '<li>'+jsonData.errors[error]+'</li>';
  665. }
  666. tmp += '</ol>';
  667. var errors = '<b>'+txtThereis+' '+i+' '+txtErrors+':</b><ol>'+tmp;
  668. $('#opc_account_errors').slideUp('fast', function(){
  669. $(this).html(errors).slideDown('slow', function(){
  670. $.scrollTo('#opc_account_errors', 800);
  671. });
  672. });
  673. $('#opc_account-overlay, #opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
  674. result = false;
  675. }
  676. else
  677. {
  678. // update addresses id
  679. $('input#opc_id_address_delivery').val(jsonData.id_address_delivery);
  680. $('input#opc_id_address_invoice').val(jsonData.id_address_invoice);
  681. result = true;
  682. }
  683. },
  684. error: function(XMLHttpRequest, textStatus, errorThrown) {
  685. if (textStatus !== 'abort')
  686. {
  687. error = "TECHNICAL ERROR: unable to save adresses \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus;
  688. if (!!$.prototype.fancybox)
  689. $.fancybox.open([
  690. {
  691. type: 'inline',
  692. autoScale: true,
  693. minHeight: 30,
  694. content: '<p class="fancybox-error">' + error + '</p>'
  695. }
  696. ], {
  697. padding: 0
  698. });
  699. else
  700. alert(error);
  701. }
  702. $('#opc_account-overlay, #opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
  703. }
  704. });
  705. return result;
  706. }
  707. function updateNewAccountToAddressBlock()
  708. {
  709. $('#opc_account-overlay, #opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');;
  710. $.ajax({
  711. type: 'POST',
  712. headers: { "cache-control": "no-cache" },
  713. url: orderOpcUrl + '?rand=' + new Date().getTime(),
  714. async: true,
  715. cache: false,
  716. dataType : "json",
  717. data: 'ajax=true&method=getAddressBlockAndCarriersAndPayments&token=' + static_token ,
  718. success: function(json)
  719. {
  720. if (json.hasError)
  721. {
  722. var errors = '';
  723. for(var error in json.errors)
  724. //IE6 bug fix
  725. if(error !== 'indexOf')
  726. errors += $('<div />').html(json.errors[error]).text() + "\n";
  727. alert(errors);
  728. }
  729. else
  730. {
  731. isLogged = 1;
  732. if (json.no_address == 1)
  733. document.location.href = addressUrl;
  734. $('#opc_new_account').fadeOut('fast', function() {
  735. if (typeof json.formatedAddressFieldsValuesList !== 'undefined' && json.formatedAddressFieldsValuesList )
  736. formatedAddressFieldsValuesList = json.formatedAddressFieldsValuesList;
  737. if (typeof json.order_opc_adress !== 'undefined' && json.order_opc_adress)
  738. $('#opc_new_account').html(json.order_opc_adress);
  739. // update block user info
  740. if (json.block_user_info !== '' && $('#header_user').length == 1)
  741. {
  742. var elt = $(json.block_user_info).find('#header_user_info').html();
  743. $('#header_user_info').fadeOut('nortmal', function() {
  744. $(this).html(elt).fadeIn();
  745. });
  746. }
  747. $(this).fadeIn('fast', function() {
  748. //After login, the products are automatically associated to an address
  749. $.each(json.summary.products, function() {
  750. updateAddressId(this.id_product, this.id_product_attribute, '0', this.id_address_delivery);
  751. });
  752. updateAddressesDisplay(true);
  753. updateCarrierList(json.carrier_data);
  754. updateCarrierSelectionAndGift();
  755. updatePaymentMethods(json);
  756. if ($('#gift-price').length == 1)
  757. $('#gift-price').html(json.gift_price);
  758. $('#opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
  759. });
  760. });
  761. }
  762. },
  763. error: function(XMLHttpRequest, textStatus, errorThrown) {
  764. if (textStatus !== 'abort')
  765. alert("TECHNICAL ERROR: unable to send login informations \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
  766. $('#opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
  767. }
  768. });
  769. }
  770. function bindInputs()
  771. {
  772. // Order message update
  773. $('#message').blur(function() {
  774. $('#opc_delivery_methods-overlay').fadeIn('slow');
  775. $.ajax({
  776. type: 'POST',
  777. headers: { "cache-control": "no-cache" },
  778. url: orderOpcUrl + '?rand=' + new Date().getTime(),
  779. async: false,
  780. cache: false,
  781. dataType : "json",
  782. data: 'ajax=true&method=updateMessage&message=' + encodeURIComponent($('#message').val()) + '&token=' + static_token ,
  783. success: function(jsonData)
  784. {
  785. if (jsonData.hasError)
  786. {
  787. var errors = '';
  788. for(var error in jsonData.errors)
  789. //IE6 bug fix
  790. if(error !== 'indexOf')
  791. errors += $('<div />').html(jsonData.errors[error]).text() + "\n";
  792. alert(errors);
  793. }
  794. else
  795. $('#opc_delivery_methods-overlay').fadeOut('slow');
  796. },
  797. error: function(XMLHttpRequest, textStatus, errorThrown) {
  798. if (textStatus !== 'abort')
  799. alert("TECHNICAL ERROR: unable to save message \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
  800. $('#opc_delivery_methods-overlay').fadeOut('slow');
  801. }
  802. });
  803. if (typeof bindUniform !=='undefined')
  804. bindUniform();
  805. });
  806. // Recyclable checkbox
  807. $('#recyclable').on('click', function(e){
  808. updateCarrierSelectionAndGift();
  809. });
  810. // Gift checkbox update
  811. $('#gift').off('click').on('click', function(e){
  812. if ($('#gift').is(':checked'))
  813. $('#gift_div').show();
  814. else
  815. $('#gift_div').hide();
  816. updateCarrierSelectionAndGift();
  817. });
  818. if ($('#gift').is(':checked'))
  819. $('#gift_div').show();
  820. else
  821. $('#gift_div').hide();
  822. // Gift message update
  823. $('#gift_message').on('change', function() {
  824. updateCarrierSelectionAndGift();
  825. });
  826. // Term Of Service (TOS)
  827. $('#cgv').on('click', function(e){
  828. updatePaymentMethodsDisplay();
  829. });
  830. }
  831. function multishippingMode(it)
  832. {
  833. if ($(it).prop('checked'))
  834. {
  835. $('#address_delivery, .address_delivery').hide();
  836. $('#address_delivery, .address_delivery').parent().hide();
  837. $('#address_invoice').removeClass('alternate_item').addClass('item');
  838. $('#multishipping_mode_box').addClass('on');
  839. $('.addressesAreEquals').hide();
  840. $('#address_invoice_form').show();
  841. $(document).on('click', '#link_multishipping_form', function(e){e.preventDefault();});
  842. $('.address_add a').attr('href', addressMultishippingUrl);
  843. $(document).on('click', '#link_multishipping_form', function(e){
  844. if(!!$.prototype.fancybox)
  845. $.fancybox({
  846. 'openEffect': 'elastic',
  847. 'closeEffect': 'elastic',
  848. 'type': 'ajax',
  849. 'href': this.href,
  850. 'beforeClose': function(){
  851. // Reload the cart
  852. $.ajax({
  853. type: 'POST',
  854. headers: { "cache-control": "no-cache" },
  855. url: orderOpcUrl + '?rand=' + new Date().getTime(),
  856. data: 'ajax=true&method=cartReload',
  857. dataType : 'html',
  858. cache: false,
  859. success: function(data) {
  860. $('#cart_summary').replaceWith($(data).find('#cart_summary'));
  861. $('.cart_quantity_input').typeWatch({ highlight: true, wait: 600, captureLength: 0, callback: function(val) { updateQty(val, true, this.el); } });
  862. }
  863. });
  864. updateCarrierSelectionAndGift();
  865. },
  866. 'beforeLoad': function(){
  867. // Removing all ids on the cart to avoid conflic with the new one on the fancybox
  868. // This action could "break" the cart design, if css rules use ids of the cart
  869. $.each($('#cart_summary *'), function(it, el) {
  870. $(el).attr('id', '');
  871. });
  872. },
  873. 'afterLoad': function(){
  874. $('.fancybox-inner .cart_quantity_input').typeWatch({ highlight: true, wait: 600, captureLength: 0, callback: function(val) { updateQty(val, false, this.el);} });
  875. cleanSelectAddressDelivery();
  876. $('.fancybox-outer').append($('<div class="multishipping_close_container"><a id="multishipping-close" class="btn btn-default button button-small" href="#"><span>'+CloseTxt+'</span></a></div>'));
  877. $(document).on('click', '#multishipping-close', function(e){
  878. var newTotalQty = 0;
  879. $('.fancybox-inner .cart_quantity_input').each(function(){
  880. newTotalQty += parseInt($(this).val());
  881. });
  882. if (newTotalQty !== totalQty) {
  883. if(!confirm(QtyChanged)) {
  884. return false;
  885. }
  886. }
  887. $.fancybox.close();
  888. return false;
  889. });
  890. totalQty = 0;
  891. $('.fancybox-inner .cart_quantity_input').each(function(){
  892. totalQty += parseInt($(this).val());
  893. });
  894. }
  895. });
  896. });
  897. }
  898. else
  899. {
  900. $('#address_delivery, .address_delivery').show();
  901. $('#address_invoice').removeClass('item').addClass('alternate_item');
  902. $('#multishipping_mode_box').removeClass('on');
  903. $('.addressesAreEquals').show();
  904. if ($('.addressesAreEquals').find('input:checked').length)
  905. $('#address_invoice_form').hide();
  906. else
  907. $('#address_invoice_form').show();
  908. $('.address_add a').attr('href', addressUrl);
  909. // Disable multi address shipping
  910. $.ajax({
  911. type: 'POST',
  912. headers: { "cache-control": "no-cache" },
  913. url: orderOpcUrl + '?rand=' + new Date().getTime(),
  914. async: true,
  915. cache: false,
  916. data: 'ajax=true&method=noMultiAddressDelivery'
  917. });
  918. // Reload the cart
  919. $.ajax({
  920. type: 'POST',
  921. headers: { "cache-control": "no-cache" },
  922. url: orderOpcUrl + '?rand=' + new Date().getTime(),
  923. async: true,
  924. cache: false,
  925. data: 'ajax=true&method=cartReload',
  926. dataType : 'html',
  927. success: function(data) {
  928. $('#cart_summary').replaceWith($(data).find('#cart_summary'));
  929. }
  930. });
  931. }
  932. if (typeof bindUniform !=='undefined')
  933. bindUniform();
  934. }