function isServiceChar(e, nCode) {
    if (nCode == 188 ||
        nCode == 9 || /*tab*/
        nCode == 13 || /*invio*/
        nCode == 37 ||
        nCode == 39 ||
        nCode == 8 ||
        nCode == 46) {
        return true;
    }
    return false;

}

function txt_CheckNumber(txt, e) {
    e = (e) ? e : ((window.event) ? event : null);
    if (e) {
        var charCode = (e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : ((e.which) ? e.which : 0));
        if (!isServiceChar(e, charCode)) {
            var ch = String.fromCharCode(charCode);
            var valid = new String("0123456789,.");

            txt.value = txt.value.replace(".", ",");

            var pos = valid.indexOf(ch);

            if (pos < 0) {
                if (window.event)
                    e.returnValue = false;
                else
                    e.preventDefault();
            }
        }
    }
}

function txt_CheckEmail(txt) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(txt.val())) {
        return false;
    } else {
        return true;
    }
}

function formatNumber(sValue, nDecimals, bNumber) {

    var alertbox = false;

    var nDecs = Math.pow(10, nDecimals);
    var sDecs = new String(nDecs);
    sDecs = sDecs.substr(1);

    sValue = new String(sValue);
    if (bNumber == null || !bNumber) {
        if (sValue.indexOf(",") >= 0) {
            sValue = sValue.replace(".", "");
            sValue = sValue.replace(",", ".");
        }
    }
    nValue = parseFloat(sValue) * nDecs;
    nValue = Math.round(nValue);
    if (isNaN(nValue)) {nValue = 0;}

    if (nValue == 0) {
        if (nDecimals <= 0) {
            return '0';
        }
        else {
            return '0,' + sDecs;
        }
    }

    //if(nValue>99999999999)alert("trillion is not supported - only millions.");
    /* converts tmp to decimal cents and adds a 0 to .1 to .9 or adds .00 to 0 */
    var cen = '';
    if (nDecimals > 0) {
        cen = nValue % nDecs;
        if (cen < 0) {cen = "," + sDecs}
        else if (cen < nDecs) {
            var sCen = ',';
            var s = new String(cen);
            for (var i = 1; i <= (sDecs.length - s.length); i++) {
                sCen += '0';
            }
            cen = sCen + cen;
        }
        else {cen = "," + cen}
        if (alertbox) alert("Cents " + cen);  /* Cents alert dialog box */
    }

    var hun = '';
    var tho = '';
    var mil = '';

    nValue = parseInt(nValue / nDecs);
    hun = nValue - ((parseInt(nValue / 1000)) * 1000);
    if (nValue >= 1000) {
        if (hun < 10)
            hun = '00' + hun;
        else if (hun < 100)
            hun = '0' + hun;
    }

    nValue = parseInt(nValue / 1000)
    tho = nValue - ((parseInt(nValue / 1000)) * 1000);
    if (nValue >= 1000) {
        if (tho < 10)
            tho = '00' + tho;
        else if (tho < 100)
            tho = '0' + tho;
    }
    else {
        if (tho == '0')
            tho = '';
    }
    if (tho != '')
        tho = tho + '.';

    nValue = parseInt(nValue / 1000)
    mil = nValue - ((parseInt(nValue / 1000)) * 1000);
    if (mil == '0')
        mil = '';
    if (mil != '') {
        mil = mil + '.';
    }

    return mil + tho + hun + cen;

    var strimporto_nuovo = "_importo";
    var strimporto_vecchio = "_importo_vecchio";
    importo_nuovo = roundVal(jQuery("#" + field + strimporto_nuovo).val());
    importo_vecchio = roundVal(jQuery("#" + field + strimporto_vecchio).val());

    importo_netto = roundVal(jQuery("#net-amount").val());
    importo_totale = roundVal(jQuery("#total-amount").val());

    jQuery("#net-amount").val(roundVal(importo_netto - importo_vecchio + importo_nuovo));
    jQuery("#total-amount").val(roundVal(importo_totale - importo_vecchio + importo_nuovo));
    jQuery("#" + field + strimporto_vecchio).val(importo_nuovo); 
}

function getNumber(val) {
    var sValue = new String(val);
    sValue = sValue.replace(".", "");
    sValue = sValue.replace(",", ".");
    var n = parseFloat(sValue);
    if (isNaN(n)) n = 0;
    return n;
}

function moca_checkFields() {
    var ret=true;
    jQuery('.MOCAWC.mandatory').each(function(index) {
        var id = "#" + this.id;
        var label = jQuery(id  + " label");
        
        var value="";
        var field = jQuery(id  + " input");
        if (field.length==0) {
            field = jQuery(id  + " select");
        }
        switch(field.attr("type")) {
            case "checkbox":
            case "radio":
                value=field.attr("checked");
                break;
            default:
                value=new String(field.val());
                value=value.trim();
                break;
        }
        var obj=jQuery(id);
        obj.removeClass("error");                
        if (value=="") {
            obj.addClass("error");
            if (ret == true) {
                ret=field;
            }
        } 
    });   
    
    if (ret == true) {
        jQuery('.MOCAWC.email').each(function(index) {
            var id = "#" + this.id;
            var obj=jQuery(id);
            var field=jQuery(id  + " input");
            obj.removeClass("error");                
            if (!txt_CheckEmail(field)) {
                obj.addClass("error");
                if (ret==true) {
                    ret=field;
                }
            }
        });
    }
    
    return ret;
}
