/*
** Javascript RSS parser
**
** copyright (c) Stas Verberkt (Legolas), 2001 - 2006
** Released under the GNU General Public License
**
** WWW: http://www.legolasweb.nl/
** E-mail: legolas@legolasweb.nl
**
** Using:
** Include this JS file in your page
** Make sure there's a div with id="rss"
** To change the feed edit the feed var at the bottom
** default it uses the query string
** (it prepends the query string with get.php?,
** because it isn't allowed to open extern sites using xmlHTTPRequest.
** get.php is a php file which servers the page.)
*/

/* Functions */
function init() {
    if (!feed) {
        var goal = document.getElementById('rss');
        goal.innerHTML = 'No feed specified';
    }
    else {
        getFeed();
    }
}

function createHTTPHandler() {
    var httphandler = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
        // JScript gives us Conditional compilation, we can cope with old IE versions.
        // and security blocked creation of the objects.
        try {
            httphandler = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                httphandler = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (E) {
                httphandler = false;
            }
        }
    @end @*/
    if (!httphandler && typeof XMLHttpRequest != 'undefined') {
        httphandler = new XMLHttpRequest();
    }
    return httphandler;
}

function getFeed() {
    xmlhttprequest.open('GET', feed, true);
    xmlhttprequest.onreadystatechange = parseFeed;
    xmlhttprequest.send(null);
}

function parseFeed() {
    if (xmlhttprequest.readyState == 4) {
        var error = null;
        if (xmlhttprequest.responseXML) {
            if (xmlhttprequest.responseXML.documentElement) {
                if (xmlhttprequest.responseXML.documentElement.tagName == 'parsererror') {
                    error = 'Error parsing XML response:<br /> ' + xmlhttprequest.responseXML.documentElement.firstChild.nodeValue;
                }
                else if (xmlhttprequest.responseXML.documentElement.tagName == 'error') {
                    error = 'Error retrieving XML response:<br /> ' + xmlhttprequest.responseXML.documentElement.firstChild.nodeValue;
                }
            }
            else {
                if (xmlhttprequest.responseXML.parseError) {
                    error = 'Error parsing XML response:<br /> ' + xmlhttprequest.responseXML.parseError.reason + ' (' + url + ')';
                }
                else {
                    error = 'No valid data in XML response';
                }
            }
        }
        else {
            error = 'The responseXML object was empty';
        }
        var goal = document.getElementById('rss');
        if (!error) {
            var rss = xmlhttprequest.responseXML;
            goal.innerHTML = '';
            parseRSS(rss, goal);
        }
        else {
            goal.innerHTML = error;
        }
    }
}

