  function toggle_email()
  {
    if ($('input#black_list_all:checked').length == 1)
    {
	    $("#email_options").slideUp('fast'); 
	}
	else
	{
	    $("#email_options").slideDown('fast'); 
	}
	return;
  }
   

 
  function toggle_user_details()
  {
    if ($('input#use_default:checked').length == 1)
    {
	    $(".user_detail").attr("disabled","disabled"); 
	}
	else
	{
		$(".user_detail").removeAttr("disabled"); 
	}
	return;
  }
  
  
function showregion()
{
var country = $('#country').val();
		if (country == 'Canada')
		{
			 $('.USA .user_detail').attr("disabled","disabled");
			 $('.USA').hide();
			 $('.Canada .user_detail').removeAttr("disabled");
			 $('.Canada').show();
			 $('.prov_select').focus();
		}
		else if (country == 'USA')
		{
			  $('.Canada').hide();
   		     $('.Canada .user_detail').attr("disabled","disabled");
			 $('.USA').show();
			 $('.USA .user_detail').removeAttr("disabled");
 			 $('.state_select').focus();
		}
		else
		{
		  $('.USA .user_detail').attr("disabled","disabled");
		  $('.USA').hide();
		  $('.Canada .user_detail').attr("disabled","disabled");
		  $('.Canada').hide();	
		}
		
}

/* Directory functions */

  function toggle_directory_details()
  {
    if ($('input#use_default:checked').length == 1)
    {
	    $("fieldset#location1 .user_detail").attr("disabled","disabled"); 
	    //$("#company_name").attr("disabled","disabled");
	}
	else
	{
		$("fieldset#location1 tr:visible .user_detail").removeAttr("disabled");
		//$("#company_name").removeAttr("disabled");
	}
	return;
  }
  




var visible_loc = 1;

function showregion_dir()
{
	var country = $('#location'+visible_loc+' .country').val();
		if (country == 'Canada')
		{
			 $('#location'+visible_loc+' .USA .user_detail').attr("disabled","disabled");
			 $('#location'+visible_loc+' .USA').hide();
			 $('#location'+visible_loc+' .Canada .user_detail').removeAttr("disabled");
			 $('#location'+visible_loc+' .Canada').show();
			 $('#location'+visible_loc+' .prov_select').focus();
		}
		else if (country == 'USA')
		{
			  $('#location'+visible_loc+' .Canada').hide();
   		     $('#location'+visible_loc+' .Canada .user_detail').attr("disabled","disabled");
			 $('#location'+visible_loc+' .USA').show();
			 $('#location'+visible_loc+' .USA .user_detail').removeAttr("disabled");
 			 $('#location'+visible_loc+' .state_select').focus();
		}
		else
		{
		  $('#location'+visible_loc+' .USA .user_detail').attr("disabled","disabled");
		  $('#location'+visible_loc+' .USA').hide();
		  $('#location'+visible_loc+' .Canada .user_detail').attr("disabled","disabled");
		  $('#location'+visible_loc+' .Canada').hide();	
		}
		
}

function show_location(id)
{
	$('.location').hide()
	$('#location'+id).fadeIn("fast");
	visible_loc = id;
	showregion_dir();
}



function add_location()
{
	var numlocs = $('#number_of_locations').val();
	numlocs++;
	$('#number_of_locations').val(numlocs);
	var newloc = $("#location1").clone(true)
	.attr("id","location"+numlocs);

	newloc.insertBefore('#first_buttons');
	$("#location"+numlocs+" legend").text("Location "+numlocs+" information");
	$("#location"+numlocs).before('<input class="loc'+numlocs+'" type="button" onclick="show_location('+numlocs+')" value="Show Location '+numlocs+'"/>'
		+'<input  class="loc'+numlocs+'" type="hidden" name="entity_id[]" value="NEW"/>'
		+'<input  class="loc'+numlocs+'" type="hidden" name="parent_id[]" value="'+$('#primary_id').val()+'"/>'
		);
	$('#location'+numlocs+' .use_primary_row').replaceWith("<tr><td colspan=\"2\" align=\"center\"><button onclick=\"remove_location("+numlocs+")\" >Remove Location "+numlocs+"</button></td></tr>");
	$('#location'+numlocs+' .display_select')[0].selectedIndex = 0;
	$('#location'+numlocs+' .country')[0].selectedIndex = $('#location1 .country')[0].selectedIndex;
	$('#location'+numlocs+' .prov_select')[0].selectedIndex = $('#location1 .prov_select')[0].selectedIndex;
	$('#location'+numlocs+' .worldwide1')[0].selectedIndex = $('#location1 .worldwide1')[0].selectedIndex;
	$('#location'+numlocs+' .worldwide2')[0].selectedIndex = $('#location1 .worldwide2')[0].selectedIndex;
    /*
    $('#location'+numlocs+' .branch_class').map(function ()
    {
    	var name = ($(this).attr('name'));
    	name = name.replace("[0]","["+(numlocs-1)+"]");
    	$(this).attr('name',name);
    });
    */
    $("fieldset#location"+numlocs+" .user_detail").removeAttr("disabled"); 
	show_location(numlocs);
}


function remove_location(id)
{
	$('#location'+id).remove();
	$('.loc'+id).remove();
	//clean up later locations
	var numlocs = $('#number_of_locations').val();
	numlocs--;
	$('#number_of_locations').val(numlocs);
	//not go through each location from here on out and rewrite the indexes
	 $('.location').map(function ()
	 {
		var loc_id = $(this).attr('id').match(/\d+/);
		loc_id = loc_id[0]
		if (loc_id > id)
		{
			new_loc = loc_id-1;
			$(this).find(".branch_class").map(function ()
		    {
		    	var name = ($(this).attr('name'));
		    	name = name.replace("["+new_loc+"]","["+(new_loc-1)+"]");
		    	$(this).attr('name',name);
		    });
		
		}
	 });
	
}



function check_cats()
{
	var max_cats = $('#max_categories').val();
	var current_cats =  $('input.cat_list_item').get();
	//[].checked;
	var num_checked = 0;
	for (var i = 0; i < current_cats.length; i++) 
	{
        if (current_cats[i].checked)
        {
        	num_checked++;
        }
    }
    if (num_checked > max_cats)
    {
    	if(confirm('You can only be in '+max_cats+' categories with this market , you currently have '+num_checked+' selected. Would you like me to remove some from the bottom of the list??'))
    	{
	 	  	for (var i = current_cats.length; i > 0; i--) 
			{
		        if ((num_checked > max_cats) && (current_cats[i-1].checked))
		        {
			        current_cats[i-1].checked = false;
			        num_checked--;
			    }
		    }
    	}
    }
    
}


function confirm_unlist()
{
	return confirm("Are you sure you want to delete your directory listing? Doing so will remove you from all of our directories and you will require approval to re list.");
}