//* Copyrights (c) 2011 - Atilla Software Engineering BV, the Netherlands

function loginSetup() {

	//* first, check if we're logged in. If not, do so
	if( $.data( document.body, "userValidated" ) != 'true' ) {
		$("#login").show();
	}
	
	//* set the username and password if stored
	if( $.cookie( "username" ) ) $("#username").val( $.cookie( "username" ) );
	if( $.cookie( "password" ) ) $("#password").val( $.cookie( "password" ) );
	if( !$.cookie( "revisit" ) && !$.cookie( "username" ) && !$.cookie( "password" ) ) {
		$("#login").find("#createnew").show();
		$("#welcome").show();
	}
	
	$("#newaccountform").submit( function() {
		$email = $("#newaccountform").find("input[id=email]").val();
		$name = $("#newaccountform").find("input[id=name]").val();
		$pass = $("#newaccountform").find("input[id=password]").val();
		$confirm = $("#newaccountform").find("input[id=confirm]").val();
		
		$.cookie( "username", $email );
		$.cookie( "password", $pass );
		
		if( $pass != $confirm ) {
			alert( "Couldn't confirm password, both entries differ. Please check the password and resubmit (" + $confirm + "-" + $pass + ")" );
			return false;
		}
		
		$.post( $engine, {
			command: "newAccount",
			name: $name,
			pass: $pass,
			email: $email
		}, function( data ) {
			if( data.sessionexpired ) session_expired();
			else {
				if( data.validated ) {		//* creating account succes. User is mailed a confirmation code that needs to be filled in here
					$("#newaccount").hide();
					$("#confirmaccount").show();
					alert( data.feedback );
				} else {
					alert( "Your account could not be created: " + data.reason );
				}
			}
		}, "json" );
		
		return false;
	});
	
	$("#confirmationform").submit( function() {
		$.post( $engine, {
			command: "confirmaccount",
			concode: $("#confirmationform").find("input[name=concode]").val()
		}, function( data ) {
			if( data.sessionexpired ) session_expired();
			
			if( data.validated == true ) {
				$("#confirmaccount").hide();
				$.cookie( "revisit", true );
				validUser( data.row );
			} else {
				alert( data.feedback );
			}
		}, "json" );

		return false;
	});
	
	$("#loginform").submit( function() {		//* user clicked 'login', validate the user if possible
		$username = $("#username").val();
		$password = $("#password").val();
		
		statusWait( "verifying identify" );
		$.cookie( "username", $username );
		$.cookie( "password", $password );
		
		$.post( $engine, {
			command: "validateUser",
			username: $username,
			password: $password
		}, function( data ) {
			if( data.sessionexpired ) session_expired();
			else {
				if( data.validated ) {
					$.cookie( "revisit", true );
					$.data( document.body, "userValidated", true );
					$("#login").hide();
					validUser( data.row );
					status( "" );
				} else {
					if( data.reconfirm ) {
						$("#login").hide();
						$("#welcome").hide();
						$("#confirmaccount").show();
						alert( "Your account is not yet confirmed. We resent your confirmation code to your email address. Please copy/paste that code in the following screen." );
						return false;
					}
					status( "" );
					$.cookie( "username", null );
					$.cookie( "password", null );
					alert( "invalid username or password" );
					return false;
				}
			}
		}, "json" );

		return false;
	});	
	
	$("#createnew").click( function() {
		$("#login").hide();
		$("#welcome").hide()
		$("#newaccount").show();
		return false;
	});
	
	$("#newaccountCancel").click( function() {
		$("#newaccount").hide();
		$("#welcome").show();
		$("#login").show();
		return false;
	});
	
	$("#confirmCancel").click( function() {
		$("#confirmaccount").hide();
		$("#login").show();
		return false;
	});
	
	$("#lostpassword").click( function() {
		$email = $("#loginform").find("input[id=username]").val();
		
		if( $email == "" ) {
			alert( "please fill in your email address and select 'forgot password'" );
			return false;
		}
		
		$(this).prepend( "<img src='./images/wait_icon.gif' height=11 id='waiticon'>" );
		$.post( $engine, {
			command: "lostpass",
			email: $email
		}, function( data ) {
			$("#waiticon").remove();
			alert( data.feedback );
		}, "json" );

		return false;
	});
}


