File: //proc/self/cwd/nueva/modules/roja45quotationsprofree/views/js/roja45quotationspro.js
/**
* roja45quotationsprofree.js.
*
* @author Roja45
* @copyright 2016 Roja45
* @license license.txt
*
* 2016 ROJA45 - All rights reserved.
*
* DISCLAIMER
* Changing this file will render any support provided by us null and void.
*/
$(document).ready(function () {
$('li .product-container .roja45quotationsprofree.product.enabled').each(function (index, element) {
var id_product = parseInt($(this).attr('data-id-product'));
var id_product_attribute = parseInt($(this).attr('data-id-product-attribute'));
var minimal_quantity = parseInt($(this).attr('data-minimal-quantity'));
var button = $(this).closest('.product-container').find('.button.ajax_add_to_cart_button');
roja45quotationsprofree.quotationpro_addlabel(button, id_product);
roja45quotationsprofree.quotationpro_addbutton($(this), id_product, id_product_attribute, minimal_quantity);
});
$('#ajax_add_quote_button').on('click', function () {
var id_product = parseInt($('#buy_block #product_page_product_id').val());
var id_product_attribute = $('#buy_block #idCombination').val();
var quantity = parseInt($('#quantity_wanted').val());
$('#quote_block input[name=id_product]').val(id_product);
$('#quote_block input[name=id_product_attribute]').val(id_product_attribute);
$('#quote_block input[name=quantity]').val(quantity);
$('#quote_block').submit();
});
$(window).bind('hashchange', function(){
if (typeof productHasAttributes !== 'undefined' && productHasAttributes) {
$('#ajax_add_quote_button').attr('data-id-product-attribute="'+$('#idCombination').val()+'"');
}
});
});
roja45quotationsprofree = {
init : function() {
$('#quotationspro_request_dialog').off('click').on('click', '.ajax-request-quotation', function (e) {
e.preventDefault();
roja45quotationsprofree.processSend($(this).closest('form').attr('action'));
});
$('#quotationspro_request_dialog').off('keypress').on('keypress', function (e) {
if (e.which == 13) {
roja45quotationsprofree.processSend($(this).find('form').attr('action'));
}
});
$('#quotationspro_request_dialog #firstname, #quotationspro_request_dialog #lastname').off('focusout').on('focusout', function (e) {
$(this).val(roja45quotationsprofree.toTitleCase($(this).val()));
});
},
toTitleCase : function(str) {
str = str.toLowerCase();
return str.replace(/(?:^|\s)\w/g, function (match) {
return match.toUpperCase();
});
},
quotationpro_addbutton : function(ele, id_product, id_product_attribute, minimal_quantity) {
if (id_product) {
ele.closest('.product-container').find('.button-container').prepend('<a href='+ roja45quotationspro_controller + '&action=addToQuote&id_product=' + id_product + '&id_product_attribute=' + id_product_attribute + '&minimal_quantity=' + minimal_quantity + '" data-id-product="'+id_product+'" data-id-product-attribute="'+id_product_attribute+'" data-minimal-quantity="'+minimal_quantity+'" class="button lnk_view btn btn-default btn-quote ajax_add_quote_button"><span>'+roja45quotationspro_button_addquote+'</span></a>');
}
},
quotationpro_convertButton : function(button, id_product, id_product_attribute, minimal_quantity) {
var productContainer = button.closest('.product-container');
var href = roja45quotationspro_controller+'&action=addToQuote&id_product='+ id_product + '&id_product_attribute='+ id_product_attribute + '&minimal_quantity='+ minimal_quantity + '"';
if (parseInt(roja45quotationspro_enable_js)) {
if (parseInt(roja45quotationspro_enablequotecart)) {
var new_button = $('<a href="' + href + ' class="exclusive btn btn-quote ajax_add_quote_button"' +
'data-id-product="'+ id_product + '" data-id-product-attribute="'+ id_product_attribute + '" data-minimal-quantity="'+ minimal_quantity +'"><span>'+roja45quotationspro_cartbutton_text+'</span></a>');
button.parent().append(new_button);
button.remove();
} else if (parseInt(roja45quotationspro_enablequotecart)) {
var new_button = $('<a href="' + href + ' class="exclusive btn btn-quote ajax_add_quote_button"' +
'data-id-product="'+ id_product + '" data-id-product-attribute="'+ id_product_attribute + '" data-minimal-quantity="'+ minimal_quantity +'"><span>'+roja45quotationspro_cartbutton_text+'</span></a>');
button.parent().append(new_button);
} else if (!parseInt(roja45quotationspro_enablequotecart)) {
button.empty();
button.html('<span>' + roja45quotationspro_button_text + '</span>');
button.removeClass('ajax_add_to_cart_button');
button.attr('href', href);
button.addClass('btn-quote');
}
} else {
if (parseInt(roja45quotationspro_use_cart) && roja45quotationspro_request_buttons==0) {
button.empty();
button.html('<span>' + roja45quotationspro_cartbutton_text + '</span>');
button.removeClass('ajax_add_to_cart_button');
button.addClass('btn-quote');
button.addClass('ajax_request_quote_button');
button.attr('href', href);
} else {
var request = button.clone();
request.html('<span>' + roja45quotationspro_cartbutton_text + '</span>');
request.removeClass('ajax_add_to_cart_button');
request.addClass('btn-quote');
request.addClass('ajax_request_quote_button');
request.attr('href', href);
var new_button = $('<p id="request_quote" class="buttons_bottom_block no-print">').append(request);
$('#add_to_cart').parent().append(new_button);
}
}
if (parseInt(roja45quotationspro_show_label)==1) {
var parent = productContainer.find('.product-image-container');
var url = productContainer.find('.product_img_link').attr('href');
parent.append('<a class="quote-box '+roja45quotationspro_label_position+'" href="'+ url + '"><span class="quote-label">'+ roja45quotationspro_quote_link_text + '</span></a>');
}
},
quotationpro_addlabel : function(ele, id_product) {
if (id_product && (roja45quotationspro_show_label == 1)) {
var url = ele.closest('.ajax_block_product').find('.product_img_link').attr('href');
ele.closest('.ajax_block_product').find('.product-image-container').append('<a class="quote-box ' + roja45quotationspro_label_position + '" href="' + url + '"><span class="quote-label">' + roja45quotationspro_quote_link_text + '</span></a>');
}
},
/*
onRequestQuoteClick : function(ele) {
//roja45quotationsprofree.init();
$('.quotationspro_request_dialog_overlay').addClass('roja45quotationsprofree-darken-background');
var id_product = ele.attr('data-id-product');
if (!id_product){
id_product = $('#product_page_product_id').val();
}
var id_combination = $('#idCombination').val();
$('#quotationspro_request_dialog .page-heading').html($('input[name=DIALOG_HEADING]').val());
$('#quotationspro_request_dialog input[name=ID_PRODUCT]').val(id_product);
$('#quotationspro_request_dialog input[name=ID_COMBINATION]').val(id_combination);
$('.quotationspro_request_dialog_overlay').css('width','100%');
$('.quotationspro_request_dialog_overlay').css('height','100%');
$('.quotationspro_request_dialog_overlay').show();
$('#quotationspro_request_dialog').fadeIn('fast');
},
submitQuotation : function(ele) {
var url = ele.attr('action')
var request = {};
request.columns = [];
$(ele).find('.quotationspro_request.column').each(function (i) {
var column = {};
var heading = $(this).find('.page-subheading').html();
column.heading = heading;
// column id
var col = $(this).data('column');
column.num = col;
column.fields = [];
$(this).find('.form-control').each(function (j) {
var label = $(this).closest('.form-group').find('.control-label').html();
var field = {};
field.pos = j;
field.name = $(this).attr('name');
field.label = label.trim();
field.value = $(this).val();
column.fields[j] = field;
});
request.columns[i] = column;
});
var json = JSON.stringify(request);
$.ajax({
url: url + '?submitQuotationRequest=1&ajax=1',
type: 'post',
dataType: 'json',
data: $(ele).closest('form').serialize() + '&request=' + json,
beforeSend: function () {
roja45quotationsprofree.toggleWaitDialog();
//$('#quotationspro_request_dialog').addClass('blur-background');
},
success: function (data) {
if (data.result == 'success') {
roja45quotationsprofree.displaySuccessMsg([roja45quotationspro_sent_success]);
} else if (data.result == 'error') {
roja45quotationsprofree.displayErrorMsg([roja45quotationspro_sent_failed]);
roja45quotationsprofree.displayErrorMsg(data.errors);
} else {
roja45quotationsprofree.displayErrorMsg([roja45quotationspro_unknown_error]);
}
},
error: function (data) {
roja45quotationsprofree.displayErrorMsg([roja45quotationspro_sent_failed]);
},
complete: function (data) {
roja45quotationsprofree.toggleWaitDialog();
roja45quotationsprofree.reset(ele);
roja45quotationsprofree.closeDialog();
//$('#quotationspro_request_dialog').removeClass('blur-background');
if (typeof sendQuotationsPro != 'undefined') {
grecaptcha.reset(sendQuotationsPro);
}
}
});
},
reset : function(ele) {
$(ele).closest('form').trigger("reset");
$(ele).find('form-error').each().removeClass('form-ok');
$(ele).find('form-error').each().removeClass('form-error');
$(ele).closest('form').find('.quotationspro_request_dialog_msg.success').hide();
$(ele).closest('form').find('.quotationspro_request_dialog_msg.error').hide();
$(ele).closest('form').find('.form-error').each(function() {
$(this).removeClass('form-error');
})
$(ele).closest('form').find('.form-ok').each(function() {
$(this).removeClass('form-ok');
})
ele.find("input[type=text], textarea").val("");
},
closeDialog : function() {
$('#page').removeClass('blur-background');
$('.quotationspro_request_dialog_overlay').hide();
$('#quotationspro_request_dialog').fadeOut('fast');
roja45quotationsprofree.reset($('#quotation_form'));
$('#quotationspro_request_dialog').off('click');
$('#quotationspro_request_dialog').off('keypress');
},
processSend : function(ele) {
var errors = 0;
$(ele).find('input.validate', 'textarea.validate').each(function (index, value) {
if ($(this).hasClass('is_required')) {
var invalid = false;
if ($(this).val().length == 0) {
invalid = true;
} else if ($(this).attr('data-validate') != 'none') {
if ($(this).attr('name') == 'postcode' && typeof(countriesNeedZipCode[$('#id_country option:selected').val()]) != 'undefined')
var result = window['validate_' + $(this).attr('data-validate')]($(this).val(), countriesNeedZipCode[$('#id_country option:selected').val()]);
else
var result = window['validate_' + $(this).attr('data-validate')]($(this).val())
if (result) {
invalid = false;
} else {
invalid = true;
}
}
if (invalid) {
$(this).parent().addClass('form-error').removeClass('form-ok');
errors++;
} else {
$(this).parent().removeClass('form-error').addClass('form-ok');
}
}
});
if (errors == 0) {
roja45quotationsprofree.submitQuotation(ele);
}
},
*/
toggleWaitDialog: function () {
if (typeof toggleRoja45GlobalWaitDialog === "function") {
toggleRoja45GlobalWaitDialog();
} else {
if ($('.roja45-quotation-modal').hasClass('invisible')) {
$('.roja45-quotation-modal').removeClass('invisible');
$('.roja45-quotation-modal').fadeIn();
} else {
$('.roja45-quotation-modal').fadeOut();
$('.roja45-quotation-modal').addClass('invisible');
}
}
},
displaySuccessMsg : function ( msgs ) {
$.each(msgs, function(index, value) {
$.growl({
title: roja45quotationspro_success_title,
message: value,
duration: 3000,
style: 'notice'
});
});
},
displayWarningMsg : function ( msgs ) {
$.each(msgs, function(index, value) {
$.growl({
title: roja45quotationspro_warning_title,
message: value,
duration: 5000,
style: 'warning'
});
});
},
displayErrorMsg : function ( msgs ) {
$.each(msgs, function(index, value) {
$.growl({
title: roja45quotationspro_error_title,
message: value,
duration: 10000,
style: 'error'
});
});
}
}