/************************************************
 * $Revision: 1.6 $
 * Last edited by: $Author: mingles $
 * $Date: 2011/04/29 18:48:20 $
 ************************************************/
/**
 * Function will submit the form via ajax to the sPostURL url.
 *
 * @param oDialogForm - The html form object
 * @param sPostURL - The url to post the form to
 */

if (typeof dynamicDialogAjaxSubmit == 'undefined')
{
  function dynamicDialogAjaxSubmit( oDialogForm, sPostURL, oPostSubmitFunc )
  {
    var oForm = oDialogForm;

    var msgDiv = document.getElementById("QuickAddFormReturnMessage");

    if (msgDiv != null)
    {
      while (msgDiv.hasChildNodes())
      {
        msgDiv.removeChild(msgDiv.firstChild);
      }

      msgDiv.innerHTML = '';
      var spinner = document.createElement("img");
      spinner.setAttribute('src', '/images/loading.gif');
      msgDiv.appendChild(spinner);

      var content = document.createElement("span");
      content.innerHTML = "Saving...";
      msgDiv.appendChild(content);
    }

    var oPostSubmitFunction = oPostSubmitFunc || null;

    $.ajax( {
      url: sPostURL,
      data: $(oForm).serialize(),
      success: function( oData, sStatus )
            {
              var msgDiv = document.getElementById("QuickAddFormReturnMessage");
              if (oData.success == true)
              {
                if(( typeof oData.callback != 'undefined' ) &&
                   ( oData.callback != null ))
                {
                  var oCallback;
                  var aCallbacks;
                  try
                  {
                    if( typeof oData.callback == 'function' )
                    {
                      oCallback = oData.callback;

                      oCallback( oForm, oData );
                    }
                    else if(( typeof oData.callback == 'string' ) && !(/^\s*$/.test(oData.callback)))
                    {
                      var aFuncPath = oData.callback.split('.'),
                          oCallback = window,
                          sTmp;
                      while( sTmp = aFuncPath.shift())
                      {
                        if( typeof oCallback[sTmp] != 'undefined' )
                        {
                          oCallback = oCallback[sTmp];
                        }
                      }

                      if( typeof oCallback == 'function' )
                      {
                        oCallback( oForm, oData );
                      }
                    }
                    else if( oData.callback.length )
                    {
                      for( var i = 0; i < oData.callback.length; i++ )
                      {
                        oCallback = oData.callback[i];

                        if( typeof oCallback == 'function' )
                        {
                          oCallback( oForm, oData );
                        }
                        else if(( typeof oCallback == 'string' ) && !(/^\s*$/.test( oCallback )))
                        {
                          var aFuncPath = oCallback.split('.'),
                              oCallback = window,
                              sTmp;
                          while( sTmp = aFuncPath.shift())
                          {
                            if( typeof oCallback[sTmp] != 'undefined' )
                            {
                              oCallback = oCallback[sTmp];
                            }
                          }

                          if( typeof oCallback == 'function' )
                          {
                            oCallback( oForm, oData );
                          }
                        }
                      }
                    }
                  }
                  catch (ex)
                  {
                    var msg = "An error occured handling the callback requested by the submitted form.<br /><br />" + ex.toSource();
                    if (msgDiv != null)
                    {
                      msgDiv.innerHTML = msg;
                      return;
                    }
                  }
                }
                else if (typeof oData.redirect == 'string' && !(/^\s*$/.test(oData.redirect)))
                {
                  if (oData.redirect == window.location.href)
                  {
                    window.location.reload();
                  }
                  else
                  {
                    window.location.href = oData.redirect;
                  }
                }

                if( typeof oPostSubmitFunction == 'function' )
                {
                  oPostSubmitFunction();
                }

                /*  There could be other dialogs open - we only want to close ours  */
                $( oDialogForm ).parent( 'div[id="jqDynamicDialog"]' ).dialog( 'close' ).empty().remove();
              }
              else
              {
                if (typeof oData.errorMessage == 'string' && !(/^\s*$/.test(oData.errorMessage)))
                {
                  var msg = oData.errorMessage;
                }
                else
                {
                  var msg = "An error occured while submitting the form.<br /><br />" + oData;
                }
                if (msgDiv != null)
                {
                  msgDiv.innerHTML = msg;
                }
              }
            },
            dataType: 'json',
            type: 'POST',
            error: function()
            {
              console.log( arguments );
            }
    } );

    //stops form from submitting normally
    return false;
  }

}

