
var wait_login_load = false;
var archiveUsernameRestore = "";

function user_arhive()
{
	if(typeof(user_arhive_almost_done) == 'undefined' || document.getElementById('wrong_login_or_pass'))
	{
		user_arhive_almost_done = true;
		if ( wait_login_load == false || (document.getElementById('username777').value != '' && document.getElementById('password_index').value != '') )
		{
			wait_login_load = true;
			
			var url = 
			{ 
				'mode' : 'user_select_arhive',
				'username' : document.getElementById('username777').value, 
				'password' : document.getElementById('password_index').value
			};

			if ( document.getElementById('username777').value != '' && document.getElementById('password_index').value != '' )
			{
				document.getElementById('error').innerHTML = '<b class="waiting"></b>';
				
				ajax
				(
					data.full_server_name + '/functions_ajax.php', 
					url, 
					
					function (e)
					{
						wait_login_load = false;
						
						if ( e != 'Hacking attempt ')
						{
							try
							{
								var back = eval( '(' + e + ')' );
							}
							catch(e)
							{
								document.getElementById('error').innerHTML = '<span style="cursor:pointer;color:#800000;" onClick="window.location.href=window.location.href;">Произошла ошибка. Обновите страницу.</span>';
								
								return;
							}
							
							if ( back['ban'] )
							{
								document.getElementById('error').innerHTML = '';
								
								banned(1);
								
								return;
							} 
							
							if ( back['status'] == 4 )
							{
	
								banned();
	
								return;			
							}
							else if ( [2,8].in_array(back['status']) )
							{
								param = 'Ваш профиль пользователя (ник <b>' + back['username'] + '</b>) был архивирован, так как Вы не посещали сайт в течение года. Вы можете восстановить свой профиль, для этого Вам необходимо подтвердить восстановление.';
							}
							else
							{
								param = 'Ваш профиль пользователя (ник <b>' + back['username'] + '</b>) был удален по Вашей просьбе с возможностью восстановления до ' + back['date'] + '. Вы можете восстановить свой профиль, для этого Вам необходимо подтвердить восстановление.';
							}
	
	
							if ( !back['error'] && back['date'] && back['arhiv'] == 'true' && back['username'] && !back['restore'] )
							{
								archiveUsernameRestore = back['username'];
								document.getElementById('error').innerHTML = '';
	
								add_oo();
								opacity();
								overlay_on();
	
								document.getElementById('overlay').innerHTML = '<div class="rounded-corner-container" style="background:transparent none !important;"><div class="rounded-corner-1" style="background:#FFFFFF;"></div><div class="rounded-corner-2" style="background:#FFFFFF;"></div><div class="rounded-corner-3" style="background:#FFFFFF;"></div><div class="rounded-corner-4" style="background:#FFFFFF;"></div></div><div style="padding:10px;color:#24476D;background:white;"><div align="center" style="width:100%;"><b>Восстановление профиля пользователя:</b></div><div style="font-size:12px;">' + param + '</div></div><table width="100%" style="background:white;padding:10px;"><tr><td undefined="" style="background:white;"><div style="width:300px; cursor: hand; cursor: pointer;" onmouseover="hover_all_main(120, 120);" onmouseout="unhover_all_main(120, 120);"><div class="rounded-corner-1" style="background:gray; padding-left:1px; padding-right:1px;"></div><div class="rounded-corner-2" style="background:gray; padding-left:2px; padding-right:2px;"><div class="120" style="height: 1px; background-attachment: scroll; background-repeat: repeat; background-image: none; background-position: 0% 0%; background-color: #fff78f; color: #0052c4"></div></div><div class="rounded-corner-3" style="background:gray; padding-left:1px; padding-right:1px;"><div class="120" style="height: 1px; background-attachment: scroll; background-repeat: repeat; background-image: none; background-position: 0% 0%; background-color: #fff78f; color: #0052c4"></div></div><div class="rounded-corner-4" style="background:gray; padding-left:1px; padding-right:1px;"><div class="120" style="height: 2px; background-attachment: scroll; background-repeat: repeat; background-image: none; background-position: 0% 0%; background-color: #fff78f; color: #0052c4"></div></div><div class="120" style="padding-top: 0px; padding-bottom: 0px; padding-right: 2px; padding-left: 2px; border-top-color: currentColor; border-bottom-color: currentColor; border-top-width: 0px; border-bottom-width: 0px; border-top-style: none; border-bottom-style: none; border-left-color: #808080; border-left-width: 1px; border-left-style: solid; border-right-color: #808080; border-right-width: 1px; border-right-style: solid; background-attachment: scroll; background-repeat: repeat; background-image: none; background-position: 0% 0%; background-color: #fff78f; color: #0052c4"><div style="width:99%; font-size:1px"><input onclick="restore()" type="button" id="submit_nick" name="save" value="Восстановить профиль пользователя" class="120" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 100%; border-top-color: currentColor; border-left-color: currentColor; border-right-color: currentColor; border-bottom-color: currentColor; border-top-width: 0px; border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-top-style: none; border-left-style: none; border-right-style: none; border-bottom-style: none; margin-top: -3px; margin-bottom: -3px; font-size: 14px; font-weight: 700; cursor: pointer; overflow-x: visible; overflow-y: visible; background-attachment: scroll; background-repeat: repeat; background-image: none; background-position: 0% 0%; background-color: #fff78f; color: #0052c4" onclick="submit_something(\'nick\')"></div></div><div class="rounded-corner-4" style="background:gray; padding-left:1px; padding-right:1px;"><div class="120" style="height: 2px; background-attachment: scroll; background-repeat: repeat; background-image: none; background-position: 0% 0%; background-color: #fff78f; color: #0052c4"></div></div><div class="rounded-corner-3" style="background:gray; padding-left:1px; padding-right:1px;"><div class="120" style="height: 1px; background-attachment: scroll; background-repeat: repeat; background-image: none; background-position: 0% 0%; background-color: #fff78f; color: #0052c4"></div></div><div class="rounded-corner-2" style="background:gray; padding-left:2px; padding-right:2px;"><div class="120" style="height: 1px; background-attachment: scroll; background-repeat: repeat; background-image: none; background-position: 0% 0%; background-color: #fff78f; color: #0052c4"></div></div><div class="rounded-corner-1" style="background:gray; padding-left:1px; padding-right:1px;"></div></div></td><td undefined="" style="padding-left:10px;background:white;"><div style="width:260px; cursor: hand; cursor: pointer;" onmouseover="hover_all_main(121, 121);" onmouseout="unhover_all_main(121, 121);"><div class="rounded-corner-1" style="background:gray; padding-left:1px; padding-right:1px;"></div><div class="rounded-corner-2" style="background:gray; padding-left:2px; padding-right:2px;"><div class="121" style="background:#FFF78F; height:1px;"></div></div><div class="rounded-corner-3" style="background:gray; padding-left:1px; padding-right:1px;"><div class="121" style="background:#FFF78F; height:1px;"></div></div><div class="rounded-corner-4" style="background:gray; padding-left:1px; padding-right:1px;"><div class="121" style="background:#FFF78F; height:2px;"></div></div><div class="121" style="background:#FFF78F; padding:0; padding-right:2px; padding-left:2px; border:0; border-left:1px solid gray; border-right:1px solid gray; background:#FFF78F;"><div style="width:99%; font-size:1px"><input onclick="block_off();" type="button" name="save" value="Отмена" class="121" style="background:#FFF78F; padding:0; width:100%; color:#0052C4; border:0; margin-top:-3px; margin-bottom:-3px; font-size: 14px; font-weight: bold; cursor: hand; cursor: pointer; overflow: visible;" onclick="cancel_something(\'nick\', \'\')"></div></div><div class="rounded-corner-4" style="background:gray; padding-left:1px; padding-right:1px;"><div class="121" style="background:#FFF78F; height:2px;"></div></div><div class="rounded-corner-3" style="background:gray; padding-left:1px; padding-right:1px;"><div class="121" style="background:#FFF78F; height:1px;"></div></div><div class="rounded-corner-2" style="background:gray; padding-left:2px; padding-right:2px;"><div class="121" style="background:#FFF78F; height:1px;"></div></div><div class="rounded-corner-1" style="background:gray; padding-left:1px; padding-right:1px;"></div></div></td></tr></table></div><div class="rounded-corner-container" style="background:transparent none !important;"><div class="rounded-corner-4" style="background:#FFFFFF;"></div><div class="rounded-corner-3" style="background:#FFFFFF;"></div><div class="rounded-corner-2" style="background:#FFFFFF;"></div><div class="rounded-corner-1" style="background:#FFFFFF;"></div></div>';
	
	
								overlay_width(600);
								overlay_size(false);
							}
							else
							{
								var url = 
								{
									'mode' : 'logpas',
									'username' : document.getElementById('username777').value, 
									'password' : document.getElementById('password_index').value,
									'db' : 0
								};
	
								ajax
								(
									data.full_server_name + '/functions_ajax.php', 
									url, 
									
									function (e)
									{
										if ( e == 1 )
										{
											login_send();
										}
										else
										{
											document.getElementById('error').innerHTML = '<b id="wrong_login_or_pass">Неправильный логин или пароль</b>';
											if(document.getElementById('error_duplicate'))
											{
												document.getElementById('error_duplicate').innerHTML = '<b id="wrong_login_or_pass">Неправильный логин или пароль</b>';
											}
											wait_login_load = false;
										}
									}
								);
							}
						}
						else
						{
							window.location.href = window.location.href;
						}
					}
				);
			}
		}
	}
}

