function goto(url) 
{
	document.location.href = document.getElementsByTagName('base')[0].href + url;
}

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function enableZipcodeLookup(source, target)
{
	Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
	
	if($(source) && $(target))
	{					
		$(target).setStyle({visibility: 'hidden', left: '-99999px', position: 'absolute'});
		
		if(!Prototype.Browser.IE7)
		{
			if($(target).parentNode.getElementsBySelector('br')[0])
				$(target).parentNode.getElementsBySelector('br')[0].remove();

			if($(target).parentNode.getElementsBySelector('span')[0]) 
				$(target).parentNode.getElementsBySelector('span')[0].remove();
		}				
		
		$(target).parentNode.innerHTML += '<input type="text" id="' + target + '_visual" class="inputText disabled special" disabled="disabled" /><span class="textRequired"> *</span><br clear="all" class="clear" />';
		
		$(source).observe('change', function(event) {
			
			if(isNumber(this.value) && this.value.length == 4)
			{
				new Ajax.Request('/de/privatpersonen/preis_und_online_abschluss/ajax=getCity', {
				 	method: 'post',
					parameters: { zipcode: $(source).value },
					onSuccess: function(transport) {
						if(transport.responseText != '')
						{
							$(target).value = transport.responseText;
							$(target + '_visual').value = transport.responseText;
							$(source).removeClassName('inputFieldError');
						}
						else
						{
							$(target).value = '';
							$(target + '_visual').value = '';
							$(source).addClassName('inputFieldError');
						}
					}
				});
			}
			else
			{
				$(target).value = '';
				$(target + '_visual').value = '';
				$(source).addClassName('inputFieldError');
			}
		});
	}
}

Array.prototype.removeByValue = function(value)
{
	if(value)
	{
		var counter = 0;
		var array = this;
		this.each(function(element) {
			if(element == value) array.splice(counter, 1);		
		
			counter++;
		});
	}
}

/* acronym */
function acronym (word, popupId)
{
	myLightWindow.activateWindow({
		title: word,
		href: "#" + popupId,
		type: 'inline'
	});	
}

/* DAS */
var objectValueMap			= 0;
var activeQuestionObject 	= null;
var questionIds 			= Array ();
var questionIdsIndexes 		= Array ();
var additions = [];
var groups = [];
var myLightWindow;
var previous_costs = 0;
var popup_lower_costs = false;
var skip_questions = Array ();

function DasTip(tip)
{
	Tip(tip, WIDTH, 200, PADDING, 10, BGCOLOR, '#FEFFED', BORDERCOLOR, '#E4E393', DELAY, 200);
}

function checkLength(self, maxLen)
{
	if(self.value.length >= maxLen) 
	{		
		self.value = self.value.substring(0, maxLen);
	}
	else 
	{
		$(self.parentNode).select("span.counter")[0].update(maxLen - self.value.length - 1);
	}
}

/* CUSTOM QUESTION FUNCTION */
function checkType(action, questionOne, questionSix)
{
	if(typeof(questionOne) == 'undefined')
		var questionOne = getAnswerByQuestion($('question_1'));
		
	var questionTwo = getAnswerByQuestion($('question_2'));
	var questionFour = getAnswerByQuestion($('question_4'));
	
	if(typeof(questionSix) == 'undefined')	
		var questionSix = getAnswerByQuestion($('question_6'));
	
	var showPrivate = false;
	var showTraffic = false;

	switch(questionOne)
	{
		case '1': // Ja, Privat + Verkehr
		 	if(questionTwo == '1' || questionFour == '1') // DAS or Bereits geundigt
			{
				showPrivate = true;
				showTraffic = true;
			}
			break;
			
		case '2': // Ja, nur Privat
			showTraffic = true;
			if(questionTwo == '1' || questionFour == '1') // DAS
			{
				showPrivate = true;
			}
			break;
			
		case '3': // Ja, nur Verkehr
			showPrivate = true;
			if(questionTwo == '1' || questionFour == '1') // DAS
			{
				showTraffic = true;
			}
			break;
			
		case '4': // Nein
			switch(questionSix)
			{
				case '1':
					showPrivate = true;
					showTraffic = true;
					break;
					
				case '2':
					showPrivate = true;
					break;
					
				case '3':
					showTraffic = true;
					break;
			}
			
			break;
	}
	
	var questions = $$("div.question");
	
	[7,8,9,10,11,12,13,14,15,16,17].each(function(questionId) {
		if(showPrivate)
			$('question_' + questionId).show();
		else
			$('question_' + questionId).hide();

		skip_questions[questionId] = showPrivate ? false : true;
	});
	
	[18,19,20,21,22,23].each(function(questionId) {
		if(showTraffic)
			$('question_' + questionId).show();
		else
			$('question_' + questionId).hide();

		skip_questions[questionId] = showTraffic ? false : true;
	});
	
	if(showTraffic && showPrivate)
		currentType = 'private_traffic';
	else if(showTraffic)
		currentType = 'traffic';
	else
		currentType = 'private';
	
	$('type').value = currentType;
	
	toggleAdditions();
	
	var otherValue = $('question_2_10_extra').value.replace(/\s/g, '');
	if((questionOne == '1' || questionOne == '2' || questionOne == '3') && questionTwo == '10' && otherValue == '')
	{
		if(action == 'submit')
		{
			alert(locale_other);
		}
		
		return false;
	}
	
	return true;
}