function parseRSS(rss, goal) {
    var roots = rss.getElementsByTagName('channel');
    for (var i = 0; i < roots.length; i++) {
        var rssdiv = document.createElement('div');
        goal.appendChild(rssdiv);
        rssdiv.setAttribute('id', 'rss' + i);

        var title = null;
        var link = null;
        var items = new Array();
        var image = new Array();
        var index = 0;

        for (var j = 0; j < roots[i].childNodes.length; j++) {
            if (roots[i].childNodes[j].tagName == 'title') {
                title = roots[i].childNodes[j].firstChild.nodeValue;
            }
            else if (roots[i].childNodes[j].tagName == 'link') {
                link = roots[i].childNodes[j].firstChild.nodeValue;
            }
            else if (roots[i].childNodes[j].tagName == 'image') {
                image = parseElement(roots[i].childNodes[j]);
            }
            else if (roots[i].childNodes[j].tagName == 'item') {
                items[index] = parseElement(roots[i].childNodes[j]);
                index++;
            }
        }

        var eltitle = document.createElement('h1');
        //rssdiv.appendChild(eltitle);
        var ellink = document.createElement('a');
        //eltitle.appendChild(ellink);
        //ellink.setAttribute('href', link);
        //ellink.setAttribute('title', title);
       // ellinktitle = document.createTextNode(title);
        //ellink.appendChild(ellinktitle);
        var elimgdiv = document.createElement('div');
        //rssdiv.appendChild(elimgdiv);
        //elimgdiv.setAttribute('id', 'rss' + i + '-image');
        //elimgdiv.setAttribute('class', 'rssimage');
        var elimg = document.createElement('img');
        //elimgdiv.appendChild(elimg);
        //elimg.setAttribute('src', image['url']);
        //elimg.setAttribute('alt', image['title']);
        var elitems = document.createElement('div');
        rssdiv.appendChild(elitems);
        //elitems.setAttribute('id', 'rss' + i + '-items');
        //elitems.setAttribute('class', 'rssitems');
		
		
        for (var j = 0; j < 5; j++) {
            var elitem = document.createElement('div');
            elitems.appendChild(elitem);
            //elitem.setAttribute('id', 'rss' + i + '-item' + j);
            //elitem.setAttribute('class', 'rssitem');
            
            
            //var elitemtitle = document.createElement('b');
            //elitemtitle.appendChild(elitemtitle);
            
            //var elitemlink = document.createElement('a');
            //elitemtitle.appendChild(elitemlink);
            //elitemlink.setAttribute('href', items[j]['link']);
            //elitemlink.setAttribute('title', items[j]['title']);
            //var elitemlinktitle = document.createTextNode(items[j]['title']);
            //elitemlink.appendChild(elitemlinktitle);
            
            // Titel
            var elitemtit = document.createElement('b');
            elitem.appendChild(elitemtit);
            var elitemlinktitle = document.createTextNode(items[j]['title']);
            elitemtit.appendChild(elitemlinktitle);
            
            
            //var elitemtexttext = document.createTextNode(items[j]['description']);
            //elitemtext.appendChild(elitemtexttext);
            
            
            //var elitemdate = document.createElement('span');
            //elitem.appendChild(elitemdate);
            //elitemdate.setAttribute('id', 'rss' + i + '-item' + j + '-date');
            //elitemdate.setAttribute('class', 'rssitemdate');
            //var elitemdatetext = document.createTextNode(items[j]['pubDate']);
            //elitemdate.appendChild(elitemdatetext);
            
            
            // Breaker
            //var elitemtext = document.createElement('div');
            //elitem.appendChild(elitemtext);
            //elitemtext.setAttribute('id', 'rss' + i + '-item' + j + '-content');
            //elitemtext.setAttribute('class', 'rssitemcontent');
            
            
            //var elitemtexttext = document.createTextNode(items[j]['description']);
            //elitemtext.appendChild(elitemtexttext);
            
            //var elitemmi = document.createElement('nobr');
            //elitem.appendChild(elitemmi);

			
            var stylert = document.createElement('div');
            elitem.appendChild(stylert);
            stylert.setAttribute('id', 'rss' + i + '-item' + j + '-content');
            stylert.setAttribute('class', 'overbill');
            
            var link = document.createElement('a');
            elitem.appendChild(link);
            link.setAttribute('href', items[j]['link']);
            link.setAttribute('title', items[j]['title']);
            link.setAttribute('class', 'a');
            link.setAttribute('target', '_blank');
            //var ltitle = 'meer info';
            var ltitle = document.createTextNode('\u00BB Lees verder \u00AB');
            link.appendChild(ltitle);

            
            //elitemmi.appendChild(elitemlink);
            //elitemmi.setAttribute('href', items[j]['link']);
            //elitemmi.setAttribute('title', 'test');
            //var elitemlinkt = 'meer info';
            //elitemlink.appendChild(elitemlinkt);
            
            var linebreak = document.createElement('br');
            elitem.appendChild(linebreak);
            
            var elimg = document.createElement('img');
        	elitem.appendChild(elimg);
        	elimg.setAttribute('src', 'http://exarchy.carerix.com/cx/line.gif');
            
            //var stylert = document.createElement('div');
            //elitem.appendChild(stylert);
            //stylert.setAttribute('id', 'rss' + i + '-item' + j + '-content');
            //stylert.setAttribute('class', 'oversplit');
            
            var stylert = document.createElement('div');
            elitem.appendChild(stylert);
            stylert.setAttribute('id', 'rss' + i + '-item' + j + '-content');
            stylert.setAttribute('class', 'overbill');
            
            //var linebreak = document.createElement('br');
           // elitem.appendChild(linebreak);
            //var linebreak = document.createElement('br');
            //elitem.appendChild(linebreak);
        }
    }
}

function parseElement(root) {
    var element = new Array();
    for (var i = 0; i < root.childNodes.length; i++) {
        if (root.childNodes[i].tagName) {
            element[root.childNodes[i].tagName] = root.childNodes[i].firstChild.nodeValue;
        }
    }

    return element;
}



/* Onload */
var feed = 'feed.xml';
var xmlhttprequest = createHTTPHandler();
window.onload = init;