/**
 * AJAXGuestbook.js
 * Tom McFarlin / December 2007
 *
 * This script uses AJAX operations to manager the guestbook
 * functionality.
 */

var AJAXGuestbook = Class.create();
AJAXGuestbook.prototype = {

	/*-----------------------------------------------------
	 * constructors
	 */

    /**
     * Initializes this manager by setting up event handlers.
     */
    initialize: function() {

        this.validator = new Validation('guestbook-form');

        this._setupHandlers();

    },  // end constructor

    /*-----------------------------------------------------
	 * functions
	 */

    /**
     * Attaches event handlers to the Sign/View buttons and sets up
     * the AJAX operation handlers.
     */
    _setupHandlers: function() {

        var This = this;

        // guestbook submission event handler
        Event.observe('gb_sign', 'click', function(e) {

            // make the user confirm the signing
            if(This.validator.validate()) {

                if(confirm("Please confirm that you wish to sign the guestbook.")) {

                    // setup the AJAX Request object to post the data
                    new Ajax.Request('http://www.davidleinweber.com/php/guestbook.php?action=sign', {

                        method: 'post',

                        // form.serialize produces an error, so I'm hard coding this
                        parameters: {
                            gb_name: $('gb_name').value,   
                            gb_contact: $('gb_info').value,
                            gb_message: $('gb_message').value
                        }, // end parameters

                        // success callback
                        onSuccess: function(transport) {
						
							alert('Thank you for your message!');
							window.location = "http://www.davidleinweber.com/?page=guestbook&start_at=0";
								
                        }, // end onSuccess

                        // failure callback
                        onFailure: function() {
                            alert('Your message failed to be sent. Please try again.');
                        }

                    }); // end AJAX request

                } // end if

            } // end if

        }); // end guestbook submission handler

        // cancel handler
        Event.observe('gb_cancel', 'click', function(e) {

            // clear the input fields
            $('gb_name').value = "";
            $('gb_info').value = "";
            $('gb_message').value = "";

            // reset the validator
            This.validator.reset();

        });

    } // end _setupHandlers

} // end GuestbookManager class
