var selected_shipping_option=0; var shipping_amount=0; var discount_amount=0; var product_total=0; $(document).ready(function() { var options = { target: '#register_status', success: register_response // post-submit callback }; $('#f_register_form').ajaxForm(options); var options = { target: '#login_status', success: login_response // post-submit callback }; $('#f_login_form').ajaxForm(options); update_cart_total(); }); function submit_register_form() { var cart_form_serial=$("#cart_form").serialize(); $.ajax({ type: "POST", data: cart_form_serial, url: "../ajax/checkout_register.php", beforeSend: function() { $("#register_status").html("Loading..."+cart_form_serial); }, success: function(html){ $("#register_status").html(html); //register_response(html); }, error: function(html) { $("#billing_box").html(html); } }); } function register_response(responseText, statusText, xhr, $form) { if(responseText=='ok') { // show logged in box $('#register_form').fadeOut('slow', function() { update_welcome_box(); }); } } function submit_login_form() { $.ajax({ type: "POST", url: "ajax/checkout_login.php", data: "login_email="+$("#login_email").val()+"&login_password="+$("#login_password").val(), beforeSend: function() { }, success: function(html){ //alert(html); login_response(html); var qs1 = new Querystring(html); var success=qs1.get('log_in_successful'); if(success>0) { $("#welcome_box").slideDown(); $("#welcome_box").html("Welcome back!"); $("#checkout_errors").fadeOut(); setTimeout(" $('#welcome_box').slideUp();",5000) populate_billing_box(html); populate_loyalty_box(qs1.get('loyalty_points')); hide_hidden_login_rows(); } else { $("#checkout_errors").html("Sorry, your login information was incorrect."); $("#checkout_errors").fadeIn(); } }, error: function(html) { $("#welcome_box").html(html); } }); } //function login_response(responseText, statusText, xhr, $form) { function login_response(responseText) { //if(responseText=='ok') { { // show logged in box $('#login_form').fadeOut('slow', function() { update_welcome_box(); populate_billing_box(responseText); //update_billing_box(); //update_shipping_box(); update_address_finder("billing_postcode","billing_postcode_finder_link","billing") var qs1 = new Querystring(responseText); var pc = qs1.get('postcode'); var countryid = qs1.get('countryid'); /* $('#ship_to_billing').attr('checked', true); animatedcollapse.hide('shipping_box'); get_delivery_information(pc,countryid); }); */ }); } } function populate_loyalty_box(points) { if(!points)$("#loyalty_points_box").hide(); false; $("#loyalty_points_box").fadeIn() //alert("points="+points); //$("#loyalty_points_box").html(points); var str=""; for(i=0;i<=points;i++) { str=str+"Use "+i+" Points"; } str=str+""; $("#loyalty_points_box").html(str); } function update_billing_box() { $.ajax({ type: "POST", url: "../ajax/billing_box.php", beforeSend: function() { $("#billing_box").show(); $("#billing_box").html("Loading"); }, success: function(html){ $("#billing_box").html(html); $('#all_forms').fadeIn('slow'); bind_billing_form(); bind_shipping_form(); }, error: function(html) { $("#billing_box").html(html); } }); } function populate_billing_box(responseStr) { var qs1 = new Querystring(responseStr); //alert(responseStr); var v1 = qs1.get('billing_dob'); $('#billing_dob').val(v1); var v1 = qs1.get('billing_first_name'); $('#billing_first_name').val(v1); var v1 = qs1.get('billing_last_name'); $('#billing_last_name').val(v1); var v1 = qs1.get('billing_company'); $('#billing_company').val(v1); var v1 = qs1.get('billing_address1'); $('#billing_address1').val(v1); var v1 = qs1.get('billing_address2'); $('#billing_address2').val(v1); var v1 = qs1.get('billing_city'); $('#billing_city').val(v1); var v1 = qs1.get('billing_postcode'); $('#billing_postcode').val(v1); var v1 = qs1.get('billing_county'); $('#billing_county').val(v1); var v1 = qs1.get('billing_country'); $('#billing_country').val(v1); var v1 = qs1.get('billing_telephone'); $('#billing_telephone').val(v1); var v1 = qs1.get('shipping_first_name'); $('#shipping_first_name').val(v1); var v1 = qs1.get('shipping_last_name'); $('#shipping_last_name').val(v1); var v1 = qs1.get('shipping_address1'); $('#shipping_address1').val(v1); var v1 = qs1.get('shipping_address2'); $('#shipping_address2').val(v1); var v1 = qs1.get('shipping_city'); $('#shipping_city').val(v1); var v1 = qs1.get('shipping_postcode'); $('#shipping_postcode').val(v1); var v1 = qs1.get('shipping_country'); $('#shipping_country').val(v1); var v1 = qs1.get('shipping_telephone'); $('#shipping_telephone').val(v1); var v1 = qs1.get('shipping_company'); $('#shipping_company').val(v1); } function update_billing_with_summaries() { $.ajax({ type: "POST", url: "../ajax/get_billing_summary.php", beforeSend: function() { $("#billing_box").show(); $("#billing_box").html("Loading"); }, success: function(html){ $("#billing_box").html(html); $('#all_forms').fadeIn('slow'); bind_billing_form(); bind_shipping_form(); }, error: function(html) { $("#billing_box").html(html); } }); } function billing_save_response(responseText, statusText, xhr, $form) { if(responseText=='1') { // billing save is successfull // $("#billing_box").html("put summary here"); //update_billing_with_summaries(); } } function bind_billing_form() { var options = { target: '#order_status', success: billing_save_response }; $('#billing_form').ajaxForm(options); } // shipping functions function update_shipping_box() { // $("#shipping_box").show(); $.ajax({ type: "POST", url: "../ajax/shipping_box.php", beforeSend: function() { $("#shipping_box").html("Loading"); }, success: function(html){ $("#shipping_box").html(html); $('#all_forms').fadeIn('slow'); bind_shipping_form(); }, error: function(html) { $("#shipping_box").html(html); } }); } function update_shipping_with_summaries() { $.ajax({ type: "POST", url: "../ajax/get_shipping_summary.php", beforeSend: function() { $("#shipping_box").show(); $("#shipping_box").html("Loading"); }, success: function(html){ $("#shipping_box").html(html); $('#all_forms').fadeIn('slow'); bind_shipping_form(); }, error: function(html) { $("#shipping_box").html(html); } }); } function shipping_save_response(responseText, statusText, xhr, $form) { if(responseText=='1') { // shipping save is successfull // $("#shipping_box").html("put summary here"); update_shipping_with_summaries(); } } function bind_shipping_form() { var options = { target: '#shipping_result', success: shipping_save_response }; $('#shipping_form').ajaxForm(options); } // end shipping functions function update_welcome_box() { $.ajax({ type: "POST", url: "../ajax/welcome_box.php", beforeSend: function() { $("#welcome_box").show(); $("#welcome_box").html("Loading"); }, success: function(html){ $("#welcome_box").html(html); }, error: function(html) { $("#welcome_box").html(html); } }); } function get_delivery_information(postcode,countryid) { var debug_string; //alert(postcode+' '+countryid); if(!postcode && !countryid) { if($('#ship_to_billing:checked').val()!=null) { //postcode=$('#billing_postcode').val(); countryid=$('#billing_country').val(); } else { //postcode=$('#shipping_postcode').val(); countryid=$('#shipping_country').val(); } } debug_string="&postcode="+postcode+"&countryid="+countryid; $.ajax({ type: "POST", url: "../ajax/get_shipping_info.php", data: debug_string, beforeSend: function() { $("#cart_shipping_info").show(); $("#cart_shipping_info").html("Loading"); $("#cart_shipping_info").hide(); }, success: function(html){ if(html>=1) { $("#cart_shipping_info").html(html); update_shipping_options(postcode,countryid); } else { $("#cart_shipping_info").html("No address provided."+html); } }, error: function(html) { $("#cart_shipping_info").html(html); } }); } function update_shipping_options(postcode,countryid) { if(!postcode && !countryid) { if($('#ship_to_billing:checked').val()!=null) { postcode=$('#billing_postcode').val(); countryid=$('#billing_country').val(); } else { postcode=$('#shipping_postcode').val(); countryid=$('#shipping_country').val(); } } var debug_string="postcode="+postcode+"&countryid="+countryid; $.ajax({ type: "POST", url: "../ajax/get_shipping_option_box.php", data: debug_string, beforeSend: function() { $("#cart_shipping_options").show(); $("#cart_shipping_options").html("Loading"); }, success: function(html){ $("#cart_shipping_options").html(html); update_shipping_amount(postcode,countryid); }, error: function(html) { $("#cart_shipping_options").html(html); } }); } function set_shipping_option() { selected_shipping_option=$("#shipping_option_id").val(); } function update_shipping_amount(postcode,countryid) { set_shipping_option(); if(!postcode && !countryid) { countryid=$("#country").val(); } var debug_string="postcode="+postcode+"&countryid="+countryid+"&optionid="+selected_shipping_option; $.ajax({ type: "POST", url: "../ajax/get_shipping_price.php", data: debug_string, beforeSend: function() { }, success: function(html){ shipping_amount=parseFloat(html); //alert('updating totals '+debug_string+"...."+html); update_total(); //alert(html); $("#cart_shipping_price").html(html); }, error: function(html) { $("#cart_shipping_price").html(html); } }); } function format_price(amount) { return "£"+number_format(amount,2,'.',''); } function update_cart_total() { get_delivery_information('',$("#country").val()); $.ajax({ type: "POST", url: "../ajax/cart_total_data.php", beforeSend: function() { $("#cart_product_total_cell").show(); $("#cart_product_total_cell").html("Loading"); }, success: function(html){ var qs2 = new Querystring(html) var v1 = qs2.get("name1") //get the line totals and print them for each product line var lineTotals = qs2.get("line_totals"); for (var i=0; i" + format_price(productInfo.discounted); } else { var resultStr = format_price(productInfo.total); } $("#cart_row_" + productInfo.productid + " td:last-child p strong").html(resultStr); } product_total=qs2.get("cart_total"); //product_total=qs2.get("initial_total"); //discount_amount=0-qs2.get("cart_savings"); update_total(); $("#cart_product_total_cell").html("£"+qs2.get("cart_total")); //$("#cart_product_discounts_cell").html("£"+qs2.get("cart_savings")+"."); $("#cart_num_items").html(qs2.get("num_items")+" items"); get_cart_summary(); if(product_total<=0) { $("#checkout_link").hide(); $("#checkout_link2").hide(); } else { $("#checkout_link").show(); $("#checkout_link2").show(); } }, error: function(html) { $("#cart_total_cell").html(html); } }); } function discount_load_sizes(productid) { if(!productid) { alert('no product specified'); die; } $.ajax({ type: "POST", url: "../ajax/cart_discount_select_product.php", data: "productid="+productid, success: function(html) { $("#cart_discount_dialogue").html("To use this coupon code you will need to add one of the following items to your cart:"+html); $("#cart_discount_dialogue").slideDown(); } }); } function submit_discount_code2() { $.ajax({ type: "POST", url: "../ajax/submit_discount_code.php", data: "discount_code="+$("#cart_discount_code_amount").val(), beforeSend: function() { $("#cart_discount_amount").html("Loading"); $("#cart_discount_dialogue").hide(); }, success: function(html){ //$("#cart_discount_status").html(html); var qs1 = new Querystring(html); //alert(html); var dialogue=qs1.get('dialogue_required'); var amount=qs1.get('amount'); var productid=qs1.get('productid'); if(dialogue=='1') { //$("#cart_discount_dialogue").html(""); discount_load_sizes(productid); discount_amount=amount; } else { //$("#cart_discount_dialogue").html("No prompt needed."); if(amount<0) $("#cart_discount_status").html("Thank you, your discount has been applied."); else $("#cart_discount_status").html("Sorry, this discount code was not found or is not valid for this order."); update_cart_total(); $("#cart_discount_amount").html(amount); discount_amount=amount; setTimeout(location.reload(),1000); } }, error: function(html) { $("#cart_discount_amount").html(html); } }); } function discount_selector_add(productid) { add_to_cart(productid,1,0,'',1); $("#cart_discount_dialogue").slideUp(); submit_discount_code(); //setTimeout(location.reload(),3000); } function submit_discount_code() { $.ajax({ type: "POST", url: "../ajax/get_discount_amount.php", data: "discount_code="+$("#cart_discount_code_amount").val(), beforeSend: function() { $("#cart_discount_amount").html("Loading"); }, success: function(html){ $("#cart_discount_amount").html(html); if(html<0) $("#cart_discount_status").html("Thank you, your discount has been applied."); else $("#cart_discount_status").html("Sorry, this discount code was not found."); discount_amount=html; //update_total(); update_cart_total(); }, error: function(html) { $("#cart_discount_amount").html(html); } }); } function number_format( number, decimals, dec_point, thousands_sep ) { // http://kevin.vanzonneveld.net // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfix by: Michael White (http://crestidg.com) // + bugfix by: Benjamin Lupton // + bugfix by: Allan Jensen (http://www.winternet.no) // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // * example 1: number_format(1234.5678, 2, '.', ''); // * returns 1: 1234.57 var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals; var d = dec_point == undefined ? "." : dec_point; var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : ""; var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0; return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); } function update_total() { //var d = Math.round(discount_amount*100)/100; var p = Math.round(product_total*100)/100; var s = Math.round(shipping_amount*100)/100; //alert(d+"\n"+p+"\n"+s); //var total=p+d+s; var total=p+s; total = Math.round(total*100)/100; $("#cart_total_cell").html(format_price(total)); if(product_total<=0) { $("#checkout_link").hide(); $("#checkout_link2").hide(); } else { $("#checkout_link").show(); $("#checkout_link2").show(); } } function update_quantities() { $.ajax({ type: "POST", url: "../ajax/cart_update.php", data: $("#cart_form").serialize(), beforeSend: function() { }, success: function(html){ $("#cart_debug").html(html); update_cart_total(); get_cart_summary(); }, error: function(html) { $("#cart_status").html("update quantities hasnt worked"); } }); } function pc_shipping_changed() { update_address_finder("shipping_postcode","shipping_postcode_finder_link","shipping"); get_delivery_information($('#shipping_postcode').val(),$('#shipping_country').val()); $("#pc_ch_note").remove(); if ($('#shipping_postcode').val().substr(0,2).toUpperCase() == 'GY' || $('#shipping_postcode').val().substr(0,2).toUpperCase() == 'JE'){ //alert("We've noticed you have set a Channel Islands shipping address! We will deduct VAT from the price you pay!"); $('#shipping_postcode').parentsUntil("table").eq(2).append("We've noticed you have set a Channel Islands shipping address! We will deduct VAT from the price you pay!"); }else{ } }