﻿function loadXML(xmlFile) {
    var xmlDoc;
    if (window.ActiveXObject) {
        xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
        xmlDoc.async = false;
        xmlDoc.load(xmlFile);
    }
    else if (document.implementation && document.implementation.createDocument) {
        xmlDoc = document.implementation.createDocument('', '', null);
        xmlDoc.async = false;
        xmlDoc.load(xmlFile);
    }
    else {
        return null;
    }
    return xmlDoc;
}
function hoursFormat(v) {
//    var now, h, bDate, eDate, nDate, mAlgorithm, aDefault, sDefault;
//    now = new Date();

    var  h, bDate, eDate, nDate, mAlgorithm, aDefault, sDefault;

    h = now.getUTCHours();

    nDate = (parseInt(now.getMonth()) + 1) + "/" + now.getDate();
    nDate = nDate.replace(/\-/g, '/').replace(/\b(\w)\b/g, '0$1');

    mAlgorithm = $(v).attr("DIFFERENCE").substring(0, 1);
    aDefault = $(v).attr("DIFFERENCE").replace(/\-/g, '').replace(/\+/g, '');

    if ($(v).children().length != 0) {
        var vNode = $(v).children();

        bDate = $(vNode[0]).attr('BEGIN').replace(/\-/g, '/').replace(/\b(\w)\b/g, '0$1');
        eDate = $(vNode[0]).attr('END').replace(/\-/g, '/').replace(/\b(\w)\b/g, '0$1');

        mAlgorithm = $(vNode[0]).attr('DIFFERENCE').substring(0, 1);
        sDefault   = $(vNode[0]).attr('DIFFERENCE').replace(/\-/g, '').replace(/\+/g, '');

        if (mAlgorithm == "+") {
            if (nDate >= bDate && nDate <= eDate) {
                h = parseInt(h) + parseInt(sDefault);
            } else {
                h = parseInt(h) + parseInt(aDefault);
            }
        }
        else if (mAlgorithm == "-") {
            if (nDate >= bDate && nDate <= eDate) {
                h -= parseInt(sDefault);
            } else {
                h -= parseInt(aDefault);
            }
        }
    }
    else {
        if (mAlgorithm == "+") {
            h = parseInt(h) + parseInt(aDefault);
        } else if (mAlgorithm == "-") {
            h -= parseInt(aDefault);
        }
    }
    return h;
}

/*获取时间*/
function getTime(h) {
//    var now, m, s;
//    now = new Date();
    var m,s;
   
    if (h < 0)
    { h += 24; }
    m = now.getUTCMinutes();
    s = now.getUTCSeconds();
    h = ((h < 10) ? "0" : "") + h;
    m = ((m < 10) ? "0" : "") + m;
    s = ((s < 10) ? "0" : "") + s;
    return "" + h + ":" + m + ":" + s + "";
}
var timeHelper_xmlPath = "../inc/UniversalTime.xml"
var timeHelper_xmlDoc = new loadXML(timeHelper_xmlPath);
function readXMLInTime(areaCode) {
    var v = $(timeHelper_xmlDoc).find("LIST > AREA");
    if (v.length > 0) {
        for (var i = 0; i < v.length; i++) {
            var myCode = $(v[i]).attr("CODE");
            if (areaCode.toUpperCase() == myCode.toUpperCase()) {
                var h = hoursFormat($(v[i]));
                var t = getTime(h);
                return t;
            }
        }
    }
}
