(function ($) {
function setPPCFields(mod, ppcInfo) {
//console.log(ppcInfo);
for(var key in ppcInfo) {
if(ppcInfo.hasOwnProperty(key)) {
var cVal = ppcInfo[key];
var cField = '' + "\n";
$(".checkout-form", mod.$ctx).append(cField);
$('#' + key, mod.$ctx).val(cVal);
}
}
}
function block(mod) {
var $ctx = mod.$ctx,
cfg = mod.sandbox.getConfig()
var img = cfg.imgBase + "ajax-loader.gif";
$('.checkout-form', $ctx).block({
message: '
Please Wait...',
css: { border: '1px solid #7322b1', width: '150px', padding: '5px', "text-align": "center" }
});
}
function unblock(mod) {
$('.checkout-form', mod.$ctx).unblock();
}
function reloadCart(mod, callback) {
var cfg = mod.sandbox.getConfig();
block(mod);
$('.modal-body:first', mod.$ctx).load(cfg.baseUrl + "cart .cart-inner:first", function() {
callback();
initRemoveBtsn(mod);
unblock(mod);
});
}
function validateContactInfo(mod) {
var $ctx = mod.$ctx;
var formId = ".quote-cart-customer";
var name = jQuery(formId + ' input:text[name="name"]', $ctx).val();
var company = jQuery(formId + ' input:text[name="company"]', $ctx).val();
var email = jQuery(formId + ' input:text[name="email"]', $ctx).val();
var phone = jQuery(formId + ' input:text[name="phone"]', $ctx).val();
if(!jQuery.trim(name).length) {
alert("Please enter your name");
jQuery(formId + ' input[name="name"]', $ctx).focus();
return false;
}
if(!jQuery.trim(company).length) {
alert("Please enter your company name");
jQuery(formId + ' input[name="company"]', $ctx).focus();
return false;
}
if(!isValidEmail(email)) {
alert("Please enter a valid email address");
jQuery(formId + ' input[name="email"]', $ctx).focus();
return false;
}
if(!jQuery.trim(phone).length) {
alert("Please enter your phone number");
jQuery(formId + ' input[name="phone"]', $ctx).focus();
return false;
}
return true;
}
function initRemoveBtsn(mod) {
$('.remove-item', mod.$ctx).click(function(){
var href = $(this).attr("href");
$.ajax({
type: 'GET',
url: href,
success:function(resp) {
if(resp.status == 1) {
reloadCart(mod, function(){});
} else {
mod.fire("notifyError", {msg: "An error occurred"}, ['notify']);
}
},
dataType: 'json'
});
return false;
});
}
Tc.Module.QuoteCart = Tc.Module.extend({
on: function (callback){
var $ctx = this.$ctx,
self = this,
cfg = self.sandbox.getConfig();
initRemoveBtsn(this);
jQuery(".checkout-form", $ctx).attr("method", "post");
jQuery(".checkout-form", $ctx).attr("action", cfg.baseUrl + "api/sync/quote_checkout");
jQuery(".checkout-form", $ctx).submit(function() {
if(!jQuery('.cart-item', $ctx).length) {
alert("Your cart is empty!");
return false;
}
if(!validateContactInfo(self)) {
return false;
}
var hasErr = false;
jQuery('.cart-item-details', $ctx).each(function(){
if(hasErr) {
return;
}
var qty = jQuery(this).find('.quote_qty:first').val();
var pname = jQuery(this).find('.prod_name:first').val();
if(!isInteger(qty)) {
alert("Please enter a quantity for '" + pname + "'");
hasErr = true;
}
var minQty = jQuery(this).find('.minQty:first').val();
qty = parseInt(qty);
minQty = parseInt(minQty);
if(qty < minQty) {
alert("Please enter a quantity of " + minQty + " or more for '" + pname + "'");
//jQuery('input[name="qty"]').focus();
hasErr = true;
}
});
// if(_gaq && (!hasErr)) {
// var numItems = jQuery('#quote-cart').find('.cart-item').length;
// _gaq.push(['_trackEvent', 'cart', 'checkout', numItems + ""]);
// }
if(!hasErr) {
block(self);
}
return !hasErr;
});
PPC_Cookie.update(function(ppcInfo) {
setPPCFields(self, ppcInfo);
});
callback();
},
onShowCart: function(data) {
$('.modal:first', this.$ctx).modal("show");
},
onItemAdded: function(data) {
var self = this;
$('.modal:first', self.$ctx).modal("show");
reloadCart(self, function(){
var prodName = data.product;
});
},
after: function () {
}
});
})(Tc.$);