function checkTermination(action)
{
	var questionOne = getAnswerByQuestion($('question_1'));
	var questionThree = getAnswerByQuestion($('question_3'));
	var questionFour = getAnswerByQuestion($('question_4'));
	
	if(questionOne == '1' && questionFour == '2')
	{
		if(action == 'submit') 
		{
			myLightWindow.activateWindow({
				href: '#popup-nichtgekundigt',
				title: locale_double_insurance,
				type: 'inline'
			});
		}

		return false;
	}
	else if((questionOne == '2' || questionOne == '3') && questionFour == '2')
	{
		if(action == 'submit') 
		{
			myLightWindow.activateWindow({
				href: '#popup-nichtgekundigt',
				title: locale_double_insurance,
				type: 'inline'
			});
		}
		
		return true;
	}
	else
	{
		return true;
	}
}

function checkFamily(action)
{
	var questionZeven= getAnswerByQuestion($('question_7'));
	var questionEight = getAnswerByQuestion($('question_8'));
	var questionNine = getAnswerByQuestion($('question_9'));
	var questionNineteen = getAnswerByQuestion($('question_19'));
	
	if(questionZeven == '1' || questionEight == '1' || questionNine == '1' || questionNineteen == '1')
	{
		currentPrice = 2;
	}
	else
	{
		currentPrice = 1;
	}
	
	toggleAdditions();
	
	return true;
}

function checkTraffic(action)
{
	var questionOne = getAnswerByQuestion($('question_1'));	
	var questionTwo = getAnswerByQuestion($('question_2'));
	var questionFour = getAnswerByQuestion($('question_4'));
	var questionSix = getAnswerByQuestion($('question_6'));
	
	var showPrivate = false;
	var showTraffic = false;

	switch(questionOne)
	{
		case '1': // Ja, Privat + Verkehr
		 	if(questionTwo == '1' || questionFour == '1') // DAS or Bereits geundigt
			{
				showPrivate = true;
				showTraffic = true;
			}
			break;
			
		case '2': // Ja, nur Privat
			showTraffic = true;
			if(questionTwo == '1' || questionFour == '1') // DAS
			{
				showPrivate = true;
			}
			break;
			
		case '3': // Ja, nur Verkehr
			showPrivate = true;
			if(questionTwo == '1' || questionFour == '1') // DAS
			{
				showTraffic = true;
			}
			break;
			
		case '4': // Nein
			switch(questionSix)
			{
				case '1':
					showPrivate = true;
					showTraffic = true;
					break;
					
				case '2':
					showPrivate = true;
					break;
					
				case '3':
					showTraffic = true;
					break;
			}
			
			break;
	}
	
	if(showTraffic && showPrivate)
		currentType = 'private_traffic';
	else if(showTraffic)
		currentType = 'traffic';
	else
		currentType = 'private';
		
	var questionEighteen = getAnswerByQuestion($('question_18'));
	var questionNineteen = getAnswerByQuestion($('question_19'));
	
	if(questionEighteen == '2' && questionNineteen == '2')
	{		
		[20, 21, 22, 23].each(function(questionId) {
			$('question_' + questionId).hide();
			skip_questions[questionId] = true;
		});
		
		switch(currentType)
		{
			case 'traffic':
				myLightWindow.activateWindow({
					href: '#popup-traffic-not-necessary',
					title: locale_warning,
					type: 'inline'
				});
				return false;
				break;
			
			case 'private_traffic':
				currentType = 'private';

				toggleAdditions();
				break;
		}
	}
	else
	{
		if(questionEighteen == '2' && questionNineteen == '1' && action == 'submit')
		{
			myLightWindow.activateWindow({
				href: '#popup-traffic-not-self',
				title: locale_warning,
				type: 'inline'
			});
		}
		
		[20, 21, 22, 23].each(function(questionId) {
			$('question_' + questionId).show();
			skip_questions[questionId] = false;
		});
	}
	
	$('type').value = currentType;
	
	return true;
}

