var speed = 300;

function dl_resetFormErrors()
{
	$('#error_name').fadeOut(speed);
	$('#error_email').fadeOut(speed);
	$('#error_subject').fadeOut(speed);
	$('#error_message').fadeOut(speed);
}

function dl_checkContactForm()
{
	var errors = false;
	
	dl_resetFormErrors();
	
	if ($('#contact_name').val() == '' || $('#contact_name').val() == 'Your name')
	{
		$('#error_name').html('Geez! You could at least tell us your name!');
		$('#error_name').fadeIn(speed, function() { $("#error_name").effect("highlight", {color:'#f07878'}, 1000); } );
		errors = true;
	}

	if ($('#contact_email').val() == '' || $('#contact_email').val() == 'Your email address')
	{
		$('#error_email').html('We\'ll need an email address from you so we can reply to your message!');
		$('#error_email').fadeIn(speed, function() { $("#error_email").effect("highlight", {color:'#f07878'}, 1000); } );
		errors = true;
	} else {
		if (checkEmailAddress($('#contact_email').val()) == false)
		{
			$('#error_email').html('That email address doesn\'t look valid. Maybe try another one?');
			$('#error_email').fadeIn(speed, function() { $("#error_email").effect("highlight", {color:'#f07878'}, 1000); } );
			errors = true;
		}
	}
	
	if ($('#contact_subject').val() == '' || $('#contact_subject').val() == 'Type your subject here')
	{
		$('#error_subject').html('No subject? No response! Sorry!');
		$('#error_subject').fadeIn(speed, function() { $("#error_subject").effect("highlight", {color:'#f07878'}, 1000); } );
		errors = true;
	}

	if ($('#contact_message').val() == '' || $('#contact_message').val() == 'Type your message here')
	{
		$('#error_message').html('You\'ll need to type your message below so we have something to read!');
		$('#error_message').fadeIn(speed, function() { $("#error_message").effect("highlight", {color:'#f07878'}, 1000); } );
		errors = true;
	}

	if (errors == true)
		return false;

	return true;
}

function dl_submitSearchForm()
{
	var s = document.getElementById('search-field');
	
	if (s.value == 'Search DesignLuv') s.value = '';
	
	document.forms.searchform.submit();
}

function dl_searchFocus(e)
{
	if (e.value == 'Search DesignLuv')
		e.value = '';
	
	e.style.color = '#000000';
}

function dl_searchBlur(e)
{
	if (e.value == '')
		e.value = 'Search DesignLuv';
	
	e.style.color = '#aaaaaa';
}

function checkEmailAddress(str) {
	var goodEmail = str.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);

	if (goodEmail)
		return true;
	else
		return false;
}

function dl_fieldFocus()
{
	if ($(this).val() == 'Your name'
		|| $(this).val() == 'Your email address'
		|| $(this).val() == 'Your website URL'
		|| $(this).val() == 'Type your subject here'
		|| $(this).val() == 'Type your message here'
		)
	{
		$(this).animate({
		    color: '#ffffff'
		}, speed, function() { $(this).val(''); $(this).css('color', '#000000'); });
	}

	if ($(this).val() != 'Your name'
		|| $(this).val() != 'Your email address'
		|| $(this).val() != 'Your website URL'
		|| $(this).val() != 'Type your subject here'
		|| $(this).val() != 'Type your message here'
		)
	{
		$(this).animate({
		    color: '#000000'
		}, speed);
	}
}

function dl_fieldBlur()
{
	if ($(this).val() == 'Your name'
		|| $(this).val() == 'Your email address'
		|| $(this).val() == 'Your website URL'
		|| $(this).val() == 'Type your subject here'
		|| $(this).val() == 'Type your message here')
	{
		$(this).stop();
		$(this).css('color', '#ffffff');
	}
	
	if ($(this).val() != '')
	{
		$(this).animate({
		    color: '#aaaaaa'
		}, speed);
	}

	if ($(this).val() == '')
	{
		$(this).stop();
		$(this).css('color', '#ffffff');
		
		if ($(this).attr('id') == 'contact_name') { $(this).val('Your name'); }
		if ($(this).attr('id') == 'contact_email') { $(this).val('Your email address'); }
		if ($(this).attr('id') == 'contact_website') { $(this).val('Your website URL'); }
		if ($(this).attr('id') == 'contact_subject') { $(this).val('Type your subject here'); }
		if ($(this).attr('id') == 'contact_message') { $(this).val('Type your message here'); }
		
		$(this).animate({
		    color: '#aaaaaa'
		}, speed);
	}
}

$(document).ready(function() 
{
	$('.contact-form').focus(dl_fieldFocus);
	$('.contact-form').blur(dl_fieldBlur);
});
