var inAttesa,
inAttesaCF,
listaComuni = new Array(),
codiceComune = new Array(),
listaCampi;


function chiudi( oggetto )
{
    document.getElementById(oggetto).style.display = 'none';
    return false;
}

function selezionaComune( oggetto, codComune )
{
    if ( oggetto.options.length > 1 )
    {
       for ( k = 0; k < oggetto.options.length; k++ )
       {
           if ( oggetto.options[ k ].value == codComune )
           {
               oggetto.options[ k ].selected = true;
               break;
           }
       }
    }
}

function cercaComuni( province, lista, url, codice )
{
    //listaComuni = eval( "document.getElementById('" + lista + "')" );
    listaComuni[codice] = document.getElementById(lista);
    
    var codProvincia = province.value;
			
    listaComuni[codice].options.length = 0;// Azzera la lista comuni
    listaComuni[codice].options[ 0 ] = new Option( "","");
    
    var ajaxProvince = new Ajax.Request(
			url, 
			{
                            method: 'get', 
                            onComplete: function(request){analizza( request );},
                            parameters: 'prv=' + codProvincia + '&cod=' + codice,
                            evalScripts: true,
                            asynchronous: true
			});
}

function analizza( originalRequest )
{
    var esito = originalRequest.responseText.split("|");
    
    if ( esito.length > 1 && esito[0].indexOf("<") == -1 )
    {
       codice = esito[0];
       
       for ( k = 1; k < esito.length - 1; k++ )
       {
           tempOption = esito[k].split(";");

           listaComuni[codice].options[ k ] = new Option( tempOption[1],tempOption[0]);
       }

       selezionaComune( listaComuni[codice], codiceComune[codice] );
    }
}

function verificaCodiceFiscale( oggettoForm, url )
{
    var parametri = 'sex=' + oggettoForm.sesso.options[ oggettoForm.sesso.selectedIndex ].value + '&cod_com=' + oggettoForm.codluogonascita.options[ oggettoForm.codluogonascita.selectedIndex ].value;
    parametri += '&giorno=' + oggettoForm.datanascita_DD.options[ oggettoForm.datanascita_DD.selectedIndex ].value + '&mese=' +oggettoForm.datanascita_MM.options[ oggettoForm.datanascita_MM.selectedIndex ].value  + '&anno=' + oggettoForm.datanascita_YY.options[ oggettoForm.datanascita_YY.selectedIndex ].value;
    parametri += '&nome=' + oggettoForm.nome.value + '&cognome=' + oggettoForm.cognome.value ;

    var ajaxProvince = new Ajax.Request(
			url, 
			{
                            method: 'get', 
                            onComplete: function(request){rispostaCF( request );},
                            parameters: parametri,
                            evalScripts: true,
                            asynchronous: true
			});

    return false;
}

function rispostaCF( originalRequest )
{
    var codFiscale = originalRequest.responseText;
    
    if ( codFiscale != null && codFiscale.length > 16 && codFiscale.indexOf("<") == -1 )
    {
        codFiscale = codFiscale.substring(0,16);
    }
    else
    {
        alert("Codice fiscale non verificato!");

        return;
    }


    if ( codFiscale != document.f1.codice_fiscale.value.toUpperCase() )
    {
         alert("Codice fiscale errato!");	
    }
}


function verificaDati()
{
    if ( listaCampi.Nascita.selectedIndex == 0 )
    {
        oggetto = document.getElementById('RequiredFieldValidator_Nascita');//alert("Selezionare citt&agrave di Nascita!");
        oggetto.style.display = 'inline';
        return false;
    }
    else
    {
        oggetto = document.getElementById('RequiredFieldValidator_Nascita');//alert("Selezionare citt&agrave di Nascita!");
        oggetto.style.display = 'none';
        return true;
    }
    
}


function vedi( id )
{
    document.location = "registraAnimale.aspx?id="+ id;
}

function rinnova( id )
{
    document.location = "rinnovaAbbonamento.aspx?id="+ id;
}

function nuovoAnimale()
{
    document.location = "registraAnimale.aspx";
}

function listaAnimali( riga, target, url, codice )
{
    scheda = document.getElementById(target);
    
    scheda.innerHTML = "";
    
    var ajaxAnimali = new Ajax.Request(
			url, 
			{
                            method: 'get', 
                            onComplete: function(request){analizzaRisposta( request, scheda );},
                            parameters: 'cod=' + codice,
                            evalScripts: true,
                            asynchronous: true
			});
}

function analizzaRisposta( originalRequest, pannello )
{
    var esito = originalRequest.responseText;
    
    if ( esito.length > 1 )//&& esito.indexOf("<") == -1 )
    {
        pannello.innerHTML = esito;
        pannello.style.display = "block";
    }
}


function elabora( codice, attiva)
{
    //scheda = document.getElementById(target);
    
    //scheda.innerHTML = "";
    var url = '../util/ElaboraDati.ashx';
    var parametri = 'tipo=1&cod=' + codice + '&attiva=' + attiva;
    
    var ajaxAnimali = new Ajax.Request(
			url, 
			{
                method: 'get', 
                onComplete: function(request){ elaboraRisposta( request );},
                parameters: parametri,
                evalScripts: true,
                asynchronous: true
			});
	
	return true;
}

