﻿$(document).ready(function()
{
	var productColours = ["#7e1206", "#171851", "#487091"];
	var calcType = $("#CalculatorForm").hasClass("super") ? "super" : "investment";
	$("#ClearButton").click(function()
	{
		$("#CalculatorForm input:text").val("");
		$("#CalculatorResult").slideUp();
		return false;
	});

	$("#CalculatorForm").submit(function()
	{
		$("#ResultChart").html("");
		$("#ResultTable").hide();
		$("#CalculatorResult").slideDown();
		$.scrollTo("#CalculatorResult", 500, { easing: 'swing', offset: { top: 0, left: 0} });
		$("#CalculatorResultData").addClass("Loading");

		var form = $(this);
		var data = {};
		$("#CalculatorForm input:text").each(function(index, item)
		{
			data[item.name] = $(item).val();
		});
		$.post(form.attr("action"), data, function(result)
		{
			// Bind solution
			$("#CalculatorSolution").html(result.K30);
			switch (result.K30.toUpperCase())
			{
				case "ASSETLINK INVESTMENT WRAP":
				case "ASSETLINK SUPERWRAP":
					$("#CalculatorSolution").css("color", productColours[0]);
					break;
				case "ASSETCHOICE INVESTMENT WRAP":
				case "ASSETCHOICE SUPERWRAP":
					$("#CalculatorSolution").css("color", productColours[1]);
					break;
				case "PORTFOLIO ADMINISTRATOR":
				case "PORTFOLIO ADMINISTRATOR SUPERWRAP":
					$("#CalculatorSolution").css("color", productColours[2]);
					break;
			}

			// Bind chart
			try
			{
				var chartData = (calcType == "super" ? [[result.H45], [result.O26], [result.W26]] : [[App.parseMoney(result.H43)], [App.parseMoney(result.O25)], [App.parseMoney(result.W25)]]);
				$.jqplot("ResultChart", chartData,
				{
					seriesColors: productColours,
					seriesDefaults:
					{
						renderer: $.jqplot.BarRenderer,
						rendererOptions: { barWidth: 65, barPadding: 26 },
						shadow: false
					},
					axes:
					{
						xaxis: { numberTicks: 0, showTicks: false },
						yaxis: { numberTicks: 10, autoscale: true, tickOptions: { formatString: (calcType == "super" ? "%.2f\%" : "$%.2f"), fontFamily: "Arial", fontSize: "11px" }, showTickMarks: false }
					},
					grid:
					{
						shadow: false,
						background: "#f0f0f0",
						gridLineColor: "#d2d2d2",
						borderWidth: 0.9,
						borderColor: "#d2d2d2"
					}
				});
			}
			catch (ex) { }

			// Bind table
			$("#ProductHeader1").css("color", productColours[0]);
			$("#ProductHeader2").css("color", productColours[1]);
			$("#ProductHeader3").css("color", productColours[2]);
			if (calcType == "super")
			{
				$("#CellH40").html(result.H40);
				$("#CellH41").html(result.H41);
				$("#CellH42").html(result.H42);
				$("#CellH43").html(result.H43);
				$("#CellO21").html(result.O21);
				$("#CellO22").html(result.O22);
				$("#CellO23").html(result.O23);
				$("#CellO24").html(result.O24);
				$("#CellW21").html(result.W21);
				$("#CellW22").html(result.W22);
				$("#CellW23").html(result.W23);
				$("#CellW24").html(result.W24);
				$("#ProductTotal1").css("color", productColours[0]).html(result.H44);
				$("#ProductTotal2").css("color", productColours[1]).html(result.O25);
				$("#ProductTotal3").css("color", productColours[2]).html(result.W25);
				$("#ProductTotalPercent1").css("color", productColours[0]).html(result.H45 + "%");
				$("#ProductTotalPercent2").css("color", productColours[1]).html(result.O26 + "%");
				$("#ProductTotalPercent3").css("color", productColours[2]).html(result.W26 + "%");
			}
			else
			{
				$("#CellH40").html(result.H40);
				$("#CellH41").html(result.H41);
				$("#CellH42").html(result.H42);
				$("#CellO22").html(result.O22);
				$("#CellO23").html(result.O23);
				$("#CellO24").html(result.O24);
				$("#CellW22").html(result.W22);
				$("#CellW23").html(result.W23);
				$("#CellW24").html(result.W24);
				$("#ProductTotal1").css("color", productColours[0]).html(result.H43);
				$("#ProductTotal2").css("color", productColours[1]).html(result.O25);
				$("#ProductTotal3").css("color", productColours[2]).html(result.W25);
				$("#ProductTotalPercent1").css("color", productColours[0]).html(result.H44 + "%");
				$("#ProductTotalPercent2").css("color", productColours[1]).html(result.O26 + "%");
				$("#ProductTotalPercent3").css("color", productColours[2]).html(result.W26 + "%");
			}

			// Done
			$("#ResultTable").show();
			$("#CalculatorResultData").removeClass("Loading");
		}, "json");
		return false;
	});
});
