﻿var cglCount = 0;
var GetNodeValue = function (obj) {
    var str = "";
    if (window.ActiveXObject)    //IE
    {
        str = obj.text;
    }
    else //Mozilla
    {
        try {
            str = obj.childNodes[0].nodeValue;
        }
        catch (ex) {
            str = "";
        }
    }
    return str;
}

if (document.implementation && document.implementation.createDocument) {
    XMLDocument.prototype.loadXML = function (xmlString) {
        var childNodes = this.childNodes;
        for (var i = childNodes.length - 1; i >= 0; i--)
            this.removeChild(childNodes[i]);

        var dp = new DOMParser();
        var newDOM = dp.parseFromString(xmlString, "text/xml");
        var newElt = this.importNode(newDOM.documentElement, true);
        this.appendChild(newElt);
    };

    // check for XPath implementation
    if (document.implementation.hasFeature("XPath", "3.0")) {
        // prototying the XMLDocument
        XMLDocument.prototype.selectNodes = function (cXPathString, xNode) {
            if (!xNode) {
                xNode = this;
            }
            var oNSResolver = this.createNSResolver(this.documentElement)
            var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
            var aResult = [];
            for (var i = 0; i < aItems.snapshotLength; i++) {
                aResult[i] = aItems.snapshotItem(i);
            }
            return aResult;
        }
        // prototying the Element
        Element.prototype.selectNodes = function (cXPathString) {
            if (this.ownerDocument.selectNodes) {
                return this.ownerDocument.selectNodes(cXPathString, this);
            }
            else {
                throw "For XML Elements Only";
            }
        }
    }

    // check for XPath implementation
    if (document.implementation.hasFeature("XPath", "3.0")) {
        // prototying the XMLDocument
        XMLDocument.prototype.selectSingleNode = function (cXPathString, xNode) {
            if (!xNode) { xNode = this; }
            var xItems = this.selectNodes(cXPathString, xNode);
            if (xItems.length > 0) {
                return xItems[0];
            }
            else {
                return null;
            }
        }
        // prototying the Element
        Element.prototype.selectSingleNode = function (cXPathString) {
            if (this.ownerDocument.selectSingleNode) {
                return this.ownerDocument.selectSingleNode(cXPathString, this);
            }
            else { throw "For XML Elements Only"; }
        }
    }
}

function loadCGLData(xmlfilename) {
    var xmlHttp;
    var name;

    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    try {
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    var xmlDOM = xmlHttp.responseXML;
                    var root = xmlDOM.documentElement;
                    try {
                        var imgs = root.selectNodes("//img");
                        var titles = root.selectNodes("//title");
                        var links = root.selectNodes("//link");
                        var lives = root.selectNodes("//live");
                        for (var i = 0; i < lives.length; i++) {
                            var checkid = CompareDate(GetNodeValue(lives[i]));
                            if (checkid == 1 && cglCount < 3) {
                                cglCount++;
                                var linkob = document.getElementById("link" + cglCount.toString());
                                var imgob = document.getElementById("img" + cglCount.toString());
                                var textlinkob = document.getElementById("textlink" + cglCount.toString());

                                imgob.src = GetNodeValue(imgs[i]);
                                linkob.href = GetNodeValue(links[i]);
                                textlinkob.href = GetNodeValue(links[i]);
                                textlinkob.innerHTML = GetNodeValue(titles[i]);
                            }
                        }
                    }
                    catch (exception) {
                        alert(exception);
                    }
                }
            }
        }
        xmlHttp.open("GET", "/CGLUpdate/"+xmlfilename + ".xml", true);
        xmlHttp.send(null);
    }
    catch (exception) {
        //alert(exception);
    }
}

function CompareDate(dateValue) {
    //alert(dateValue);
    var checkid = 0;
    var thisDate = new Date(dateValue);
    var TodayDate = new Date();
    if (thisDate < TodayDate) {
        checkid = 1;
    }
    //alert(checkid);
    return checkid;
}