function checkObjectValue(action)
{
	var questionTwelve = getAnswerByQuestion($('question_12'));
	
	$('objectvalue').value = questionTwelve;
	
	return setObjectValue($('objectvalue'));
}

function clearObjectValue(action)
{
	$('objectvalue').value = '';
	$('objectvalue_line').hide();
	
	$('zusatz_1_pricespan').hide();
	objectValueMap = 0;
	
	calculateCosts(false);
	
	return true;
}

/* DAS MODULE */

function submitForm(self)
{
	var form = self.parentNode.parentNode.parentNode;
	var popup = form.parentNode;

	new Ajax.Updater(
		popup, 
		form.action,
		{ parameters: form.serialize(true) }
	);
}

function calculateDiscount(value)
{
	return (value - (value * phpDiscounts[currentDiscount]));
}

function initQuestionButtons ()
{
	var questions = $$("div.question");
		
	for (var i = 0; i < questions.length; i++)
	{
		questions[i].select("input.weiter-button")[0].observe('click', weiterButtonMouseClick);
		questions[i].select("input[type=radio]").each(function (element) { 
			element.observe('click', makeClickableNext); 
		});
		questions[i].select("input[type=text]").each(function (element) { 
			element.observe('keydown', function (e) {
				if(e.keyCode == Event.KEY_RETURN)
					weiterButtonMouseClick(e);
			}); 
		});
		
		if(hasValues(questions[i])) 
		{
			makeInActive(questions[i]);
		}
		
		questionIds[questions[i].id] = i;
		questionIdsIndexes[i] = questions[i].id;

		questionId = getRealId(questions[i].id);
		if(phpQuestions[questionId]['hidden'])
		{
			questions[i].hide();
			
			skip_questions[questionId] = true;
		}
		
		if (i == 0) activeQuestionObject = questions[i];
	}

	$('mod-das-footer').select("input.zusatz_checkbox[type=checkbox]").each(function (element) { 
		element.observe('click', changeAddition); 
	});
	
	$('type').observe('change', changeType); 
	$('price').observe('change', changePrice); 
	$('group').observe('change', changeGroup); 
	$('student').observe('click', changeDiscount); 
	
	$('mod-das-submit').enable();
	
	toggleAdditions();
	
	var today = new Date();
	var year = today.getFullYear();
	
	$j.datepicker.setDefaults({ dateFormat: 'dd.mm.yy', changeMonth: true, changeYear: true, maxDate: '+3y', minDate: '-1y', yearRange: '' + (year - 1) + ':' + (year + 5) + '' });

	$j('input.datepicker').datepicker();
}

function deactivateAddition(addition)
{
	additions.removeByValue(addition);
	$('zusatz_' + addition).checked = false;
}

function activateAddition(addition)
{
	additions.removeByValue(addition);
	additions[additions.length] = addition;
	$('zusatz_' + addition).checked = true;
}


function toggleAdditions()
{
	switch(currentType)
	{
		case 'private':
			[2,3,4].each(function(addition) {
				deactivateAddition(addition);
				$('zusatz_line_' + addition).hide();
			});
			
			[1,6].each(function(addition) {
				$('zusatz_line_' + addition).show();
			});
			
			if(currentPrice == 2) $('zusatz_line_5').show();
			else $('zusatz_line_5').hide();
			break;
			
		case 'traffic':
			[2,3,4].each(function(addition) {
				$('zusatz_line_' + addition).show();
			});
			
			[1,5,6].each(function(addition) {
				deactivateAddition(addition);
				$('zusatz_line_' + addition).hide();
			});
			break;
			
		case 'private_traffic':
			[1,2,3,4,6].each(function(addition) {
				$('zusatz_line_' + addition).show();
			});
			
			if(currentPrice == 2) $('zusatz_line_5').show();
			else $('zusatz_line_5').hide();			
			break;
	}
}

