When you submit a voucher on http://www.DontPayAll.co.uk, You help
others save money.
";
markup += "
" + repo.storeUrl + "
";
markup += "
";
return markup;
}
function repoFormatSelectionStore(repo) {
return repo.name;
}
$(".select-store").select2({
placeholder: "Only use store name suggest by us *",
minimumInputLength: 2,
// instead of writing the function to execute the request we use Select2's convenient helper
ajax: {
url: "https://dontpayall.co.uk/store/getStores",
dataType: "json",
quietMillis: 500,
delay: 500,
data: function (term, page) {
return {
// search term
q: term
};
},
results: function (data, page) {
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to alter the remote JSON data
return {results: data.items};
},
cache: true
},
formatResult: repoFormatResultStore,
formatSelection: repoFormatSelectionStore
});
var $form = $('#submit-box-form');
var addCouponValidator = $form.validate({
rules: {
title: {
required: true,
minlength: 10,
maxlength: 200
},
description: {
minlength: 25,
maxlength: 500
},
couponCode: {
minlength: 2,
maxlength: 15
}
},
messages: {
title: {
required: 'Please enter the offer title.',
minlength: 'Please give more details for the title'
},
description: {
required: 'Please enter the offer details.',
minlength: 'Please add a little more detail about the offer.'
},
couponCode: {
minlength: 'That code looks a bit short. Please double check and try again.',
maxlength: 'That code looks a bit long. Please double check and try again.'
}
},
errorElement: "span", // contain the error msg in a small tag
errorClass: 'help-block myErrorClass',
focusInvalid: false,
invalidHandler: function (form, validator) {
if (!validator.numberOfInvalids())
return;
$('html, body').animate({
scrollTop: $('#submit-box-form').offset().top - 100//$(validator.errorList[0].element).offset().top
}, 1000);
},
errorPlacement: function (error, element) { // render error placement for each input type
if (element.attr("type") == "radio" || element.attr("type") == "checkbox" || element.attr("type") == "file") { // for chosen elements, need to insert the error after the chosen container
error.insertAfter($(element).closest('.form-group').children('div').children().last());
} else if (element.hasClass("ckeditor")) {
error.appendTo($(element).closest('.form-group'));
} else if (element.parent().hasClass("input-group")) {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
// for other inputs, just perform default behavior
}
},
highlight: function (element, errorClass, validClass) {
var elem = $(element);
if (elem.hasClass("select2-offscreen")) {
$("#s2id_" + elem.attr("id") + " ul").addClass(errorClass);
} else {
$(element).closest('.help-block').removeClass('valid');
// display OK icon
$(element).closest('.form-group').removeClass('has-success').addClass('has-error').find('.symbol').removeClass('ok').addClass('required');
// add the Bootstrap error class to the control group
}
},
unhighlight: function (element, errorClass, validClass) {
// revert the change done by hightlight
var elem = $(element);
if (elem.hasClass("select2-offscreen")) {
$("#s2id_" + elem.attr("id") + " ul").removeClass(errorClass);
} else {
$(element).closest('.form-group').removeClass('has-error');
// set error class to the control group
}
},
success: function (label, element) {
label.addClass('help-block valid');
// mark the current input as valid and display OK icon
$(element).closest('.form-group').removeClass('has-error').addClass('has-success').find('.symbol').removeClass('required').addClass('ok');
}
});
$form.on('submit', function (e) {
e.preventDefault();
if ($form.valid()) {
$form.find('button[type="submit"]').empty().append("
").addClass('disabled');
$.ajax({
type: 'post',
url: $form.attr('action'),
data: $form.serialize(),
success: function (data) {
if (data.status == 'success') {
/*
Author:HaiHT
If submited success then add submited coupon id to local storage
*/
if(data.msg['id']){
saveSubmitedCouponsToLocalStorage(data.msg['id']);
}
addCouponValidator.resetForm();
$form[0].reset();
$('.coupon-code').attr('placeholder', 'Enter your code *').rules('add', {
required: true
});
$(".select-store").select2('data', null);
$form.find('button[type="submit"]').empty().text("SUBMITTED SUCCESSFULLY");
$form.find('button[type="submit"]').prev('span').remove();
} else if (data.status == 'error') {
$("
" + data.msg + "").insertBefore($('#btn-submit-box'));
}
setTimeout(function () {
$form.find('button[type="submit"]').empty().text("SUBMIT voucher CODE").removeClass('disabled');
},3000);
grecaptcha.reset(widgetId1);
}
});
} else grecaptcha.reset(widgetId1)
});
$('#couponType').on('change', function () {
var cur = $(this).val();
if (cur == 'Coupon Code') {
$('.coupon-code').attr('placeholder', 'Enter your code *').rules('add', {
required: true
});
} else if (cur == 'Free Shipping') {
$('.coupon-code').attr('placeholder', 'Enter your code').removeClass('required').rules('remove', 'required');
} else if (cur == 'Great Offer') {
$('.coupon-code').attr('placeholder', 'Enter your code').removeClass('required').rules('remove', 'required');
}
});
/*
Author:HaiHT
*/
function saveSubmitedCouponsToLocalStorage(couponID){
var localObj = {}; localObj.submitCoupons = [];
// If exist submitCoupons ids in LS then push to localObj submitCoupons
if(localStorage['submitCoupons']){
localObj.submitCoupons = (localStorage.submitCoupons).split(',');
}
// console.log('before submited coupon:', localObj.submitCoupons);
// Check if browser support Web storage
if(typeof(Storage) !== "undefined") {
localObj.submitCoupons.push(couponID);
localStorage['submitCoupons'] = localObj.submitCoupons;
// console.log('after submited coupon:',localStorage['submitCoupons']);
} else {
console.log('No Web Storage support');
}
}
});