/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function prendiElementoDaId(id_elemento) {
    // elemento da restituire
    var elemento;

    // se esiste il metodo getElementById
    // questo if sarÃ  diverso da false, null o undefined
    // e sarÃ  quindi considerato valido, come un true
    if(document.getElementById)
        elemento = document.getElementById(id_elemento);

    // altrimenti Ã¨ necessario usare un vecchio sistema
    else
        elemento = document.all[id_elemento];

    // restituzione elemento
    return elemento;
}

function controll_Config(){

    var hostname = prendiElementoDaId('smtphostname');
    var authuser = prendiElementoDaId('smtpauthuser');
    var authpsw = prendiElementoDaId('smtpauthpwd');
    var mittente = prendiElementoDaId('mittentestandard');

    var recordpag = prendiElementoDaId('recordpag');
    var maximage = prendiElementoDaId('maximageviewall');
    var maxvideo = prendiElementoDaId('maxvideoviewall');
    var forum = prendiElementoDaId('urlforum');

    var descrizioneazienda = prendiElementoDaId('descrizioneazienda');
    var contatto = prendiElementoDaId('contattoazienda');
    var googlemap = prendiElementoDaId('sourcegmaps');
    var condizioni = prendiElementoDaId('condizioniuso');


    var ris=0;
    var err='';

    if(hostname.value == '' || hostname.value.indexOf('.') <=0 ){
        err+=(ris+1)+' - Errore sul campo Smtp Server\n';
        hostname.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        hostname.style.backgroundColor = '#90ee90';
    }


    if(authuser.value == ''  || authuser.value.indexOf('@') <= 0 || authuser.value.indexOf('.') <=0 ){
        err+=(ris+1)+' - Errore sul campo UserName del server SMTP\n';
        authuser.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        authuser.style.backgroundColor = '#90ee90';
    }


    if(authpsw.value == ''){
        err+=(ris+1)+' - Errore sul campo Password del server SMTP  \n';
        authpsw.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        authpsw.style.backgroundColor = '#90ee90';
    }

    if(mittente.value == '' || mittente.value.indexOf('@') <= 0 || mittente.value.indexOf('.') <=0 ){
        err+=(ris+1)+' - Errore sul campo Mittente delle Email Automatiche\n';
        mittente.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        mittente.style.backgroundColor = '#90ee90';
    }

    if(recordpag.value == '' || isNaN(recordpag.value) ){
        err+=(ris+1)+' - Errore sul campo Record Visibili per Pagina\n';
        recordpag.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        recordpag.style.backgroundColor = '#90ee90';
    }

    if(maximage.value == '' || isNaN(maximage.value) ){
        err+=(ris+1)+' - Errore sul campo Numero Massimo di Immagini\n';
        maximage.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        maximage.style.backgroundColor = '#90ee90';
    }

    if(maxvideo.value == '' || isNaN(maxvideo.value) ){
        err+=(ris+1)+' - Errore sul campo Numero Massimo di Video\n';
        maxvideo.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        maxvideo.style.backgroundColor = '#90ee90';
    }

    if(forum.value == '' || forum.value.indexOf('.') <=0 ){
        err+=(ris+1)+' - Errore sul campo Link del Forum \n';
        forum.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        forum.style.backgroundColor = '#90ee90';
    }

    /*if(descrizioneazienda.value == '' ){
        err+=(ris+1)+' - Errore sul campo Descrizione Aziendale\n';
        descrizioneazienda.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        descrizioneazienda.style.backgroundColor = '#90ee90';
    }*/


    /*if(contatto.value == '' ){
        err+=(ris+1)+' - Errore sul campo Contatto Aziendale\n';
        contatto.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        contatto.style.backgroundColor = '#90ee90';
    }*/

    if(googlemap.value == '' || googlemap.value.indexOf('.') <=0 ){
        err+=(ris+1)+' - Errore sul campo Localizzazione di GoogleMap\n';
        googlemap.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        googlemap.style.backgroundColor = '#90ee90';
    }

    /*if(condizioni.value == '' ){
        err+=(ris+1)+' - Errore sul campo Condizioni d\'Uso\n';
        condizioni.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        condizioni.style.backgroundColor = '#90ee90';
    }*/

    if(ris>0){
        alert(ris+'   Errori riscontrati\n' + '---------------------------------------------\n'+err);
        return false;
    }else{
        return true;
    }
}

