var targetElement;
var xmlHttpReq = false;
var tableType = 0;

function getRssFeed(feedUrl, enclosingEl, type) {
    tableType = type;
    targetElement = enclosingEl;
    xmlHttpReq=null;

    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
        try {
            xmlHttpReq = new XMLHttpRequest();
        } catch(e) {
            xmlHttpReq = false;
        }
        // IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
        try {
            xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                xmlHttpReq = false;
            }
        }
    }


    if(xmlHttpReq) {
        xmlHttpReq.onreadystatechange=feedReqStateChanged;
        xmlHttpReq.open("GET",feedUrl,true);
        xmlHttpReq.send(null);
    }
}



function feedReqStateChanged() {
    if (xmlHttpReq.readyState==4) {
        if (xmlHttpReq.status==200) {
            var xml = xmlHttpReq.responseXML;
            if(xml) {
                if(tableType == 0) {
                    buildHeadlineTable(xml);
                } else {
                    buildFullTable(xml);
                }
            }
        }
    }
}

function buildHeadlineTable(xml) {
    var table = document.createElement("table");
    table.setAttribute("class", "rssMainTable");

    //var tableTitle = xml.getElementsByTagName("title")[0].childNodes[0].nodeValue;

    //var tableTitleRow = document.createElement("tr");
    //tableTitleRow.setAttribute("class", "rssMainTitleRow");
    //var tableTitleCell = document.createElement("td");
    //tableTitleCell.setAttribute("class", "rssMainTitleCell");
    //tableTitleCell.appendChild(document.createTextNode(tableTitle));
    //tableTitleRow.appendChild(tableTitleCell);
    //table.appendChild(tableTitleRow);

    var items = xml.getElementsByTagName("item");
    for(var i=0; i<items.length; i++){
        var item = items[i];

        var title = item.getElementsByTagName("title")[0].childNodes[0].nodeValue;
        //var category = item.getElementsByTagName("category")[0].childNodes[0].nodeValue;
        //var creator = item.getElementsByTagName("dc:creator")[0].childNodes[0].nodeValue;
        //var pubDate = item.getElementsByTagName("pubDate")[0].childNodes[0].nodeValue;
        var link = item.getElementsByTagName("link")[0].childNodes[0].nodeValue;
        //var description = item.getElementsByTagName("description")[0].childNodes[0].nodeValue;

        var itemTable = document.createElement("table");
        itemTable.setAttribute("class", "rssItemTable");

        var titleBody = document.createElement('tbody');
        var titleRow = document.createElement("tr");
        var titleCell = document.createElement("td");
        titleCell.setAttribute("class", "rssItemCell");
        var linkAnchor = document.createElement("a");
        linkAnchor.setAttribute("target", "_blank");
        linkAnchor.setAttribute("href", link);
        linkAnchor.appendChild(document.createTextNode(title));
        titleCell.appendChild(linkAnchor);
        titleRow.appendChild(titleCell);
        titleBody.appendChild(titleRow)
        itemTable.appendChild(titleBody);


        var itemBody = document.createElement('tbody');
        var itemRow = document.createElement("tr");
        var itemCell = document.createElement("td");
        itemRow.appendChild(itemCell);
        itemBody.appendChild(itemRow);

        itemCell.appendChild(itemTable);
        table.appendChild(itemBody);
    }
    targetElement.appendChild(table);
}

function buildFullTable(xml) {
    var table = document.createElement("table");

    var tableTitle = xml.getElementsByTagName("title")[0].childNodes[0].nodeValue;
    var tableTitleRow = document.createElement("tr");
    tableTitleRow.setAttribute("class", "rssMainTitleRow");
    var tableTitleCell = document.createElement("td");
    tableTitleCell.setAttribute("class", "rssMainTitleCell");
    tableTitleCell.appendChild(document.createTextNode(tableTitle));
    tableTitleRow.appendChild(tableTitleCell);
    table.appendChild(tableTitleRow);

    var items = xml.getElementsByTagName("item");
    for(var i=0; i<items.length; i++){
        var item = items[i];

        var title = item.getElementsByTagName("title")[0].childNodes[0].nodeValue;
        //var category = item.getElementsByTagName("category")[0].childNodes[0].nodeValue;
        //var creator = item.getElementsByTagName("dc:creator")[0].childNodes[0].nodeValue;
        var pubDate = item.getElementsByTagName("pubDate")[0].childNodes[0].nodeValue;
        //var link = item.getElementsByTagName("link")[0].childNodes[0].nodeValue;
        var description = item.getElementsByTagName("description")[0].childNodes[0].nodeValue;

        var itemTable = document.createElement("table");
        itemTable.setAttribute("class", "rssItemTable");

        var date = new Date(pubDate);
        var dateRow = document.createElement("tr");
        var dateCell = document.createElement("td");
        dateCell.setAttribute("class", "rssItemCell");
        dateCell.appendChild(document.createTextNode(date.toLocaleString()));
        dateRow.appendChild(dateCell);
        itemTable.appendChild(dateRow);

        var titleRow = document.createElement("tr");
        var titleCell = document.createElement("td");
        titleCell.setAttribute("class", "rssItemCell");
        titleCell.appendChild(document.createTextNode(title));
        titleRow.appendChild(titleCell);
        itemTable.appendChild(titleRow);

        var descriptionRow = document.createElement("tr");
        var descriptionCell = document.createElement("td");
        descriptionCell.setAttribute("class", "rssItemCell");
        descriptionCell.innerHTML = description;
        descriptionRow.appendChild(descriptionCell);
        itemTable.appendChild(descriptionRow);

        var itemRow = document.createElement("tr");
        var itemCell = document.createElement("td");
        itemRow.appendChild(itemCell);

        itemCell.appendChild(itemTable);
        table.appendChild(itemRow);
    }
    targetElement.appendChild(table);
}
