var getFormValue = function( key ) {

    // used for everything except select fields
    var form = $( 'feedbackForm' );
    
    // just return the value if its one field e.g a standard text field
    if ( form[ key ] && !form[ key ].length ) {
    	var inputValue = form[ key ].value;
        return inputValue;
	}

    // otherwise loop through all the fields until we get a true value, then return it
    $log( 'looking for: ' + key );
    
    for ( var i = 0; i <= form[ key ].length - 1; i++) {
    
        var inputValue = form[ key ][ i ].checked;

        if ( inputValue )
        {
            // console.log( 'found true value, returning: ' + inputValue )
            
            var inputValue = form[ key ][ i ].value;
            
            return inputValue; 
        }
        // else
            // console.log( 'false value encountered (' + inputValue + '), continuing...' );
        
    };

};

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

    // populate the select field
    /* new Request.JSON({
        
        url: "/Platforms/Aurora/Remote/Hairfree.cfc?method=GetLocations&appointmentType=lumologie",
    
        onComplete: function( rtnData ) {
        
    		rtnData.each( function( i ) {
    		
    			new Element( 'option' )
    				.setProperty( 'value', i.CalendarId )
    				.set( 'html', i.State + ' - ' + i.Suburb )
    				.inject( $('locationId') );
    		
    		} );
    	
        }
    
    }).post(); */
	
	$( 'submit' ).addEvent( 'click', function( event ) {
	
	    event.stop();
	    
	    var form = $( 'feedbackForm' ); 
	    
	    var send = {};
	 
        send.bookingType = getFormValue( 'bookingType' );
        send.firstname = getFormValue( 'firstname' );
        send.lastname = getFormValue( 'lastname' );
        send.emailaddress = getFormValue( 'emailAddress' );
        send.phone = getFormValue( 'phone' );
	    send.locationId = form.locationId.value;

		// send.interested = getFormValue( 'interested' );
		send.interested = [];

	    if ( $( 'checkbox_cellulite' ).checked )
	    	send.interested.push( 'Cellulite' );
	    	
	    if ( $( 'checkbox_losingInches' ).checked )
	    	send.interested.push( 'Losing Inches' );
	    	
	    if ( $( 'checkbox_detoxification' ).checked )
	    	send.interested.push( 'Detoxification' );
	    	
	    send.interested = send.interested.toString();
	    	
        send.referralType = $( 'selectField_referralType' ).value;
        
        // send.promotionCode = getFormValue( 'promotionCode' );
    
    	$log( send );
    	
    	if ( !send.firstname || !send.lastname || !send.emailaddress || !send.phone || !send.locationId || !send.interested.length ) {
    		alert( 'Please ensure all required fields are entered.\n\nThese fields are indicated by the * symbol.' );
    		return;
    	}
    	
    	$( 'submit' ).setProperty( 'disabled', 'disabled' );
    
        new Request.JSON({
            
     	    url: "/Platforms/Aurora/Remote/hairfree.cfc?method=OnlineBooking&bookingFormKey=lumologie",

	        onComplete: function( rtnData ) {
	        
	            // $log( rtnData );
			
			    if ( rtnData.SUCCESS )
			    {
			        // form.setStyle( 'display', 'none' );
			        
			        // new Element( 'div' ).set( 'html', '<h2>Booking Successful</h2><p>One of our staff will be in contact with you soon to confirm the booking.</p>' ).injectAfter( form );

			        top.location.href = '/Page/BookOnlineSuccess';
			    }
			    else
			    {
	                // $log( rtnData.invalid );    // struct of invalid fields, key = field name, value = error message
			        
			        /* var errorMessage = '';
			        
			        for ( i in rtnData.invalid )
			        {
			            console.log( rtnData.invalid[i] );
			            errorMessage += rtnData.invalid[i];
			        } */
			        
			        $( 'submit' ).removeProperty( 'disabled' );
	                            
	                alert( 'Please make sure you have entered all required fields.\n\nThis includes: name, email, phone, location and what you are interested in, as indicated.' );
			    }
	
	        }
	
	    }).post( send );
		
	}); 
});