$(document).ready(function(){

	// Check checkboxes if labels are clicked 
	
	$('label').click(function(){
		//alert("clicked");
		var lFor = $(this).attr("for");
		//alert(lFor);
		
		var cInput = 'input[name="'+lFor+'"]'; 
		//alert(cInput);
		
		
		if($(cInput).attr("checked")){
			//alert('checked');
			$(cInput).attr("checked",false);
		
		} else {
			//alert('un-checked');
			$(cInput).attr("checked",true);
		}
	
	});

	// Make sure a golfer or manager value is checked on the mailing list subscribe form 

	$('#mailing_list').submit(function() {
		var errmsg = "";
	  	if(!$("#golfer_list").attr("checked") && !$("#club_list").attr("checked")){
	  		errmsg += "- Please indicate if you are a golfer or a course manager \n\n";
	  	} 
	  	if($("input[name='email']").val()=="" || $("input[name=email]").val()=="enter your email address"){
	  		errmsg += "- Please enter a valid email address \n";
	  	}
	  	if(errmsg != ""){
	  		alert(errmsg);
	  		return false;
	  	} else {
	  		return true;
	  	}
	});
	
	// Show and hide the reg menu options in the toplinks (top right)
	
	$("body").click(function(e){
		if (!$(e.target).is('#reg-dd') && !$(e.target).is('#reg_link') ) {
			$("#reg-dd").hide();
		}
	});
	
	
	// Show and hide details textareas for different course options
	
	function hideDetails(){
		$(".text_details").each(function(){
			id = $(this).attr('id');
			relId = $(this).attr('id').slice(0,-8);
			//alert(relId);
			//alert(id);
			if($("#"+relId+" .reveal").is(":checked")){
				$("#"+id).show();
				//alert('yes');
			} else {
				$("#"+id).hide();
				//alert('no');
			}
		})
	}
	hideDetails();

	
	$("#course_form .reveal").change(function(){
		if($(this).is(":checked")){
			var newId = $(this).closest('div').attr("id");
			//alert(newId);
			$("#"+newId+"_details").show('slow');
		}
	});
	$("#course_form .hide").change(function(){
		if($(this).is(":checked")){
			var newId = $(this).closest('div').attr("id");
			//alert(newId);
			$("#"+newId+"_details").hide('slow');
		}
	});


	$('#keyword_search').submit(function(){
		var q = $('#q').val();
		if (q != '') {
			window.location = window.location.protocol+'//'+window.location.host+'/courses/results/q:'+q;
		}
		return false;
	})
	

});



