/* * Czerwony Konik - shop * * Copyright (c) 2008 Czerwony Konik * */ // common function replaceAll( str, from, to ) { var idx = str.indexOf( from ); while ( idx > -1 ) { str = str.replace( from, to ); idx = str.indexOf( from ); } return str; } function getParameter(name) { var url = window.location.href; var paramsStart = url.indexOf("?"); if (paramsStart != -1) { var paramString = url.substr(paramsStart + 1); var tokenStart = paramString.indexOf(name); if (tokenStart != -1) { paramToEnd = paramString.substr(tokenStart + name.length + 1); var delimiterPos = paramToEnd.indexOf("&"); if(delimiterPos == -1) { paramToEnd = replaceAll(paramToEnd, "%20", " "); return paramToEnd; } else { paramToEnd = paramToEnd.substr(0, delimiterPos) paramToEnd = replaceAll(paramToEnd, "%20", " "); return paramToEnd; } } } } // shop var ALL_BOOKS_COUNT = 5; function calcPrice(book_no) { var value = $("#book" + book_no + "_count").val() * $("#book" + book_no + "_price").text(); $("#book" + book_no + "_total").text(getPriceValue(value)); } function calcTotalPrice() { var total = 0; for (i = 1; i <= ALL_BOOKS_COUNT; i++) { calcPrice(i); total += 1 * $("#book" + i + "_total").text(); } if ($("#_order_type_transfer").attr("checked")) { $("#delivery_price").text($("#transferPrice").text()); } else { $("#delivery_price").text($("#ondeliveryPrice").text()); } if (getPriceValue(total) > 0) { total += 1 * $("#delivery_price").text(); } $("#books_total").text(getPriceValue(total)); refreshSendOrder(); } function getPriceValue(value) { if (isNaN(value)) { return 0; } else { return value; } } function refreshSendOrder() { if (!isValid() || ($("#books_total").text() == 0)) { $("#send_order").attr("disabled", "disabled"); } else { $("#send_order").removeAttr("disabled"); } } function isValid() { return ($(".reqAware[value='']").length == 0); } function validateField(field) { if (field.val() == "") { field.addClass("req"); } else { field.removeClass("req"); } refreshValidationMessage(); } function refreshValidationMessage() { if ($(".reqAware").hasClass("req")) { $("#validation").show(); } else { $("#validation").hide(); } refreshSendOrder(); } function install() { for (i = 1; i <= ALL_BOOKS_COUNT; i++) { $("#book" + i + "_count").change(function() { calcTotalPrice(); }); } $("#_order_type_transfer").add("#_order_type_ondelivery").change(function() { calcTotalPrice(); }); calcTotalPrice(); $(".reqAware").blur(function() { validateField($(this)); }); // show error var error = getParameter('error'); if (typeof error != 'undefined') { $("#errorInfo").text(error).show(); } else { $("#errorInfo").hide(); } } $(document).ready(function() { install(); });