/* ----------------------------------------------------------------------------  
 *  Message Box widget
 *  (c) 2007 Third Light Ltd
 * ----------------------------------------------------------------------------
 */

/* ----------------------------------------------------------------------------
 * MessageBox class. 
 * The instance of this class is bound to the form identified by strMsgBoxForm
 * and turns that form into a widget where its visibility can be toggled and
 * it is updated asynchronously.
 *
 * strMsgBoxForm - the id of a form holding all the input elements
 * strMsgBoxInsert - the id of an element that when clicked will show the form
 * strMsgBoxSubmit - the id of an element to submit the form
 * strMsgBoxCancel - the id of an element to cancel the form
 * strMsgBoxContainer - the id of an element holding the history notes
 * strMsgBoxError - the id of an element that can take error messages
 */
 
var MessageBox = Class.create({
    initialize: function(strMsgBoxForm, strMsgBoxInsert, strMsgBoxSubmit, strMsgBoxCancel, strMsgBoxContainer, strMsgBoxError) {
        this.strMsgBoxForm      = strMsgBoxForm;
        this.strMsgBoxInsert    = strMsgBoxInsert;  
        this.strMsgBoxSubmit    = strMsgBoxSubmit;
        this.strMsgBoxCancel    = strMsgBoxCancel; 
        this.strMsgBoxContainer = strMsgBoxContainer;
        this.strMsgBoxError     = strMsgBoxError;
        this.nEffectDuration    = 0.2;
       	this.AttachEventHandlers();
    },
    ShowForm: function() {    	
    	$(this.strMsgBoxInsert).hide();
    	$(this.strMsgBoxForm).enable();
    	new Effect.BlindDown(this.strMsgBoxForm, {duration: this.nEffectDuration}); 
    },
    HideForm: function() {
    	strInsertID  = this.strMsgBoxInsert;
    	fnShowInsert = this.ShowInsert;
    	new Effect.BlindUp(
    		this.strMsgBoxForm, {
    			duration: this.nEffectDuration, 
    			afterFinish: function() {fnShowInsert(strInsertID)}
    		}
    	);    	
    },
    ShowInsert: function(strInsertID) {   	
    	$(strInsertID).show();
    },
    AttachEventHandlers: function() {
    	Event.observe (this.strMsgBoxSubmit, "click", this.SubmitForm.bindAsEventListener(this));
    	Event.observe (this.strMsgBoxCancel, "click", this.CancelForm.bindAsEventListener(this));
    	Event.observe (this.strMsgBoxInsert, "click", this.ShowForm.bindAsEventListener(this));
    },
    SubmitForm: function() {        	
    	var elMsgBoxForm      = $(this.strMsgBoxForm);
    	var elMsgBoxContainer = $(this.strMsgBoxContainer);
    	var strParameters     = elMsgBoxForm.serialize()
    	var strAction         = elMsgBoxForm.action;
    	var myself            = this;
    	
    	$(this.strMsgBoxError).innerHTML = "";
    	elMsgBoxForm.disable();
	
    	new Ajax.Request (
    	    strAction, {
 		        onSuccess: function(transport) {myself.DoSuccess(transport);}, 
    			onFailure: function(transport) {myself.DoFailure(transport);},
    			parameters:  strParameters
    		}
    	);    		
    },
    CancelForm: function() {
    	$(this.strMsgBoxForm).reset();
    	$(this.strMsgBoxError).innerHTML = "";
    	this.HideForm();
    },
    DoSuccess: function(transport) {      
        var objResponse = eval('(' + transport.responseText + ')');
        var bError  = objResponse.error;
        var strHTML = objResponse.html;        
        if (bError == true) {
            $(this.strMsgBoxError).insert({top: strHTML});
            $(this.strMsgBoxForm).enable();
        }
        else {
            if ($('nohistorynotes')) {
                $('nohistorynotes').innerHTML = "";
            }
            $(this.strMsgBoxContainer).insert({top: strHTML});
            this.CancelForm();
        }
    },
    DoFailure: function(transport) {
        $(this.strMsgBoxError).innerHTML = "<strong>Failed to connect to your IMS. Please try again.</strong>";
        $(this.strMsgBoxForm).enable();
    }
});

function MessageBoxWidgetSetup(strMsgBoxForm, strMsgBoxInsert, strMsgBoxSubmit, strMsgBoxCancel, strMsgBoxContainer, strMsgBoxError) {
	$(strMsgBoxForm).msgBox = new MessageBox(strMsgBoxForm, strMsgBoxInsert, strMsgBoxSubmit, strMsgBoxCancel, strMsgBoxContainer, strMsgBoxError);
}

