jQuery.noConflict();

jQuery(document).ready(function ($) {
		
		jQuery(".calc.tx-powermail-pi1_fieldset select option").each(function(index){
				var selectvalue = jQuery(this).val();
				var selecttext = jQuery(this).text();
				//jQuery(this).attr('rel') = selecttext;
				jQuery(this).attr('rel', selectvalue);
				jQuery(this).val(selecttext);
		});	
		
		
		if(jQuery("#antragunfallmotorsport").length > 0) {
			jQuery("#antragunfallmotorsport select").bind("change", calcunfallmotor);
			calcunfallmotor();
		}
		jQuery(".tx-powermailcalc-pi1 select").bind("change", calcunfallmotor);
		
		if(jQuery("#antragunfallmotorsportdeckung").length > 0) {
			jQuery("#antragunfallmotorsportdeckung select").bind("change", calcunfallmotordeckung);
			calcunfallmotordeckung();
		}
		
		jQuery(".calc_betrag").css("font-weight","bold");
		
		
});



function calcunfallmotor(){
		
		//var grundsumme_array = jQuery("select.calc_grundsumme :selected").val().split("-");
		//jQuery("fieldset.calc .calc_anzahl :selected").attr('rel'),
		
		if(jQuery("select.calc_grundsumme :selected").attr('rel')) {
			var grundsumme_array = jQuery("select.calc_grundsumme :selected").attr('rel').split("-");
		}
		
		if(jQuery("select.calc_beruf :selected").attr('rel')) {
			var arraykey = jQuery("select.calc_beruf :selected").attr('rel');
			var grundsumme = grundsumme_array[arraykey];
		} 
		
		if(jQuery("select.calc_zahlweise :selected").attr('rel') == 12) {
			var grundsumme = (0.95 * grundsumme);
		}
		if(jQuery("select.calc_zahlweise :selected").attr('rel') == 6) {
			var grundsumme = ((0.95 * grundsumme) / 0.97);
		}
		
		jQuery("input.calc_betrag").calc(
			// the equation to use for the calculation
			"(grundsumme / laufzeitwert) * zahlweisewert",
			// define the variables used in the equation, these can be a jQuery object
			{
				grundsumme: grundsumme,
				zahlweisewert: jQuery(".calc_zahlweise :selected").attr('rel'),
				laufzeitwert: jQuery(".calc_laufzeit :selected").attr('rel')
				
			},
			
			// 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) + " Euro ";
			},
			// 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 = jQuery(this).sum();
				jQuery(".calc_betrag").val(
					// round the results to 2 digits
					sum.toFixed(2) + " Euro "
				);
			}
		);
		
}

function calcunfallmotordeckung(){
		
		//var grundsumme_array = jQuery("select.calc_grundsumme :selected").val().split("-");
		if(jQuery("select.calc_grundsumme :selected").attr('rel')) {
			var grundsumme_array = jQuery("select.calc_grundsumme :selected").attr('rel').split("-");
		}
		
		if(jQuery("select.calc_beruf :selected").attr('rel')) {
			var arraykey = jQuery("select.calc_beruf :selected").attr('rel');
			var grundsumme = grundsumme_array[arraykey];
		} 
				
		jQuery("input.calc_betrag").calc(
			// the equation to use for the calculation
			"grundsumme / zahlweisewert",
			// define the variables used in the equation, these can be a jQuery object
			{
				grundsumme: grundsumme,
				zahlweisewert: jQuery(".calc_zahlweise :selected").attr('rel')
				
			},
			
			// 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) + " Euro ";
			},
			// 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 = jQuery(this).sum();
				jQuery(".calc_betrag").val(
					// round the results to 2 digits
					sum.toFixed(2) + " Euro "
				);
			}
		);
		
}



