function settatasti(ins, mod, can, cer, sta, esc) {
    
    if (ins == "D") {
        document.getElementById("Toolbar1_cmdins").src = "images/cmdins_d.jpg";
        document.getElementById("Toolbar1_cmdins").disabled = true;
    } else {
        document.getElementById("Toolbar1_cmdins").src = "images/cmdins_n.jpg";
        document.getElementById("Toolbar1_cmdins").disabled = false;    
    }

    if (mod == "D") {    
        document.getElementById("Toolbar1_cmdmod").src = "images/cmdann_d.jpg";
        document.getElementById("Toolbar1_cmdmod").disabled = true;
    } else {
        document.getElementById("Toolbar1_cmdmod").src = "images/cmdann_n.jpg";
        document.getElementById("Toolbar1_cmdmod").disabled = false;
    
    }
    if (can == "D") {          
        document.getElementById("Toolbar1_cmdcan").disabled = true;
        document.getElementById("Toolbar1_cmdcan").src = "images/cmdcan_d.jpg";
    } else { 
        document.getElementById("Toolbar1_cmdcan").disabled = false;
        document.getElementById("Toolbar1_cmdcan").src = "images/cmdcan_n.jpg";
    }

    if (cer == "D") {
        document.getElementById("Toolbar1_cmdcer").disabled = true;
        document.getElementById("Toolbar1_cmdcer").src = "images/cmdcer_d.jpg";
    } else {
        document.getElementById("Toolbar1_cmdcer").disabled = false;
        document.getElementById("Toolbar1_cmdcer").src = "images/cmdcer_n.jpg";
    }

    if (sta == "D") {
        document.getElementById("Toolbar1_cmdsta").disabled = true;
        document.getElementById("Toolbar1_cmdsta").src = "images/cmdsta_d.jpg";
    } else {
        document.getElementById("Toolbar1_cmdsta").disabled = false;
        document.getElementById("Toolbar1_cmdsta").src = "images/cmdsta_n.jpg";
    }

    if (esc == "D") {
        document.getElementById("Toolbar1_cmdesc").disabled = true;
        document.getElementById("Toolbar1_cmdesc").src = "images/cmdesc_d.jpg";    
    } else {
        document.getElementById("Toolbar1_cmdesc").disabled = false;
        document.getElementById("Toolbar1_cmdesc").src = "images/cmdesc_n.jpg";
        
    }
}

function settatasti_s(ins, cer, sta, esc) {

    if (ins == "D") {
        document.getElementById("Toolbar1_cmdins").src = "images/cmdins_d.jpg";
        document.getElementById("Toolbar1_cmdins").disabled = true;
    } else {
        document.getElementById("Toolbar1_cmdins").src = "images/cmdins_n.jpg";
        document.getElementById("Toolbar1_cmdins").disabled = false;
    }

    if (cer == "D") {
        document.getElementById("Toolbar1_cmdcer").disabled = true;
        document.getElementById("Toolbar1_cmdcer").src = "images/cmdcer_d.jpg";
    } else {
        document.getElementById("Toolbar1_cmdcer").disabled = false;
        document.getElementById("Toolbar1_cmdcer").src = "images/cmdcer_n.jpg";
    }

    if (sta == "D") {
        document.getElementById("Toolbar1_cmdsta").disabled = true;
        document.getElementById("Toolbar1_cmdsta").src = "images/cmdsta_d.jpg";
    } else {
        document.getElementById("Toolbar1_cmdsta").disabled = false;
        document.getElementById("Toolbar1_cmdsta").src = "images/cmdsta_n.jpg";
    }

    if (esc == "D") {
        document.getElementById("Toolbar1_cmdesc").disabled = true;
        document.getElementById("Toolbar1_cmdesc").src = "images/cmdesc_d.jpg";
    } else {
        document.getElementById("Toolbar1_cmdesc").disabled = false;
        document.getElementById("Toolbar1_cmdesc").src = "images/cmdesc_n.jpg";

    }
}



