// JavaScript Document
function swap_ship_location(){
        var us_shipping = document.getElementById("us_shipping");
        var country_shipping = document.getElementById("country_shipping");
        var domestic = document.getElementById("Domestic");
        if(domestic.checked == true)
        {
                us_shipping.style.display = "inline";
                country_shipping.style.display = "none";
        }
        else
        {
                us_shipping.style.display = "none";
                country_shipping.style.display = "inline";
        }
}


function update_total()
{
		var choose_shipping = document.getElementById("choose_shipping");
		var sub_total = document.getElementById("sub_total");
		var shipping_price = document.getElementById("shipping_price");
		var grand_total = document.getElementById("grand_total");
		if(document.getElementById("tax"))
			var tax = parseFloat(document.getElementById("tax").innerHTML);
		else
			var tax = 0;
		if(document.getElementById("ship_index"))///for finalizing the order value
		{
			var ship_index = document.getElementById("ship_index");
			ship_index.value = choose_shipping.selectedIndex;
		}
		if(choose_shipping)
		{	
			shipping_price.innerHTML = '$' + choose_shipping.options[choose_shipping.selectedIndex].value;
			grand_total.innerHTML = addCommas((parseFloat(choose_shipping.options[choose_shipping.selectedIndex].value) + parseFloat(remove_Commas(sub_total.innerHTML)) + tax).toFixed(2));
			$.cookie("choose_shipping", choose_shipping.options[choose_shipping.selectedIndex].value);
		}
}

function manual_discount()
{
	var sub_total =  parseFloat($("#sub_total_fixed").val());
	var discount = parseFloat($("#manual_order_discount").val());

	if(discount > 0)
	{
		sub_total_new = addCommas((sub_total*(1-discount)).toFixed(2));
		$("#sub_total").html(sub_total_new);
		$("#grand_total").html(sub_total_new);
		savings = (sub_total.toFixed(2) - (sub_total*(1-discount)).toFixed(2)).toFixed(2);
		$("#manual_savings").html(" | $" + savings + " savings");
		$.cookie("manual_order_discount", discount);
	}
	else if(sub_total)
	{
		$("#sub_total").html(sub_total.toFixed(2));
		$("#grand_total").html(sub_total.toFixed(2));
		$("#manual_savings").html('');
		$.cookie("manual_order_discount", null);
	}
	if(sub_total > 0)
		update_total();
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function remove_Commas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /,/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx,'');
	}
	return x1 + x2;
}
