function getArgsString( aArgs )
{
  var sRet = '';

  for( var i = 1 ; i <= aArgs.length ; i++ )
  {
    sRet += '&arg' + i + '=' + (aArgs[ i-1 ] != null ? aArgs[ i-1 ] : '');
  }

  sRet += '&num_args=' + aArgs.length;

  return sRet;
}

function showExportPopup(sURL)
{
  popup( sURL, 'exportPDF', 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportProducts( sURL, nRMethodID, sFormName )
{
  var aArgs = new Array();

  aArgs[0] = ( nRMethodID == null ) ? '' : nRMethodID;
  aArgs[1] = '';

  for( var i = 0 ; i < getFormObject( 'export_etab', sFormName ).options.length ; i++ )
  {
    if( getFormObject( 'export_etab', sFormName ).options[ i ].selected == true )
    {
      aArgs[1] += ( ( aArgs[1] == '' ) ? '' : ',' ) + getFormObject( 'export_etab', sFormName ).options[ i ].value ;
    }
  }
  aArgs.push( getInputValue( 'export_campaign_code', sFormName ) );

  popup( sURL + getArgsString( aArgs ), 'exportProducts' + nRMethodID, 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportConventionProducts( sURL, nRMethodID, sFormName )
{
  var aArgs = new Array();

  aArgs[0] = ( nRMethodID == null ) ? '' : nRMethodID;
  aArgs[1] = '';

  for( var i = 0 ; i < getFormObject( 'export_etab', sFormName ).options.length ; i++ )
  {
    if( getFormObject( 'export_etab', sFormName ).options[ i ].selected == true )
    {
      aArgs[1] += ( ( aArgs[1] == '' ) ? '' : ',' ) + getFormObject( 'export_etab', sFormName ).options[ i ].value ;
    }
  }

  popup( sURL + getArgsString( aArgs ), 'exportConventionProducts' + nRMethodID, 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportProductsTrace( sURL, nProductID, sFormName )
{
  var aArgs = new Array();

  aArgs[0] = '';
  aArgs[1] = '';
  aArgs.push( getInputValue( 'export_campaign_code', sFormName ) );
  aArgs.push( nProductID );

  popup( sURL + getArgsString( aArgs ), 'exportProducts' + nProductID, 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportPlanning( sURL, sFormName )
{
  popup( sURL, 'exportPlanning', 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportPlanningMergedCells( sURL, sFormName )
{
  var bExportDate = getFormObject( 'export_current_date_pattern', sFormName ).checked ? 1 : 0;
  popup( sURL+'&export_date='+bExportDate, 'exportPlanning', 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportGlobalPlanning( sURL, sFormName )
{
  var aArgs = new Array();

  aArgs.push( getInputValue( 'campaign_code', sFormName ) );
  aArgs.push( getInputValue( 'quota_code', sFormName ) );

  popup( sURL + getArgsString( aArgs ), 'exportAllPlanning', 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportPDF( sURL, sListparamClass, sFormName )
{
  var aArgs = new Array();

  aArgs[0] = sListparamClass;

  popup( sURL + getArgsString( aArgs ), 'exportPDF', 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportConventionPDF( sURL, nConventionID, sFormName )
{
  var aArgs = new Array();

  aArgs[0] = nConventionID;

  popup( sURL + getArgsString( aArgs ), 'exportPDF', 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportReservationPDF( sURL, nDocWorkflowID, sFormName, bHasBeenSent, nType, nLanguageID, bTestTemplate, bTestModeDisplay, bCache )
{
  var aArgs = new Array();

  aArgs[0] = nDocWorkflowID;
  aArgs[1] = bHasBeenSent; // le paramètre $p_bForceBeenSent, pour visualiser le document comme si il avait été envoyé
  aArgs[2] = nType ? nType : sFormName ? getInputValue( "type", sFormName ) : null;
  aArgs[3] = nLanguageID;
  aArgs[4] = bTestTemplate;
  aArgs[5] = bTestModeDisplay;
  aArgs[6] = '';
  aArgs[7] = bCache;
  
  // on ouvre a chaque fois dans une nouvelle fenêtre afin de pouvoir visualiser plusieurs documents en même temps
  popup( sURL + getArgsString( aArgs ), '_blank', 'menubar=no,scrollbars=yes,resizable=yes,status=no,toolbar=yes,titlebar=no,location=no,width=800,height=600' );
}

function exportQuota( sURL, sFormName )
{
  popup( sURL, 'exportQuota', 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportAlo( sURL, sFormName )
{
  popup( sURL, 'exportAlo', 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportListparam( sURL, sListparamClass, sFormName, sType, bGrouped )
{
  var aArgs = new Array();
 
  aArgs[0] = sListparamClass;
  aArgs[1] = sType;
  if (bGrouped != null)
    aArgs[2] = bGrouped;

  popup( sURL + getArgsString( aArgs ), 'exportListparam', 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportListparamFetchParams( sURL, sListparamClass, sFormName, bGrouped )
{
  var nExportFormat = getInputValue( 'export_format', sFormName );
  var nExportOffset = getInputValue( 'export_offset', sFormName );
  var nExportLimit  = getInputValue( 'export_limit', sFormName );

  var sType = nExportFormat;
  
  var sFormat = "";
  var sFileName = "";
  if (nExportFormat == 0)
  {
    sFormat = "text/html";
    sFileName = "export.htm";
  }
  if (nExportFormat == 1)
  {
    sFormat = "application/vnd.ms-excel";
    sFileName = "export.xls";
  }
  if (nExportFormat == 2)
  {
    sFormat = "text/csv";
    sFileName = "export.csv";
  }
  if (nExportFormat == 3)
  {
    sFormat = "application/pdf";
    sFileName = "export.pdf";
  }
  if (nExportFormat == 4)
  {
    sFormat = "text/xml";
    sFileName = "export.pdf";
  }
  
  // dans le doute...
  sURL = sURL.replace("&mime_type=", "");
  sURL = sURL.replace("&attachment_name=", "");
  
  sURL += "&mime_type=" + sFormat;
  sURL += "&attachment_name=" + sFileName;

  if (nExportOffset != '')
    sURL += "&export_offset=" + nExportOffset;
  if (nExportLimit != '')
    sURL += "&export_limit=" + nExportLimit;

  exportListparam(sURL, sListparamClass, sFormName, sType, bGrouped );
}

function exportActivity( sURL )
{
  popup( sURL, 'exportActivity', 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportAccountingFetchParams( sURL, sFormName )
{
  var sStartDate = getInputValue( 'start_date', sFormName );
  var sEndDate   = getInputValue( 'end_date', sFormName );
  var nEtabNum   = getInputValue( 'etab_num', sFormName );

  sURL = sURL + '&start_date=' + sStartDate + '&end_date=' + sEndDate + '&etab_num=' + nEtabNum;

  popup( sURL, 'exportAccounting', 'menubar=yes,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportDBFile( sURL, nDBFileID, bUseAttachmentName )
{
  var aArgs = new Array();
  
  aArgs[0] = nDBFileID;
  aArgs[1] = bUseAttachmentName ? 1 : 0;
  
  //on ouvre a chaque fois dans une nouvelle fenêtre afin de pouvoir visualiser plusieurs documents en même temps
  popup( sURL + getArgsString( aArgs ), '_blank', 'menubar=no,scrollbars=yes,resizable=yes,status=no,toolbar=yes,titlebar=no,location=no,width=800,height=600' );
}

function exportReservationPDFOrDBFile( sReservationURL, nDocWorkflowID, sFormName, bHasBeenSent, nType, nLanguageID, bTestTemplate, bTestModeDisplay, sDBFileURL, nDBFileID, bFileExists )
{ 
  if( nDBFileID )
  {
    if( bFileExists == '0' || bFileExists == '' )
    {
      if( ! confirmUserMessage( 'LANG_file_does_not_exist_anymore' ) )
      {
        return;
      }
      else
      {
        //le fichier n'existe plus sur le HD l'utilisateur veut cependant le générer via le model actuel :
        exportReservationPDF( sReservationURL, nDocWorkflowID, sFormName, bHasBeenSent, nType, nLanguageID, bTestTemplate, bTestModeDisplay );
      }
    }
    else
    {
      //le fichier existe et il est sur le HD
      exportDBFile( sDBFileURL, nDBFileID, 0); 
    }
  }
  else
  {
    exportReservationPDF( sReservationURL, nDocWorkflowID, sFormName, bHasBeenSent, nType, nLanguageID, bTestTemplate, bTestModeDisplay );
  }
}

function exportBordereau( sURL )
{
  popup( sURL, '_blank', 'menubar=no,scrollbars=yes,resizable=yes,status=no,toolbar=yes,titlebar=no,location=no,width=$p_nWidth,height=$p_nHeight,screenX=0,screenY=0,left=0,top=0' );
}

function exportDocWorkflowFromID( nDocWorkflowID, nLanguageID, sFormName, sURL, bCache )
{
  try
  {
    // flag permettant de savoir dans le Perl que l'on a cliqué sur ce docWorkflow en particulier
    sPreviewKey = remoteCall( "custom::remote::reservation::docworkflow",
                               'generatePDFFile',
                               null,
                               nLanguageID,
                               bCache,
                               nDocWorkflowID
                            );

    // on retire le flag sur ce docWorkflow
    wPopup = window.open( sURL+"&arg1="+sPreviewKey+"&num_args=1",
                          'exportDialog',
                          'directories=no,fullscreen=no,location=no,menubar=no,status=no,toolbar=no,width=800,height=600,alwaysRaised=1' );
  }
  catch(ex)
  {
    if(ex instanceof CheckerException)
    {
      ex.showUser();
    }
    else
    {
      throw ex;
    }
  }
}
