// dom ready
$(function(){

	var username,
		password,
		// redirect to this url after succesful login
		redirectTo = "/extra/Kumppaniverkko";

	$("form#secondary-extranet-login").submit(function(e){
		// prevent form submit
		e.preventDefault();
		
		// remove failed text
		$("#failed").remove();
		
		// get username and password
		username = $("input#username").val();
		password = $("input#password").val();
		
		$.ajax({
			type: "GET",
			// send username and password
			url: "/wps/wcm/login?username=" + username + "&password=" + password,
				// username sent
				complete: function(){
					// if ltpatoken cookie received continue to kumppaniverkko
					($.cookie('LtpaToken')) ? window.location.href=redirectTo : loginFailed();
				}
		});	
	});
});

function loginFailed() {
	$("#success").after("<p id='failed'>K&auml;ytt&auml;j&auml;tunnus v&auml;&auml;rin.</p>");
}
