if (!loginIsCookieEnabled()) {
	document.write("<div class='warning'><strong>Deze applicatie vereist cookies.</strong><br>Uw browser ondersteunt helaas geen cookies. Neem contact op met uw systeembeheerder.</div>");
}

window.onload = function() {
	var logging = document.forms["loginRedirectForm"];
	if (logging)
	{
		logging.submit();
		return;
	}

	var lForm = document.getElementById("loginForm");
	if (lForm.password_expired.value == "1") {
		lForm.new_password1.focus();
		lForm.onsubmit = function(e){
			if (!e) var e = window.event;

			if (!loginIsValidPassword(
					lForm.new_password1.value,
					lForm.gebruikersnaam.value,
					lForm.bedrijfsnaam.value))
			{
				e.preventDefault();
				alert('The password is too simple. Please add digits and special characters.');
				lForm.new_password1.focus();
				return false;
			}

			if (lForm.new_password1.value != lForm.new_password2.value)
			{
				e.preventDefault();
				alert('Passwords do not match.');
				lForm.new_password2.focus();
				return false;
			}
		}
		if (lForm.captureEvents) lForm.captureEvents(Event.SUBMIT);
	} else if (lForm.security_token_required.value == "1") {
		lForm.security_token_value.focus();
	} else if (lForm.bedrijfsnaam.value == "") {
		lForm.bedrijfsnaam.focus();
	} else if (lForm.gebruikersnaam.value == "") {
		lForm.gebruikersnaam.focus();
	} else {
		lForm.wachtwoord.focus();
	}
}

function loginIsValidPassword(password, user_name, bedrijfsnaam) {
	var result = true;
	if (
		   (password.indexOf(user_name) > -1) // username is (part of) the password
		|| (password.indexOf(bedrijfsnaam) > -1) // bedrijfsnaam is (part of) the password
		|| (!password.match(/\W/) && !password.match(/\d/)) // no digits or other special characters included
		|| password.length < 6 // too short
	) {
		result = false;
	}
	return result;
}

function loginIsCookieEnabled() {
	if (typeof document.cookie != "string") {
		return false;
	}
	
	document.cookie = "cookies=1";
	document.cookie = "";
	if (document.cookie.indexOf("cookies") == -1) {
		return false;
	}
	
	return true;
}

