function ToggleCountries(list, action, stays) {
		$(list + ' li').each(function(){
			if(action == 'hide') {
				if($(this).attr('rel') != stays) {
					$(this).hide();
				}
			}else{
				if($(this).attr('rel') != stays) {
					$(this).show();
				}
			}
		});
	}
$(document).ready(function() {
	$("#tabs").tabs();
	$.datepicker.setDefaults($.extend({showOn: 'button', buttonImage: 'images/icoCalendar.gif', buttonImageOnly: true}, $.datepicker.regional['pl']));
	$("input.date").datepicker();
	$('#erase-form').click(function() {
		$('input[name=cpa]').val('');
		$('input[name=cpb]').val('');
		return false;
	});
	$(".autoSubmit").change(function() {
		$(this).parents("form").get(0).submit();
	});
		
	$('#facb').change(function(){
		if($('#facb:checked').val()!=undefined) {
			ToggleCountries('#cpb_prefix_list', 'hide', '48');
			ToggleCountries('#cpa_prefix_list', 'show', '48');
			$('#cpb_prefix_list li[rel=48]').trigger('click');
			$('#cpb_prefix_container').trigger('click');
		} else {
			ToggleCountries('#cpa_prefix_list', 'hide', '48');
			ToggleCountries('#cpb_prefix_list', 'show', '48');
			$('#cpa_prefix_list li[rel=48]').trigger('click');
			$('#cpa_prefix_container').trigger('click');
		}
	});
	var password = $("form input[name=password]").val()
	if(password) passwordStrength.run(password);
	
	$("input[type=checkbox].trigger").each(function() {
		if(!this.checked) $("#" + this.value).hide();
		$(this).click(function() {
			this.blur();
			if(this.checked) $("#" + this.value).show();
			else $("#" + this.value).hide();
		});
	});
	
	$("input[type=radio].filterTrigger").each(function() {
		if(!this.checked) $("#" + this.value).hide();
		$(this).click(function() {
			this.blur();
			$(".filterBox").slideUp('fast');
			if(this.checked)
					$("#" + this.value).slideDown('fast');
		});
	});
	
	$("input[type=text].hideDefaultValue").each(function() {
		
		this.defaultValue = this.value;
		$(this).focus(function() {
			if(this.value == this.defaultValue) 
				this.value = '';
		});
		
		$(this).blur(function() {
			if(this.value == '') 
				this.value = this.defaultValue;
		});
	});
	
	$("a.deleteConfirmation").click(function() {
		if (confirm("Na pewno usunąć?"))
			return true;
		return false;
	});
});

var passwordStrength = {
	run: function(value) {
		var points = 0;
	
		if(value.match(/[A-Z]/)) points++;
		if(value.match(/[a-z]/)) points++;
		if(value.match(/[0-9]/)) points++;
		if(value.match(/[A-Z]{3,}/)) points++;
		if(value.match(/[0-9]{2,}/)) points++;
		if(value.match(/[^A-Za-z0-9]/)) points++;
		if(value.match(/[^A-Za-z0-9]{2,}/)) points++;
		if(value.match(/[^A-Za-z0-9]{3,}/)) points++;
		
		if(value.length > 5) points++;
		if(value.length > 8) points++;
		if(value.length > 12) points++;
		
		
		if(points > 6) points = 6;
		this.set(points);
	},
	
	set: function(points) {
		if(points == this.previousValue) return 0;
		this.previousValue = points;
		
		var current = 0;
		$(".passwordStrength li").each(function() {
			if(current < points) {
				$(this).css("background", passwordStrength.colors[points]);
			} else {
				$(this).css("background", passwordStrength.colors[0]);
			}
			current++;
		})
	}
}

passwordStrength.previousValue = 0;
passwordStrength.defaultColor = '';
passwordStrength.colors = {0: '#E8E6DD', 1: '#D22400', 2: '#D22400', 3: '#FF8533', 4: '#FF8533', 5: '#25AE03', 6: '#25AE03'}

function setSponsor(id) {
	$("#sponsorValue").attr("value", id);
}
