function $(id){
	return document.getElementById(id);
}


function getIndex(needle, haystack){
	for(i=0; i<haystack.length; i++){
		if(needle == haystack[i]){
			return i;
		}
	}
	return false;
}
var questions = ['q1','q2','q3','q4','q5','q6','q7','q8','q9','q10','q11','q12','q13'];


var currentQuestion = questions[0];
var nextQuestion;
var d = 400;

function qeval(){
	var score = 0;
	var inputs = $('questions').getElementsByTagName('input');
	for(var i=0; i<inputs.length; i++){
		if(inputs[i].checked){
			score = score + Number(inputs[i].value);
		}
	}
	//return score;
	var type = 0;
	var title = '';
	if(score > 215){
		type = 't4';
		title = 'You are the urbane type.';
	}else if(score > 155){
		type = 't3';
		title = 'You are the sporting type.';
	}else if(score > 85){
		type = 't2';
		title = 'You are the adventurer.';
	}else{
		type = 't1';
		title = 'You are the bon vivant.';
	}
	$('image_head').src = 'http://data.kulmhotel-stmoritz.ch/image/head/wyl_auswertung.jpg';
	$('image_title').src ='http://data.kulmhotel-stmoritz.ch/image/title/title_wyl_auswertung_en.gif';
	$('result_content').innerHTML = $(type+'_content').innerHTML;
	$(type).style.display='none';
	
	$('result_title').innerHTML = title;
	$('result').style.display='block';
	$('wunschtypen').style.display='block';
	
}

function resetTest(){
	var inputs = $('questions').getElementsByTagName('input');
	for(var i=0; i<inputs.length; i++){
		inputs[i].checked = false;
	}
}
function validateQuestion(qId){
	var inputs = $(qId).getElementsByTagName('input');
	for(var i=0; i<inputs.length; i++){
		if(inputs[i].checked){
			return true;
		}
	}
	return false;
}
function showQuestion(q){
	// Validate 
	$('msgdiv').innerHTML = '';
	/*
	if(!validateQuestion(currentQuestion)){
		$('msgdiv').innerHTML = 'Bitte wählen sie eine Antwort aus.';
		return false;
	}
	*/
	nextQuestion = questions[getIndex(currentQuestion, questions)+q];
	//alert(nextQuestion);
	/*
	var hideQ = new Spry.Effect.Fade(currentQuestion, {	from: "100%", 
														to: "0%",
														duration: d,
														toggle: true,
														finish: function() {
																	var showQ = new Spry.Effect.Fade(nextQuestion, {from: "0%", to: "100%", duration: d, toggle: true}); 
																	showQ.start() 
																}  
														
														});
														
	*/
	var hideQ = new Spry.Effect.Fade(currentQuestion, {	from:100, 
														to: 0,
														duration: d,toggle:true,finish:function(element,effect){element.style.visibility='hidden'; } });
	
										
										
	hideQ.start();
	if(currentQuestion == 'q13'){
		// finish 
		qeval();
	}else{	
		var showQ = new Spry.Effect.Fade(nextQuestion, {from: 0, to:100, duration: d,toggle:true });	
		showQ.start();
		currentQuestion = nextQuestion;
	}
}
function highlightAndShowNext(el){
	var hl = new Spry.Effect.Highlight(el.parentNode, {duration:  400,from:'#ffffff',to:'#CAE6FF',finish:function(){showQuestion(1) }});
	hl.start();
	//showQuestion(1);
}

function showType(type){
	
	$(type+'_content').style.display='block';
}