// copyright 2001-2008 DTLink, L.L.C. all rights reserved.
// http://www.dtlink.com
//
// handle an inline ajax form submit.
//
// parms:
//
// formName - name of the form.

// var ddt = new DDT();
// ddt._ddtOn();

if ( typeof( fv_ajaxSubmit_onResponse ) == 'undefined' )
	{

// execute submit

function fv_ajaxSubmit( parms )
{

var formName = parms.formName;

// store the element in the options so we can get to it in the callback.
// FIXME: this has to be done cleaner.

var elem = parms.elem;
var options = parms.options;

$( 'form[name=' + formName + ']' ).data( "ajaxSubmitOptions", options );

// the following is for the benefit of the Xinha editor. MSIE doesn't seem 
// to have an onsubmit() method.

if ( typeof( $( 'form[name=' + formName + ']' ).get()[0].onsubmit ) == 'function' )
	$( 'form[name=' + formName + ']' ).get()[0].onsubmit();

$( 'form[name=' + formName + ']' ).ajaxSubmit(options);

// return false to suspend the chain. The callback will optionally
// continue chain processing.

return false;

}

// ---------------------------------------------
// before submit. Just for debugging.

function fv_ajaxSubmit_beforeSubmit( formData, jqForm, options ) 
{

// alert( "BEFORE SUBMIT" );

return true;

}

// ---------------------------------------------
// callback on successful submit.
//
// This is expected to be a JSON reply.

function fv_ajaxSubmit_onResponse( JSONresponse, statusText )
{

// ddt._ddt( "fv_ajaxSubmit", "43", "in fv_ajaxSubmit_onResponse(): dialog is '" + this.formName + "'" );
// for ( i in this ) ddt._ddt( "fv_ajaxSubmit", "43", "in fv_ajaxSubmit_onResponse(): form is '" + i + "'" );
	
// "this" seems to refer to the jquery.form object.

var options = $( 'form[name=' + this.formName + ']' ).data( "ajaxSubmitOptions" );

var elem = options.elem;

if ( JSONresponse.error != '' )
	{
	alert( JSONresponse.error );
	return false;
	}

// pass the JSON response on to other callbacks in the chain

$( elem ).data( "_fv_payload", JSONresponse );

// check the return code from JSON.

return fv_continueJSChain( elem );

} // end of fv_ajaxSubmit_onResponse

// ---------------------------------------------
// ajax callback on error.

function fv_ajaxSubmit_onError( XMLHttpRequest, textStatus, errorThrown )
{

// for ( var i in XMLHttpRequest ) ddt._ddt( "fv_ajaxSubmit.js", "79", "fv_ajaxSubmit_onError(): xmlobject " + i );
// ddt._ddt( "fv_ajaxSubmit.js", "79", "fv_ajaxSubmit_onError(): Responnse is '" + XMLHttpRequest.responseText );

alert( "ERROR - " + textStatus );

return false;

}

	}	// end of if fv_ajaxSubmit() was not defined.
