////################################################################################################
////########## GENERAL (STRINGS) ###################################################################
////################################################################################################


////==== trim given string of leading and trailing whitespace
function trim_string(str) {
    // trim leading
    str = str.replace(/^[\s]+/, "");

    // trim trailing
    str = str.replace(/[\s]+$/, "");

    return str;
}


////==== squeeze multiple spaces into one in given string
function squeeze_string(str) {
    // squeeze
    str = str.replace(/[\s]+/g, " ");

    return str;
}


////################################################################################################
////########## ADMIN FORM ##########################################################################
////################################################################################################


////==== variables
var submit_in_progress = false;


////==== select given form field
function form_select_field(form, field) {
    document[form + "_form"][field].select();
}


////==== check form for malformed input
function check_contact_us_form() {
    // trim and get all necessary field values
    var var_full_name       = document.contact_us_form.full_name.value      = squeeze_string(trim_string(document.contact_us_form.full_name.value));
    var var_email_address   = document.contact_us_form.email_address.value  = trim_string(document.contact_us_form.email_address.value);
    var var_subject         = document.contact_us_form.subject.value        = squeeze_string(trim_string(document.contact_us_form.subject.value));
    var var_message         = document.contact_us_form.message.value        = trim_string(document.contact_us_form.message.value);

    // full name
    if (var_full_name.length === 0) {
        alert("Please enter your full name.");
        form_select_field("contact_us", "full_name");
        return false;
    }

    // email address
    if (var_email_address.length === 0) {
        alert("Please enter your email address.");
        form_select_field("contact_us", "email_address");
        return false;
    }

    // subject
    if (var_subject.length === 0) {
        alert("Please enter the subject of your message.");
        form_select_field("contact_us", "subject");
        return false;
    }

    // message
    if (var_message.length === 0) {
        alert("Please enter your message.");
        form_select_field("contact_us", "message");
        return false;
    }

    return true;
}


////==== submit form
function submit_contact_us_form() {
    // check form
    if (!submit_in_progress && check_contact_us_form()) {
        // set submit in progress
        submit_in_progress = true;

        // submit form
        document.contact_us_form.submit();
    }
}
