var arrImg=new Array()

arrImg[0]=new Image()
arrImg[0].src="/images/checkoff.gif"
arrImg[1]=new Image()
arrImg[1].src="/images/delete.gif"


        function lmAggiungi(T)
        {
        var i,j,k,s
        var n  // numero di righe
        var aC
        var tr
        var td
        var el1=null

        // n=T.firstChild.childNodes.length
        n=0
        for (i=0;i<T.firstChild.childNodes.length;i++)
            if (T.firstChild.childNodes[i].nodeName=="TR")
				{
                n=n+1;
				// modifica per firefox
				if (n==3)
					tr=T.firstChild.childNodes[i].cloneNode(true);
				}
        n=n-3   // intestazione (2) Pulsanti (1)
        //tr=T.firstChild.lastChild.previousSibling.cloneNode(true)

        // sul nodo clonato effettuiamo gli azzeramenti necessari
        for (i=0;i<tr.childNodes.length;i++)
            {
            if (tr.childNodes[i].nodeName=="TD")
                {
                td = tr.childNodes[i]
                for (j=0;j<td.childNodes.length;j++)
                if (td.childNodes[j].nodeName!='#text'
                    && td.childNodes[j].nodeName!='BR'  /* non č dato di sapere cosa sia questo elemento nel mezzo! */
                    && td.childNodes[j].nodeName!='SPAN')
                    {
                    if (td.childNodes[j].nodeName=='A')
                        {
                        if (td.childNodes[j].target=="lookup")
                            {
                            k=td.childNodes[j].href.indexOf("FIELDNAME=")
                            aC = td.childNodes[j].href.substring(k+1).split("_")
                            s = td.childNodes[j].href.substring(0,k)+"FIELDNAME=FIELD_" + (n+1)
                            for (k=2;k<aC.length;k++)
                            s = s + "_" + aC[k]
                            s=s.substring(11)  // esiste about:blank!
                            td.childNodes[j].href=s
                            }
                        }
                    if (td.childNodes[j].name.substr(0,6)=="FIELD_")
                        {
                        aC = td.childNodes[j].name.split("_")
                        s = "FIELD_" + (n+1)
                        for (k=2;k<aC.length;k++)
                            s = s + "_" + aC[k]
                        td.childNodes[j].name=s
                        td.childNodes[j].onblur=new Function("xsm(event,"+(n+1)+")")
                        if (td.childNodes[j].type=="checkbox")
                            {
                            td.childNodes[j].checked=false
                            }
                        else 
                            td.childNodes[j].value=""
                        if (el1==null)
                            el1=td.childNodes[j]
                        }
                    if (td.childNodes[j].name=="X3")
                        {
                        td.childNodes[j].value = "X"
                        }
                    if (td.childNodes[j].name.substr(0,3)=="X1_")
                        {
                        td.childNodes[j].name="X1_" + (n+1)
                        td.childNodes[j].value=""
                        }
                    if (td.childNodes[j].name.substr(0,3)=="X2_")
                        {
                        td.childNodes[j].name="X2_" + (n+1)
                        td.childNodes[j].value=""
                        }
                    if (td.childNodes[j].name.substr(0,4)=="IMG_")
                        {
                        td.childNodes[j].name="IMG_" + (n+1)
                        td.childNodes[j].src = arrImg[0].src
                        //td.childNodes[j].src="/images/checkoff.gif"
                        }
                    if (td.childNodes[j].name.substr(0,9)=="REQUIRED_")
                        {
                        aC = td.childNodes[j].name.split("_")
                        s = "REQUIRED_" + (n+1)
                        for (k=2;k<aC.length;k++)
                            s = s + "_" + aC[k]
                        td.childNodes[j].name=s
                        }
                    if (td.childNodes[j].name.substr(0,3)=="X6_")
                        {
                        aC = td.childNodes[j].name.split("_")
                        s = "X6_" + (n+1)
                        for (k=2;k<aC.length;k++)
                            s = s + "_" + aC[k]
                        td.childNodes[j].name=s
                        }
                    if (td.childNodes[j].name.substr(0,5)=="DATE_")
                        {
                        aC = td.childNodes[j].name.split("_")
                        s = "DATE_" + (n+1)
                        for (k=2;k<aC.length;k++)
                            s = s + "_" + aC[k]
                        td.childNodes[j].name=s
                        }
                    }
                else
                    {
                    if (td.childNodes[j].nodeName=='SPAN')
                        {
                        aC = td.childNodes[j].id.split("_")
                        s = "LD_FIELD_" + (n+1)
                        for (k=3;k<aC.length;k++)
                            s = s + "_" + aC[k]
                        td.childNodes[j].id=s
                        td.childNodes[j].innerHTML=""
                        }
                    }
                }
            }

        T.firstChild.insertBefore(tr,T.firstChild.lastChild)
        try
            {
            el1.focus()
            }
        catch(exception)
            {
            }
        }

        function xsv(event)
        {
        // salva il vecchio valore del controllo quando prende il focus
        oldValue = event.srcElement.value
        }

        function xsm(event, nRiga)
        {
        var o
        var e
        
        if (event.srcElement)
            e=event.srcElement
        else
            e=event
            
        // setta il flag modificato sulla riga di LISTMODIFY alla perdita focus sul singolo controllo
        if (nRiga>0)
            {
            if (e.value != oldValue || e.type=="checkbox")
                {
                o=document.LISTMODIFY("X2_"+nRiga)   // molto pių veloce!
                // l'undefined serverse sono state aggiunte righe nel qual caso l'istruzione precedente 
                // non funziona per cui si deve passare al DOM
                if (o==null)
                    {
                    o=getElementByNameDOM(document.getElementsByTagName("INPUT"),"X2_" + nRiga)
                    }
                else
                    {
                    if(!o.name)  // equivalente a if (o.name==undefined) ma solo su IE5.5 esiste undefined
                        {
                        o=getElementByNameDOM(document.getElementsByTagName("INPUT"),"X2_" + nRiga)
                        }
                    }
                o.value="ON" 
                calcolaDipendenze(e.name,nRiga)
                // ???? da gestire anche per altri tipi di campo
                }
            }
        }

        function swapDelete(event)
        {
        var aC
        var o
        var i
        var el
        
        if (wf.NS)
        	el=event.currentTarget;
        else
        	el=event.srcElement;

        aC=el.name.split("_")
        if (el.src.indexOf("delete.gif",0)>0)
            {
            if (wf.all)
	            document.all("X1_" + aC[1]).value="";
	        else
	        	document.getElementById("X1_" + aC[1]).value="";
            el.src = arrImg[0].src
            }
        else
            {
            if (wf.all)
	            document.all("X1_" + aC[1]).value="ON";
	        else
	        	document.getElementById("X1_" + aC[1]).value="ON";
            el.src = arrImg[1].src;
            //event.srcElement.src="/images/delete.gif"
            }
        }

        function btnGoto(shref,starget,sConfirm)
        {


        if (shref=="")
            {
            alert("Indirizzo non disponibile (vuoto!)")
            return false
        }
        
        if (sConfirm!="")
            if (!confirm(sConfirm))
                return false
        /* cambiato da test s epresent http a test se presente www 
        if (shref.toUpperCase().substring(0,4)!="HTTP")
            shref="http://" + shref
         */
        if (shref.toUpperCase().substring(0,4)=="WWW.")
            shref="http://" + shref

        // false nella window.open crea sempre una nuova finestra
        if (starget!="")
            var newWindow = window.open (shref
                ,starget,  "menubar=1,status=0,directories=0,toolbar=1,scrollbars=1,resizable=1,height=500",false);
        else
            location.href=shref
        }

        function selezionaRecord(id,nome,fieldname)
        {
        var arrS,s,i
        
        if (opener.MANGENERIC2)
            {
            opener.MANGENERIC2(fieldname).value = id
            }
        else
            {
            if (opener.LISTMODIFY)
                {
                arrS=fieldname.split("_")    
                for (i=0;i<opener.LISTMODIFY.length;i++)
                    {   
                    if (opener.LISTMODIFY[i].name==fieldname)
                        opener.LISTMODIFY[i].value = id
                    if (opener.LISTMODIFY[i].name=="X2_"+arrS[1])
                        opener.LISTMODIFY[i].value = "ON"
                    }
                //opener.LISTMODIFY(fieldname).value = id
                //opener.LISTMODIFY("X2_"+arrS[1]).value = "ON"
                }
            else
                {            
                opener.SELEZIONE(fieldname).value = id
                }
            }
        opener.document.all("LD_"+fieldname).innerText = nome
        self.close()
        }

        var imgMenu=new Array()

        imgMenu[0]=new Image()
        imgMenu[0].src="/IMAGES/MENUCLOSE.GIF"
        imgMenu[1]=new Image()
        imgMenu[1].src="/IMAGES/MENUOPEN.GIF"
        imgMenu[2]=new Image()
        imgMenu[2].src="/IMAGES/MENULEAF.GIF"

        function cambiaMenu(sMenu,level)
        {
        var s
        var i
        var l = sMenu.length

        if (document.all("IMAGE_"+sMenu).src.toUpperCase().indexOf("MENUCLOSE.GIF")>0)
            {
            document.all("IMAGE_"+sMenu).src=imgMenu[1].src
            s = "inline"
            }
        else
            {
            document.all("IMAGE_"+sMenu).src=imgMenu[0].src
            s = "none"
            }

        for (i=0;i<document.all.length;i++)
            {
            if (document.all(i).tagName=="TR")
                {
                if (
                    document.all(i).id.substr(0,l)==sMenu &&
                    (
                        (document.all(i).id.length == l+1 && s!="none") ||
                        (document.all(i).id.length > l && s=="none")
                    )
                   )
                    document.all(i).style.display = s
                }
            else
                if (document.all(i).tagName=="IMG")
                    {
                    if (document.all(i).name.length>l+6)
                        {                                        
                        if (document.all(i).src.toUpperCase().indexOf("MENUOPEN.GIF")>0)
                            document.all(i).src=imgMenu[0].src
                        }
                    }

            }
        }

