garancia/themes/default/js/order-address.js
2016-10-10 15:24:25 +02:00

457 lines
15 KiB
JavaScript
Executable File

/*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2013 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
function checkPR(){
var returnOK=true;
$('.delivery_options .delivery_option_radio:checked').each(function(){
if($(this).next().find('.delivery_option_title').html()=="Mondial Relay"){
returnOK=false;
if($('.PS_MRSelectedCarrier input:checked').length==1){
returnOK=true;
}
}
});
if(returnOK==false)
alert('Vous devrez sélectionner 1 point relais pour poursuivre');
return returnOK;
}
var refreshAddress=0;
$(document).ready(function()
{
if (typeof(formatedAddressFieldsValuesList) !== 'undefined')
updateAddressesDisplay(true);
resizeAddressesBox();
$('.edit_delivery').click(function(){
if($('.mondialRelay:visible').length==0){
var newHref=$('#address_delivery li.address_update a').attr('href');
document.location.href=newHref;
}
});
$('.edit_invoice').click(function(){
var newHref=$('#address_invoice li.address_update a').attr('href');
document.location.href=newHref;
});
$('.delivery_option_radio .delivery_option input,.delivery_option_radio .delivery_option label').click(function(){
$('.delivery_option').removeClass('selected');
$(this).parent().addClass('selected');
});
});
//update the display of the addresses
function updateAddressesDisplay(first_view)
{
// alert(first_view);
// alert('test');
// update content of delivery address
// updateAddressDisplay('delivery');
var txtInvoiceTitle = "";
try{
var adrs_titles = getAddressesTitles();
txtInvoiceTitle = adrs_titles.invoice;
}
catch (e)
{}
// update content of invoice address
//if addresses have to be equals...
// alert($('input[type=radio]#addressesAreEquals1:checked').length );
if ($('input[type=radio]#addressesAreEquals1:checked').length == 1)
{
$('ul#address_invoice').html($('ul#address_delivery').html());
$('#deliveryItems').slideUp();
// $('ul#address_invoice li.address_title').html(txtInvoiceTitle);
}
else
{
$('#address_invoice_form:hidden').show('fast');
if ($('#id_address_invoice').val()!=$('#id_address_delivery').val())
updateAddressDisplay('invoice');
else
{
$('ul#address_invoice').html($('ul#address_delivery').html());
// $('ul#address_invoice li.address_title').html(txtInvoiceTitle);
}
$('#deliveryItems').slideDown();
}
// if(!first_view)
// {
// if (orderProcess === 'order')
updateAddresses();
// }
return true;
}
function updateAddressDisplay(addressType)
{
if (formatedAddressFieldsValuesList.length <= 0)
return false;
var idAddress = parseInt($('#id_address_' + addressType + '').val());
// alert(idAddress);
if(addressType=="delivery")
updateAddressSelection(idAddress);
buildAddressBlock(idAddress, addressType, $('#address_' + addressType));
// change update link
var link = $('ul#address_' + addressType + ' li.address_update a').attr('href');
var expression = /id_address=\d+/;
if (link)
{
link = link.replace(expression, 'id_address=' + idAddress);
$('ul#address_' + addressType + ' li.address_update a').attr('href', link);
}
resizeAddressesBox();
}
function updateAddresses()
{
var idAddress_delivery = parseInt($('#id_address_delivery').val());
var idAddress_invoice = $('input[type=checkbox]#addressesAreEquals:checked').length === 1 ? idAddress_delivery : parseInt($('#id_address_invoice').val());
if(isNaN(idAddress_delivery) == false && isNaN(idAddress_invoice) == false)
$.ajax({
type: 'POST',
headers: { "cache-control": "no-cache" },
url: baseUri + '?rand=' + new Date().getTime(),
async: true,
cache: false,
dataType : "json",
data: {
processAddress: true,
step: 2,
ajax: 'true',
controller: 'order',
'multi-shipping': $('#id_address_delivery:hidden').length,
id_address_delivery: idAddress_delivery,
id_address_invoice: idAddress_invoice,
token: static_token
},
success: function(jsonData)
{
if(jsonData){
if (jsonData.hasError)
{
var errors = '';
for(var error in jsonData.errors)
// IE6 bug fix
if(error !== 'indexOf')
errors += jsonData.errors[error] + "\n";
alert(errors);
}
}
updateAddressDisplay('delivery');
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (textStatus !== 'abort')
console.log("TECHNICAL ERROR: unable to save adresses \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
}
});
resizeAddressesBox();
}
function updateCarrierList(json)
{
var html = json.carrier_block;
// @todo check with theme 1.4
//if ($('#HOOK_EXTRACARRIER').length == 0 && json.HOOK_EXTRACARRIER !== null && json.HOOK_EXTRACARRIER != undefined)
// html += json.HOOK_EXTRACARRIER;
// alert(html);
if(refreshAddress==1){
$('#carrier_area').replaceWith(html);
$('#carrier_area').fadeTo('fast',1);
}
bindInputs();
/* update hooks for carrier module */
$('#HOOK_BEFORECARRIER').html(json.HOOK_BEFORECARRIER);
}
function updateAddressSelection(deliveryAddress)
{
var idAddress_delivery = $('#id_address_delivery').val();
var idAddress_invoice = ($('#id_address_invoice').length == 1 ? $('#id_address_invoice').val() : ($('#addressesAreEquals:checked').length == 1 ? idAddress_delivery : ($('#id_address_invoice').length == 1 ? $('#id_address_invoice').val() : deliveryAddress)));
// alert(refreshAddress);
$('#carrier_area').fadeTo('fast',0.5);
if(refreshAddress==0){
}else{
}
// alert(idAddress_delivery);
// $('#opc_delivery_methods-overlay').fadeIn('slow');
// $('#opc_payment_methods-overlay').fadeIn('slow');
// alert(orderOpcUrl + '?rand=' + new Date().getTime()+'&ajax=true&method=updateAddressesSelected&id_address_delivery=' + idAddress_delivery + '&id_address_invoice=' + idAddress_invoice + '&token=' + static_token);
$.ajax({
type: 'POST',
headers: { "cache-control": "no-cache" },
url: orderOpcUrl + '?rand=' + new Date().getTime(),
async: true,
cache: false,
dataType : "json",
data: 'ajax=true&method=updateAddressesSelected&id_address_delivery=' + idAddress_delivery + '&id_address_invoice=' + idAddress_invoice + '&token=' + static_token,
success: function(jsonData)
{
if (jsonData.hasError)
{
var errors = '';
for(var error in jsonData.errors)
//IE6 bug fix
if(error !== 'indexOf')
errors += jsonData.errors[error] + "\n";
alert(errors);
}
else
{
// Update all product keys with the new address id
$('#cart_summary .address_'+deliveryAddress).each(function() {
$(this)
.removeClass('address_'+deliveryAddress)
.addClass('address_'+idAddress_delivery);
$(this).attr('id', $(this).attr('id').replace(/_\d+$/, '_'+idAddress_delivery));
if ($(this).find('.cart_unit span').length > 0 && $(this).find('.cart_unit span').attr('id').length > 0)
$(this).find('.cart_unit span').attr('id', $(this).find('.cart_unit span').attr('id').replace(/_\d+$/, '_'+idAddress_delivery));
if ($(this).find('.cart_total span').length > 0 && $(this).find('.cart_total span').attr('id').length > 0)
$(this).find('.cart_total span').attr('id', $(this).find('.cart_total span').attr('id').replace(/_\d+$/, '_'+idAddress_delivery));
if ($(this).find('.cart_quantity_input').length > 0 && $(this).find('.cart_quantity_input').attr('name').length > 0)
{
var name = $(this).find('.cart_quantity_input').attr('name')+'_hidden';
$(this).find('.cart_quantity_input').attr('name', $(this).find('.cart_quantity_input').attr('name').replace(/_\d+$/, '_'+idAddress_delivery));
if ($(this).find('[name='+name+']').length > 0)
$(this).find('[name='+name+']').attr('name', name.replace(/_\d+_hidden$/, '_'+idAddress_delivery+'_hidden'));
}
if ($(this).find('.cart_quantity_delete').length > 0 && $(this).find('.cart_quantity_delete').attr('id').length > 0)
{
$(this).find('.cart_quantity_delete')
.attr('id', $(this).find('.cart_quantity_delete').attr('id').replace(/_\d+$/, '_'+idAddress_delivery))
.attr('href', $(this).find('.cart_quantity_delete').attr('href').replace(/id_address_delivery=\d+&/, 'id_address_delivery='+idAddress_delivery+'&'));
}
if ($(this).find('.cart_quantity_down').length > 0 && $(this).find('.cart_quantity_down').attr('id').length > 0)
{
$(this).find('.cart_quantity_down')
.attr('id', $(this).find('.cart_quantity_down').attr('id').replace(/_\d+$/, '_'+idAddress_delivery))
.attr('href', $(this).find('.cart_quantity_down').attr('href').replace(/id_address_delivery=\d+&/, 'id_address_delivery='+idAddress_delivery+'&'));
}
if ($(this).find('.cart_quantity_up').length > 0 && $(this).find('.cart_quantity_up').attr('id').length > 0)
{
$(this).find('.cart_quantity_up')
.attr('id', $(this).find('.cart_quantity_up').attr('id').replace(/_\d+$/, '_'+idAddress_delivery))
.attr('href', $(this).find('.cart_quantity_up').attr('href').replace(/id_address_delivery=\d+&/, 'id_address_delivery='+idAddress_delivery+'&'));
}
});
// Update global var deliveryAddress
deliveryAddress = idAddress_delivery;
if (window.ajaxCart !== undefined)
{
$('#cart_block_list dd, #cart_block_list dt').each(function(){
if (typeof($(this).attr('id')) != 'undefined')
$(this).attr('id', $(this).attr('id').replace(/_\d+$/, '_' + idAddress_delivery));
});
}
// updatePaymentMethods(jsonData);
// updateCartSummary(jsonData.summary);
// updateHookShoppingCart(jsonData.HOOK_SHOPPING_CART);
// updateHookShoppingCartExtra(jsonData.HOOK_SHOPPING_CART_EXTRA);
// if ($('#gift-price').length == 1)
// $('#gift-price').html(jsonData.gift_price);
if(refreshAddress==0){
refreshAddress=1;
updateAddresses();
}else{
refreshAddress=0;
updateCarrierList(jsonData.carrier_data);
}
$('#carrier_area').fadeTo('fast',1);
// alert($('#id_address_' + addressType + '').val());
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (textStatus !== 'abort')
console.log("TECHNICAL ERROR: unable to save adresses \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
$('#opc_account-overlay, #opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
}
});
}
function updateCarrierSelectionAndGift()
{
var recyclablePackage = 0;
var gift = 0;
var giftMessage = '';
var delivery_option_radio = $('.delivery_option_radio');
var delivery_option_params = '&';
$.each(delivery_option_radio, function(i) {
if ($(this).prop('checked'))
delivery_option_params += $(delivery_option_radio[i]).attr('name') + '=' + $(delivery_option_radio[i]).val() + '&';
});
if (delivery_option_params == '&')
delivery_option_params = '&delivery_option=&';
if ($('input#recyclable:checked').length)
recyclablePackage = 1;
if ($('input#gift:checked').length)
{
gift = 1;
giftMessage = encodeURIComponent($('#gift_message').val());
}
$('#opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
// alert(orderOpcUrl + '?rand=' + new Date().getTime());
// alert('ajax=true&method=updateCarrierAndGetPayments' + delivery_option_params + 'recyclable=' + recyclablePackage + '&gift=' + gift + '&gift_message=' + giftMessage + '&token=' + static_token);
$.ajax({
type: 'POST',
headers: { "cache-control": "no-cache" },
url: orderOpcUrl + '?rand=' + new Date().getTime(),
async: true,
cache: false,
dataType : "json",
data: 'ajax=true&method=updateCarrierAndGetPayments' + delivery_option_params + 'recyclable=' + recyclablePackage + '&gift=' + gift + '&gift_message=' + giftMessage + '&token=' + static_token ,
success: function(jsonData)
{
// if (jsonData!=null && jsonData.hasError)
// {
// var errors = '';
// for(var error in jsonData.errors)
// if(error !== 'indexOf')
// errors += jsonData.errors[error] + "\n";
// alert(errors);
// }
// else
// {
// updateCartSummary(jsonData.summary);
// updatePaymentMethods(jsonData);
// updateHookShoppingCart(jsonData.summary.HOOK_SHOPPING_CART);
// updateHookShoppingCartExtra(jsonData.summary.HOOK_SHOPPING_CART_EXTRA);
updateCarrierList(jsonData.carrier_data);
// $('#opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
// refreshDeliveryOptions();
// }
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (textStatus !== 'abort')
console.log("TECHNICAL ERROR: unable to save carrier \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
$('#opc_delivery_methods-overlay, #opc_payment_methods-overlay').fadeOut('slow');
}
});
}
function bindCheckbox()
{
if ($('#invoice_address:checked').length > 0)
{
$('#opc_invoice_address').slideDown('slow');
if ($('#company_invoice').val() == '')
$('#vat_number_block_invoice').hide();
updateState('invoice');
updateNeedIDNumber('invoice');
updateZipCode('invoice');
}
else
$('#opc_invoice_address').slideUp('slow');
}
function bindInputs()
{
// Order message update
$('#message').blur(function() {
$('#opc_delivery_methods-overlay').fadeIn('slow');
$.ajax({
type: 'POST',
headers: { "cache-control": "no-cache" },
url: orderOpcUrl + '?rand=' + new Date().getTime(),
async: false,
cache: false,
dataType : "json",
data: 'ajax=true&method=updateMessage&message=' + encodeURIComponent($('#message').val()) + '&token=' + static_token ,
success: function(jsonData)
{
if (jsonData.hasError)
{
var errors = '';
for(var error in jsonData.errors)
//IE6 bug fix
if(error !== 'indexOf')
errors += jsonData.errors[error] + "\n";
alert(errors);
}
else
$('#opc_delivery_methods-overlay').fadeOut('slow');
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (textStatus !== 'abort')
console.log("TECHNICAL ERROR: unable to save message \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
$('#opc_delivery_methods-overlay').fadeOut('slow');
}
});
});
// Recyclable checkbox
$('#recyclable').click(function() {
updateCarrierSelectionAndGift();
});
// Gift checkbox update
$('#gift').click(function() {
if ($('#gift').is(':checked'))
$('#gift_div').show();
else
$('#gift_div').hide();
updateCarrierSelectionAndGift();
});
if ($('#gift').is(':checked'))
$('#gift_div').show();
else
$('#gift_div').hide();
// Gift message update
$('#gift_message').change(function() {
updateCarrierSelectionAndGift();
});
// Term Of Service (TOS)
$('#cgv').click(function() {
updatePaymentMethodsDisplay();
});
}