////////////////////////////////////////////////////////////////////////
//
// Filename:    formcontentHTML.js
// Purpose:     Create a form to edit HTML content
// Method:      JavaScript class
//
// Author:      J.van.der.Steen@gobase.org
// Date:        2010-03-07
//
// Note:        Requires prototype.js
//
////////////////////////////////////////////////////////////////////////

var FormContentHTML = Class.create( FormBase,
{

    initialize: function($super, values)
    {
        this.valAssign(values, "type", "contentHTML");

        $super(values);

        /*
         * Editable properties
         */
        this.propAdd( values
                    , "HTMLcode"
                    , { type     : 'textarea'
                      , rows     : 10
                      , cols     : 64
                      , key      : 'html code'
                      , val      : ''
                      , valid    : 1
                      , validator: null
                      }
                    ) ;

        /*
         * System properties
         */
        this.propAdd( values
                    , "functionaris"
                    , { type     : 'hidden'
                      , size     : 64
                      , key      : 'functionaris'
                      , val      : config.user.username
                      , valid    : 1
                      , validator: 'validateNonempty'
                      }
                    ) ;
        this.propAdd( values
                    , "filename"
                    , { type     : 'hidden'
                      , size     : 64
                      , key      : 'filename'
                      , val      : ''
                      , valid    : 1
                      , validator: 'validateNonempty'
                      }
                    ) ;
        this.propAdd( values
                    , "item"
                    , { type     : 'hidden'
                      , size     : 64
                      , key      : 'item'
                      , val      : ''
                      , valid    : 1
                      , validator: 'validateNonempty'
                      }
                    ) ;
        this.propAdd( values
                    , "op"
                    , { type     : 'hidden'
                      , size     : 64
                      , key      : 'op'
                      , val      : 'load'
                      , valid    : 1
                      , validator: 'validateNonempty'
                      }
                    ) ;

        /*
         * Buttons
         */
        this.propAdd( values
                    , "versturen"
                    , { type     : 'button'
                      , size     : 32
                      , key      : 'versturen'
                      , val      : 'versturen'
                      , valid    : 1
                      , validator: 'validateNonempty'
                      , onclick  : 'save'
                      }
                    ) ;

        /**
         * Title of this form
         */
        this.setTitle('');

    }

    /**
     * Load the content
     */
,   load: function()
    {
        var parameters = { type: this.type
                         , guid: this.guid
                         , form: {}
                         , user: config.user
                         } ;

        debug('');

        this.valid = this.validateForm(parameters);

        parameters.form = Object.toJSON(parameters.form);
        parameters.user = Object.toJSON(parameters.user);

        if (this.valid) {
            //
            // Submit
            //
            var request = new Ajax.Request( this.submit
                                          , { method: 'post'
                                            , onFailure: this.opFailed
                                            , onSuccess: this.opSuccess
                                            , parameters: parameters
                                            }
                                          ) ;
        }

    }

,   opFailed: function(transport)
    {
        var person = config.board.webmaster;

        debug( "Loading of '" + this.filename + "' failed,"
             + " please contact: <a href=mailto:'" + person.email + "'>" + person.name + "</a>"
             ) ;
    }

,   opSuccess: function($super, transport)
    {
        var response = $super(transport);

        if (response === null) {
            return;
        }
        switch (response.op) {
            case 'ack':
                response.theform.propSetVal('HTMLcode', response.HTMLcode);

                Element.update('content', response.theform.edit());
                response.theform.propSetVal('op', 'save');
                break;
            case 'login':
                debug("You are not authorized");
                break;
            default:
                debug("Unexpected reponse.op='" + response.op + "'");
                break;
        }
    }

} ) ;

function emitFormContentHTML(guid, filename)
{
    var form = new FormContentHTML({});
    var item = config.lookupTable.get(guid);

    form.setTitle  (basename(filename));
    form.propSetVal('filename', filename);
    form.propSetVal('item'    , guid);
    form.load();
    // debug("Editing '" + filename + "' is not yet implemented");
}


