//constructor
//========================================================
function pDAFUIFileUploadClass() 
//========================================================
{

}

//========================================================
pDAFUIFileUploadClass.prototype = 
//========================================================
{
  //------------------------------------------------------
  startUpload : function (sServletPath, sFileUploadID)
  //------------------------------------------------------
  {
    phloc_server_log(null, "start upload (ID:" + sFileUploadID + ", path:" + sServletPath + ")", "info", null);
    var aUploadFrame = jphloc.ge(sFileUploadID + "_frame");
    if (aUploadFrame)
    {
      var aHeads = jphloc.getChildrenByTagName(aUploadFrame.contentDocument, "head", true);
      if (aHeads)
      {        
        var aMeta = jphloc.ce("meta");
        aMeta.setAttribute("http-equiv", "Content-Type");
        aMeta.setAttribute("content", "text/xml; charset=UTF-8");
        aHeads[0].appendChild(aMeta);
      }
    }
    jphloc.hide(sFileUploadID + "_form");
    jphloc.hide(sFileUploadID + "_frame");
    jphloc.show(sFileUploadID + "_progress");
    this.getUploadState(sServletPath, sFileUploadID);
  },
  
  //------------------------------------------------------
  getUploadState : function (sServletPath, sFileUploadID)
  //------------------------------------------------------  
  {
    phloc_server_log(null, "getUploadState (ID:" + sFileUploadID + ", path:" + sServletPath + ")", "info", null);

    if (window.XMLHttpRequest) // Non-IE browsers 
    { 
      req = new XMLHttpRequest();
      req.open("GET", sServletPath, true);
      req.onreadystatechange = function() 
      {
        if (req.readyState == 4) 
        {
          pDAFUIFileUpload.updateUploadState(sServletPath, sFileUploadID);
        }
      }
      req.send(null);
    }  
    else if (window.ActiveXObject) // IE Browsers
    {
      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req) 
      {
        req.onReadyStateChange = function (){pDAFUIFileUpload.updateUploadState(sServletPath, sFileUploadID)};
        req.open("GET", sServletPath, true);
        req.send();
      }
    }
  },

  /**
   * State Description 
   * <ul>
   *   <li>0: The request is not initialized</li>
   *   <li>1: The request has been set up</li>
   *   <li>2: The request has been sent</li>
   *   <li>3: The request is in process</li>
   *   <li>4: The request is complete</li>
   * </ul>
   */
  //------------------------------------------------------
  updateUploadState : function (sServletPath, sFileUploadID)
  //------------------------------------------------------  
  {
    phloc_server_log(null, "updateUploadState (ID:" + sFileUploadID + ", path:" + sServletPath + ")", "info", null);

    if (req.status == 200) // OK response
    {
      var xml = req.responseXML;

      // get data from response XML
      var aFinished = xml.getElementsByTagName("finished")[0];
      phloc_server_log(null, "aFinished:" + aFinished, "info", null);
      var aBytesRead = xml.getElementsByTagName("bytes_read")[0];
      phloc_server_log(null, "aBytesRead:" + aBytesRead, "info", null);
      var aContentLength = xml.getElementsByTagName("content_length")[0];
      var aPercent = xml.getElementsByTagName("percent_complete")[0];
      var aFailure = xml.getElementsByTagName("failure")[0];
      phloc_server_log(null, "aFailure:" + aFailure, "info", null);
      var aSuccess = xml.getElementsByTagName("success")[0];
      phloc_server_log(null, "aSuccess:" + aSuccess, "info", null);
      var aAborted = xml.getElementsByTagName("aborted")[0];
      phloc_server_log(null, "aAborted:" + aAborted, "info", null);
      var aAbortRedirect = aAborted && aAborted.firstChild ? aAborted.firstChild.data : "";
      var aDescription = xml.getElementsByTagName("description")[0];
      var sDescription = aDescription && aDescription.firstChild ? aDescription.firstChild.data : ""; 
      var sProgressBarID = sFileUploadID + "_progress";
      pDAFUIProgressBar.setDescription(sProgressBarID, sDescription);
      if (aAborted)
      {
        jphloc.href(aAbortRedirect);          
        return false;
      }      
      // Check to see if it's even started yet
      if ((aFinished == null) && (aPercent == null)) 
      {
        // Sleep then call the function again
        window.setTimeout(function (){pDAFUIFileUpload.getUploadState(sServletPath, sFileUploadID);}, 500);
      } 
      else 
      {
        var sBytesRead = aBytesRead.firstChild.data;
        var sContentLength = aContentLength.firstChild.data;

        if (aFailure)
        {
          var sError = aFailure.firstChild.data;
          $("#" + sProgressBarID).fadeOut(500, pDAFUIFileUpload.showError(sFileUploadID, sError));
        }
        else if (aSuccess)
        {
          pDAFUIProgressBar.setPercentage(sProgressBarID, 100);          
          var aProgressBar = jphloc.ge(sProgressBarID);
          var sSuccess = aSuccess.firstChild.data;
          $("#" + sProgressBarID).fadeOut(500, pDAFUIFileUpload.showSuccess(sFileUploadID, sSuccess));
        }
        else // It's started, get the status of the upload
        {
          aPercent = aPercent.firstChild.data;
          pDAFUIProgressBar.setPercentage(sProgressBarID, aPercent);
          // Sleep then call the function again
          window.setTimeout(function (){pDAFUIFileUpload.getUploadState(sServletPath, sFileUploadID);}, 500);
        } 
      }
    } 
    else 
    {
      phloc_server_error("pDAFUIFileUpload.updateUploadState() encountered error:" + req.statusText);
    }
  },
  
  //------------------------------------------------------
  debug : function (sMsg, aValue)
  //------------------------------------------------------
  {
    phloc_server_log(null, "aAborted:" + aAborted, "info", null);
  },
  
  //------------------------------------------------------
  showSuccess : function (sFileUploadID, sMsg)
  //------------------------------------------------------
  {
    var aSuccessMsg = jphloc.ge(sFileUploadID + "_successMsg");
    aSuccessMsg.innerHTML = jphloc.nlToBR(sMsg);
    $("#" + sFileUploadID + "_successBox").delay(500).fadeIn(500);
  },

  //------------------------------------------------------
  showError : function (sFileUploadID, sMsg)
  //------------------------------------------------------
  {
    var aErrorMsg = jphloc.ge(sFileUploadID + "_errorMsg");
    aErrorMsg.innerHTML = jphloc.nlToBR(sMsg);
    $("#" + sFileUploadID + "_errorBox").delay(500).fadeIn(500);
  }
}

var pDAFUIFileUpload = window.pDAFUIFileUpload = new pDAFUIFileUploadClass();