function salva() {

    document.getElementById("txtstato").value = "Lettura";
    document.getElementById("txtcampo").value = "Salva";
    document.forms[0].submit();

}

function inserisci() {
    var a = document.getElementById("txtstato").value;
    if (a == "Aggiungi" || a == "Modifica") {
        salva();
    } else {
        document.getElementById("txtstato").value = "Aggiungi";
        document.getElementById("txtcampo").value = "Aggiungi";
        document.forms[0].submit();
    }
}
function modifica() {
    var a = document.getElementById("txtstato").value;
    if (a == "Modifica" || a == "Aggiungi") {
        annulla();
    }else{
     document.getElementById("txtstato").value = "Modifica";
     document.getElementById("txtcampo").value = "Modifica";
    document.forms[0].submit();
  }
    
}

function annulla() {
    document.getElementById("txtstato").value = "Lettura";
    document.getElementById("txtcampo").value = "Annulla";
    document.forms[0].submit();
}

function cancella() {
    var conferma = confirm('Sei Sicuro di voler Eliminare questa registrazione?');
    if (conferma) {
        document.getElementById("txtstato").value = "Lettura";
        document.getElementById("txtcampo").value = "Elimina";
        document.forms[0].submit();
    }
}

function delegati() {
    var id_cli = document.getElementById("txtidcf").value;
    // alert(id_cli);
    if (id_cli == "") {
        alert("Selezionare prima il cliente");
    } else {
        document.location.href = "delegati.aspx?filtro=" + id_cli;
    }

}


function ubicazioni() {
    var id_cli = document.getElementById("txtidcf").value;
    // alert(id_cli);
    if (id_cli == "") {
        alert("Selezionare prima il cliente");
    } else {
        document.location.href = "ubicazioni.aspx?filtro=" + id_cli;
    }
}

function ctrlrd1(e) {
    var a = document.getElementById("txtstato").value;
    if (a != "Modifica") {
        alert("Cliccare prima su Modifica");       
    }
}






function cerca() {

    
        document.getElementById("txtstato").value = document.getElementById("Toolbar1_txtcerca").value;
        document.getElementById("txtcampo").value = "Cerca";
        document.forms[0].submit();

   
}




function stampa() {
    document.getElementById("txtstato").value = "Lettura";
    document.getElementById("txtcampo").value = "Stampa";
    document.forms[0].submit();
}

function esci() {
    document.getElementById("txtstato").value = "Lettura";
    document.getElementById("txtcampo").value = "Esci";
    document.forms[0].submit();
}




function conferma_vendita()
{

 var p_piva = document.getElementById("txtpiva").value;
  
	 if (p_piva != "") 
	 {
	     salva();
	 } else {
	    alert("Il cliente selezionato non ha la Partita Iva in anagrafica");
	 }
}

function elimina_elem(e, lung) {

    var conferma = confirm('Sei Sicuro di voler Eliminare questa registrazione?');
    if (conferma) {

        var oggetto = e.id.substr(0, lung);
        var nriga = e.id.substr(lung);
        if (nriga.substr(0, 1) == "0") {
            nriga = e.id.substr(lung + 1);
        }
        var riga = parseInt(nriga);
        var p_campo = oggetto + padNum(riga, 2) + "_" + "IDKEY";
        var idcod = document.getElementById(p_campo).innerHTML;
        if (idcod == "") {
            idcod = document.getElementById(p_campo).value;
        }
        oggetto = "";
        var pp_stato = oggetto + "txtstato";
        document.getElementById(pp_stato).value = "Elimina";
        pp_stato = oggetto + "txtcampo";
        document.getElementById(pp_stato).value = idcod;
        document.forms[0].submit();

    } else {
        return false;
    }


}

