﻿// JScript 文件
function RtsDownload(spid,packagePath,configFilePath,clientType,version)
{
    this.spId = spid;
    this.packageUrl = packagePath;
    this.configFile = configFilePath;
    this.ClientType = clientType;
    this.isServer = (packagePath.toLowerCase().indexOf("client")==-1);
    this.install = "0";
    this.downloadCtrlId = "WebI8001";
    this.ServiceMode = "1"
    this.GroupId = 0;
    this.packageSize = "0"; 
    this.version = version;
    this.ControlState="-1";
    var self = this;
    var timerId_Status = null;   
    
    this.start = function()
    {
        window.attachEvent("onload",self.onloadHandler);
    }
    
    this.onloadHandler = function()
    {
         if(self.ClientType=="installableclient")
	       window.setTimeout(self.downPackage,10);
	     else
	        window.setTimeout(self.downPackage,1000);
    }
    
    this.downPackage = function()
	{
	    
		if(self.ClientType=="installableclient")
		{
            window.external.ServiceRequest(self.configFile);
            window.location.replace("SpResume.aspx?SpId=" + self.spId + "&ClientType=" + clientType + "&Version=" + self.version);
            return;
		}

        if (self.ControlState == "0") return; 

		var id = self.spId;
		if(self.isServer) id = "S" + self.spId;
		try
		{
			if(self.packageUrl=="")	window.history.back();
			var downCtrl = document.getElementById(self.downloadCtrlId);

			downCtrl.attachEvent("OnDownloadFinished",self.downloadFinished);
			downCtrl.attachEvent("OnDownloadFailed",self.downloadFailed);
			bResult = downCtrl.DownLoad2(self.packageSize,self.packageUrl,self.configFile,id);
			if(!bResult) self.downloadFailed();
		}
		catch(e)
		{
			self.downloadFailed();
		}
	}
	this.downloadTest = function()
	{
        try
        {
		   var id = self.spId;

		   var downCtrl = document.getElementById(self.downloadCtrlId);
		   downCtrl.Hello();
		   self.ControlState = "1";
        }
        catch(e)
        {
            self.testFailed();
        }
	}
	
	this.testFailed = function()
	{
        var divMessage = document.getElementById("divDownCtrl");
        if(divMessage != null)
            divMessage.style.display = "block";
        self.ControlState = "0";
	}
	
	this.downloadFailed = function()
	{
		var tb1 = document.getElementById("Table1");
		var tb2 = document.getElementById("Table2");
		if(tb1 != null) tb1.style.display = "none";
		if(tb2 != null) tb2.style.display = "block";
		ctl00_ctl00_cphBody_cphRight_CnCValSummary1_ValSummary.style.display = "none";
		alert(GlobalString.Download_DownloadFail);
	}
	
	//状态检查
	this.getRefreshInterval = function()
    {	
	    var interval = 10;
	    var frm = document.getElementById("aspnetForm");
	    if(frm.attributes["RefreshInterval"])
	    {
		    var attr = frm.attributes["RefreshInterval"]
		    if(!isNaN(attr.value)) interval = parseInt(attr.value);
	    }
	    return interval;
    }
    
    this.startCheck = function()
	{	
		var interval = self.getRefreshInterval();
		timerId_Status = window.setInterval(self.checkStatus,interval * 1000);
	}
	
	this.checkStatus = function()
	{
	    Service_Download.GetWebStatus(self.spId,self.checkStatus_callback);
	}
	
	this.checkStatus_callback = function(res)
    {
        if(res.error!=null)
        {
            alert(res.error);
            return;
        }
        window.status = res.value;
        
		if(self.isServer && res.value != 66) //Downloading	
		    window.location.replace("Queue.aspx?viewer=ws&FromRts=0&GroupID=" + self.GroupId+"&SpId"+self.spId);		
			//window.location.replace("Index.aspx?GroupID=" + self.GroupId + "&ClientType=" + clientType+ "&Version=" + self.version);
		    //alert(res.value);
		if(self.isServer==false && res.value == 69)
			window.location.replace("SpResume.aspx?SpId=" + self.spId + "&ClientType=" + clientType+ "&Version=" + self.version);
	}  
	
	this.downloadFinished = function(path)
    {
        var downCtrl = $(self.downloadCtrlId);
		if("1"==self.install)
		{
			var pos1 = path.lastIndexOf("\\");
			var folder1 = path.substr(0,pos1);
			var iniFile = folder1 + "\\RTSServer.ini";
			downCtrl.RunPackage2(path,iniFile,"HW\\web1800\\");
		}
		else
			downCtrl.RunPackage(path);
		window.location.replace(self.isServer?"Queue.aspx?viewer=ws&FromRts=0&GpID=" + self.GroupId + "&ClientType=" + clientType + "&Version=" + self.version+"&SpId=" + self.spId: "SpResume.aspx?SpId=" + self.spId + "&ClientType=" + clientType+ "&Version=" + self.version);
    }
}