function changeAddition(event)
{
	var previous_costs = 0;
	additions.each(function(addition) {
		previous_costs += calculateDiscount(parseFloat(phpAdditions[addition]['price'][currentGroup]));
	});
	
	var addition = getRealId(Event.element(event).id);
	
	additions.removeByValue(addition);
	if(Event.element(event).checked)
	{
		additions[additions.length] = addition;
		
		if(addition == 1) $('objectvalue_line').show();
	}
	else
	{	
		if(addition == 1)
		{
			$('objectvalue').value = '';
			$('objectvalue_line').hide();
			
			$('zusatz_1_pricespan').hide();
			objectValueMap = 0;
		}
	}
	
	calculateCosts(false);
	
	var new_costs = 0;
	additions.each(function(addition) {
		new_costs += calculateDiscount(parseFloat(phpAdditions[addition]['price'][currentGroup]));
	});
	if (!popup_lower_costs && new_costs < previous_costs)
	{
		//popup_lower_costs = true;
		myLightWindow.activateWindow({
			href: '#popup-auswahl-angepasst',
			title: locale_warning,
			type: 'inline'
		});
	}
}

function changeType(event)
{
	var previous_costs = calculateDiscount(parseFloat(phpPrices[currentType][currentPrice]['price'][currentGroup]));
	
	currentType = Event.element(event).value;
	calculateCosts();
	
	toggleAdditions();
	
/*	var new_costs = calculateDiscount(parseFloat(phpPrices[currentType][currentPrice]['price'][currentGroup]));
	if (!popup_lower_costs && new_costs < previous_costs)
	{
		//popup_lower_costs = true;
		myLightWindow.activateWindow({
			href: '#popup-auswahl-angepasst',
			title: locale_warning,
			type: 'inline'
		});
	}*/
}

function changePrice(event)
{
	var previous_costs = calculateDiscount(parseFloat(phpPrices[currentType][currentPrice]['price'][currentGroup]));
	
	currentPrice = Event.element(event).value;
	calculateCosts();
	
	toggleAdditions();
	
/*	var new_costs = calculateDiscount(parseFloat(phpPrices[currentType][currentPrice]['price'][currentGroup]));
	if (!popup_lower_costs && new_costs < previous_costs)
	{
		//popup_lower_costs = true;
		myLightWindow.activateWindow({
			href: '#popup-auswahl-angepasst',
			title: locale_warning,
			type: 'inline'
		});
	}*/
}

function changeGroup(event)
{
	var previous_costs = calculateDiscount(parseFloat(phpPrices[currentType][currentPrice]['price'][currentGroup]));
	
	currentGroup = Event.element(event).value;
	calculateCosts();
	
	toggleAdditions();
	
	if(currentGroup == 1) $('basic-warning').show();
	else $('basic-warning').hide();
	
/*	var new_costs = calculateDiscount(parseFloat(phpPrices[currentType][currentPrice]['price'][currentGroup]));
	if (!popup_lower_costs && new_costs < previous_costs)
	{
		//popup_lower_costs = true;
		myLightWindow.activateWindow({
			href: '#popup-auswahl-angepasst',
			title: locale_warning,
			type: 'inline'
		});
	}*/
}

function changeDiscount(event)
{
	currentDiscount = (Event.element(event).checked ? 'student' : 'default');
		
	if(currentDiscount == 'student')
	{
		$('discount-student').show();
		$('discount-student-disabled').hide();
	}
	else 
	{
		$('discount-student').hide();
		$('discount-student-disabled').show();
	}
	
	//$('discount').update(phpDiscounts[currentDiscount] * 100);
	
	calculateCosts();
}

function getAnswerByQuestion(questionObj)
{
	inputObjs = questionObj.select("input[type=radio]");
	for (var i = 0; i < inputObjs.length; i++)
	{
		if (inputObjs[i].checked)
		{
			return inputObjs[i].value;
		}
	}
	
	inputObjs = questionObj.select("input[type=text]");
	for (var i = 0; i < inputObjs.length; i++)
	{
		return inputObjs[i].value;
	}
}

