﻿function Collecter(xml, form)//xml:存放收集到的数据的XmlHelper类,form:要收集数据的form,可为空.
{
    this.Xml = xml;
    this.Form = form;
}

Collecter.prototype = {
    Collect: function() {
        this.UpdateFCKEditor();
        this.CollectInput();
        this.CollectTextArea();
        this.CollectSelect();
        return this.Xml.XmlDocument;
    },

    GetDomArray: function(tag) {
        return this.Form ? this.Form.getElementsByTagName(tag) : document.getElementsByTagName(tag);
    },

    CollectSelect: function() {
        var t = this.GetDomArray("select");
        if (t) {
            for (var x = 0; x < t.length; x++) {
                if (t[x].multiple) {
                    var v = "";
                    for (var z = 0; z < t[x].length; z++) {
                        if (t[x][z].selected) {
                            v += t[x][z].value + ",";
                        }
                    }
                    this.Xml.Append(t[x].name, v.trim(","));
                    continue;
                }
                this.Xml.Append(t[x].name, t[x].value);
            }
        }
    },
    CollectTextArea: function() {
        var t = this.GetDomArray("textarea");
        if (t) {
            for (var x = 0; x < t.length; x++) {
                this.Xml.Append(t[x].name, t[x].value);
            }
        }
    },
    CollectInput: function() {
        var input = this.GetDomArray("input");
        var t = new Array();
        for (var x = 0; x < input.length; x++) {
            t[x] = input[x];
        }
        if (t) {
            for (var x = 0; x < t.length; x++) {
                if (!t[x].name || t[x].disabled) {
                    continue;
                }
                if (t[x].type == "checkbox") {
                    var v = t[x].checked ? t[x].value : "";
                    for (var z = x + 1; z < t.length; z++) {
                        if (t[x].name == t[z].name) {
                            if (t[z].checked) {
                                v += "," + t[z].value;
                            }
                            t.splice(z, 1);
                            z--;
                        }
                    }
                    this.Xml.Append(t[x].name, v);
                    continue;
                }
                if (t[x].type == "radio") {
                    var v = t[x].checked ? t[x].value : "";
                    if (t[x].checked) {
                        this.Xml.Append(t[x].name, v);
                    }
                    continue;
                }
                this.Xml.Append(t[x].name, t[x].value);
            }
        }
    },

    UpdateFCKEditor: function() {
        var fck;
        try {
            fck = FCKeditorAPI
        }
        catch (e) {
            return;
        }
        for (var name in fck.__Instances) {
            var oEditor = fck.__Instances[name];
            if (!this.Form || (oEditor.GetParentForm && oEditor.GetParentForm() == this.Form)) {
                oEditor.UpdateLinkedField();
            }
        }
    }
};

function XmlHelper(doc) {
    if (!doc) {
        this.XmlDocument = this.CreateDocument();
        this.Document = this.XmlDocument.createElement("K2046");
        this.XmlDocument.appendChild(this.Document);
    }
    else {
        this.XmlDocument = doc;
    }
}

XmlHelper.prototype = {
    Append: function(name, value) {
        var el = this.XmlDocument.createElement("K");
        el.setAttribute("SN", name.toUpperCase());
        el.setAttribute("V", value);
        this.Document.appendChild(el);
    },
    CreateDocument: function()//创建XML文档对象
    {
        var signatures = ["Msxml2.DOMDocument", "Microsoft.XmlDom"];
        for (var i = 0; i < signatures.length; i++) {
            try {
                var domDoc = new ActiveXObject(signatures[i]);
                return domDoc;
            }
            catch (e) { }
        }
        try {
            var domDoc = document.implementation.createDocument("text/xml", "", null);
            return domDoc;
        }
        catch (e) { }
        return null;
    },
    GetValue: function(el) {
        var result = "";
        var xe = this.XmlDocument.getElementsByTagName(el);
        if (xe.length > 0) {
            for (var x = 0; x < xe.length; x++) {
                var s = xe[x].text == undefined ? xe[x].textContent : xe[x].text;
                result += s + ",";
            }
        }
        return result.trim(",");
    }
};