Advertisement
Guest User

order-adress

a guest
Jul 1st, 2014
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var checkedFound = false;
  2. var bNewCountry, bOldCountry;
  3.  
  4. $(document).ready(function() {
  5.     $('fieldset#address_delivery select.id_country').bind('change', function() {
  6.         $('.countryPreloader').show();
  7.         updateAddresses();
  8.     });
  9.  
  10.     var value = Get_Cookie('forRedirecting');
  11.     if (value === 'yes') {
  12.         var date = new Date();
  13.         date.setTime(date.getTime() + (100 * 24 * 60 * 60 * 1000));
  14.         var expires = "expires=" + date.toGMTString();
  15.         document.cookie = 'forRedirecting=' + "no" + ';' + expires + '; path=/';
  16.         window.location = baseDir + "order.php/?step=2";
  17.     }
  18.  
  19.     updateAddressesDisplay(true, '');
  20.  
  21.     $('table#carrierType input[type|="radio"]:checked').each(showShipping);
  22.     $('table#carrierType input[type|="radio"]').bind('click', showShipping);
  23.     if (!checkedFound) {
  24.         fillEmptyFields();
  25.     }
  26.  
  27.     $('#carrierType input').not('#carrierType input#id_carrier60').bind('click', function() {
  28.         $('#shippingAddress').show();
  29.     });
  30.  
  31.     $('#id_carrier60').bind('click', function() {
  32.         $('#shippingAddress').hide();
  33.     });
  34.  
  35.     $('#carrierType input').each(function() {
  36.         if ($(this).attr("id") !== 'id_carrier60' && $(this).attr('checked')) {
  37.             $('#shippingAddress').show();
  38.         }
  39.     });
  40.  
  41. //    updateAddressDisplay('delivery', true);
  42. });
  43. function swapSelectList(type) {
  44.     var totype = type === 'delivery' ? 'invoice' : 'delivery',
  45.             sel1 = $('select#id_address_' + type + ' option:selected'),
  46.             sel2 = $('select#id_address_' + totype + ' option:selected');
  47.     if (sel1.val() === sel2.val()) {
  48.         sel2.attr('selected', false);
  49.         if (sel2.next() === sel2) {
  50.             sel2.first().attr('selected', true);
  51.         } else {
  52.             sel2.next().attr('selected', true);
  53.         }
  54.         updateAddressDisplay(totype);
  55.     }
  56. }
  57. function showShipping(thisOne) {
  58.     checkedFound = true;
  59.     if ($(this).val() !== '40') {
  60.         $('div#hidePart').show();
  61.         $('div#shippingAddress input[type|="text"]').each(function() {
  62.             var v = $(this).val();
  63.             if ((v === ' --- ') || (v === '-')) {
  64.                 $(this).attr('value', '');
  65.             }
  66.         });
  67.     } else {
  68.         $('div#hidePart').hide();
  69.         fillEmptyFields();
  70.     }
  71. }
  72.  
  73. function fillEmptyFields() {
  74.     $('div#shippingAddress input[type|="text"]').each(function() {
  75.         if ($(this).val() === '') {
  76.             $(this).attr('value', ' --- ');
  77.         }
  78.     });
  79. }
  80.  
  81. function updateAddressesDisplay(first_view, address_type) {
  82.     if ($('input[type=checkbox]#addressesAreEquals:visible').is(':checked')) {
  83.         $('fieldset#address_invoice:visible').hide('fast');
  84.     } else {
  85.         $('fieldset#address_invoice:hidden').show('fast');
  86.     }
  87.  
  88.     if (typeof address_type !== "undefined") {
  89.         if (address_type === '' && first_view) {
  90.             updateAddressDisplay('delivery', true);
  91.             updateAddressDisplay('invoice', true);
  92.         } else {
  93.             updateAddressDisplay(address_type, false);
  94.         }
  95.  
  96.         $('div#shippingAddress input[type|="text"]').each(function() {
  97.             var v = $(this).val();
  98.             if ((v === ' --- ') || (v === '-')) {
  99.                 $(this).attr('value', '');
  100.             }
  101.         });
  102.  
  103.         if (!first_view && address_type === 'delivery'  && bNewCountry !== bOldCountry) {
  104.             updateAddresses();
  105.         }
  106.     }
  107.  
  108.     return true;
  109. }
  110.  
  111. function updateAddressDisplay(addressType, first_view) {
  112.     if (typeof addresses === "undefined") {
  113.         return false;
  114.     }
  115.     if (typeof submitedForm !== "undefined" && submitedForm && first_view) {
  116.         return true;
  117.     }
  118.     bOldCountry = $('fieldset#address_delivery select.id_country option:selected').val();
  119.     var idAddress = $('select#id_address_' + addressType + '').val();
  120.     $('fieldset#address_' + addressType + ' input.id_address').attr('value', idAddress);
  121.     $('fieldset#address_' + addressType + ' input.firstname').attr('value', addresses[idAddress][1]);
  122.     $('fieldset#address_' + addressType + ' input.lastname').attr('value', addresses[idAddress][2]);
  123.     $('fieldset#address_' + addressType + ' input.alias').attr('value', addresses[idAddress][9]);
  124.     $('fieldset#address_' + addressType + ' select.id_country option').each(function() {
  125.         $(this).attr('selected', $(this).text() === addresses[idAddress][7]);
  126.     });
  127.     $('fieldset#address_' + addressType + ' input.city').attr('value', addresses[idAddress][6]);
  128.     $('fieldset#address_' + addressType + ' input.postcode').attr('value', addresses[idAddress][5]);
  129.     $('fieldset#address_' + addressType + ' input.address1').attr('value', addresses[idAddress][3]);
  130.     $('fieldset#address_' + addressType + ' input.phone_mobile').attr('value', addresses[idAddress][4]);
  131.     $('fieldset#address_' + addressType + ' textarea.other').val(addresses[idAddress][8]);
  132.  
  133.     $('fieldset#address_' + addressType + ' input.company').attr('value', addresses[idAddress][0]);
  134.     $('fieldset#address_' + addressType + ' input.company_code').attr('value', addresses[idAddress][10]);
  135.     $('fieldset#address_' + addressType + ' input.vat_code').attr('value', addresses[idAddress][11]);
  136.  
  137.     var link = $('fieldset#address_' + addressType + ' a.address_update').attr('href');
  138.     var expression = /id_address=\d+/;
  139.     link = link.replace(expression, 'id_address=' + idAddress);
  140.     $('fieldset#address_' + addressType + ' a.address_update').attr('href', link);
  141.     bNewCountry = $('fieldset#address_delivery select.id_country option:selected').val();
  142.  
  143.     swapSelectList(addressType);
  144. }
  145.  
  146. function updateAddresses() {
  147.     var idAddress_delivery = $('select#id_address_delivery').val();
  148.     var id_country = $('fieldset#address_delivery select.id_country option:selected').val();
  149.     $.ajax({
  150.         type: 'POST',
  151.         url: baseDir + 'order.php',
  152.         async: true,
  153.         cache: false,
  154.         dataType: "json",
  155.         data: 'processCountryChange=true&step=2&id_address=' + idAddress_delivery + '&id_country=' + id_country + '&token=' + static_token,
  156.         success: function(jsonData) {
  157.             if (jsonData.hasError) {
  158.                 var errors = '';
  159.                 for (error in jsonData.errors) {
  160.                     if (error !== 'indexOf') {
  161.                         errors += jsonData.errors[error] + "\n";
  162.                     }
  163.                 }
  164.             }
  165.             $('.countryPreloader').hide();
  166.             location.reload();
  167.         }, error: function(XMLHttpRequest, textStatus, errorThrown) {/*alert("TECHNICAL ERROR: unable to save adresses \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);*/
  168.         }
  169.     });
  170. }
  171. function Get_Cookie(check_name) {
  172.     var a_all_cookies = document.cookie.split(';');
  173.     var a_temp_cookie = '';
  174.     var cookie_name = '';
  175.     var cookie_value = '';
  176.     var b_cookie_found = false;
  177.     for (i = 0; i < a_all_cookies.length; i++) {
  178.         a_temp_cookie = a_all_cookies[i].split('=');
  179.         cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
  180.         if (cookie_name === check_name) {
  181.             b_cookie_found = true;
  182.             if (a_temp_cookie.length > 1) {
  183.                 cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
  184.             }
  185.             return cookie_value;
  186.             break;
  187.         }
  188.         a_temp_cookie = null;
  189.         cookie_name = '';
  190.     }
  191.     if (!b_cookie_found) {
  192.         return null;
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement