////==== check the complaint form for malformed input
function check_comp_form() {

    // trim and get all necessary field values that need to be checked
    var val_comp_type =         document.comp_form.comp_type.value =            trim_string(squeeze_string(document.comp_form.comp_type.value));
    var val_comp_address =      document.comp_form.comp_address.value =         trim_string(squeeze_string(document.comp_form.comp_address.value));
    var val_comp_date =         document.comp_form.comp_date.value =            trim_string(squeeze_string(document.comp_form.comp_date.value));
    var val_comp_description =  document.comp_form.comp_description.value =     trim_string(squeeze_string(document.comp_form.comp_description.value));
    var val_your_name =         document.comp_form.your_name.value =            trim_string(squeeze_string(document.comp_form.your_name.value));
    var val_your_address =      document.comp_form.your_address.value =         trim_string(squeeze_string(document.comp_form.your_address.value));
    var val_your_email =        document.comp_form.your_email.value =           trim_string(squeeze_string(document.comp_form.your_email.value));
    var val_your_phone =        document.comp_form.your_phone.value =           trim_string(squeeze_string(document.comp_form.your_phone.value));

    // comp_type
    if (val_comp_type.length == 0) {
        alert("Please enter the type of complaint.");

        // highlight field
        document.comp_form.comp_type.focus();
        return false;
    }

    // comp_address
    if (val_comp_address.length == 0) {
        alert("Please enter the address of violation.");

        // highlight field
        document.comp_form.comp_address.focus();
        return false;
    }

    // comp_date
    if (val_comp_date.length == 0) {
        alert("Please enter the date and time of violation.");

        // highlight field
        document.comp_form.comp_date.focus();
        return false;
    }

    // comp_description
    if (val_comp_description.length == 0) {
        alert("Please enter the description of problem.");

        // highlight field
        document.comp_form.comp_description.focus();
        return false;
    }

    // your_name
    if (val_your_name.length == 0) {
        alert("Please enter your name.");

        // highlight field
        document.comp_form.your_name.focus();
        return false;
    }

    // your_address
    if (val_your_address.length == 0) {
        alert("Please enter your address.");

        // highlight field
        document.comp_form.your_address.focus();
        return false;
    }

    // your_email
    if (val_your_email.length == 0) {
        alert("Please enter your email.");

        // highlight field
        document.comp_form.your_email.focus();
        return false;
    }

    // your_phone
    if (val_your_phone.length == 0) {
        alert("Please enter your phone number.");

        // highlight field
        document.comp_form.your_phone.focus();
        return false;
    }

    return true;
}


////==== submit the complaint form
function submit_comp_form() {
    // check form
    if (check_comp_form()) {
        // disable submit button (to prevent multiple presses)
        document.comp_form.submit_button.disabled = true;

        // submit form
        document.comp_form.submit();
    }
}

/* ############################## 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;
}
