function collapseImage(id) {

    var idElement = window.document.getElementById(id);
    var divClassName = idElement.className;

    var toTestShow = /plus/;
    var checkShow = toTestShow.test(divClassName);
    newClassShow = divClassName.replace(toTestShow,'minus');

    var toTestHide = /minus/;
    var checkHide = toTestHide.test(divClassName);
    newClassHide = divClassName.replace(toTestHide,'plus');

    if (checkShow == true) {
        //document.getElementById(id).disabled = false;
        idElement.className=newClassShow;
        idElement.innerHTML = '<img class="a-show" src="' + HOST + PATH + '/images/minus.png" width="15" height="15" />';
    }
    if (checkHide == true) {
        //document.getElementById(id).disabled = true;
        idElement.className=newClassHide;
        idElement.innerHTML = '<img class="a-show" src="' + HOST + PATH + '/images/plus.png" width="15" height="15" />';
    }
}


function textCounter(id, id2, maxlimit) {
    var field = window.document.getElementById(id);
    var countfield = window.document.getElementById(id2);
    if (field.value.length > maxlimit) {
         // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
        // otherwise, update 'characters left' counter
    } else {
        countfield.value = maxlimit - field.value.length;
    }
}



var counter = 1;
function AddFileUpload(filesMaxNum)
{
    // la variabile filesMaxNum è impostata a livello di php nella variabile $this->filesMaxNum
    // devo togliere una unità perché un campo upload è impostato e presente di default nella pagina
    var maxNum = filesMaxNum-1;
    if (counter <= maxNum) {
         var div = document.createElement('DIV');
         div.innerHTML = '\n\t\t\t<input id="file' + counter + '" name = "file' + counter + '" type="file" />' +
                         //'\n\t\t\t<input id="button' + counter + '" type="button" class="button remove" ' + 'value="Rimuovi" onclick = "RemoveFileUpload(this)" />';
                         '\n\t\t\t<a class="remove" id="button' + counter + '" href="javascript:void(0);" onclick="RemoveFileUpload(this);" title="Clicca per rimuovere il file">Rimuovi</a>';
         document.getElementById("FileUploadContainer").appendChild(div);
         counter++;
     } else {
         alert("Non e' possibile inserire piu' di "+ filesMaxNum +" files!");
     }
}
function RemoveFileUpload(div)
{
     document.getElementById("FileUploadContainer").removeChild(div.parentNode);
     counter--;
}

