//////////////////////////////////////////////////////////////////////////////
// configurable variables
//////////////////////////////////////////////////////////////////////////////

var auth_url = "/forums/auth/protect.php";
var login_page = "/en/community/login.php";
var asynchronous = false;

//////////////////////////////////////////////////////////////////////////////
// there should be no need to edit any code below this point
//////////////////////////////////////////////////////////////////////////////

//
// make an ajax call to protect.php to try and validate the user login
// if this fails then redirect the browser to our login page
//

function check_login() 
{
	//
	// create our http request
	//

	var http_request = false;

	//
	// mozilla/safari
	//

	if (window.XMLHttpRequest) 
	{
		http_request = new XMLHttpRequest();
	}

	//
	// ie
	//

	else
	{
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}

	// request the page with asynchronous set to true or false as configured
	http_request.open('POST', auth_url, asynchronous);

	// define an anonymous function to be called when the asynchronous 
    // http request is ready

    if (asynchronous) {
        http_request.onreadystatechange = function() {
            if (http_request.readyState == 4) {
                redirect_unless_ok(http_request.responseText);
            }
        }
    }

    http_request.send(null);

    if (!asynchronous) {
        redirect_unless_ok(http_request.responseText);
    }
}

function redirect_unless_ok(response) {
    if (response.search(/ok/) == -1) {
        // we need to pass the current html file name to the login
        // code so we can be redirected back after a successful login
        var pagename = window.location.pathname;

        var base_url = window.location.protocol + '//' + window.location.host;
        window.location = base_url + login_page + "?page=" + pagename;
    }
}

check_login();