function runSelect(sName,evt)
{
var i
var o
var s
var arrS

if (wf.all)
    {
    o=event.srcElement.options
    s=o(event.srcElement.selectedIndex).value
    }
else
    {
    o=evt.target.options
    s=o[evt.target.selectedIndex].value
    }
    
arrS=s.split("|")    
if (arrS[2]=="")
    return
    
if (arrS[0]!="")
    if (!confirm(arrS[0]))
        return
        
var newWindow = window.open (arrS[2]
    ,arrS[1],  "menubar=1,status=0,directories=0,toolbar=1,scrollbars=1,resizable=1,height=500",true);

}

function textExpand(sName,sTitle)
{
var newWindow = window.open ("Editazione.htm"
    ,"EDITA",  "menubar=0,status=0,directories=0,toolbar=0,scrollbars=0,resizable=0,height=500,width=600",false);
while (newWindow.document.readyState!="complete")
    {
    }
newWindow.document.all.titolo.innerText=sTitle
newWindow.document.EDITA("campoOrig").value=sName    
newWindow.document.EDITA.TXT.value=document.all(sName).value
newWindow.document.EDITA.TXT.focus()    

}
 
/*function myFixed(n,d)
{
return Math.floor(Math.round(n*Math.pow(10,d)))/Math.pow(10,d)
}

*/