function parseCustomJavascriptForQuestion(questionObj, action)
{
	var id = getRealId(questionObj.id);
	var answer;
	
	var result = true;
	
	if(phpQuestions[id]['javascript'])
	{
		eval('result = ' + phpQuestions[id]['javascript'] + '("' + action + '");');
	}
	
	var answer = getAnswerByQuestion(questionObj);
	if (answer > 0 && phpQuestions[id]['answers'][answer])
	{
		if(phpQuestions[id]['answers'][answer]['javascript'])
		{
			if(phpQuestions[id]['answers'][answer]['javascript'] instanceof Array)
				phpQuestions[id]['answers'][answer]['javascript'].each(function(javascript) {
					eval('result = ' + javascript + '("' + action + '");');
				});
			else
				eval('result = ' + phpQuestions[id]['answers'][answer]['javascript'] + '("' + action + '");');
		}
		
		if(action == "submit" && phpQuestions[id]['answers'][answer]['popup'])
		{
			myLightWindow.activateWindow({
				href: phpQuestions[id]['answers'][answer]['popup']['id'],
				title: phpQuestions[id]['answers'][answer]['popup']['title'],
				type: phpQuestions[id]['answers'][answer]['popup']['type'],
				customDeactivateFunction: phpQuestions[id]['answers'][answer]['popup']['deactivate']
			});
		}
				
		if(phpQuestions[id]['answers'][answer]['stop'] == 1) result = false;
		
		if(phpQuestions[id]['answers'][answer]['show'])
		{
			phpQuestions[id]['answers'][answer]['show'].each(function(questionId) {
				skip_questions[questionId] = false;
				
				$('question_' + questionId).show();
			});
		}
		
		if(phpQuestions[id]['answers'][answer]['hide'])
		{
			phpQuestions[id]['answers'][answer]['hide'].each(function(questionId) {
				skip_questions[questionId] = true;
				
				$('question_' + questionId).hide();
			});
		}
	}
	
	return result;
}

function formatAnswer(questionObj, id, answer)
{
	var value = '';

	switch(id)
	{
		case '2':
			if(answer == 10) 
			{
				inputObjs = questionObj.select("input[type=text]");
				for (var i = 0; i < inputObjs.length; i++)
				{
					value = inputObjs[i].value;
				}
			}
			else value = phpQuestions[id]['answers'][answer]['title']
			break;
		
		case '3':
			value = answer;
			break;
			
		case '12':
			value = 'CHF ' + answer;
			break;
			
		default:
			if(phpQuestions[id]['answers'][answer])
				value = phpQuestions[id]['answers'][answer]['title'];
			
			break;
	}
	
	return value;
}


