var bIsFirebugReady = (!!window.console && !!window.console.log);

	$(document).ready(
	
		
	
		function (){
			// update the plug-in version
			$("#idPluginVersion").text($.Calculation.version);
				$("input[id^=InvoiceDataQuantity]").bind("keyup", recalc);
			recalc();
				$("input[id^=InvoiceDataRate]").bind("keyup", recalc);
			recalc();
				$("input[id^=CISAmount]").bind("keyup",recalc);
			recalc();
				$("input[id^=VatAmount]").bind("keyup",recalc);
			recalc();
				
			
			/*	$("input[id^=LessCIS]").bind("keyup", recalc);
			recalc();
			*/
			
			// automatically update the "#totalAvg" field every time
			// the values are changes via the keyup event
			
			/*$("input[name^=avg]").avg({
				bind:"keyup"
				, selector: "#totalAvg"
				// if an invalid character is found, change the background color
				, onParseError: function(){
					this.css("backgroundColor", "#cc0000")
				}
				// if the error has been cleared, reset the bgcolor
				, onParseClear: function (){
					this.css("backgroundColor", "");
				}
			});*/

			// automatically update the "#minNumber" field every time
			// the values are changes via the keyup event
			/*$("input[name^=min]").min("keyup", "#numberMin");*/

			// automatically update the "#minNumber" field every time
			// the values are changes via the keyup event
			/*$("input[name^=max]").max("keyup", {
				selector: "#numberMax"
				, oncalc: function (value, options){
					// you can use this to format the value
					$(options.selector).val(value);
				}
			});*/

			/*// this calculates the sum for some text nodes
			$("#idTotalTextSum").click(
				function (){
					// get the sum of the elements
					var sum = $(".textSum").sum();

					// update the total
					$("#totalTextSum").text('\u00A3' + sum.toString());
				}
			);*/
			
		
			// this calculates the average for some text nodes
			/*$("#idTotalTextAvg").click(
				function (){
					// get the average of the elements
					var avg = $(".textAvg").avg();

					// update the total
					$("#totalTextAvg").text(avg.toString());
				}
			);*/
		}
	);
	
	
	function recalc(){
		
		$("[id^=InvoiceDataTotal]").calc(
			"qty * price",
			{	qty: $("input[id^=InvoiceDataQuantity_]"),
				price: $("input[id^=InvoiceDataRate_]"), },
			function (s){
				return '\u00A3' + s.toFixed(2);
			},
			function ($this){
				var sum = $this.sum();
				$("#subtotal").text(
					'\u00A3' + sum.toFixed(2)
				);
			}
		);
		
		
		$("[id^=vattotal]").calc(
			"subtotal * vat /100",
			{	vat:  		$("[id^=VatAmount]"),
				subtotal: 	$("[id^=subtotal]"),
			},
			
			function (s){
				return '\u00A3' + s.toFixed(2);
			},
			
			function ($this){
				var sum = $this.sum();
				$("#vattotal").text(
					'\u00A3' + sum.toFixed(2)
				);
			}
		);
		
		$("[id^=cistotal]").calc(
			"subtotal * cis / 100",
			{	cis:  		$("[id^=CISAmount]"),
				subtotal: 	$("[id^=subtotal]"),
			},
			
			function (s){
				return '\u00A3' + s.toFixed(2);
			},
			
			function ($this){
				var sum = $this.sum();
				$("#cistotal").text(
					'\u00A3' + sum.toFixed(2)
				);
			}
		);
		
		$("[id^=grandtotal]").calc(
			"subtotal + vat",
			{	vat:		$("[id^=vattotal]"),
				subtotal: 	$("[id^=subtotal]"),
			},
			
			function (s){
				return '\u00A3' + s.toFixed(2);
			},
			
			function ($this){
				var sum = $this.sum();
				$("#grandtotal").text(
					'\u00A3' + sum.toFixed(2)
				);
			}
		);
		
		
		
		
		/*$("[id^=PaidAmount]").calc(
			// the equation to use for the calculation
			"price + qty",
			// define the variables used in the equation, these can be a jQuery object
			{
				qty: $("input[id^=LessCIS]"),
				price: $("[id^=CISAmount]")
			},
			// define the formatting callback, the results of the calculation are passed to this function
			function (s){
				// return the number as a dollar amount
				return s.toFixed(2);
			},
			// define the finish callback, this runs after the calculation has been complete
			function ($this){
				// sum the total of the $("[id^=total_item]") selector
				var sum = $this.sum();
				
				$("#grandTotal").text(
					// round the results to 2 digits
					sum.toFixed(2)
				);
			}
		);*/
		
	}
