document.observe('dom:loaded', function() {
    $('saveProgramButton').observe('click', function() {
        saveProgram( this, this.readAttribute('rel'));
    });
});

document.observe('dom:loaded', function() {
    $('schoolContactButtonLoggedIn').observe('click', function(event) {
        event.stop();
        $('loginSection').hide();
        $('registrationSection').hide();
        Modalbox.show( $('schoolContactFormPanel'), {
            title: this.rel,
            width: 550,
            overlayClose: false,
            afterLoad: function() {
                $('loginMessage').update();
                $('messageSection').show();
                Modalbox.resizeToContent();

                $('messageCancelButton').observe('click', function() {
                    Modalbox.hide();
                });

                displayMessageForm('');

                $('messageForm').observe('submit', function(event) {
                    event.stop();
                    sendMessage();
                });
            }
        });
    });
});

document.observe('dom:loaded', function() {
    $('schoolContactButton').observe('click', function(event) {
        event.stop();
        Modalbox.show( $('schoolContactFormPanel'), {
            title: this.title, 
            width: 550,
            // Uncomment 'height' option to enable scrolling
            // height: 560,
            overlayClose: false,
            afterLoad: function() {
                // Ensure that the submit buttons are enabled when the popup is first loaded
                $('registrationFormSubmit').writeAttribute({disabled: false});
                $('loginFormSubmit').writeAttribute({disabled: false});

                // Hook up the cancel buttons
                $('registrationFormCancel').observe('click', function() {
                    Modalbox.hide();
                });
                $('loginFormCancel').observe('click', function() {
                    Modalbox.hide();
                });
                $('messageCancelButton').observe('click', function() {
                    Modalbox.hide();
                });

                $('loginForm').observe('submit', function(event) {
                    event.stop();
                    registerOrLoginViaPopup('login');
                });

                // Submit registration form when someone clicks on the Register <label>, per Keith
                $('registrationLabel').observe('click', function() {
                    $('registrationForm').simulate('submit');
                });

                $('registrationForm').observe('submit', function(event) {
                    event.stop();
                    // Combine the values of the Begin Studies month/year <select> lists
                    // into a properly formatted, single field
                    var beginStudiesDate = $('begin_studies_year').getValue() + '-' + $('begin_studies_month').getValue() + '-' + '01';
                    $('registrationForm').insert('<input type="hidden" id="begin_studies" name="begin_studies" value="' + beginStudiesDate + '"/>');
                    registerOrLoginViaPopup('register');
                });


                $('messageForm').observe('submit', function(event) {
                    event.stop();
                    sendMessage();
                });

                // jQuery DatePicker widget for the birth date
                (function($) {
                    $('#birth_date').datepicker({
                        dateFormat: 'yy-mm-dd',
                        maxDate: '-16Y',
                        minDate: '-65Y',
                        changeMonth: true,
                        changeYear: true
                    });
                })(jQuery);
            }
        });
    });
});

function displayMessageForm(message) {
    $('loginSection').hide();
    $('registrationSection').hide();
    $('loginMessage').update(message); // Update the status notice area
    //
    // Populate the preferred method of contact field
    var contactPrefs = {};
    new Ajax.Request(relative_base_path + 'ajax/getmessageformdata.php', {
        onSuccess: function( xhr ) {
            // Copy the response details (contactMethod & phone)
            Object.extend(contactPrefs, xhr.responseJSON);

            if( contactPrefs.contactMethod == 'email' ) {
                // We only need to populdate the phone field, b/c the user's
                // email address is also his username
                $('contact_via_email').writeAttribute({checked: 'checked'});
            } else if( contactPrefs.contactMethod == 'phone' ) {
                $('contact_via_phone').writeAttribute({checked: 'checked'});

                if( ! contactPrefs.phone.empty() ) {
                    Form.Element.setValue('new_phone_number', contactPrefs.phone);
                }
            }
        }
    });

    $('messageSection').show();
    displayUserInfoArea( $F('username') );
    Modalbox.resizeToContent();
}
