
   var myInterval;   // a global timer object
   var totalSize = 0;   // total uploaded size sofar
   var counter = 0;

   /*
      This function set up the timer to connect to the
      server every half second
    */

   function connectToServer()
   {
	   
      myInterval = window.setInterval("requestInfo()",100);
   }

   /*
      This function creates an xmlHttpRequest object
      according to the browser used.

      return:  created xmlHttpRequest object
    */

   function createXMLHttpRequest()
   {
      var transfer;
      if (window.ActiveXObject)
      {
         transfer = new ActiveXObject('Msxml2.XMLHTTP');
          }
      else if (window.XMLHttpRequest)
      {
         transfer = new XMLHttpRequest();
      }
      return transfer;
   }

   /*
      This function shows the current uploaded total
      on a progress bar.

      Parameter:  subtotal - the current total size uploaded sofar
    */

   function updateBar(subtotal)
   {
      
	   var myBar = document.getElementById('uploadStatus');
	   
      var barWidth = 400 * subtotal / 6000000;
	//   var barWidth = subtotal;
	      
	   myBar.style.width = barWidth + 'px';
      
    }

   /*
      This function formats the bytes into 'KB' and 'MB'
      according to their value. It inclues a tenth and handredth
      digits if it is formatted into 'KB' or 'MB'.

      Parameter:  bytes - the total size in bytes
      Return:     A string corresponds to the passed-in parameter
                  in 'bytes', 'KB', or 'MB' and 'Bytes', 'KB', or
                  'MB' included at the end of this string.
    */

      function bytesFormatter(bytes)
   {
      if (bytes < 1024)
         return bytes + " Bytes";
      if (bytes >=1024 && bytes < (1024 * 1024))
         return Math.round(bytes/1024 * 100) / 100 + " KB";
      return Math.round(bytes/1024/1024*100) / 100 + " MB";
   }

   /*
      This function parse the return response from the server
      to extract the size of the total uploaded files sofar.

      Parameter:  res - the response come back from server
      return:     A subtotal of size of uploaded files sofar
    */

   function parseResponse(res)
   {
	   alert('1');
      var firstIndex = res.indexOf("subtotal:");
      if (firstIndex != -1)
      {
         var retTotal = res.substring(firstIndex+9);
         var subSize = parseInt(retTotal);
         return subSize;
      }
      else
         return 0;
   }

   /*
      This function sends request to the server, and register
      the callback function to process the response when result
      comes back from the server.
    */

   function requestInfo(uid)
   {
	 
	  var sessionId=readCookie("PHPSESSID");
	     
      var xmlHttpObject;
      xmlHttpObject = createXMLHttpRequest();
      xmlHttpObject.open("get", "http://localhost/talent/php/progress.php?id="+uid, true);
      // callback function
      
      xmlHttpObject.onreadystatechange = function ()
      {
    	  
         // The response is stable for process when readyState is 4
         if (xmlHttpObject.readyState == 4)
         {  
            // check if upload is successful or not
           // if (xmlHttpObject.status == 200)
            //{
        	 	
        	 
               var response = xmlHttpObject.responseText;
               var subTotal = parseResponse(response);
      
               if (subTotal >= totalSize)
               {
            	   
                  var formatedBytes =
                  bytesFormatter(subTotal);
                  updateBar(subTotal);
                  displayInfo(formatedBytes);
                  totalSize = subTotal;
               }
               else
               { 
            	 
                  setIntervalOut(); // upload has finished
               return true;
            	}
          //     }
            //else
           /* {
               displayInfo("An error occurred: "
                           + xmlHttpObject.statusText);
                  //statusText is not always accurate
               setIntervalOut();
               return false;
            }
         	*/
         }
         
      };
      xmlHttpObject.send(null);
      
   }

   /*
      This function turns the timer off.
   */

   function setIntervalOut()
   {
      if (myInterval != null)
         clearInterval(myInterval);
   }

   /*
      This function is used to display the current
      uploaded size sofar on the browser
   */

   function displayInfo(text)
   {
      //var divInfo = document.getElementById("progressInfo");
      //divInfo.innerHTML = text;
   }

   // Turns the timer off if there is an error or a user-abort action
   //window.onAbort=setIntervalOut;
   //window.onError=setIntervalOut;
   
   
   /*************************************************************************
    *      
    *
    *  
   /************************************************************************/    
   function readCookie(name) 
   {
   var nameEQ = name + "=";
   var ca = document.cookie.split(';');
   
   for(var i=0;i < ca.length;i++) 
     {
   	var c = ca[i];
   	while (c.charAt(0)==' ') c = c.substring(1,c.length);
   	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
   	}
   return null;
   }   



