
 function checkPhoneField(phoneField)   {
        //INITIALIZE VARS
        var firstThree = "";
        var middleThree = "";
        var lastNumbers = "";
        var errors = "";
        var phone=phoneField.value;
        // CLEAR ALL NON NUMBER CHARACTERS
        phone = phone.replace(/[\(\)\- ,\.]/g,"");
        // CLEAR LEADING 1

        if (phone.length > 10){
            phone = (phone.charAt(0)=="1"?phone.substr(1,phone.length):phone);
        }

        // CHECK LENGTH OF PHONE NUMBER
        if (phone.length < 10 && phone.length > 0){
            errors += "Invalid phone format: Must include area code\n"
        }
        // CHECK if INPUT IS A NUMBER
        if (isNaN(phone)){
            errors += "Invalid phone format: Invalid characters\n"
        }
        // SHOW ERRORS
        if (errors.length > 0){
            errors += "\nMust use following format: 000-000-0000";
            alert(errors);
            phoneField.focus();
            return false;
        }
        // GET FIRST THREE
        if (phone.length > 2){
            firstThree = "" + phone.substr(0,3);
            // GET SECOND THREE
            if (phone.length > 5){
                middleThree = phone.substr(3,3) + "-";
               // GET REST OF NUMBER
                if (phone.length > 5){
                    lastNumbers = phone.substr(6,phone.length);
                }
            } else {
                lastNumbers = phone.substr(3,phone.length);
            }
        } else {
            lastNumbers = phone;
        }

        // PUT NUMBERS TOGETHER
        if (phone.length >7){
            newPhone = "(" + firstThree + ") " + middleThree + lastNumbers;
        } else {
            newPhone = firstThree + middleThree + lastNumbers;
        }
        phoneField.value = newPhone;
    }