function parseQuestion(question)
{
	var id = getRealId(question);
	var obj = $(question);
	
	var result = true;
	
	var answer = getAnswerByQuestion(obj);
	if (answer > 0)
	{
		groups[id] = null;
		
		if(phpQuestions[id]['answers'][answer] && phpQuestions[id]['answers'][answer]['redirect'])
		{
			document.location.href = document.getElementsByTagName('base')[0].href + phpQuestions[id]['answers'][answer]['redirect'];
		}
		
		if(id == 12)
		{
			$('objectvalue').value = answer;
		}
		
		if(phpQuestions[id]['answers'][answer] && phpQuestions[id]['answers'][answer]['discount'])
		{
			currentDiscount = phpQuestions[id]['answers'][answer]['discount'];
			
			if(currentDiscount == 'student')
			{
				$('discount-student').show();
				$('discount-student-disabled').hide();
				$('student').checked = true;
			}
			else 
			{
				$('discount-student').hide();
				$('discount-student-disabled').show();
				$('student').checked = false;
			}
			
			//$('discount').update(phpDiscounts[currentDiscount] * 100);
		}
		
		activeQuestionObject.select("span.question-answer")[0].update(formatAnswer(obj, id, answer));
		
		//additions.removeByValue(phpQuestions[id]['answers'][answer]['addition']);
		if(phpQuestions[id]['answers'][answer] && phpQuestions[id]['answers'][answer]['addition'] > 0)
		{
			if(phpQuestions[id]['answers'][answer]['addition'] == 1)
				$('objectvalue_line').show();

			additions[additions.length] = phpQuestions[id]['answers'][answer]['addition'];
		}
		else if(phpQuestions[id]['answers'][answer] && phpQuestions[id]['answers'][answer]['remove_addition'] > 0)
		{
			if(phpQuestions[id]['answers'][answer]['addition'] == 1)
			{
				$('objectvalue').value = '';
				$('objectvalue_line').hide();
			}
			
			additions.removeByValue(phpQuestions[id]['answers'][answer]['remove_addition']);
			$('zusatz_' + phpQuestions[id]['answers'][answer]['remove_addition']).checked = false;
		}
		
		if(phpQuestions[id]['answers'][answer] && phpQuestions[id]['answers'][answer]['price'] > 0)
		{
			currentPrice = phpQuestions[id]['answers'][answer]['price'];
		}
		
		if(phpQuestions[id]['answers'][answer] && phpQuestions[id]['answers'][answer]['group'] > 0)
		{		
			groups[id] = phpQuestions[id]['answers'][answer]['group'];
		}
			
		first = true;
		for (i in groups) 
		{ 
			if (groups[i] > 0) 
			{
				if(first) currentGroup = groups[i];
				else
				{
					if(groups[i] > currentGroup) currentGroup = groups[i];
				}
				first = false;
			}
		}
		
		if(currentGroup == 1) $('basic-warning').show();
		else $('basic-warning').hide();
		
		if(phpQuestions[id]['answers'][answer] && phpQuestions[id]['answers'][answer]['popup'])
		{
			myLightWindow.activateWindow({
				href: phpQuestions[id]['answers'][answer]['popup']['id'],
				title: phpQuestions[id]['answers'][answer]['popup']['title'],
				type: phpQuestions[id]['answers'][answer]['popup']['type'],
				customDeactivateFunction: phpQuestions[id]['answers'][answer]['popup']['deactivate']
			});
		}
		
		if(phpQuestions[id]['answers'][answer] && phpQuestions[id]['answers'][answer]['stop'] == 1) result = false;
			
		if(phpQuestions[id]['answers'][answer] && phpQuestions[id]['answers'][answer]['skip'])
		{
			skip_questions[phpQuestions[id]['answers'][answer]['skip']['question']] = true;
			
			$('question_' + phpQuestions[id]['answers'][answer]['skip']['question']).addClassName ("question-done");
			$('question_' + phpQuestions[id]['answers'][answer]['skip']['question']).select("h2")[0].observe('mouseover', questionMouseOver);
			$('question_' + phpQuestions[id]['answers'][answer]['skip']['question']).select("h2")[0].observe('mouseout', questionMouseOut);
			$('question_' + phpQuestions[id]['answers'][answer]['skip']['question']).select("h2")[0].observe('click', questionMouseClick);
			$('question_' + phpQuestions[id]['answers'][answer]['skip']['question']).select("span.question-answer")[0].update(
				phpQuestions[phpQuestions[id]['answers'][answer]['skip']['question']]['answers'][phpQuestions[id]['answers'][answer]['skip']['answer']]['title']
			);
			$('question_' + phpQuestions[id]['answers'][answer]['skip']['question'] + '_' + phpQuestions[id]['answers'][answer]['skip']['answer']).checked = true;
		}
		
		if(phpQuestions[id]['answers'][answer] && phpQuestions[id]['answers'][answer]['show'])
		{
			phpQuestions[id]['answers'][answer]['show'].each(function(questionId) {
				skip_questions[questionId] = false;
				
				$('question_' + questionId).show();
			});
		}
		
		if(phpQuestions[id]['answers'][answer] && phpQuestions[id]['answers'][answer]['hide'])
		{
			phpQuestions[id]['answers'][answer]['hide'].each(function(questionId) {
				skip_questions[questionId] = true;
				
				$('question_' + questionId).hide();
			});
		}
		
		calculateCosts();
	}
	
	return result;
}

function setObjectValue(element)
{
	var value = element.value.replace(/[^0-9]/g, '');
	objectValueMap = 0;
	
	if(value > 0)
	{
		if(value <= 100000)
			objectValueMap = 1;
		else if(value > 100000 && value <= 800000)
			objectValueMap = 2;
		else if(value > 800000 && value <= 1500000)
			objectValueMap = 3;
		else if(value > 1500000 && value <= 2500000)
			objectValueMap = 4;
		else if(value > 2500000)
		{
			objectValueMap = 0;
			
			myLightWindow.activateWindow({
				href: '#popup-nichtmoeglich-versicherungswert',
				title: locale_warning,
				type: 'inline'
			});
		}
	}
	
	calculateCosts(false);
	
	return objectValueMap != 0;
}

