addLoadListener(init);
addLoadListener(dateboxes);

function init()
{
	var submit = document.getElementById("submit");
	submit.onclick = validate;

	return true;
};

function dateboxes()
{
	var month1 = document.forms["contact"]["month1"];
	var month2 = document.forms["contact"]["month2"];
	var month3 = document.forms["contact"]["month3"];
	var month4 = document.forms["contact"]["month4"];
	
	month1.onchange = function() {clearOthers(1);}
	month2.onchange = function() {clearOthers(2);}
	month3.onchange = function() {clearOthers(3);}
	month4.onchange = function() {clearOthers(4);}
};

function validate()
{
	var errors = '';
	var errorCount = 0;
	var plural = '';
	
	var name = document.getElementById("name");
	var email = document.getElementById("email");
	var alphaQ = document.getElementById("alphabet_question");
	
	//alert('Number of People = ' + document.getElementById("numpeople").selectedIndex);
	
	//These are here because IE is rubbish!
	var numPeople = document.getElementById("numpeople");
	//var numPeople = altNum.options[altNum.selectedIndex];

	if (name.value == "" || /^\s+$/.test(name.value))
	{
		errors += ' - Name has been left empty.\n';
		errorCount++;
	}
	if (email.value == "" || !/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/.test(email.value))
	{
		errors += ' - Email must contain an e-mail address.\n';
		errorCount++;
	}
	//Using .selectedIndex instead of .value, so IE6 works...
	if ((parseInt(numPeople.selectedIndex) != numPeople.selectedIndex - 0) || numPeople.selectedIndex == 0)
	{
		errors += ' - Must specify number of people.\n';
		errorCount++;
	}
	if (alphaQ.value != 'a' && alphaQ.value != 'A')
	{
		errors += ' - Must answer the alphabet question.\n';
		errorCount++;
	}
	
	var month1 = document.getElementById('month1');
	var month2 = document.getElementById('month2');
	var month3 = document.getElementById('month3');
	var month4 = document.getElementById('month4');
	var dataCount = 0;

	if (month1.value != "") dataCount++;
	if (month2.value != "") dataCount++;
	if (month3.value != "") dataCount++;
	if (month4.value != "") dataCount++;

	if (dataCount != 1)
	{
		errors += ' - Please select a single date for your tour.\n';
		errorCount++;
	}
	
	if (errorCount > 1) plural = 's';
	
	if (errors != '')
	{
		errors = '  Please correct the following error' + plural + ' on the form:\n' + errors;
		alert(errors);
		return false;
	}
	else
	{
		return true;
	}
};

function clearOthers(box)
{
	switch(box)
	{
		case 1:
			document.forms["contact"]["month2"].value = "";
			document.forms["contact"]["month3"].value = "";
			document.forms["contact"]["month4"].value = "";
			break;
		case 2:
			document.forms["contact"]["month1"].value = "";
			document.forms["contact"]["month3"].value = "";
			document.forms["contact"]["month4"].value = "";
			break;
		case 3:
			document.forms["contact"]["month1"].value = "";
			document.forms["contact"]["month2"].value = "";
			document.forms["contact"]["month4"].value = "";
			break;
		case 4:
			document.forms["contact"]["month1"].value = "";
			document.forms["contact"]["month2"].value = "";
			document.forms["contact"]["month3"].value = "";
			break;
		default:
	}
};

function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined')
	{
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined')
	{
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined')
	{
		window.attachEvent('onload', fn);
	}
	else
	{
		var oldfn = window.onload;
		if (typeof window.onload != 'function')
		{
		  window.onload = fn;
		}
		else
		{
			window.onload = function()
			{
				oldfn();
				fn();
			};
		}
	}
};