/*

	Leach Homes
	Brochure Request JS Functions
	Created: 13th July 2010 by AM

*/

function showHideOtherTitle(e) {
	if($F('contTitle') == 'Other')
		$('contOtherTitle').style.display = '';
	else
		$('contOtherTitle').style.display = 'none';
}


function validateForm(e) {

	var tmp;
	var errs = '';
	var reqFields = new Array('contFirstName', 'contLastName', 'contEmail', 'contTelephone', 'contAddress1', 'contTownCity', 'contPostCode');
	var fieldNames = new Array('First Name', 'Last Name', 'Email Address', 'Telephone Number', 'First Address Line', 'Town or City', 'Post Code');

	if($F('contTitle') == 'Other')
	{
		tmp = trimString($F('contOtherTitle'));
		errs += '- your title is missing\n';
	}
	
	for(var i = 0; i < reqFields.length; i++)
	{
		tmp = trimString($F(reqFields[i]));
		if(tmp == '')
			errs += '- ' + fieldNames[i] + ' is missing.\n';
	}
	
	if(!isEmailFormatCorrect($F('contEmail')))
		errs += '- email address is not in the correct format.\n';
	
	var brochureId = 1;
	var bBrochureSelected = false;
	
	while($(('br' + brochureId)) != null)
	{
		if($(('br' + brochureId)).checked == true)
			bBrochureSelected = true;
		brochureId++;
	}
	
	if(!bBrochureSelected)
		errs += '- please select one or more brochures.\n';
	
	if(errs != '')
	{
		alert('Sorry, you have not entered all the required information.\n\n' + errs + '\nPlease fix and try again.');
	}
	else
	{
		$('brochureForm').submit();
	}
}

Event.observe(window, 'load', function() {
	$('contTitle').observe('click', showHideOtherTitle); // IE and FF
	$('contTitle').observe('change', showHideOtherTitle); // WEBKIT
	$('contTitle').observe('keyup', showHideOtherTitle);
	$('formSubmit').observe('click', validateForm);
});
