﻿
var defaultValueLocation = "Suburb, Town, or Postcode";

function initialiseSearchForm(e) {
    var searchForm;

	var list = document.searchbar.service;
	addServicesToList(list);

    var cookies = new CookieService;
    if (cookies.getCookie('lastLocation'))
        document.searchbar.searchLocation.value = cookies.getCookie('lastLocation');
    else
    {
        document.searchbar.searchLocation.value = defaultValueLocation;
        document.searchbar.searchLocation.style.color = '#959595';        
    }
    
    if (cookies.getCookie('lastService')) {
        var lastService = cookies.getCookie('lastService');
        
        for (i=0; i<list.options.length; i++) 
        {
            if (list.options[i].value == lastService)
                list.options[i].selected = true;
        }
    }
    
}

function validateSearch()
{
    var prefixErrorMessage = 'Help us help you\nWe need more information to complete your search.\n';
    var errorMessage = '';
        
    if (document.searchbar.service[document.searchbar.service.selectedIndex].value=='0')
		errorMessage += '\n-Please select a Service or Trade';
	if (document.searchbar.searchLocation.value.length==0 || document.searchbar.searchLocation.value == defaultValueLocation)
	    errorMessage += '\n-Please enter a Suburb, Town or Postcode';

	if (errorMessage != "")
    {
        alert(prefixErrorMessage + errorMessage);
        return false;
    }
    else
    {
        document.searchbar.submit();
        return true;
    }
}

// Immediately execute initialiseSearchForm - this relies on the script being added to the page AFTER the
// search form HTML has been delivered.
initialiseSearchForm();