var success = true;

$(document).ready(function () 
{
	$('input.error').keyup(hide_msg).change(hide_msg);
});

function hide_msg()
{
	$(this).removeClass('error').unbind('keyup');
	$(this).parent().parent().find('.msg').eq(0).html('');
}

function display_msg(input,text)
{
	if (typeof input == 'object' && input.length>1)
	{
		input[0].parent().parent().find('.msg').html(text);
		for (var i=0; i < input.length; i++)
		{
			if (input[i].attr('type') == 'text' || input[i].attr('type') == 'password' || input[i].is('textarea')) input[i].addClass('error');
			input[i].keyup(hide_msg).change(hide_msg);
		}
    }
	else
	{
		if (input.attr('type') == 'text' || input.attr('type') == 'password' || input.is('textarea')) input.addClass('error');
		input.parent().parent().find('.msg').html(text);
		input.keyup(hide_msg).change(hide_msg);
	}
	
	$('#children').change(function () 
	{
		$(this).removeClass('error').unbind('keyup');
		$('#household_size').parent().parent().find('.msg').eq(0).html('');
	});
}

function check_shared()
{
	if ($('#clicked_on_share').val() == 'false')
	{
		$('#share_msg').html('Vi håber at du har lyst til at fortælle dine venner om opeepl. Læs ovenfor hvordan du kan bruge Facebook og Twitter. Ellers kan du klikke <a href="' + global_url + 'completed" >her</a> for at afslutte din registrering med det samme.')
		$('#share_msg').show();
		$('#clicked_on_share').val('true');
		return false;
	}
	return true;
}

function click_share()
{
	$('#clicked_on_share').val('true');
}

function check_recommend ()
{
	if (!$('#name')[0].value)
	{
		alert('Du skal indtaste dit navn');
		return false;
	}
	return true;
}

function check_respondent_birth()
{
	success = true;
	
	if (!/^[0-9]{4}$/.test($('#yearofbirth')[0].value) || !$('#monthofbirth')[0].value || !$('#dayofbirth')[0].value)
	{
		display_msg([$('#yearofbirth'),$('#monthofbirth'),$('#dayofbirth')],'Du skal vælge din fødselsdato');
		success = false;
	}
	
	if (success==true) return true;
	else return false;	
}

function submit_check_shared()
{
	if (check_shared()) {window.location = global_url + 'completed';}
}
function submit_check_respondent_birth()
{
	if (check_respondent_birth()) {$('#birth_form').submit();}
}

