// determine if a variable is numeric or not
function isNumeric(VARIABLE){
    if(String(VARIABLE).match(/^\d+$/)) return true;
    return false;
}
function tel_autoformat(myValue){
    var telClean = myValue;
        telClean = telClean.replace('(', '');
        telClean = telClean.replace('-', '');
        telClean = telClean.replace('-', '');
        telClean = telClean.replace(')', '');

    // If last value is NOT a number or - or ( or )
    // Alert and remove it
    var lastValue = telClean.substr(-1);
    if(!isNumeric(lastValue)){
        return myValue.substr(0, myValue.length - 1);
    }
    
    // Re-format this number
    if(telClean.length < 4) return '(' + telClean.substr(0, 3);
    else if(telClean.length >= 4 && telClean.length < 7) return '(' + telClean.substr(0, 3) + ')-' + telClean.substr(3, 3);
    else if(telClean.length >= 7 && telClean.length < 11) return '(' + telClean.substr(0, 3) + ')-' + telClean.substr(3, 3) + '-' + telClean.substr(6, 4);
    else if(telClean.length >= 11) return '(' + telClean.substr(0, 3) + ')-' + telClean.substr(3, 3) + '-' + telClean.substr(6, 4);
}