function controll_register(){
    
    var nome = prendiElementoDaId('nome');
    var cognome = prendiElementoDaId('cognome');
    var email = prendiElementoDaId('email');
    var condizioni = prendiElementoDaId('condizioni');

    var ris=0;
    var err='';

    if(nome.value == ''){
        err+=(ris+1)+' - Errore sul campo Nome\n';
        nome.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        nome.style.backgroundColor = '#90ee90';
    }
    if(cognome.value == ''){
        err+=(ris+1)+' - Errore sul campo cognome\n';
        cognome.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        cognome.style.backgroundColor = '#90ee90';
    }
    if(email.value == '' || email.value.indexOf('@') <= 0 || email.value.indexOf('.') <=0 ){
        err+=(ris+1)+' - Errore sul campo email\n';
        email.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        email.style.backgroundColor = '#90ee90';
    }

    if(condizioni.checked == false){
        err+=(ris+1)+' - Accettare le condizioni\n';
        condizioni.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        condizioni.style.backgroundColor = '#90ee90';
    }


    if(ris>0){
        alert(ris+'   Errori riscontrati\n' + '---------------------------------------------\n'+err);
        return false;
    }else{
        return true;
    }
}


function controll_Tecnologia(){

    var nome = prendiElementoDaId('nome');
    //var desc = prendiElementoDaId('descrizione');

    //alert(desc.value);
    
    var ris=0;
    var err='';

    if(nome.value == ''){
        err+=(ris+1)+' - Errore sul campo Nome\n';
        nome.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        nome.style.backgroundColor = '#90ee90';
    }
    /*if(desc.value.length == 0){
        err+=(ris+1)+' - Errore sul campo Descrizione\n';
        desc.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        desc.style.backgroundColor = '#90ee90';
    }*/
    

    if(ris>0){
        alert(ris+'   Errori riscontrati\n' + '---------------------------------------------\n'+err);
        return false;
    }else{
        return true;
    }
}

function controll_Prodotto(){

    var nome = prendiElementoDaId('nome');
    var desc = prendiElementoDaId('descrizione');

    //alert(desc.value);
    
    var ris=0;
    var err='';

    if(nome.value == ''){
        err+=(ris+1)+' - Errore sul campo Nome\n';
        nome.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        nome.style.backgroundColor = '#90ee90';
    }
    /*if(desc.value.length == 0){
        err+=(ris+1)+' - Errore sul campo Descrizione\n';
        desc.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        desc.style.backgroundColor = '#90ee90';
    }*/
    

    if(ris>0){
        alert(ris+'   Errori riscontrati\n' + '---------------------------------------------\n'+err);
        return false;
    }else{
        return true;
    }
}

function controll_Progetto(){

    var nome = prendiElementoDaId('nome');
    var cognome = prendiElementoDaId('descrizione');

    var ris=0;
    var err='';

    if(nome.value == ''){
        err+=(ris+1)+' - Errore sul campo Nome\n';
        nome.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        nome.style.backgroundColor = '#90ee90';
    }
    /*if(cognome.value == ''){
        err+=(ris+1)+' - Errore sul campo Descrizione\n';
        cognome.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        cognome.style.backgroundColor = '#90ee90';
    }*/


    if(ris>0){
        alert(ris+'   Errori riscontrati\n' + '---------------------------------------------\n'+err);
        return false;
    }else{
        return true;
    }
}

function controll_Update(){

    var nome = prendiElementoDaId('titolo');

    var ris=0;
    var err='';

    if(nome.value == ''){
        err+=(ris+1)+' - Errore sul campo Titolo\n';
        nome.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        nome.style.backgroundColor = '#90ee90';
    }
    /*if(cognome.value == ''){
        err+=(ris+1)+' - Errore sul campo Descrizione\n';
        cognome.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        cognome.style.backgroundColor = '#90ee90';
    }*/


    if(ris>0){
        alert(ris+'   Errori riscontrati\n' + '---------------------------------------------\n'+err);
        return false;
    }else{
        return true;
    }
}


var imgSel=-1;

function setImage(el,id,type){
    //alert(el);

    if(id!=imgSel){
        if(imgSel!=-1){
            var oldImg = prendiElementoDaId('divimg_'+imgSel);
            //alert(oldImg)
            oldImg.style.border = '0px solid #892105';
            oldImg.style.padding = '0px';
        }

        imgSel = id;

        var link = prendiElementoDaId('setImg');

        el.style.border = '1px solid #892105';
        el.style.padding = '3px';

        if(type=='prod')link.href = 'delImageProdServlet?iid='+id;
        if(type=='proj')link.href = 'delImageProjServlet?iid='+id;
        if(type=='tech')link.href = 'delImageTechServlet?iid='+id;
        if(type=='gallery')link.href = 'delImageGalleryServlet?iid='+id;
    }
}

