/*

*******************************************************************
File: contents_form.js
Date: 01/04/2005
Purpose: Provides DHTML behaviour and form validation to the Contents Calculator

REQUIRES these javascript files to also be included in the host page:
	- scripts/event.js
	- scripts/error.js
	- scripts/string.js
	- scripts/postcode.js

HISTORY:
*******************************************************************

*/

// show/hide all "select" elements
// these functions are required here, as the contents form does not include standard nav code


function mbHE(v) 
{
	mbHEV(v,document.getElementsByTagName('select'));
}

function mbHEV(v,e) 
{
	for (var i=0;i<e.length;i++) e[i].style.visibility=v;
}

function calculatorOnLoad()
{
	document.forms['home-information'].elements['postcode'].focus();
	document.forms['home-information'].onsubmit = formSubmit;
	
	document.getElementById('button-show-search').onclick = showSuburbSearch;
	document.getElementById('button-close-search').onclick = hideSuburbSearch;
	
	document.getElementById('suburb').onkeyup = suburbSearchOnKeyUp;
	document.getElementById('button-search').onclick = suburbSearchSubmit; 
	document.getElementById('suburb-search').onsubmit = suburbSearchOnSubmit;
	document.getElementById('suburb-search').onmouseover = suburbSearchOnMouseOver;
}

function formSubmit()
{
	var errors = new Array();
	var errorFields = new Array();
		
	var homeInformation = document.forms['home-information'];
	
	if (homeInformation.elements["postcode"].value.trim() == '')
	{
		errors[errors.length] = (errors.length + 1) + '. ' + getErrorMessage(1);
		errorFields[errorFields.length] = homeInformation.elements["postcode"];
	}
	else
	{
		if (!homeInformation.elements["postcode"].value.isInteger())
		{
			errors[errors.length] = (errors.length + 1) + '. ' + getErrorMessage(1);
			errorFields[errorFields.length] = homeInformation.elements["postcode"];
		}
		else
		{
			if (!isValidPostcode(homeInformation.elements["postcode"].value))
			{
				errors[errors.length] = (errors.length + 1) + '. ' + getErrorMessage(2);
				errorFields[errorFields.length] = homeInformation.elements["postcode"]; 
			}
		}
	}
	
	if (homeInformation.elements["contents-insured"].value.trim() != '')
	{
		if (!homeInformation.elements["contents-insured"].value.isNumeric())
		{
			errors[errors.length] = (errors.length + 1) + '. ' + getErrorMessage(6);
			errorFields[errorFields.length] = homeInformation.elements["contents-insured"];
		}
	}
	
	if (errors.length > 0)
	{
		// display the errors
		alert('The following corrections need to be made:\r\n' + errors.join('\r\n'));
		
		// set focus to the first error
		errorFields[0].focus();
	}
	
	return errors.length == 0;
}