$(document).ready(function(){

	// Fra/til-dato
	$('.ViewHelper_DateTime input[type=checkbox]')
		.each(function(i) {
			if (this.checked) {
				$(this).parent().find('select').removeAttr('disabled');
			}
			else {
				$(this).parent().find('select').attr('disabled', 'disabled');
			}
		})
		.click(function() {
			if (this.checked) {
				$(this).parent().find('select').removeAttr('disabled');
			}
			else {
				$(this).parent().find('select').attr('disabled', 'disabled');
			}
		});

	// Fylke og kommune
	$('.FF_ViewHelper_MunicipalitySelector select.county').change(function() {
		var countySelect = this;
		$.get("index.php", { 
			c_: "FF_MunicipalityAjaxController", 
			m_: "getMunicipalityOptions", 
			countyid: $(this).val() 
		}, function(html) {
			$(countySelect).parent().find('select.municipality').html(html);
		});
	});

	// Søk/filter
	$('.selectTags .selectAll')
		//.each(function() {
		//	var checked = $(this).is(':checked');
		//	$(this).closest('.selectTags').find('input[type=checkbox]').each(function() {
		//		$(this).attr('checked', checked);
		//	});
		//})

		.click(function() {
			var checked = $(this).is(':checked');
			$(this).closest('.selectTags').find('input[type=checkbox]').each(function() {
				$(this).attr('checked', checked);
			});
		});
	
	// Bedriftsavdelinger
	$('.company_add_department').click(function(event) {
		event.preventDefault();
		var html = $('#company_department_blueprint').html();
		var size = $('.company_departments').length;
		html = html.replace(/id=/gi, 'name=' );
		$('.company_departments').append(html);
	});
	$('.company_remove_department').live("click", function(event) {
		event.preventDefault();
		$(this).closest('li').remove();
	});
	
	// Employee endre passord
	$('.employee_change_password').click(function() {
		var checked = $(this).is(':checked');
		$(this).closest('fieldset').find('input[type=password]').each(function() {
			if (checked) {
				$(this).removeAttr('disabled');
			}
			else {
				$(this).attr('disabled', 'disabled');
			}
		});
	});
	
	// Jobbstatus (student) kommentar
	if ($('#workstatus select').val() != 1) {
		$('#workstatuscomment').hide();
	}
	$('#workstatus select').change(function() {
		if ($(this).val() != 1) {
			$('#workstatuscomment').hide();
		}
		else {
			$('#workstatuscomment').show();
		}
	});

	// Maxlength på textarea
	var txts = document.getElementsByTagName('TEXTAREA');
	for(var i = 0, l = txts.length; i < l; i++) {
		if(/^[0-9]+$/.test(txts[i].getAttribute("maxlength"))) {
			txts[i].onkeyup = function() {
				var len = parseInt(this.getAttribute("maxlength"), 10);

				if(this.value.length > len) {
					this.value = this.value.substr(0, len);
					return false;
				}
			}
		}
	}
});