function calculateCosts(allow_addition_check)
{
	var costs = 0;
	var costs_original = 0;
	
	if(allow_addition_check == null) allow_addition_check = true;
		
	$('price').value = currentPrice;
	
	$('group').descendants().each(function (item) {
		item.remove();
	});

	// CALCULATE BASE
	costs = calculateDiscount(parseFloat(phpPrices[currentType][currentPrice]['price'][currentGroup]));
	costs_original = parseFloat(phpPrices[currentType][currentPrice]['price'][currentGroup]);

	$('group').insert('<option value="1">' + phpGroups[1]['title'] + ' (CHF ' + commaFormatted(currencyFormatted(calculateDiscount(parseFloat(phpPrices[currentType][currentPrice]['price'][1])))) + ' ' + locale_yearly + ')</option>');
	$('group').insert('<option value="2">' + phpGroups[2]['title'] + ' (CHF ' + commaFormatted(currencyFormatted(calculateDiscount(parseFloat(phpPrices[currentType][currentPrice]['price'][2])))) + ' ' + locale_yearly + ')</option>');
	$('group').insert('<option value="3">' + phpGroups[3]['title'] + ' (CHF ' + commaFormatted(currencyFormatted(calculateDiscount(parseFloat(phpPrices[currentType][currentPrice]['price'][3])))) + ' ' + locale_yearly + ')</option>');
	$('group').value = currentGroup;
		
	$$('input.zusatz_checkbox').each(function (item) {
		if(allow_addition_check) item.checked = false;
		
		var addition = getRealId(item.id);
		if(phpAdditions[addition]['price'] && $(item.id + '_price'))
			$(item.id + '_price').update(commaFormatted(currencyFormatted(calculateDiscount(parseFloat(phpAdditions[addition]['price'][currentGroup])))));
	});

	// CALCULATE ADDITIONS
	additions.each(function(addition) {
		if(addition == 1)
		{
			if(objectValueMap > 0)
			{
				costs += additionCosts = calculateDiscount(parseFloat(phpAdditions[addition]['mapper'][objectValueMap][currentGroup]));
				costs_original += parseFloat(phpAdditions[addition]['mapper'][objectValueMap][currentGroup]);
				
				$('zusatz_1_price').innerHTML = commaFormatted(currencyFormatted(additionCosts));
				$('zusatz_1_pricespan').show();
			}
		}
		else
		{
			costs += calculateDiscount(parseFloat(phpAdditions[addition]['price'][currentGroup]));
			costs_original += parseFloat(phpAdditions[addition]['price'][currentGroup]);
		}
		
		if(allow_addition_check) $('zusatz_' + addition).checked = true;
	});

	$('mod-das-group').update(phpGroups[currentGroup]['title']);
	$('mod-das-costs').update(commaFormatted(currencyFormatted(costs)));
	$('mod-das-costs-original').update(commaFormatted(currencyFormatted(costs_original)));
	
	if(previous_costs != costs) 
	{
		new Effect.Highlight('mod-das-footer-right');	
	}
	
	previous_costs = costs;
}

function weiterButtonMouseClick (e)
{
	if (hasValues(activeQuestionObject) && parseCustomJavascriptForQuestion(activeQuestionObject, 'submit')) 
		makeActiveNext();
}

function questionMouseOver (e)
{
	var element = e.element();
	element.addClassName("mouseover");
}

function questionMouseOut (e)
{
	var element = e.element();
	element.removeClassName("mouseover");
}

function questionMouseClick (e)
{
	var element = e.element();		
	
	if(element.hasClassName('question-answer')) element = element.parentNode;
	
	if(element.parentNode.id) 
	{
		if(hasValues(element.parentNode))
		{
			if(!element.parentNode.hasClassName('question-active'))
			{
				parseQuestion(activeQuestionObject.id);
				makeActive (element.parentNode, true);
			}
		}
		else 
		{
			parseQuestion(activeQuestionObject.id);
			makeActive (element.parentNode);
		}
	}
}

function makeActive (obj, force)
{
	if (force || obj != activeQuestionObject)
	{
		if (activeQuestionObject && obj != activeQuestionObject)
		{
			makeInActive (activeQuestionObject);
			
			// track event in analytics
			if(pageTracker) pageTracker._trackPageview("/tool/answer/" + activeQuestionObject.select("div.question-text")[0].id);
		}
	
		if(obj)
		{
			activeQuestionObject = obj;	
	
			obj.addClassName("question-active");

			if(obj.select("div.question-text")[0].id) Effect.SlideDown(obj.select("div.question-text")[0].id, { duration: 0.5 });
		}	
	}
}

function makeClickableNext ()
{
	var active_id = activeQuestionObject.id;
	var active_id_index = questionIds[active_id];

	if (hasValues(activeQuestionObject) && parseCustomJavascriptForQuestion(activeQuestionObject, 'change'))
	{	
		active_id_index = findNext(active_id_index);
		
		if(active_id_index != false)
		{
			if(active_id_index < questionIdsIndexes.length)
			{		
				$(questionIdsIndexes[active_id_index]).select("h2")[0].observe('click', questionMouseClick);
				$(questionIdsIndexes[active_id_index]).select("h2")[0].observe('mouseover', questionMouseOver);
				$(questionIdsIndexes[active_id_index]).select("h2")[0].observe('mouseout', questionMouseOut);
			}
		}
	}
}

