/* Licence:
*   Use this however/wherever you like, just don't blame me if it breaks anything.
*
* Credit:
*   If you're nice, you'll leave this bit:
*
*   Class by Pierre-Alexandre Losson -- http://www.telio.be/blog
*   email : plosson@users.sourceforge.net
*/

var title0 = "upload in progress: ";
var title1 = "of";
var conversion = "MB";
var dwrErrorIndicator = false;
var uploadDone = false;

function refreshProgress()
{
  if(!uploadDone)
  {
    uploadMonitor.getUploadInfo(updateProgress);  
  }  
}

function updateProgress(uploadInfo)
{
  if(!uploadDone)
  {
    dwrErrorIndicator = false;

    if(uploadInfo.inProgress)
    {
        //var fileIndex = uploadInfo.fileIndex;
        var bytes_read = uploadInfo.bytesRead;
        var total_size = uploadInfo.totalSize;
        var progressPercent = 0;
        var bytes_read_convert = 0.0;
        var total_size_convert = 0.0;
        var str_bytes_read_convert = "";
        var str_total_size_convert = "";


        if(total_size > 0)
        {
          progressPercent = Math.ceil((bytes_read / total_size) * 100);
        }
        
       // document.getElementById('progressBarText').innerHTML = title0 + progressPercent + '%';

        document.getElementById('progressBarBoxContent').style.width = parseInt(progressPercent * 2) + 'px';

        if(total_size > 0)
        {
          if(total_size > 102400)
          {   
            conversion = "MB";
 
            total_size_convert = Math.ceil((total_size / 1048567) * 10);

            if(total_size_convert > 0)
            {
              total_size_convert = (total_size_convert /10);

              if(bytes_read > 0)
              {
                bytes_read_convert = Math.ceil((bytes_read / 1048567) * 10);

                if(bytes_read_convert > 0)
                {
                  bytes_read_convert = (bytes_read_convert /10);
                }
              }
            }
          }
          else
          {
            conversion = "KB";

            total_size_convert = Math.ceil((total_size / 102400) * 10);

            if(total_size_convert > 0)
            {
              total_size_convert = (total_size_convert /10);

              if(bytes_read > 0)
              {
                bytes_read_convert = Math.ceil((bytes_read / 102400) * 10);

                if(bytes_read_convert > 0)
                {
                  bytes_read_convert = (bytes_read_convert /10);
                }
              }
            }
          }
        }

        if(total_size_convert > 0)
        {
          str_bytes_read_convert = new String(bytes_read_convert);

          if(str_bytes_read_convert.indexOf('.') < 0) { str_bytes_read_convert += '.0'; }

          str_total_size_convert = new String(total_size_convert);

          if(str_total_size_convert.indexOf('.') < 0) { str_total_size_convert += '.0'; }

          document.getElementById('progressBarText2').innerHTML = str_bytes_read_convert + conversion  + ' ' + title1 + ' ' + str_total_size_convert + conversion;             
        }
        else
        {
          document.getElementById('progressBarText2').innerHTML = '...' + conversion  + ' ' + title1 + ' ...' + conversion;
        } 

        window.setTimeout('refreshProgress()', 1000);
    }
    else
    {    
      if(uploadInfo.done)
      {
        /*
        uploadDone = true;
        document.getElementById('progressBarText').innerHTML = '&nbsp;';
        document.getElementById('progressBarText2').innerHTML = '&nbsp;'; 
        document.getElementById('progressBarBoxContent').style.width = '200px';
        */
      } 
      
      if(uploadInfo.uploadInterrupted)
      {
        /*
        uploadDone = true;
        document.getElementById('progressBarText').innerHTML = '&nbsp;';
        document.getElementById('progressBarText2').innerHTML = '&nbsp;';
        document.getElementById('progressBarBoxContent').style.width = '0px';
        */
      } 
    }
  }
  
  return true;
}

function startProgress()
{
    uploadDone = false;
 
    if(lang == "NL")
    {
      title0 = "verzenden... ";
      title1 = "van";
    }
    if(lang == "FR")
    {
      title0 = "envoyer...  ";
      title1 = "de";
    }

    document.getElementById('progressBarText').innerHTML = title0;//+'0%'
    document.getElementById('progressBarText2').innerHTML = '...MB '+ title1 + ' ...MB';
    document.getElementById('progressBarBoxContent').style.width = '0px';

    // wait a little while to make sure the upload has started ..
    window.setTimeout("refreshProgress()", 2000);
    return true;
}

