var interval = 0;
var UrlList = new Array();
var GatherState = 0; //采集状态
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 += "&N=" + Math.random();

    var Ajax = new AjaxClass();
    if ($("Reverse").checked == false)
        Ajax.RequestUrl = "GatherClass.ashx" + parameters;
    else
        Ajax.RequestUrl = "GatherClassReverse.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);
                GatherState = 1;
                CurrentRowIndex = 0;
                GatherWeb(GetArr(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 GetArr(index)
{
    var str = '';
    for (var i = index; i < (index + thread); i++)
    {
        if (i == (UrlList.length - 1))
        {
            break;
        }
        str += UrlList[i] + '|';
        CurrentRowIndex = i;
    }
    return str;
}

function GatherWeb(urls)
{
    //    if (GatherState == 0)
    //    {
    //        return;
    //    }
    //    if (UrlList == null)
    //    {
    //        Stop();
    //        return;
    //    }
    //    if (index >= UrlList.length && NextClassUrl != "")
    //    {
    //        return;
    //    }

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

    //    if (UrlList[index] == null)
    //    {
    //        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 + ')');

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

                if (ReturnValue.Gathered == true)
                {
                    GatheringNum++;
                    ProgressNumCtl.innerText = GatheringNum;
                }
                else
                {
                    IgnoreNum++;
                    ProgressIgnoreNum.innerText = IgnoreNum;
                }
                 
                window.setTimeout("GatherWeb(" + CurrentRowIndex + ")", interval);

                if (NextClassUrl != "" && (index + 1) == UrlList.length)
                {
                    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();

    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 = 0;
    CurrentRowIndex = 0;
    UrlList = new Array();
    NextClassUrl = "";
    GatherNum = 0;
    GatheringNum = 0;
    IgnoreNum = 0;

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


function Message(str, state)
{
    if (stoplog == false)
    {
        var today = new Date();
        AddLog(today.format('hh:mm:ss:fff'), str, state);
    }
}
function FillString(value, fullStr, length)
{
    while (value.length < length)
    {
        value += fullStr;
    }
    return value;
}

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;
} 