﻿
/*
	-----------------------------------------------------------------------------------------------
	JQuery Cost Calculator v 1.0
	(C) Copyright 2009 MC Creative & Product Ltd (MC+Co) (solutions@mc-co.co.uk)
	All rights reserved.
	Developed under licence to Utilize Plc.
	-----------------------------------------------------------------------------------------------
*/

// Define plugin enclosure
; (function($) {

	// Public Object
	$.fn.calc = function(opts) {

		// Manage multiple instances
		return this.each(function() {

			// Create a reference to the instance
			var instance = $(this);

			// Merge defaults and options
			var options = $.extend($.fn.calc.defaults, opts);

			// Process the config
			var config = $("#" + options.config);

			reset();

			instance.find(".usersBox").bind("click select", function() {
				//reset();
			});
			instance.find(".calcButton").bind("click", function() {
				doCalc();
			});
			instance.find(".usersBox").bind("change", function() {
				doCalc();
			});
			instance.find(".usersBox").keypress(function(e) {
				if (e.which == 13) {
					instance.find(".calcButton").click();
					e.preventDefault();
					return false;
				}
			});
			instance.find(".buyNowButton").click(function() {
				if (!$("input[@name='option_layout']:checked").val()) {
					alert("Please select a pricing option using the calculator below.");
					return false;
				}
			});

			function reset() {
				instance.find(".config").hide();
				instance.find("#OutOfBounds").hide();
				instance.find(".dynamic").remove();
				instance.find("#CalcOptions").hide();
				instance.find("#CalcWaiting").show();
			}

			function prepTo2Dec(val) {
				return (val).toFixed(2);
			}

			function doCalc() {

				reset();

				var calcPerformed = false;
				var ref = config.attr("ref");
				var name = config.attr("name");
				var users = parseInt(instance.find(".usersBox").val());
				if (isNaN(users)) { users = 0; }

				users = parseInt(users);

				//alert("Config length: " + config.length);

				config.find("span").each(function() {

					var band = $(this);
					var min = parseInt(band.attr("min"));
					var max = parseInt(band.attr("max"));
					var price = parseFloat(band.attr("price"));
					var rrp = parseFloat(band.attr("rrp"));
					var save = 0;
					var years = parseInt(band.attr("years"));

					//alert("BandMin: " + min + " BandMax:" + max);

					if (users >= min) {

						if (users <= max) {

							calcPerformed = true;

							// Generate price options for selected band

							//alert("Found band: " + min + " - " + max + ".");

							instance.find(".dynamic").remove();

							var tbl = instance.find(".optionsTable");
							for (var i = 1; i < years + 1; i++) {

								var value = price;
								var rrpVal = rrp;

								if (i > 1) {
									rrpVal = rrp + ((rrp / 2) * (i - 1));
								}

								if (price == 0) {
									value = rrpVal - ((rrpVal / 100) * options.priceDiscount);
								} else {
									value = price + ((price / 2) * (i - 1));
								}

								value = (value * users);
								rrpVal = (rrpVal * users);

								value = prepTo2Dec(value);
								rrpVal = prepTo2Dec(rrpVal);

								var lblYears = (i > 1) ? 'years' : 'year';
								var radio = $('<input type="radio" name="priceOption" id="priceOption"> ' + i + ' ' + lblYears + '</input>');

								var radioValue = name + ' ## ' + users + ' licences, ' + i + ' ' + lblYears + ' ## Price: £' + value;
								radio.val(radioValue);

								var tr = $('<tr class="dynamic"></tr>');
								var tdo = $('<td class="opt"></td>');
								tdo.append(radio);
								var tdp = $('<td class="pay">£' + value + '</td>');
								var tdr = $('<td class="rrp">£' + rrpVal + '</td>');
								var tds = $('<td class="save">£' + (prepTo2Dec(rrpVal - value)) + '</td>');

								tr.append(tdo);
								tr.append(tdp);
								tr.append(tdr);
								tr.append(tds);

								tbl.append(tr);

								instance.find("#CalcWaiting").hide();
								instance.find("#CalcOptions").show();

							} // for
						} // if
					} // if

				}); // config.each()

				instance.find(".noUsers").html(0 + users + ' ');

				if (!calcPerformed) {
					reset();
					instance.find("#CalcWaiting").hide();

					if (users == 0) {
						instance.find("#OutOfBounds").html('Please enter a required number of licences.').show();
					} else {
						instance.find("#OutOfBounds").html('If you require ' + users + ' licences, please contact us directly for custom pricing options.').show();
					}
				}
			}

		}); // this.each
	};

	$.fn.calc.defaults = {
		priceDiscount: 0
	}

})(jQuery);