/*
	@author: Nicola Poluzzi
	@website: http://www.poluz.net
	
	Copyright (C) 2007  Nicola Poluzzi

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


function getRSS () {
	// al caricamento, mostra il throbber per alleggerire l'attesa
	document.getElementById("rssdisplay").innerHTML += "Caricamento...";
	
	// carico l'xml chiamando lo script php
	if (window.ActiveXObject) //IE
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest) //altri
        xhr = new XMLHttpRequest();
    else
        document.getElementById("rssdisplay").innerHTML = "Si è verificato un errore nella ricezione delle informazioni";

    xhr.open("GET", "/blog/rssajax/getCachedRss.php", true);

    xhr.setRequestHeader("Cache-Control", "no-cache");
    xhr.setRequestHeader("Pragma", "no-cache");

    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                if (xhr.responseText != null) {
                    processRSS(xhr);
                } else {
                    document.getElementById("rssdisplay").innerHTML += "Impossibile ricevere le informazioni. Il server ha restituito una risposta vuota.";
                    return false;
                }
            } else {
                document.getElementById("rssdisplay").innerHTML += "Ricevuto un errore " + xhr.status + ": " + xhr.statusText;
            }
        }
    }

    xhr.send(null);
}

/////////////////////////////

// legge e stampa la prima notizia del feed rss
function processRSS (xmlhttp) {
	var rssxml = xmlhttp.responseXML;

    var elemento = rssxml.getElementsByTagName("item")[0];
    
    var descrizione = elemento.getElementsByTagName("description")[0].firstChild.nodeValue;
    //bonvar data = elemento.getElementsByTagName("pubDate")[0].firstChild.nodeValue;
    var link = elemento.getElementsByTagName("link")[0].firstChild.nodeValue;
    
    descrizione = descrizione.substring(7);
    var datalink = " <a href='" + link + "' style='margin-left:5px;'>#</a>";
    
    var rsshtml = descrizione + datalink;
    
    document.getElementById("rssdisplay").innerHTML = rsshtml;
    
    return true;
    
}
