function bind_logout() {

	$('a#logout').click(function(){
		$.get('/forum/cook.php?'+(new Date()).getTime(),'',function(sid) {
			$.get('/forum/ucp.php?mode=logout&sid='+sid,'',function(resp) {
				$.get('/_w/wp-content/themes/flybuys/login/logout.php?'+(new Date()).getTime(),'',function(resp) {
					if (location.href.match(/verified/)) {
						location.href='/';
					} else {
						location.reload();
					}
				});
			});
		});
		return false;
	});

}


function logout() {
		$.get('/forum/cook.php?'+(new Date()).getTime(),'',function(sid) {
			$.get('/forum/ucp.php?mode=logout&sid='+sid,'',function(resp) {
				$.get('/_w/wp-content/themes/flybuys/login/logout.php?'+(new Date()).getTime());
			});
		});
}

$(document).ready(function() {

	$('input').keydown(function(e){
        if (e.keyCode == 13)  {
        	if (this.id!='email') {
	            $(this).parents('form').submit();
	        } else {
	        	$('#button_forgot').click();
	        }
            return false;
        }
    });

	$('#forgot_cancel').click(function(){
		$('#user_name_login,#password_login,#button_login').show();
		$('#user_name').focus();
		$('#email_forgot,#button_forgot').hide();
		$('#password_error').html('');
		$('#password_error').hide();
		return false;
	});

	bind_logout();

	$('#login_forgot').click( function() {
		$('#user_name_login,#password_login,#button_login').hide();
		$('#email_forgot,#button_forgot').show();
		$('#email').focus();
		return false;
	});

	$('#button_forgot').click(function () {

		if (is_email($('#email'))===null) {
			
			$.post('/_w/wp-content/themes/flybuys/register/forgot_password.php',
					{"email":$('#email').val()},
					function(resp){
						$('#mesg_forgot').show();
						if (resp=='') {
							$('#email_forgot, #button_forgot').hide();
							$('#mesg_forgot').html('Your password has been sent to your email address.');
						} else {
							$('#mesg_forgot').html(resp);
						}
					});
		} else {
			$('#mesg_forgot').show();
			$('#mesg_forgot').html('Please enter a valid email address.')
		}
		return false;
	});

	$('#login_form').submit(function() {

		loggedIn=false;
		wp_comp=false;
		f_comp=false;
		var msg;

		function checkLogin() {
			if (!loggedIn) {
				$('#password_error').html(msg);
				$('#password_error').show();

			} else {
				$.get('/_w/wp-content/themes/flybuys/login/identity.php?'+(new Date()).getTime(),'',function(identity){
					if (identity!=='') {
						if (location.href.match(/verified/)) {
							location.href='/';
						} else {
							location.reload();
						}

					} else {

						$('#password_error').html('This user has not been activated');
						logout();
						$('#password_error').show();
					}
				});
			}

		}


		$.get('/forum/cook.php?'+(new Date()).getTime(),'',function(sid) {
			$.get('/forum/ucp.php?mode=logout&sid='+sid,'',function() {
				$.get('/_w/wp-content/themes/flybuys/login/logout.php','',function() {

							msg='Incorrect password';
							// login to forum
							$.post('/forum/ucp.php?mode=login',
										{"login":"Login",
										 "username":$('#user_name').val(),
										 "password":$('#password').val(),
										 "redirect":"/_w/wp-content/themes/flybuys/login/success.html"},
										 function(response){

										 	if (response.search('You have been successfully logged in')>0) {
										 		loggedIn=true;
										 	}
										 	f_comp=true;

										 	if (f_comp && wp_comp) {
										 		checkLogin();
										 	}

										 });



							// login to wordpress
							$.post('/_w/wp-login.php',
										{"wp-submit":"Log In",
										 "log":$('#user_name').val(),
										 "pwd":$('#password').val(),
										 "redirect_to":"/_w/wp-content/themes/flybuys/login/success.html",
										 "testcookie":1},
										 function(response){

										 	if (response.search('ERROR')==-1) {
										 		loggedIn=true;

										 	} else {
										 		loggedIn=false;

										 		if(response.search('field is empty')>0) {
										 			msg='Enter a password';
										 		}
										 		if(response.search('Invalid username')>0) {
										 			msg='Invalid username';
										 		}
										 	}

										 	wp_comp=true;

										 	if (f_comp && wp_comp) {
										 		checkLogin();
										 	}

										 });

				});
			});
		});

		return false;

	});

});
