$(document).ready(function() {
    //alert($('#minicart').html());
    // add to cart
    $('.atc').click(function() {
        //alert(cartHasItems);
        actionid = this.id;
        // bits will contain the action (add/del/view) and the productid if there is one
        bits = actionid.split("_");
		
        $('#minicart').show('fast', function() {
            });
        $.post("/incs/cart.php", {
            action: bits[0],
            pid: bits[1],
            price: bits[2],
            location: 'mini'
        },
        function(html){
            showMiniCartData(html);
        });
    });
    // adjust full cart
    $('.afc').click(function() {
        actionid = this.id;
        // bits will contain the action (add/del) and the productid if there is one
        bits = actionid.split("_");
        $.post("/incs/cart.php", {
            pid: bits[1],
            action: bits[0],
            location: 'full'
        },
        function(xml){
            showFullCartData(xml);
        });
    });
    // for this to run it means that the add-to-cart link has not been clicked - page loaded
    var currentLocation = String(document.location);
    //alert(currentLocation);
    var noShowPages = /checkout|viewcart|secure/g;
    match = noShowPages.test(currentLocation);
    //alert(match);
    //alert(currentLocation.indexOf(noShowPages));
    if (match == false) {
        // this will run for pages that need to display the minicart
        // calling showMiniCartData() will ensure the mini cart matches
        // any mods made in viewcart
        $.post("/incs/cart.php", {
            action: 'update'
        },
        function(html){
            showMiniCartData(html);
        });
    }
    else {
        $('#minicart').css('display', 'none');
    }
}); 

function showMiniCartData(xhtml) {
    //alert("Data Loaded: " + xhtml);
    $("#minicart").html(xhtml);
    // make sure the cart persists
    cartHTML = $('#minicart').html();
}


function showFullCartData(xml) {
    //alert("Data Loaded: " + xml);
	
    var gt = $("grandtotal",xml).text();
    $("product",xml).each(function(id) {
        product = $("product",xml).get(id);
        var pid = $("id",product).text();
        if($("delete",product).text() != "") {
            // something has been deleted
            //alert($("delete",product).text());
            var did = $("delete",product).text();
            $('#row_'+did).remove();
        }
        $("#qty_"+pid).html($("qty",product).text());
        $("#st_"+pid).html("$"+$("subtotal",product).text());
        $("#tot").html("$"+gt);
    });
}