function cancella_carico(e, lung) {
    var conferma = confirm('Sei Sicuro di voler Eliminare questo carico?');
    if (conferma) {

        var oggetto = e.id.substr(0, lung);
        var nriga = e.id.substr(lung);
        if (nriga.substr(0, 1) == "0") {
            nriga = e.id.substr(lung + 1);
        }
        var riga = parseInt(nriga);
        var p_campo = oggetto + padNum(riga, 2) + "_" + "IDKEY";
        var idcod = document.getElementById(p_campo).innerHTML;

        oggetto = "";
        var pp_stato = oggetto + "txtstato";
        document.getElementById(pp_stato).value = "Elimina";
        pp_stato = oggetto + "txtcampo";
        document.getElementById(pp_stato).value = idcod;
        document.forms[0].submit();       
       
    } else {
        return false;
    }

    
}

function cancella_vendita(e, lung) {
    var conferma = confirm('Sei Sicuro di voler Eliminare questa vendita?');
    if (conferma) {

        var oggetto = e.id.substr(0, lung);
        var nriga = e.id.substr(lung);
        if (nriga.substr(0, 1) == "0") {
            nriga = e.id.substr(lung + 1);
        }
        var riga = parseInt(nriga);
        var p_campo = oggetto + padNum(riga, 2) + "_" + "IDKEY";
        var idcod = document.getElementById(p_campo).innerHTML;

        var p_campo = oggetto + padNum(riga, 2) + "_" + "txtdata";
        document.getElementById("txtdataope").value = document.getElementById(p_campo).innerHTML;
        
        oggetto = "";
        var pp_stato = oggetto + "txtstato";
        document.getElementById(pp_stato).value = "Elimina";
        pp_stato = oggetto + "txtcampo";
        document.getElementById(pp_stato).value = idcod;
        document.forms[0].submit();

    } else {
        return false;
    }


}


function cancella_riga_prenum(e, lung) {   
  var conferma = confirm('Sei Sicuro di voler Eliminare questa riga vendita?');
    if (conferma) {
        var oggetto = e.id.substr(0, lung);
        var nriga = e.id.substr(lung);
        if (nriga.substr(0, 1) == "0") {
            nriga = e.id.substr(lung+1);
        }
        var riga = parseInt(nriga);
        var p_campo = oggetto + padNum(riga, 2) + "_" + "chiaveidl";
        var idcod = document.getElementById(p_campo).value;

        oggetto = "";
        var pp_stato = oggetto + "txtstato";
        document.getElementById(pp_stato).value = "Elimina";
        pp_stato = oggetto + "txtcampo";
        document.getElementById(pp_stato).value = idcod;
        document.forms[0].submit();
    } else {
        return false;
    }    

}

function controlla_rigavendita() {
    var m_fogli = document.getElementById("txtfoglixblocco").value
   
    var risp = controllanum();
    if (risp == false) {
        return false;
    } else {
        document.getElementById("txtfoglixblocco").value = m_fogli;
        var oggetto = "";
        pp_stato = oggetto + "txtstato";
        document.getElementById(pp_stato).value = "addriga";
        document.forms[0].submit();
    }
}


function controllaserie() {
    var a = document.getElementById("txtserie").value;
    if (a == null || a == "") {
        alert("inserire Serie prenumerato");
        return false;
    } else {
        var oggetto = "";
        pp_stato = oggetto + "txtstato";
        document.getElementById(pp_stato).value = "controlla_serie";
        var idcod = document.getElementById("txtserie").value;
        pp_stato = oggetto + "txtcampo";
        document.getElementById(pp_stato).value = idcod;
        document.forms[0].submit();
    }
}

    
function controlla_carico() {
    var ok = true;
    var a = document.getElementById("txtcod").value;
    if (a == null || a=="") {
        alert("inserire codice prenumerato");       
        return false;
    }
    a = document.getElementById("txtdes").value;
    if (a == null || a == "") {
        alert("inserire descrizione");
        return false;
    }

    a = document.getElementById("txtserie").value;
    if (a == null || a == "") {
        alert("inserire allegato");
        return false;
    }

    a = document.getElementById("txtprod").value;
    if (a == null || a == "") {
        alert("inserire anno di produzione");
        return false;
    }  else {
          
          var risp = IsNumericvariabile(a);    
          if (risp == false)  { 
          return false;
          }
    }
        
    a = document.getElementById("txtcaricata").value;
    if (a == null || a == "") {
        alert("inserire numero blocchi");
        return false;
    }else {
          var risp = IsNumericvariabile(a);
          if (risp == false)  { 
	           return false;
          }
    }
    
    a = document.getElementById("txtfoglixblocco").value;
    if (a == null || a == "") {
        alert("inserire fogli per blocco");
        return false;
    }else {
          var risp = IsNumericvariabile(a);
          if (risp == false)  { 
	           return false;
          }
    }
    
    a = document.getElementById("txtprefisso").value;
    if (a == null || a == "") {
        alert("inserire prefisso numerazione");
        return false;
    }
    
    a = document.getElementById("txtn_iniz").value;
    if (a == null || a == "") {
        alert("inserire numerazione iniziale");
        return false;
    }else {
          var risp = controllanum();
          if (risp == false)  { 
	            return false;
          }
    }
    a = document.getElementById("txtn_fine").value;
    if (a == null || a == "") {
        alert("inserire numerazione finale");
        return false;
    }else {
          var risp = IsNumericvariabile(a);
          if (risp == false)  { 
	            return false;
          }
    }
    
    if (ok == true) {
        var oggetto = "";
        pp_stato = oggetto + "txtstato";
        document.getElementById(pp_stato).value = "addriga";
        document.forms[0].submit(); 
    }
}

function controllanum() {
    var p_iniz = parseInt(document.getElementById("txtn_iniz").value);
    var p_caric = parseInt(document.getElementById("txtcaricata").value);

    if (isNaN(p_caric)) {
        p_caric = 0;
    }

    if (p_caric == 0) {
        p_caric = 0;
        var p_iniz = parseInt(document.getElementById("txtn_iniz").value);
        if (isNaN(p_iniz)) {
            p_iniz = 0;
            alert("inserire numero iniziale");
            return false;
        } else {
            var risp = IsNumericvariabile(p_iniz);            
            if (risp == false) {
                alert("inserire valore numerico");
                return false;
            } else {
                var p_nfine = parseInt(document.getElementById("txtn_fine").value);
                if (isNaN(p_nfine)) {
                    p_nfine = 0;
                    alert("inserire numero finale");
                    return false;
                } else {
                    var risp = IsNumericvariabile(p_nfine);
                    if (risp == false) {
                        alert("inserire valore numerico");
                        return false;
                    } else {
                        if (p_nfine > p_iniz && p_nfine > 0 && p_iniz > 0) {
                            var p_fogli = parseInt(document.getElementById("txtfoglixblocco").value);
                            var qta = (((p_nfine - p_iniz) + 1));
                            if ((qta % p_fogli) !== 0) {
                                alert("Numerazione Errata");
                                return false;
                            } else {
                                document.getElementById("txtcaricata").value = (((p_nfine - p_iniz) + 1) / p_fogli);
                            }
                        }

                    }
                }

            }
        }
    } else {

        if (isNaN(p_iniz)) {
            p_iniz = 0;
            alert("inserire numero iniziale");
            return false;
        } else {

            var risp = IsNumericvariabile(p_iniz);
            if (risp == false) {
                alert("inserire valore numerico");
                return false;
            } else {

                var p_fogli = parseInt(document.getElementById("txtfoglixblocco").value);
                var p_caric = parseInt(document.getElementById("txtcaricata").value);

                if (((p_iniz + (p_fogli * p_caric) - 1) % p_fogli) !== 0) {
                    alert("Numerazione Errata");
                    return false;
                } else {

                    document.getElementById("txtn_fine").value = p_iniz + (p_fogli * p_caric) - 1;

                }
            }

        }

    }
}


function IsNumericvariabile(sText) { 
   
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
            alert("Inserire un numero");
           
           
            return false;
        }
    }
    return IsNumber;

}

