var baseUrl="";

$(function() {

	$("#postageOptions").change(changePostageOptions);

	$("#ship_postcode").change(changePostageOptions);

	function changePostageOptions() {

		// Display "loading..."
		$("#finalPostageCost").html("<img src='images/ajax-loader.gif' title='loading...' alt='loading...' />");
		$("#finalTax").html("<img src='images/ajax-loader.gif' title='loading...' alt='loading...' />");
		$("#total_price").html("<img src='images/ajax-loader.gif' title='loading...' alt='loading...' />");
		var subtotal = $("#subtotal").html().substr(1);			// Take off "$"
		
		// reset coupon code and calculations
		$("#coupon_code").val() == "";
		$("#coupon_code").html() == "";
		$("#disc_table").hide();
		$("#kindy_table").hide();
		$("#totalLabelText").css("text-decoration", "");
		$("#total_price").css("text-decoration", "");

		if ($("#postageOptions").val() != "collect")
		{
			$.get("https://secure4.ingenuity.net.au/gabbyglamour/savePostageOptions.php", { postageOptions: $("#postageOptions").val(), postcode: $("#ship_postcode").val()}, 
				function(data){
						if(data<0) {
							$("#finalPostageCost").html("Unknown");
							$("#finalTax").html("Unknown");
							$("#total_price").html("Unknown");
						} else {
							$("#finalPostageCost").html("$" + parseFloat(data).toFixed(2));
							$("#originalFreight").val(data);
							
							var total = parseFloat(subtotal) + parseFloat(data);
							var gst = total / 11;

							$("#finalTax").html("$" + String(gst.toFixed(2)));
							$("#total_price").html("$" + String(total.toFixed(2)));
						}
			 }, "text");
		 } else {
				var total = parseFloat(subtotal);
				var gst = total / 11;

				$("#finalPostageCost").html("$0.00");
				$("#finalTax").html("$" + String(gst.toFixed(2)));
				$("#total_price").html("$" + String(total.toFixed(2)));
		}

		 

		//displayPostageCost();
	}

	/*$("#internationalOptions").change(function() {
		$.get(baseUrl+"savePostageOptions.php", { internationalOptions: $(this).val() });
		displayPostageCost();
	});

	$("#ausInsuranceOptions").change(function() {
		$.get(baseUrl+"savePostageOptions.php", { ausInsuranceOptions: $(this).val() });
		displayPostageCost();
	});

	$("#internationalInsuranceOption").change(function() {
		$.get(baseUrl+"savePostageOptions.php", { internationalInsuranceOption: $(this).val() });
		updatePaymentOptions();
	});
	$("#otherIntDeliveryOptions").click(function() {
		$.get(baseUrl+"savePostageOptions.php", { otherIntDeliveryOptions: $(this).attr("checked") });
		updatePaymentOptions();
	});

	$("#weight").keyup(function() {
		var cleanText = $("#weight").val().replace(/^0-9\./g, '');
		$("#weight").val(cleanText);

		displayPostageCost();
	});*/

});

function updatePaymentOptions() {
	
	// If they want to be contacted about International Insurance
	if ($("#postageOptions").val() > 3 && ($("#otherIntDeliveryOptions").attr("checked") || $("#internationalInsuranceOption").val()==1))
	{
		$("input[name=paymethod]").attr("disabled", true);

		//Contact us is the only payment option
		$($("input[name=paymethod]")[3]).attr("disabled", false);
		$($("input[name=paymethod]")[3]).attr("checked", true);
	}
	else
	{
		$("input[name=paymethod]").attr("disabled", false);
	}
	
	// Money Order / Cheque is Aust Only
	if ($("#postal_country").val()=="Australia")
	{
		$("input[rel=AusOnly]").attr("disabled", false);
	}
	else
	{
		$("input[rel=AusOnly]").attr("disabled", true);
		$("input[rel=AusOnly]").attr("checked", false);
	}
}




