function ivHideOverlay () {
	
	$(document).scrollTop(ivScrollTop);
	$('div.overlay').fadeOut(300);
	$('div.innerOverlay').fadeOut(300);
	if ($.browser.msie) {
		$('select').fadeIn(400);
	}
	return false;
}


function ivSwitchFillLevelWizard (tankNum) {
	$("input[name='user_iwosaveenergycheck_pi1[tanktyp]']").val(tankNum);
	$('.fillLevelWizard').hide();
	$('#fillLevelWizard'+tankNum).show();
	return false;
}


var ivFillLevelInputField;
var ivScrollTop;
var investmentCostsArray = [];


$(document).ready(function(){
	
	var messages = [
		'', 
		'Sie k&ouml;nnen 1 weitere Betankung eintragen.', 
		'Sie k&ouml;nnen bis zu 2 weitere Betankungen eintragen.', 
		'Sie k&ouml;nnen bis zu 3 weitere Betankungen eintragen.', 
		'Sie k&ouml;nnen bis zu 4 weitere Betankungen eintragen.'
	];

	var link = $('#additionalRefuelings');
	var hiddens = function(){ return $("#zukaeufe tr.hidden"); };
	
	var update = function(){
		var count = hiddens().length;
		count ? link.show().html(messages[count]) : link.hide();
	};

	link.click(function(){
		var first = hiddens()[0];
		first && $(first).removeClass("hidden");
		update();
		return false;
	});
	
	
	// event for the save button to save the whole form.
	$("#saveButton").click(function(e) {
		e.preventDefault();
		$("input[name='user_iwosaveenergycheck_pi1[cmd]']").val("save");
		$("form#energycheck_1").submit();
		
		return false;
	});
	

	// event for the save button to save the whole form.
	$("#saveButtonLogin").click(function(e) {//Redmine 9705 , a[href$='mein-profil.html?return_url=%2Fonline-berater%2Fenergiesparcheck%2Fenergieverbrauch.html']").click(function() {
		//e.preventDefault();

		$.ajax({
			type: "POST",
			url: $("form#energycheck_1").attr("action"),
			data: ($("form#energycheck_1").serialize()),
			success: function(response){				
				window.location.href = $("#saveButtonLogin").attr("href");
			},
			error: function(request, error){
				window.location.href = $("#saveButtonLogin").attr("href");
			}
		});

		//$("input[name='user_iwosaveenergycheck_pi1[cmd]']").val("save");
		//$("form#energycheck_1").submit();

		return false;
	});
	
	
	update();
});


function checkClosingInventoryDate () {
	var time = 0;
	
	for (var index = 0; index < 6; index++) {
		Math.max(time, Date.UTC(
			$('#refueled' + index + 'year').val(), 
			$('#refueled' + index + 'month').val(), 
			$('#refueled' + index + 'day').val()
		));
	}
	
	var closing = Date.UTC(
		$('#closinginventoryyear').val(), 
		$('#closinginventorymonth').val(), 
		$('#closinginventoryday').val()
	);
	/*
	if (end > closing) {
		alert ('Das Endbestandsdatum liegt vor der letzten Betankung!');
	}
	*/
}

function ivShowTankOverlay(task, field) {
	var view;
	
	if (task == 'filllevel') {
		
		if($("input[name='user_iwosaveenergycheck_pi1[tankChanged]']").val()) {
			view = $("input[name='user_iwosaveenergycheck_pi1[tanktyp]']").val();
		} else {
			view = 1;
		}
		
		ivSwitchFillLevelWizard(view);
		ivFillLevelInputField = field;
	}

	var height = Math.max(
		$.browser.opera ? window.innerHeight : $(window).height(),
		$(document).height()
	);
	
	var width = $(document).width();
	var overlayWidth = 660;
	
	ivScrollTop = $(document).scrollTop();
	$(document).scrollTop(0);
	
	if ($.browser.msie) {
		$('select').hide();
		$('div.fillLevelWizard select').show();
	}
	
	$('div.overlay').css({
		'height': height + 'px',
		'width': width + 'px'
	});

	$('div.innerOverlay').css({
		'left': ((width / 2) - (overlayWidth / 2)) + 'px',
		'top': 20 + 'px'
	});

	$('div.overlay').fadeIn(300);
	$('div.overlay').fadeTo(100, 0.8);
	$('div.innerOverlay').fadeIn(300);
	
	return false;
}

/***********
* Fuellstandsberechnung
**************************/