function IsNumeric(sText) { 
    sText=sText.value;
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
            alert("Inserire un numero");
            document.getElementById(sText).value =0;
           
            return false;
        }
    }
    return IsNumber;

}

function fotogp(e)
    {
   // ctl00_ContentPlaceHolder1_GridView1_ctl02_Label1
    var oggetto = e.id.substr(0,13);  
    var nriga = e.id.substr(13);
    if (nriga.substr(0,1)=="0") {
        nriga = e.id.substr(14);            
    }        
    var riga =  parseInt(nriga);   
    var p_campo = oggetto + padNum(riga,2)+ "_" + "chiaveidl";
    var idcat = document.getElementById(p_campo).value;
    var width = 830
    var height = 650
    var ah = screen.availHeight - 30;
    var aw = screen.availWidth - 10;
    var sinistra = (aw - width) / 2;
    var alto = (ah - height) / 2;
    
    var mywin=window.open("fotogallery.aspx?fotocart=" + idcat ,"","location=no,menubar=no,resizable=no,width="+ width +",height="+height+",top="+alto+",left="+sinistra+ ",status=no,toolbar=no,scrollbars=no");
    }

    function controlla_option(e) {
        var oggetto = e.id;
        var a = document.getElementById("txtstato").value;
        if ((a == "Modifica") || (a == "Inserisci")) {
            return true
        } else {
            alert("Entrare prima in Modifica della Pagina");
            return false
        }
    }

function fotog(e)
    {
   // ctl00_ContentPlaceHolder1_GridView1_ctl02_Label1
    var oggetto = e.id.substr(0,39);  
    var nriga = e.id.substr(39);
    if (nriga.substr(0,1)=="0") {
        nriga = e.id.substr(40);            
    }        
    var riga =  parseInt(nriga);   
    var p_campo = oggetto + padNum(riga,2)+ "_" + "chiaveidl";
    var idcat = document.getElementById(p_campo).value;
    var width = 830
    var height = 650
    var ah = screen.availHeight - 30;
    var aw = screen.availWidth - 10;
    var sinistra = (aw - width) / 2;
    var alto = (ah - height) / 2;
    
    var mywin=window.open("fotogallery.aspx?fotocart=" + idcat ,"","location=no,menubar=no,resizable=no,width="+ width +",height="+height+",top="+alto+",left="+sinistra+ ",status=no,toolbar=no,scrollbars=no");
    }


function confermiuscita()
{
  var oggetto =  "ctl00_ContentPlaceHolder1";
  var pp_pass = oggetto + "_txtoldp";
 
  
  var conferma = confirm('Sei Sicuro di voler disattivare la tua pagina pubblicitaria?');        
  if (conferma)
  {
     pp_stato = oggetto + "_txtstato";
     document.getElementById(pp_stato).value ="uscita";     
     document.forms[0].submit();     
  } else {
      return false;
  }
}

function esciportale()
{
 window.location.href("esciportale.aspx")				  			                        
}




function comefunziona()
{
var mywin=window.open("come_funzionat.aspx","","location=,menubar=,resizable=no,width=850,height=600,status=no,toolbar=no,scrollbars");													  			                        

//var mywin=window.open("come_funziona.aspx","","location=,menubar=,resizable=no,width=850,height=600,status=no,toolbar=no,scrollbars");													  			                        
}


function cambiavirgola(valore) 
{   
    valore.value = valore.value.replace(",", ".");   
    return valore;   
}

function isnum(obj) 
{
    if (isNaN(obj.value) || parseInt(obj.value)<0 || parseInt(obj.value) > 999999)
    {
    alert('Campo numerico, immettere solo numeri!, e usare il punto per la virgola');
    obj.value="";
    obj.focus();
    }
}


