var toggled = false;
var fieldHasFocus = false;
var timeout = null;

function toggleDetails() {
	$('banner_close').show();
	$('message').hide();
	$('message_2').show();
	$('secondary').show();
	$('submit_email').show();
	$('submit_name').hide();
	$('join_club').hide();
	if (fieldHasFocus) {
		timeout = setTimeout(toggleDetails, 300);
		return;
	}
	return true;
}

function submitName() {
	clearTimeout(timeout);
	if(!toggled) {
		timeout = setTimeout(toggleDetails, 0);
	}
	$('acquisition_banner').addClassName('step_2');
}

function resetBanner() {
	$('formSubscribe').reset();
	$('banner_close').hide();
	$('message_2').hide();
	$('message').show();
	$('submit_name').show();
	$('primary').show();
	$('secondary').hide();
	$('submit_email').hide();
	$('acquisition_banner').removeClassName('step_2');
	$('join_club').show();
	if(!toggled) {
		clearTimeout(timeout);
	} else {
		timeout = setTimeout(toggleDetails, 0);
	}
}

function submitForm() {
	if (oldEnough()) {
		$('banner_close').hide();
		$('message_2').hide();
		$('primary').hide();
		$('secondary').hide();
		$('message').innerHTML = "<img src=\"/images/bfc_banner/officially_a_member.gif\" alt=\"\" />";
		$('message').show();
		$('acquisition_banner').removeClassName('step_2');
		oldEnough();
		$('formSubscribe').request();
	} else {
		alert("You must be at least 13 years of age to participate in the Blizzard Fan Club. If you're younger than 13 years of age, visit our kids website at Deeqs.com.");
	}
	return false;
}

function oldEnough() {
	var minAge	= 13;
	var day		= parseInt($('DOBD').value);
	var month 	= parseInt($('DOBM').value - 1);
	var year	= parseInt($('DOBY').value);
	var age		= new Date((year + minAge), month, day);
	var today	= new Date();
	if (today.getTime() - age.getTime() < 0) {
		return false;
	} else {
		return true;
	}										
}

function clearTextFields() {

	$('FirstName').onfocus = function() {
		// if already cleared, do nothing
		if (this._cleared) return
	
		// when this code is executed, "this" keyword will in fact be the field itself
		this.clear()
		this._cleared = true
	}
	$('LastName').onfocus = function() {
		// if already cleared, do nothing
		if (this._cleared) return
	
		// when this code is executed, "this" keyword will in fact be the field itself
		this.clear()
		this._cleared = true
	}
	$('EmailAddress').onfocus = function() {
		// if already cleared, do nothing
		if (this._cleared) return
	
		// when this code is executed, "this" keyword will in fact be the field itself
		this.clear()
		this._cleared = true
	}
}

Event.observe(window, 'load', function() {
  clearTextFields();
  new RSV({
    displayType: 'alert-all',
		formID: "formSubscribe",
		onCompleteHandler: submitForm,
    rules: [
      "required,FirstName,Please enter your first name.",
      "required,LastName,Please enter your last name.",
      "valid_date,DOBM,DOBD,DOBY,any_date,Please enter a valid date.",
      "required,EmailAddress,Please enter your email address.",
      "valid_email,EmailAddress,Please enter a valid email address."
    ]
  });
});