function calculateFillLevel() {

	//setting flag that entries for tanks have been changed
	$("input[name='user_iwosaveenergycheck_pi1[tankChanged]']").val(1);

	var name = $("input[name='user_iwosaveenergycheck_pi1[tanktyp]']").val();

	var _val = function(key){ 
		return $("#tank" + name + key).val(); 
	};

	var width 	 = _val("b");
	var height 	 = _val("h");
	var depth 	 = _val("t");
	var fill 	 = _val("f");
	var average1 = _val("d1");
	var average2 = _val("d2");
	var amount 	 = _val("a");
	
	switch (name) {
		case '1': calculateCube(width, depth, height, fill); break;
		case '2': calculateCylinder(height, average1, average2, fill); break;
		case '3': calculateTube(height, width, depth, fill); break;
		case '4': calculateSphere(average1, fill); break;
		case '5': calculateBatteryCylindric(amount, height, width, depth, fill); break;
		case '6': calculateBatteryCubic(amount, width, height, depth, fill); break;
	}

	var level = $("#calcFillLevelResult" + name).val();
	$("#" + ivFillLevelInputField).val(level);

	return false;
}


function calculateCube(width, depth, height, fill) {
	var volume = width * depth * height;
	$("#calcVolumeResult1").val(Math.round(volume / 1000));
	
	var level = width * depth * fill;
	$("#calcFillLevelResult1").val(Math.round(level / 1000));
}


function calculateCylinder(height, average1, average2, fill) {
	var volume = height * Math.PI * average1 * average2 / 4;
	$("#calcTankCapacityResult2").val(Math.round(volume / 1000));
	
	var level = fill * Math.PI * average1 * average2 / 4;
	$("#calcFillLevelResult2").val(Math.round(level / 1000));
}


function calculateTube(height, width, depth, fill) {
	var volume = calculateEllipseSurface(height, width) * depth;
	$("#calcTankCapacityResult3").val(Math.round(volume / 1000));
	
	var level = calculateEllipseSection(height, width, fill) * depth;
	$("#calcFillLevelResult3").val(Math.round(level / 1000) || "-");
}


function calculateEllipseSurface(height, width) {
	return Math.PI * height * width / 4;
}


function calculateEllipseSection(height, width, fill){
    var y2, result, height2, fill2, width2;

	height = height / 2;
    width = width / 2;

	height2 = height * height;
	fill2 = fill * fill;
	width2 = width * width;
	
    if (fill > height) {
        fill = fill - height;
        y2 = Math.sqrt((1 - (fill2 / height2)) * width2);
        result = height * width * Math.acos(fill / height) - (fill * y2);
        result = calculateEllipseSurface(height * 2, width * 2) - result;
    } else {
        fill = height - fill;
        y2 = Math.sqrt((1 - (fill2 / height2)) * width2);
        result = height * width * Math.acos(fill / height) - (fill * y2);
    }
	
	return result;
}

/** calculates and sets the volume and filllevel of a cylindric tank
* 	@param int amount amount of cells
*   @param int height height of cells
*   @param int width width of cells
*   @param int length length/depth of cells
*   @param int fill fill-height
*/
function calculateBatteryCylindric(amount, height, width, depth, fill) {
	var volume = amount * calculateEllipseSurface(height, width) * length ;
	$("#calcTankCapacityResult5").val(Math.round(volume / 1000));
	
	var level = amount * calculateEllipseSection(height, width, fill) * depth;
	$("#calcFillLevelResult5").val(Math.round(level / 1000));
	
	return false;
}

/**
 * @param int amount amount of cells
 * @param int height height of cells
 * @param int width width of cells
 * @param int depth length/depth of cells
 * @param int fill fill-height
 * @return
 */
function calculateBatteryCubic(amount, width, height, depth, fill) {
	var volume = amount * width * length * height;
	$("#calcTankCapacityResult6").val(Math.round(volume / 1000));
	
	var level = amount * width * depth * fill;
	$("#calcFillLevelResult6").val(Math.round(level/ 1000));
	
	return false;
}


function calculateSphere(height, fill) {
	
	var volume = (4 / 3) * Math.PI * Math.pow(height / 2, 3);
	$("#calcTankCapacityResult4").val(Math.round(volume / 1000));
	
	var level = (1 / 3) * Math.PI * Math.pow(fill, 2) * (1.5 * height - fill);
	$("#calcFillLevelResult4").val(Math.round(level / 1000));
	
	return false;
}