function paginapub(e)
 {
    var oggetto = e.id.substr(0,39);    
    var nriga = e.id.substr(39);     
    if (nriga.substr(0,1)=="0") {
        nriga = e.id.substr(40);            
    }        
    var riga =  parseInt(nriga);           
    var p_campo = oggetto + padNum(riga,2)+ "_" + "idfor";        
    var idfor = document.getElementById(p_campo).innerHTML;        
    if ( idfor > 0)
    {                            
      var mywin=window.open("paginaforn.aspx?nrforn=" + idfor ,"","location=,menubar=,resizable=no,width=850,height=600,status=no,toolbar=no,scrollbars");													  			                        
    }
 }


     
 

 function cambiopassw(e)
          {
           var ncampo = "ctl00_ContentPlaceHolder1_txtpassw";
           //var valore = window.document.getElementById(ncampo).value;
           var valore = e;           
            var width = 300;
            var height = 220;
            var ah = screen.availHeight - 30;
            var aw = screen.availWidth - 10;
            var sinistra = (aw - width) / 2;
            var alto = (ah - height) / 2;
	        var mywin=window.open("cambio_passw.aspx?filtro="+valore,"","location=no,menubar=no,resizable=no,width="+ width +",height="+height+",top="+alto+",left="+sinistra+ ",status=no,toolbar=no,scrollbars=no");
          }

function backButtonOverride()
{
  setTimeout("backButtonOverrideBody()", 0);
}

function backButtonOverrideBody()
{
  // Works if we backed up to get here
  try {
    history.forward();
  } catch (e) {
    // OK to ignore
  }
  // Every quarter-second, try again. The only
  // guaranteed method for Opera, Firefox,
  // and Safari, which don't always call
  // onLoad but *do* resume any timers when
  // returning to a page
  setTimeout("backButtonOverrideBody()", 0);
}

 function annullavaliditapw(e)
         {
            Page_ValidationActive=false;            
            var pcampo = e.id.substr(0,26) + "TXTSTATO"
            alert(pcampo);
            document.getElementById(pcampo).value="manda_pw"
            document.forms[0].submit();              
         }


 function annullavalidita(e)
         {
             Page_ValidationActive = false;

             switch (e) {

                 case "import":
                     location.href = "menu.aspx"; 
                     break;                
                 default:
                     location.href = "login.aspx"; 
             } 
                        
         }



function cercalookn(e, tabella, campodes, campocod, altrocampo, altrocampo2, lung) {

             var a = document.getElementById("txtstato").value;
               if (a != "Modifica" && a !="Inserisci") {
                 alert("Cliccare prima su Modifica");
             } else {

                 var oggetto = e.id.substr(0, lung);

                 var nriga = e.id.substr(lung);

                 if (nriga.substr(0, 1) == "0") {
                     nriga = e.id.substr(lung + 1);
                 }
                 var riga = parseInt(nriga);

                 if (riga == null) {
                     riga = 0;
                 }

                 if (riga > 0) {
                     var newcampo = oggetto + padNum(riga, 2) + "_" + altrocampo;
                     var newcampo2 = oggetto + padNum(riga, 2) + "_" + altrocampo2;
                     var p_campo = oggetto + padNum(riga, 2) + "_" + campodes;
                     var p_campo = window.document.getElementById(p_campo).value;
                     //var altrocampo =  window.document.getElementById(newcampo).value;
                     //var altrocampo2 =  window.document.getElementById(newcampo2).value;

                 } else {

                     var newcampo = oggetto + altrocampo;
                     var newcampo2 = oggetto + altrocampo2;
                     var p_campo = oggetto + campodes;
                     var p_campo = window.document.getElementById(p_campo).value;
                     //var altrocampo =  window.document.getElementById(newcampo).value;
                     //var altrocampo2 =  window.document.getElementById(newcampo2).value;
                 }

                 if (p_campo == null) {
                     p_campo = '';
                 }

                 if ((tabella == 'COMUNIN' || tabella == 'COMUNI')  && p_campo == '') {
                     alert("Inserire la parola da cercare");
                     return true
                 }
                 if ((tabella == 'FORNITORI' || tabella == 'FORNITORIF') && p_campo == '') {
                     alert("Inserire la parola da cercare");
                     return true
                 }

                 if ((tabella == 'CLIENTI' || tabella == 'CLIENTIF') && p_campo == '') {
                     alert("Inserire la parola da cercare");
                     return true
                 }


                 var width = 500
                 var height = 300
                 var ah = screen.availHeight - 30;
                 var aw = screen.availWidth - 10;
                 var sinistra = (aw - width) / 2;
                 var alto = (ah - height) / 2;
                 var mywin = window.open("popup.aspx?filtro=" + tabella + "&filtro2=" + p_campo + "&CAMPO1=" + campodes + "&CAMPO2=" + campocod + "&CAMPO3=" + altrocampo + "&CAMPO4=" + altrocampo2 + "&OGGETTO=" + oggetto + "&numriga=" + riga, "", "location=no,menubar=no,resizable=no,width=" + width + ",height=" + height + ",top=" + alto + ",left=" + sinistra + ",status=no,toolbar=no,scrollbars=yes,modal=yes");


             }
}



