$(document).ready(function() 
{
	$('.questionbox:first').show()
	//$('.questionbox').show()



	$('.toggle_terms').click(function()
	{
		var id = $(this).parent('div').attr('id').substring(1);
		changeBgImg($(this));
	
		//var id = $(this).
		
		//animateScroll($(this));
		$("#campaign_" + id).attr('value', $(this).attr('title'));
		show_more($("#campaign_" + id), $(this).hasClass('ja'));
	});
	
	$('.questionbox .val').click(function()
	{
		changeBgImg($(this)); 
	
		$(this).parent().children("input").attr({value:$(this).attr("title")});
		
		if ($(this).attr('id') == 'he_tm_stop')
			$(this).parent().next().next().next().slideDown("fast");
		else if ($(this).attr('id') == 'he_tm2_no' || $(this).attr('id') == 'canal_digital_more')
			$(this).parent().next().next().slideDown("fast");
		else if ($(this).hasClass('bt_ja'))
		{
			$('#campaign_157000').attr('value','ja');
			$(this).parent().next().slideDown("fast");
		}
		else if ($(this).hasClass('bt_nej'))
		{
			$('#campaign_157000').attr('value','nej');
			$(this).parent().next().next().slideDown("fast");
		}
		
		else 	
			$(this).parent().next().slideDown("fast");
	
		//var nextQuestion = $(this).parent().next();
	
		animateScroll($(this));
	
		//$.scrollTo(nextQuestion, {duration:3000});
	});
});

function changeBgImg(obj)
{
	obj.parent().children(".val").each(function()
	{
	$(this).css('background-image','url(img/answer.gif)'); 
	});
	obj.css('background-image','url(img/answer_c.gif)');
}


function animateScroll(obj)
{
	var window_bottom = $(window).scrollTop() + $(window).height();
	var form_bottom = $("#right_column").height() + $("#right_column").offset().top;
	if(window_bottom < (form_bottom + 150))
	$('html,body').animate({scrollTop: '+=' + (obj.parent().height()+20) + 'px'}, 500);
}

function timeToMove(obj)
{
	animateScroll($(obj).parent());
}


function show_more(obj, value)
{
	
	if(!value)
	{
		$(obj).parent('p').next(".hidden").slideUp();
		$(obj).parent("p").next(".hidden").find("p").children().each(function()
		{
			$(this).removeAttr('validate').removeAttr('errormsg');
		});
	}
	else if(value)
	{
		$(obj).parent('p').next(".hidden").slideDown();
		
		$(obj).parent("p").next(".hidden").find("p").children().each(function()
		{
			if(this.tagName != 'INPUT' && this.tagName != 'SELECT')
				return true;
				
			var name = this.name.match(/\[([^\]]+)\]$/);
			name = name[1];
			
			if(this.type == 'checkbox')
				$(this).attr('validate', 'checked').attr('errormsg', country + '.' + name);
			else if(this.type == 'select-one' || this.type == 'select')
				$(this).attr('validate', 'notindexzero').attr('errormsg', country + '.' + name);
			else if(name == 'pnr')
				$(this).attr('validate', 'pnr').attr('errormsg', country + '.birthdate');
			else if(this.type == 'text')
				$(this).attr('validate', 'notempty').attr('errormsg', country + '.' + name);
		});
	}
}


function movePNR()
{
	var tmp_value = $('#pnr_raw').attr('value');
	tmp_value = tmp_value.replace('-', '');
	if(tmp_value.length != 10)
		tmp_value = tmp_value.substring(2);
		
	$('#pnr_formatted').val(tmp_value);
	tmp_value = tmp_value.substring(0,6);
	$('.prn_short').val(tmp_value);
}
