var interval = 0;
var UrlList = new Array();
var GatherState = -1; //采集状态
var NextClassUrl = "";
var GatherNum = 0;
var GatheringNum = 0;
var IgnoreNum = 0;
var ProgressNumCtl;
var ProgressTotalCtl;
var ProgressIgnoreNum;
var thread = 5;
//正常采集
function GatherMain()
{
    var parameters = "?ID=" + Request.QueryString("ID");
    parameters += "&ModelID=" + Request.QueryString("ModelID");
    parameters += "&NextClassUrl=" + NextClassUrl;
    parameters += "&Reverse=" + $("Reverse").checked;
    parameters += "&N=" + Math.random();

    var Ajax = new AjaxClass();
    Ajax.RequestUrl = "GatherClass.ashx" + parameters;
    Ajax.Method = "get"
    Ajax.StateChangeEvent = function ()
    {
        if (Ajax.XmlHttp.readyState == 4)
        {
            if (Ajax.XmlHttp.status == 200)
            {
                var xmlDoc = CreateXMLDOM();

                xmlDoc.loadXML(Ajax.XmlHttp.responseText);

                if (xmlDoc.childNodes.length == 0)
                {
                    Message("服务端程序错误.", 0);
                    Stop();
                    return;
                }
                var Err = xmlDoc.getElementsByTagName("Err")[0].text;
                if (Err != "")
                {
                    Message(Err, 0);
                    Stop();
                    Message("采集任务中止。", 3);
                    return;
                }

                //GatherNum = xmlDoc.getElementsByTagName("GatherNum")[0].text;
                NextClassUrl = xmlDoc.getElementsByTagName("NextClassUrl")[0].text;
                var xmlUrlList = xmlDoc.getElementsByTagName("UrlList")[0].getElementsByTagName("url");
                for (var i = 0; i < xmlUrlList.length; i++)
                {
                    UrlList.push(xmlUrlList[i].text);
                }

                if (GatherNum > 0)
                    ProgressTotalCtl.innerText = GatherNum;
                else
                    ProgressTotalCtl.innerText = "∞";

                //开始采集
                //window.setTimeout("GatherWebStart()", 10);
                if (GatherState != -1)
                    GatherState = 1;
                CurrentRowIndex = 0;
                GatherWeb(GetUrl(CurrentRowIndex));
            }
            else
            {
                Message("服务器连接失败。错误代码为：[" + Ajax.XmlHttp.status + "]," + Ajax.XmlHttp.statusText, 0);
            }
        }
    };
    Ajax.SendRequest();
}

function GatherWebStart()
{
    if (UrlList.length > 0)
    {
        GatherState = 1;
        CurrentRowIndex = 0;
        GatherWeb(CurrentRowIndex);
    }
}
function GetUrl(index)
{
    var str = '';
    var J = index + thread;
    if (index + thread > UrlList.length)
        J = UrlList.length;

    for (var i = index; i < J; i++)
    {
        str += UrlList[i] + '|';
        CurrentRowIndex = i;
    }
    return str;
}