function elaboraRisposta( origRequest )
{
    //var esito = origRequest.responseText;
    var esito = origRequest.responseXML;
    var animale = esito.documentElement.getElementsByTagName("animale")[0];
    //var codice = esito.getElementsByTagName('id')[0].childNodes[0].nodeValue
    var codice = animale.getElementsByTagName('id')[0].childNodes[0].nodeValue;
    var attiva = animale.getElementsByTagName('valido')[0].childNodes[0].nodeValue;
    var test   = animale.getElementsByTagName('test')[0].childNodes[0].nodeValue;
    
    
    if ( test == 1 )
    {
        var imgAttiva  = document.getElementById('ATTIVA_' + codice );
        var imgRinnovo = document.getElementById('RINNOVO_' + codice );
        var imgBlocca  = document.getElementById('BLOCCA_' + codice );
        
        var rigaTabella = imgAttiva.parentNode.parentNode;// img/td/tr
        
        //var attributi = imgAttiva.attributes;
        //var src   = attributi.getNamedItem("src");
        //var click = attributi.getNamedItem("onclick");
        //var stile = attributi.getNamedItem("class");
        
//element.addEventListener('click',startDragDrop,false)
//element.addEventListener('click',spyOnUser,false)
//element.removeEventListener('click',spyOnUser,false)
        
        
        if ( (attiva & 1) > 0 )  //Attivo
        {
            imgAttiva.src = "../img/on.gif";
            //imgAttiva.setAttribute("onclick","");
            imgAttiva.onclick = null;
            imgAttiva.className = "";
        }
        else if ( (attiva & 6) == 6 )//Scaduto e richiesto rinnovo
        {
            imgAttiva.src = "../img/off.gif";
            imgAttiva.onclick = null;
            imgAttiva.className = "link";
        }
        else
        {
            imgAttiva.src = "../img/off.gif";
            //imgAttiva.setAttribute("onclick","elabora('" + codice + "',1);");
            //addEvent(imgAttiva, "click", "elabora('" + codice + "',1);", true);//imgAttiva.onclick = "elabora('" + codice + "',1);";
            imgAttiva.onclick = function() { elabora( codice,1); };
            imgAttiva.className = "link";
        }
        
        if ( (attiva & 2) > 0 )//Rinnovo
        {
            imgRinnovo.src = "../img/on.gif";
            //imgRinnovo.setAttribute("onclick","");
            imgRinnovo.onclick = function() { elabora( codice,2); };
            imgRinnovo.className = "link";
        }
        else
        {
            imgRinnovo.src = "../img/off.gif";
            //imgRinnovo.setAttribute("onclick","elabora('" + codice + "',2);");
            //imgRinnovo.onclick = "onclick","elabora('" + codice + "',2);";
            imgRinnovo.onclick = null;
            imgRinnovo.className = "";
        }
        
        if ( (attiva & 8) > 0 )  //Bloccato
        {
            imgBlocca.src = "../img/on.gif";
        }
        else
        {
            imgBlocca.src = "../img/off.gif";
        }
        
        
        switch ( eval(attiva) ) 
        {
            case 1:
                 rigaTabella.className = "";
                 break;
            case 3:
                 rigaTabella.className = "inr";
                 break;
            case 4:
                 rigaTabella.className = "out";
                 break;
            case 6:
                 rigaTabella.className = "outr";
                 break;
            case 0:
                 rigaTabella.className = "off";
                 break;
            case 8:
            case 9:
            case 11:
            case 12:
            case 14:
                 rigaTabella.className = "bloc";
                 break;
        }

    }
}

function elaboraUtente( codice, attiva)
{
    //scheda = document.getElementById(target);
    
    //scheda.innerHTML = "";
    var url = '../util/ElaboraDati.ashx';
    var parametri = 'tipo=2&cod=' + codice + '&attiva=' + attiva;
    
    var ajaxAnimali = new Ajax.Request(
			url, 
			{
                method: 'get', 
                onComplete: function(request){ elaboraRispostaUtente( request );},
                parameters: parametri,
                evalScripts: true,
                asynchronous: true
			});
	
	return true;
}


function elaboraRispostaUtente( origRequest )
{
    //var esito = origRequest.responseText;
    var esito = origRequest.responseXML;
    var animale = esito.documentElement.getElementsByTagName("utente")[0];
    //var codice = esito.getElementsByTagName('id')[0].childNodes[0].nodeValue
    var codice = animale.getElementsByTagName('id')[0].childNodes[0].nodeValue;
    var attiva = animale.getElementsByTagName('valido')[0].childNodes[0].nodeValue;
    var test   = animale.getElementsByTagName('test')[0].childNodes[0].nodeValue;
    
    
    if ( test == 1 )
    {
        var imgAttiva  = document.getElementById('ATTIVAUT_' + codice );
        
        var rigaTabella = imgAttiva.parentNode.parentNode;// img/td/tr
        
        if ( (attiva & 1) > 0 )  //Attivo
        {
            imgAttiva.src = "../img/on.gif";
            imgAttiva.onclick = function() { elaboraUtente( codice,4); };
        }
        else//Bloccato
        {
            imgAttiva.src = "../img/off.gif";
            imgAttiva.onclick = function() { elaboraUtente( codice,1); };
        }
        
        //imgAttiva.className = "link";
        
        switch ( eval(attiva) ) 
        {
            case 1:
                 rigaTabella.className = "";
                 break;
            case 4:
            default:
                 rigaTabella.className = "off";
                 break;
        }

    }
}
