/* Preisberechnung der Steuerkiste per Ajax im Hintergrund */
function calculatePriceByAjax()
{			
	//Formatierung der Inputs
	formatNumbers();	
	
	// get Parameter-Values
	var iProductId 								= jQuery("#productId").val();
	//var iTaxYear								= jQuery("#taxYear").val();
	var iIncome 								= jQuery("#income").val();
	var iInterestIncome 						= jQuery("#interestIncome").val();
	var iRentalIncome 							= jQuery("#rentalIncome").val();
	var iAdvancedEducationCosts 				= 0;
	var iHousekeeping 							= 0;
	var iConstructionZonesBox 					= 0;
	var iConstructionZones 						= 0;
	var iProfitOrLoss 							= 0;
	var iNumberOfDocuments 						= 0;
	var iCopies 								= 0;
	var strTaxableSalesRentalIncome 			= "";
	var strAnnualSales				 			= "";
	var strProfitAndLoss 						= "";
	
	var boolAdvancedEducationCosts 				= jQuery('#advancedEducationCosts').attr('checked');
	var boolHousekeeping 						= jQuery('#housekeeping').attr('checked');
	var boolConstructionZones 					= jQuery('#constructionZonesBox').attr('checked');
	var boolTaxableSalesRentalIncomeCheckbox 	= jQuery("#tBrWtaxforrent").attr('checked');
	
	var boolBonusSettings 						= jQuery("#bonusSettings").attr('checked');
	var boolCashBasisAccounting 				= jQuery("#cashBasisAccounting").attr('checked');
	var boolVATReturns 							= jQuery("#VATReturns").attr('checked');
	var boolBusinessTaxReturn 					= jQuery("#businessTaxReturn").attr('checked');
	var boolAssessmentDeclaration 				= jQuery("#assessmentDeclaration").attr('checked');

	//Checkboxen die geprüft werden müssen:
	if( boolAdvancedEducationCosts )
	{
		iAdvancedEducationCosts 		= jQuery("#advancedEducationCosts").val();
	}
	if( boolHousekeeping )
	{
		iHousekeeping 					= jQuery("#housekeeping").val();
	}
	if( boolConstructionZones )
	{
		iConstructionZonesBox 			= jQuery("#constructionZonesBox").val();
		iConstructionZones 				= jQuery("#constructionZones").val();
	}
	if( boolTaxableSalesRentalIncomeCheckbox )
	{
		strTaxableSalesRentalIncome		= jQuery("#tbRwRentsum").val();
	}
	if( boolBonusSettings )
	{
		iProfitOrLoss 						= jQuery("#tbRwGewinnVerlustSelect").val();
		iNumberOfDocuments 					= jQuery("#tbRwBelegeAnzahl").val();
		strAnnualSales				 		= jQuery("#tbRwUmsatz").val();
		strProfitAndLoss 					= jQuery("#tbRwGewinnVerlust").val();
		
		if( boolCashBasisAccounting )
		{
			iCopies 						= jQuery("#copies").val();
		}
	}

	if(true)
	{
		//Mauszeiger ändern und Submit-Buttons ausblenden
		document.body.style.cursor = "wait";

		//wait for the end of this requeest
		jQuery("#intoShoppingcartLink").hide();
		jQuery("#intoShoppingcartPic").show();
		
		jQuery.ajax
		(
			{
				//Ort des Skriptes in dem die Daten verarbeitet werden sollen
				url: "/_cfaction/SolseitActionTaxbox.cfc",
				type: "POST",
				//Daten die gesendet werden sollen
				data: ({
						method: "calculatePriceForTaxbox",
						productId:iProductId,
						//taxYear;iTaxYear,
						income:iIncome,
						interestIncome:iInterestIncome,
						rentalIncome:iRentalIncome,
						advancedEducationCosts:iAdvancedEducationCosts,
						housekeeping:iHousekeeping,
						constructionZonesBox:iConstructionZonesBox,
						constructionZones:iConstructionZones,
						bonusSettings:boolBonusSettings,
						taxableSalesRentalIncomeCheckbox:boolTaxableSalesRentalIncomeCheckbox,
						taxableSalesRentalIncome:strTaxableSalesRentalIncome,
						annualSales:strAnnualSales,
						profitAndLoss:strProfitAndLoss,
						profitOrLoss:iProfitOrLoss,
						cashBasisAccounting:boolCashBasisAccounting,
						VATReturns:boolVATReturns,
						businessTaxReturn:boolBusinessTaxReturn,
						assessmentDeclaration:boolAssessmentDeclaration,
						copies:iCopies,
						numberOfDocuments:iNumberOfDocuments
					}),
				dataType: "xml",
				//bei Antwort des Requests (Response)
				error: function()
				{ 
					alert('Fehler beim Abschicken des Formulares.');
				},
				success: function (responseXML) 
				{
					//XML-Daten auswerten
					buildResult( responseXML );
				}
			}
		)
		//Mauszeiger zurücksetzen 
		document.body.style.cursor = "default";
		//wait for the end of this requeest
		jQuery("#intoShoppingcartLink").show();
		jQuery("#intoShoppingcartPic").hide();
	}
}

