var PaulBurkhart = {};

function isEmailValid( email ) {
	var regEx = /[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+?\.[a-zA-Z]{2,3}$/;
	return regEx.exec( email );
}

function isPhoneNumberValid( phoneNumber ) {
	var regEx = /^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/;
	return regEx.exec( phoneNumber );
}

function validateForm( formToValidate )
{
	var result = true;
	
	var errorMessage = formToValidate.find( '#error' );
	$( ':input', formToValidate ).each(
		function( item ) {
			$( this ).parent().find( 'LABEL' ).removeClass( 'isNotValid' );
		}
	);
	errorMessage.addClass( 'hidden' );
	
	var emailField = formToValidate.find( '#email' );
	var messageField = formToValidate.find( '#message' );
	var nameField = formToValidate.find( '#name' );
	var phoneField = formToValidate.find( '#phone' );
	if ( !isEmailValid( emailField.val() ) 
		&& !isPhoneNumberValid( phoneField.val() ) ) 
	{
		emailField.parent().find( 'LABEL' ).addClass( 'isNotValid' );
		phoneField.parent().find( 'LABEL' ).addClass( 'isNotValid' );
		result = false;
	}
	if ( $.trim( messageField.val() ) === '' ) {
		messageField.parent().find( 'LABEL' ).addClass( 'isNotValid' );
		result = false;
	}
	if ( $.trim( nameField.val() ) === '' ) {
		nameField.parent().find( 'LABEL' ).addClass( 'isNotValid' );
		result = false;
	}	
	if ( !result ) {
		errorMessage.removeClass( 'hidden' );
	}
	
	return result;
}

PaulBurkhart.Contact =
	function() {
		var mapForm = $( '#mapForm' );
		var addressField = mapForm.find( 'input:text' );
		var directionsButton = mapForm.find( 'BUTTON' );
		var directionsListing = document.getElementById( 'directionsListing' );
		var directionsMap = document.getElementById( 'directionsMap' );
		var geoCoder;
		var googleDirections;
		function loadMap() {
			if ( GBrowserIsCompatible () ) {
				directionsMap = new GMap2 ( directionsMap );
				geocoder = new GClientGeocoder ();

				directionsMap.addControl ( new GSmallMapControl () );
				directionsMap.addControl ( new GMapTypeControl () );
				directionsMap.setCenter ( new GLatLng ( 26.836011, -80.072782 ), 13 );
				
				var point = new GLatLng( 26.836011, -80.072782 );
				directionsMap.addOverlay( new GMarker( point ) );
				
				googleDirections = 
					new GDirections ( 
						directionsMap, 
						directionsListing );
			}
		};
		addressField.blur(
			function() {
				if ( $.trim( addressField.val() ) === '' ) {
					addressField.parent().find( 'LABEL' ).show();
				}
			}
		);
		addressField.focus( 
			function(){
				addressField.parent().find( 'LABEL' ).hide();
			}
		);
		directionsButton.click(
			function() {
				if ( $.trim( addressField.val() ) !== '' ) {
					geocoder.getLatLng ( addressField.val(), function ( point ) { } );
					directionsListing.innerHTML = "";
					directionsListing.className = "display";
					googleDirections.clear();
					googleDirections.load( "from: " + addressField.val() + " to: 800 Village Square Crossing, Palm Beach Gardens, FL 33410" );
				}
			}
		);
		$( window ).load(
			function() {
				loadMap();
			}
		).unload(
			function() {
				GUnload();
			}
		);

		var emailForm = $( '#emailForm' );
		var sendButton = emailForm.find( 'BUTTON' );
		sendButton.click(
			function() {
				if ( validateForm( emailForm ) ) {
					$.ajax(
						{
							data: emailForm.serialize(),
							type: 'POST',
							url: '/include/process/contact.php',
							success: function() {
								emailForm.find( 'FIELDSET' ).fadeOut( 
									'slow',
									function() {
										$( '#success' ).fadeIn( 'slow' );
									}
								);
							}
						}
					);
				}
			}
		);
	};