function control_ImageSelect(){
    if(imgSel==-1){
        alert('Selezionare almeno un\'immagine dalla lista.');
        return false;
    }else{
        return confirm("Eliminare definitavamente l\'immagine selezionata?");
    }
}


function changeFormAction(c,prodotto,progetto,tecnologia,numimg,maximg){
    var form = prendiElementoDaId('formAddImg');

    //alert(form.action);
    if(numimg<maximg){
        if(prodotto > -1){
            if(c.checked == false){
                form.action='addImageServlet?prodotto='+prodotto+'&progetto=-1&tecnologia=-1&update=false';
            }else{
                form.action='addImageServlet?prodotto='+prodotto+'&progetto=-1&tecnologia=-1&update=true';
            }
        }
        if(progetto > -1){
            if(c.checked == false){
                form.action='addImageServlet?prodotto=-1&progetto='+progetto+'&tecnologia=-1&update=false';
            }else{
                form.action='addImageServlet?prodotto=-1&progetto='+progetto+'&tecnologia=-1&update=true';
            }
        }
        if(tecnologia > -1){
            if(c.checked == false){
                form.action='addImageServlet?prodotto=-1&progetto=-1&tecnologia='+tecnologia+'&update=false';
            }else{
                form.action='addImageServlet?prodotto=-1&progetto=-1&tecnologia='+tecnologia+'&update=true';
            }
        }
    }else{
        alert('E\' stato raggiunto il Numero Massimo di inserimenti di immagini senza Aggiornamenti.\nVerrà forzato l\'inserimento negli update.');
        if(prodotto>-1){
            form.action='addImageServlet?prodotto='+prodotto+'&progetto=-1&tecnologia=-1&update=true';
            c.checked = true;
        }
        if(progetto>-1){
            form.action='addImageServlet?prodotto=-1&progetto='+progetto+'&tecnologia=-1&update=true';
            c.checked = true;
        }
        if(tecnologia>-1){
            form.action='addImageServlet?prodotto=-1&progetto=-1&tecnologia='+tecnologia+'&update=true';
            c.checked = true;
        }
    }

//alert(form.action);

}

function changeFormActionFileNorma(c,norma){
    var form = prendiElementoDaId('formAddFile');

    // Cambio il form
    if(c.checked == false){
        form.action='addFileServlet?norma='+norma+'&update=false';
    }else{
        form.action='addFileServlet?norma='+norma+'&update=true';
    }

//alert(form.action);

}

function changeFormActionFileProdotto(c,pid){
    var form = prendiElementoDaId('formAddFile');

    // Cambio il form
    if(c.checked == false){
        form.action='addFileProdServlet?pid='+pid+'&update=false';
    }else{
        form.action='addFileProdServlet?pid='+pid+'&update=true';
    }

//alert(form.action);

}

function changeFormActionFileProgetto(c,pid){
    var form = prendiElementoDaId('formAddFile');

    // Cambio il form
    if(c.checked == false){
        form.action='addFileProjServlet?pid='+pid+'&update=false';
    }else{
        form.action='addFileProjServlet?pid='+pid+'&update=true';
    }

//alert(form.action);

}

function changeFormActionFileTecnologia(c,tid){
    var form = prendiElementoDaId('formAddFile');

    // Cambio il form
    if(c.checked == false){
        form.action='addFileTechServlet?tid='+tid+'&update=false';
    }else{
        form.action='addFileTechServlet?tid='+tid+'&update=true';
    }

//alert(form.action);

}

function controll_ModFile(){
    var nome = prendiElementoDaId('nome');
    var ris=0;
    var err='';

    if(nome.value == ''){
        err+=(ris+1)+' - Errore sul campo Nome\n';
        nome.style.backgroundColor = '#ff7f50';
        ris++;
    }else{
        nome.style.backgroundColor = '#90ee90';
    }
    
    if(ris>0){
        alert(ris+'   Errori riscontrati\n' + '---------------------------------------------\n'+err);
        return false;
    }else{
        return true;
    }
}

