function we_KeyPressCF(IDElem, tasto) {
    var value = IDElem.value;
    
    var codice_tasto = tasto.keyCode ? tasto.keyCode : tasto.which ? tasto.which : tasto.charCode;

    document.getElementById('pluto').value = value.length;
    document.getElementById('pippo').value = codice_tasto;

    tasto = 80;

    // il CF è lungo max 16 caratteri AAAAAAANNANNANNNA (A=alfabetico, N=numerico)
    if (value.length > 5 && codice_tasto != 8 && codice_tasto != 46)
    {
        return false;
    }

    return true;

}
function we_escludi_PASSWORD(IDElem, tasto) {
    var tasti_validi = /[a-zA-Z0-9]{1}/;
    return true; //we_escludi(IDElem, tasto, tasti_validi);
}
function we_textMask_PASSWORD(IDElem) {
//    var pattern = /[a-zA-Z0-9][_\\.a-zA-Z0-9-]+@([a-zA-Z0-9][0-9a-zA-Z-]+\\.)+([a-zA-Z]{2,4})/;
//    var pattern = /[a-zA-Z0-9][_\\.a-zA-Z0-9-]+@([a-zA-Z0-9][_a-zA-Z0-9-]+\\.)+([a-zA-Z]{2,4})/;
    var pattern = /^[a-zA-Z0-9_]{8,32}$/;
    we_color_elem(IDElem, pattern);
}

function we_escludi_USER(IDElem, tasto) {
    var tasti_validi = /[a-zA-Z0-9_]{1}/;
    return true; //we_escludi(IDElem, tasto, tasti_validi);
}
function we_textMask_USER(IDElem) {
//    var pattern = /[a-zA-Z0-9][_\\.a-zA-Z0-9-]+@([a-zA-Z0-9][0-9a-zA-Z-]+\\.)+([a-zA-Z]{2,4})/;
//    var pattern = /[a-zA-Z0-9][_\\.a-zA-Z0-9-]+@([a-zA-Z0-9][_a-zA-Z0-9-]+\\.)+([a-zA-Z]{2,4})/;
    var pattern = /^[a-zA-Z0-9]{3,20}$/;
    we_color_elem(IDElem, pattern);
}

function we_escludi_DATA(IDElem, tasto) {
    var tasti_validi = /[/0-9]{1}/;
    return we_escludi(IDElem, tasto, tasti_validi);
}
function we_textMask_DATA(IDElem) {
//    var pattern = /[a-zA-Z0-9][_\\.a-zA-Z0-9-]+@([a-zA-Z0-9][0-9a-zA-Z-]+\\.)+([a-zA-Z]{2,4})/;
//    var pattern = /[a-zA-Z0-9][_\\.a-zA-Z0-9-]+@([a-zA-Z0-9][_a-zA-Z0-9-]+\\.)+([a-zA-Z]{2,4})/;
    var pattern = /^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[-/.](19|20)\d\d/;
    we_color_elem(IDElem, pattern);
}

function we_escludi_ANNO(IDElem, tasto) {
    var tasti_validi = /[0-9]{1}/;
    return we_escludi(IDElem, tasto, tasti_validi);
}
function we_textMask_ANNO(IDElem) {
    var pattern = /^(19|20)\d\d/;
    we_color_elem(IDElem, pattern);
}

function we_escludi_EMAIL(IDElem, tasto) {
    var tasti_validi = /[@]{1}/;
    return true; //we_escludi(IDElem, tasto, tasti_validi);
}
function we_textMask_EMAIL(IDElem) {
//    var pattern = /[a-zA-Z0-9][_\\.a-zA-Z0-9-]+@([a-zA-Z0-9][0-9a-zA-Z-]+\\.)+([a-zA-Z]{2,4})/;
//    var pattern = /[a-zA-Z0-9][_\\.a-zA-Z0-9-]+@([a-zA-Z0-9][_a-zA-Z0-9-]+\\.)+([a-zA-Z]{2,4})/;
//    var pattern = /^[a-zA-Z0-9]+(\.?[a-zA-Z0-9\-\_]+)*\@[a-zA-Z]+(\.?[a-zA-Z0-9\-\_]+)*\.[a-zA-Z]{2,}$/;
//    we_color_elem(IDElem, pattern);
}
function we_escludi_IMPORTO(IDElem, tasto) {
    var tasti_validi = /([\,0-9]){1}/;
    return we_escludi(IDElem, tasto, tasti_validi);
}
function we_textMask_IMPORTO(IDElem) {
    var pattern = /([0-9]{1,6})|([0-9]{1,4}[,][0-9]{1,2})/;
    we_color_elem(IDElem, pattern);
}

function we_escludi_PIVA(IDElem, tasto) {
    var tasti_validi = /[0-9]{1}/;
    return we_escludi(IDElem, tasto, tasti_validi);
}
function we_textMask_PIVA(IDElem) {
    var pattern = /[0-9]{11}/;
    we_color_elem(IDElem, pattern);
}

function we_escludi_CAP(IDElem, tasto) {
    var tasti_validi = /[0-9]{1}/;
    return we_escludi(IDElem, tasto, tasti_validi);
}
function we_textMask_CAP(IDElem) {
    var pattern = /[0-9]{5}/;
    we_color_elem(IDElem, pattern);
}

function we_escludi_CF(IDElem, tasto) {
    var tasti_validi = /[a-zA-Z0-9]{1}/;
    return we_escludi(IDElem, tasto, tasti_validi);
}
function we_textMask_CF(IDElem) {
    var pattern = /([a-zA-Z]{6}[0-9]{2}[a-zA-Z][0-9]{2}[a-zA-Z][0-9]{3}[a-zA-Z])|([0-9]{11})/;
    we_color_elem(IDElem, pattern);
}

function we_ucase(IDElem) {
    var ucase = IDElem.value.toUpperCase();
    if (IDElem.value != ucase)
        IDElem.value = ucase;
}

function we_escludi(IDElem, tasto, tasti_validi) {
    var codice_tasto = tasto.keyCode ? tasto.keyCode : tasto.which ? tasto.which : tasto.charCode;
    var chr = String.fromCharCode(codice_tasto);

//    alert(codice_tasto);
//    document.getElementById('pippo').value = codice_tasto + ' - ' + chr;
//    for(var i=0; i < 255; i++)
//        document.getElementById('pippo').value = document.getElementById('pippo').value + '; ' + i + ' = ' + String.fromCharCode(i);
//
//    tasto.keyCode = 76;
//    tasto.which = 76;
//    tasto.charCode = 76;
//        
    // 8 = backspace; 46 = canc; 9 = tab; 18 = alt; 37 = cursore sx; 39 = cursore dx; 35 = fine; 36 = inizio;
    if (chr.search(tasti_validi) && codice_tasto != 8 && codice_tasto != 9 && codice_tasto != 46 && codice_tasto != 18 && codice_tasto != 37 && codice_tasto != 39 && codice_tasto != 35 && codice_tasto != 36)
        return false;
    
    return true;
    
    
}

function we_color_elem(IDElem, pattern) {
    var result = IDElem.value.search(pattern);
    
    var format = document.getElementById(IDElem.name + '_format');
    
    if (format != null)
    {
        format.style.color = (result && IDElem.value != '') ? '#FF0000' : '#C0C0C0';
        format.style.fontSize = (result && IDElem.value != '') ? 'small' : 'x-small';
    }

    IDElem.style.color = (!result || IDElem.value == '') ? '#000000' : '#FF0000';
    IDElem.style.fontWeight = (!result || IDElem.value == '') ? 'normal' : 'bold';
    
}

