/**********************************************
 **
 ** Ajax Mini Form Object
 **   (depends on jquery)
 **
 **********************************************/

/* BEGIN: MiniForm */

function MiniForm( container, options )
{
    var tag = container[0].tagName.toUpperCase();

    this.container = container;
    this.form      = tag == "FORM" ? container : $( 'form', container );
    this.options   = options || {};

    if( typeof this.options.hideForm == "undefined" )
	this.options.hideForm = 'slide';
    
    if( typeof this.options.resetOnSave == "undefined" )
	this.options.resetOnSave = true; 

    if( typeof this.options.pleaseWait == "undefined" )
	this.options.pleaseWait = false; 

    if( typeof this.options.alertResponse == "undefined" )
	this.options.alertResponse = false; 

    if( typeof this.options.logResponse == "undefined" )
	this.options.logResponse = false; 

    if( $( '.submit', container ) )
	$( '.submit', container ).click( 
	    this.bind( 'submitMiniForm', this )
	);

    if( $( '.cancel', container ) )
	$( '.cancel', container ).click(
	    this.bind( 'cancelMiniForm', this )
	);

    this.form.ajaxForm( this.getAjaxFormOptions() ); 

    container[0].miniForm = this;
    
    return;
}

MiniForm.prototype = {

    submitMiniForm : function () {
	
	if( this.options.pleaseWait )
	    this.pleaseWait = new PleaseWait( $( '.submit', this.container ) );

	if( this.options.beforeSubmit )
	    this.options.beforeSubmit();
	this.form.submit();
	return false;
    } // submitMiniForm

    ,

    cancelMiniForm : function () {
	if( this.options.hideForm == 'slide' )
	{
	    this.container.slideUp( 'normal', 
				    this.bind( 'resetMiniForm', this ) );
	}
	else
	{
	    this.resetMiniForm();
	}
	return false;
    } // cancelMiniForm

    ,

    resetMiniForm : function () {
	this.clearErrors();
	this.clearInput();
	return false;
    } // resetMiniForm
    
    ,

    getAjaxFormOptions : function () {
	var afOpts = {
	    beforeSubmit : this.bind( 'clearErrors', this ),
	    success      : this.bind( 'onSubmitSuccess', this ),
            error        : this.bind( 'onSubmitFailure', this ),
	    dataType     : 'xml'
	};

	if( this.options.alertResponse ) {
	    afOpts.complete = function (resp) {
		alert( resp.responseText );
		return false;
	    }
	}
	else if( this.options.logResponse ) {
	    afOpts.complete = function (resp) {
		console.log( 'MiniForm Ajax Response: ' + resp.responseText );
		return false;
	    }
	}

	return afOpts;
    } // getAjaxFormOptions
    
    ,

    clearErrors : function () {
	$( '.error', this.container ).html( '' ).hide();
    } // clearErrors

    ,

    clearInput : function () {
	$( "input[type!='hidden'][type!='submit'][type!='button']", this.container ).val( [] );
	$( 'select', this.container ).val( '' );
	$( 'textarea', this.container ).val( '' );
    } // clearInput

    ,

    onSubmitSuccess : function (xml) {

	xmlRoot     = xml.documentElement;
	var success = xmlRoot.getAttribute( 'code' );

	if( this.options.pleaseWait && this.pleaseWait ) {
	    this.pleaseWait.stop();
	    this.pleaseWait = null;
	}
    
	if( success == 1 ) {
	    var afterSlideUp = function () { this.resetMiniForm() };

	    if( this.options.success ) {
		afterSlideUp = function () {
		    if( this.options.resetOnSave )
			this.resetMiniForm();

		    this.options.success( xml );
		}
	    }
	    
	    if( this.options.hideForm == 'slide' )
	    {
		this.container.slideUp( 'normal', 
					this.bind( afterSlideUp, this ) );
	    }
	    else
	    {
		afterSlideUp.call( this );
	    }
	}
	else {
	    this.showErrors( xmlRoot );
	    if( this.options.error ) {
		this.options.error(xml);
	    }
	}
    } // onSubmitSuccess
    
    ,

    onSubmitFailure : function (xml) {
	if( this.options.pleaseWait && this.pleaseWait ) {
	    this.pleaseWait.stop();
	    this.pleaseWait = null;
	}
	if( this.options.failure ) {
	    this.options.failure(xml);
	}
    } // onSubmitFailure

    ,

    showErrors : function (xml) {
	var errors = xml.getElementsByTagName( 'error' );
	for( var idx = 0; idx < errors.length; idx++ ) {
	    var err = errors[idx];
	    
	    var id  = err.getAttribute( 'key' );
	    var msg = err.getAttribute( 'msg' );
	    
	    $( '#' + id + '-error', this.container ).html( msg ).show();
	}
    } // showErrors

    ,

    bind : function (funcName, obj) {
	var method 
	    = typeof funcName == "function" ? funcName
	    :                                 obj[funcName]
            ;
    
	return function() {
	    return method.apply( obj, arguments );
	};
    } // bind

} // MiniForm.prototype

/* END: MiniForm */
