function get_attribute(node, attr)
{
/*@cc_on
    if (attr == 'class')
        attr = 'className';
@*/
    return node.getAttribute(attr);
}

function set_attribute(node, attr, val)
{
/*@cc_on
    if (attr == 'class')
        attr = 'className';
@*/
    node.setAttribute(attr, val);
}
function new_httprequest() {
    var XhrObj = null;

    // Évite un bug du navigateur Safari :
    if (window.overrideMimeType) {
        window.overrideMimeType("text/xml");
    }
    if(window.XMLHttpRequest) // Firefox
        return new XMLHttpRequest();
    else if(window.ActiveXObject) // Internet Explorer
        return new ActiveXObject("Msxml2.XMLHTTP");
    else { // XMLHttpRequest non supporté par le navigateur
        return alert("Your browser does not seem to handle XMLHTTPRequest objects, which are needed for the javascript functions on this website!");
    } 
}
function get_childs_text(node)
{
    var ret = ''
    if (node != null)
    {
        var childs = node.childNodes;
        if (childs != null)
            for (var i = 0; i < childs.length; i++)
                ret += childs[i].data;
    }
    return ret;
}

function print_item(item, li)
{
    var title = get_childs_text(item.getElementsByTagName("title")[0]);
    var description = get_childs_text(item.getElementsByTagName("description")[0]);
    var link = get_childs_text(item.getElementsByTagName("link")[0]);
    if (title != '')
    {
        if (link != '')
        {
            var a = window.document.createElement("a");
            li.appendChild(a);
            set_attribute(a, "href", link);
            set_attribute(a, "class", "title");
            a.appendChild(window.document.createTextNode(title));
        }
        else
        {
            var span = window.document.createElement("a");
            li.appendChild(span);
            set_attribute(span, "class", "title");
            span.appendChild(window.document.createTextNode(title));
        }
        if (description != '')
        {
            li.appendChild(window.document.createElement("br"));
            li.appendChild(window.document.createTextNode(description));
        }
    }
}
function handle_response_read_file(obj, div, filename)
{
    if (obj.readyState != 4) 
    return;
    var newdoc = obj.responseXML;
    if (newdoc == null) return;
    var data = newdoc.documentElement;
    if (data == null)
    {
        alert(obj.responseText);
        return;
    }
    var title = get_childs_text(data.getElementsByTagName("title")[0]);
    var span = window.document.createElement("div");
    div.appendChild(span);
    set_attribute(span, "class", "title");
    span.appendChild(window.document.createTextNode(title));
    var ul = window.document.createElement("ul");
    div.appendChild(ul);
    var li;
    var channels = data.getElementsByTagName("channel");
    for (i = 0; i < channels.length; i++)
    {
        var channel = channels[i];
        var items = channel.getElementsByTagName("item");
        for (j = 0; j < items.length; j++)
        {
            var item = items[j];
            li = window.document.createElement("li");
            ul.appendChild(li);
            print_item(item, li);
        }
    }
}

function load_rss(div, filename)
{
    var obj = new_httprequest(); 
    obj.open('GET', filename, true); 
    obj.onreadystatechange = function ()
    {
        handle_response_read_file(obj, div, filename);
    }
    obj.send(null); 
}

load_rss(window.document.getElementById("rss"), "updates.rss");
