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

/* Referenced */
function toggleDetails() {
	$('banner_close').setStyle({display: "block"}); // Close
	$('message_2').setStyle({display: "block"}); // Join the Blizzard Fan Club. Share the Blizzard Love.
	$('secondary').setStyle({display: "block"}); // Birthday and Email
	$('submit_email').setStyle({display: "block"}); // Send
	$('message').addClassName('add_margin');
	$('submit_name').setStyle({display: "none"}); // Go
	if (fieldHasFocus) {
		timeout = setTimeout(toggleDetails, 300);
		return;
	}
	return true;
}

function submitName() {
	//alert ("You clicked 'Go'");
	clearTimeout(timeout);
	if(!toggled) {
		timeout = setTimeout(toggleDetails, 0);
	}
	urchinTracker('/bfcacquisition/go/');
	$('acquisition_banner').addClassName('step_2');
}

function resetBanner() {
	$('formSubscribe').reset();
	$('banner_close').setStyle({display: "none"}); // Close
	$('message_2').setStyle({display: "none"}); // Join the Blizzard Fan Club. Share the Blizzard Love.
	$('message').innerHTML = "<img src=\"/templates/img/bfc_banner/receive_coupon.gif\" alt=\"Receive a buy one, get one free Blizzard treat coupon.\" />";
	$('message').removeClassName('add_margin');
	$('submit_name').setStyle({display: "block"}); // Go
	$('primary').setStyle({display: "block"}); // First Name and Last Name
	$('secondary').setStyle({display: "none"}); // Birthday and Email
	$('submit_email').setStyle({display: "none"}); // Send
	$('acquisition_banner').removeClassName('step_2');
	if(!toggled) {
		clearTimeout(timeout);
	} else {
		timeout = setTimeout(toggleDetails, 0);
	}
	urchinTracker('/bfcacquisition/close/');
}

function submitForm() {
	if (oldEnough()) {
		// Prototype has two built-in methods--hide() and show()--which set an element's style.display property to 'none' and 'block' respectively.
		$('banner_close').hide(); // Close
		$('message_2').hide(); // Join the Blizzard Fan Club. Share the Blizzard Love.
		$('primary').hide(); // First Name and Last Name
		$('secondary').hide(); // Birthday and Email
		$('message').innerHTML = "<img src=\"/templates/img/bfc_banner/officially_a_member.gif\" alt=\"\" />";
		$('acquisition_banner').removeClassName('step_2');
		oldEnough();
		// SEND AJAX REQUEST HERE
		$('formSubscribe').request();
		urchinTracker('/bfcacquisition/success/');
	} 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.",
      "valid_email,EmailAddress,Please enter a valid email address."
    ]
  });
});
