﻿

$(document).ready( function() {
    
   $(".talktous .metric img").each( setPollPercent );
   
   handlePollTable();
   
   // do validation for poll
   $(".pollSubmit").click( function() {
        var isValid = true;
        $("#errorText").empty().hide();
        // make sure each question has one radio button checked
        $("span.answer").each( function() {
               // make sure one input is checked
               var num = $(this).find("input:checked").length;
               if (num !== 1) {
                    isValid = false;
                    $("#errorText").text("Please answer all questions.").show();
               } 
        });// end each

        if (isValid === false) {
            return false;
        } else {
            return true;
        }   
   });
    
});

function setPollPercent() {
    var perc = 100 - Math.floor($(this).attr("percent"));
    $(this).animate ({
        marginLeft: "-" + perc + "%"
    },1000);


}

// this handles the somewhat strange situation with the radio button list
// wherein the other selection is not actually part of the first set of radio button options
// so we have to extend the exclusion logic to that other checkbox
function handlePollTable() {
    $(".answer input:radio").change( function() { 
            
            $(this).parents(".answer").find("input:radio").not(this).each( 
                function() {
                   this.checked = false;
                }            
            )
     });
}
