// Ensure an e-mail address is entered and at least one checkbox is selected on step 1.
function validateSubscribe()
{
	// Validate e-mail address field.
	if (!isFieldValidAndFocus(document.getElementById("Email"), "Email", false, "Email address"))
	{
		return false;
	}

	/* Add up the bitwise values to indicate products of interest.
	2:     Credit cards
	4:     Loans
	8:     Savings accounts
	16:    Home insurance
	32:    Car insurance
	64:    Travel insurance
	128:   Current accounts
	256:   Mortgages
	512:   Motoring
	1024:  Life insurance
	2048:  Travel
	4096:  Gas and electricity
	8192:  Mobile phones
	16384: Broadband
	32768: Shopping
	*/
	var chkWeeklyBW = document.getElementsByName("chkWeeklyBW");
	var chkWeeklyCount = 0;
	for (var i = 0; i < chkWeeklyBW.length; i++)
	{
		if (chkWeeklyBW[i].checked)
		{
			chkWeeklyCount += parseInt(chkWeeklyBW[i].value);
		}
	}
	document.getElementById("chkWeekly").value = chkWeeklyCount;

	return true;
}

// Optional details page validation.
function validateOptionalDetails()
{
	if (document.getElementById("title").selectedIndex == 0)
	{
		alert("Please select your title");
		document.getElementById("title").focus();
		return false;
	}

	if (!isFieldValidAndFocus(document.getElementById("forename"), "Alphabetic", false, "Forename"))
	{
		return false;
	}

	if (!isFieldValidAndFocus(document.getElementById("surname"), "Alphabetic", false, "Surname"))
	{
		return false;
	}

	// Check the user is over 18.
	var userage = getUserAge(parseInt(document.getElementById("dobd").value), parseInt(document.getElementById("dobm").value), parseInt(document.getElementById("doby").value))
	if (userage < 18)
	{
		alert("Sorry, you must be over 18 to apply for this promotion.");
		return false;
	}

	if (!isFieldValidAndFocus(document.getElementById("dobd"), "Day", false, "Day of birth"))
	{
		return false;
	}
	if (!isFieldValidAndFocus(document.getElementById("dobm"), "Month", false, "Month of birth"))
	{
		return false;
	}
	if (!isFieldValidAndFocus(document.getElementById("doby"), "Year", false, "Year of birth"))
	{
			return false;
	}

	return true;
}

// Calculate age.
function getUserAge(d, m, y)
{
	m--;
	var currentDate = new Date();
	var diff = currentDate.getFullYear() - y;
	
	if(currentDate.getMonth() < m || (currentDate.getMonth() == m && currentDate.getDate() < d))
	{
		diff--;
	}
	
	return diff;
}

// Ensure an e-mail address is entered.
function validateUnsubscribe()
{
	// Validate e-mail address field.
	if (!isFieldValidAndFocus(document.getElementById("txtUnsubscribe_Email"), "Email", false, "Email address"))
	{
		return false;
	}
	return true;
}
