var recruitSearchDefault = 'E.g. Driver';
var recruitLocationDefault = 'E.g. London, Greater London, South East, ENGLAND';

$.fn.extend({
	clear_input: function(def)
	{
		if (this.val() == def)
		{
			$(this).css({ 'color': '#5F5F5F' });
		}
		
		this.focus( function()
		{
			if (this.value == def)
			{
				this.value = '';
				$(this).css({ 'color': '#000000' });
			}
		});
		
		this.blur( function()
		{
			if (this.value == '')
			{
				this.value = def;
				$(this).css({ 'color': '#5F5F5F' });
			} else
			{
				$(this).css({ 'color': '#000000' });
			}
		});
	}
});

$(document).ready(function()
{
	$('#recruitment #search').clear_input(recruitSearchDefault);
	$('#recruitment #location').clear_input(recruitLocationDefault);
	$('#recruitment #je_register_email').clear_input('Enter email address');
	
	/*
	$('#recruitment #location').autocomplete(
		'/xml/locations.php',
		{
			delay:10,
			minChars:3,
			matchSubset:1,
			matchContains:1,
			cacheLength:10,
			autoFill:true
		}
	);
	*/

	$('#recruit_search_je').submit( function()
	{
		var searchURI = 'http://' + window.location.host +'/recruitment/';
		
		if ($('#recruitment #location').val() != '' && $('#recruitment #location').val() != recruitLocationDefault)
		{
			var s = $('#recruitment #location').val().toLowerCase().split(",");
			for(var i=0; i<s.length; i++)
				s[i] = s[i].replace(/^\s+|\s+$/g,"").replace(/ /g, ".");
				
			searchURI += s.join(',') + '/';
		} else
		{
			searchURI += 'all/';
		}
		
		if ($('#recruitment #search')[0].value != '' && $('#recruitment #search')[0].value != recruitSearchDefault)
		{
			searchURI += $('#recruitment #search')[0].value.replace(/^\s+|\s+$/g,"").replace(/ /g, ".") + '/';
		} else
		{
			$('#recruitment #search').addClass('errorField');
			return false;
		}
		
		//window.open(searchURI);
		window.location.href = searchURI;
		
		return false;
	});
	
	$('#je_register').submit( function()
	{
		var registerURI = 'http://www.justrail.net/JobSeekerMembership.aspx?email=';
		
		if ($('#je_register_email').val() != '' && $('#je_register_email').val() != 'Enter email address')
		{	
			$('#je_register_email').removeClass('errorField');
			window.open(registerURI + $('#je_register_email').val())
		} else
		{
			$('#je_register_email').addClass('errorField');
		}
		
		return false;
	});
});
	
