function getSelectValue(id) {
    return $("[#" + id).val();
}


function getRadioValue(name) {
    return $("input[name=" + name + "]:checked").val();
}


function submitQuiz() {
    $.ajax({
        type: "GET",
        url: "/syphilis-quiz/",
        data: {
            'gender':getSelectValue("id_gender"), 
            'age':getSelectValue("id_age"), 
            'symptoms':getRadioValue("symptoms"), 
            'unprotected':getRadioValue("unprotected"), 
            'multiple_partners':getRadioValue("multiple_partners"), 
            'gay':getRadioValue("gay")
            },
        success: function(msg){
            $('.riskTest').empty().append(msg);
        }
    });
}

$(document).ready(function(){
    
    // Controls spinner while ajax request is active
    $('.riskTest')
        .ajaxStart(function() {
            $('.riskTest').empty().append('<img id="spinner" src="{{ MEDIA_URL }}i/spinner.gif" alt="Wait"/>');
        })
        .ajaxStop(function() {
    });
    
    
    // Validate Form
    var validator = $("form").validate({
        rules: {
            gender: "required",
            age: "required",
            symptoms: "required",
            unprotected: "required",
            multiple_partners: "required",
            gay: "required"
        },
        messages: { 
            gender: { required: "Gender is required." }, 
            age: { required: "Age is required." }, 
            symptoms: { required: "Are you experiencing symptoms is required." }, 
            unprotected: { required: "Have you had unprotected sex in the last 12 months? (including oral sex) is required." }, 
            multiple_partners: { required: "Have you had sex with more than 1 person in the last 12 months? is required." }, 
            gay: { required: "Are you gay or bisexual? is required" } 
        },
        // the errorPlacement has to take the table layout into account 
        errorPlacement: function(error, element) { 
            error.appendTo("#quiz_errors"); 
        },
        submitHandler: function() {
            submitQuiz();
            return false;
        }
    });


    // Enable Google Analytics Event Tracking
    if (pageTracker) {

        field_list = {}

        field_list["symptoms"] = "Are you experiencing symptoms?";
        field_list["unprotected"] = "Have you had unprotected sex in the last 12 months? (including oral sex)";
        field_list["multiple_partners"] = "Have you had sex with more than 1 person in the last 12 months?";
        field_list["gay"] = "Are you gay or bisexual?";
        
        $.each(field_list, function(name, label) {
            $("input[name=" + name + "]").change(function () {
                value = $("input[name=" + name + "]:checked").val();
                pageTracker._trackEvent('Syphilis Risk Quiz', label, value);
            });
        });
        
        $("#id_age").change(function() {
           pageTracker._trackEvent('Syphilis Risk Quiz','Age', $(this).val()); 
        });
        $("#id_gender").change(function() {
           pageTracker._trackEvent('Syphilis Risk Quiz','Gender', $(this).val()); 
        });
    }
});
