/**
* 2007-2015 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-2015 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
$(document).ready(function () {
$('body').prepend($('.ocpc_stage, .one_click_product_checkout').remove());
$('#submitOneClickCheckout').live('click', function () {
var self = this;
var form = $(self).closest('.one_click_product_checkout');
var id_product_attribute = parseInt(form.find('[name="id_product_attribute"]').val());
if (id_product_attribute > 0 && !ocpc_combinations[id_product_attribute].available_for_order)
return false;
form.find('._loader').fadeIn(500);
setTimeout(function () {
$.ajax({
url: document.location.href,
type: 'POST',
dataType: 'json',
data: getDataAuth(form, 'submitOneClickCheckout', 'oneclickproductcheckout'),
success: function (r)
{
form.find('._loader').fadeOut(500);
hideAllErrorBox();
if(r.hasError)
{
var errors = '<li>' + r.errors.join('</li><li>') + '</li>';
showError(form, errors);
}
else
{
setSuccess(form);
$('.one_click_product_checkout').setCenterPosAbsBlockOCPC();
setTimeout(function(){$('.one_click_product_checkout, .ocpc_stage').fadeOut('fast')},3000);
}
},
error: function ()
{
showError(form,'<li>Has unknown error</li>');
form.find('._loader').fadeOut(500);
}
});
},500);
});
$('#quantity').live('keyup', function () {
var price = $(this).data('price');
var quantity = parseInt($(this).val());
$(this).val((!isNaN(quantity) ? quantity : ''));
var form = $(this).closest('.one_click_product_checkout');
form.find('.total_price').text(formatCurrency((price * (!isNaN(quantity) ? quantity : 0)), currencyFormat, currencySign, currencyBlank));
});
$('#quantity').live('blur', function () {
var quantity = parseInt($(this).val());
$(this).val((!isNaN(quantity) ? quantity : 1));
$(this).trigger('keyup');
});
$('#showOneClickCheckout').live('click', function (e) {
e.preventDefault();
$('.one_click_product_checkout, .ocpc_stage').fadeIn(500);
$('.one_click_product_checkout').setCenterPosAbsBlockOCPC();
});
$('#cancelOneClickCheckout, .ocpc_stage').live('click', function (e) {
e.preventDefault();
$('.one_click_product_checkout, .ocpc_stage').fadeOut(500);
});
$('.incrementQuantity').live('click', function (e) {
e.preventDefault();
$(this).closest('.wrapper_quantity').find('[id=quantity]').val(parseInt($(this).closest('.wrapper_quantity').find('[id=quantity]').val()) + 1);
$(this).closest('.wrapper_quantity').find('[id=quantity]').trigger('keyup');
});
$('.decrementQuantity').live('click', function (e) {
e.preventDefault();
if (parseInt($(this).closest('.wrapper_quantity').find('[id=quantity]').val()) > 1){
$(this).closest('.wrapper_quantity').find('[id=quantity]').val(parseInt($(this).closest('.wrapper_quantity').find('[id=quantity]').val()) - 1);
$(this).closest('.wrapper_quantity').find('[id=quantity]').trigger('keyup');
}
});
if (typeof $.fn.setCenterPosAbsBlockOCPC == 'undefined')
$.fn.setCenterPosAbsBlockOCPC = function ()
{
var offsetElemTop = 20;
var scrollTop = $(document).scrollTop();
var elemWidth = $(this).width();
var windowWidth = $(window).width();
$(this).css({
top: ($(this).height() > $(window).height() ? scrollTop + offsetElemTop : scrollTop + (($(window).height()-$(this).height())/2)),
left: ((windowWidth-elemWidth)/2)
});
};
$(window).resize(function () {
$('.one_click_product_checkout').setCenterPosAbsBlockOCPC();
});
$('.one_click_product_checkout').setCenterPosAbsBlockOCPC();
})(jQuery);
function getDataAuth(form, method, module)
{
data = {};
data.ajax = true;
data.method = method;
data.module = module;
data.qty = form.find('#quantity').val();
data.id_product_attribute = form.find('[name="id_product_attribute"]').val();
var fields = fields_json;
for(item in fields)
{
if(!fields[item].visible)
continue;
data[fields[item].name] = $('#ocpc_' + fields[item].name).val();
}
return data;
}
function showError(form,text)
{
var error_box = form.find('._error');
error_box.html('<ul>' + text + '</ul>');
error_box.show();
error_box.css({
height: 'auto',
overflow: 'visible'
});
}
function hideAllErrorBox()
{
$('._error').hide();
}
function setSuccess(form)
{
form.html('<div class="success_message">' + success_message + '</div>');
}