//###############################################################################
//# Copyright© 2000-2009, Overnite Software Inc.
//###############################################################################
//# Warning: This program is protected by copyright law and international
//# treaties. Unauthorized reproduction or distribution of this program, or any
//# portion of it, may result in severe civil and criminal penalties, and will
//# be prosecuted to the maximum extent possible under law.
//###############################################################################
//# File Transfer Routines
//###############################################################################
//# Revision History
//# init   date   rev modifications
//# ---- -------- --- ----------------------------------------------------------
//# crh  05/23/08 401 #1735 Original
//# kjh  06/20/08 401 #1881 Added logic to catch IOError
//# kpc  01/30/09 401 #2378 Inserted new code for flash 10 uploader.
//###############################################################################
function FileTransfer( sJSName, oSWF )
{
  // javascript variable name
  this.JSName = sJSName;

  if( typeof( this.JSName ) != "string" || this.JSName.length == 0 )
  {
    var dt = new Date();
    this.JSName = "ft_" + dt.getTime();
  }

  // id/name of SWF or Object
  this.SWF = oSWF;

  this.GetApp = function()
  {
    try
    {
      if( typeof( this.SWF ) == "string" )
        return( ( document[this.SWF] ) ? document[this.SWF] : window[this.SWF] );
      else
        return( this.SWF );
    }
    catch( e )
    {
      this.Alert( e );
      return( null );
    }
  }

  this.Alert = function( s )
  {
    if( this.Debug )
      alert( this.JSName + ": " + s.message );
  }

  this.Browse = function( aFilter, bList )
  {
    try
    {
      //this.SetAll();
      //this.GetApp().App_Browse( aFilter, bList );
      alert( "Function not implemented!" );
    }
    catch( e )
    {
      this.Alert( e );
    }
  }

  this.Cancel = function()
  {
    try
    {
      this.SetAll();
      this.GetApp().App_Cancel();
    }
    catch( e )
    {
      this.Alert( e );
    }
  }

  this.Upload = function( sURL, n )
  {
    try
    {
      this.SetAll();
      this.GetApp().App_Upload( sURL, n );
    }
    catch( e )
    {
      this.Alert( e );
    }
  }

  this.Download = function( sURL, sName )
  {
    try
    {
      this.SetAll();
      this.GetApp().App_Download( sURL, sName );
    }
    catch( e )
    {
      this.Alert( e );
    }
  }

  this.GetFile = function( f )
  {
    try
    {
      return( this.GetApp().App_GetFile( f ) );
    }
    catch( e )
    {
      this.Alert( e );
    }
  }

  this.GetFileCount = function()
  {
    try
    {
      return( this.GetApp().App_GetFileCount() );
    }
    catch( e )
    {
      this.Alert( e );
    }
  }

  this.Get = function( p )
  {
    try
    {
      return( this.GetApp().App_Get( p ) );
    }
    catch( e )
    {
      this.Alert( e );
    }
  }

  this.Set = function( p, v )
  {
    try
    {
      this.GetApp().App_Set( p, v );
    }
    catch( e )
    {
      this.Alert( e );
    }
  }

  this.SetAll = function()
  {
    for( p in this )
    {
      if( p.charAt(0) == '_' )
        continue;

      switch( typeof( this[p] ) )
      {
        case 'function':
          if( p.substr( 0, 2 ) == "On" )
            this.Set( p, "window.FileTransfer." + this.JSName + "." + p );
          break;

        case 'boolean':
          this.Set( p, ( this[p] ) ? 1 : 0 );
          break;

        case 'string':
        case 'number':
        case 'object':
        default:
          this.Set( p, this[p] );
          break;
      }
    }
  }

  this.Clear = function()
  {
    try
    {
      this.GetApp().App_Clear();
    }
    catch( e )
    {
      this.Alert( e );
    }
  }

  this.OnError = function( o )
  {
    var msg = o.source + " -- " + o.extra;
    switch( o.source )
    {
      case "oListener.onHTTPError":
        switch( o.extra )
        {
          case 421: msg = "Invalid or missing data"; break;
          case 422: msg = "Database Error"; break;
          case 451: msg = "Must be logged in"; break;
          case 452: msg = "Unable to save file"; break;
          case 453: msg = "Unable to resize image"; break;
          case 454: msg = "Unsupported image type"; break;
          case 460: msg = "Unsupported audio file"; break;
          case 499: // file too large
            msg = ParamString( "#0# is too large (#1#). Maximum size allowed is #2#.",
              o.file.name, Format.Size( o.file.size ), Format.Size( o.file.MaxSize ) );
            break;
        }
        break;
      case "oListener.onIOError":
        msg = "Network Connection error, please try again.";
        break;
      default:
        break;
    }

    if( typeof( this.OnComplete ) == "function" )
      this.OnComplete( o.file, msg );
    else
      alert( msg );
  }

  window.FileTransfer[this.JSName] = this;
}