// GLOBAL VARIABLES
var pathPrefix;
var gIsAdmin;
var gQuestWindow;
var gIncidWindow;

var selMessageId;
var popupWindow;

function createXmlHttp() {
    var xmlhttp
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
        try {
            xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
        } catch (e) {
            try {
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
            } catch (E) {
                xmlhttp=false
            }
        }
    @else
        xmlhttp=false
    @end @*/
    
    if (!xmlhttp) {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp=false
        }
    }
    
    return xmlhttp
}

function createXmlDoc(xmltxt) {
    if (typeof parseXML=='undefined') {
        try {
            xml = new ActiveXObject("Microsoft.XMLDOM");
            xml.async=false
                    xml.validateOnParse=false
                    xml.resolveExternals=false
                    xml.loadXML(xmltxt)
        } catch (e) {
                try {
                // This makes konqueror to throw an exception
                    try {
                        Document.prototype.loadXML = function (s) {
                        // parse the string to a new doc   
                            var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
                        
                        // remove all initial children
                            while (this.hasChildNodes()) {
                                this.removeChild(this.lastChild);
                                if (this.lastChild == null) {
                                    break;
                                }
                            }
                        
                        // insert and import nodes
                            for (var i = 0; i < doc2.childNodes.length; i++) {
                                this.appendChild(this.importNode(doc2.childNodes[i], true));
                            }
                        }
                    } catch(ex) {
                    //alert("None esiste l'oggetto Document. " + ex);
                    }
                
                    xml=document.implementation.createDocument('', '', null);
                    xml.loadXML(xmltxt);
                } catch (e) {
                    alert(e);
                //if (typeof alert!=null) alert("OK, I give up, you're not ASV, Batik, IE or\n a Mozilla build or anything else a bit like them.")
                }
        }
    } else {
        xml = parseXML(xmltxt, null)
    }
    
    return xml;
}

function trimString(sInString) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
}

function removeAllChildNodes(aNode) {
    while (aNode.hasChildNodes()) {
        aNode.removeChild(aNode.lastChild);
        if (aNode.lastChild == null) {
            break;
        }
    }
}

function removeAllChildNodesById(nodeId) {
    nodeToClear = document.getElementById(nodeId);
    removeAllChildNodes(nodeToClear);
}

function hideDiv(divId) {
    divToHide = document.getElementById(divId);
    divToHide.style.display = "none";
}

function cercaAnnunciJSON(isAdmin) {
    pathPrefix = "";
    gIsAdmin = false;
    if (isAdmin) {
        pathPrefix = "../";
        gIsAdmin = isAdmin;
    }
    
    var reqOpt = {
    	// Use POST
        method: 'post',
        
        // Send this lovely data
        postBody: 'isAdmin=' + isAdmin,
        
        // Handle successful response
        onSuccess: cercaAnnunciJSON_callback,
        
        // Handle 404
        on404: function(t) {
            alert('Error 404: location "' + t.statusText + '" was not found.');
        },
        // Handle other errors
        onFailure: function(t) {
            alert('Error ' + t.status + ' -- ' + t.statusText);
        }
    };
	
    var myAjax = new Ajax.Request(pathPrefix + "listaannunci.php", reqOpt);
}

function cercaAnnunciJSON_callback(originalRequest) {
    try {
    // var response = eval( '(' + originalRequest.responseText.gsub('\r\n', '\\r\\n') + ')' );
    var response = originalRequest.responseText.evalJSON();
    } catch(e) {
        alert(e);
    }
    
    if (response.status != "ERROR") {
        var values = response.values;
        
        // clears previous results
        removeAllChildNodes($("tblBodyAnnunci"));
        
        for (i = 0; i < values.length; i++) {
            var objannuncio = values[i];
            
            var trElement = document.createElement("tr");
            
            // data annuncio
            var tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            tdElement.appendChild(document.createTextNode(objannuncio.data_inserimento));
            trElement.appendChild(tdElement);
            
            if (gIsAdmin) {
                // nome utente
                tdElement = document.createElement("td");
                tdElement.className = "rigaTabella";
                tdElement.appendChild(document.createTextNode(objannuncio.nome_utente));
                trElement.appendChild(tdElement);
                
                // email
                tdElement = document.createElement("td");
                tdElement.className = "rigaTabella";
                tdElement.appendChild(document.createTextNode(objannuncio.email));
                trElement.appendChild(tdElement);
            }
            
            // testo annuncio
            var preElement = document.createElement("pre");
            preElement.appendChild(document.createTextNode(objannuncio.testo));
            
            tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            // tdElement.appendChild(document.createTextNode(objannuncio.testo));
            tdElement.appendChild(preElement);
            trElement.appendChild(tdElement);
            
            if (gIsAdmin) {
                // flag attivo/disattivo
                tdElement = document.createElement("td");
                tdElement.className = "rigaTabella";
                tdElement.appendChild(document.createTextNode(objannuncio.attivo));
                trElement.appendChild(tdElement);
            }
            
            // azioni
            
            // reply
            tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            
            var linkElement = document.createElement("a");
            linkElement.setAttribute("href", "javascript: rispondiAnnuncio(" + objannuncio.id_annuncio + "," + gIsAdmin + ")");
            
            var imageElement = document.createElement("img");
            imageElement.setAttribute("src", pathPrefix + "images/reply.png");
            imageElement.setAttribute("border", "0");
            imageElement.setAttribute("alt", "rispondi all'annuncio");
            imageElement.setAttribute("title", "rispondi all'annuncio");
            imageElement.setAttribute("hspace", "2");
            imageElement.setAttribute("align", "left");
            
            linkElement.appendChild(imageElement);
            tdElement.appendChild(linkElement);
            trElement.appendChild(tdElement);
            
            if (gIsAdmin) {
                // enable
                linkElement = document.createElement("a");
                linkElement.setAttribute("href", "javascript: abilitaDisabilitaAnnuncioJSON(" + objannuncio.id_annuncio + ", \"enable\");");
                
                imageElement = document.createElement("img");
                imageElement.setAttribute("src", pathPrefix + "images/enable.png");
                imageElement.setAttribute("border", "0");
                imageElement.setAttribute("alt", "abilita  l'annuncio");
                imageElement.setAttribute("title", "abilita l'annuncio");
                imageElement.setAttribute("hspace", "2");
                imageElement.setAttribute("align", "left");
                
                linkElement.appendChild(imageElement);
                tdElement.appendChild(linkElement);
                trElement.appendChild(tdElement);
                
                // disable
                linkElement = document.createElement("a");
                linkElement.setAttribute("href", "javascript: abilitaDisabilitaAnnuncioJSON(" + objannuncio.id_annuncio + ", \"disable\");");
                
                imageElement = document.createElement("img");
                imageElement.setAttribute("src", pathPrefix + "images/disable.png");
                imageElement.setAttribute("border", "0");
                imageElement.setAttribute("alt", "disabilita  l'annuncio");
                imageElement.setAttribute("title", "disabilita l'annuncio");
                imageElement.setAttribute("hspace", "2");
                imageElement.setAttribute("align", "left");
                
                linkElement.appendChild(imageElement);
                tdElement.appendChild(linkElement);
                trElement.appendChild(tdElement);
                
                // delete
                linkElement = document.createElement("a");
                linkElement.setAttribute("href", "javascript: eliminaAnnuncioJSON(" + objannuncio.id_annuncio + ");");
                
                imageElement = document.createElement("img");
                imageElement.setAttribute("src", pathPrefix + "images/delete.png");
                imageElement.setAttribute("border", "0");
                imageElement.setAttribute("alt", "elimina  l'annuncio");
                imageElement.setAttribute("title", "elimina l'annuncio");
                imageElement.setAttribute("hspace", "2");
                imageElement.setAttribute("align", "left");
                
                linkElement.appendChild(imageElement);
                tdElement.appendChild(linkElement);
                trElement.appendChild(tdElement);
            }
            
            $("tblBodyAnnunci").appendChild(trElement);
        }
    } else {
        alert("Errore nelle operazioni di ricerca.");
    }
}

