String.prototype.trim = function() {
    return this.replace(/^\s+/, '').replace(/\s+$/, '');
}

function isValid(field, defVal) {
    var val = document.getElementById(field).value;
    if (val.trim() == "" || val.trim() == defVal) {
        return false;
    }
    return true;
}

var regex = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
function isValidEmail(field) {
    var val = document.getElementById(field).value;
    return String(val).search(regex) != -1;
}

function validateForm() {
    var missing = "";
//    if (!isValid("contactform_name", "Enter Full Name")) {
//        missing += "Name is required\n";
//    }
    var contacts = 0;
    if (isValid("contactform_email", "Type Your Email")) {
        if (!isValidEmail("contactform_email")) {
            missing += "Email is invalid\n";
        } else {
            contacts++;
        }
    } 
//    if (!isValid("contactform_procedure", "Enter Procedure")) {
//        missing += "Procedure is required\n";
//    }
    // set phone number to blank if not set (left default value)
    var contactnum = document.getElementById("contactform_number").value;
    if (contactnum == "Enter Phone Number") {
        document.getElementById("contactform_number").value = "";
    } else if (document.getElementById("contactform_number").value != "") {
        contacts++;
    }
    if (contacts == 0) {
        missing += "Please provide a way for us to contact you (email or phone)\n";
    }
    if (missing == "") {
        return true;
    }
    alert("Please fix the following:\n" + missing);
    return false;
}


function toggleField(field, val) {
    if (field.value.trim() == val) {
        field.value = "";
    } else if (field.value.trim() == "") {
        field.value = val;
    }
}