function formatNumbers()
{	
	if( jQuery("#tBrWtaxforrent").attr('checked') )
	{
		var strTaxableSalesRentalIncome		= jQuery("#tbRwRentsum").val();
		if( strTaxableSalesRentalIncome != '')
		{
			//Punkte entfernen und Komma durch Punkt ersetzen -> 10.000.500,50 --> 10000500.50
			strTaxableSalesRentalIncome = strTaxableSalesRentalIncome.replace(/\./g, '').replace(',','.').replace(' ','');
			//Einfügen von Tausender-Punkten
			strTaxableSalesRentalIncome = jQuery().number_format(
					strTaxableSalesRentalIncome, 
				{
			     numberOfDecimals:2,
			     decimalSeparator: ',',
			     thousandSeparator: '.',
			     symbol: ''
			    });
			//schreiben ins Inputfeld
			jQuery("#tbRwRentsum").val( strTaxableSalesRentalIncome );
		}
	}
	
	if( jQuery("#bonusSettings").attr('checked') )
	{
		var strAnnualSales				 		= jQuery("#tbRwUmsatz").val();		
		if( strAnnualSales != '')
		{
			//Punkte entfernen und Komma durch Punkt ersetzen -> 10.000.500,50 --> 10000500.50
			strAnnualSales = strAnnualSales.replace(/\./g, '').replace(',','.').replace(' ','');
			//Einfügen von Tausender-Punkten
			strAnnualSales = jQuery().number_format(
				strAnnualSales, 
				{
			     numberOfDecimals:2,
			     decimalSeparator: ',',
			     thousandSeparator: '.',
			     symbol: ''
			    });
			//schreiben ins Inputfeld
			jQuery("#tbRwUmsatz").val( strAnnualSales );
		}
		
		var strProfitAndLoss 					= jQuery("#tbRwGewinnVerlust").val();
		if( strProfitAndLoss == '')
		{
			strProfitAndLoss = "0";
		}
		//Punkte entfernen und Komma durch Punkt ersetzen -> 10.000.500,50 --> 10000500.50
		strProfitAndLoss = strProfitAndLoss.replace(/\./g, '').replace(',','.').replace(' ','');
		//Einfügen von Tausender-Punkten
		strProfitAndLoss = jQuery().number_format(
				strProfitAndLoss, 
			{
		     numberOfDecimals:2,
		     decimalSeparator: ',',
		     thousandSeparator: '.',
		     symbol: ''
		    });
		//schreiben ins Inputfeld
		jQuery("#tbRwGewinnVerlust").val( strProfitAndLoss );		
	}
}
/* Schreibt den berechneten Preis in die entsprechenden Felder */
function buildResult(xmlResponse)
{
	var result 				= xmlResponse.getElementsByTagName("result");
	var iPrice 				= 0;
	var iMiddleFee 			= 0;
	var iMaximumFee 		= 0;
	var iSaleable			= 0;
	var iInklCentBetrag 	= -1;
	var strMessage			= "";

	iPrice = getElementTextNS("", "price", result[0], 0);
	iMiddleFee = getElementTextNS("", "middleFee", result[0], 0).replace('.',',');
	iMaximumFee = getElementTextNS("", "maximumFee", result[0], 0).replace('.',',');
	iSaleable = getElementTextNS("", "salable", result[0], 0);
	iSaleable = getElementTextNS("", "salable", result[0], 0);
	strMessage = getElementTextNS("", "message", result[0], 0);

	// Mindestpreis?
	if( iPrice <  74.90 )
	{
		iPrice = "ab " + jQuery("#defaultPrice").val();
		jQuery("#costs_content").hide();
		jQuery("#price").addClass( "defaultPrice" );
		jQuery("#price").removeClass( "calculatetPrice" );
	}
	else
	{
		jQuery("#costs_content").show();
		jQuery("#price").addClass( "calculatetPrice" );
		jQuery("#price").removeClass( "defaultPrice" );
	}

	//Preise anpassen
	jQuery("#price").html( "&nbsp;" + iPrice );
	jQuery("#middleFee").html( iMiddleFee );
	jQuery("#maximumFee").html( iMaximumFee );

	//Kaufbarkeit	
	if(iSaleable == 1)
	{
		jQuery("#ajaxMessage").html( strMessage );
		jQuery("#taxBoxSaleable").show();
		jQuery("#taxBoxNotSaleable").hide();
	}
	else
	{
		jQuery("#ajaxMessage").html( strMessage );
		jQuery("#taxBoxSaleable").hide();
		jQuery("#taxBoxNotSaleable").show();
	}
	
}

