/* Function that is supposed to execute commands after the AJAX execution */
var parseResponseFunction;

/* Name of the function that is supposed to run after and if login OK */
//var afterLogin;
/* Parameters to be sent to the function */
//var paramsLogin;


var controlerAction;
var controlerParameters;


/* This token avoids having many AJAX queries running at the same time */
var token = false;


/* Checks whether the guy is logged in or not */
/*
function isLoggedIn(afterlogin, params)
{
	afterLogin = afterlogin;
	paramsLogin = params;
	parseResponseFunction = isLoggedInResult;
	runAJAX('member/login.php', 'ajax_is_connected', '', 'get');
}

// Connection state result
function isLoggedInResult(response)
{
	if(response == 1)
	{
		var func = function()
		{
			afterLogin(paramsLogin);
		}
		func()
	}
	else
		needstoLogin();
}
*/

function runAJAX(controller, action, parameters, form_method)
{
	controlerAction = action;
	controlerParameters = parameters;

	/* If an instance is already running */
	if (token == true) {
		return;
	}

	token = true;

	param_string = ''
	for(key in parameters)
	{
		param_string = param_string + '&'+key+'='+parameters[key];
	}

	http_request = false;

	if (window.XMLHttpRequest)
	{ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{ // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Your browser is not AJAX compatible');
		return
	}

	http_request.onreadystatechange = resultAJAXQuery;

	if(form_method == 'get')
	{
		http_request.open('GET', '/'+controller+'?action='+action+param_string, true);
		http_request.send(null);
	}
	else if (form_method == 'post')
	{
		http_request.open('POST', '/'+controller, true);
		//http_request.overrideMimeType('text/html; charset=utf-8');
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send('action='+action+param_string);
	}
}

function resultAJAXQuery()
{
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			//alert(http_request.responseText)
			var func = function()
			{
				if (http_request.responseText == -1) { // if a request returns API_ERROR_NOT_CONNECTED
					needstoLogin();
				}
				else {

					// If an AJAX query produces an error
					// Just uncomment the following line
					// You'll have the full response of the query
					// And you'll know what is going wrong
					//alert(http_request.responseText)

					parseResponseFunction(http_request.responseText);
					}
			}
			func()
		}
		else
		{
			alert(JST_AJAX_ERROR);
		}
	}
	else
	{
		// Still waiting for the answer
	}

	/* Frees the token */
	token = false;
}


function needstoLogin()
{
	document.getElementById(controlerAction).style.visibility = 'visible';
    document.getElementById(controlerAction+"_register").href = '/register/?post_login_url='+escape(encodeURI(document.location))+'&act='+escape(encodeURI(controlerAction))+'&sig='+parameters['sig']+'&rating='+parameters['rating']+'&channel_name='+escape(encodeURI(parameters['channel_name']));
	/*
    if(window.confirm(JST_NEED_TO_LOGIN1 + "\n" + JST_NEED_TO_LOGIN2))
	{
		url = '/register/?post_login_url='+escape(encodeURI(document.location)) + '&action=' + escape(encodeURI(controlerAction)) + '&parameters=' + escape(encodeURI(controlerParameters))
		document.location = '/register/?post_login_url='+escape(encodeURI(document.location))+'&act='+escape(encodeURI(controlerAction))+'&sig='+parameters['sig']+'&rating='+parameters['rating']+'&channel_name='+escape(encodeURI(parameters['channel_name']));
	}
	*/
}