function banned(param)
{
	add_oo();
	opacity();
	overlay_on('white');
	close = close_overlay();
	
	if (param == 1)
	{
		document.getElementById('overlay').innerHTML = close + '<div align="center" class="alink" style="padding:5px;"><span class="genmed">Пользователю <b>' + document.getElementById('username777').value + '</b> запрещен доступ к сайту. Причину запрета можно узнать в разделе "<a href="' + data.full_server_name + '/forum/viewforum.php?f=81">Нарушения и санкции</a>"</span><br><br></div>';
	}
	else
	{
		document.getElementById('overlay').innerHTML = close + '<div align="center" class="alink" style="padding:5px;"><span class="genmed">Пользователь <b>' + document.getElementById('username777').value + '</b> был удалён администрацией сайта за нарушение <b><a target="blank" href="http://posobie.info/faq.php">правил</a></b> на posobie.info' + '</span></div>';

	}
	
	overlay_width(500);
	overlay_size(false);
}


function create_cross_domain_request(subject, domain_array, callback)
{
	for(var i = 0; i < domain_array.length; i++)
	{
		var file_adress = 'http://' + domain_array[i] + '/set_cookies.php';
		uploaderJS.file_send(document.getElementById('form_cross_domain_'+subject), file_adress, callback);
	}
}

