// Funzione utilizzata per aprire la popup delle date
function selData( callbackfunction, date_start, optparam )
{
    //alert(optparam)
    if( date_start == "" )
    {
        //alert("data vuota");
        today = new Date();
        date_start = today.getDate()+"/"+(today.getMonth()+1)+"/"+today.getFullYear();
    }
    //alert(date_start);
    
    array_date_start = date_start.split("/");
    
    modalpopup("/extras/selectdate?nameprefix="+optparam+"&parentfunction="+callbackfunction+"&day="+array_date_start[0]+"&month="+array_date_start[1]+"&year="+array_date_start[2],'300', '165', 'no', 'no');
}
// Funzione richiamata dalla popup delle date per la data passata come prefix ( senza reload )
function selectedDate( prefix, m, d, y )
{
    m = m+1;
    if( m < 10 )
        m = "0"+m;
    if( d < 10 )
        d = "0"+d;
    document.getElementById(prefix).value = d+"/"+m+"/"+y;
    //document.getElementById(prefix+"_span").innerHTML = d+"/"+m+"/"+y;
}
// funzione che trova un elemento in un array, restituisce true/false
function inArray(value,arg)
{
	var i;
	for (i=0; i < arg.length; i++) {
		// Matches identical (===), not just similar (==).
		if (arg[i] == value) {
			return true;
		}
	}
	return false;
}


////////////////////////////////// FUNZIONI USATE NELLA NEWS_PAGE ///////////////////////////////////////

// Definisco var usate nella page
stuffPrimo = new Array();
check_scelti = new Array();
staff_selected_ids = new Array();
staff_selected_ids_pub = new Array();
// array per la gestione abled/disabled del checkbox PUBBLICA     //
arr_CategPublishPermission = new Array();
// array per la gestione checked/unchecked del checkbox PUBBLICA  //
arr_publishedNews = new Array();
// indica lo stato dell'intestazione
drawedInt = false;

/*
    Funzione che disegna / cancella l'intestazione del PUBBLICA / CANCELLA
*/
function drawIntestazione()
{
    draw = false;
    for(i=0;i<check_scelti.length;i++)
    {
        if (check_scelti[i] != null)
            draw = true;
    }
    if ( draw == true && drawedInt == false )
    {

        // creo la tabella con i suoi attributi
        document['headtab'] = document.createElement("TABLE")
        document['headtab'].setAttribute("width","469")
        document['headtab'].setAttribute("border","0")
        document['headtab'].setAttribute("cellspacing","0")
        document['headtab'].setAttribute("cellpadding","0")

        // creo TBODY e lo appendo alla tabella
        // ancora una volta, questo passaggio lo richiede Explorer 
        // (infatti nell'esempio di Cecchin non c'era alcun TBODY). 
        // IE permette di inserire dati in una tabella creata
        // dinamicamente solo all'interno di un tbody.
        tboHead = document.createElement("TBODY")
        document['headtab'].appendChild(tboHead)	
        // creo la prima riga e la appendo al TBODY
        tr0 = document.createElement("TR")
        tboHead.appendChild(tr0)

        // 1^ cella
        td1 = document.createElement("TD")
        td1.setAttribute("align","left")
        td1.setAttribute("valign","top")
        td1.setAttribute("width","10")
        tr0.appendChild(td1)

        // 2^ cella
        td2 = document.createElement("TD")
        td2.setAttribute("align","left")
        td2.setAttribute("valign","top")
        td2.setAttribute("width","300")
        tr0.appendChild(td2)

        // 3^ cella
        td3 = document.createElement("TD")
        td3.setAttribute("align","left")
        td3.setAttribute("valign","top")
        td3.setAttribute("width","75")
        tr0.appendChild(td3)

        // 4^ cella
        td4 = document.createElement("TD")
        td4.setAttribute("align","left")
        td4.setAttribute("valign","top")
        td4.setAttribute("width","75")

        tr0.appendChild(td4)

        tr0.childNodes[1].innerHTML = "<span class='GlobalTestoB1'>Categorie</span>"
        tr0.childNodes[2].innerHTML = "<span class='GlobalTestoB1'>Pubblica&nbsp;&nbsp;</span>"
        tr0.childNodes[3].innerHTML = "<span class='GlobalTestoB1'>DEL</span>"			

        document.getElementById('intestazioneGancio').appendChild(document['headtab'])
        
        // indica che l'intestazione è già stata disegnata!
        drawedInt = true;
    }

    
    if ( !draw && drawedInt )
    {
        document.getElementById('intestazioneGancio').removeChild(document['headtab'])
        drawedInt = false;
    }
}


function aggiungi_check( n, cella )
{
    var controllo = "";
    if( cella == "gancio")
        controllo = check_scelti[n];
    
    if( controllo == n )
        alert("Gia selezionato")
    else
    {
        //////////////////////////////////////////
        // creo la tabella con i suoi attributi //
        //////////////////////////////////////////
        document[cella+n] = document.createElement("TABLE")
        document[cella+n].setAttribute("width","469")
        document[cella+n].setAttribute("border","0")
        document[cella+n].setAttribute("cellspacing","0")
        document[cella+n].setAttribute("cellpadding","0")
        
        // creo TBODY e lo appendo alla tabella
        // ancora una volta, questo passaggio lo richiede Explorer 
        // (infatti nell'esempio di Cecchin non c'era alcun TBODY). 
        // IE permette di inserire dati in una tabella creata
        // dinamicamente solo all'interno di un tbody.
        tbo = document.createElement("TBODY")
        document[cella+n].appendChild(tbo)	
        
        // disegno intestaz
        //drawIntestazione();
        
        // creo la riga dei risultati e la appendo al TBODY
        tr1 = document.createElement("TR")
        tbo.appendChild(tr1)
        
        // 1^ cella
        td01 = document.createElement("TD")
        td01.setAttribute("align","left")
        td01.setAttribute("valign","top")
        td01.setAttribute("width","10")
        
        // 2^ cella
        td02 = document.createElement("TD")
        td02.setAttribute("align","left")
        td02.setAttribute("valign","top")
        td02.setAttribute("width","300")
        
        // 3^ cella
        td03 = document.createElement("TD")
        td03.setAttribute("align","left")
        td03.setAttribute("valign","top")
        td03.setAttribute("width","75")
        
        // 4^ cella
        td04 = document.createElement("TD")
        td04.setAttribute("align","left")
        td04.setAttribute("valign","top")
        td04.setAttribute("width","75")
        
        tr1.appendChild(td01)
        tr1.appendChild(td02)
        tr1.appendChild(td03)
        tr1.appendChild(td04)
        
        //////////////////////////////////////////
        //        fine della creaz tabella      //
        //////////////////////////////////////////
        
        // Ora riempiamo la seconda cella della seconda riga per ogni articolo, 
        // usando il metodo innerHTML
        var nome = "";
        var valore = "";
        if( cella == "gancio")
        {
            nome   = stuffPrimo[n][0];
            valore = stuffPrimo[n][1];
        }
        //alert(valore + " - aggiungi_check(" + n + ")")
        
        
        //////////////////////////////////////////////////////////////////////////////////
        // se l'utente NON ha i permessi per pubblicare la news, il checkbox è disabled //
        //////////////////////////////////////////////////////////////////////////////////
        check_string = "";
        checked_ok = "";
        // controllo se disabilitare il check
        searchArr = inArray( valore, arr_CategPublishPermission )
        if ( searchArr )
            check_string = "";
        else
        {
            check_string = " disabled";
        }
        // Check
        if (arr_publishedNews.length > 0)
        {
            ///////////////////////////////////////////////////////////////////
            // se la news era già pubblicata prima metto il checkbox checked //
            ///////////////////////////////////////////////////////////////////
            if ( arr_publishedNews[valore] == 1 )
            {
                checked_ok = " checked";
                check_pub(n);
            }
            else
                checked_ok = "";
        }
        
        tr1.childNodes[1].innerHTML = "<span class='GlobalTesto1'>"+nome+"</span>";
        tr1.childNodes[2].innerHTML = "<input id='News_ReadPubblDel[]' value='"+valore+"' type='checkbox' " + check_string  + checked_ok + " onclick='check_pub("+n+")'>"
        tr1.childNodes[3].innerHTML = "<input id='News_ReadGroupDel[]' value='"+valore+"' type='checkbox' onclick='elimina_tab("+n+",\""+cella+"\")'>"
        
        // Infine non resta che appendere
        // la tabella cosi creata alla cella con ID="gancio"
        document.getElementById(cella).appendChild(document[cella+n])
    }
}

function elimina_tab( ind, cella )
{
    document.getElementById(cella).removeChild(document[cella+ind]);
    if( cella == "gancio" )
    {
        check_scelti[ind] = null;
        staff_selected_ids[ind] = null;
        staff_selected_ids_pub[ind] = null;
        
        // disegno intestaz
        drawIntestazione();
    }
}

function elimina_option( indice, val, cella )
{
    if( cella == "gancio" )
    {
        check_scelti[indice] = indice;
        staff_selected_ids[indice] = val;
        
        if (staff_selected_ids_pub[indice] != 0 && staff_selected_ids_pub[indice] != null )
            staff_selected_ids_pub[indice] = 1;
        else
            staff_selected_ids_pub[indice] = 0;
        
        // disegno intestaz
        drawIntestazione();
    }
}

// gestione checkbox PUBBLICA
function check_pub(ind)
{
    if ( staff_selected_ids_pub[ind] != 1 )
        staff_selected_ids_pub[ind] = 1;
    else
        staff_selected_ids_pub[ind] = 0;
}
// Funzione che invia i dati della news per inserirli nel DB
function save_news( theForm )
{
    document.forms[theForm].elements["newstext"].value = editor.getHTML();
    //alert("staff_selected_ids -> " + staff_selected_ids);
    //alert("staff_selected_ids_pub -> " + staff_selected_ids_pub);
    document.forms[theForm].elements["News_HiddenCat"].value = staff_selected_ids;
    document.forms[theForm].elements["News_HiddenPub"].value = staff_selected_ids_pub;
    document.forms[theForm].submit();
}
// funzione che genera il preview dell'immagine
function previewTH()
{
    if ( document.getElementById("newsImage").value != "" )
    {
        Img1= new Image(150,100);                            // definisco la dimensione dell' immagine iniziale
        Img1.src = "<? echo $newsImage ?>";                  // path contenente l' immagine iniziale
        Img2 = document.getElementById("newsImage").value;   // associo all' Img2 il contenuto che prelevo mediante l' input type file nella form
        //alert(Img2);
        document.swapIMG.src = "file://"+Img2;               // swappo l' immagine iniziale con quella caricata
        return true;
    }
    else
        return false;
}