function myFixed(n,d)
{
if (n==null) return 0
var x="" + n
if (x=="") return 0
var i=x.indexOf(".")
if (i<0) return n
var j=x.indexOf("e-")
if (j>0)
    {
    x=x.replace('.','')
    j=j-1
    x=('0.'+'000000000000000'.substring(0,parseInt(x.substring(j+2,x.length))-1)+x).substring(0,12)
    i=x.indexOf('.')
    }
if (x.length - i - 1<=d) return n
return x.substring(0,i+d+1)
}

function myRound(n,d)
{
return Math.round(n*Math.pow(10,d))/Math.pow(10,d)
}

function calcolaDipendenze(sNomeCampo,nRiga)
{
var i
var s
var oDip

i=sNomeCampo.indexOf("_")
i=sNomeCampo.indexOf("_",i+1)
s=sNomeCampo.substring(i+1,100).toUpperCase()
for (i=0;i<aDip.length;i++)
    {    
    oDip=aDip[i]
    if (oDip.orig.toUpperCase()==s)
        if (oDip.formula)
            {
            try
                {
                if (nRiga==0)
                    document.all["FIELD_"+oDip.dest].value=eval(parseMG(oDip.formula,nRiga))
                else
                    document.all["FIELD_"+nRiga+"_"+oDip.dest].value=eval(parseMG(oDip.formula,nRiga))
                }
            catch(e)
                {
                if (nRiga==0)
                    document.all["FIELD_"+oDip.dest].value="*ERR*"
                else
                    document.all["FIELD_"+nRiga+"_"+oDip.dest].value="*ERR*"
                }
            } 
    }
}

function parseMG(s,nRiga)
{
var i,j,v,xn

j=0
i=-1
do
    {
    i=s.indexOf("[",i+1)
    j=s.indexOf("]",i+1)
    if (i<0 || j<0) 
        break
    if (s.substring(i+1,i+2)=='!')
        {
        if (nRiga==0)
            v = "document.all['FIELD_" + s.substring(i+2,j) +"'].value "
        else
            v = "document.all['FIELD_" + nRiga + "_" + s.substring(i+2,j) + "'].value "
        }
    else
        {        
        if (nRiga==0)
            {        
            v = document.all["FIELD_" + s.substring(i+1,j)].value 
            xn = document.all["XN_" + s.substring(i+1,j)].value
            }       
        else
            {  
            v = document.all["FIELD_" + nRiga + "_" + s.substring(i+1,j)].value
            xn = document.all["XN_"+ nRiga + "_" + s.substring(i+1,j)]
            }       
        }       
    if (typeof(xn)!="undefined")
        v=v.replace(",",".")
    s = s.substring(0,i) + v + s.substring(j+1,1000)
    i=(s.substring(0,i) + v).length
    }
while (true) 
return s
}