function puliscicar(valore)
{
    da_sost=array("'",chr(34),"<",">","\(","\)","%","\?",chr(232),chr(233),chr(236),chr(242),chr(224),chr(249));
    sostituti=array("&#39;","&quot;","&lt;","&gt;","&#40;","&#41;","&#63;","&#44;","&#232;","&#233;","&#236;","&#242;","&#224;","&#249;");
    for (i=0; i<14; i++) { 
    valore = Replace(valore, sostituti[i], da_sost[i]);
    }
    return valore;
}

 function cmddata_onclick(campo)
   {   
    var width = 160;
    var height = 205;
    var ah = screen.availHeight - 30;
    var aw = screen.availWidth - 10;
    var sinistra = (aw - width) / 2;
    var alto = (ah - height) / 2;   
    var mywin=window.open("datapopup.aspx?filtro="+campo+"&amp;filtro2=NORMALE","","location=no,menubar=no,resizable=no,width="+ width +",height="+height+",top="+alto+",left="+sinistra+ ",status=no,toolbar=no,scrollbars=no");   
   }
   
   
function isCodiceFiscale(obj)
{
 var nTempNum;
 var nAppoNum;
 var i;
 var szTempCodFisc;
 var szLastChar;
 var szValued;
 var szInValued;
 
 szCodFisc = obj.value;
 
 if (szCodFisc.length<16)
 {
  if (szCodFisc.length == 11)
  {
    ControllaPIVA(obj);
  } else {
     alert("Codice Fiscale errato");
     obj.value = "";
     return false;
  }
 } else {
     if (szCodFisc == "") return false;
     else {
      szCodFisc =szCodFisc.toUpperCase();
      szLastChar = szCodFisc.substr(szCodFisc.length - 1,1);
      szTempCodFisc =szCodFisc.substr(0,szCodFisc.length - 1);
      nTempNum = 0;
      i = 0;
     
      while (true) {
       szValued="B1A0KKPPLLC2QQD3RRE4VVOOSSF5TTG6UUH7MMI8NNJ9WWZZYYXX";
       // I DISPARI
       szInValued=szTempCodFisc.substr(i,1);
       nAppoNum = szValued.search(szInValued);
       nTempNum = nTempNum + (nAppoNum &  0x7FFE) / 2;
       i = i + 1;
       if (i>15)
        break;
       szValued="A0B1C2D3E4F5G6H7I8J9KKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ";
       // I PARI
       szInValued=szTempCodFisc.substr(i,1);
       nAppoNum = szValued.search(szInValued);
       nTempNum =  nTempNum + (nAppoNum &  0x7FFE) / 2;
       i = i + 1;
      }
      nTempNum = nTempNum % 26; //mod
      szValued="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      szValued= szValued.substr(nTempNum, 1);
      if (szValued==szLastChar) return true;
      else {
        obj.value = "";
        alert("Codice Fiscale errato");
      }
     }
  } 
}

