$(document).ready(function() {
	$('#select_country').live('click', function() {
		$('#birthday').css('visibility', 'visible');
		var country_id = $(this).attr('name');
		$('#prehomeHtml5Form_country_id').attr('value', country_id);
		
		$('.formtable ul a').removeClass('selected');
		$(this).addClass('selected');
		var country_id = $('#select_other').val('0');
	});

	$('#select_other').live('change', function() {
		$('#birthday').css('visibility', 'visible');
	});
	
	var clearValue = function() {
		this.value = "";
	};
	$('.birthday input').bind('click', clearValue);
	$('.birthday input').bind('focus', clearValue);
	
	var keypress = function(min, max, jumpLength) {
		return function(evt) {
			if(evt.which == 8) {
				return true;
			}
			if(evt.which < 48 || evt.which > 57) {
				return false;
			}
			keychar = String.fromCharCode(evt.which);
			var newValue = this.value + keychar;
			if(newValue == 0 && newValue.length > 1) {
				return false;
			}
			if(newValue < min || newValue > max) {
				return false;
			}
			if(jumpLength && newValue.length >= jumpLength) {
				// Merci IE
				var self = this;
				setTimeout(function() {
					$(self).next().focus();
				}, 10);
			}
			return true;
		};
	};
	
	$('#prehomeHtml5Form_day').bind('keypress', keypress(0,31,2));
	$('#prehomeHtml5Form_month').bind('keypress', keypress(0,12,2));
	$('#prehomeHtml5Form_year').bind('keypress', keypress(0,2100,4));
	
	var windowHeight = window.innerHeight;
	if(! windowHeight) {
		windowHeight = document.documentElement.clientHeight;
	}
	
	var margin = (windowHeight - $('#form-wrap').height()) / 2;
	$('#form-wrap').css('margin-top', margin + "px");
	
	
	$('#legalscrollbar').tinyscrollbar();
	$('#legals').hide();
	
	$('#legals .close-button').click(function() {
		$('#legals').fadeOut('fast');
	});
	
	$('#legal-terms').click(function() {
		var windowWidth = window.innerWidth;
		var windowHeight = window.innerHeight;
		if(! windowWidth) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}
		var left = (windowWidth - $('#legals').width()) / 2;
		var top = (windowHeight - $('#legals').height()) / 2;
		
		$('#legals').css('left', left + 'px').css('top', top + 'px').fadeIn('fast');
	});

  
  
});

function selectcountry() {
	var country_id = $('#select_other').val();
	$('#prehomeHtml5Form_country_id').attr('value', country_id);
	$('.formtable ul a').removeClass('selected');
}