/* Legt die Taxbox mit den gewählten Einstellungen in den Warenkorb */
function setTaxbox2010()
{
	var boolBonusSettings 					= jQuery("#bonusSettings").attr('checked');
	var strAnnualSales				 		= jQuery("#tbRwUmsatz").val();
	var strProfitAndLoss 					= jQuery("#tbRwGewinnVerlust").val();
	var boolCheck							= true;
	
	if( boolBonusSettings && strAnnualSales != '' && ( strProfitAndLoss == 0 || strProfitAndLoss == '0,00' )  )
	{
		boolCheck = confirm("Sind Sie wirklich sicher, das Sie weder einen Gewinn noch einen Verlust gemacht haben?");
	}
	
	if(boolCheck)
	{
		//Mauszeiger ändern und Submit-Buttons ausblenden
		document.body.style.cursor = "wait";
		
		document.taxBoxSelectionForm.submit();
	}
}

function showHelp(id) {
	jQuery("#" + id).toggle("fade");
	return false;
}

function hideHelp(id) {
    jQuery("#" + id).hide("fade");
	return false;
}

function clone(o) {
  function OneShotConstructor(){}
  OneShotConstructor.prototype = o;
  return new OneShotConstructor();
}

/* Enter-Taste zu Tab-Taste um Input Felder zu überspringen */
jQuery('input').live("keypress", function(e)
{
	
	/* ENTER PRESSED*/
    if (e.keyCode == 13)
    {
    	/* nicht unter IE kleiner Version 8 */
    	if ( !( jQuery.browser.msie && jQuery.browser.version < 8 ) )
    	{    	
	        /* FOCUS ELEMENT */
	        var inputs = jQuery(this).parents("form").eq(0).find(":input");
	        var idx = inputs.index(this);
	
	        if (idx == inputs.length - 1)
	        {
	            inputs[0].select()
	        }
	        else
	        {
	            inputs[idx + 1].focus(); //  handles submit buttons
	            inputs[idx + 1].select();
	        }
	        return false;
    	}
    }
});
/* Enter-Taste zu Tab-Taste um Input Felder zu überspringen */
jQuery('select').live("keypress", function(e)
{	
	/* ENTER PRESSED*/
    if (e.keyCode == 13)
    {
    	/* nicht unter IE kleiner Version 8 */
    	if ( !( jQuery.browser.msie && jQuery.browser.version < 8 ) )
    	{ 
	        /* FOCUS ELEMENT */
	        var inputs = jQuery(this).parents("form").eq(0).find(":input");
	        var idx = inputs.index(this);
	
	        if (idx == inputs.length - 1)
	        {
	            inputs[0].select()
	        }
	        else
	        {
	            inputs[idx + 1].focus(); //  handles submit buttons
	            inputs[idx + 1].select();
	        }
	        return false;
	    }
	}
});

/* JQUERY METHODEN */
jQuery(
	function()
	{
		//Zeigt die InfoBox an
		function changeQuestion(id)
		{
			//zeige die gewählte Info
			jQuery("#" + id).toggle("fade");
		};
		
		//Blende alle InfoBoxen aus
		function hideQuestion(id)
		{
			//zeige die gewählte Info
			jQuery("#" + id).hide("fade");
		};
		
		//die Methoden mit den einzelnen IDs verbinden
		jQuery("#audioInterview").click
		(
			function()
			{
				changeQuestion('audioForTaxBox');
				return false;
			}
		);

		jQuery(function () {
		    if (jQuery.browser.msie) {
		    	jQuery('input:checkbox').click(function () {
		            this.blur();
		            this.focus();
		        });
		    }
		});

	}
);

function doHelp(config){
	jQuery(config.questionId).bind({
		click: function () {
			showHelp(config.infoId);
			return false;
		},
		mouseout: function () {
		    if (config.infoId == 'info_34') {
                return false;
			}
			hideHelp(config.infoId);
			return false;
		}
	});
}

