var membership_type = null;
var after_login_function = null;

function open_login( title )
{
	var ajax_post_data = new Array();

	if ( membership_type == null )
	{
		document.getElementById( 'login_title' ).innerHTML = title;
		document.getElementById( 'login_popup' ).style.display = 'block';

		document.getElementById( 'login_username' ).focus();
	}
	else
	{
		get_ajax_data( 'logout.php', ajax_post_data, process_logout );
	}
}

function close_login()
{
	document.getElementById( 'login_popup' ).style.display = 'none';
}

function do_login()
{
	var ajax_post_data = new Array();

	ajax_post_data[ 'username' ] = document.getElementById( 'login_username' ).value;
	ajax_post_data[ 'password' ] = document.getElementById( 'login_password' ).value;

	get_ajax_data( 'login.php', ajax_post_data, process_login );
}

function process_login( data )
{
	data_fields = data.split( '|' );

	if ( data_fields[ 1 ] != '' )
	{
		window.alert( data_fields[ 1 ] );
	}

	if ( data_fields[ 0 ] == '1' )
	{
		close_login();

		document.getElementById( 'login_control' ).innerHTML = 'Logout';
		set_side_menu1( 'my<br />details' );

		membership_type = data_fields[ 2 ];

		document.getElementById( 'login_username' ).value = '';
		document.getElementById( 'login_password' ).value = '';

		if ( after_login_function != null )
		{
			after_login_function_copy = after_login_function;

			after_login_function = null;

			after_login_function_copy();
		}
		else if ( window.location.href.indexOf( 'login_register.php' ) != -1 )
		{
			window.location.href = 'associates.php';
		}
		else if ( window.location.href.indexOf( 'associates.php' ) != -1 )
		{
			// Reload page in update mode
			window.location.href = 'associates.php';
		}
		else if ( ( window.location.href.indexOf( 'index.php' ) != -1 )
			   || ( window.location.href.substr( window.location.href.length - 1 ) == '/' ) )
		{
			// Go to my details page
			window.location.href = 'associates.php';
		}
	}
}

function process_logout()
{
	document.getElementById( 'login_control' ).innerHTML = 'Login';
	set_side_menu1( 'join<br />BTA' );

	membership_type = null;

	if ( window.location.href.indexOf( 'associates.php' ) != -1 )
	{
		window.location.href = 'login_register.php';
	}
}

function set_membership_type( type )
{
	membership_type = type;

	document.getElementById( 'login_control' ).innerHTML = 'Logout';
}

function login_on_return( event )
{
	var keynum
	var keychar
	var numcheck

	if( window.event ) // IE
	{
		keynum = event.keyCode;
	}
	else if( event.which ) // Netscape/Firefox/Opera
	{
		keynum = event.which;
	}

	if ( keynum == 13 )
	{
		do_login();
	}
}

function set_side_menu1( text )
{
	var side_menu1_element;
	
	side_menu1_element = document.getElementById( 'side_menu1' );

	if ( side_menu1_element != null )
	{
		side_menu1_element.innerHTML = text;
	}
}