function GatherWeb(urls)
{
    if (GatherState == -1)
    {
        Message("用户停止采集.", 3);
        return;
    }

    if (urls == "" && NextClassUrl != "")
    {
        Message("异常," + NextClassUrl, 0);
        return;
    }

    if (urls.length == 0)
    {
        Message("异常," + NextClassUrl, 0);
        return;
    }

    var Ajax = new AjaxClass();
    Ajax.RequestUrl = "GatherWeb.ashx";
    Ajax.Method = "post"
    Ajax.IsAsyn = true;
    Ajax.ContentType = "application/x-www-form-urlencoded";
    Ajax.StateChangeEvent = function ()
    {
        if (Ajax.XmlHttp.readyState == 4)
        {
            if (Ajax.XmlHttp.status == 200)
            {
                var ReturnValue = eval('(' + Ajax.XmlHttp.responseText + ')');

                for (var j = 0; j < ReturnValue.task.length; j++)
                {
                    if (ReturnValue.task[j].Messages.length > 0)
                    {
                        Message(ReturnValue.task[j].URL + "<br />消息：" + ReturnValue.task[j].Messages, 1);
                    }
                    if (ReturnValue.task[j].Alerts.length > 0)
                    {
                        Message(ReturnValue.task[j].URL + "<br />警告：" + ReturnValue.task[j].Alerts, 2);
                    }
                    if (ReturnValue.task[j].Errs.length > 0)
                    {
                        Message(ReturnValue.task[j].URL + "<br />错误：" + ReturnValue.task[j].Errs, 0);
                    }

                    if (ReturnValue.task[j].Gathered == true)
                    {
                        GatheringNum++;
                        ProgressNumCtl.innerText = GatheringNum;
                    }
                    else
                    {
                        IgnoreNum++;
                        ProgressIgnoreNum.innerText = IgnoreNum;
                    }
                }
                if (GatherState == -1 && CurrentRowIndex == 0)
                {
                    Message("采集完成.", 3);
                    return;
                }

                if ((GatheringNum >= GatherNum && GatherNum > 0) || (NextClassUrl == "" && CurrentRowIndex == UrlList.length - 1))
                {
                    Stop();
                    Message("采集完成.", 3);
                    return;
                }

                if (CurrentRowIndex < UrlList.length - 1)
                {
                    CurrentRowIndex++;

                    window.setTimeout("GatherWeb(\"" + GetUrl(CurrentRowIndex) + "\")", interval);
                }

                if (NextClassUrl != "" && CurrentRowIndex == UrlList.length - 1)
                {
                    CurrentRowIndex = 0;
                    UrlList = new Array();

                    GatherMain(); // 重新采集一下页栏目页
                }
            }
            else
            {

                Message(" 错误代码为：[" + Ajax.XmlHttp.status + "]," + Ajax.XmlHttp.statusText, 0);
            }
        }
    };

    var parameters = "ID=" + Request.QueryString("ID");
    parameters += "&ModelID=" + Request.QueryString("ModelID");
    parameters += "&URL=" + escape(urls);
    parameters += "&BadLinkNum=" + $("BadLinkNum").value;
    parameters += "&IsTest=" + $("IsTest").checked;
    parameters += "&IsUpdate=" + $("IsUpdate").checked;
    parameters += "&IsLeech=" + $("IsLeech").checked;
    parameters += "&N=" + Math.random();

    Ajax.Content = parameters;
    Ajax.SendRequest();
}

function Start()
{
    GatherState = 1;
    interval = 5;
    UrlList = new Array();
    GatherNum = 0;
    GatheringNum = 0;
    IgnoreNum = 0;
    ProgressNumCtl = $("ProgressNum");
    ProgressNumCtl.innerText = "0";
    ProgressTotalCtl = $("ProgressTotal");
    ProgressTotalCtl.innerText = "0";
    ProgressIgnoreNum = $("ProgressIgnoreNum");
    ProgressIgnoreNum.innerText = "0";
    GatherNum = parseInt($("GatherNum").innerText);

    $('bt_start').disabled = true;
    $('bt_stop').disabled = false;

    Message("开始采集。", 3);

    GatherMain();
}
function Stop()
{
    interval = 0; //生成间隔
    GatherState = -1;
    CurrentRowIndex = 0;
    UrlList = new Array();
    NextClassUrl = "";


    $("bt_start").disabled = false;
    $("bt_stop").disabled = true;
}

function CreateXMLDOM()
{
    var ActiveX = new Array("MSXML2.DOMDocument.5.0",
    "MSXML2.DOMDocument.4.0",
    "MSXML2.DOMDocument.3.0",
    "MSXML2.DOMDocument",
    "Microsoft.XMLDOM",
    "MSXML.DOMDocument");

    for (var i = 0; i < ActiveX.length; i++)
    {
        try
        {
            return new ActiveXObject(ActiveX[i]);
        }
        catch (e) { }
    }
    return null;
} 