function controll_AddUpdate(max,num,link){

    if(num>=max){
        alert('Limite Massimo di Aggiornamenti Raggiunto!!!');
        window.location = link;
        return false;
    }else{

        var nome = prendiElementoDaId('titolo');
        var ris=0;
        var err='';

        if(nome.value == ''){
            err+=(ris+1)+' - Errore sul campo Titolo\n';
            nome.style.backgroundColor = '#ff7f50';
            ris++;
        }else{
            nome.style.backgroundColor = '#90ee90';
        }

        if(ris>0){
            alert(ris+'   Errori riscontrati\n' + '---------------------------------------------\n'+err);
            return false;
        }else{
            return true;
        }
    }
}

function controll_AddImage(imgMax,numImg,link){

    if(numImg>=imgMax){
        alert('Limite Massimo di Immagini Raggiunto!!!');
        window.location = link;
        return false;
    }else{

        var nome = prendiElementoDaId('nome');
        var ris=0;
        var err='';

        if(nome.value == ''){
            err+=(ris+1)+' - Errore sul campo Nome\n';
            nome.style.backgroundColor = '#ff7f50';
            ris++;
        }else{
            nome.style.backgroundColor = '#90ee90';
        }

        if(ris>0){
            alert(ris+'   Errori riscontrati\n' + '---------------------------------------------\n'+err);
            return false;
        }else{
            return true;
        }
    }
}

function controll_AddVideo(vidMax, numVid, link){
    if(numVid>=vidMax){
        alert('Limite Massimo di Video Raggiunto!!!');
        window.location = link;
        return false;
    }else{

        var nome = prendiElementoDaId('nome');
        var url = prendiElementoDaId('url');
        var ris=0;
        var err='';

        if(nome.value == ''){
            err+=(ris+1)+' - Errore sul campo Nome\n';
            nome.style.backgroundColor = '#ff7f50';
            ris++;
        }else{
            nome.style.backgroundColor = '#90ee90';
        }

        if(url.value == ''){
            err+=(ris+1)+' - Errore sul campo Codice YouTube\n';
            url.style.backgroundColor = '#ff7f50';
            ris++;
        }else{
            url.style.backgroundColor = '#90ee90';
        }

        if(ris>0){
            alert(ris+'   Errori riscontrati\n' + '---------------------------------------------\n'+err);
            return false;
        }else{
            return true;
        }
    }
}

function controll_AddFile(vidMax, numVid, link){
    if(numVid>=vidMax){
        alert('Limite Massimo di File Raggiunto!!!');
        window.location = link;
        return false;
    }else{

        var nome = prendiElementoDaId('file');
        var ris=0;
        var err='';

        if(nome.value == ''){
            err+=(ris+1)+' - Errore sul campo File\n';
            nome.style.backgroundColor = '#ff7f50';
            ris++;
        }else{
            nome.style.backgroundColor = '#90ee90';
        }

        if(ris>0){
            alert(ris+'   Errori riscontrati\n' + '---------------------------------------------\n'+err);
            return false;
        }else{
            return true;
        }
    }
}

//***************************** BLOCCO PER LA GESTIONE DELLE CATEGORIE *********************************

var idCatSel=-99;
var idSopraSel = -99;
var red = '#ff7f50';
var blu = '#001e98';
var grigio = '#888';
var blocco_addCat=true;
var blocco_modCat=true;
var blocco_delCat=true;

function selectCat(v,idcat,idsopracat, sottocat,tipo){

    if(idCatSel!=-99){
        var select = prendiElementoDaId('menu_'+idCatSel+'_'+idSopraSel);
        select.style.fontWeight = 'normal';
        select.style.color = blu;
    }

    v.style.fontWeight = 'bold';
    v.style.color = red;
    idCatSel= idcat;
    idSopraSel = idsopracat;


    // Pulsanti
    // Gestione dei pulsanti di ADD MOD e DEL

    var add_btn = prendiElementoDaId('addCategoria');
    var mod_btn = prendiElementoDaId('modCategoria');
    var del_btn = prendiElementoDaId('delCategoria');
    var console = prendiElementoDaId('consoleCat');

    // RESETTO ADD
    blocco_addCat=true;
    add_btn.style.color = grigio;
    add_btn.href = 'javascript:addCategoria(-99,-99)';
    // RESETTO MOD
    blocco_modCat=true;
    mod_btn.style.color = grigio;
    mod_btn.href = 'javascript:modCategoria('+idcat+','+tipo+')';
    // RESETTO DEL
    blocco_delCat=true;
    del_btn.style.color = grigio;
    del_btn.href = 'javascript:delCategoria('+idcat+','+sottocat+')';


    if(idcat== -2){
        //Significa che ho preso il root dell'albero'
        blocco_addCat=false;
        add_btn.style.color = blu;
        add_btn.href = 'javascript:addCategoria('+idcat+','+tipo+')';
        console.innerHTML= 'Selezionata: '+v.innerHTML+' - Base delle Categorie';
    }

    if(idcat>-2){
        blocco_addCat=false;
        add_btn.style.color = blu;
        add_btn.href = 'javascript:addCategoria('+idcat+','+tipo+')';
        console.innerHTML= 'Selezionata: '+v.innerHTML;
    
        blocco_modCat=false;
        mod_btn.style.color = blu;
        mod_btn.href = 'javascript:modCategoria('+idcat+','+tipo+')';
        console.innerHTML= 'Selezionata: '+v.innerHTML;
    }
    if(sottocat==0 && idcat>-2){
        blocco_delCat=false;
        del_btn.style.color = blu;
        del_btn.href = 'javascript:delCategoria('+idcat+','+sottocat+')';
        console.innerHTML= 'Selezionata: '+v.innerHTML;
    }

}

