$(function()
{
	// resize
	// window.resizeTo(600, 800);
	
	// remember css attirbutes (to restore)
	var input_border_color = $('input').css('border-color');
	
	if( $.cookie('blocked_emails') == null )
		$.cookie('blocked_emails', '');
	
	// show 'helper' text on <input>s
	$('.initial').each(function() 
	{
		var input = $(this);

		input.val( input.attr('alt') );

		input.bind('focus', function() 
		{	
			if( input.is('.initial') ) {

				// remove initial value
				input.val('');
				input.removeClass('initial');
			}
		});

		input.bind('blur', function() 
		{
			if( input.val() == '' ) {

				// show initial input if blank
				input.addClass('initial');
				input.val( input.attr('alt') );
			}
		});
	});
	
	// get rid of that ugly dotted line after selection
	$('select').change(function()
	{
		$(this).blur();
	})
	
	$('input').bind('keydown', function(key)
	{
		if( key.which == 13 )	// <return> || <enter>
			validateAndSubmit();
	});
	
	$('.submit_btn').bind('click', validateAndSubmit)
	
	function validateAndSubmit()
	{
		$('.initial').val('');
		$('input, select').css('border-color', input_border_color);
		
		if( !isValid() ) {
			
			// restore helper values
			$('.initial').each(function()
			{
				$(this).val($(this).attr('alt'));
			});
			
			//$(this).val('Try Again →');
			return false;
		}
		
		// change text
		$(this).attr('disabled', 'disabled');
		$(this).val('Saving...');
		
		$('#signup_form').submit();
		return false;
	}
	
	// validate form  
	function isValid(under13check)
	{
		var valid = true;
		var errs = [];
		var errnum = 1;
		
		/*
		// firstname
		if( $('.firstname').is('.initial') ) {
			
			valid = false;
			errs.push(errnum + '. Please enter your first name');
			$('.firstname').css('border-color', 'red');
			errnum++;
		}
		
		// lastname
		if( $('.lastname').is('.initial') ) {
			
			valid = false;
			errs.push(errnum + '. Please enter your last name');
			$('.lastname').css('border-color', 'red');
			errnum++;
		}
		*/
		
		// email
		if( !__isValidEmail($('.email').val()) ) {
			
			valid = false;
			errs.push(errnum + '. Please enter a valid email address');
			$('.email').css('border-color', 'red');
			errnum++;
		}
		else if(__isBlocked( $('.email').val() )) {
			
			// short circuit here
			$('input, select').css('border-color', input_border_color);
			$('.age').css('border-color', 'red');
			$('.email').css('border-color', 'red');
			alert('Sorry, we have restricted registration for this email address.\n\nYou must be over 13 in order to register.');
			return false;
		}
		
		/*
		// state
		if( $('.state').val() == 'null' ) {
			
			valid = false;
			errs.push(errnum + '. Please select your state');
			$('.state').css('border-color', 'red');
			errnum++;
		}
		*/
		
		// zip
		if( $('.zip').is('.initial') ) {
			
			/* Zip is optional
			valid = false;
			errs.push(errnum + '. Please enter a valid zip code');
			$('.zip').css('border-color', 'red');
			errnum++;
			*/
		}
		else {
		
			// zip length
			if( !__isValidZipLength( $('.zip').val() ) )
			{
				valid = false;
				$('.zip').css('border-color', 'red');
				errs.push(errnum + '. Please enter a valid zip code (5 or 9 digits)');
				errnum++;
			}
		
			// zip (only numbers)
			if( !__isValidZip( $('.zip').val() ) )
			{
				valid = false;
				$('.zip').css('border-color', 'red');
				errs.push(errnum + '. Please make sure there are no letters in your zip');
				errnum++;
			}
		}
		
		/*
		// phone
		if( $('.phone').is('.initial') ) {
			// no phone number provided
		}
		else {
		
			// phone length
			if( !__isValidPhoneLength( $('.phone').val() ) )
			{
				valid = false;
				$('.phone').css('border-color', 'red');
				errs.push(errnum + '. Please enter a valid mobile #  ( ie. 555-555-5555)');
				errnum++;
			}
		
			// phone (only numbers)
			if( !__isValidPhone( $('.phone').val() ) )
			{
				valid = false;
				$('.phone').css('border-color', 'red');
				errs.push(errnum + '. Please make sure there are no letters in your mobile #');
				errnum++;
			}
		}
		*/
		
		// birthday -> age conversion
		var valid_bd = true;
		
		if( !__isValidBDMonth() ) {
			
			valid = false;
			errs.push(errnum + '. Please enter a valid Birthdate (MM)');
			$('.birthday_month').css('border-color', 'red');
			errnum++;	
		}
		if( !__isValidBDDay() ) {
			
			valid = false;
			errs.push(errnum + '. Please enter a valid Birthdate (DD)');
			$('.birthday_day').css('border-color', 'red');
			errnum++;	
		}
		if( !__isValidBDYear() ) {
			
			valid = false;
			errs.push(errnum + '. Please enter a valid Birthdate (YYYY)');
			$('.birthday_year').css('border-color', 'red');
			errnum++;	
		}
		
		valid_bd = valid;	// is birthdate valid?
		
		// age
		if( valid_bd ) {
			
			var bd_year = $('.birthday_year').val();
			var bd_month = $('.birthday_month').val() - 1;	// javascript Date starts at 0 for month (0-11)
			var bd_day = $('.birthday_day').val();
			
			// must be older than 13
			if( __age(new Date(bd_year, bd_month, bd_day)) < 13 ) {
				
				// short circuit here
				$('input, select').css('border-color', '#CCC');
				$('.age').css('border-color', 'red');
				alert('You must be over 13 in order to register.');
				__recordUnder13( $('.email').val() );
				return false;
			}
		}
		
		// gender
		if( $('.gender:checked').val() == null ) {
			
			valid = false;
			errs.push(errnum + '. Please select your gender');
			errnum++;
		}
		
		if( !valid )
			alert( 'Please correct the following error(s) to continue:\n' + errs.join('\n') );
		
		return valid;
	}
	
	function __isValidBDMonth()
	{
		var val = $('.birthday_month').val();
		return !isNaN(val) && val > 0 && val <= 12;
	}
	
	function __isValidBDDay()
	{
		var val = $('.birthday_day').val();
		return !isNaN(val) && val > 0 && val <= 31;
	}
	
	function __isValidBDYear()
	{	
		var val = $('.birthday_year').val();
		return !isNaN(val) && val > 1900;
	}
	
	function __isValidEmail(email)
	{
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		return reg.test(email);
	}
	
	function __isValidPhone(phone)
	{
		var hasLetters = /([a-zA-Z])/;
		return !hasLetters.test(phone);
	}
	
	function __isValidPhoneLength(phone)
	{
		var justnums = phone.replace(/\D/g,'');
		return justnums.length >= 10 && justnums.length <= 11;
	}
	
	function __isValidZip(zip)
	{
		var hasLetters = /([a-zA-Z])/;
		return !hasLetters.test(zip);
	}
	
	function __isValidZipLength(zip)
	{
		var justnums = zip.replace(/\D/g,'');
		return justnums.length == 5 || justnums.length == 9;
	}
	
	function __isOfAge(dob)
	{
		var age = 13;	// must be at least 13
		var today = new Date();
		var valid_dob = new Date( today.getFullYear() - age, today.getMonth(), today.getDate() );
		
		return dob <= valid_dob;
	}
	
	function __age(dob)
	{
		var today = new Date();
		var age = new Date( today.getFullYear() - dob.getFullYear(), today.getMonth() - dob.getMonth(), today.getDate() - dob.getDate() );
		
		if( today.getDate() ==  dob.getDate() )
			return age.getFullYear() - 1900 + 1;	// add 1 year for their birthday today!
		
		return age.getFullYear() - 1900;
	}
});

function __recordUnder13(email)
{	
	var blocked_emails = $.cookie('blocked_emails');
	var blocked_emails_arr = blocked_emails.split(',');
	
	blocked_emails_arr.push(email);
	
	blocked_emails = blocked_emails_arr.join(',');
	$.cookie('blocked_emails', blocked_emails);
}

// see if we have an under-13 attempt at the following email
function __isBlocked(email)
{
	var blocked_emails = $.cookie('blocked_emails');
	
	if( blocked_emails == null )
	    return false;
	
	var blocked_emails_arr = blocked_emails.split(',');
	
	// console.log(blocked_emails);
	
	for( var i in blocked_emails_arr )
		if( blocked_emails_arr[i] == email )
			return true;
	
	return false;
}