﻿
function getXml(url, callback, options) {
    var req = createXMLHTTPObject();
    if (!req) return;
    postData = null;
    var method = (postData) ? "POST" : "GET";
    req.open(method, url, true);
    req.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
    if (postData)
        req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

    req.onreadystatechange = function () {
        if (req.readyState != 4) return;
        if (req.status != 200 && req.status != 304) {
            alert('HTTP error ' + req.status);
            return;
        }
        callback(req, options);
    }
    if (req.readyState == 4) return;
    req.send(postData);
}


var XMLHttpFactories = [
	function () { return new XMLHttpRequest() },
	function () { return new ActiveXObject("Msxml2.XMLHTTP") },
	function () { return new ActiveXObject("Msxml3.XMLHTTP") },
	function () { return new ActiveXObject("Microsoft.XMLHTTP") }
];

function createXMLHTTPObject() {
    var xmlhttp = false;
    for (var i = 0; i < XMLHttpFactories.length; i++) {
        try {
            xmlhttp = XMLHttpFactories[i]();
        }
        catch (e) {
            continue;
        }
        break;
    }
    return xmlhttp;
}

function parseMyDate(strDate) {
    // expected date input: "Mon, 23 Aug 2010 14:27:23 +0200"
    // wanted date output : "23. august 2010"
    pdDate = new Date(strDate);
    arrMonth = ['januar', 'februar', 'marts', 'april', 'maj', 'juni', 'juli', 'august', 'september', 'oktober', 'november', 'december'];
    strDay = pdDate.getDate();
    strMonth = arrMonth[pdDate.getMonth()];
    strYear = pdDate.getFullYear();
    return strDay + '. ' + strMonth + ' ' + strYear;
}

function doXml(req, options) {
    xmlDoc = req.responseXML;
    rssContainer = document.getElementById(options);
    if (rssContainer != null) {
        var x = xmlDoc.getElementsByTagName("item");
        for (i = 0; i < x.length; i++) {

            itemTitle = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
            itemDescription = x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
            itemLink = x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue;
            itemDate = x[i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue;

            if (itemDate != null) {
                strDate = parseMyDate(itemDate);
            } else {
                strDate = "-";
            }

            var theDiv = document.createElement('div');
            theDiv.className = 'rssItem';

            var theTitle = document.createElement('a');
            theTitle.className = 'rssTitle';
            theTitle.appendChild(document.createTextNode(itemTitle));
            theTitle.setAttribute('href', itemLink);
            theTitle.setAttribute('target', '_blank');
            theDiv.appendChild(theTitle);

            var theDate = document.createElement('div');
            theDate.className = 'rssDate';
            theDate.appendChild(document.createTextNode(strDate));
            theDiv.appendChild(theDate);

            var theText = document.createElement('div');
            theText.className = 'rssText';
            theText.innerHTML = itemDescription;
            theDiv.appendChild(theText);

            rssContainer.appendChild(theDiv);

        }
    }
}