function cercaAnnunci(isAdmin) {
    xmlhttp = createXmlHttp();
    var pathPrefix = "";
    if (isAdmin) {
        pathPrefix = "../";
    }
    
    xmlhttp.open("POST", pathPrefix + "listaannunci.php", true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            
            xmlResponse = createXmlDoc(xmlhttp.responseText);
            
            // status node
            statusNode = xmlResponse.getElementsByTagName("status").item(0);
            statusNodeValue = (statusNode.hasChildNodes()) ? statusNode.firstChild.nodeValue : "";
            
            // message node
            messageNode = xmlResponse.getElementsByTagName("message").item(0);
            messageNodeValue = (messageNode.hasChildNodes()) ? messageNode.firstChild.nodeValue : "";
            //alert(messageNodeValue);
            
            if (statusNodeValue != "ERROR") {
                // data node
                dataNode = xmlResponse.getElementsByTagName("data").item(0);
                dataNodeValue = (dataNode.hasChildNodes()) ? dataNode.firstChild.nodeValue : "";
                
                // results table body element
                tableBody = document.getElementById("tblBodyAnnunci");
                    
                // clears previous results
                removeAllChildNodes(tableBody);
                
                if (dataNode.hasChildNodes()) {
                    for (i = 0; i < dataNode.childNodes.length; i++) {
                        currentNode = dataNode.childNodes.item(i);
                        // is this a NODE type?
                        if (currentNode.nodeType == 1 && currentNode.nodeName == "row") {
                            // scans columns
                            if (currentNode.hasChildNodes()) {
                                row = document.createElement("tr");
                                
                                tableBody.appendChild(row);
                                
                                for (z = 0; z < currentNode.childNodes.length; z++) {
                                    columnNode = currentNode.childNodes.item(z);
                                    
                                    if (columnNode.nodeType == 1 && columnNode.nodeName == "column") {
                                        columnNodeValue = (columnNode.hasChildNodes && columnNode.firstChild != null) ? columnNode.firstChild.nodeValue : "";
                                        
                                        colName = columnNode.getAttribute("name");
                                        
                                        if (isAdmin ||
                                            (colName != "nome_utente" && colName != "email" && 
                                            colName != "attivo")) {
                                            
                                            col = document.createElement("td");
                                            col.setAttribute("class", "rigaTabella");
                                            col.className = "rigaTabella";
                                            // col.setAttribute("style", "border-bottom-color : #aaaaaa; border-bottom-style : solid; border-bottom-width : 1px; vertical-align : top;");
                                            
                                            if (colName == "id_annuncio") {
                                                col.setAttribute("align", "right");
                                                
                                                /////////////
                                                /// REPLY ///
                                                /////////////
                                                link = document.createElement("a");
                                                link.setAttribute("href", "javascript: rispondiAnnuncio(" + columnNodeValue + "," + isAdmin + ")");
                                                
                                                image = document.createElement("img");
                                                image.setAttribute("src", pathPrefix + "images/reply.png");
                                                image.setAttribute("border", "0");
                                                image.setAttribute("alt", "rispondi all'annuncio");
                                                image.setAttribute("title", "rispondi all'annuncio");
                                                image.setAttribute("hspace", "2");
                                                image.setAttribute("align", "left");
                                                
                                                link.appendChild(image);
                                                col.appendChild(link);
                                                
                                                if (isAdmin) {
                                                    //////////////
                                                    /// ENABLE ///
                                                    //////////////
                                                    link = document.createElement("a");
                                                    link.setAttribute("href", "javascript: abilitaDisabilitaAnnuncio(" + columnNodeValue + ", \"enable\");");
                                                    
                                                    image = document.createElement("img");
                                                    image.setAttribute("src", pathPrefix + "images/enable.png");
                                                    image.setAttribute("border", "0");
                                                    image.setAttribute("alt", "abilita  l'annuncio");
                                                    image.setAttribute("title", "abilita l'annuncio");
                                                    image.setAttribute("hspace", "2");
                                                    image.setAttribute("align", "left");
                                                    
                                                    link.appendChild(image);
                                                    col.appendChild(link);
                                                    
                                                    ///////////////
                                                    /// DISABLE ///
                                                    ///////////////
                                                    link = document.createElement("a");
                                                    link.setAttribute("href", "javascript: abilitaDisabilitaAnnuncio(" + columnNodeValue + ", \"disable\");");
                                                    
                                                    image = document.createElement("img");
                                                    image.setAttribute("src", pathPrefix + "images/disable.png");
                                                    image.setAttribute("border", "0");
                                                    image.setAttribute("alt", "disabilita  l'annuncio");
                                                    image.setAttribute("title", "disabilita l'annuncio");
                                                    image.setAttribute("hspace", "2");
                                                    image.setAttribute("align", "left");
                                                    
                                                    link.appendChild(image);
                                                    col.appendChild(link);
                                                    
                                                    //////////////
                                                    /// DELETE ///
                                                    //////////////
                                                    link = document.createElement("a");
                                                    link.setAttribute("href", "javascript: eliminaAnnuncio(" + columnNodeValue + ");");
                                                    
                                                    image = document.createElement("img");
                                                    image.setAttribute("src", pathPrefix + "images/delete.png");
                                                    image.setAttribute("border", "0");
                                                    image.setAttribute("alt", "elimina  l'annuncio");
                                                    image.setAttribute("title", "elimina l'annuncio");
                                                    image.setAttribute("hspace", "2");
                                                    image.setAttribute("align", "left");
                                                    
                                                    link.appendChild(image);
                                                    col.appendChild(link);
                                                }
                                                
                                            } else {
                                                col.appendChild(document.createTextNode(columnNodeValue));
                                            }
                                            
                                            row.appendChild(col);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                alert("Errore nelle operazioni di ricerca.");
            }
        }
    }
    //alert("admin=" + isAdmin);
    xmlhttp.send("isAdmin=" + isAdmin);
}

function cercaCommentiJSON(isAdmin) {
    pathPrefix = "";
    gIsAdmin = false;
    if (isAdmin) {
        pathPrefix = "../";
        gIsAdmin = isAdmin;
    }
    
    var reqOpt = {
    	// Use POST
        method: 'post',
        
        // Send this lovely data
        postBody: 'isAdmin=' + isAdmin,
        
        // Handle successful response
        onSuccess: cercaCommentiJSON_callback,
        
        // Handle 404
        on404: function(t) {
            alert('Error 404: location "' + t.statusText + '" was not found.');
        },
        // Handle other errors
        onFailure: function(t) {
            alert('Error ' + t.status + ' -- ' + t.statusText);
        }
    };
    
    var myAjax = new Ajax.Request(pathPrefix + "listacommenti.php", reqOpt);
}

function cercaCommentiJSON_callback(originalRequest) {
    var response = eval( '(' + originalRequest.responseText + ')' );
    
    if (response.status != "ERROR") {
        var values = response.values;
        
        // clears previous results
        removeAllChildNodes($("tblBodyCommenti"));
        
        for (i = 0; i < values.length; i++) {
            var objcommento = values[i];
            
            var trElement = document.createElement("tr");
            
            $("tblBodyCommenti").appendChild(trElement);
            
            // data commento
            var tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            tdElement.appendChild(document.createTextNode(objcommento.data_inserimento));
            trElement.appendChild(tdElement);
            
            // nome utente
            tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            tdElement.appendChild(document.createTextNode(objcommento.nome));
            trElement.appendChild(tdElement);
            
            // testo commento
            tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            tdElement.appendChild(document.createTextNode(objcommento.messaggio));
            trElement.appendChild(tdElement);
            
            // attivo
            tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            tdElement.appendChild(document.createTextNode(objcommento.attivo));
            trElement.appendChild(tdElement);
            
            // azioni
            if (gIsAdmin) {
                tdElement = document.createElement("td");
                tdElement.className = "rigaTabella";
                
                // enable
                var linkElement = document.createElement("a");
                linkElement.setAttribute("href", "javascript: abilitaDisabilitaCommentoJSON(" + objcommento.id_commento + ", \"enable\");");
                
                var imageElement = document.createElement("img");
                imageElement.setAttribute("src", pathPrefix + "images/enable.png");
                imageElement.setAttribute("border", "0");
                imageElement.setAttribute("alt", "abilita  l'annuncio");
                imageElement.setAttribute("title", "abilita l'annuncio");
                imageElement.setAttribute("hspace", "2");
                imageElement.setAttribute("align", "left");
                
                linkElement.appendChild(imageElement);
                tdElement.appendChild(linkElement);
                
                // disable
                linkElement = document.createElement("a");
                linkElement.setAttribute("href", "javascript: abilitaDisabilitaCommentoJSON(" + objcommento.id_commento + ", \"disable\");");
                
                imageElement = document.createElement("img");
                imageElement.setAttribute("src", pathPrefix + "images/disable.png");
                imageElement.setAttribute("border", "0");
                imageElement.setAttribute("alt", "disabilita  l'annuncio");
                imageElement.setAttribute("title", "disabilita l'annuncio");
                imageElement.setAttribute("hspace", "2");
                imageElement.setAttribute("align", "left");
                
                linkElement.appendChild(imageElement);
                tdElement.appendChild(linkElement);
                
                // delete
                linkElement = document.createElement("a");
                linkElement.setAttribute("href", "javascript: eliminaCommentoJSON(" + objcommento.id_commento + ");");
                
                imageElement = document.createElement("img");
                imageElement.setAttribute("src", pathPrefix + "images/delete.png");
                imageElement.setAttribute("border", "0");
                imageElement.setAttribute("alt", "elimina  l'annuncio");
                imageElement.setAttribute("title", "elimina l'annuncio");
                imageElement.setAttribute("hspace", "2");
                imageElement.setAttribute("align", "left");
                
                linkElement.appendChild(imageElement);
                tdElement.appendChild(linkElement);
                
                trElement.appendChild(tdElement);
            }
        }
    }
}

function cercaCommenti(isAdmin) {
    xmlhttp = createXmlHttp();
    var pathPrefix = "";
    if (isAdmin) {
        pathPrefix = "../";
    }
    
    xmlhttp.open("POST", pathPrefix + "listacommenti.php", true);
    
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            xmlResponse = createXmlDoc(xmlhttp.responseText);
            
            // status node
            statusNode = xmlResponse.getElementsByTagName("status").item(0);
            statusNodeValue = (statusNode.hasChildNodes()) ? statusNode.firstChild.nodeValue : "";
            
            // message node
            messageNode = xmlResponse.getElementsByTagName("message").item(0);
            messageNodeValue = (messageNode.hasChildNodes()) ? messageNode.firstChild.nodeValue : "";
            //alert(messageNodeValue);
            
            if (statusNodeValue != "ERROR") {
                // data node
                dataNode = xmlResponse.getElementsByTagName("data").item(0);
                dataNodeValue = (dataNode.hasChildNodes()) ? dataNode.firstChild.nodeValue : "";
                
                // results table body element
                tableBody = document.getElementById("tblBodyCommenti");
                    
                // clears previous results
                removeAllChildNodes(tableBody);
                
                if (dataNode.hasChildNodes()) {
                    for (i = 0; i < dataNode.childNodes.length; i++) {
                        currentNode = dataNode.childNodes.item(i);
                        // is this a NODE type?
                        if (currentNode.nodeType == 1 && currentNode.nodeName == "row") {
                            // scans columns
                            if (currentNode.hasChildNodes()) {
                                row = document.createElement("tr");
                                
                                tableBody.appendChild(row);
                                
                                for (z = 0; z < currentNode.childNodes.length; z++) {
                                    columnNode = currentNode.childNodes.item(z);
                                    
                                    if (columnNode.nodeType == 1 && columnNode.nodeName == "column") {
                                        columnNodeValue = (columnNode.hasChildNodes && columnNode.firstChild != null) ? columnNode.firstChild.nodeValue : "";
                                        
                                        colName = columnNode.getAttribute("name");
                                        
                                        if (isAdmin || (colName != "attivo")) {
                                            
                                            col = document.createElement("td");
                                            col.setAttribute("class", "rigaTabella");
                                            col.className = "rigaTabella";
                                            
                                            if (colName == "id_commento") {
                                                col.setAttribute("align", "right");
                                                
                                                if (isAdmin) {
                                                    //////////////
                                                    /// ENABLE ///
                                                    //////////////
                                                    link = document.createElement("a");
                                                    link.setAttribute("href", "javascript: abilitaDisabilitaCommento(" + columnNodeValue + ", \"enable\");");
                                                    
                                                    image = document.createElement("img");
                                                    image.setAttribute("src", pathPrefix + "images/enable.png");
                                                    image.setAttribute("border", "0");
                                                    image.setAttribute("alt", "abilita  l'annuncio");
                                                    image.setAttribute("title", "abilita l'annuncio");
                                                    image.setAttribute("hspace", "2");
                                                    image.setAttribute("align", "left");
                                                    
                                                    link.appendChild(image);
                                                    col.appendChild(link);
                                                    
                                                    ///////////////
                                                    /// DISABLE ///
                                                    ///////////////
                                                    link = document.createElement("a");
                                                    link.setAttribute("href", "javascript: abilitaDisabilitaCommento(" + columnNodeValue + ", \"disable\");");
                                                    
                                                    image = document.createElement("img");
                                                    image.setAttribute("src", pathPrefix + "images/disable.png");
                                                    image.setAttribute("border", "0");
                                                    image.setAttribute("alt", "disabilita  l'annuncio");
                                                    image.setAttribute("title", "disabilita l'annuncio");
                                                    image.setAttribute("hspace", "2");
                                                    image.setAttribute("align", "left");
                                                    
                                                    link.appendChild(image);
                                                    col.appendChild(link);
                                                    
                                                    //////////////
                                                    /// DELETE ///
                                                    //////////////
                                                    link = document.createElement("a");
                                                    link.setAttribute("href", "javascript: eliminaCommento(" + columnNodeValue + ");");
                                                    
                                                    image = document.createElement("img");
                                                    image.setAttribute("src", pathPrefix + "images/delete.png");
                                                    image.setAttribute("border", "0");
                                                    image.setAttribute("alt", "elimina  l'annuncio");
                                                    image.setAttribute("title", "elimina l'annuncio");
                                                    image.setAttribute("hspace", "2");
                                                    image.setAttribute("align", "left");
                                                    
                                                    link.appendChild(image);
                                                    col.appendChild(link);
                                                }
                                            } else {
                                                col.appendChild(document.createTextNode(columnNodeValue));
                                            }
                                            
                                            row.appendChild(col);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                alert("Errore nelle operazioni di ricerca.");
            }
        }
    }
    
    xmlhttp.send("");
}

function riempiQuestionarioJSON(idQuestionario, questWindow) {
    pathPrefix = "../";
    gIsAdmin = false;
    gQuestWindow = questWindow;
    
    var reqOpt = {
    	// Use POST
        method: 'post',
        
        // Send this lovely data
        postBody: 'mode=view&id_questionario=' + idQuestionario,
        
        // Handle successful response
        onSuccess: riempiQuestionarioJSON_callback,
        
        // Handle 404
        on404: function(t) {
            alert('Error 404: location "' + t.statusText + '" was not found.');
        },
        // Handle other errors
        onFailure: function(t) {
            alert('Error ' + t.status + ' -- ' + t.statusText);
        }
    };
    
    var myAjax = new Ajax.Request(pathPrefix + "listaquestionari.php", reqOpt);
}

function riempiQuestionarioJSON_callback(originalRequest) {
    var response = eval( '(' + originalRequest.responseText + ')' );
    
    if (response.status != "ERROR") {
        var values = response.values;
        
        for (i = 0; i < values.length; i++) {
            var objquestionario = values[i];
            
            gQuestWindow.$("txtName").appendChild(document.createTextNode(objquestionario.nominativo));
            setRadioValue(gQuestWindow.document.getElementsByName("rdnQ1"), objquestionario.q1);
            setRadioValue(gQuestWindow.document.getElementsByName("rdnQ2"), objquestionario.q2);
            setRadioValue(gQuestWindow.document.getElementsByName("rdnQ3"), objquestionario.q3);
            setRadioValue(gQuestWindow.document.getElementsByName("rdnQ4"), objquestionario.q4);
            setRadioValue(gQuestWindow.document.getElementsByName("rdnQ5"), objquestionario.q5);
            setRadioValue(gQuestWindow.document.getElementsByName("rdnQ6"), objquestionario.q6);
            gQuestWindow.$("txtQ7").appendChild(document.createTextNode(objquestionario.t7));
            gQuestWindow.$("txtQ8").appendChild(document.createTextNode(objquestionario.t8));
            gQuestWindow.$("txtQ9").appendChild(document.createTextNode(objquestionario.t9));
            setRadioValue(gQuestWindow.document.getElementsByName("rdnQ10"), objquestionario.q10);
        }
    }
}

function riempiQuestionario(idQuestionario, questWindow) {
    xmlhttp = createXmlHttp()
    xmlhttp.open("POST", "../listaquestionari.php", true);
    
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            //alert(xmlhttp.responseText);
            xmlResponse = createXmlDoc(xmlhttp.responseText);
            
            // status node
            statusNode = xmlResponse.getElementsByTagName("status").item(0);
            statusNodeValue = (statusNode.hasChildNodes()) ? statusNode.firstChild.nodeValue : "";
            
            // message node
            messageNode = xmlResponse.getElementsByTagName("message").item(0);
            messageNodeValue = (messageNode.hasChildNodes()) ? messageNode.firstChild.nodeValue : "";
            //alert(messageNodeValue);
            
            if (statusNodeValue != "ERROR") {
                // data node
                dataNode = xmlResponse.getElementsByTagName("data").item(0);
                dataNodeValue = (dataNode.hasChildNodes()) ? dataNode.firstChild.nodeValue : "";
                
                if (dataNode.hasChildNodes()) {
                    
                    for (i = 0; i < dataNode.childNodes.length; i++) {
                        currentNode = dataNode.childNodes.item(i);
                        // is this a NODE type?
                        if (currentNode.nodeType == 1 && currentNode.nodeName == "row") {
                            // scans columns
                            if (currentNode.hasChildNodes()) {
                                
                                for (z = 0; z < currentNode.childNodes.length; z++) {
                                    columnNode = currentNode.childNodes.item(z);
                                    
                                    if (columnNode.nodeType == 1 && columnNode.nodeName == "column") {
                                        columnNodeValue = (columnNode.hasChildNodes && columnNode.firstChild != null) ? columnNode.firstChild.nodeValue : "";
                                        
                                        colName = columnNode.getAttribute("name");
                                        //alert(colName);
                                        if (colName == "nominativo") {
                                            questWindow.document.getElementById("txtName").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "q1") {
                                            setRadioValue(questWindow.document.getElementsByName("rdnQ1"), columnNodeValue);
                                        }
                                        if (colName == "q2") {
                                            setRadioValue(questWindow.document.getElementsByName("rdnQ2"), columnNodeValue);
                                        }
                                        if (colName == "q3") {
                                            setRadioValue(questWindow.document.getElementsByName("rdnQ3"), columnNodeValue);
                                        }
                                        if (colName == "q4") {
                                            setRadioValue(questWindow.document.getElementsByName("rdnQ4"), columnNodeValue);
                                        }
                                        if (colName == "q5") {
                                            setRadioValue(questWindow.document.getElementsByName("rdnQ5"), columnNodeValue);
                                        }
                                        if (colName == "q6") {
                                            setRadioValue(questWindow.document.getElementsByName("rdnQ6"), columnNodeValue);
                                        }
                                        if (colName == "t7") {
                                            questWindow.document.getElementById("txtQ7").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t8") {
                                            questWindow.document.getElementById("txtQ8").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t9") {
                                            questWindow.document.getElementById("txtQ9").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "q10") {
                                            setRadioValue(questWindow.document.getElementsByName("rdnQ10"), columnNodeValue);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                alert("Errore nelle operazioni di ricerca.");
            }
        }
    }
    
    xmlhttp.send("mode=view&id_questionario=" + idQuestionario);
}

function riempiIncidenteJSON(idIncidente, incidWindow) {
    pathPrefix = "../";
    gIsAdmin = false;
    gIncidWindow = incidWindow;
    
    var reqOpt = {
    	// Use POST
        method: 'post',
        
        // Send this lovely data
        postBody: 'mode=view&id_incidente=' + idIncidente,
        
        // Handle successful response
        onSuccess: riempiIncidenteJSON_callback,
        
        // Handle 404
        on404: function(t) {
            alert('Error 404: location "' + t.statusText + '" was not found.');
        },
        // Handle other errors
        onFailure: function(t) {
            alert('Error ' + t.status + ' -- ' + t.statusText);
        }
    };
    
    var myAjax = new Ajax.Request(pathPrefix + "listaincidenti.php", reqOpt);
}

function riempiIncidenteJSON_callback(originalRequest) {
    var response = eval( '(' + originalRequest.responseText + ')' );
    
    if (response.status != "ERROR") {
        var values = response.values;
        
        for (i = 0; i < values.length; i++) {
            var objincidente = values[i];
            
            setRadioValue(gIncidWindow.document.getElementsByName("rdnAccidentQ1"), objincidente.t1);
            gIncidWindow.$("txtAccidentQ2").appendChild(document.createTextNode(objincidente.d2));
            gIncidWindow.$("txtAccidentQ3").appendChild(document.createTextNode(objincidente.t3));
            gIncidWindow.$("txtAccidentQ4").appendChild(document.createTextNode(objincidente.t4));
            gIncidWindow.$("txtAccidentQ5").appendChild(document.createTextNode(objincidente.t5));
            gIncidWindow.$("txtAccidentQ6").appendChild(document.createTextNode(objincidente.t6));
            gIncidWindow.$("txtAccidentQ7").appendChild(document.createTextNode(objincidente.t7));
            gIncidWindow.$("txtAccidentQ8").appendChild(document.createTextNode(objincidente.t8));
            gIncidWindow.$("txtAccidentQ9").appendChild(document.createTextNode(objincidente.t9));
            gIncidWindow.$("txtAccidentQ10").appendChild(document.createTextNode(objincidente.t10));
            gIncidWindow.$("txtAccidentQ11").appendChild(document.createTextNode(objincidente.t11));
            gIncidWindow.$("txtAccidentQ12").appendChild(document.createTextNode(objincidente.t12));
            gIncidWindow.$("txtAccidentQ13").appendChild(document.createTextNode(objincidente.t13));
            gIncidWindow.$("txtAccidentQ14").appendChild(document.createTextNode(objincidente.t14));
            gIncidWindow.$("txtAccidentQ15").appendChild(document.createTextNode(objincidente.t15));
            gIncidWindow.$("txtAccidentQ16").appendChild(document.createTextNode(objincidente.t16));
            gIncidWindow.$("txtAccidentQ17").appendChild(document.createTextNode(objincidente.t17));
            gIncidWindow.$("txtAccidentQ18").appendChild(document.createTextNode(objincidente.t18));
            gIncidWindow.$("txtAccidentQ19").appendChild(document.createTextNode(objincidente.t19));
        }
    }
}

function riempiIncidente(idIncidente, incidWindow) {
    xmlhttp = createXmlHttp()
    xmlhttp.open("POST", "../listaincidenti.php", true);
    
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            //alert(xmlhttp.responseText);
            xmlResponse = createXmlDoc(xmlhttp.responseText);
            
            // status node
            statusNode = xmlResponse.getElementsByTagName("status").item(0);
            statusNodeValue = (statusNode.hasChildNodes()) ? statusNode.firstChild.nodeValue : "";
            
            // message node
            messageNode = xmlResponse.getElementsByTagName("message").item(0);
            messageNodeValue = (messageNode.hasChildNodes()) ? messageNode.firstChild.nodeValue : "";
            //alert(messageNodeValue);
            
            if (statusNodeValue != "ERROR") {
                // data node
                dataNode = xmlResponse.getElementsByTagName("data").item(0);
                dataNodeValue = (dataNode.hasChildNodes()) ? dataNode.firstChild.nodeValue : "";
                
                if (dataNode.hasChildNodes()) {
                    
                    for (i = 0; i < dataNode.childNodes.length; i++) {
                        currentNode = dataNode.childNodes.item(i);
                        // is this a NODE type?
                        if (currentNode.nodeType == 1 && currentNode.nodeName == "row") {
                            // scans columns
                            if (currentNode.hasChildNodes()) {
                                
                                for (z = 0; z < currentNode.childNodes.length; z++) {
                                    columnNode = currentNode.childNodes.item(z);
                                    
                                    if (columnNode.nodeType == 1 && columnNode.nodeName == "column") {
                                        columnNodeValue = (columnNode.hasChildNodes && columnNode.firstChild != null) ? columnNode.firstChild.nodeValue : "";
                                        
                                        colName = columnNode.getAttribute("name");
                                        //alert(colName);
                                        if (colName == "t1") {
                                            setRadioValue(incidWindow.document.getElementsByName("rdnAccidentQ1"), columnNodeValue);
                                        }
                                        if (colName == "d2") {
                                            incidWindow.document.getElementById("txtAccidentQ2").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t3") {
                                            incidWindow.document.getElementById("txtAccidentQ3").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t4") {
                                            incidWindow.document.getElementById("txtAccidentQ4").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t5") {
                                            incidWindow.document.getElementById("txtAccidentQ5").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t6") {
                                            incidWindow.document.getElementById("txtAccidentQ6").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t7") {
                                            incidWindow.document.getElementById("txtAccidentQ7").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t8") {
                                            incidWindow.document.getElementById("txtAccidentQ8").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t9") {
                                            incidWindow.document.getElementById("txtAccidentQ9").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t10") {
                                            incidWindow.document.getElementById("txtAccidentQ10").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t11") {
                                            incidWindow.document.getElementById("txtAccidentQ11").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t12") {
                                            incidWindow.document.getElementById("txtAccidentQ12").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t13") {
                                            incidWindow.document.getElementById("txtAccidentQ13").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t14") {
                                            incidWindow.document.getElementById("txtAccidentQ14").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t15") {
                                            incidWindow.document.getElementById("txtAccidentQ15").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t16") {
                                            incidWindow.document.getElementById("txtAccidentQ16").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t17") {
                                            incidWindow.document.getElementById("txtAccidentQ17").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t18") {
                                            incidWindow.document.getElementById("txtAccidentQ18").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        if (colName == "t19") {
                                            incidWindow.document.getElementById("txtAccidentQ19").appendChild(document.createTextNode(columnNodeValue));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                alert("Errore nelle operazioni di ricerca.");
            }
        }
    }
    
    xmlhttp.send("mode=view&id_incidente=" + idIncidente);
}

function visualizzaIncidente(idIncidente) {
    document.getElementById("hdnIdIncidente").value = idIncidente;
    win = window.open("visualizzaincidente.php", "visualizza", "width=700,height=600, top=100, left=300, scrollbars=yes, resizable=yes, dependent=no");
}

function visualizzaQuestionario(idQuestionario) {
    document.getElementById("hdnIdQuestionario").value = idQuestionario;
    win = window.open("visualizzaquestionario.php", "visualizza", "width=700,height=600, top=100, left=300, scrollbars=yes, resizable=yes, dependent=no");
}

function eliminaQuestionarioIncidenteJSON(qType, id) {
    var confirmMessage = "";
    var responseMessage = "";
    if (qType == "q") {
        confirmMessage = "Eliminare il questionario?";
        responseMessage = "Questionario eliminato";
    } else {
        confirmMessage = "Eliminare il rapporto sull'incidente?";
        responseMessage = "Rapporto eliminato";
    }
    
    if (confirm(confirmMessage)) {
        var reqOpt = {
            // Use POST
            method: 'post',
            
            // Send this lovely data
            postBody: "qType=" + qType + "&id=" + id,
            
            // Handle successful response
            onSuccess: function(originalRequest) {
                var response = eval( '(' + originalRequest.responseText + ')' );
                
                if (response.status == "OK") {
                    alert(responseMessage);
                    if (qType == "q") {
                        cercaQuestionariJSON();
                    } else {
                        cercaIncidentiJSON();
                    }
                }
            },
            
            // Handle 404
            on404: function(t) {
                alert('Error 404: location "' + t.statusText + '" was not found.');
            },
            // Handle other errors
            onFailure: function(t) {
                alert('Error ' + t.status + ' -- ' + t.statusText);
            }
        };
            
        var myAjax = new Ajax.Request("../gestisciquestincidente.php", reqOpt);
    }
}

function eliminaQuestionarioIncidente(qType, id) {
    var confirmMessage = "";
    var responseMessage = "";
    if (qType == "q") {
        confirmMessage = "Eliminare il questionario?";
        responseMessage = "Questionario eliminato";
    } else {
        confirmMessage = "Eliminare il rapporto sull'incidente?";
        responseMessage = "Rapporto eliminato";
    }
    
    if (confirm(confirmMessage)) {
        xmlhttp = createXmlHttp();
        xmlhttp.open("POST", "../gestisciquestincidente.php", true);
                
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                xmlResponse = createXmlDoc(xmlhttp.responseText);
                
                // status node
                statusNode = xmlResponse.getElementsByTagName("status").item(0);
                statusNodeValue = (statusNode.hasChildNodes()) ? statusNode.firstChild.nodeValue : "";
                
                // message node
                messageNode = xmlResponse.getElementsByTagName("message").item(0);
                messageNodeValue = (messageNode.hasChildNodes()) ? messageNode.firstChild.nodeValue : "";
                        
                if (statusNodeValue == "OK") {
                    alert(responseMessage);
                    if (qType == "q") {
                        cercaQuestionari();
                    } else {
                        cercaIncidenti();
                    }
                }
            }
        }
        
        xmlhttp.send("qType=" + qType + "&id=" + id);
    }
}

function cercaQuestionariJSON() {
    pathPrefix = "../";
    gIsAdmin = false;
    
    var reqOpt = {
    	// Use POST
        method: 'post',
        
        // Send this lovely data
        postBody: 'mode=all',
        
        // Handle successful response
        onSuccess: cercaQuestionariJSON_callback,
        
        // Handle 404
        on404: function(t) {
            alert('Error 404: location "' + t.statusText + '" was not found.');
        },
        // Handle other errors
        onFailure: function(t) {
            alert('Error ' + t.status + ' -- ' + t.statusText);
        }
    };
    
    var myAjax = new Ajax.Request(pathPrefix + "listaquestionari.php", reqOpt);
}

function cercaQuestionariJSON_callback(originalRequest) {
    var response = eval( '(' + originalRequest.responseText + ')' );
    
    if (response.status != "ERROR") {
        var values = response.values;
        
        // clears previous results
        removeAllChildNodes($("tblBodyQuestionari"));
        
        for (i = 0; i < values.length; i++) {
            var objquestionario = values[i];
            
            var trElement = document.createElement("tr");
            
            $("tblBodyQuestionari").appendChild(trElement);
            
            // data questionario
            var tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            tdElement.appendChild(document.createTextNode(objquestionario.data_inserimento));
            trElement.appendChild(tdElement);
            
            // nominativo
            tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            tdElement.appendChild(document.createTextNode(objquestionario.nominativo));
            trElement.appendChild(tdElement);
            
            // azioni
            tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            
            // view
            var linkElement = document.createElement("a");
            linkElement.setAttribute("href", "javascript: visualizzaQuestionario(" + objquestionario.id_questionario + ");");
            
            var imageElement = document.createElement("img");
            imageElement.setAttribute("src", "../images/document.png");
            imageElement.setAttribute("border", "0");
            imageElement.setAttribute("alt", "visualizza questionario");
            imageElement.setAttribute("title", "visualizza questionario");
            imageElement.setAttribute("hspace", "2");
            imageElement.setAttribute("align", "left");
            
            linkElement.appendChild(imageElement);
            tdElement.appendChild(linkElement);
            
            // delete
            linkElement = document.createElement("a");
            linkElement.setAttribute("href", "javascript: eliminaQuestionarioIncidenteJSON('q', " + objquestionario.id_questionario + ");");
            
            imageElement = document.createElement("img");
            imageElement.setAttribute("src", "../images/delete.png");
            imageElement.setAttribute("border", "0");
            imageElement.setAttribute("alt", "elimina il questionario");
            imageElement.setAttribute("title", "elimina il questionario");
            imageElement.setAttribute("hspace", "2");
            imageElement.setAttribute("align", "left");
            
            linkElement.appendChild(imageElement);
            tdElement.appendChild(linkElement);
            
            trElement.appendChild(tdElement);
        }
    }
}

function cercaQuestionari() {
    xmlhttp = createXmlHttp()
    xmlhttp.open("POST", "../listaquestionari.php", true);
    
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            xmlResponse = createXmlDoc(xmlhttp.responseText);
            
            // status node
            statusNode = xmlResponse.getElementsByTagName("status").item(0);
            statusNodeValue = (statusNode.hasChildNodes()) ? statusNode.firstChild.nodeValue : "";
            
            // message node
            messageNode = xmlResponse.getElementsByTagName("message").item(0);
            messageNodeValue = (messageNode.hasChildNodes()) ? messageNode.firstChild.nodeValue : "";
            //alert(messageNodeValue);
            
            if (statusNodeValue != "ERROR") {
                // data node
                dataNode = xmlResponse.getElementsByTagName("data").item(0);
                dataNodeValue = (dataNode.hasChildNodes()) ? dataNode.firstChild.nodeValue : "";
                
                // results table body element
                tableBody = document.getElementById("tblBodyQuestionari");
                    
                // clears previous results
                removeAllChildNodes(tableBody);
                
                if (dataNode.hasChildNodes()) {
                    for (i = 0; i < dataNode.childNodes.length; i++) {
                        currentNode = dataNode.childNodes.item(i);
                        // is this a NODE type?
                        if (currentNode.nodeType == 1 && currentNode.nodeName == "row") {
                            // scans columns
                            if (currentNode.hasChildNodes()) {
                                row = document.createElement("tr");
                                
                                tableBody.appendChild(row);
                                
                                for (z = 0; z < currentNode.childNodes.length; z++) {
                                    columnNode = currentNode.childNodes.item(z);
                                    
                                    if (columnNode.nodeType == 1 && columnNode.nodeName == "column") {
                                        columnNodeValue = (columnNode.hasChildNodes && columnNode.firstChild != null) ? columnNode.firstChild.nodeValue : "";
                                        
                                        colName = columnNode.getAttribute("name");
                                            
                                        col = document.createElement("td");
                                        col.setAttribute("class", "rigaTabella");
                                        col.className = "rigaTabella";
                                        
                                        if (colName == "id_questionario") {
                                            col.setAttribute("align", "right");
                                            
                                            /////////////
                                            /// VIEW ///
                                            /////////////
                                            link = document.createElement("a");
                                            link.setAttribute("href", "javascript: visualizzaQuestionario(" + columnNodeValue + ");");
                                            
                                            image = document.createElement("img");
                                            image.setAttribute("src", "../images/document.png");
                                            image.setAttribute("border", "0");
                                            image.setAttribute("alt", "visualizza questionario");
                                            image.setAttribute("title", "visualizza questionario");
                                            image.setAttribute("hspace", "2");
                                            image.setAttribute("align", "left");
                                            
                                            link.appendChild(image);
                                            col.appendChild(link);
                                            
                                            //////////////
                                            /// DELETE ///
                                            //////////////
                                            link = document.createElement("a");
                                            link.setAttribute("href", "javascript: eliminaQuestionarioIncidente('q', " + columnNodeValue + ");");
                                            
                                            image = document.createElement("img");
                                            image.setAttribute("src", "../images/delete.png");
                                            image.setAttribute("border", "0");
                                            image.setAttribute("alt", "elimina il questionario");
                                            image.setAttribute("title", "elimina il questionario");
                                            image.setAttribute("hspace", "2");
                                            image.setAttribute("align", "left");
                                            
                                            link.appendChild(image);
                                            col.appendChild(link);
                                        } else {
                                            col.appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        
                                        row.appendChild(col);
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                alert("Errore nelle operazioni di ricerca.");
            }
        }
    }
    
    xmlhttp.send("mode=all");
}

function cercaIncidentiJSON() {
    pathPrefix = "../";
    gIsAdmin = false;
    
    var reqOpt = {
    	// Use POST
        method: 'post',
        
        // Send this lovely data
        postBody: 'mode=all',
        
        // Handle successful response
        onSuccess: cercaIncidentiJSON_callback,
        
        // Handle 404
        on404: function(t) {
            alert('Error 404: location "' + t.statusText + '" was not found.');
        },
        // Handle other errors
        onFailure: function(t) {
            alert('Error ' + t.status + ' -- ' + t.statusText);
        }
    };
    
    var myAjax = new Ajax.Request(pathPrefix + "listaincidenti.php", reqOpt);
}

function cercaIncidentiJSON_callback(originalRequest) {
    var response = eval( '(' + originalRequest.responseText + ')' );
    
    if (response.status != "ERROR") {
        var values = response.values;
        
        // clears previous results
        removeAllChildNodes($("tblBodyIncidenti"));
        
        for (i = 0; i < values.length; i++) {
            var objincidente = values[i];
            
            var trElement = document.createElement("tr");
            
            $("tblBodyIncidenti").appendChild(trElement);
            
            // data inserimento
            var tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            tdElement.appendChild(document.createTextNode(objincidente.data_inserimento));
            trElement.appendChild(tdElement);
            
            // data incidente
            tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            tdElement.appendChild(document.createTextNode(objincidente.d2));
            trElement.appendChild(tdElement);
            
            // struttura sanitaria
            tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            tdElement.appendChild(document.createTextNode(objincidente.t3));
            trElement.appendChild(tdElement);
            
            // dispositivo
            tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            tdElement.appendChild(document.createTextNode(objincidente.t4));
            trElement.appendChild(tdElement);
            
            // modello
            tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            tdElement.appendChild(document.createTextNode(objincidente.t5));
            trElement.appendChild(tdElement);
            
            // segnalato da
            tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            tdElement.appendChild(document.createTextNode(objincidente.t13));
            trElement.appendChild(tdElement);
            
            // azioni
            tdElement = document.createElement("td");
            tdElement.className = "rigaTabella";
            
            // view
            var linkElement = document.createElement("a");
            linkElement.setAttribute("href", "javascript: visualizzaIncidente(" + objincidente.id_incidente + ");");
            
            var imageElement = document.createElement("img");
            imageElement.setAttribute("src", "../images/document.png");
            imageElement.setAttribute("border", "0");
            imageElement.setAttribute("alt", "visualizza incidente");
            imageElement.setAttribute("title", "visualizza incidente");
            imageElement.setAttribute("hspace", "2");
            imageElement.setAttribute("align", "left");
            
            linkElement.appendChild(imageElement);
            tdElement.appendChild(linkElement);
            
            // delete
            linkElement = document.createElement("a");
            linkElement.setAttribute("href", "javascript: eliminaQuestionarioIncidenteJSON('i', " + objincidente.id_incidente + ");");
            
            imageElement = document.createElement("img");
            imageElement.setAttribute("src", "../images/delete.png");
            imageElement.setAttribute("border", "0");
            imageElement.setAttribute("alt", "elimina il rapporto");
            imageElement.setAttribute("title", "elimina il rapporto");
            imageElement.setAttribute("hspace", "2");
            imageElement.setAttribute("align", "left");
            
            linkElement.appendChild(imageElement);
            tdElement.appendChild(linkElement);
            
            trElement.appendChild(tdElement);
        }
    }
}

function cercaIncidenti() {
    xmlhttp = createXmlHttp()
            xmlhttp.open("POST", "../listaincidenti.php", true);
    
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            xmlResponse = createXmlDoc(xmlhttp.responseText);
            
            // status node
            statusNode = xmlResponse.getElementsByTagName("status").item(0);
            statusNodeValue = (statusNode.hasChildNodes()) ? statusNode.firstChild.nodeValue : "";
            
            // message node
            messageNode = xmlResponse.getElementsByTagName("message").item(0);
            messageNodeValue = (messageNode.hasChildNodes()) ? messageNode.firstChild.nodeValue : "";
            //alert(messageNodeValue);
            
            if (statusNodeValue != "ERROR") {
                // data node
                dataNode = xmlResponse.getElementsByTagName("data").item(0);
                dataNodeValue = (dataNode.hasChildNodes()) ? dataNode.firstChild.nodeValue : "";
                
                // results table body element
                tableBody = document.getElementById("tblBodyIncidenti");
                    
                // clears previous results
                removeAllChildNodes(tableBody);
                
                if (dataNode.hasChildNodes()) {
                    for (i = 0; i < dataNode.childNodes.length; i++) {
                        currentNode = dataNode.childNodes.item(i);
                        // is this a NODE type?
                        if (currentNode.nodeType == 1 && currentNode.nodeName == "row") {
                            // scans columns
                            if (currentNode.hasChildNodes()) {
                                row = document.createElement("tr");
                                
                                tableBody.appendChild(row);
                                
                                for (z = 0; z < currentNode.childNodes.length; z++) {
                                    columnNode = currentNode.childNodes.item(z);
                                    
                                    if (columnNode.nodeType == 1 && columnNode.nodeName == "column") {
                                        columnNodeValue = (columnNode.hasChildNodes && columnNode.firstChild != null) ? columnNode.firstChild.nodeValue : "";
                                        
                                        colName = columnNode.getAttribute("name");
                                            
                                        col = document.createElement("td");
                                        col.setAttribute("class", "rigaTabella");
                                        col.className = "rigaTabella";
                                        
                                        if (colName == "id_incidente") {
                                            col.setAttribute("align", "right");
                                            
                                            /////////////
                                            /// VIEW ///
                                            /////////////
                                            link = document.createElement("a");
                                            link.setAttribute("href", "javascript: visualizzaIncidente(" + columnNodeValue + ");");
                                            
                                            image = document.createElement("img");
                                            image.setAttribute("src", "../images/document.png");
                                            image.setAttribute("border", "0");
                                            image.setAttribute("alt", "visualizza incidente");
                                            image.setAttribute("title", "visualizza incidente");
                                            image.setAttribute("hspace", "2");
                                            image.setAttribute("align", "left");
                                            
                                            link.appendChild(image);
                                            col.appendChild(link);
                                            
                                            //////////////
                                            /// DELETE ///
                                            //////////////
                                            link = document.createElement("a");
                                            link.setAttribute("href", "javascript: eliminaQuestionarioIncidente('i', " + columnNodeValue + ");");
                                            
                                            image = document.createElement("img");
                                            image.setAttribute("src", "../images/delete.png");
                                            image.setAttribute("border", "0");
                                            image.setAttribute("alt", "elimina il questionario");
                                            image.setAttribute("title", "elimina il questionario");
                                            image.setAttribute("hspace", "2");
                                            image.setAttribute("align", "left");
                                            
                                            link.appendChild(image);
                                            col.appendChild(link);
                                        } else {
                                            col.appendChild(document.createTextNode(columnNodeValue));
                                        }
                                        
                                        row.appendChild(col);
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                alert("Errore nelle operazioni di ricerca.");
            }
        }
    }
    
    xmlhttp.send("mode=all");
}

function eliminaCommentoJSON(idCommento) {
    if (confirm("Eliminare il commento?")) {
        var reqOpt = {
            // Use POST
            method: 'post',
            
            // Send this lovely data
            postBody: "mode=delete&id_commento=" + idCommento,
            
            // Handle successful response
            onSuccess: function(originalRequest) {
                var response = eval( '(' + originalRequest.responseText + ')' );
                
                if (response.status == "OK") {
                    alert("Commento eliminato");
                    cercaCommentiJSON(true);
                }
            },
            
            // Handle 404
            on404: function(t) {
                alert('Error 404: location "' + t.statusText + '" was not found.');
            },
            // Handle other errors
            onFailure: function(t) {
                alert('Error ' + t.status + ' -- ' + t.statusText);
            }
        };
            
        var myAjax = new Ajax.Request("../gestiscicommento.php", reqOpt);
    }
}

function eliminaCommento(idCommento) {
    if (confirm("Eliminare il commento?")) {
        xmlhttp = createXmlHttp();
        xmlhttp.open("POST", "../gestiscicommento.php", true);
                
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                xmlResponse = createXmlDoc(xmlhttp.responseText);
                
                // status node
                statusNode = xmlResponse.getElementsByTagName("status").item(0);
                statusNodeValue = (statusNode.hasChildNodes()) ? statusNode.firstChild.nodeValue : "";
                
                // message node
                messageNode = xmlResponse.getElementsByTagName("message").item(0);
                messageNodeValue = (messageNode.hasChildNodes()) ? messageNode.firstChild.nodeValue : "";
                        
                if (statusNodeValue == "OK") {
                    alert("Commento eliminato");
                    cercaCommenti(true);
                }
            }
        }
        
        xmlhttp.send("mode=delete&id_commento=" + idCommento);
    }
}

function abilitaDisabilitaCommentoJSON(idCommento, mode) {
    var message = "";
    var respMessage = "";
    if (mode == "enable") {
        message = "Abilitare il commento?";
        respMessage = "Commento abilitato";
    } else {
        message = "Disabilitare il commento?";
        respMessage = "Commento disabilitato";
    }
    
    if (confirm(message)) {
        var reqOpt = {
            // Use POST
            method: 'post',
            
            // Send this lovely data
            postBody: "mode=" + mode + "&id_commento=" + idCommento,
            
            // Handle successful response
            onSuccess: function(originalRequest) {
                var response = eval( '(' + originalRequest.responseText + ')' );
                
                if (response.status == "OK") {
                    alert(respMessage);
                    cercaCommentiJSON(true);
                }
            },
            
            // Handle 404
            on404: function(t) {
                alert('Error 404: location "' + t.statusText + '" was not found.');
            },
            // Handle other errors
            onFailure: function(t) {
                alert('Error ' + t.status + ' -- ' + t.statusText);
            }
        };
            
        var myAjax = new Ajax.Request("../gestiscicommento.php", reqOpt);
    }
}

function abilitaDisabilitaCommento(idCommento, mode) {
    var message = "";
    var respMessage = "";
    if (mode == "enable") {
        message = "Abilitare il commento?";
        respMessage = "Commento abilitato";
    } else {
        message = "Disabilitare il commento?";
        respMessage = "Commento disabilitato";
    }
    
    if (confirm(message)) {
        xmlhttp = createXmlHttp();
        xmlhttp.open("POST", "../gestiscicommento.php", true);
                
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                xmlResponse = createXmlDoc(xmlhttp.responseText);
                
                // status node
                statusNode = xmlResponse.getElementsByTagName("status").item(0);
                statusNodeValue = (statusNode.hasChildNodes()) ? statusNode.firstChild.nodeValue : "";
                
                // message node
                messageNode = xmlResponse.getElementsByTagName("message").item(0);
                messageNodeValue = (messageNode.hasChildNodes()) ? messageNode.firstChild.nodeValue : "";
                        
                if (statusNodeValue == "OK") {
                    alert(respMessage);
                    cercaCommenti(true);
                }
            }
        }
        
        xmlhttp.send("mode=" + mode + "&id_commento=" + idCommento);
    }
}

function eliminaAnnuncioJSON(idAnnuncio) {
    if (confirm("Eliminare il messaggio?")) {
        var reqOpt = {
            // Use POST
            method: 'post',
            
            // Send this lovely data
            postBody: "mode=delete&id_annuncio=" + idAnnuncio,
            
            // Handle successful response
            onSuccess: function(originalRequest) {
                var response = eval( '(' + originalRequest.responseText + ')' );
                
                if (response.status == "OK") {
                    alert("Messaggio eliminato");
                    cercaAnnunciJSON(true);
                }
            },
            
            // Handle 404
            on404: function(t) {
                alert('Error 404: location "' + t.statusText + '" was not found.');
            },
            // Handle other errors
            onFailure: function(t) {
                alert('Error ' + t.status + ' -- ' + t.statusText);
            }
        };
            
        var myAjax = new Ajax.Request("../gestisciannuncio.php", reqOpt);
    }
}

function eliminaAnnuncio(idAnnuncio) {
    if (confirm("Eliminare il messaggio?")) {
        xmlhttp = createXmlHttp();
        xmlhttp.open("POST", "../gestisciannuncio.php", true);
                
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                xmlResponse = createXmlDoc(xmlhttp.responseText);
                
                // status node
                statusNode = xmlResponse.getElementsByTagName("status").item(0);
                statusNodeValue = (statusNode.hasChildNodes()) ? statusNode.firstChild.nodeValue : "";
                
                // message node
                messageNode = xmlResponse.getElementsByTagName("message").item(0);
                messageNodeValue = (messageNode.hasChildNodes()) ? messageNode.firstChild.nodeValue : "";
                        
                if (statusNodeValue == "OK") {
                    alert("Messaggio eliminato");
                    cercaAnnunci(true);
                }
            }
        }
        
        xmlhttp.send("mode=delete&id_annuncio=" + idAnnuncio);
    }
}

function abilitaDisabilitaAnnuncioJSON(idAnnuncio, mode) {
    var message = "";
    var respMessage = "";
    if (mode == "enable") {
        message = "Abilitare il messaggio?";
        respMessage = "Messaggio abilitato";
    } else {
        message = "Disabilitare il messaggio?";
        respMessage = "Messaggio disabilitato";
    }
    
    if (confirm(message)) {
        var reqOpt = {
            // Use POST
            method: 'post',
            
            // Send this lovely data
            postBody: "mode=" + mode + "&id_annuncio=" + idAnnuncio,
            
            // Handle successful response
            onSuccess: function(originalRequest) {
                var response = eval( '(' + originalRequest.responseText + ')' );
                
                if (response.status == "OK") {
                    alert(respMessage);
                    cercaAnnunciJSON(true);
                }
            },
            
            // Handle 404
            on404: function(t) {
                alert('Error 404: location "' + t.statusText + '" was not found.');
            },
            // Handle other errors
            onFailure: function(t) {
                alert('Error ' + t.status + ' -- ' + t.statusText);
            }
        };
            
        var myAjax = new Ajax.Request("../gestisciannuncio.php", reqOpt);
    }
}

function abilitaDisabilitaAnnuncio(idAnnuncio, mode) {
    var message = "";
    var respMessage = "";
    if (mode == "enable") {
        message = "Abilitare il messaggio?";
        respMessage = "Messaggio abilitato";
    } else {
        message = "Disabilitare il messaggio?";
        respMessage = "Messaggio disabilitato";
    }
    
    if (confirm(message)) {
        xmlhttp = createXmlHttp();
        xmlhttp.open("POST", "../gestisciannuncio.php", true);
                
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                xmlResponse = createXmlDoc(xmlhttp.responseText);
                
                // status node
                statusNode = xmlResponse.getElementsByTagName("status").item(0);
                statusNodeValue = (statusNode.hasChildNodes()) ? statusNode.firstChild.nodeValue : "";
                
                // message node
                messageNode = xmlResponse.getElementsByTagName("message").item(0);
                messageNodeValue = (messageNode.hasChildNodes()) ? messageNode.firstChild.nodeValue : "";
                        
                if (statusNodeValue == "OK") {
                    alert(respMessage);
                    cercaAnnunci(true);
                }
            }
        }
        
        xmlhttp.send("mode=" + mode + "&id_annuncio=" + idAnnuncio);
    }
}

function salvaAnnuncioJSON() {
    var user = trimString($F("txtUser"));
    var email = trimString($F("txtEmailAddress"));
    var message = trimString($F("txtMessage"));
    var privacy = $("chkPrivacy").checked;
    
    var fieldsOk = true;
    
    if (user == "" || email == "" || message == "") {
        alert("Inserire tutti i dati per salvare il messaggio.");
        fieldsOk = false;
    }
    
    if (privacy) {
        if (fieldsOk) {
            var reqOpt = {
                // Use POST
                method: 'post',
                
                // Send this lovely data
                postBody: "txtUser=" + user + "&txtEmailAddress=" + email + "&txtMessage=" + message + "&chkPrivacy=" + privacy,
                
                // Handle successful response
                onSuccess: salvaAnnuncioJSON_callback,
                
                // Handle 404
                on404: function(t) {
                    alert('Error 404: location "' + t.statusText + '" was not found.');
                },
                // Handle other errors
                onFailure: function(t) {
                    alert('Error ' + t.status + ' -- ' + t.statusText);
                }
            };
                
            var myAjax = new Ajax.Request("salvaannuncio.php", reqOpt);
        }
    } else {
        alert("Non è possibile inserire un messaggio senza accettare la nota informativa sulla tutela della privacy.");
    }
}

function salvaAnnuncioJSON_callback(originalRequest) {
    var response = eval( '(' + originalRequest.responseText + ')' );
    
    if (response.status == "OK") {
        alert("Messaggio inserito correttamente");
        
        $("txtUser").value = "";
        $("txtEmailAddress").value = "";
        $("txtMessage").value = "";
        $("chkPrivacy").checked = false;
    }
}

function salvaAnnuncio() {
    user = trimString(document.getElementById("txtUser").value);
    email = trimString(document.getElementById("txtEmailAddress").value);
    message = trimString(document.getElementById("txtMessage").value);
    privacy = document.getElementById("chkPrivacy").checked;
    
    fieldsOk = true;
    
    if (user == "" || email == "" || message == "") {
        alert("Inserire tutti i dati per salvare il messaggio.");
        fieldsOk = false;
    }
    
    if (privacy) {
        if (fieldsOk) {
            xmlhttp = createXmlHttp();
            xmlhttp.open("POST", "salvaannuncio.php", true);
            
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
                    xmlResponse = createXmlDoc(xmlhttp.responseText);
                    
                    // status node
                    statusNode = xmlResponse.getElementsByTagName("status").item(0);
                    statusNodeValue = (statusNode.hasChildNodes()) ? statusNode.firstChild.nodeValue : "";
                    
                    // message node
                    messageNode = xmlResponse.getElementsByTagName("message").item(0);
                    messageNodeValue = (messageNode.hasChildNodes()) ? messageNode.firstChild.nodeValue : "";
                    
                    if (statusNodeValue == "OK") {
                        alert("Messaggio inserito correttamente");
                        
                        document.getElementById("txtUser").value = "";
                        document.getElementById("txtEmailAddress").value = "";
                        document.getElementById("txtMessage").value = "";
                        document.getElementById("chkPrivacy").checked = false;
                    }
                }
            }
            
            xmlhttp.send("txtUser=" + user + "&txtEmailAddress=" + email + "&txtMessage=" + message + "&chkPrivacy=" + privacy);
        }
    } else {
        alert("Non è possibile inserire un messaggio senza accettare la nota informativa sulla tutela della privacy.");
    }
}

function salvaCommentoJSON() {
    var name = trimString($F("txtName"));
    var message = trimString($F("txtMessage"));
    var privacy = $("chkPrivacy").checked;
    
    var fieldsOk = true;
    
    if (message == "") {
        alert("Inserire il testo del commento.");
        fieldsOk = false;
    }
    
    if (privacy) {
        if (fieldsOk) {
            var reqOpt = {
                // Use POST
                method: 'post',
                
                // Send this lovely data
                postBody: "txtName=" + name + "&txtMessage=" + message + "&chkPrivacy=" + privacy,
                
                // Handle successful response
                onSuccess: function(originalRequest) {
                    var response = eval( '(' + originalRequest.responseText + ')' );
                    
                    if (response.status == "OK") {
                        alert("Messaggio inserito correttamente");
                        
                        $("txtName").value = "";
                        $("txtMessage").value = "";
                        $("chkPrivacy").checked = false;
                    }
                },
                
                // Handle 404
                on404: function(t) {
                    alert('Error 404: location "' + t.statusText + '" was not found.');
                },
                // Handle other errors
                onFailure: function(t) {
                    alert('Error ' + t.status + ' -- ' + t.statusText);
                }
            };
                
            var myAjax = new Ajax.Request("salvacommento.php", reqOpt);
        }
    } else {
        alert("Non è possibile inserire un messaggio senza accettare la nota informativa sulla tutela della privacy.");
    }
}

function salvaCommento() {
    name = trimString(document.getElementById("txtName").value);
    message = trimString(document.getElementById("txtMessage").value);
    privacy = document.getElementById("chkPrivacy").checked;
    
    fieldsOk = true;
    
    if (message == "") {
        alert("Inserire il testo del commento.");
        fieldsOk = false;
    }
    
    if (privacy) {
        if (fieldsOk) {
            xmlhttp = createXmlHttp();
            xmlhttp.open("POST", "salvacommento.php", true);
            
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
                    xmlResponse = createXmlDoc(xmlhttp.responseText);
                    
                    // status node
                    statusNode = xmlResponse.getElementsByTagName("status").item(0);
                    statusNodeValue = (statusNode.hasChildNodes()) ? statusNode.firstChild.nodeValue : "";
                    
                    // message node
                    messageNode = xmlResponse.getElementsByTagName("message").item(0);
                    messageNodeValue = (messageNode.hasChildNodes()) ? messageNode.firstChild.nodeValue : "";
                    
                    if (statusNodeValue == "OK") {
                        alert("Messaggio inserito correttamente");
                        
                        document.getElementById("txtName").value = "";
                        document.getElementById("txtMessage").value = "";
                        document.getElementById("chkPrivacy").checked = false;
                    }
                }
            }
            
            xmlhttp.send("txtName=" + name + "&txtMessage=" + message + "&chkPrivacy=" + privacy);
        }
    } else {
        alert("Non è possibile inserire un messaggio senza accettare la nota informativa sulla tutela della privacy.");
    }
}

function getRadioValue(radioName) {
    var radioElements = document.getElementsByName(radioName);
    var radioValue = 0;
    for (i = 0; i < radioElements.length; i++) {
        if (radioElements.item(i).checked) {
            radioValue = radioElements.item(i).value;
            break;
        }
    }
    
    return radioValue;
}

function setRadioValue(radioElements, radioValue) {
    for (i = 0; i < radioElements.length; i++) {
        if (radioElements.item(i).value == radioValue) {
            radioElements.item(i).checked = true;
            //break;
        }
    }
}

function salvaSegnalazIncidente() {
    var privacy = $("chkAccidentPrivacy").checked;
    
    var t1Value = getRadioValue("rdnAccidentQ1");
    
    var d2Value = trimString($F("txtAccidentQ2"));
    
    var t3Value = trimString($F("txtAccidentQ3"));
    
    var t4Value = trimString($F("txtAccidentQ4"));
    
    var t5Value = trimString($F("txtAccidentQ5"));
    
    var t6Value = trimString($F("txtAccidentQ6"));
    
    var t7Value = "";
    if ($("chkAccidentPaziente").checked) {
        t7Value += $F("chkAccidentPaziente") + "; ";
    }
    if ($("chkAccidentOperatore").checked) {
        t7Value += $F("chkAccidentOperatore") + "; ";
    }
    if ($("chkAccidentTerze").checked) {
        t7Value += $F("chkAccidentTerze") + "; ";
    }
    if ($("chkAccidentPersone").checked) {
        t7Value += $F("chkAccidentPersone") + "; ";
    }
    if ($("chkAccidentAmbiente").checked) {
        t7Value += $F("chkAccidentAmbiente") + "; ";
    }
    
    var t8Value = trimString($F("txtAccidentQ8"));
    
    var t9Value = trimString($F("txtAccidentQ9"));
    
    var t10Value = trimString($F("txtAccidentQ10"));
    
    var t11Value = trimString($F("txtAccidentQ11"));
    
    var t12Value = trimString($F("txtAccidentQ12"));
    
    var t13Value = trimString($F("txtAccidentQ13"));
    
    var t14Value = trimString($F("txtAccidentQ14"));
    
    var t15Value = trimString($F("txtAccidentQ15"));
    
    var t16Value = trimString($F("txtAccidentQ16"));
    
    var t17Value = trimString($F("txtAccidentQ17"));
    
    var t18Value = trimString($F("txtAccidentQ18"));
    
    var t19Value = "";
    if ($("chkAccidentDecesso").checked) {
        t19Value += $F("chkAccidentDecesso") + "; ";
    }
    if ($("chkAccidentIntChirurgico").checked) {
        t19Value += $F("chkAccidentIntChirurgico") + "; ";
    }
    if ($("chkAccidentIntMedico").checked) {
        t19Value += $F("chkAccidentIntMedico") + "; ";
    }
    if ($("chkAccidentOspedalizzazione").checked) {
        t19Value += $F("chkAccidentOspedalizzazione") + "; ";
    }
    
    fieldsOk = true;
    
    if (t1Value == "" || d2Value == "" || t3Value == "" || t4Value == "" || t5Value == "" ||
    t6Value == "" || t7Value == "" || t8Value == "" || t9Value == "" || t10Value == "" ||
    t11Value == "" || t12Value == "" || t13Value == "" || t14Value == "" || t15Value == "" ||
    t16Value == "" || t17Value == "" || t18Value == "") {
        alert("Questionario non completo. Completarlo.");
        fieldsOk = false;
    }
    
    if (privacy) {
        if (fieldsOk) {
            var sendStr = "txtQ1=" + t1Value + 
                    "&txtQ2=" + d2Value + 
                    "&txtQ3=" + t3Value + 
                    "&txtQ4=" + t4Value + 
                    "&txtQ5=" + t5Value + 
                    "&txtQ6=" + t6Value + 
                    "&txtQ7=" + t7Value + 
                    "&txtQ8=" + t8Value + 
                    "&txtQ9=" + t9Value + 
                    "&txtQ10=" + t10Value + 
                    "&txtQ11=" + t11Value + 
                    "&txtQ12=" + t12Value + 
                    "&txtQ13=" + t13Value + 
                    "&txtQ14=" + t14Value + 
                    "&txtQ15=" + t15Value + 
                    "&txtQ16=" + t16Value + 
                    "&txtQ17=" + t17Value + 
                    "&txtQ18=" + t18Value + 
                    "&txtQ19=" + t19Value + 
                    "&chkPrivacy=" + privacy;
                    
            var reqOpt = {
                // Use POST
                method: 'post',
                
                // Send this lovely data
                postBody: sendStr,
                
                // Handle successful response
                onSuccess: function(originalRequest) {
                    var response = eval( '(' + originalRequest.responseText + ')' );
                    
                    if (response.status == "OK") {
                        alert("Questionario inserito correttamente");
                        
                        $("frmAccidentForm").reset();
                    } else {
                        alert(response.message);
                    }
                },
                
                // Handle 404
                on404: function(t) {
                    alert('Error 404: location "' + t.statusText + '" was not found.');
                },
                // Handle other errors
                onFailure: function(t) {
                    alert('Error ' + t.status + ' -- ' + t.statusText);
                }
            };
            
            var myAjax = new Ajax.Request("salvaquestincidente.php", reqOpt);
        }
    } else {
        alert("Non è possibile inserire un messaggio senza accettare la nota informativa sulla tutela della privacy.");
    }
}

function salvaQuestSodd() {
    var name = trimString($F("txtName"));
    
    var privacy = $("chkPrivacy").checked;
    
    var q1Value = getRadioValue("rdnQ1");
    
    var q2Value = getRadioValue("rdnQ2");
    
    var q3Value = getRadioValue("rdnQ3");
    
    var q4Value = getRadioValue("rdnQ4");
    
    var q5Value = getRadioValue("rdnQ5");
    
    var q6Value = getRadioValue("rdnQ6");
    
    var t7Value = trimString($F("txtQ7"));
    
    var t8Value = trimString($F("txtQ8"));
    
    var t9Value = trimString($F("txtQ9"));
    
    var q10Value = getRadioValue("rdnQ10");
    
    fieldsOk = true;
    
    if (name == "" || q1Value == 0 || q2Value == 0 || q3Value == 0 || q4Value == 0 || q5Value == 0 ||
        q6Value == 0 || t7Value == "" || t8Value == "" || t9Value == "" || q10Value == 0) {
        alert("Questionario non completo. Completarlo.");
        fieldsOk = false;
    }
    
    if (privacy) {
        if (fieldsOk) {
            var sendStr = "txtName=" + name + 
                "&rdnQ1=" + q1Value + 
                "&rdnQ2=" + q2Value + 
                "&rdnQ3=" + q3Value + 
                "&rdnQ4=" + q4Value + 
                "&rdnQ5=" + q5Value + 
                "&rdnQ6=" + q6Value + 
                "&txtQ7=" + t7Value + 
                "&txtQ8=" + t8Value + 
                "&txtQ9=" + t9Value + 
                "&rdnQ10=" + q10Value + 
                "&chkPrivacy=" + privacy;
                
            var reqOpt = {
                // Use POST
                method: 'post',
                
                // Send this lovely data
                postBody: sendStr,
                
                // Handle successful response
                onSuccess: function(originalRequest) {
                    var response = eval( '(' + originalRequest.responseText + ')' );
                    
                    if (response.status == "OK") {
                        alert("Questionario inserito correttamente");
                        
                        $("frmSatisfactionForm").reset();
                    } else {
                        alert(messageNodeValue);
                    }
                },
                
                // Handle 404
                on404: function(t) {
                    alert('Error 404: location "' + t.statusText + '" was not found.');
                },
                // Handle other errors
                onFailure: function(t) {
                    alert('Error ' + t.status + ' -- ' + t.statusText);
                }
            };
            
            var myAjax = new Ajax.Request("salvaquestionario.php", reqOpt);
        }
    } else {
        alert("Non è possibile inserire un messaggio senza accettare la nota informativa sulla tutela della privacy.");
    }
}

function rispondiAnnuncio(idAnnuncio, isAdmin) {
    var pathPrefix = "";
    if (isAdmin) {
        pathPrefix = "../";
    }
    
    document.getElementById("hdnIdAnnuncio").value = idAnnuncio;
    
    popupWindow = window.open(pathPrefix + "rispondiannuncio.php", "rispondi", "width=500,height=520, top=100, left=300, resizable=yes, dependent=no");
}

function inviaEmailAnnuncio() {
    from = trimString(document.getElementById("txtMailFrom").value);
    message = trimString(document.getElementById("txtMailBody").value);
    idAnnuncio = window.opener.document.getElementById("hdnIdAnnuncio").value
    
    fieldsOk = true;
    
    if (from == "") {
        alert("Per rispondere all'annuncio devi inserire il tuo indirizzo email.");
        fieldsOk = false;
    } else {
        if (message == "") {
            alert("Inserire il testo del messaggio.");
            fieldsOk = false;
        }
    }
    

    if (fieldsOk) {
        var reqOpt = {
            // Use POST
            method: 'post',
            
            // Send this lovely data
            postBody: "id_annuncio=" + idAnnuncio + "&txtMailFrom=" + from + "&txtMailBody=" + message,
            
            // Handle successful response
            onSuccess: function(originalRequest) {
                var response = eval( '(' + originalRequest.responseText + ')' );
                
                if (response.status == "OK") {
                    alert("Risposta inviata correttamente");
                    
                    if (navigator.userAgent.indexOf("Firefox") == -1) {
                        window.close();
                    } else {
                        alert("Ora si può chiudere la finestra");
                    }
                } else {
                    alert(response.message);
                }
            },
            
            // Handle 404
            on404: function(t) {
                alert('Error 404: location "' + t.statusText + '" was not found.');
            },
            // Handle other errors
            onFailure: function(t) {
                alert('Error ' + t.status + ' -- ' + t.statusText);
            }
        };
        
        var myAjax = new Ajax.Request("inviamailannuncio.php", reqOpt);
    }
}

function switchLink(pageName) {
    xmlhttp = createXmlHttp();
    xmlhttp.open("GET", pageName + ".php.inc", true);
    
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            document.getElementById("mainDIV").innerHTML = xmlhttp.responseText;
        }
    }
    
    xmlhttp.send("");
}

function showInsertMessage() {
    insertDiv = document.getElementById("boardInsertDiv");
    listDiv = document.getElementById("boardListDiv");
    
    insertDiv.style.display = "none";
    listDiv.style.display = "none";
    
    insertDiv.style.display = "block";
}

function showMessageBoard() {
    insertDiv = document.getElementById("boardInsertDiv");
    listDiv = document.getElementById("boardListDiv");
    
    insertDiv.style.display = "none";
    listDiv.style.display = "none";
    
    listDiv.style.display = "block";
}

function showElement(idElement) {
    tempElement = document.getElementById(idElement);
    
    tempElement.style.display = "block";
}

function hideElement(idElement) {
    tempElement = document.getElementById(idElement);
    
    tempElement.style.display = "none";
}

function setupCalendar() {
    Calendar.setup({
        inputField     :    "txtAccidentQ2",    // id of the input field
        ifFormat       :    "%Y-%m-%d",        // format of the input field
        button         :    "f_trigger_c",      // trigger for the calendar (button ID)
        align          :    "Tl",               // alignment (defaults to "Bl")
        singleClick    :    true,
        firstDay       :    1
    });
}