function after_create_cross_domain_cookie()
{
	/* begin Mustela */
	
	if ( window.location.href.indexOf('/mustela/') > 0 )
	{
		document.getElementById("form_test").style.display = document.getElementById("address_button").style.display = "";
		document.getElementById("adress_authorised").style.display = document.getElementById("fake_button").style.display = document.getElementById("guest_message").style.display = "none";

		return;
	}
	
	/* end Mustela */
			
			
		if (document.getElementById('redirect') && document.getElementById('redirect').value != '/' && document.getElementById('redirect').value != '')
		{
			var redirect = document.getElementById('redirect').value;
			if (redirect.indexOf('mode=firstreg') != -1)
			{
				redirect = '';
			}
		}
		else
		{ 
			var redirect = '';
		}
	window.location.href = 'http://' +  window.location.host + redirect;
}



function login_send(param)
{
	base_url = ( document.getElementById('base_url') ) ? document.getElementById('base_url') : '';
	redirect = ( document.getElementById('redirect') ) ? document.getElementById('redirect').value : '';
	username = ( document.getElementById('username777') ) ? document.getElementById('username777').value : '';
	password = ( document.getElementById('password_index').value ) ? document.getElementById('password_index').value : '';
	login = ( document.getElementById('but0') ) ? document.getElementById('but0').value : '';
	
	url = 
	{
		'base_url' : base_url,
		'redirect' : redirect,
		'username' : username,
		'password' : password,
		'func_back' : 'login_back',
		'login' : login,
		'ajax' : 1
	};
	
	if ( document.getElementById('autologin') && document.getElementById('autologin').checked == true )
	{
		url['autologin'] = document.getElementById('autologin').checked;
	}
	
	if ( param )
	{
		url['db'] = 1;
	}

	ajax
	(
		data.full_server_name + '/login.php', 
		url, 
		
		function (e, param)
		{

			
			if (e != 'Hacking attempt')
			{
				var back = eval( '(' + e + ')' ); // alert('here'+e);

				if (back['have_a_cook'])
				{
					var obj = findClass('form_cross_domain','form');
					for(i=0; i < obj.length; i++)
					{
						obj[i].parentNode.removeChild(obj[i]);
					}
					one_form = document.createElement('form');
					one_form.id = 'form_cross_domain_cookie';
					one_form.className = 'form_cross_domain';
					one_form.method = 'get';
					one_form.style.display = 'none';
	
					one_form.innerHTML = '<input type="text" value="'+back['have_a_cook']+'" name="cook_number" id="cook_number">';

					if(!document.getElementById('end_one_form'))
					{
						document.getElementById('submit_loggin').innerHTML += '<div id="end_one_form"></div>';
					}	
					document.getElementById('end_one_form').parentNode.insertBefore(one_form, document.getElementById('end_one_form'));

					domain_array = [test_part+'xn--80aqkbgg.xn--p1ai'];

					for(var j = 0; j < domain_array.length; j++)
					{
						domains = domain_array[j];
						for(var i = 0; i < back['have_a_cook']; i++)
						{
							var domain = back['cook_domain_'+i];
							var cook_value = escape(back['cook_value_'+i]);
		
							var first_input = document.createElement('input');
							first_input.value = back['cook_name_'+i];
							first_input.name = 'cook_name_'+i;
							first_input.type = 'text';
			
							document.getElementById('cook_number').parentNode.insertBefore(first_input, document.getElementById('cook_number'));
	
							var sec_input = document.createElement('input');
							sec_input.value = cook_value;
							sec_input.name = 'cook_value_'+i;
							sec_input.type = 'text';
			
							document.getElementById('cook_number').parentNode.insertBefore(sec_input, document.getElementById('cook_number'));

							var third_input = document.createElement('input');
							third_input.value = back['cook_expires_'+i];
							third_input.name = 'cook_expires_'+i;
							third_input.type = 'text';
			
							document.getElementById('cook_number').parentNode.insertBefore(third_input, document.getElementById('cook_number'));
	
							var fourth_input = document.createElement('input');
							fourth_input.value = back['cook_path_'+i];
							fourth_input.name = 'cook_path_'+i;
							fourth_input.type = 'text';
	
							document.getElementById('cook_number').parentNode.insertBefore(fourth_input, document.getElementById('cook_number'));
	
							var fifth_input = document.createElement('input');
							fifth_input.value = domains;
							fifth_input.name = 'cook_domain_'+i;
							fifth_input.type = 'text';
			
							document.getElementById('cook_number').parentNode.insertBefore(fifth_input, document.getElementById('cook_number'));
						}
					}

//					var domain_array = [];
//					domain_array[0] = window.location.hostname;
					create_cross_domain_request('cookie', domain_array, after_create_cross_domain_cookie);
				}
			}

			if ( !param )
			{ 
				if ( document.getElementById('redirect') && document.getElementById('redirect').value != '/' && document.getElementById('redirect').value != '' )
				{
					var redirect = document.getElementById('redirect').value;

					if ( redirect.indexOf('mode=firstreg') != -1 )
					{
						redirect = '';
					}
				}
				else
				{ 
					var redirect = '';
				}

				if(!domain_array)
				{
					window.location.href = data.full_server_name + redirect;
				}
			}
			else
			{
				url = 
				{
					'mode' : restore_message
				};
				
				ajax
				(
					data.full_server_name + '/functions_ajax.php', 
					url, 
					
					function (e, param)
					{
						if ( e == 1 )
						{
							window.location.href = data.full_server_name + '/?' + param;
						}
						else
						{
							// ошибка 
						}
					},
					
					param
				);		
			}
		},
		
		param
	)
}

