var ajaxUrl = '/ajaxpost';
var finishUrl = '/afronden';
$(document).ready(function() {
$('a[@rel$="blank"]').click(function(){
this.target = "_blank";
});
updateOrderReview();
$('#ff-cust-email').click(function(e){
$('#cust-email-container').addClass('cust-email');
$('#email-msg').html('<br />Is het e-mail adres correct? Een bevestigingsbericht wordt naar dit adres gestuurd.');
});
$('form#order-form').ajaxSubmit();
$('a#condition-agreement-toggle').toggle(function() {
$('div#condition-agreement-contents').animate({ height: 'show' }, 'slow');
},function(){
$('div#condition-agreement-contents').animate({ height: 'hide' }, 'slow');
});
$('#ff-cust-city').blur( function() {
updateDeliveryLocation();
} );
$('a#select-delivery-location-toggle').toggle(function() {
$('#delivery-location-select').animate({ height: 'show' }, 'slow')
},function(){
$('#delivery-location-select').animate({ height: 'hide' }, 'slow')
});
});
function setDeliveryLocationBestMatch(id, lbl) {
if (getDeliveryLocation() > 0) return;
setDeliveryLocation(id, lbl);
}
function setDeliveryLocation(id, lbl) {
$('#ff-delivery-location').val(id);
lbl = '<a href="#" id="select-delivery-location-toggle">' + lbl + '</a>';
$('#delivery-location-label').html(lbl);
$('#delivery-location-select').animate({ height: 'hide' }, 'slow');
$('a#select-delivery-location-toggle').toggle(function() {
$('#delivery-location-select').animate({ height: 'show' }, 'slow')
},function(){
$('#delivery-location-select').animate({ height: 'hide' }, 'slow')
});
}
function getDeliveryLocation() {
return $('#ff-delivery-location').val();
}
function updateDeliveryLocation(pc, city) {
var cur = getDeliveryLocation();
var target = $('#delivery-location-select');
if (pc == undefined) {
pc = $('#ff-cust-postalcode').val();
}
if (city == undefined) {
city = $('#ff-cust-city').val();
}
var url = '/zoek-winkel/pc/' + pc + '/city/' + city;
$.post(
url,
{ },
function(content){
if (content.length > 0) {
target.html(content);
} else {
target.html('Kon geen gegevens opvragen.');
}	}
);
}
function manualSearch() {
var city = $('#manual-shopfinder-city').val();
var pc = $('#manual-shopfinder-pc').val();
updateDeliveryLocation(pc, city);
}
function updateOrderReview() {
if ($('#order-review-content').html() != null) {
$.post(
ajaxUrl,
{ action: 'get-orderreview'},
function(content){
if (content.length > 0) {
$('#order-review-content').html(content);
} else {
window.location = finishUrl;
}	}
);
}
}
function updateOrderCount(obj, modifyid) {
var count = obj.value;
if (modifyid > 0) {
if (count > 0) {
$.post(
ajaxUrl,
{ action: 'set-ordercount', modifyid: modifyid, count: count},
function(result){
updateOrderReview();
}
);
} else if (count == 0) {
$("body").append('<div id="confirm-overlay"></div>');
msg = '<p>Weet u zeker dat dit product verwijderd moet worden?</p>';
msg = msg + '<p><a href="#" title="Verwijderen" onclick="confirmDelete(' + modifyid + ');">Verwijderen</a> <a href="#" title="Annuleren" onclick="cancelDelete();">Annuleren</a></p>';
$("body").append('<div class="confirm" id="orderdeleteconfirm">' + msg + '</div>');
}
}
}
function cancelDelete() {
$('div#confirm-overlay').hide();
$('div#orderdeleteconfirm').hide();
updateOrderReview();
}
function confirmDelete(modifyid) {
$('div#confirm-overlay').hide();
$('div#orderdeleteconfirm').hide();
if (modifyid > 0) {
$.post(
ajaxUrl,
{ action: 'delete-orderline', modifyid: modifyid, confirm: (modifyid * 11)},
function(result){
updateOrderReview();
}
);
}
}
$.fn.ajaxSubmit = function(e) {
strErrorFields = '';
$("div.formErrors").html('');
this.submit(function(){
var params = {};
$(this)
.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea, label, select")
.filter(":enabled")
.each(function() {
$('#' + this.id).removeClass("fieldError");
params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
});
$("body").addClass("curWait");
$.post(ajaxUrl, params, function(xml){
$("body").removeClass("curWait");
strError = "De bestelling kon niet verzonden worden.";
oFocus = null;
$("AjaxResponse", xml).each(function() {
strRedirect = this.getAttribute("redirecturl");
strError = this.getAttribute("error");
strErrorFields = this.getAttribute("errorfields");
oFocus = this.getAttribute("focus");
});
if (strError.length == 0) {
window.location = strRedirect;
} else {
$("div.formErrors").html("<h3>Fout<\/h3><ul>" + strError.replace(/(\t)(.+)/g, "<li>$2<\/li>") + "<\/ul>").filter(":hidden").fadeIn("normal");
if (oFocus) $("#" + oFocus).get(0).focus();
if (strErrorFields.length > 0) {
arrErrorFields = strErrorFields.split('|');
for (var i = arrErrorFields.length - 1; i >= 0; --i) {
var fieldName = arrErrorFields[i];
if (fieldName.length > 0) {
$('#' + fieldName).addClass("fieldError");
}
}
}
}
});
return false;
});
return this;
}
