$(document).ready(function(){
			
$('#radio1').click( function() {
            if ($('#radio1:checked').val() == 'unversichert') {
            $('input[name=versand]').val('unversichert');
            $('input[id=qty_item_12]').val('1');
            $('input[id=qty_item_13]').val('0')
            recalc();
            }
            if ($('#radio1:checked').val() == 'versichert') {
            $('input[name=versand]').val('unversichert');
            $('input[id=qty_item_12]').val('1');
            $('input[id=qty_item_13]').val('0')
            recalc();
            }
        });
        
$('#radio2').click( function() {
            if ($('#radio2:checked').val() == 'versichert') {
            $('input[name=versand]').val('versichert');
            $('input[id=qty_item_12]').val('0');
            $('input[id=qty_item_13]').val('1')
            recalc();
            }
            if ($('#radio2:checked').val() == 'unversichert') {
            $('input[name=versand]').val('versichert');
            $('input[id=qty_item_12]').val('0');
            $('input[id=qty_item_13]').val('1')
            recalc();
            }
        });
        
	//if submit button is clicked
$('#submit').click(function () {		
		//Get the data from all the fields
		var vorname = $('input[name=vorname]');
		var nachname = $('input[name=nachname]');
		var mailadresse = $('input[name=mailadresse]');
		var strasse = $('input[name=strasse]');
		var ort = $('input[name=ort]');
		var item1 = $('input[id=qty_item_1]');
		var item2 = $('input[id=qty_item_2]');
		var item3 = $('input[id=qty_item_3]');
		var item4 = $('input[id=qty_item_4]');
		var item5 = $('input[id=qty_item_5]');
		var item6 = $('input[id=qty_item_6]');
		var item7 = $('input[id=qty_item_7]');
		var item8 = $('input[id=qty_item_8]');
		var item9 = $('input[id=qty_item_9]');
		var item10 = $('input[id=qty_item_10]');
		var item11 = $('input[id=qty_item_11]');
		var versand = $('input[name=versand]');
		var total = $('input[id=grandTotal]');
		var nummer = $('input[id=random]');
		var numRand = Math.floor(Math.random()*10001);
		
		$('input[id=random]').val(numRand);
		
		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if (vorname.val()=='') {
			vorname.addClass('hightlight');
			return false;
		} else vorname.removeClass('hightlight');
		
		if (nachname.val()=='') {
			nachname.addClass('hightlight');
			return false;
		} else nachname.removeClass('hightlight');
	
		if (mailadresse.val()=='') {
			mailadresse.addClass('hightlight');
			return false;
		} else mailadresse.removeClass('hightlight');
		
		if (strasse.val()=='') {
			strasse.addClass('hightlight');
			return false;
		} else strasse.removeClass('hightlight');
	
		if (ort.val()=='') {
			ort.addClass('hightlight');
			return false;
		} else ort.removeClass('hightlight');
		
		//organize the data properly
		var data = 'vorname=' + vorname.val() + '&nachname=' + nachname.val() + '&mailadresse=' + mailadresse.val() + '&strasse=' + strasse.val() + '&ort=' + ort.val() + '&item1=' + item1.val() + '&item2=' + item2.val() + '&item3=' + item3.val() + '&item4=' + item4.val() + '&item5=' + item5.val() + '&item6=' + item6.val() + '&item7=' + item7.val() + '&item8=' + item8.val() + '&item9=' + item9.val() + '&item10=' + item10.val() + '&item11=' + item11.val() + '&versand=' + versand.val() + '&total=' + total.val() + '&numRand=' + nummer.val();
		
		//disabled all the text fields
		$('.text').attr('disabled','true');
		
		//show the loading sign
		$('.loading').show();
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "process.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {				
				//if process.php returned 1/true (send mail success)
				if (html==1) {					
					//hide the form
					$('.form').fadeOut('slow');					
					
					//show the success message
					$('.done').fadeIn('slow');
					
				//if process.php returned 0/false (send mail failed)
				} else alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
			// 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");
			
			
		}
	);
	
	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 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();
				
				$('label[name=grandTotal]').text(
					// round the results to 2 digits
					sum.toFixed(2)
				);
				$('input[id=grandTotal]').val(
				
					sum.toFixed(2)
				);
			}

		);
	}