function restore()
{
	var url = 
	{
		'mode' : 'restore',
		'username' : archiveUsernameRestore,
		'password' : document.getElementById('password_index').value
	};

	ajax
	(
		data.full_server_name + '/functions_ajax.php', 
		url, 
		
		function (e)
		{
			if ( e == 1 )
			{
				login_send('restore')
			}
			else
			{
				// ошибка
			}
		}
	);
}

function get_by_email(subject)
{
	var email = document.getElementById("box_email").value;

	if ( email == "" )
	{
		alert("Вы должны заполнить все поля!");
	}
	else if ( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email) )
	{
		if ( document.getElementById('get_by_email_message_error') )
		{
			document.getElementById('get_by_email_message_error').parentNode.removeChild(document.getElementById('get_by_email_message_error'));
		}
		
		var button = document.getElementById('get_remember_box_button').innerHTML;
		
		document.getElementById('get_remember_box_button').innerHTML = '<img src="http://users.posobie.info/img/loading1.gif">';
		
		ajax
		(
			data.full_server_name + "/vitrum/vitrum_update.php",
			
			{
				"type" : "get_by_email",
				"subject" : "password",
				"email" : email
			},
			
			function ( e, button )
			{
				try
				{
					var back = eval( "(" + e + ")" );
					var message = "";
					
					if ( back["message"] )
					{
						message = back["message"];
					}
					
					document.getElementById("recovery_pass_epic_fail").innerHTML = '<table width="100%" height="400px;" cellpadding="0" cellspacing="0" border="0"><tr><td align="center" valign="middle"><b class="genmed">' + message + '</b></td></tr><tr><td align="center" valign="bottom"><b style="cursor:pointer;font-size:10px;" class="oh_darkblue" onclick="floating_container.destroy();">[ Закрыть окно ]</b></td></tr></table>';	
				}
				catch ( k )
				{
					//	ошибка
					
					document.getElementById('get_remember_box_button').innerHTML = button + '<div id="get_by_email_message_error" class="genmed" style="color:red;">Не удалось восстановить пароль. Попробуйте еще раз.</div>';
				}
			},
			
			button
		);
	}
	else
	{
	    alert("Неправильный e-mail"); 
	}
}

