function condition(ele, field, match) {
    var question    = $(ele).parents("div.question");
    var subquestion = $("+ div.question", question);
    var values      = $(":input[name='"+ field +"']").fieldValue();
    var isArray     = true;

    $("div.required", question).remove();

    if (values.length < 1) {
        values  = ele.value;
        isArray = false;
    }

    var show = false;
    switch (match.substr(0, 1)) {
        case ">":
            show = parseInt(values) > parseInt(match.substring(1));
            break;
        case "<":
            show = parseInt(values) < parseInt(match.substring(1));
            break;
        default:
            if (isArray) {
                show = inArray(values, match.substring(1));
            } else {
                show = (values == match.substring(1));
            }
    }

    if (show) {
        subquestion.show();
        $("div.options", question).css("margin-bottom", "0");
        subquestion.css("margin-left", "1.5em");
    } else {
        subquestion.hide();
    }
}
function inArray(arr, value) {
    var i;
    for (i = 0; i < arr.length; i++) {
        if (arr[i] === value) {
            return true;
        }
    }
    return false;
}
function validateSurvey() {
    valid = true;
    $("div.required").remove();

    var requiredField = "<div class='required'><p>This is a required field</p></div>";

    $("#survey div.question").each(function(i) {
        /* CHECKBOXES */
        var checkboxes = $(":checkbox.required:first", this);
        if (checkboxes.length > 0) {
            var name    = $(checkboxes).attr("name");
            var value   = $(":checkbox", this).fieldValue();
            if (value.length < 1) {
                $("div.options", this).append(requiredField);
                valid = false;
            }
        }

        /* RADIO BUTTONS */
        var radioButtons = $(":radio.required:first", this);
        if (radioButtons.length > 0) {
            var name    = $(radioButtons).attr("name");
            var value   = $(":radio", this).fieldValue();
            if (value.length < 1) {
                $("div.options", this).append(requiredField);
                valid = false;
            }
        }

        /* TEXT */
        var text = $(":text.required", this);
        if (text.length > 0) {
            if (text.val().length < 1) {
                $("div.options", this).append(requiredField);
                valid = false;
            }
        }

        /* E-MAIL */
        var email = $(":text.email", this);
        if (email.length > 0) {
            if (email.val().length > 0) {
                var isEmail = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$/.test(email.val());
                if (!isEmail) {
                    $("div.options", this).append("<div class='required'><p>Please input a valid e-mail address</p></div>");
                    valid = false;
                }
            }
        }
    });
    return valid;
}