﻿// JScript File

function ControllaDatiTxtVuota(txtDaContr,Messaggio){
/*Se la TextBox txtDaContr è vuota --> FALSE e apre un Popup con scritto MESSAGGIO
                altrimenti         --> TRUE
! La TextBox viene vista in javascript come un INPUT*/
    if (trimSTR(txtDaContr.value) == ''){
        alert(Messaggio);
        return false;
    }
    return true;
}

function ControllaListBoxSel(lstBox,messNonSel,messConferma){
/*p.e. ControllaListBoxSel(ctl00_ContentPlaceHolder1_lstRuoli,'Scegliere Ruolo','Sei sicuro di voler eliminare il RUOLO:')
Se il ListBox lstBox ha un elemento selezionato, chiede conferma di eliminazione --> TRUE/FALSE
altrimenti         --> FALSE  e apre un Popup con scritto 'Scegliere Ruolo'
! Il ListBox viene visto in javascript come un SELECT*/
    if (lstBox.selectedIndex == -1){
        alert(messNonSel);
        return false;
    }else{
        return confirm(messConferma + ' ' + lstBox.options[lstBox.selectedIndex].text + '?');
    }
}
        
function SelezionatoListBoxPerTextBox(lstBox,txtBox){
/*Se il ListBox lstBox ha un elemento selezionato --> scrive nella TextBox txtBox il testo selezionato
    altrimenti         --> scrive nella TextBox txtBox ''
! Il ListBox viene visto in javascript come un SELECT*/
    var iInd;
    iInd = lstBox.selectedIndex;
    if (iInd != -1){
        txtBox.value = lstBox.options[iInd].value;
    }else{
        txtBox.value = '';
    }
}
        
function esisteStringaInLstBox (lstBox,stringa){
/* Verifica se nel ListBox lstBox, esiste un elemento di valore STRINGA
restituisce TRUE/FALSE*/
    stringa=stringa.toLowerCase();
    if (stringa =='') return false;
    var iLstBoxItems;
    iLstBoxItems = lstBox.options.length;
    for (i=0; i < iLstBoxItems ; i++){
        if (lstBox.options[i].value.toString().toLowerCase() == stringa) return true;
    }
    return false;
}
        
function checkDatiBtnAdd(txtBox,lstBox,messTxt,messLst,messConferma){
/*Verifica che:
txtBox sia non vuota
lstBox non contenga già il dato contenuto in txtBox
messTxt         --> Messaggio nel caso in cui txtBox.value == ''
messLst         --> Messaggio nel caso in cui il lstBox contenga già la voce presente in txtBox
messConferma + txtBox.value   --> 'Sei sicuro di ... ? '*/

    //Verifica che la TextBox sia vuota
    if (!ControllaDatiTxtVuota(txtBox,messTxt)) return false;
    //Verifica che txtBox.value non sia già presente nel ListBox
    if (esisteStringaInLstBox(lstBox,trimSTR(txtBox.value))){
        alert(messLst);
        return false;
    }
    return confirm(messConferma + ' ' + txtBox.value + '?');
}
   
    function azzeraControlli(arrTextBox,arrListBox,arrLabel,arrNomiTreeView){
    //arrTextBox        --> Array di textBox da azzerare
    //arrListBox        --> Array di ListBox da deselezionare
    //arrLabel          --> Array di Label, Div, TextArea da deselezionare
    //arrNomiTreeView   --> Array di nomi di TreeView da deselezionare (p.e. 'tvwRuoli, tvwUtenti, ...)
    
        //TextBox.Text = ""
        if(arrTextBox){
            for(i=0; i<arrTextBox.length; i++){
                arrTextBox[i].value='';
            }
        }
        
        //ListBox.SelectedIndex = -1
        if(arrListBox){
            for(i=0; i<arrListBox.length; i++){
                arrListBox[i].selectedIndex=-1;
            }
        }
        
        //Label.Text = "" - Cancella contenuto di Div e TextArea
        if(arrLabel){
            for(i=0; i<arrLabel.length; i++){
                arrLabel[i].innerHTML='';
            }
        }
        
        //deseleziona i CheckBox figli dei vari TreeView
        if(arrNomiTreeView){
        
            var listCheck;//per adesso in listCheck finiscono tutti gli INPUT della pagina (non solo i checkbox, ma anche i text, ...)
            listCheck = document.getElementsByTagName('input');
            
            var nomeTreeView = new String();//varrà tvwRuoli, poi tvwruoli
            var nomeControllo = new String();//varrà ctl00_ContentPlaceHolder1_tvwRuolin4CheckBox, poi ctl00_contentplaceholder1_tvwruolin4checkbox
            
            for(i=0; i<arrNomiTreeView.length; i++){//per tutti i TreeView
                nomeTreeView = arrNomiTreeView[i];
                nomeTreeView = nomeTreeView.toLowerCase();
                
                for(j=0; j<listCheck.length; j++){//per tutti gli INPUT trovati nella pagina
                    nomeControllo = listCheck[j].name;
                    nomeControllo = nomeControllo.toLowerCase();
                    // se nomeControllo = ctl00_contentplaceholder1_tvwruolin4checkbox, allora è un CheckBox figlio del TreeView tvwRuoli
                    if ((nomeControllo.indexOf(nomeTreeView) != -1) && (nomeControllo.indexOf('checkbox') != -1)){
                        listCheck[j].checked=false;
                    }
                }
                
            }
        }
                   
    }
    