function makeClickableOrder()
{
	//$("mod-das-submit").enable();
}

function makeActiveNext (force)
{
	var active_id = activeQuestionObject.id;
	var active_id_index = questionIds[active_id];
	
	active_id_index = findNext(active_id_index);
	
	if(force || parseQuestion(active_id))
	{	
		if(force) calculateCosts();

		if (active_id_index >= questionIdsIndexes.length)
		{
			makeInActive (activeQuestionObject);
			makeClickableOrder();
			
			activeQuestionObject = null;
		}
		else
		{
			makeActive ($(questionIdsIndexes[active_id_index]));
		}
	}
}

function findNext(active_id_index)
{
	active_id_index++;

	if(questionIdsIndexes[active_id_index] == undefined)
		return false;

	var question_id = getRealId(questionIdsIndexes[active_id_index]);

	if(skip_questions[question_id]) 
		return findNext(active_id_index);
	else
		return active_id_index;
}

function getRealId (htmlId)
{
	htmlId = htmlId.split("_");
	if (htmlId[1]) return htmlId[1];
	else return false;
}

function hasValues (obj)
{
	inputObjs = obj.select("input");
	var hasValues = false;

	for (var i = 0; i < inputObjs.length; i++)
	{
		if (inputObjs[i].type == "radio")
		{
			if (inputObjs[i].checked)
			{
				hasValues = true;
			}
		}
		else if (inputObjs[i].type == "text")
		{
			if (inputObjs[i].value != '')
			{
				hasValues = true;
			}
		}
	}
	
	selectObjs = obj.select("select");
	for (var i = 0; i < selectObjs.length; i++)
	{
		if (selectObjs[i].value != '')
		{
			hasValues = true;
		}
	}
	
	return hasValues;
}

function makeInActive (obj)
{
	if (hasValues(obj)) 
	{
		obj.addClassName ("question-done");
		obj.select("h2")[0].observe('mouseover', questionMouseOver);
		obj.select("h2")[0].observe('mouseout', questionMouseOut);
		obj.select("h2")[0].observe('click', questionMouseClick);
		
		var id = getRealId(obj.id);
		var answer = getAnswerByQuestion(obj);
		
		obj.select("span.question-answer")[0].update(formatAnswer(obj, id, answer));
	}
	
	obj.removeClassName("question-active");
	if(obj.select("div.question-text")[0].id && obj.select("div.question-text")[0].visible()) Effect.SlideUp(obj.select("div.question-text")[0].id, { duration: 0.2 });
}

function checkAllQuestions ()
{
	var completed = true;
	
	$$("div.question").each(function(element) {
		if(!hasValues(element)) completed = false;
	});

	return completed;
}

function currencyFormatted (amount)	
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function commaFormatted (amount) 
{
	var delimiter = "."; // replace comma if desired
	var a = amount.split('.',2);
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	//else if (d == '00') { amount = n + '.-'; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

function validateEmail(obj)
{
	var email = obj.value;
	
	if(email.length > 0)
	{
		var x=email;
		var atpos=x.indexOf("@");
		var dotpos=x.lastIndexOf(".");
		if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
		{
			$(obj).addClassName('inputFieldError');
			
			return false;
		}
		else
		{
			$(obj).removeClassName('inputFieldError');
			
			return true;
		}
	}
	else
	{
		$(obj).removeClassName('inputFieldError');
		
		return true;
	}
}

function number_format (number, decimals, dec_point, thousands_sep) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival
    // +      input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +   improved by: davook
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Jay Klehr
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Amir Habibi (http://www.residence-mixte.com/)
    // +     bugfix by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Theriault
    // +      input by: Amirouche
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'
    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'
    // *     example 7: number_format(1000.55, 1);
    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');
    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);
    // *     returns 9: '1'
    // *    example 10: number_format('1.20', 2);
    // *    returns 10: '1.20'
    // *    example 11: number_format('1.20', 4);
    // *    returns 11: '1.2000'
    // *    example 12: number_format('1.2000', 3);
    // *    returns 12: '1.200'
    // *    example 13: number_format('1 000,50', 2, '.', ' ');
    // *    returns 13: '100 050.00'
    // Strip all characters but numerical ones.
    number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}
