function trim(text) {
    text = text.replace(/^ *$/, "");
    text = text.replace(/^ *([^ ].*[^ ]) *$/, "$1");
    return text;
}

function stripNonDigits(text) {
    return text.replace(/[^0-9]/g, "");
}
    
function formatStringField(field) {
    field.value = trim(field.value);
}

function formatPhoneField(field) {
    var newValue = stripNonDigits(field.value);
    if ((newValue.length == 11) && (newValue.charAt(0) == "1")) {
        newValue = newValue.substr(1, 10);
    }
    if (newValue.length == 10) {
        field.value = newValue.substr(0, 3) + "-" + newValue.substr(3, 3) + "-" + newValue.substr(6, 4);
    }
}

function formatZipCodeField(field) {
    var newValue = stripNonDigits(field.value);
    if (newValue.length == 5) {
        field.value = newValue;
    } else if (newValue.length == 9) {
        field.value = newValue.substr(0, 5) + "-" + newValue.substr(5, 4);
    }
}