function addCategoria(idCat,tipo){
    if(idCat==-99){
        alert('Selezionare almeno una categoria!!');
    }else{
        if(blocco_addCat){
            alert('Non e possibile aggiungere una Sotto Categoria!!');
        }else{
            // Redirect alla pagina index.jsp?do=addCategoria&cid=idCat

            window.location = 'index.jsp?do=addCategoria&cid='+idCat+'&tipo='+tipo;

        }
    }
}

function modCategoria(idCat,tipo){
    if(idCat==-99){
        alert('Selezionare almeno una categoria!!');
    }else{
        if(idCat==-2){
            // ROOT
            alert("Non è possibile modificare la Base delle Categorie")
        }else{
            if(blocco_modCat){
                alert('Non e possibile modificare una Sotto Categoria!!');
            }else{
                window.location = 'index.jsp?do=modCategoria&cid='+idCat+'&tipo='+tipo;
            }
        }
    }
}

function delCategoria(idCat,sottoCat){
    alert('Eliminando la categoria verranno eliminati tutti i collegamenti ad essa');
    if(idCat==-99){
        alert('Selezionare almeno una categoria!!');
    }else{
        if(idCat==-2){
            // ROOT
            alert("Non è possibile cancellare la Base delle Categorie")
        }else{
            if(sottoCat>0){
                alert('Non è possibile ELIMINARE una categoria che contiene delle Sotto-Categorie')
            }else{
                if(blocco_delCat){
                    alert('Non e possibile eliminare una Categoria!!');
                }else{
                    if(confirm('Sei sicuro di di eliminare la categoria?'))
                        window.location = 'delCategoriaServlet?idcat='+idCat;
                }
            }
        }
    }
}


//**************************************************************************************************************


function selectCat2(v,idcat,idsopracat, sottocat,tipo){

    if(idCatSel!=-99){
        var select = prendiElementoDaId('menu_'+idCatSel+'_'+idSopraSel);
        select.style.fontWeight = 'normal';
        select.style.color = blu;
    }

    v.style.fontWeight = 'bold';
    v.style.color = red;
    idCatSel= idcat;
    idSopraSel = idsopracat;

    var console = prendiElementoDaId('consoleCat');

    var catsel = prendiElementoDaId('menu_'+idCatSel+'_'+idSopraSel);


    console.innerHTML = 'Selezionata la Categoria ['+catsel.innerHTML+']';

    var hidden = prendiElementoDaId("cat");
  
    hidden.value = idcat;

    

}

//********************* TREE ********************************

function openCategoria(cid,type){

    var typejsp = '';
    if(type == "Prodotti"){
        typejsp = 'prod';
    }
    if(type == "Progetti"){
        typejsp = 'proj';
    }
    if(type == "Norme"){
        typejsp = 'norma';
    }
    if(type == "Tecnologie"){
        typejsp = 'tech';
    }

    if(cid>0){
        window.location = 'index.jsp?do='+typejsp+'&act=bycat&cid='+cid;
    }
    
    li.style.display = "none";
}


//*********************** GALLERY ****************************

function caricaImmagine(urlImg){
    
    var URLImg=prendiElementoDaId('imgBig');

    
    URLImg.src=urlImg;
    var altezza=URLImg.height;
    var larghezza=URLImg.width;

  
    var max_larghezza=496;
    
    if(larghezza > max_larghezza){
        var max_altezza=Math.round((max_larghezza*altezza)/larghezza);
        URLImg.heigth=max_altezza;
        URLImg.width=max_larghezza;
    }
    
}
