jQuery(document).ready(function(){
	getSplit();
								
    jQuery("#quantity").blur( function () {
		getSplit();
    });

    jQuery("#ship_usps").click( function () {
		getSplit();
    });

    jQuery("#ship_exp").click( function () {
		getSplit();
    });

});


function getSplit()
{
	ship_usps = jQuery("input#ship_usps:checked").val();
	
	if (ship_usps == "ship_usps" ) {
		shipType = "USPS";
	}
	else {
		shipType = "Express";
	}
	
	url = "http://www.thumbby.com/split.php?quantity=" + $("input#quantity").val() + 
        "&ship_type=" + shipType + 
		"&split_bin=" + $("input#split_bin").val() + 
		"&jsoncallback=?";	
		
	$.getJSON(url,
            function(data){
				//alert(data.gets);
                $('#cost').html(   "<strong> $" + Cash(data.cost)      + " </strong>");
                $('#postage').html("<strong> $" + Cash(data.ship_cost) + "</strong>");
                $('#total').html(  "<strong> $" + Cash(data.total)     + "</strong>");
				$('#split_bin').val(data.split_bin);
				$('#chargetotal').html("<input type='hidden' id='chargetotal' name='chargetotal' value='" + Cash(data.total) + "'>");
				$('#chargetotal').val(Cash(data.total));
             });

}

function Cash(money) {
    money=Math.round(money*100).toString();
    if (money<10) return '0.0'+money;
    return (money<100)?'0.'+money:+money.substring(0,money.length-2)+'.'+money.substring(money.length-2,money.length);
}
