// 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('');
}

// 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 );
	}
}

// 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 ( $( '#sweet-name' ).val() ) || 
		  $( '#sweet-name' ).val() == 'First & Last' || 
		  cIsEmpty ( $( '#sweet-email' ).val() ) || 
		  $( '#sweet-email' ).val() == 'YourEmail@domain.com' ||
		  cIsEmpty ( $( '#sweet-phone' ).val() ) || 
		  $( '#sweet-phone').val() == '(222) 222-2222' ||
		  cIsEmpty ( $( '#sweet-comment' ).val() ) )
	{
		alert ( 'All fields are required.' );
	}
	else
	{ 
	// i'll get back to this ajax mess in a bit.
	var sweetname = $('#sweet-name').val();
	var sweetemail = $('#sweet-email').val();
	var sweetphone = $('#sweet-phone').val();
	var sweetstylist = $('#sweet-stylist').val();
	var sweetservice = $('#sweet-service').val();
	var sweetcomment = $('#sweet-comment').val();
	
	$( '#sweet-schedule' ).html( '<h2>scheduling appointment...</h2>' ).load( 'src/submitform.ajx.php', {
	  				'name': sweetname,
					'email': sweetemail,
					'phone': sweetphone,
					'stylist': sweetstylist,
					'service': sweetservice,
					'comments': sweetcomment,
					'randomz': Math.random()*99999
	});
	
	/*$( '#sweet-schedule' ).html( '<h2>scheduling appointment...</h2>' ).load( 'src/submitform.ajx.php',  
		'name=' + $( '#sweet-name' ).val()
		+ '&email=' + $( '#sweet-email' ).val() 
		+ '&phone=' + $( '#sweet-phone' ).val()
		+ '&stylist=' + $( '#sweet-stylist' ).val()
		+ '&service=' + $( '#sweet-service' ).val()
		+ '&comments=' + $( '#sweet-comment' ).val()
		+ '&randomz=' + Math.random()*99999
		);*/
	}
}

// Wait for page to load, then start listening!
$( document ).ready( function( )
{
	$( '#sweet-submit' ).click(function ( )									  
	{
		cSubmit ( );
	});
	
	// Name
	$( '#sweet-name' ).focus(function ( )									  
	{
		cClear ( 'sweet-name' );
	});
	
	$( '#sweet-name' ).blur(function ( )									  
	{
		cResetDefault ( 'sweet-name', 'First & Last' );
	});
	
	// Email
	$( '#sweet-email' ).focus(function ( )									  
	{
		cClear ( 'sweet-email' );
	});
	
	$( '#sweet-email' ).blur(function ( )									  
	{
		cResetDefault ( 'sweet-email', 'YourEmail@domain.com' );
	});
	
	// Phone
	$( '#sweet-phone' ).focus(function ( )									  
	{
		cClear ( 'sweet-phone' );
	});
	
	$( '#sweet-phone' ).blur(function ( )									  
	{
		cResetDefault ( 'sweet-phone', '(222) 222-2222' );
	});
	
});
