﻿function ShowRow(iOrderLineID, bChecked) {
	if (bChecked)
		$("#giftwraptr_" + iOrderLineID).show();
	else
		$("#giftwraptr_" + iOrderLineID).hide();
}

function showApply() {

	if ($(this).val() != '' && $(this).val().length > 2)
		$("#applycode, #applycodedesc").show();
	else
		$("#applycode, #applycodedesc").hide();

}

// Update gift messages and quantities when the customer goes to checkout,
// in case they didn't click the update basket button
function updateMessages(formObject) {

    var data = new Array();

    $('#cartform input[type=text]').each(function() {

        var v = $(this).val();
        var rx = new RegExp("&", "gi");
        v = v.replace(rx, '%26');
        data.push($(this).attr('name') + '=' + v);
    });

    // This gets all checkbox values, even the non-checked - is this necessary?
    $('#cartform input[type=checkbox]').each(function() {
        data.push($(this).attr('name') + '=' + (($(this).attr('checked')) ? 1 : 0));
    })
    
    $.ajax({ url: '/ajax_update_quantities.aspx',
             type: 'POST',
             dataType: 'json',
             async: false,
             data: data.join('&'),
             success: function(json) {

             $('#checkoutform input[name=OrderItemCount]').val(json.count);
             $('#checkoutform input[name=OrderTotal]').val(json.total);
             
             }
         });

         

    pageTracker._linkByPost(formObject);

    return true;
}

$(document).ready(function() {

    $('#CartSourceCode').bind('keyup', showApply).bind('change', showApply).bind('blur', showApply);


    // onclick="ShowRow(<%=line.ID%>, this.checked);" <%=((line.GiftWrap) ? "checked=\"checked\" " : "")

    $('#postage').bind('change', function() {

        $('#postagetotal').html($(this).val());

        var total = parseFloat($(this).val(), 10) + parseFloat($('#itemtotal').html(), 10);

        $('#totaltopay').html(total);

    });
    
    

});