/* =============================================================================
 POP UP
 apre un Pop Up in base ai parametri fissati:

 h = altezza della pagina
 w = larghezza della pagina
 page = url della pagina
============================================================================= */
function popUp(w,h,page,name) {
    var screenH = (screen.height-h)/2-100;
    var screenW = (screen.width-w)/2;
    newWindow=window.open(page,name,"left="+screenW+",top="+screenH+",status=yes,location=no,scrollbars=yes,toolbar=no,directories=no,menubar=no,width="+w+",height="+h+",resizable=no");
        newWindow.creator=top;
        newWindow.focus();
    //newwindow.creator=top;
}
//onSubmit MODIFY
function confirmInsert() {
    return confirm('Inviare i dati?');
}
//onSubmit MODIFY
function confirmModify() {
    return confirm('Modificare il record corrente?');
}
//onSubmit DELETE
function confirmDelete() {
    return confirm('Cancellare il record corrente?\nATTENZIONE! L\'operazione non potra\' essere annullata');
}
//onSubmit ATTACH
function confirmAttach() {
    return confirm('Allegare un file al record corrente?');
}
//onSubmit IMAGE
function confirmImage() {
    return confirm('Allegare una immagine o una foto al record corrente?');
}
//onSubmit MOVE
function confirmMove() {
    return confirm("Spostare il record corrente?\nNon e' possibile spostare una sezione all'interno di se' stessa o di una sua sottosezione.");
}
//onSubmit HIDDEN
function confirmHidden() {
    return confirm('Nascondere il record corrente?');
}
//onSubmit SHOW
function confirmShow() {
    return confirm('Mostrare il record corrente?');
}
//onSubmit CHANGE ORDER
function confirmChangeOrder() {
    return confirm("Modificare l'ordine corrente?\n");
}
function changeSubmitValue(checkObj) {
    if (checkObj.checked == true) {
        document.forms['formRecord'].elements['invio'].value='New Insert';
    } else {
        document.forms['formRecord'].elements['invio'].value='Modify';
    }
}
//===================================
// funzione per inviare i vari form
function submitTheForm() {

    var oRadio = document.forms['formMateriale'].elements['mode'];

    var myForm = document.forms['formMateriale'];
    var message = "ATTENZIONE!\nSi è verificato un errore.\nI seguenti campi obbligatori non sono stati compilati.\n";
    var error = false;

    if (myForm.utente.value == '' || myForm.utente.value == null) {
        message += "- Nome\n";
        var error = true;
    }

    if (myForm.email.value == '' || myForm.email.value == null) {
        message += "- Email\n";
        var error = true;
    }
    if (myForm.idarea.value == '' || myForm.idarea.value == null) {
        message += "- Area\n";
        var error = true;
    }
    if (myForm.titolo.value == '' || myForm.titolo.value == null) {
        message += "- Titolo\n";
        var error = true;
    }
    if (myForm.descrizione.value == '' || myForm.descrizione.value == null) {
        message += "- Descrizione\n";
        var error = true;
    }
    for (var j=0;j<myForm.elements['mode'].length;j++) {
        if (oRadio[j].checked) {
            var oMode = oRadio[j].value;
        }
    }
    if (oMode == 'file') {
        if (document.formMateriale.file0.value == '' || document.formMateriale.file0.value == null) {
            message += "- File\n";
            var error = true;
        }
    } else if (oMode == 'link') {
        if (document.formMateriale.link.value == '' || document.formMateriale.link.value == null) {
            message += "- Link\n";
            var error = true;
        }
    }
    if (error == false) {
        if(confirmInsert() == true) {
            // imposta il div dell'aggiornamento pagina in primo piano
            popup('popUpDiv');
            var oSend = setTimeout("sendTheForm()",2000);
        }
    } else {
        alert(message);
    }
}
function sendTheForm() {
    document.forms['formMateriale'].submit();
}
//===================================
// funzione per chiedere la conferma se inviare o meno un record
// prima di inserire un allegato, immagine, video ecc...

