function ajaxRequest(xmlFile, asyncFunction) {
//debugger;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    try {
        req.onreadystatechange = asyncFunction;
        req.open("GET", xmlFile, true);
        req.send(null);
    } catch (e) { alert(e); }
}

function loadGuestbook() {
    showGuestbook(null); 
}

function loadGuestbookThree() {
    showGuestbook(3);
}

function showGuestbook(maxEntries) {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
	        var response = req.responseXML.documentElement;
	        var entries=response.getElementsByTagName('entry');
	        //debugger;
	        if (maxEntries == null) {
	            maxEntries = entries.length;
	        } else {
	            maxEntries = (maxEntries>entries.length) ? entries.length : maxEntries;
	        }
	        
	        var p = document.getElementById('ajaxRendered');
            for (i=0;i<maxEntries;i++) {
                addGuestbookEntry(entries[i], p);
            }
        } else {
            //alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function addGuestbookEntry(entry, p) {
    var div = document.createElement('div');
    div.className = "entry";
    
    var name = document.createElement('class');
    name.className="name";
    name.appendChild(document.createTextNode(entry.getElementsByTagName('name')[0].firstChild.data));
    div.appendChild(name);
    
    var when = document.createElement('class');
    when.className="when";
    when.appendChild(document.createTextNode(entry.getElementsByTagName('date')[0].firstChild.data));
    div.appendChild(when);
    
    var message = document.createElement('class');
    message.className="comment";
    //debugger;
    var comment = entry.getElementsByTagName('comment')[0].firstChild.data;
    comment = unescape(comment);
    message.appendChild(document.createTextNode(comment));
    div.appendChild(message);
    
    p.appendChild(div);
}

function showCalendar() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
	        response = req.responseXML.documentElement;
	        var events=response.getElementsByTagName('evento');
		var p = document.getElementById('ajaxRendered');
//		modifica per ordine cronologico inverso
            var i = events.length -1;
	    while (i >= 0) {
                addEvent(events[i], p);
		i--;
            }
//	    for (i=0;i<events.length;i++) {
//                addEvent(events[i], p);
            //}
        } else {
            //alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function addEvent(evento, p) {
    var div = document.createElement('div');
    div.className = "event";
    
    var title = document.createElement('div');
    title.className="title";
    titleNode = document.createTextNode(evento.getElementsByTagName('oggetto')[0].firstChild.data);
    title.appendChild(titleNode);
    div.appendChild(title);
    
    var date =document.createElement('div');
    date.className="date";
    date.appendChild(document.createTextNode(evento.getElementsByTagName('data')[0].firstChild.data+' '+evento.getElementsByTagName('ora')[0].firstChild.data));
    div.appendChild(date);
    
    var durataElement = evento.getElementsByTagName('durata')[0];
    if (durataElement != null) {
	    var durata = document.createElement('div');
	    durata.className="durata";
	    durata.appendChild(document.createTextNode('Durata: '+durataElement.firstChild.data));
	    div.appendChild(durata);
    }
    
    var address = document.createElement('div');
    address.className="address";
    address.appendChild(document.createTextNode(evento.getElementsByTagName('indirizzo')[0].firstChild.data));
    div.appendChild(address);
    
    var costElement = evento.getElementsByTagName('costo')[0];
    if (costElement != null) {
	    var cost = document.createElement('div');
	    cost.className="cost";
	    cost.appendChild(document.createTextNode('Costo: '+costElement.firstChild.data));
	    div.appendChild(cost);
    }
    
    var noteElement = evento.getElementsByTagName('note')[0];
    if (noteElement!=null) {
	    var note = document.createElement('div');
	    note.className="note";
	    note.appendChild(document.createTextNode(noteElement.firstChild.data));
	    div.appendChild(note);
    }
    
    var linkElement = evento.getElementsByTagName('link')[0];
    if (linkElement!=null) {
        div.appendChild(document.createTextNode('Per maggiori informazioni visita il sito '));
        var linkDiv = document.createElement('div');
        var link = document.createElement('a');
        var url = linkElement.firstChild.data;
        note.className="link";
        var text = url;
        if (text.length > 40) {
        	//alert(text.substring(0,30).length);
       		text = " h"+text.substring(1,40) + "...";
        }
        link.appendChild(document.createTextNode(text));
        link.setAttribute('href', url);
        link.setAttribute('target','_blank');
        linkDiv.appendChild(link); 
        div.appendChild(linkDiv);
    }   
    
    p.appendChild(div);    
}

function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
	}

	function urldecode(str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}