//Javascript name: Ajax encopsulator
//Date created: 16-Nov-2003 23:19
//Scripter: Ginzburg L.


/*
 * AjaxFun is a hypothetical object that encapsulates the transaction
 *     request and callback logic.
 *
 * handleSuccess( ) provides success case logic
 * handleFailure( ) provides failure case logic
 * processResult( ) displays the results of the response from both the
 * success and failure handlers
 * call( ) calling this member starts the transaction request.
 */

AjaxFun = {

    elementId : null,
    waitMessageId : null,

    handleSuccess:function(o){
        // This member handles the success response
        // Put Response into element defined by ID
        if ( this.elementId != null ) {
            var divElem = document.getElementById( this.elementId );
            if ( divElem != null ) {
                divElem.innerHTML = o.responseText;
            }
            this.handleLoad();
        }
    },

    handleLoad:function(){
    },

    handleFailure:function(o){
        // Failure handler
        alert('Internal server error. Please try again later.')
    },

    // initial envocation of AJAX request
    startRequest:function( sUrl) {
        if ( this.elementId != null ) {
            var divElem = document.getElementById( this.elementId );
            if ( divElem != null ) {
                divElem.innerHTML = '<div style="margin:20px;">Loading the form... please wait.</div>';
            }
        }
        YAHOO.util.Connect.asyncRequest('GET', sUrl, callback );
    },

    waitMessage:function(show) {
        var divElem = null;
        if ( this.waitMessageId!= null ) {
            divElem = document.getElementById(this.waitMessageId);
            if ( divElem != null ) {
                divElem.style.visibility = show ? "visible" : "hidden";
            }
        }
    },

    // intercation with server - send POST Ajax Request
    interactionRequest: function( fromElemId, sUrl) {
        // argument formId can be the id or name attribute value of the
        // HTML form, or an HTML form object.
        var formObject = document.getElementById(fromElemId);
        YAHOO.util.Connect.setForm(formObject);

        // show wait message
        this.waitMessage(true);

        // Execute a POST transaction.
        var cObj = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback);
    },

    hideAjaxPane: function() {
        if ( this.elementId != null ) {
            var divElem = document.getElementById( this.elementId );
            if ( divElem != null )
                divElem.innerHTML = '';
        }
    }

};

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callback =
{
    success:AjaxFun.handleSuccess,
    failure:AjaxFun.handleFailure,
    scope: AjaxFun
};

/* to Start the transaction you neeed.
 1) define element that would contain interaction data : AjaxFun.elementId = 'someElementId';
 2) start process by AjaxFun.startRequest();
*/