function displayPostageCost() {
	
	var weight = $("#weight").val();
	var cost = 0;

	updatePaymentOptions();

	// Standard Post Within Australia
	if ($("#postageOptions").val()==1) {
		if (weight <= 0.5) {
			cost = 7.5;
		}
		else if (weight <= 3) {
			cost = 10.5;
		}
		else if (weight <= 5) {
			cost = 13.50;
		}
		else if (weight <= 8) {
			cost = 17.5;
		}
		else {
			cost = 22;
		}
	}


	// Registered Post Within Australia
	if ($("#postageOptions").val()==2) {
		if (weight <= 0.5) {
			cost = 10.2;
		}
		else if (weight <= 3) {
			cost = 13.2;
		}
		else if (weight <= 5) {
			cost = 17.2;
		}
		else if (weight <= 8) {
			cost = 20.2;
		}
		else {
			cost = 24.7;
		}
		
		if ($("#ausInsuranceOptions").val()) {
			cost = parseFloat(cost) + parseFloat($("#ausInsuranceOptions").val());
		}
	}

	// Express Post Within Australia
	if ($("#postageOptions").val()==3) {
		if (weight <= 0.5) {
			cost = 10.5;
		}
		else if (weight <= 1) {
			cost = 15;
		}
		else if (weight <= 2) {
			cost = 20;
		}
		else if (weight <= 3) {
			cost = 25;
		}
		else if (weight <= 5) {
			cost = 35;
		}
		else if (weight <= 8) {
			cost = 60;
		}
		else {
			cost = 80;
		}
	}		

	// International Air Mail
	if ($("#postageOptions").val()==4) {
		var locationCost = [];
		locationCost[0] = 0;
		if (weight <= 0.25) {
			locationCost[1] = 10.45;
			locationCost[2] = 12.65;
			locationCost[3] = 14.85;
			locationCost[4] = 18.15;
		}
		else if (weight <= 0.5) {
			locationCost[1] = 14.05;
			locationCost[2] = 17.35;
			locationCost[3] = 20.65;
			locationCost[4] = 25.60;
		}
		else if (weight <= 0.75) {
			locationCost[1] = 17.65;
			locationCost[2] = 22.05;
			locationCost[3] = 26.45;
			locationCost[4] =33.05;
		}
		else if (weight <= 1) {
			locationCost[1] = 21.25;
			locationCost[2] = 26.75;
			locationCost[3] = 32.25;
			locationCost[4] = 40.50;
		}
		else if (weight <= 1.25) {
			locationCost[1] = 31.25;
			locationCost[2] = 36.75;
			locationCost[3] = 42.25;
			locationCost[4] = 50.50;
		}
		else if (weight <= 1.5) {
			locationCost[1] = 34.85;
			locationCost[2] = 41.45;
			locationCost[3] = 48.05;
			locationCost[4] = 57.95;
		}
		else if (weight <= 1.75) {
			locationCost[1] = 38.45;
			locationCost[2] = 46.15;
			locationCost[3] = 53.85;
			locationCost[4] = 65.40;
		}
		else if (weight <= 2) {
			locationCost[1] = 42.05;
			locationCost[2] = 50.85;
			locationCost[3] = 59.65;
			locationCost[4] = 72.85;
		}
		else {
			// Extra 500g or part thereof
			var locationFactor = [];
			locationFactor[0] = 0;
			locationFactor[1] = 3.8;
			locationFactor[2] = 4.85;
			locationFactor[3] = 7.05;
			locationFactor[4] = 9.35;

			// On top of the maximum cost
			locationCost[1] = 42.05;
			locationCost[2] = 50.85;
			locationCost[3] = 59.65;
			locationCost[4] = 72.85;

			cost = parseFloat(Math.ceil(((weight*1000)-2000)/500)*locationFactor[$("#internationalOptions").val()]);
			//console.log(cost);
		}
		cost += parseFloat(locationCost[$("#internationalOptions").val()]);
	}

	// International Registered Post
	if ($("#postageOptions").val()==5) {
		var locationCost = [];
		locationCost[0] = 0;
		if (weight <= 0.5) {
			locationCost[1] = 19.05;
			locationCost[2] = 22.35;
			locationCost[3] = 25.65;
			locationCost[4] = 30.60;
			cost = locationCost[$("#internationalOptions").val()];
		}
		else if (weight <= 0.75) {
			locationCost[1] = 22.65;
			locationCost[2] = 27.05;
			locationCost[3] = 31.45;
			locationCost[4] = 38.05;
			cost = locationCost[$("#internationalOptions").val()];
		}
		else if (weight <= 1) {
			locationCost[1] = 26.25;
			locationCost[2] = 31.75;
			locationCost[3] = 37.25;
			locationCost[4] = 45.5;
			cost = locationCost[$("#internationalOptions").val()];
		}
		else if (weight <= 1.25) {
			locationCost[1] = 36.25;
			locationCost[2] = 41.75;
			locationCost[3] = 47.25;
			locationCost[4] = 55.5;
			cost = locationCost[$("#internationalOptions").val()];
		}
		else if (weight <= 1.5) {
			locationCost[1] = 39.85;
			locationCost[2] = 46.45;
			locationCost[3] = 53.05;
			locationCost[4] = 62.95;
			cost = locationCost[$("#internationalOptions").val()];
		}
		else if (weight <= 1.75) {
			locationCost[1] = 43.45;
			locationCost[2] = 51.15;
			locationCost[3] = 58.85;
			locationCost[4] = 70.4;
			cost = locationCost[$("#internationalOptions").val()];
		}
		else if (weight <= 2) {
			locationCost[1] = 47.05;
			locationCost[2] = 55.85;
			locationCost[3] = 64.65;
			locationCost[4] = 77.85;
			cost = locationCost[$("#internationalOptions").val()];
		}
		else {
			alert('International Registered Post is only available up to a maximum of 2kg’s weight. For orders heavier than this other registered options are available, like international couriers. Please advise if you would like us to advise costs for these services (using the checkbox below), otherwise you can select to send your order via International air mail above.');
			$("#finalPostageCost").html('To be advised');

			//Contact us is the only payment option
			$("input[name=paymethod]").attr("disabled", true);
			$($("input[name=paymethod]")[3]).attr("disabled", false);
			$($("input[name=paymethod]")[3]).attr("checked", true);

			cost=0;

			$.get("https://secure4.ingenuity.net.au/gabbyglamour/savePostageOptions.php", { finalPostageCost: 0 }, function() {
				// OUTSIDE Australia, GST DOES NOT includes Freight.
				var gst = parseFloat($("#originalprice").val()) / 11;
				$("#incGSTtext").html("Less GST (for delivery outside Australia)");
				$("#finalTax").html("-$"+gst.toFixed(2));
				$("#totalprice").val($("#originalprice").val()-gst+parseFloat(cost));
				var myTotalPrice = $("#totalprice").val();
				$("#total_price").html("$"+parseFloat(myTotalPrice).toFixed(2));
				return;
			});	
		}
		
	}		

	// reset coupon code and calculations
	$("#coupon_code").val() == "";
	$("#coupon_code").html() == "";
	$("#disc_table").hide();
	$("#kindy_table").hide();

	// Display final postage cost if it was determined
	if (cost) {
		$.get(baseUrl+"https://secure4.ingenuity.net.au/gabbyglamour/savePostageOptions.php", { finalPostageCost: cost });

		//totalprice - the field
		$("#totalprice").val(parseFloat($("#originalprice").val())+parseFloat(cost));
		
		//total_price - the display
		var myTotalPrice = $("#totalprice").val();
		$("#total_price").html("$"+parseFloat(myTotalPrice).toFixed(2));

	}
	$("#finalPostageCost").html("$"+cost.toFixed(2));

	// update gst display
	if ($("#postal_country").val()=="Australia")
	{
		// In Australia, GST includes Freight.
		var gst = (parseFloat($("#originalprice").val())+parseFloat(cost)) / 11;
	
		//$("#incGSTtext").html("The total includes GST of");
		//$("#finalTax").html("$"+gst.toFixed(2));

		// Total, THEN tax
		var myTotalPrice = $("#totalprice").val();
		$("#incGSTtext").html("Total");
		$("#incGSTtext").css("font-weight", "bold");
		$("#finalTax").css("font-weight", "bold");
		$("#finalTax").html("$"+parseFloat(myTotalPrice).toFixed(2));	//so total is in where tax usually is

		$("#totalLabelText").html("The total includes GST of");
		$("#totalLabelText").css("font-weight", "normal");
		$("#total_price").css("font-weight", "normal");
		$("#total_price").html("$"+gst.toFixed(2));
		
		/*
		$("#totalprice").val($("#totalprice").val()-gst);
		var myTotalPrice = $("#totalprice").val();
		$("#total_price").html("$"+parseFloat(myTotalPrice).toFixed(2));
		*/
	}
	else {
		// OUTSIDE Australia, GST DOES NOT includes Freight.
		var gst = parseFloat($("#originalprice").val()) / 11;

		$("#finalTax").css("font-weight", "normal");
		$("#incGSTtext").css("font-weight", "normal");
		$("#incGSTtext").html("Less GST (for delivery outside Australia)");
		$("#finalTax").html("-$"+gst.toFixed(2));
		
		$("#totalLabelText").html("Total");
		$("#totalLabelText").css("font-weight", "bold");
		$("#total_price").css("font-weight", "bold");
		$("#totalprice").val($("#originalprice").val()-gst+parseFloat(cost));
		var myTotalPrice = $("#totalprice").val();
		$("#total_price").html("$"+parseFloat(myTotalPrice).toFixed(2));
	}
	
	$("#shopCartTable").load("https://secure4.ingenuity.net.au/gabbyglamour/cartTable.php");
}
