// HuB. AJAX login script

function ajax_show_login_panel() {
	document.getElementById("login-form").style.display = "block";
	return true;
}

function ajax_hide_login_panel()
{
	document.getElementById('login-form').style.display = 'none';
	return false;
} 

function ajax_getHTTPRequestObject() {
    var xmlHttpRequest;
    if (typeof ActiveXObject != 'undefined') {
        xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
    } else if (typeof XMLHttpRequest != 'undefined') {
        xmlHttpRequest = new XMLHttpRequest();
    } else {
        xmlHttpRequest = false;
    }
    return xmlHttpRequest;
}

function ajax_submit_login()
{
	var ajax_httpRequester = ajax_getHTTPRequestObject();
	ajax_httpRequester.onreadystatechange = function() {
	    if (ajax_httpRequester.readyState == 4) {
	        if (ajax_httpRequester.status==200) {
	            // OK
	            result = ajax_httpRequester.responseText;
	            if (result=="+logged") {
	            	//alert('Вы успешно авторизированы!');
	            	document.location.reload();
	            } else if (result=="-login") {
	            	alert('Неправильное имя пользователя!');
	            } else if (result=="-password") {
	            	alert('Неправильный пароль!');
	            } else {
	            	alert('Вход не возможен!');
	            }
	        } else {
	        	alert('Ошибка! Невозможно соединиться с сервером.');
	        	// failed
	        }
			button = document.getElementById("_floginbutton");
			//button.value="Войти »";
			button.style.background="white";
			button.onclick = ajax_submit_login;
	    }
	}
	ajax_httpRequester.open('POST', '/login?ajax=Y', true);
	ajax_httpRequester.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	var login = document.getElementById("_flogin").value;
	if (login.length==0) { alert('Введите имя пользователя!'); return; }
	var passw = document.getElementById("_fpassword").value;
	if (passw.length==0) { alert('Введите пароль!'); return; }
	/*if (document.getElementById("id_logout_at").checked)
		logoutat = 'cookie';
	else logoutat = 'session';*/
	var logoutat = 'cookie';
	ajax_httpRequester.send('_flogin='+login+'&_fpassword='+passw+'&_logout_at='+logoutat);
	button = document.getElementById("_floginbutton");
	//button.value="Подождите... »";
	button.style.background="gray";
	button.onclick = function() { alert('Подождите, запрос обрабатывается...'); }
}
