// Set up the text swap for the company field
function setCompanyText () {
    var el = $("#input_company");

    if (el.val () == "") {
        el.val ("Company");
    } // if ()
} // setCompanyText ()

function checkCompanyText () {
    var el = $("#input_company");

    if (el.val () == "Company") {
        el.val ("");
    } // if ()
} // checkCompanyText ()

// Set up the text swap for the name field
function setNameText () {
    var el = $("#input_name");

    if (el.val () == "") {
        el.val ("Name*");
    } // if ()
} // setNameText ()

function checkNameText () {
    var el = $("#input_name");

    if (el.val () == "Name*") {
        el.val ("");
    } // if ()
} // checkNameText ()

// Set up the text swap for the email field
function setEmailText () {
    var el = $("#input_email");

    if (el.val () == "") {
        el.val ("Email Address*");
    } // if ()
} // setEmailText ()

function checkEmailText () {
    var el = $("#input_email");

    if (el.val () == "Email Address*") {
        el.val ("");
    } // if ()
} // checkEmailText ()

// Set up the text swap for the phone field
function setPhoneText () {
    var el = $("#input_phone");

    if (el.val () == "") {
        el.val ("Phone Number*");
    } // if ()
} // setPhoneText ()

function checkPhoneText () {
    var el = $("#input_phone");

    if (el.val () == "Phone Number*") {
        el.val ("");
    } // if ()
} // checkPhoneText ()

// Set up the text swap for the message field
function setMessageText () {
    var el = $("#input_message");

    if (el.val () == "") {
        el.val ("How can we help you today?");
    } // if ()
} // setMessageText ()

function checkMessageText () {
    var el = $("#input_message");

    if (el.val () == "How can we help you today?") {
        el.val ("");
    } // if ()
} // checkMessageText ()

// Set up the text swap for the code field
function setCodeText () {
    var el = $("#input_code");

    if (el.val () == "") {
        el.val ("Verification Code*");
    } // if ()
} // setCodeText ()

function checkCodeText () {
    var el = $("#input_code");

    if (el.val () == "Verification Code*") {
        el.val ("");
    } // if ()
} // checkCodeText ()

/**
 *  This does the form submit and checking
 */
function submitForm (e) {
    e.preventDefault ();

    var el_company = $("#input_company");
    var el_name = $("#input_name");
    var el_email = $("#input_email");
    var el_phone = $("#input_phone");
    var el_message = $("#input_message");
    var el_code = $("#input_code");

    var error = false;
    var errors = [];

    if (el_name.val () == "Name*" || el_name.val ().length < 2) {
        error = true;
        errors.push ("You must include your name.");
    } // if ()

    var email_filter = /^^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,6})+$/;

    if (el_email.val () == "Email Address*" || !email_filter.test (el_email.val ())) {
        error = true;
        errors.push ("Your email address is not valid.");
    } // if ()

    if (el_phone.val () != "Phone Number*" && el_phone.val ().length < 8) {
        error = true;
        errors.push ("You must include your phone number.");
    } // if ()

    if (el_message.val ().length < 2) {
        error = true;
        errors.push ("You must include your message.");
    } // if ()

    if (!error) {
        var data = $("#contact_footer_form").serialize ();

        $.post ("mailer.php", data, function (data) {
            if (data.success == true) {
                alert ("Your message has been received.");

                $("#contact_footer_form").find (':input').each (function () {
                    switch(this.type) {
                        case "password":
                        case "select-multiple":
                        case "select-one":
                        case "text":
                        case "textarea":
                            $(this).val ("");
                            break;
                        case "checkbox":
                        case "radio":
                            this.checked = false;
                    }
                });

                setCompanyText ();
                setNameText ();
                setEmailText ();
                setPhoneText ();
                setMessageText ();
                setCodeText ();
            } // if ()
            else {
                var error = "Please check these areas:";

                for (var i = 0; i < data.errors.length; i++) {
                    error = error + "\n  - " + data.errors [i];
                } // for ()

                alert (error);
            } // else
        }, "json");
    } // if ()
    else {
        var error = "Please check these areas:";

        for (var i = 0; i < errors.length; i++) {
            error = error + "\n  - " + errors [i];
        } // for ()

        alert (error);
    } // else
} // sumbitForm ()





// Start off the listeners
$(document).ready (function () {
    // COMPANY input
    var el_company = $("#input_company");
    el_company.blur (setCompanyText);
    el_company.focus (checkCompanyText);

    // NAME input
    var el_name = $("#input_name");
    el_name.blur (setNameText);
    el_name.focus (checkNameText);

    // EMAIL input
    var el_email = $("#input_email");
    el_email.blur (setEmailText);
    el_email.focus (checkEmailText);

    // PHONE input
    var el_phone = $("#input_phone");
    el_phone.blur (setPhoneText);
    el_phone.focus (checkPhoneText);

    // MESSAGE input
    var el_message = $("#input_message");
    el_message.blur (setMessageText);
    el_message.focus (checkMessageText);

    // CODE input
    var el_code = $("#input_code");
    el_code.blur (setCodeText);
    el_code.focus (checkCodeText);

    // Form submit
    $("#input_submit").click (submitForm);
});