function clearError(id) {
    var selectedNode = document.getElementById(id);
    if (selectedNode != null) {
        selectedNode.innerHTML = "";
    }
}

function displayError(id, text) {
    var error = document.getElementById(id);
    if(error != null) {
        error.innerHTML = text;
        error.className = "error";
    }
}

function focusError(id) {
    var element = document.getElementById(id);
    if (element != null) {
        element.value = "";
        element.focus();
    }
}

function trim(s) {
    s = s.replace(/^\s+/, "");
    s = s.replace(/\s+$/, "");
    return s;
}

function getValue(id) {
    if (id ==null || id == "") {
        return "";
    }
    var element = document.getElementById(id);
    if (element !=null && element.value != null) {
        return trim(element.value);
    }
    else {
        return "";
    }
}

function validateStartNetworkForm() {
    var contact = getValue("sn-contact");
    var organization = getValue("sn-organization");
    var website = getValue("sn-website");
    var email = getValue("sn-email");
    var phone = getValue("sn-phone");

    var errors = false;

    if (contact == "" && !errors) {
        focusError("sn-contact");
        errors = true;
    }
    if (organization == "" && !errors) {
        focusError("sn-organization");
        errors = true;
    }
    if (website == "" && !errors) {
        focusError("sn-website");
        errors = true;
    }
    if (email == "" && !errors) {
        focusError("sn-email");
        errors = true;
    }
    if (phone == "" && !errors) {
        focusError("sn-phone");
        errors = true;
    }

    if (errors) {
        displayError("sn-missing-required", "Please complete all required fields.");
    } else {
        var contactform = document.getElementById("snform");
        contactform.submit();
    }
    return;
}
