$extend( FY, {

	initContactForm: function( itemId )
	{
		var resetFields = function() {	
			FY.FormTools.initDefaultValueAsHint( 'contactForm_name', 'Your Name' );
			FY.FormTools.initDefaultValueAsHint( 'contactForm_emailAddress', 'Your Email Address' );
			FY.FormTools.initDefaultValueAsHint( 'contactForm_phoneNumber', 'Your Phone Number' );
			FY.FormTools.initDefaultValueAsHint( 'contactForm_message', 'Your Message' );
		};
		
		resetFields();
	
		if ( !$( 'contactForm' ) )
			return;
			
		$( 'contactForm' ).addEvent( 'submit', function(e) {
			
			e.stop();
			
			var data = {
				itemId: itemId,
				contactName: $( 'contactForm_name' ).value,
				emailAddress: $( 'contactForm_emailAddress' ).value,
				phoneNumber: $( 'contactForm_phoneNumber' ).value,
				message: $( 'contactForm_message' ).value
			};
			
			if ( !/^[\w\-]+(\.[\w\-]+)*@[\w\-]+\.([\w\-]+\.)*[a-z]{2,}$/i.test( data.emailAddress ) )
   			{
   				alert( 'That is not a valid email address, please try again.' );
   				$( 'contactForm_emailAddress' ).focus();
   				return;
   			}
			
			// Shouldn't the form validation function pick up on this?
			if ( data.message == 'Your Message' )
				data.message = '';
			
			data.name = data.contactName + ' - ' + ( data.message > 250 ? data.message.slice( 0, 250 ) + '...' : data.message );
			
			if ( !FY.FormTools.validateFields( data, {
					'message': 'Please enter your message before trying to send the form.'
				}))
				return;
			
			
			$( 'contactForm_submit' ).set( 'value', '...' ).set( 'disabled', true );
				
			
			Aurora.callAPI({
				
				key: 'create enquiry',
				data: data,
				
				onComplete: function( rtnData ) {
					
					$( 'contactForm_submit' ).set( 'value', 'Send' ).set( 'disabled', false );
					
					if ( rtnData.success )
					{
						$( 'contactForm' ).hide();
						
						$( 'contactForm_success' ).show();
						
						$( 'contactForm_name' ).set( 'value', '' ).fireEvent( 'blur' );
						$( 'contactForm_emailAddress' ).set( 'value', '' ).fireEvent( 'blur' );
						$( 'contactForm_phoneNumber' ).set( 'value', '' ).fireEvent( 'blur' );
						$( 'contactForm_message' ).set( 'value', '' ).fireEvent( 'blur' );
						
						$( 'map' ).setStyle( 'width', '730px' );
					}
					else
					{
					
						alert( "Sorry, there was an error sending your message. Please reload the page and try again.\n\nIf you continue to get this message, please get in contact with us." );
						return;
					
					}
					
					resetFields();
					
				}
				
			});
			
		});
	
	},
	
	renderMap: function( data )
	{
		var $map = $( 'map' );
		
		if ( !GBrowserIsCompatible() ) {
			$( 'map' ).hide();
			return;
		}
		
	    var map = new GMap2( $map );
	   	var geocoder = new GClientGeocoder();
		
		if ( !data.location || !data.location.longitude && !data.location.latitude ) {
			$map.hide();
			return;
		}
		
		var x = data.location.longitude.toString();
		var y = data.location.latitude.toString();
		
		if ( !x && !y ) {
			$map.hide();
			return;
		}
		
		var center = new GLatLng( Number( y ), Number( x ) );
		
		map.setCenter( center, 14 );
		map.addControl( new GSmallMapControl() );

		var marker = new GMarker( center );
		
		map.addOverlay( marker );
		
	}

});
