$(document).ready(function() {
	$('input[title]').each(function() {
		if($(this).val() === '') {
			$(this).val($(this).attr('title'));
		}

		$(this).focus(function() {
			if($(this).val() === $(this).attr('title')) {
				$(this).val('').addClass('focused');
			}
		});

		$(this).blur(function() {
			if($(this).val() === '') {
				$(this).val($(this).attr('title')).removeClass('focused');
			}
		});
	});
	
	
	var searchUrl = '/ajax/search.php';
	
	$('#searchBox').autocomplete(searchUrl, {
		selectFirst: false,
		max: 20,
		matchSubset: true,
		matchContains: true,
		parse: function(data) {
			return $.map(eval(data), function(row) {
				return {
					data: row,
					value: row.name,
					result: row.name
				}
			});
		},
		formatItem: function(item) {
			return item.name;
		}	
	}).result(function(event, item) {
		location.href = item.url;
	});

});


function doSearch() {
	var box = document.getElementById('searchBox');

	var search = escape(box.value);
	if(search.length == 0 || search == 'Search...') {
		alert('Please enter a search term');
		return false;
	}
	window.location.href='/search/'+search+'/';
	return false;
}

function validateEmail(field, alerttxt)
{
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2)
		{
			alert(alerttxt);
			return false;
		}
		else {
			return true;
		}
	}
}

function validateForm(thisform)
{
	with (thisform)
	{
		if (validateEmail(email, "Whoops! Please enter a valid email address.") == false)
		{
			email.focus();
			return false;
		}
	}
}
