var cycleFlag = true;
var timer;
var fileName = null;
var percent = null;
var speed = null;
var readTotalSize = null;
var totalSize = null;
var remainTime = null;
var totalTime = null;
var td1;
var td2;
function init1() {
    cycleFlag = true;
    td1 = document.getElementById("progressTd1");
    td2 = document.getElementById("progressTd2");
    percent = document.form.percent;
    speed = document.form.speed;
    remainTime = document.form.remainTime;
    totalTime = document.form.totalTime;
}

function updateDetail(_percent, _fileName, _speed, _readTotalSize, _totalSize, _remainTime, _totalTime) {
    if (_percent <= 0) {
        _percent = 1;
    }
    percent.value = _percent + "%";
    td1.width = _percent + "%";
    speed.value = _speed + "KB/S";
    remainTime.value = _remainTime;
    totalTime.value = _totalTime;
}

function shutdown() {
    window.close();
}

function HttpRequestObject() {
    this.channal = null;
    this.instance = null;
}


var Request = new function () {
    this.showStatus = true;
    this.httpRequestCache = new Array();
    this.createInstance = function () {
        var instance = null;
        if (window.XMLHttpRequest) {
            instance = new XMLHttpRequest();
            if (instance.overrideMimeType) {
                instance.overrideMimeType = "text/xml";
            }
        } else {
            if (window.ActiveXObject) {
                var MSXML = ["Microsoft.XMLHTTP", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP"];
                for (var i = 0; i < MSXML.length; i++) {
                    try {
                        instance = new ActiveXObject(MSXML[i]);
                        break;
                    }
                    catch (e) {
                    }
                }
            }
        }
        return instance;
    };
    
    
    this.getInstance = function (_channal) {
        var instance = null;
        var object = null;
        if (_channal == undefined) {
            _channal = "default";
        }
        var getOne = false;
        for (var i = 0; i < this.httpRequestCache; i++) {
            object = HttpRequestObject(this.httpRequestCache[i]);
            if (object.channal == _channal) {
                if (object.instance.readyState == 0 || object.instance.readyState == 4) {
                    instance = object.instance;
                }
                getOne = true;
                break;
            }
        }
        if (!getOne) {
            object = new HttpRequestObject();
            object.channal = _channal;
            object.instance = this.createInstance();
            this.httpRequestCache.push(object);
            instance = object.instance;
        }
        return instance;
    };
    
    
    this.send = function (_url, _data, _channal, _asynchronous) {
        if (_channal == undefined || _channal == "") {
            _channal = "default";
        }
        if (_asynchronous == undefined) {
            _asynchronous = true;
        }
        var instance = this.getInstance(_channal);
        instance.onreadystatechange = function () {
            if (instance.readyState == 4) {
                if (instance.status == 200) {
                    var text = instance.responseText;
                    if (text.length > 0) {
                        var paras = text.split("||");
                        updateDetail(paras[0], paras[1], paras[2], paras[3], paras[4], paras[5], paras[6]);
                    }
                }
            }
        };
        
        
        if (_url.indexOf("?") != -1) {
            _url += "&requestTime=" + (new Date()).getTime();
        } else {
            _url += "?requestTime=" + (new Date()).getTime();
        }
        if (_data.length == 0) {
            instance.open("GET", _url, _asynchronous);
            instance.send(null);
        } else {
            instance.open("POST", _url, _asynchronous);
            instance.setRequestHeader("Content-Length", _data.length);
            instance.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            instance.send(_data);
        }
    };
    
    
    this.intervalSend = function (_url, _channal) {
        if (_channal == undefined) {
            _channal = "defaultInterval";
        }
        var instance = Request.getInstance(_channal);
        instance.onreadystatechange = function () {
            if (instance.readyState == 4) {
                if (instance.status == 200) {
                    var text = instance.responseText;
                    if (text.length > 0) {
                        var paras = text.split("||");
                        updateDetail(paras[0], paras[1], paras[2], paras[3], paras[4], paras[5], paras[6]);
                    } else {
                        if(window.opener.document.form1==null){
                           window.setTimeout("shutdown()", 0);
                        }
                        if (window.opener.document.form1.file1.value == "") {
                            window.setTimeout("shutdown()", 0);
                        }
                    }
                }
            }
        };
        
        
        if (_url.indexOf("?") != -1) {
            _url += "&requestTime=" + (new Date()).getTime();
        } else {
            _url += "?requestTime=" + (new Date()).getTime();
        }
        instance.open("GET", _url, true);
        instance.send("");
    };
};


function send() {
    Request.intervalSend("/web/getPercentage.up", true);
}
function cycle() {
    if (cycleFlag) {
        send();
		
        timer = window.setTimeout("cycle()", 2000);
    } else {
        window.clearTimeout(timer);
    }
}
function show() {
    init1();
    cycle();
}