function confirmPreSubmit() {
    var oConf = confirm('Prima di inserire un file è necessario inserire il documento nell\'archivio. Vuoi continuare?');
    if (oConf == true) {
        //document.forms[0].elements['action'].value = "preInsert";
        document.forms[0].submit();
    } else {
        return false;
    }
}
//===================================
// Elimina i tag HTML da un textarea (in text mode)
function strip_tags(idElement) {
    var oElement = document.getElementById(idElement);
    var oValue = oElement.value;
    var oNewValue = oValue.replace(/<.*?>/g,'');
    oElement.value = oNewValue;
}
//===================================
// Imposta un textarea come editor HTML o solo testo
function setHTMLorText(idForm,mode) {
    if (document.createElement && document.getElementById) {
        var oForm = document.getElementById(idForm);
        if (mode == 'html') {
            var oField = document.createElement("input");
            oField.setAttribute("type","hidden");
            oField.setAttribute("name","html");
            oField.setAttribute("id","html");
            oField.setAttribute("value","1");
            oForm.appendChild(oField);
        } else if (mode == 'text') {
            var oField = document.createElement("input");
            oField.setAttribute("type","hidden");
            oField.setAttribute("name","html");
            oField.setAttribute("id","html");
            oField.setAttribute("value","0");
            oForm.appendChild(oField);
        }
    }
}
//===================================
// Crea un nuovo elemento del dom tramite js
function createNewElement(oDivName,oElementName,oValue) {
    if (document.createElement && document.getElementById) {
        var oDiv = document.getElementById(oDivName);
        if (document.getElementById(oElementName) == null) {
            var oNewElement = document.createElement("input");
            oNewElement.setAttribute("type","hidden");
            oNewElement.setAttribute("name",oElementName);
            oNewElement.setAttribute("id",oElementName);
            oNewElement.setAttribute("value",oValue);
            oDiv.appendChild(oNewElement);
         }
    }
}
//===================================
// elimina un elemento del dom
function deleteIdElement(oDiv) {
    list_row = window.parent.document.getElementById(oDiv);
    alert(list_row);
    list_row.parentNode.removeChild(list_row);
}
//===================================
// cambia un valore di un id del dom
function changeIdValue(oDivId,oNewValue) {
    oDiv = document.getElementById(oDivId);
    oDiv.value = oNewValue;
    //alert(oValue);
}
//===================================
// Imposta il valore di un elemento uguale a quello di un altro elemento
function syncroElements(formId,valueFrom,valueTo) {
    document.forms[formId].elements[valueTo].value = document.forms[formId].elements[valueFrom].value;
}
//===================================
// cambia la classe di un elemento id
function changeClass(idToChange,classNameNew) {
    var idElement = window.document.getElementById(idToChange);
    idElement.className=classNameNew;
}
//===================================
// mostra un elemento id
function showIdElement(id) {
    var idElementToShow = window.parent.document.getElementById(id);
    if (idElementToShow != null) {
        var divClassNameToShow = idElementToShow.className;

        var toTestShow = /hide/;
        var checkShow = toTestShow.test(divClassNameToShow);
        newClassShow = divClassNameToShow.replace(toTestShow,'show');

        if (checkShow == true) {
            idElementToShow.className=newClassShow;
        }
    }
}
//===================================
// nasconde un elemento id
function hideIdElement(id) {
    var idElementToShow = window.parent.document.getElementById(id);
    if (idElementToShow != null) {
        var divClassNameToShow = idElementToShow.className;

        var toTestShow = /show/;
        var checkShow = toTestShow.test(divClassNameToShow);
        newClassShow = divClassNameToShow.replace(toTestShow,'hide');

        if (checkShow == true) {
            idElementToShow.className=newClassShow;
        }
    }
}
//===================================
// mostra o nasconde (alternativamente) un elemento id
function hideShowDomId(id) {
    var idElement = window.document.getElementById(id);
    var divClassName = idElement.className;
    var toTestShow = /hide/;
    var checkShow = toTestShow.test(divClassName);
    newClassShow = divClassName.replace(toTestShow,'show');
    var toTestHide = /show/;
    var checkHide = toTestHide.test(divClassName);
    newClassHide = divClassName.replace(toTestHide,'hide');
    if (checkShow == true) {
        document.getElementById(id).disabled = false;
        idElement.className=newClassShow;
    }
    if (checkHide == true) {
        document.getElementById(id).disabled = true;
        idElement.className=newClassHide;
    }
}
//===================================
// mostra o nasconde (alternativamente) un elemento id sulla base del valore
// passato dalla variabile obj
function hideShowIdElementOnChange(objName,oValue,idToShow,idToHide) {
    if (objName.value == 'file') {
        var idElementToShow = window.document.getElementById(idToShow);
        idElementToShow.className = 'show';
        var idElementToHide = window.document.getElementById(idToHide);
        idElementToHide.className = 'hide';
        //~ var toTestShow = /hide/;
        //~ var checkShow = toTestShow.test(divClassName);
        //~ newClassShow = divClassName.replace('hide','show');
        //~ if (checkShow == true) {
            //~ document.getElementById(idToShow).disabled = false;
            //~ idElement.className=newClassShow;
        //~ }
    } else if (objName.value == 'link') {
        var idElementToShow = window.document.getElementById(idToShow);
        //~ alert(idElementToShow);
        idElementToShow.className = 'hide';
        var idElementToHide = window.document.getElementById(idToHide);
        idElementToHide.className = 'show';
        //~ var idElement = window.document.getElementById(idToShow);
        //~ var divClassName = idElement.className;
        //~ var toTestHide = /show/;
        //~ var checkHide = toTestHide.test(divClassName);
        //~ newClassHide = divClassName.replace(toTestHide,'hide');

        //~ if (checkHide == true) {
            //~ document.getElementById(idToShow).disabled = true;
            //~ idElement.className=newClassHide;
        //~ }
    }
}
//===================================
// mostra o nasconde (alternativamente) un elemento id
function hideShowIdElement(id,field) {
    var idElementToShow = window.parent.document.getElementById(id);
    var divClassNameToShow = idElementToShow.className;

    var toTestShow = /hide/;
    var checkShow = toTestShow.test(divClassNameToShow);
    newClassShow = divClassNameToShow.replace(toTestShow,'show');

    var toTesthide = /show/;
    var checkhide = toTesthide.test(divClassNameToShow);
    newClasshide = divClassNameToShow.replace(toTesthide,'hide');
    if (checkShow == true) {
        if (document.getElementById(field) != null) {
            if (field != '')
                document.getElementById(field).disabled = false;
        }
        idElementToShow.className=newClassShow;
    }
    if (checkhide == true) {
        if (document.getElementById(field) != null) {
            if (field != '')
                document.getElementById(field).disabled = true;
        }
        idElementToShow.className=newClasshide;
    }

}


