$(document).ready(function(){
	$("#couponform").validate({
		rules: {
			shipping_salutation: {
				required: true,
				minlength: 2
			},
			salutation: {
				required: true,
				minlength: 2
			},
			surname: {
				required: true,
				minlength: 2
			},
			lastname: {
				required: true,
				minlength: 2
			},
			street: {
				required: true,
				minlength: 2
			},
			city: {
				required: true,
				minlength: 2
			},
			zipcode: {
				required: true,
				minlength: 2
			},
			country: {
				required: true,
				minlength: 1
			},
			email: {
				required: true,
				email: true
			},
			shipping_surname: {
				required: true,
				minlength: 2
			},
			shipping_lastname: {
				required: true,
				minlength: 2
			},
			shipping_street: {
				required: true,
				minlength: 2
			},
			shipping_city: {
				required: true,
				minlength: 2
			},
			shipping_zipcode: {
				required: true,
				minlength: 2
			},
			shipping_country: {
				required: true,
				minlength: 2
			},
			agb: {
				required: true
			},

			mailing: {
				required: true
			}
		},
		messages: {
				agb: "Sie m&uuml;ssen unsere AGBs akzeptieren.",
				email: "Bitte geben Sie eine g&uuml;ltige E-Mail Adresse an",
				mailing: "Bitte w&auml;hlen Sie eine Versandart aus"
		}
	});

	//  $("#couponform").validate();

	//code to hide topic selection, disable for demo
	var coup_ad = $("#coup_ad");
	// newsletter topics are optional, hide at first
	var inital = coup_ad.is(":checked");
	var topics = $("#coupon_adress")[inital ? "removeClass" : "addClass"]("gray");
	var topicInputs = topics.find("input").attr("disabled", !inital);
	// show when newsletter is checked
	coup_ad.click(function() {
		topics[this.checked ? "removeClass" : "addClass"]("gray");
		topicInputs.attr("disabled", !this.checked);
	});
});




function showCoupenDescription(desc) {
  $(document).ready(function() {
    $("div.desc"+desc).toggle('slow');
  });
}

function showAgbText() {
  $(document).ready(function() {
    $("div.agbText").toggle('slow');
  });
}

	var bIsFirebugReady = (!!window.console && !!window.console.log);

	$(document).ready(
		function (){
			// update the plug-in version
			//$("#idPluginVersion").text($.Calculation.version);

/*
			$.Calculation.setDefaults({
				onParseError: function(){
					this.css("backgroundColor", "#cc0000")
				}
				, onParseClear: function (){
					this.css("backgroundColor", "");
				}
			});
*/

			// bind the recalc function to the quantity fields
			$("input[@name^=qty_item_]").bind("keyup", recalc);
			// run the calculation function now
			recalc();

			// automatically update the "#totalSum" field every time
			// the values are changes via the keyup event
			$("input[@name^=sum]").sum("keyup", "#totalSum");

			// 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", "#numberMax");

			// 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("Euro " + 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^=total_item]").calc(
			// the equation to use for the calculation
			"qty * price",
			// define the variables used in the equation, these can be a jQuery object
			{
				qty: $("input[@name^=qty_item_]"),
				price: $("[@id^=price_item_]")
			},
			// define the formatting callback, the results of the calculation are passed to this function
			function (s){
				// return the number as a dollar amount
				return "Euro " + 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
						"Euro " + sum.toFixed(2)
				);
			}
		);
	}