/* 3+11 Ausnahmen, Schliessen auf Klick */
jQuery(document).ready(function(){
    for (var i = 0; i < 31; i++) {
		var config = {
			"infoId": 'info_' + i,
			"questionId": '#questionmark_' + i
		};
		doHelp(clone(config));
	}

	jQuery('div.infoPanel > a.closePanel').click(function(){
		jQuery(this).closest('div').hide();
		return false;
	});

	//Zeige die Photovoltaik Box 7
	jQuery('#cashBasisAccounting').change(function(){

		if( jQuery("#cashBasisAccounting").is(':checked') )
		{
			jQuery("#photovoltaicsPart7").show();
		}
		else
		{
			jQuery("#photovoltaicsPart7").hide();
		}
		return false;
    });

});

//Zeige die Baustellen Box
function showAndHideConstructionZonesBox()
{
	if( jQuery("#constructionZonesBox").is(':checked') )
	{
		//jQuery("#photovoltaicsPart1").show();
		jQuery("#constructionZones_on").show();
	}
	else
	{
		jQuery("#constructionZones_on").hide();
		//Darf nur versteckt werden, wenn keine Photovoltaiks ausgewählt sind
		if( !jQuery("#photovoltaics").is(':checked') )
		{
			jQuery("#photovoltaicsPart1").hide();
		}
	}	
	return false;
}

//Zeige die BonusSettings Box
function showAndHideBonusSettings()
{
	if( jQuery("#bonusSettings").is(':checked') )
	{
		jQuery("#photovoltaicsPart1").show();
		jQuery("#photovoltaicsPart2").show();
		jQuery("#photovoltaicsPart3").show();
		jQuery("#photovoltaicsPart4").show();
		jQuery("#photovoltaicsPart5").show();
		jQuery("#photovoltaicsPart6").show();
		jQuery("#photovoltaicsPart8").show();
		jQuery("#photovoltaicsPart12").show();
		//Anzahl gebundene Exempalre auch einblenden wenn die Checkbox aktiv
		if( jQuery("#cashBasisAccounting").is(':checked') )
		{
			jQuery("#photovoltaicsPart7").show();
		}
	}
	else
	{
		jQuery("#photovoltaicsPart1").hide();
		jQuery("#photovoltaicsPart2").hide();
		jQuery("#photovoltaicsPart3").hide();
		jQuery("#photovoltaicsPart4").hide();
		jQuery("#photovoltaicsPart5").hide();
		jQuery("#photovoltaicsPart6").hide();
		jQuery("#photovoltaicsPart7").hide();
		jQuery("#photovoltaicsPart8").hide();		
		jQuery("#photovoltaicsPart12").hide();
	}
	return false;									
}

/* Beim Wechsel des Steuerjahrs soll ein anderes Produkt geladen werden */
/* WknID der Kiste 2008 ist fürs SteuerJahr 2007 zuständig!! */
function changeTaxYear(url, comeFrom, wkn2005, wkn2006, wkn2007, wkn2008, wkn2009, wkn2010, wkn2011, wkn2012)
{
	var strDescription 	= jQuery('select#taxYear :selected').text();
	var i2005 			= strDescription.search(/2004/);
	var i2006 			= strDescription.search(/2005/);
	var i2007 			= strDescription.search(/2006/);
	var i2008 			= strDescription.search(/2007/);
	var i2009 			= strDescription.search(/2008/);
	var i2010 			= strDescription.search(/2009/);
	var i2011 			= strDescription.search(/2010/);
	var i2012 			= strDescription.search(/2011/);
	var iTargetWknID	= 0;
	
	if (i2005 != -1)
	{
		iTargetWknID = wkn2005;
	}
	else if (i2006 != -1)
	{
		iTargetWknID = wkn2006;
	}
	else if (i2007 != -1)
	{
		iTargetWknID = wkn2007;
	}	
	else if (i2008 != -1)
	{
		iTargetWknID = wkn2008;
	}
	else if (i2009 != -1)
	{
		iTargetWknID = wkn2009;
	}
	else if (i2010 != -1)
	{
		iTargetWknID = wkn2010;
	}
	else if (i2011 != -1)
	{
		iTargetWknID = wkn2011;
	}
	else if (i2012 != -1)
	{
		iTargetWknID = wkn2012;
	}
	
	window.location.href=url + iTargetWknID + '?comeFrom=' + comeFrom;
}