function get_remember_box()
{
	closew();
	
	close_iframe_simple();
	
	var message = '<div id="recovery_pass_epic_fail"><table cellpadding="6" class="forumline" style="margin:0px 10px 0px 10px;border:0;"><tr><td colspan="3"><p align="center" class="genmed_b" style="font-size:14px;">Как восстановить ЗАБЫТЫЙ ПАРОЛЬ:</p><br><p class="genmed_b" style="font-size:12px;text-align:justify;">1) Введите выбранный Вами при регистрации на сайте адрес e-mail и нажмите кнопку &laquo;Получить пароль на электронную почту&raquo;. Если появится сообщение о ошибке - обратитесь к администратору через <a style="text-decoration:underline;" href="' + data.full_server_name + '/profile.php?mode=email_guest&back=1">форму обратной связи</a>, скопировав в письмо текст полученного Вами сообщения об ошибке.<br>2) Если Вы не получили сообщения об ошибке, то новый пароль придет на Ваш адрес e-mail. <b style="color:red;">Перед тем, как его использовать, обязательно перейдите по ссылке для активизации пароля, указанной в письме!</b><br>3) После активизации пароля зайдите на сайт под новым паролем. Копируйте пароль из письма внимательно, Или введите пароль вручную, <b style="color:red;">обращая внимание на то, чтобы раскладка клавиатуры была английской!</b><br>4) Для удобства входа на сайт можно на странице «О себе» (вкладка «Управление профилем») заменить автоматически сгенерированный пароль на пароль, который Вам будет удобно запомнить и ввести без ошибок.</p></td></tr><tr><td class="row2" width="38%" align="right" style="border:0;"><tr><td align="center"><table><tr><td class="row2" style="border:0;" align="center"><strong class="genmed_b">Укажите Ваш адрес e-mail:</strong><td class="row2" style="border:0;"><span class="gensmall"><div class="rounded-corner-1" style="background:gray; padding-left:1px; padding-right:1px;"></div><div class="rounded-corner-2" style="background:gray; padding-left:2px; padding-right:2px;"><div style="background:#FFFFFF; height:1px;"></div></div><div class="rounded-corner-3" style="background:gray; padding-left:1px; padding-right:1px;"><div style="background:#FFFFFF; height:1px;"></div></div><div class="rounded-corner-4" style="background:gray; padding-left:1px; padding-right:1px;"><div style="background:#FFFFFF; height:2px;"></div></div><div style="padding-right:2px; padding-left:2px; border-left:1px solid gray; border-right:1px solid gray;"><div style="width:200px;"><input class="post" type="text" width="100%" maxlength="255" name="email" id="box_email" style="width:94%; border:0; margin-top:-3px; margin-bottom:-3px;" value=""></div></div><div class="rounded-corner-4" style="background:gray; padding-left:1px; padding-right:1px;"><div style="background:#FFFFFF; height:2px;"></div></div><div class="rounded-corner-3" style="background:gray; padding-left:1px; padding-right:1px;"><div style="background:#FFFFFF; height:1px;"></div></div><div class="rounded-corner-2" style="background:gray; padding-left:2px; padding-right:2px;"><div style="background:#FFFFFF; height:1px;"></div></div><div class="rounded-corner-1" style="background:gray; padding-left:1px; padding-right:1px;"></div></span></td><tr><td colspan="3" align="center" style="padding-top:20px; padding-bottom:20px;"><div><div id="get_remember_box_button" class="bricbb"><button onclick="get_by_email();" data-ring="border" name="submit" type="submit" class="bribb">Получить пароль на электронную почту</button></div></div></td></tr></table></tr></tr><tr><td colspan="3" align="center"><b style="cursor: pointer;font-size:10px;" class="oh_darkblue" id="close_get_remember_box">[ Закрыть окно ]</b></td></tr></table></div>';

	floating_container.create
	(
		{
			"close" : false,
			"content" : message,
			"round" : true,
			"width" : 700
		}
	);
	
	addEvent
	(
		document.getElementById('close_get_remember_box'), "click",
		
		function ()
		{
			floating_container.destroy();
		}
	);
}
