$(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();
	 $('form input').attr('checked',false);
	 
	 // event triggers when one of the questionOne Radio Buttons is clicked
	 $("#questionOne input[type=radio]").click(function()
	 {
		 if(($(this).attr("checked") == true) && ($(this).parents("div.questionParentContainer:first").attr("id") != parentDivID))
		 {
			$('form 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).attr("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
				 $('form 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);
			
		}		
		

		
	 });


	 
	 // 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 + "/depression/firststepsnrx.jsp";
					break;
				case  "depressionTreatmentSelectYes":
					URL = "http://" + location.hostname + "/depression/firststepsrx.jsp";
					break;
				case  "depressionSymptomsSelectNo":
					URL = "http://" + location.hostname + "/depression/firststepsnsy.jsp";
					break;
				case  "depressionSymptomsSelectYes":
					URL = "http://" + location.hostname + "/depression/firststepssy.jsp";
					break;
				case  "diabeticTreatmentSelectNo":
					URL = "http://" + location.hostname + "/diabeticnervepain/firststepsnrx.jsp"; 
					break;
				case  "diabeticTreatmentSelectYes":
					URL = "http://" + location.hostname + "/diabeticnervepain/firststepsrx.jsp"; 
					break;
				case  "diabeticSymptomsSelectNo":
					URL = "http://" + location.hostname + "/diabeticnervepain/firststepsnsy.jsp"; 
					break;
				case  "diabeticSymptomsSelectYes":
					URL = "http://" + location.hostname + "/diabeticnervepain/firststepssy.jsp"; 
					break;
				case  "gadTreatmentSelectNo":
					URL = "http://" + location.hostname + "/generalanxiety/firststepsnrx.jsp"; 
					break;
				case  "gadTreatmentSelectYes":
					URL = "http://" + location.hostname + "/generalanxiety/firststepsrx.jsp"; 
					break;
				case  "gadSymptomsSelectNo":
					URL = "http://" + location.hostname + "/generalanxiety/firststepsnsy.jsp"; 
					break;
				case  "gadSymptomsSelectYes":
					URL = "http://" + location.hostname + "/generalanxiety/firststepssy.jsp"; 
					break;
				default :
					URL = "";
		  }
		  
		  return URL;
	 }
});