function ControllaPIVA(oggetto) {
    
    var pi = oggetto.value; 
    //pi = pi.value;
    if( pi == '' )  return '';   
    if( pi.length != 11 )
    {
        alert("Lunghezza della partita IVA non \n" +
            "corretta: la partita IVA dovrebbe essere lunga \n" +
            "esattamente 11 caratteri .\n");
    }
    validi = "0123456789";
    for( i = 0; i < 11; i++ ){
        if( validi.indexOf( pi.charAt(i) ) == -1 )
            alert("La partita IVA contiene un carattere non valido '" +
                pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n");
    }
    s = 0;
    for( i = 0; i <= 9; i += 2 )
        s += pi.charCodeAt(i) - '0'.charCodeAt(0);
    for( i = 1; i <= 9; i += 2 ){
        c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
        if( c > 9 )  c = c - 9;
        s += c;
    }
    if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
    {
        alert("Partita IVA non valida:\n" +
            "il codice di controllo non corrisponde.\n");
             document.getElementById(oggetto.id).value ="";
             
    }
    document.getElementById(oggetto.id).value = pi.substr(0, 11);
    return ;
}


function padNum(number, numDigits) {
var str = number.toString();
while (str.length < numDigits) str = '0' + str;
return(str);
}


function InterceptKey() {
    //alert(window.event.keyCode);

    if (event.keyCode == 13) {
        window.event.keyCode = 9;
    }

    if (event.keyCode == 8) {
        window.event.keyCode = 0;
    }

    if (event.keyCode == 38) {
        window.event.keyCode = 64;
    }
}




function controlladata(valore)
{
    //ASSEGNO AD UNA VARIABILE IL CONTENUTO DEL CAMPO TESTO valore=document.mioForm.campo.value
    //ESTRAGGO I VALORE RELATIVI A GIORNO,MESE,ANNO
    primacoppia=valore.value.substr(0,2);

    secondacoppia=valore.value.substr(3,2);

    quadrupla=valore.value.substr(6,4);

    //CONVERTO I VALORI STRINGA IN NUMERI

    numero=eval(primacoppia);
    numero1=eval(secondacoppia);
    numero2=eval(quadrupla);

    //estraggo le posizioni relative agli slash
    primoslash=valore.value.substr(2,1);
    secondoslash=valore.value.substr(5,1);
    //CALCOLO LA LUNGHEZZA DELLE VARIABILE CHE CONTENGONO I NUMERI
    primalunghezza=primacoppia.length;
    secondalunghezza=secondacoppia.length;
    terzalunghezza=quadrupla.length;
    //alert(primalunghezza + " " + numero + " " + secondalunghezza + " " + numero1 + " " + terzalunghezza + " " + numero2);

    if ((primalunghezza=2) && (numero>=1) && (numero<=31) && (secondalunghezza=2) && (numero1>=1) && (numero1<=12) && (terzalunghezza=4) && (numero2>=1800) && (numero2<=3000))
        {
        }
    else
    {
        alert("Data errata inserire la data nel seguente formato es: 31-01-2011 ");
        valore.value = "";
    }

} 

function padNum(number, numDigits) {
var str = number.toString();
while (str.length < numDigits) str = '0' + str;
return(str);
}

 function mappa()
    {
    var comune = document.getElementById("txtcomune").value;
    var indi   = document.getElementById("txtindirizzo").value;
    var sito = "http://maps.google.it/maps?f=q&hl=it&geocode=&time=&date=&ttype=&q=" + comune + " " + indi;
    var mywin=window.open(sito ,"","location=no,directories=no,menubar=no,resizable=yes,width=850,height=600,status=no,toolbar=no,scrollbars");
}


function trim(stringa) {
    while (stringa.substring(0, 1) == ' ') {
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length - 1, stringa.length) == ' ') {
        stringa = stringa.substring(0, stringa.length-1);
    }
    return stringa;
}
