// Little utility function to check if a string is empty :)
function isEmpty( _0 ) 
{ 
	if ( _0 == null || _0 == "" )
	{
		return true;
	}
	
	return false;
}

// Clears a field of it's default value
function cClear ( _0 )
{ 
	var field = $( '#' + _0 );
	
	field.val('');
	field.css('color', '#660000');
}

// Resets a field of it's default value if nothing has been entered
function cResetDefault ( _0, _1 )
{
	var field = $( '#' + _0 );
	
	var defaultValue = _1;
	
	var currentValue = field.val().replace(/\s/g, "");
	
	if ( isEmpty ( currentValue ) )
	{
		field.val( defaultValue );
		field.css('color', '#cccccc');
	}
}

// is a string empty
function cIsEmpty ( _0 )
{
	var str = _0;
	
	if ( str.length == 0 || str == '' || str == null )
	{
		return true;
	}
	
	return false;
}

// Submits the contact form and returns result
function cSubmit ( )
{	
	if ( cIsEmpty ( $( '#aw-email-input' ).val() ) || 
		  $( '#aw-email-input' ).val() == 'your@email.com' )
	{
		alert ( 'Please enter your e-mail address.' );
	}
	else
	{ 
	
	// i'll get back to this ajax mess in a bit.
	var awemail = $('#aw-email-input').val();
	
	$( '#aw-form' ).html( '<div id=\'aw-addemail\'><h2>saving...</h2></div>' ).load( 'src/submitform.ajx.php', {
					'email': awemail,
					'randomz': Math.random()*99999
	});
	
	}
}

// Wait for page to load, then start listening!
$( document ).ready( function( )
{
	$( '#aw-submit' ).click(function ( )									  
	{
		cSubmit ( );
	});
	
	// Email
	$( '#aw-email-input' ).focus(function ( )									  
	{
		cClear ( 'aw-email-input' );
	});
	
	$( '#aw-email-input' ).blur(function ( )									  
	{
		cResetDefault ( 'aw-email-input', 'your@email.com' );
	});
	
});