function strControllo(controllo,messErrore){
/*  Classe per la gestione dei Controlli in controllaDati:
p.e. 
    var C = new strControllo(document.getElementById('ctl00_ContentPlaceHolder1_lstTabelle'),'Scegliere Tabella');
    si avrà:    C.Controllo --> intero controllo lstTabelle
                C.Errore    --> 'Scegliere Tabella' --> (messaggio che comparirà se la lstTabelle non ha un elemento selezionato)
*/
    this.Controllo = controllo;
    this.Errore = messErrore;
}

function controllaDati(arrList,arrTxt,arrVari){
/*Verifica se:
    i ListBox hanno .selectedIndex <> -1
    i TextBox, HTMLFileUpload hanno .Value <> ""
    le Label, i Div, .. hanno .innerHTML <> ""
    
    in caso positivo restituisce        TRUE
    altrimenti                          FALSE*/

    //ListBox.selectedIndex <> -1
    if(arrList){
        for(i=0;i<arrList.length;i++){
            if (arrList[i].Controllo.selectedIndex == -1){
                alert(arrList[i].Errore);
                return false;
            }
        }
    }

    //TextBox.Text, HTMLFileUpload.Value,  ...   <> ""
    if(arrTxt){
        for(i=0;i<arrTxt.length;i++){
            if (arrTxt[i].Controllo.value == ''){
                alert(arrTxt[i].Errore);
                return false;
            }
        }
    }
    
    //Label.Text, Div.InnerHTML,  ...   <> ""
    if(arrVari){
        for(i=0;i<arrVari.length;i++){
            if (arrVari[i].Controllo.innerHTML == ''){
                alert(arrVari[i].Errore);
                return false;
            }
        }
    }    
   
    return true;
}    
 
function caricaSrcImg(button,sNomeImgDaCaricare){
/*Imposta l'immagine di sfondo di un button: p.e richiamiamo da un button:
    onmouseover --> caricaImg(this,'Pulsante-Salva-Hover-01.gif')
    imposterà come nuova immagine di sfondo: http://localhost:2423/25/App_Themes/Theme1/immagini/Pulsante-Salva-Hover-01.gif
    */
    
    var sPathImmagini = new String();
    sPathImmagini = button.src;
    var iIndex = new Number();
    iIndex = sPathImmagini.lastIndexOf('/');
    var sNuovoPathImg = new String();
    sNuovoPathImg = sPathImmagini.substr(0,iIndex + 1) + sNomeImgDaCaricare;
    button.src = sNuovoPathImg;
    
}

function impostaDataAttuale(txtData){
//Scrive in txtData, la data attuale
    var D = new Date();
    var nMese = new Number();
    nMese = D.getMonth() + 1;
    var sData = new String();
    sData = D.getDate() + '/' + nMese.toString() + '/' + D.getFullYear() + ' ' + D.toLocaleTimeString();
    txtData.value = sData;
    return false;
}

function impostaDataAttualeRidotta(txtData){
//Scrive in txtData, la data attuale in forma ridotta (10/12/2008)
    var D = new Date();
    var nMese = new Number();
    nMese = D.getMonth() + 1;
    var sData = new String();
    sData = D.getDate() + '/' + nMese.toString() + '/' + D.getFullYear();
    txtData.value = sData;
    return false;
}

function trimSTR(sStringa){
//TRIM di sStringa

    //se inizialmante l'INPUT non conteneva testo, allora sStringa-->undefined
    if(!sStringa){
        return '';
    }
    
    var S = new String();
    S = sStringa;
    
    //Stringa vuota
    if (S.length == 0){
        return '';
    }
    
    //Cerca gli indici degli spazi
    var iIndice = new Number();
    iIndice = S.indexOf(' ');
    
    //Nessuno spazio nell'intera sStringa
    if (iIndice == -1){
        return S;
    }
    
    //Elimina gli spazi iniziali
    while(iIndice == 0){
        S = S.substr(1);
        iIndice = S.indexOf(' ');
    }
    
    //Elimina gli spazi finali
    iIndice = S.lastIndexOf(' ');
    while(iIndice == S.length - 1){
        S = S.substring(0,iIndice);
        iIndice = S.lastIndexOf(' ');
    }

    return S;
}
