$(document).ready(function(){
var quiz_score
	/* Radio Button actions */
    $("input:radio").click(function() { 
 
		// find current answer number
		var selected_answer = $(this).parent().index();
			//var explanation = $(this).parents().siblings(".explanationWrapper").children("div:eq("+selected_answer+")").html()+" "+$(this).parent().parent().siblings(".explanationWrapper").children(".fullExplanation").html();
		
			$(this).parent().siblings().slideUp(200);

			// concatenate answer string 

			//fill display explanation with latest string
			//$(this).parent().parent().siblings(".explanationWrapper").children(".displayExplanation").html(explanation);

			
			//$(this).parent().parent().siblings(".explanationWrapper").children(".displayExplanation").html(explanation);
	
			//reveal div
			//$(this).parent().parent().next(".explanationWrapper").children("div.displayExplanation").slideDown(400);
		
		

		//define the correct input for total addition
		var input = $("input:radio");
		
		//reset total
		var total = 0;
		
		//scan all radio buttons for their totals
		$('input:radio:checked').each(function(){ 
			total+=parseInt($(this).val());
			
		});
		
		if($('input:radio:checked').length == $('.assessRiskAnswer').length){
		//alert("all question answered");
			//alert((total/()*100);
			
			var posible_score = $('.assessRiskAnswer').length*100;
			
			quiz_score = (total/posible_score)*100;
			//alert(quiz_score);
			
			$('#scoreTotalWrapper').html("Your result was : "+Math.round(quiz_score)+"%")
			$('#assessRiskResult').slideDown();
			$(".finalAnswer").slideUp();
			
			if (quiz_score >= 80){
				$("#aboveEight").slideDown();
			}else if(quiz_score >= 50){
				$("#midFiftyToEight").slideDown();
			}else if(quiz_score < 50){
				$("#belowFifty").slideDown();
			}

			$("#signupWrapper").slideDown();

			
		}
		
		//print total
		$('#test_score').html("Current score : "+total);
		
    });
    
    
    /* RADIO BUTTON STYLING */
    
    function setupLabel() {
        if ($('.label_check input').length) {
            $('.label_check').each(function(){ 
                $(this).removeClass('c_on');
            });
            $('.label_check input:checked').each(function(){ 
                $(this).parent('label').addClass('c_on');
            });                
        };
        if ($('.styled_radio input').length) {
            $('.styled_radio').each(function(){ 
                $(this).removeClass('r_on');
            });
            $('.styled_radio input:checked').each(function(){ 
                $(this).parent('label').addClass('r_on');
            });
        };
    };
    
    $('body').addClass('has-js');
    $('.label_check, .styled_radio').click(function(){
        setupLabel();
    });
    setupLabel(); 
    
    /* mailchimp signup */
    
   	$('#signup').submit(function() {
		// update user interface
		//alert($('input:radio[name=question1]').parent().val());
		//alert($('input:radio[name=question1]').val());

		
		
		$('#response').html('Building report...');
		$('#response').show();
		// Prepare query string and send AJAX request
		//alert (quiz_score) 
		$.ajax({
			url: 'inc/store-address.php',
			data: 'ajax=true&email=' + escape($('#email').val())+'&question1Answer=' + escape($('input[name=question1]:checked').val())+'&question2Answer=' + escape($('input[name=question2]:checked').val())+'&question3Answer=' + escape($('input[name=question3]:checked').val())+'&question4Answer=' + escape($('input[name=question4]:checked').val())+'&question5Answer=' + escape($('input[name=question5]:checked').val())+'&question6Answer=' + escape($('input[name=question6]:checked').val())+'&question7Answer=' + escape($('input[name=question7]:checked').val())+'&question8Answer=' + escape($('input[name=question8]:checked').val())+'&question9Answer=' + escape($('input[name=question9]:checked').val())+'&question10Answer=' + escape($('input[name=question10]:checked').val())+'&question11Answer=' + escape($('input[name=question11]:checked').val())+'&question12Answer=' + escape($('input[name=question12]:checked').val())+'&finalScore=' + escape(Math.round(quiz_score)),
			success: function(msg) {
				if(msg != "success"){
				$('#response').html(msg);
				//alert(msg);
				}else{
				//alert("SENT");
				$("#signupWrapper").slideUp();
				$("#thanksMessage").slideDown();
				
				}
			}
		});
	
	
	
	
		return false;
	});

    
    
});
