/* backlink.js */

function checkRestoreEmail()
{
	var re = $("rest_email").value;
	if(re == '')
	{
		alert("Укажите email адрес !");
		//$("auth_err_info").innerHTML = "Укажите email адрес !";
		jQuery("#rest_email").focus();
		return false;
	}
	if(!check_email(re))
	{
		alert("Укажите правильный email адрес !");
		//$("auth_err_info").innerHTML="Укажите правильный email адрес !";
		jQuery("#rest_email").focus();
		return false;
	}
	return true;
}

function authPopupForm()
{
	jQuery("#backlink_form").hide();
	jQuery("#authpopup_form").show();
	jQuery("#authpopup_form").focus();
	jQuery("#authpopup_form").css("top", 210);
	jQuery("#authpopup_form").css("left", ((document.documentElement.clientWidth || document.body.clientWidth) - 100 ) / 2);
}

function authOnEnter(evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	if (charCode == 13) {
		ajaxCheckAndAuth();
		return true;
	} else {
	return true;
	}
}

function applyOnEnter(evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	if (charCode == 13) {
		var l_email = $("l_email").value;
		if(l_email == '') return true;
		ajaxCheckAndApply();
		return true;
	} else {
	return true;
	}
}

function ajaxUnAuth()
{
	jQuery("#auth_reg_x").load("/unauth_reg_space.php");
}

function ajaxCheckAndAuth()
{
	if(rigthAuthForm())
	{	
		var l_email = $("l_email").value;
		var l_pass = $("l_pass").value;
		//alert('log_name='+l_email+'&log_pass='+l_pass);
	
		jQuery.ajax({
				  type: 'POST',
				  url: '/auth_reg_res.php',
				  data: 'log_name='+l_email+'&log_pass='+l_pass,
				  timeout: 2000, 
				  async: false, 
				  success: function(res) 
							{ 
								if(parseInt(res) == 1) // успешная авторизация 
								{
									$("auth_err_info").innerHTML = ""; 
									jQuery("#authpopup_form").hide();
									jQuery("#auth_reg_x").load("/auth_reg_space.php"); 
									reloadBasketContent();
									jQuery("#forming_form").show();
									jQuery("#identify_form").hide();
									jQuery("#basket_table_content").load("/main_after_auth.php?qq="+location.href);
								}
								else 
								{
									if(parseInt(res) == 3) // указанные email адрес отсутствует 
									{
										$("auth_err_info").innerHTML = "Указанный e-mail в базе отсутствует! Попробуйте ещё раз или <a href='/registration/'>зарегистрируйтесь</a>"; 
										$("l_email").value = '';
										$("l_pass").value = '';
									}
									if(parseInt(res) == 4) // указан непрпавильный пароль
									{
										$("auth_err_info").innerHTML = "Неправильный пароль!"; 
										$("l_pass").value = '';
									}
									if(parseInt(res) != 3 && parseInt(res) != 4) // и как обычно - неправильны ответ сервера, которого не бывает ))
										$("auth_err_info").innerHTML = "Неправильный параметр!("+parseInt(res)+")"; 
								}
							},
					error: function ()
							{
								//alert("Невозможно получить ответ с сервера!");
								$("auth_err_info").innerHTML = "Невозможно получить ответ с сервера!";
							}
				});
	}
}

function ajaxCheckAndApply()
{
	if(rigthApplyData())
	{
		var l_email = $("client_email").value;
		$("apply_err_info").innerHTML = "Отправлен запрос на сервер...";
		jQuery.ajax({
				  type: 'POST',
				  url: '/apply_client_email.php',
				  data: 'apply_email='+l_email,
				  timeout: 2000, 
				  async: false, 
				  success: function(res) 
							{ 
								if(parseInt(res) == 1) /* успешно сохранён введённый пользователем адрес */
								{
									jQuery("#email_mess").hide();
									jQuery("#email_mess").css("width", 0);
									jQuery("#email_mess").css("height", 0);
									$("email_mess").innerHTML='';
									// 21 march 2010 - onclick  jQuery("#bsk_forming").css("display","block");
									//jQuery("forming_form").submit(); // next step 
									document.forms["forming_form"].submit();
								}
								else 
								{
									if(parseInt(res) == 3) /* такой email в базе уже есть */ 
									{
										$("apply_err_info").innerHTML = "Указанный email в базе уже есть!"; 
										//$("client_email").value = '';
									}
									
									if(parseInt(res) != 3)
										$("apply_err_info").innerHTML = "Неправильный параметр!"; 
								}
							},
					error: function ()
							{
								//alert("Невозможно получить ответ с сервера!");
								$("apply_err_info").innerHTML = "Невозможно получить ответ с сервера!";
							}
				});
	}
}

function rigthAuthForm()
{
	var l_email = $("l_email").value;
	var l_pass = $("l_pass").value;

	if(l_email == "")
	{
		//alert("Введите Ваш email адрес !");
		$("auth_err_info").innerHTML = "Введите Ваш email адрес!";
		jQuery("#l_email").focus();
		return false;
	}
	if(!check_email(l_email))
	{
		if(l_pass=='') return false;
		//alert("Укажите правильный email !");
		$("auth_err_info").innerHTML = "Укажите правильный email!";
		jQuery("#l_email").focus();
		return false;
	}
	if(l_pass == '')
	{
		//alert("Введите пароль !");
		$("auth_err_info").innerHTML = "Введите пароль!";
		jQuery("#l_pass").focus();
		return false;
	}
	return true;
}

function rigthApplyData()
{
	var l_email = $("client_email").value;
	if(l_email == "")
	{
		//alert("Введите Ваш email адрес !");
		$("apply_err_info").innerHTML = "Введите Ваш email адрес!";
		jQuery("#client_email").focus();
		return false;
	}
	if(!check_email(l_email))
	{
		//alert("Укажите правильный email !");
		$("apply_err_info").innerHTML = "Укажите правильный email!";
		jQuery("#client_email").focus();
		return false;
	}
	return true;
}


