﻿$(document).ready(function () {

    /*******************************************************************
		Global Variables 
    *******************************************************************/

    // initializes the URL variable used to create the link out URL address later
    var URL = "";

    // initializes the ID for the second set of questions
    var questionSelection = "";

    // this is a boolean to check whether one of the second question containers are open or not
    var openState = 0;

    var parentDivID = "";

    /*******************************************************************
		Cymbalta Customized Resource Question Process Script
    *******************************************************************/

    // Hide questions and re-set all check/radio inputs to UNCHECKED
    $(".questionsContainer").hide();
    $('input').attr('checked', false);

    // event triggers when one of the questionOne Radio Buttons is clicked
    $("#questionOne input[type=radio]").click(function () {

        if (($(this).is(":checked")) && ($(this).parents("div.questionParentContainer:first").attr("id") != parentDivID)) {
           
            $('input[type="radio"]').attr('checked', false);
            $(".questionsContainer").hide();
            $(this).attr("checked", true);
        }

        parentDivID = $(this).parents("div.questionParentContainer:first").attr("id");
        //alert(parentDivID);	

        // checks to whether the event that is captured is when a Radio button is selected and not being turned off

        if ($(this).is(":checked") == true) {
            // gets the ID of the radio button that is clicked

            var radioSelectionID = $(this).attr("id");

            //alert("radioSelectionID=" + radioSelectionID);
            // appended the string "Select" at the end of the clicked Radio button ID so we can select the appropriate
            // second question container to be shown

            questionSelection = radioSelectionID + "Select";

            //alert("questionSelection=" + questionSelection);

            // checks the whether the ID exists or not. This is for the case where the radio button directly
            // links out to URL
            if ($("#" + questionSelection).length) {
                // sets all checkboxs to UNCHECKED
                $('input[type="checkbox"]').attr('checked', false);

                // this checks whether or not a second question container was previously open or not
                if (openState == 1) {
                    // if a previous second question container is open then this closes it
                    $("#" + parentDivID + " .questionsContainer").stop(true, true).hide("fast");
                    $("#" + questionSelection).slideDown();
                }

                else {
                    // if no other second question containers were previously opened, then open the selected second question
                    // container
                    $("#" + questionSelection).slideDown();
                }

                // sets the openState of the session to 1 - meaning that there is currently a second question container open
                openState = 1;
            }

            // if the ID does not exist of the second question container that is opened from selecting a First Question Radio button
            // then go to the Radio Button Specific URL
            else {
                // hides all of the second question containers that are visible
                $("#" + parentDivID + " .questionsContainer").stop(true, true).hide();

                // returns the URL relative to what Radio Button ID (radioSelectionID) you selected
                URL = getURL(radioSelectionID);
            }
        }
    });

    $(".questionItemNo").click(function () {
        $(".questionItemYes").attr("checked", false);
    });

    $(".questionItemYes").click(function () {
        $(".questionItemNo").attr("checked", false);
    });

    // event that is triggered if the button is clicked inside of the second question container
    $(".questionTwoNext").click(function () {
        // checks whether or not the Yes or No checkbox were selected and retrieves the relative URL for
        // the selection

        /*$("#"+questionSelection+" .questionItemYes").attr("checked", true ).each(function()
        {
			
        //alert("questionSelection=" + questionSelection);
        var questionType = questionSelection+"Yes";
        URL = getURL(questionType);
        document.location.href = URL;
        //alert(URL);
			
        });
		
        if($("#"+questionSelection+" .questionItemNo").attr("checked") == true)
        {
			
        var questionType = questionSelection+"No";
        URL = getURL(questionType);
        document.location.href = URL;
        $(".questionItemYes").attr("checked", false);
        //alert(URL);
			
        }	*/

        $("#" + questionSelection + " .questionItemYes").each(function () {
            if ($(this).is(":checked") == true) {
                //alert("questionSelection=" + questionSelection);
                var questionType = questionSelection + "Yes";
                URL = getURL(questionType);
                document.location.href = URL;
                //alert(URL);
            }
        });

        if ($("#" + questionSelection + " .questionItemNo").is(":checked") == true) {
            var questionType = questionSelection + "No";
            URL = getURL(questionType);
            document.location.href = URL;
            $(".questionItemYes").attr("checked", false);
            //alert(URL);
        }
    });

    // this function determines what URL to return based on what ID is passed in
    function getURL(questionType) {
        switch (questionType) {
            case "depressionTreatmentSelectNo":
                URL = "http://" + location.hostname + "/Pages/firststepsnrxdepression.aspx";
                break;
            case "depressionTreatmentSelectYes":
            	URL = "http://" + location.hostname + "/Pages/firststepsrxdepression.aspx";
                break;
            case "depressionSymptomsSelectNo":
            	URL = "http://" + location.hostname + "/Pages/firststepsnsydepression.aspx";
                break;
            case "depressionSymptomsSelectYes":
            	URL = "http://" + location.hostname + "/Pages/firststepssydepression.aspx";
                break;
            case "diabeticTreatmentSelectNo":
            	URL = "http://" + location.hostname + "/Pages/firststepsnrxdiabeticnervepain.aspx";
                break;
            case "diabeticTreatmentSelectYes":
            	URL = "http://" + location.hostname + "/Pages/firststepsrxdiabeticnervepain.aspx";
                break;
            case "diabeticSymptomsSelectNo":
            	URL = "http://" + location.hostname + "/Pages/firststepsnsydiabeticnervepain.aspx";
                break;
            case "diabeticSymptomsSelectYes":
            	URL = "http://" + location.hostname + "/Pages/firststepssydiabeticnervepain.aspx";
                break;
            case "gadTreatmentSelectNo":
            	URL = "http://" + location.hostname + "/Pages/firststepsnrxgeneralanxiety.aspx";
                break;
            case "gadTreatmentSelectYes":
            	URL = "http://" + location.hostname + "/Pages/firststepsrxgeneralanxiety.aspx";
                break;
            case "gadSymptomsSelectNo":
            	URL = "http://" + location.hostname + "/Pages/firststepsnsygeneralanxiety.aspx";
                break;
            case "gadSymptomsSelectYes":
            	URL = "http://" + location.hostname + "/Pages/firststepssygeneralanxiety.aspx";
                break;
            default:
                URL = "";
        }
        return URL;
    }
});
