$(document).ready(function(){

var this_page=1;
var percent_complete=0;
var radios_complete=0;

// Fix png on the percent bars for IE6...
$("div.progress_bar").pngFix();

// Make Email/Confirm Email fields match each other
$("input.email_address").change(function(){
	$("input.email_address").val( $(this).val() );
});


// Navigation
$("div.navigation div.nav").hover(function(){
	$(this).css("cursor","pointer");
},function(){});

$("div.navigation div.nav").click(function(){
	fieldset = $(this).parents("fieldset:first");
	
	if( $(this).hasClass("disabled") ){
		if( $("fieldset#subscribe_fset").length == 1 ){
			add_error(fieldset,"Please complete all of the required fields<br />");
		}else if( this_page == 2 ){
			add_error(fieldset,"Please complete all of the required fields<br />");
		}else if( this_page >= 3 && this_page <= 7 ){
			add_error(fieldset,"Please answer all of the questions on this page<br />");
		}else if( this_page == 8 ){
			add_error(fieldset,"Please enter your email address<br />");
		}
		
		return false;
	}

	if( $("fieldset#subscribe_fset").length == 1 ){
		$("form#subscribe").submit();
		return false;
	}
	
	if( $(this).hasClass("right") ){
		
		if( this_page == 2 || this_page == 8 ){
			email_address = fieldset.find("input.email_address").val();
			
			if( !email_address.match( /.+\@.+\..+/g ) ){
				add_error(fieldset,"Please enter a valid email address<br />");
				alert(1);
				return false;
			}
		}
		
		if( this_page == 8 ){
			$(this).addClass("disabled");
			$(this).find("a").html("Please wait...");
			$("form#questionnaire").submit();
			return false;
		}
		
		$("fieldset#page"+this_page).hide();
		this_page = this_page + 1;
		$("fieldset#page"+this_page).show();
		
		remove_error( fieldset );
		
	}else if( $(this).hasClass("left") ){
		
		$("fieldset#page"+this_page).hide();
		this_page = this_page - 1;
		$("fieldset#page"+this_page).show();
		
	}

});



// Radio buttons
$("td.option a").hover(function(){
	$(this).children("img").attr("src",URLstatic+"/img/radio_hover.png");
},function(){
	if( $(this).parents("tr:first").find("td.question input.option_selected").val() && $(this).hasClass( $(this).parents("tr:first").find("td.question input.option_selected").val() ) ){
		$(this).children("img").attr("src",URLstatic+"/img/radio_on.png");
	}else{
		$(this).children("img").attr("src",URLstatic+"/img/radio.png");
	}
});

$("td.option a").mousedown(function(){
	$(this).children("img").attr("src",URLstatic+"/img/radio_on.png");
	return false;
});

$("td.option a").click(function(){
	if( $(this).parents("tr:first").find("td.question input.option_selected").val() ){
		$(this).parents("tr:first").find("a."+$(this).parents("tr:first").find("td.question input.option_selected").val()).children("img").attr("src",URLstatic+"/img/radio.png");
	}else{
		radios_complete = radios_complete + 1;
		percent_complete = percent_complete + 4;
		$("div.progress_bar_bar").css("width",percent_complete+"%");
		$("div.progress_bar_percent").html(percent_complete+"%");
		
		if( percent_complete <= 8 ){
			1;
		}else if( percent_complete == 100 ){
			1;
		}else{
			$("div.progress_bar_percent").css("padding-left",(6+(3.44*percent_complete)-38)+"px");
		}
	}
	$(this).children("img").attr("src",URLstatic+"/img/radio_on.png");
	$(this).parents("tr:first").find("td.question input.option_selected").val( $(this).attr("class") );
	
	// Page 3-7 Verification
	var completed = true;
	
	$(this).parents("table:first").find("td.question input").each(function(){
		if( $(this).val() == "" ){
			completed = false;
		}
	});
	
	if( completed == true && $(this).parents("fieldset:first").find("div.right").hasClass("disabled") ){
		$(this).parents("fieldset:first").find("div.right").removeClass("disabled");
		remove_error( $(this).parents("fieldset:first") );
	}
	
	return false;
});


// Subscribe Verification
$("fieldset#subscribe_fset input").keyup(function(){
	
	var completed = true;
	
	$("fieldset#subscribe_fset input").each(function(){
		if( $(this).val() == "" ){
			completed = false;
		}
	});
	
	if( completed == true ){
		if( $("fieldset#subscribe_fset div.right").hasClass("disabled") ){
			$("fieldset#subscribe_fset div.right").removeClass("disabled");
			remove_error( $(this).parents("fieldset#page2") );
		}
	}else{
		if( !$("fieldset#subscribe_fset div.right").hasClass("disabled") ){
			$("fieldset#subscribe_fset div.right").addClass("disabled");
		}
	}
	
});


// Page 2 Verification
$("fieldset#page2 input").keyup(function(){
	
	var completed = true;
	
	$("fieldset#page2 input").each(function(){
		if( $(this).val() == "" ){
			completed = false;
		}
	});
	
	if( completed == true ){
		if( $("fieldset#page2 div.right").hasClass("disabled") ){
			$("fieldset#page2 div.right").removeClass("disabled");
			remove_error( $(this).parents("fieldset#page2") );
		}
	}else{
		if( !$("fieldset#page2 div.right").hasClass("disabled") ){
			$("fieldset#page2 div.right").addClass("disabled");
		}
	}
	
});

// Page 8 Verification
$("fieldset#page8 input.text").keyup(function(){
	
	var completed = true;
	
	$("fieldset#page8 input.text").each(function(){
		if( $(this).val() == "" ){
			completed = false;
		}
	});
	
	if( completed == true ){
		if( $("fieldset#page8 div.right").hasClass("disabled") ){
			$("fieldset#page8 div.right").removeClass("disabled");
			remove_error( $(this).parents("fieldset#page8") );
		}
	}else{
		if( !$("fieldset#page8 div.right").hasClass("disabled") ){
			$("fieldset#page8 div.right").addClass("disabled");
		}
	}
	
});



// Add/remove errors
function add_error( fieldset, error ){
	remove_error(fieldset);
	fieldset.find("div.navigation").prepend('<div class="error">'+error+'</div>');
}

function remove_error( fieldset ){
	fieldset.find("div.error").remove();
}



});

