var original_span = '';
var original_span_default = '';
var selectedtab = '';

jQuery(function($) {
				
  selectedtab = location.hash;
				
  $('a[rel*=facebox]').facebox();
  
  
  $("#page-content ul").idTabs();
  
  original_span_default = jQuery("#learn-more span").html();
  
  if(selectedtab != ""){
	  jQuery("li a[href=" + selectedtab + "]").click();
	  
	  $(".tabs ul li a").each(function (i) {
		if($(this).hasClass("selected")){
			//i is the selected one
			current_step = i;
			next_step = current_step + 1;
		}
	  });
	  
	  total_steps = $(".tabs ul li a").length - 1;
	  
	  
	  if($(".tabs ul li a").eq(next_step).attr('href').indexOf("#sleep") == 0){
		  if($(".tabs ul li a").eq(next_step).attr('href').indexOf("#sleep-test") == 0){
		  	$("#learn-more span").html("Please Send Me The Results! " + original_span_default.slice(original_span_default.indexOf("<")));
		  }else if($(".tabs ul li a").eq(next_step).attr('href').indexOf("#sleep-wait") == 0){
			$("#learn-more span").html("<strong>Are You At Risk?</strong> Take a Test Now." + original_span_default.slice(original_span_default.indexOf("<")));
		  }else{
			$("#learn-more span").html(original_span_default);
		  }
	  }
	  
	  
  }
  
  $('a.button').click(function() {
	$(this).blur();			   
  });
  
  
  
  $("#learn-more a").click(function () { 
	  $(".tabs ul li a").each(function (i) {
		if($(this).hasClass("selected")){
			//i is the selected one
			current_step = i;
			next_step = current_step + 1;
		}
	  });
	  
	  total_steps = $(".tabs ul li a").length - 1;
	  
	  
	  if(pageTracker!=undefined){
	  	pageTracker._trackPageview(location.pathname+$(".tabs ul li a").eq(next_step).attr('href'));
	  }
	  
	  //###############################
	  // custom for sleep apnea
	  //###############################
	  var test_submitted = true;
	  if($(".tabs ul li a").eq(next_step).attr('href').indexOf("#sleep") == 0){
		  if($(".tabs ul li a").eq(next_step).attr('href').indexOf("#sleep-test") == 0){
		  	$("#learn-more span").html("Please Send Me The Results! " + original_span_default.slice(original_span_default.indexOf("<")));
		  }else if($(".tabs ul li a").eq(next_step).attr('href').indexOf("#sleep-wait") == 0){
			$("#learn-more span").html("<strong>Are You At Risk?</strong> Take a Test Now." + original_span_default.slice(original_span_default.indexOf("<")));
		  }else{
			$("#learn-more span").html(original_span_default);
		  }
		  
		  if($(".tabs ul li a").eq(next_step).attr('href').indexOf("#sleep-test-2") == 0){
			  test_submitted = checkWholeForm9385( $("#sleep-test").get(0) );
		  }else if($(".tabs ul li a").eq(next_step).attr('href').indexOf("#sleep-consult") == 0){
			  test_submitted = checkWholeForm94416( $("#sleep-form").get(0) );
			  if(test_submitted == true){
				  $("#learn-more").hide();
			  }
		  }
	  }
	  if(test_submitted == false){
		return false;
	  }
	  //###############################
	  
	  //if going to last one
	  if(total_steps == next_step && $(".tabs ul li a").eq(next_step).attr('href') == "#report"){
		//hide consultation btn
		//$("#book-consultation").hide();
		//change learn more button to submit form
		if(original_span == ''){
			//original_span = $("#learn-more span").html();
			//$("#learn-more span").html('Subscribe');
		}
		$("#learn-more").hide();
		//move back button
		//$("#back").addClass("report");
	  }else if(total_steps == next_step){
      	$("#learn-more").hide();
        //$("#back").addClass("lastpane");
      }else if(next_step > total_steps){
		$('form').submit();	
		$('form div.mc-field-group').css('visibility','hidden');
		$("#learn-more").hide();
	  }
      
      if(current_step == 0){
	  	$("#back").show();
	  }
	  
	  $(".tabs ul li a:eq("+next_step+")").click();
	  
      /* TODO: does this work? */
	  //$("#page-content").pngFix();
      
	  return false; 
    });
	
	$("#back a").click(function () { 
		$(".tabs ul li a").each(function (i) {
		if($(this).hasClass("selected")){
			//i == to selected
			current_step = i;
			next_step = parseInt(current_step) - 1;
		}
	  });
	  
	  total_steps = $(".tabs ul li a").length - 1;
	  
	  
	  //###############################
	  // custom for sleep apnea
	  //###############################
	  if($(".tabs ul li a").eq(next_step).attr('href').indexOf("#sleep") == 0){
		  if($(".tabs ul li a").eq(next_step).attr('href').indexOf("#sleep-test") == 0){
		  	$("#learn-more span").html("Please Send Me The Results! " + original_span_default.slice(original_span_default.indexOf("<")));
		  }else if($(".tabs ul li a").eq(next_step).attr('href').indexOf("#sleep-wait") == 0){
			$("#learn-more span").html("<strong>Are You At Risk?</strong> Take a Test Now. " + original_span_default.slice(original_span_default.indexOf("<")));
		  }else{
			$("#learn-more span").html(original_span_default);
		  }
	  }
	  //###############################
	  
	  //if on the last one
	  if(total_steps == next_step){
		//show consultation btn
		//$("#book-consultation").show();
		//show learn-more btn
		$("#learn-more span").html(original_span);
		original_span = '';
		//move back button
        if($("#back").hasClass("report")){
			$("#back").removeClass("report");
        }else if($("#back").hasClass("lastpane")){
        	$("#back").removeClass("lastpane");
        }
	  }else if(next_step < total_steps){
		  if(original_span != ''){
		  	$("#learn-more span").html(original_span);
		  	original_span = '';
		  }
		  $("#learn-more").show();
	  }
      if(current_step == 1){
	  	$("#back").hide();
	  }
	  
	  $(".tabs ul li a:eq("+next_step+")").click();
      
      
      
	  return false; 
    });
  
  });


