$extend( RA, {

	initContactForm: function()
	{
		if ( !$( 'form_memberContact' ) )
			return;
		
		// Hide the form
		$( 'form_memberContact' ).hide();
	
		// Show the button
		$( 'button_contact' ).show();
		
		// Set some styles
		$( 'button_contact' ).setStyle( 'display', 'block' );
		$( 'form_memberContact_container' ).setStyle( 'margin-top', '0px' );
	
		// Place the content in the right place
		var contactElements = $( 'form_memberContact_container' );
		
		new Element( 'div' )
			.inject( $( 'contact_form' ) )
			.adopt( contactElements );
			
		// Button Events
		$( 'button_contact' ).addEvent( 'click', function( e ) {
			this.hide();
			$( 'form_memberContact' ).show();
			$( 'form_memberContact_success' ).hide();
		});
		
		$( 'button_cancelContact' ).addEvent( 'click', function( e ) {
			$( 'button_contact' ).show();
			$( 'form_memberContact' ).hide();
			$( 'form_memberContact_success' ).hide();

			$( 'contactForm_name' ).set( 'value', '' );
			$( 'contactForm_emailAddress' ).set( 'value', '' );
			$( 'contactForm_message' ).set( 'value', '' );			
		});
			
		$( 'button_contactAgain' ).addEvent( 'click', function( e ) {
			$( 'form_memberContact' ).show();
			$( 'form_memberContact_success' ).hide();
		});
		
		$( 'contactForm' ).addEvent( 'submit', function(e) {
			
			e.stop();
			
			var data = {
				itemId: Aurora.dataAPI.data.load.member.results.itemId,
				contactName: $( 'contactForm_name' ).value,
				emailAddress: $( 'contactForm_emailAddress' ).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;
   			}
			
			data.name = data.contactName + ' - ' + ( data.message.length > 80 ? data.message.slice( 0, 80 ) + '...' : data.message );
			
			if ( !RA.FormTools.validateFields( data, {
					'contactName': 'Please enter your name before trying to send the form.',
					'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 )
					{
						$( 'form_memberContact_success' ).show();
						$( 'form_memberContact' ).hide();
						
						$( 'contactForm_name' ).set( 'value', '' ).fireEvent( 'blur' );
						$( 'contactForm_emailAddress' ).set( 'value', '' ).fireEvent( 'blur' );
						$( 'contactForm_message' ).set( 'value', '' ).fireEvent( 'blur' );
						
					}
					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;
					
					}
					
				}
				
			});
			
		});
	
	}

});

window.addEvent( 'domready', function() {

	RA._initHooks = [ RA.initContactForm() ];

});
