var loginvisible=0;
var loggedin=0;
var response_div_id_g='';

function get_cookie(cookie_name)
{
	var results=document.cookie.match('(^|;) ?'+cookie_name+'=([^;]*)(;|$)');
	if (results)
		return (unescape(results[2]));
	else
		return null;
}

function createObject() {
	var request_type;
	var browser = navigator.appName;

	if(browser == "Microsoft Internet Explorer")
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	else
		request_type = new XMLHttpRequest();
	return request_type;
}

var http = createObject();
var nocache = 0;
var email;
var psw;

function login(response_div_id)
{

// эта функция неверно работает на странице http://sobory/login
// она берет значение логина и пароля только из всплывающей формы, игнорируя основную форму на странице.
    response_div_id_g=response_div_id;
    document.getElementById(response_div_id_g).innerHTML = "Проверка...";
	email = encodeURI(document.getElementById('elogin').value);
    if (email == '')
        email = encodeURI(document.getElementById('login').value);
	psw = encodeURI(document.getElementById('epassword').value);
//    alert(psw);
    if (psw == '')
        psw = encodeURI(document.getElementById('password').value);
	nocache = Math.random();
//	http.open('get', 'http://'+window.location.hostname+'/_Kernel/ajax/login.php?email='+email+'&psw='+psw+'&nocache = '+nocache);
//	http.open('get', 'http://'+window.location.hostname+'/_k/ajax/login.php?email='+email+'&psw='+psw+'&nocache = '+nocache);
	http.open('get', 'http://'+window.location.hostname+'/_k/ajax/login.php?email='+email+'&psw='+psw+'&nocache = '+nocache);
	http.onreadystatechange = loginReply;
	http.send(null);
}

function loginReply()
{
	if(http.readyState == 4)
	{
		var response = http.responseText;
		if(response != '1')
		{
			document.getElementById(response_div_id_g).innerHTML = '<font color=red>'+response+'</font>';
		}
		else
		{
			document.getElementById(response_div_id_g).innerHTML = 'Добро пожаловать.';
// 			var expiresDate = new Date();
//			expiresDate.setTime(expiresDate.getTime() + 365 * 24 * 60 * 60 * 1000); // срок - 365 дней
//			var expires = expiresDate.toGMTString();
//			document.cookie = "soboryuser="+email+";expires="+expires;
            if (document.location.href!='http://'+window.location.hostname+'/login/')
    			document.location.reload();
            else
                location.assign('http://'+window.location.hostname+'/profile/');
		}
	}
}

function password_remind(response_div_id)
{
    response_div_id_g=response_div_id;
    document.getElementById(response_div_id_g).innerHTML = "Поиск.";
	email = encodeURI(document.getElementById('elogin').value);
    if (email == '')
        email = encodeURI(document.getElementById('login').value);
    document.getElementById(response_div_id_g).innerHTML = "Поиск..";
	nocache = Math.random();
//	alert(window.location.hostname);

//	http.open('get', 'http://'+window.location.hostname+'/_Kernel/ajax/login.php?email='+email+'&psw='+psw+'&nocache = '+nocache);
//	http.open('get', 'http://'+window.location.hostname+'/_k/ajax/p_reminder.php?email='+email+'&nocache = '+nocache);
	http.open('get', 'http://'+window.location.hostname+'/_k/ajax/p_reminder.php?email='+email+'&nocache = '+nocache);
    document.getElementById(response_div_id_g).innerHTML = "Поиск...";
	http.onreadystatechange = remindReply;
	http.send(null);
    return false;
}

function remindReply()
{
	if(http.readyState == 4)
	{
		var response = http.responseText;
//	    alert(response);
		if(response != '1')
		{
			document.getElementById(response_div_id_g).innerHTML = '<font color=red>'+response+'</font>';
		}
		else
		{
			document.getElementById(response_div_id_g).innerHTML = 'Пароль отправлен на указанный e-mail.';
		}
	}
}
