// JavaScript Document

//格式化日期字符串 yyyy-mm-dd
function ql_date_yyyymmdd(date) {
    if (typeof (date) != "object") date = ql_date_fromstr(date);

    var year = "" + date.getFullYear();
    var month = "" + (100 + date.getMonth() + 1)
    var day = "" + (100 + date.getDate())

    return year + "-" + month.substr(1, 2) + "-" + day.substr(1, 2);
}

//格式化日期字符串 yyyy-mm-dd
function ql_date_yyyymmddhhss(date) {
    if (typeof (date) != "object") date = ql_date_fromstr(date);

    var year = "" + date.getFullYear();
    var month = "" + (100 + date.getMonth() + 1);
    var day = "" + (100 + date.getDate());
    var hour = "" + (100 + date.getHours());
    var minutes = "" + (100 + date.getMinutes());

    return year + "-" + month.substr(1, 2) + "-" + day.substr(1, 2) + " " + hour.substr(1, 2) + ":" + minutes.substr(1, 2);
}

//通用转日期函数
function ql_date_fromstr(date) {
    if (typeof (date) == "date") return date;
    if (typeof (date) == "string") return new Date(date.replace(/-/g, "/"));
    return Date.parse(date);
}

//日期比较函数
function ql_date_compare(date1, date2, sortdir) {
    var a = ql_date_fromstr(date1);
    var b = ql_date_fromstr(date2);
    var idate1 = a.getFullYear() * 10000000000 + (a.getMonth() + 1) * 100000000 + a.getDate() * 1000000 + a.getHours() * 10000 + a.getMinutes() * 100 + a.getSeconds();
    var idate2 = b.getFullYear() * 10000000000 + (b.getMonth() + 1) * 100000000 + b.getDate() * 1000000 + b.getHours() * 10000 + b.getMinutes() * 100 + b.getSeconds();
    if (typeof (sortdir) == "undefined" || sortdir)
        return idate1 - idate2;
    else
        return idate2 - idate1;
}

//格式化数字字符串
function ql_number_format(value, format) {
    if (typeof (format) == "undefined") format = "n2";
    if (format == null || format == "") format = "n2";

    var formtype = format.substr(0, 1);
    var declen = parseInt(format.length >= 2 ? format.substr(1, 1) : "0");
    if (formtype == "p") value *= 100;
    value = "" + value;

    var pos = value.indexOf(".");
    var part1 = pos > 0 ? value.substr(0, pos) : value;
    var part2 = pos >= 0 ? value.substr(pos + 1) : "";
    if (part1 == "") part1 = "0";

    var result = part1;
    if (formtype == "n") result = result.replace(/(\.\d{3}).*$/, "$1").replace(/(\d{1,3})(?=(\d{3})+(?:$|\D))/g, "$1,");
    if (declen > 0) result += "." + (part2 + "00000000000").substr(0, declen);
    if (formtype == "p") result += "%";
    return result;
}

//通用转字符串函数
function ql_format(text, type, format) {
    if (typeof (text) == "undefined" || text == null || text == "") return "--";
    switch (type) {
        case "date":
            if (format == null || format == "") return ql_date_yyyymmdd(text);
            return ql_date_fromstr(text).format(format);
        case "int":
            if (format == null || format == "") format = "n0";
            return ql_number_format(parseInt(text), format);
        case "number":
            if (format == null || format == "") format = "n2";
            return ql_number_format(parseFloat(text), format);
    }

    return text;
}

//对象转XML字符串
function ql_xml_objecttoxml(name, value) {
    var xml = "";

    if (typeof (value) == "undefined" || value == null) {
        // do nothing
        xml = "<" + name + "></" + name + ">";
    }
    else {
        if (typeof (value) == "object" && value.constructor == Array) {
            for (i = 0; i < value.length; i++)
                xml += ql_xml_objecttoxml(name, value[i]);
            return xml;
        }
        else {
            xml = "<" + name + ">";
            if (typeof (value) == "object") {
                for (var p in value) {
                    xml += ql_xml_objecttoxml(p, value[p]);
                }
            }
            else if (typeof (value) == "string") {
                xml += "<![CDATA[" + value + "]]>";
            }
            else {
                xml += "" + value;
            }
            xml += "</" + name + ">";
        }
    }

    return xml;
}

//XML字符串转XML对象
function ql_xml_stringtoxml(xmlstring) {
    var doc = null;
    if (window.ActiveXObject) {
        var ActiveIds = ['MSXML2.XMLDOM', 'Microsoft.XMLDOM', 'MSXML.XMLDOM', 'MSXML3.XMLDOM'];
        for (var len = ActiveIds.length, i = 0; i < len; i++) {
            var id = ActiveIds[i];
            try {
                var doc = new ActiveXObject(id);
                doc.async = false;
                doc.setProperty('SelectionLanguage', 'XPath');
                doc.loadXML(xmlstring);
                break;
            }
            catch (e) {
            }
            finally {
                if (doc && doc.parseError && doc.parseError.errorCode != 0) {
                    //throw {parser:'MSXML', message:doc.parseError.reason, xml:xmlstring, func:'xmlDocument'};
                    return;
                }
            }
        }
    }
    else if (typeof DOMParser != 'undefined') {
        var parser = new DOMParser();
        var doc = parser.parseFromString(xmlstring, 'text/xml');
        if (doc.documentElement.nodeName == 'parsererror') {
            // throw {parser:'DOMParser', message:doc.documentElement.firstChild.nodeValue, xml:xmlString, func:'xmlDocument'};
            return;
        }
    }
    else {
        return;
    }
    return doc;
}

//XML对象转字符串
function ql_xml_xmltostring(xml) {
    if (window.ActiveXObject) {
        return (xml.xml);
    } else {
        return ((new XMLSerializer()).serializeToString(xml));
    }
}
