$(document).ready(function() {
var kItemID, susaoSelector, iIDTokenLength, iIDTokenWidth, iIDTokenHeight, iIDTokenbaseaddition, length, width, height;
susLFVItemLoad();
susResetPrice();
if($( "#nIstVater" ).length > 0 && parseInt($( "#nIstVater" ).text(), 10) !== 1)
susCatchInput();
// Disable Weight Inputtext
$( "dt:contains('Gewicht')" ).css("display", "none").next().css("display", "none");
if(iTokenUnit.length > 0 && iTokenUnit != "null")
$( "#buy_form .bulk-price table thead tr").children().slice(0,1).html("ab "+iTokenUnit).next().html(iTokenUnit+" Preis");
});
///////
// Important Stuff
///////
function susLFVItemLoad() {
sussDataJSON = $( "#sus_lfv_data_json" ).text();
sussSelector = $( "#sus_lfv_selector_json" ).text();
if(sussDataJSON.length > 0) {
susaoLFVData = $.parseJSON(sussDataJSON);
kItemID = susGetkArtikel();
iIDTokenLength = parseInt(susaoLFVData[kItemID].susArtikelLaengeID, 10);
iIDTokenWidth = parseInt(susaoLFVData[kItemID].susArtikelBreiteID, 10);
iIDTokenHeight = parseInt(susaoLFVData[kItemID].susArtikelHoeheID, 10);
iTokenUnit = String(susaoLFVData[kItemID].sus_lfv_einheit_ausgabe).replace("2","²").replace("3","³");
}
if(sussSelector.length > 0) {
susaoSelector = $.parseJSON(sussSelector);
}
}
function susResetPrice() {
// Textanpassungen
var PriceText = $( susaoSelector['selector_main']+" "+susaoSelector['selector_price'] ).text();
if(PriceText.indexOf(',') > -1)
$( susaoSelector['selector_main']+" "+susaoSelector['selector_price'] ).text(function(i, txt) {return txt.replace(/[+-]?\d+(\,\d+)?/, '0,00')});
else if(PriceText.indexOf('.') > -1)
$( susaoSelector['selector_main']+" "+susaoSelector['selector_price'] ).text(function(i, txt) {return txt.replace(/[+-]?\d+(\.\d+)?/, '0.00')});
}
function susGetkArtikel() {
return $( "#kArtikel" ).text();
}
function susCatchInput() {
$( "input[name='eigenschaftwert[" + iIDTokenLength + "]'], input[name='eigenschaftwert[" + iIDTokenWidth + "]'], input[name='eigenschaftwert[" + iIDTokenHeight + "]']").on("keyup", function() {
$(this).val($(this).val().replace(/([^0-9\,\.])/, ""));
$(this).val($(this).val().replace('..', "."));
$(this).val($(this).val().replace(',', "."));
susCalcLFV(this.getAttribute("data-key"));
});
$( "select[name='eigenschaftwert[" + iIDTokenLength + "]'], select[name='eigenschaftwert[" + iIDTokenWidth + "]'], select[name='eigenschaftwert[" + iIDTokenHeight + "]']").on("change", function() {
susCalcLFV(parseInt(this.getAttribute("data-key"), 10));
});
$( ".variations input[name^='eigenschaftwert']" ).on("change", function() {
var name = parseInt($(this).attr("name").replace("eigenschaftwert[", "").replace("]", ""), 10);
if(name !== iIDTokenLength && name !== iIDTokenWidth && name !== iIDTokenHeight)
susCalcLFV(parseInt(this.getAttribute("data-key"), 10));
});
$( ".variations select" ).on("change", function() {
var name = parseInt($(this).attr("name").replace("eigenschaftwert[", "").replace("]", ""), 10);
if(name !== iIDTokenLength && name !== iIDTokenWidth && name !== iIDTokenHeight)
susCalcLFV(parseInt(this.getAttribute("data-key"), 10));
});
$( susaoSelector['selector_main']+" input#quantity" ).on("keyup", function() {
susCalcLFV();
});
$( susaoSelector['selector_main']+" input#quantity" ).on("change", function() {
susCalcLFV();
});
$( ".group-items input" ).on("change", function() {
susCalcLFV();
});
}
function susCalcLFV(IDToken = null) {
var timeout = null;
var lfvdata = new Array();
var length = susGetLWH(iIDTokenLength);
var width = susGetLWH(iIDTokenWidth);
var height = susGetLWH(iIDTokenHeight);
var quantity = $( susaoSelector['selector_main']+" input#quantity" ).val();
var postdata = {susGetPrice: 1, suskItem: kItemID, susQuantity: quantity, lfv: lfvdata, susLength: length, susWidth: width, susHeight: height};
if(!isNaN(iIDTokenLength))
postdata.lfv.push('susToken'+iIDTokenLength+':'+susGetLWH(iIDTokenLength));
if(!isNaN(iIDTokenWidth))
postdata.lfv.push('susToken'+iIDTokenWidth+':'+susGetLWH(iIDTokenWidth));
if(!isNaN(iIDTokenHeight))
postdata.lfv.push('susToken'+iIDTokenHeight+':'+susGetLWH(iIDTokenHeight));
clearTimeout(timeout);
timeout = setTimeout(function () {
$.ajax({
method: "POST",
url: window.location.hostname,
async: false,
data: postdata
})
.done(function(results) {
data = JSON.parse(results);
})
.error(function(results) {
data = 0;
});
susCheckRequirements(data.LFVerror);
if(data != 0)
susWritePage(data);
else
susWritePage(0);
}, 800);
}
function susGetLWH(id) {
if($( "input[name='eigenschaftwert[" + id + "]']" ).length > 0)
return parseFloat($('input[name="eigenschaftwert[' + id + ']"]').val());
else
return parseFloat($('select[name="eigenschaftwert[' + id + ']"] option:checked').attr("data-original"));
}
function susCheckRequirements(LFVerror){
var validated = true;
if(LFVerror[0] >= 0) {
if(LFVerror[0] == 1) {
// minlength
msg = $( "#sus_its_lfvcalc_msg_minlength" ).text();
$( "#lfv_infotext").text(msg);
susShowNotice(msg, iIDTokenLength);
}else if(LFVerror[0] == 2) {
// maxlength
msg = $( "#sus_its_lfvcalc_msg_maxlength" ).text();
$( "#lfv_infotext").text(msg);
susShowNotice(msg, iIDTokenLength);
}else {
susHideNotice(iIDTokenLength);
}
}
if(LFVerror[1] >= 0) {
if(LFVerror[1] == 1) {
// minwidth
msg = $( "#sus_its_lfvcalc_msg_minwidth" ).text();
$( "#lfv_infotext").text(msg);
susShowNotice(msg, iIDTokenWidth);
}else if(LFVerror[1] == 2) {
// maxwidth
msg = $( "#sus_its_lfvcalc_msg_maxwidth" ).text();
$( "#lfv_infotext").text(msg);
susShowNotice(msg, iIDTokenWidth);
}else {
susHideNotice(iIDTokenWidth);
}
}
if(LFVerror[2] >= 0) {
if(LFVerror[2] == 1) {
// minheight
msg = $( "#sus_its_lfvcalc_msg_minheight" ).text();
$( "#lfv_infotext").text(msg);
susShowNotice(msg, iIDTokenHeight);
}else if(LFVerror[2] == 2) {
// maxheight
msg = $( "#sus_its_lfvcalc_msg_maxheight" ).text();
$( "#lfv_infotext").text(msg);
susShowNotice(msg, iIDTokenHeight);
}else {
susHideNotice(iIDTokenHeight);
}
}
if(LFVerror[3] == 1)
validated = false;
// wenn (immernoch) alles ok, dann kaufbutton freischalten
if(validated) {
$( "button[name='inWarenkorb']" ).removeAttr("disabled");
$( "#lfv_infotext").text($( "#sus_its_lfvcalc_msg_ok" ).text());
} else {
$( "button[name='inWarenkorb']" ).attr("disabled","disabled");
}
}
function susShowNotice(msg, IDToken) {
if(!$( "#sus_lfv_notice_"+IDToken ).length)
$( "input[name='eigenschaftwert[" + IDToken + "]']" ).after('<span id="sus_lfv_notice_'+ IDToken+'">'+msg+'</span>');
}
function susHideNotice(IDToken) {
if($( "#sus_lfv_notice_"+IDToken ).length)
$( "#sus_lfv_notice_"+IDToken ).remove();
}
function susWritePage(data) {
var quant = $( susaoSelector['selector_main']+" input#quantity" ).val();
//var zuschlag = sus_variationszuschlag($(sus_json)[0][kartikelid].sus_lfv_grundpreiszuschlag);
$( "#sus_lfv_rechenergebnis" ).html(data.calculation);
$( "#sus_lfv_gesamtergebnis" ).html(data.calculation * quant);
$( "#buy_form .price_wrapper .price span" ).html(data.price);
$( "#buy_form .price_wrapper strong.price" ).html(data.price);
$( "#product-configuration-sidebar strong.price").html(data.price);
}