﻿    theUniqueID = 0;
    iTimerID = null;        //這個變數是作計時器的ID
    if (window.ActiveXObject) {
       oXMLDoc = new ActiveXObject("Microsoft.XMLDOM");            //創建'MSXML'對象
    }
    else {
       oXMLDoc = document.implementation.createDocument( " ", " ",null); 
    }
    
    
    if (window.ActiveXObject) {
       oXMLDoc.async = true;
    }
    
    uninitialized = 0;
   //讓資料提交的同時執行顯示進度條的函數

function ProgressBar(SerialNumber)
{
    //document.all.rate.innerHTML = SerialNumber
    //tmpName = document.getElementById(SerialNumber);
    sURL = "/upload/Progress_Xml.asp?ProgressID=" + SerialNumber + "&temp="+Math.random();      //獲取上傳狀態資料的位址
    oXMLDoc.onreadystatechange = function() {
                                    var iReadyState = 0
                                        try
                                        {
                                            iReadyState = oXMLDoc.readyState;
                                        }
                                        catch(e)
                                        {
                                            return;
                                        }
                                        if(  iReadyState != 4 ) return;

                                        if( oXMLDoc == null || oXMLDoc.xml == "" ){
                                    		//window.status = 'Xml load fault';
                                    		return;
                                        }
                                   
                                    var oRoot = oXMLDoc.documentElement;     //獲取返回xml資料的根節點

                                    if(oRoot != null)
                                    	{
                                    		if (oRoot.selectSingleNode("ErrorMessage") == null)
                                    		{

                                    			var readyState = oRoot.selectSingleNode("ReadyState").text;

                               		            // 如果還沒初始化完成，繼續
                                    			if ( readyState == "uninitialized" )
                                    			{
                                    				//如果超過初始化時間過久
                                    				uninitialized ++

                                    				if (uninitialized >= 64) {
                                    				   //UploadDone("timeout" ,SerialNumber)
                                    				   return ;
                                    				}
                                 				    document.all.Up_Button.value = oRoot.selectSingleNode("Percentage").text 
                                    				iTimerID = setTimeout("ProgressBar('" + SerialNumber + "')", 10);                                    				

                                    			}
                                    			// 上傳進行中
                                    			else if ( readyState == "loading" )              //檔上傳結束就取消計時器
                                    			{                               
                                    				//這裏設定時間間隔是0.5秒，你也可以根據你的情況修改獲取資料時間間隔
                                 				    document.all.Up_Button.value = oRoot.selectSingleNode("Percentage").text 
                                    				iTimerID = setTimeout("ProgressBar('" + SerialNumber + "')", 10);                                    				
                                    			}
                                    			// 上傳結束
                                    			else if ( readyState == "loaded" )
                                    			{
                                    				//PercentDone.style.width = "100%";        //設置進度條的百分比例
                                    				if (iTimerID != null)
                                    					clearTimeout(iTimerID)
                                    				iTimerID = null;	
                                 				    document.all.Up_Button.value = oRoot.selectSingleNode("Percentage").text 
                                    				iTimerID = setTimeout("ProgressBar('" + SerialNumber + "')", 10);
                                    				//alert("上傳結束，伺服器處理中...");
                                    			}
                               	                // 上傳結束
                                    			else
                                    			{
                                 				   document.all.Up_Button.value = oRoot.selectSingleNode("Percentage").text 
                                    			   //iTimerID = setTimeout("ProgressBar('" + SerialNumber + "')", 10);
                                 		  		   document.all.Up_Button.disabled = false
                                 		  		   document.all.Up_Button.value = "開始上傳"
                                 		  		   if (iTimerID != null) {
                                    					clearTimeout(iTimerID)
                                    				    iTimerID = null;

                                		     	    }
                                    			}
                                    		}
                                    		else
                                    		{
                               		  		   document.all.Up_Button.disabled = false
                               		  		   document.all.Up_Button.value = "開始上傳"
                                    		}
                                    	}
                                    	
                                    	

    }; //****************
    
    oXMLDoc.load( sURL );
} 


function uploadimg(thisform) {
  	var theUniqueID = parseInt(Math.random() * 10000)
  	document.all.Up_Button.disabled = true
    thisform.method="POST"
    thisform.target="Upload_Image"
    thisform.action="Upload/UploadProcess/index.asp?ProgressID=" + theUniqueID + "&sys=" + Math.random()
    thisform.enctype="multipart/form-data"
    ProgressBar(theUniqueID)
}


function CacelUpload(upform) {
	upform.location.href = "about:blank"

	if (!(iTimerID = null)) {
		clearTimeout (iTimerID)
	    iTimerID = null	
	}
}