/* =============================================================================
INCREMENTO DATA
incrementa tramite il pulsante relativo la data di una unita'
============================================================================= */
function incData(campo,syncroField) {
    if (campo.value != '' && campo.value != '00-00-0000') {
        // VARIABILI
        var gg,mm,aaaa;
        // RICAVA IL VALORE DAL NOME DEL CAMPO IMPUT-TEXT
        var data = campo.value;
        gg = data.substr(0,2);
        mm = data.substr(3,2);
        aaaa = data.substr(6,4);
        prova = typeof(gg);
        //window.alert(prova);
        // GIORNO SUCCESSIVO
        gg = ++gg;
        // CONVERTE IN STRINGA
        ggNew = String(gg);
        // SE IL GIORNO e' MINORE DI 10 AGGIUNGE LO 0 DAVANTI
        for (x=0;x<10;x++) {
            if (gg == x) ggNew = '0'+gg;
        }
        // MESE SUCCESSIVO
        // trenta giorni a novembre, con aprile, giugno ....
        if (gg > '30' && (mm == '11' || mm == '04' || mm == '06' || mm == '09')) {
            mm = ++mm;
            mmNew = String(mm);
            for (y=0;y<10;y++) {
                if (mm == y) {
                    mmNew = '0'+mm;
                }
            }
            ggNew = '01';
        // .. di 28 ce n'e' uno ....
        } else if (gg > '28' && mm == '02') {
            mm = ++mm;
            mmNew = String(mm);
            for (y=0;y<10;y++) {
                if (mm == y) {
                    mmNew = '0'+mm;
                }
            }
            ggNew = '01';
        // ... tutti gli altri ne han 31!
        } else if (gg > '31' && ( mm == '01' || mm == '03' || mm == '05' || mm == '07' || mm == '08' || mm == '10' || mm == '11' || mm == '12')) {
            if (mm == '12') {
                mmNew = '01';
            } else {
                mm = ++mm;
                mmNew = String(mm);
                for (y=0;y<10;y++) {
                    if (mm == y) {
                        mmNew = '0'+mm;
                    }
                }
            }
            ggNew = '01';
        } else {
           mmNew = mm;
        }
        // ANNO SUCCESSIVO
        if (gg>'31' && mm=='12')
           aaaaNew = ++aaaa;
        else
           aaaaNew = aaaa;
        dateNew = ggNew+'-'+mmNew+'-'+aaaaNew;
        campo.value =  dateNew;

        if(document.getElementById(syncroField)) { //alert('ciao');
        //if (data2.value != '' && data2.value != '00-00-0000')
            document.forms[0].elements[syncroField].value =  dateNew;
        }
        return(dateNew);
    } else {
        alert("Attenzione!\nAffinché il pulsante funzioni, il campo non può essere vuoto o uguale a '00-00-0000'.");
    }
}
/* =============================================================================
DECREMENTO DATA
decrementa tramite il pulsante relativo la data di una unita'
============================================================================= */
function decData(campo,syncroField) {
    if (campo.value != '' && campo.value != '00-00-0000') {
        // VARIABILI
        var gg,mm,aaaa;
        // RICAVA IL VALORE DAL NOME DEL CAMPO IMPUT-TEXT
        var data = campo.value;
        gg = data.substr(0,2);
        mm = data.substr(3,2);
        aaaa = data.substr(6,4);
        // GIORNO PRECEDENTE
        gg = --gg;
        // CONVERTE IN STRINGA
        ggNew = String(gg);
        // SE IL GIORNO e' MINORE DI 10 AGGIUNGE LO 0 DAVANTI
        for (x=0;x<10;x++) {
            if (gg == x) ggNew = '0'+gg;
        }
        // MESE PRECEDENTE
        if (gg < '01' && (mm == '12' || mm == '05' || mm == '07' || mm == '10')) {
            mm = --mm;
            mmNew = String(mm);
            for (y=0;y<10;y++) {
                if (mm == y) {
                    mmNew = '0'+mm;
                }
            }
            ggNew = '30';
        } else if (gg < '01' && mm == '03') {
            mm = --mm;
            mmNew = String(mm);
            for (y=0;y<10;y++) {
                if (mm == y) {
                    mmNew = '0'+mm;
                }
            }
            ggNew = '28';
        } else  if (gg < '01' && (mm == '02' || mm == '04' || mm == '06' || mm == '08' || mm == '09' || mm == '11' || mm == '12' || mm == '01')) {
           if (mm == '01') {
               mmNew = '12';
           } else {
               ggNew = '31';
                mm = --mm;
                mmNew = String(mm);
                for (y=0;y<10;y++) {
                    if (mm == y) {
                        mmNew = '0'+mm;
                    }
                }
            }
            ggNew = '31';
        } else {
        //mm = ++mm;
        // CONVERTE IN STRINGA
           mmNew = mm;
        }
        // ANNO SUCCESSIVO
        if (gg<'01' && mm=='01')
           aaaaNew = --aaaa;
        else
           aaaaNew = aaaa;
        dateNew = ggNew+'-'+mmNew+'-'+aaaaNew;
        campo.value =  dateNew;

        if(document.getElementById(syncroField)) { //alert('ciao');
        //if (data2.value != '' && data2.value != '00-00-0000')
            document.forms[0].elements[syncroField].value =  dateNew;
        }
        return(dateNew);
    } else {
        alert("Attenzione!\nAffinché il pulsante funzioni, il campo non può essere vuoto o uguale a '00-00-0000'.");
    }
}
/* =============================================================================
INCREMENTO ORA
incrementa tramite il pulsante relativo l'ora di una unita'
============================================================================= */
function incOra(campo) {
    //alert  (document.forms[0].campo.value);
    //alert  (campo.value);
    if (campo.value != '') {
        // VARIABILI
        var h,altro;

        // RICAVA IL VALORE DAL NOME DEL CAMPO IMPUT-TEXT
        var ora = campo.value;
        h = ora.substr(0,2);
        min_sec = ora.substr(2,6);
        // Ora successiva
        h = ++h;
            if (h > 24) h = 0;

        // CONVERTE IN STRINGA
        hNew = String(h);
        // SE l'ora e' MINORE DI 10 AGGIUNGE LO 0 DAVANTI
        for (x=0;x<10;x++) {
            if (h == x) hNew = '0'+h;
        }
        oraNew = hNew+min_sec;
        campo.value =  oraNew;
        return(oraNew);
     } else {
        //alert('Attenzione!\\nAffinché il pulsante funzioni, il campo non può essere vuoto.');
        oraNew =  '21:00';
        campo.value =  oraNew;
        return(oraNew);
    }
}
/* =============================================================================
DECREMENTO ORA
decrementa tramite il pulsante relativo la data di una unita'
============================================================================= */
function decOra(campo) {
    if (campo.value != '') {
        // VARIABILI
        var h,altro;
        // RICAVA IL VALORE DAL NOME DEL CAMPO IMPUT-TEXT
        var ora = campo.value;
        h = ora.substr(0,2);
        min_sec = ora.substr(2,6);
        // Ora successiva
        h = --h;
            if (h < 0) h = 24;
        // CONVERTE IN STRINGA
        hNew = String(h);
        // SE l'ora e' MINORE DI 10 AGGIUNGE LO 0 DAVANTI
        for (x=0;x<10;x++) {
            if (h == x) hNew = '0'+h;
        }
        oraNew = hNew+min_sec;
        campo.value =  oraNew;
        return(oraNew);
     } else {
        //alert('Attenzione!\\nAffinché il pulsante funzioni, il campo non può essere vuoto.');
        oraNew =  '21:00';
        campo.value =  oraNew;
        return(oraNew);
    }
}
/* =============================================================================
INCREMENTO MINUTI
incrementa tramite il pulsante relativo l'ora di una unita'
============================================================================= */
function incMinuti(campo) {
    if (campo.value != '') {
        // VARIABILI
        var h,min,sec;
        // RICAVA IL VALORE DAL NOME DEL CAMPO IMPUT-TEXT
        var minuti = campo.value;
        h = minuti.substr(0,2);
        min = minuti.substr(3,2);
        if (min == '00') minNew = '15';
        if (min == '15') minNew = '30';
        if (min == '30') minNew = '45';
        if (min == '45') minNew = '00';
        minutiNew = h+':'+minNew+':00';
        campo.value =  minutiNew;
        return(minutiNew);
     } else {
        //alert('Attenzione!\\nAffinché il pulsante funzioni, il campo non può essere vuoto.');
        oraNew =  '21:00:00';
        campo.value =  oraNew;
        return(oraNew);
    }
}
/* =============================================================================
DECREMENTO MINUTI
DECREMENTa tramite il pulsante relativo l'ora di una unita'
============================================================================= */
function decMinuti(campo) {
    if (campo.value != '') {
        // VARIABILI
        var h,min,sec;
        // RICAVA IL VALORE DAL NOME DEL CAMPO IMPUT-TEXT
        var minuti = campo.value;
        h = minuti.substr(0,2);
        min = minuti.substr(3,2);
        //sec = minuti.substr(5,2);
        if (min == '00') minNew = '45';
        if (min == '45') minNew = '30';
        if (min == '30') minNew = '15';
        if (min == '15') minNew = '00';
        minutiNew = h+':'+minNew+':00';
        campo.value =  minutiNew;
        return(minutiNew);
     } else {
        //alert('Attenzione!\\nAffinché il pulsante funzioni, il campo non può essere vuoto.');
        oraNew =  '21:00:00';
        campo.value =  oraNew;
        return(oraNew);
    }

}
/* =============================================================================
AUMENTO E DECREMENTO ANNO
decrementa tramite il pulsante relativo l'anno corrente di una unita'
============================================================================= */
function annoIncrOrDecr(campo,action) {
    if (campo.value != '') {
        var annoCorr = campo.value;
        if (action == '-') {
            var nuovoAnno = parseInt(annoCorr) - 1;
        } else if (action == '+') {
            var nuovoAnno = parseInt(annoCorr) + 1;
        }
        campo.value = nuovoAnno;
    }
}