var submitcount9385 = 0;

function checkWholeForm9385(theForm){
	var why = "";
	if (theForm.CAT_Custom_168194) why += checkSelected(theForm.CAT_Custom_168194, "Sitting and reading.");
	if (theForm.CAT_Custom_168195) why += checkSelected(theForm.CAT_Custom_168195, "Watching TV.");
	if (theForm.CAT_Custom_168196) why += checkSelected(theForm.CAT_Custom_168196, "Sitting inactive in a public place. (e.g. a theater or meeting)");
	if (theForm.CAT_Custom_168197) why += checkSelected(theForm.CAT_Custom_168197, "As a passenger in a car for an hour without a break.");
	if (theForm.CAT_Custom_168198) why += checkSelected(theForm.CAT_Custom_168198, "Lying down to rest in the afternoon when circumstances permit.");
	if (theForm.CAT_Custom_168199) why += checkSelected(theForm.CAT_Custom_168199, "Sitting and talking to someone.");
	if (theForm.CAT_Custom_168200) why += checkSelected(theForm.CAT_Custom_168200, "Sitting quietly after a lunch without alcohol.");
	if (theForm.CAT_Custom_168201) why += checkSelected(theForm.CAT_Custom_168201, "In a car, while stopped for a few minutes in traffic.");
	if(why != ""){
		alert(why);
		return false;
	}else{
		test_result = 0;
		jQuery("#sleep-test input[type=radio]:checked").each(function(){
			test_result = parseInt(test_result) + parseInt(jQuery(this).val());
		});
		jQuery("#CAT_Custom_168539").html("Your score was: " + String(test_result) + ".");
		return true; 
	}
}


var submitcount94416 = 0;function checkWholeForm94416(theForm){var why = "";if (theForm.FirstName) why += isEmpty(theForm.FirstName.value, "First Name");if (theForm.LastName) why += isEmpty(theForm.LastName.value, "Last Name"); if (theForm.EmailAddress) why += checkEmail(theForm.EmailAddress.value); if (theForm.HomePhone) why += isEmpty(theForm.HomePhone.value, "Home Phone Number");if(why != ""){alert(why);return false;}if(submitcount94416 == 0){submitcount94416++;jqsub();return true;}else{alert("We've already sent you your Sleep Apnea test results. Refresh the page to take the test again.");return true;}}


function jqsub() {

var $form = $('#sleep-form'); // set your form ID
var $messagebox = $('#message'); // set your message box ID
var $successmessage = "Thank You! The results of your test have been emailed to you."; // replace with your own success message in HTML format
var $errormessage = "Error - Please Try Again"; // replace with your own error message in HTML format

$.ajax({
	type: 'POST',
	url: $form.attr('action'),
	data: $form.serialize(), 
	success: function (msg) { },
	error: function (msg) { alert($errormessage); }
     });
}