var Debug = {
	elm: null,
	
	init: function(){
		var elm = document.createElement('span');
		elm.setAttribute('style', 'position:fixed; z-index:2000; display:block; left:0; top:0; width:150px; height:35px; background-color:#ccc');
		elm.style.position = 'fixed';
		elm.style.top = 0;
		elm.style.left = 0;
		
		document.body.appendChild(elm);
		Debug.elm = elm;
	},
	print: function(data){
		Debug.elm.innerHTML = data;
	}
}

Popup = 
{
	show: function()
	{
		var isIE = ( window.ActiveXObject );
		if(!isIE)
		{
			window.onresize = function() { document.getElementById('overflow').style.height = document.getElementById('wrap').offsetHeight + 'px'; };
			document.getElementById('overflow').style.height = document.getElementById('wrap').offsetHeight + 'px';
			document.getElementById('overflow').style.position = 'absolute';
		}
		
		document.getElementById('overflow').style.display = 'block';
		
		
		window.onscroll = function()
           {
               var init = 100;
               var popup = document.getElementById('popup_content');
               var pageOffset = (window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop;
               popup.style.top = (init + pageOffset) + 'px';
               return function()
               {
                   var pageOffset = (window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop;
                   popup.style.top = (init + pageOffset) + 'px';
               }
           }(); 
	},
	hide: function()
	{
		document.getElementById('overflow').style.display = 'none';
	}
};
ShowHideElem = 
{
	Show: function( elem )
	{
		document.getElementById(elem).style.display = ''
		return false;
	},
	Hide: function( elem )
	{
		document.getElementById(elem).style.display = 'none';
		return false;
	}
};



Auth =
{
	Register: function( last_name, first_name, patronymic_name, passwd, confirmed_passwd, email, company, phone )
	{
		var reg = /[0-9a-z_]+@[0-9a-z_^.]+.[a-z]{2,3}/i 
		if( passwd != confirmed_passwd)
		{
			alert("Поле \"пароль\" заполнено неверно. Повторите, пожалуйста, ввод.");
			return;
		}
		else if( last_name.length == 0 || first_name.length == 0 || patronymic_name.length == 0 )
		{
			alert("Поле \"Фамилия Имя Отчество\" обязательно для заполнения");
			return;
		}
		else if( reg.test(email) == true && passwd.length > 0 )
		{
			new Ajax.Request('/ajax/registration.php', {
				asynchronous: true,
				method: 'post',
				postBody: 'ajax_email=' + email + '&ajax_password=' + passwd + '&ajax_l_name=' + last_name +
							'&ajax_f_name=' + first_name + '&ajax_p_name=' + patronymic_name + 
							'&ajax_company=' + company + '&ajax_phone=' + phone,
				onSuccess: function (r) { 
					var regResponse = r.responseText;
					if( regResponse == '1')
					{
						document.getElementById('submit_button').click();
					}
					else if( regResponse == '0')
					{
						alert('Пользователь с таким e-mail уже зарегестрирован.');
					}
					else
					{
						alert('Данные введены некорректно. Пожалуйста повторите регистрацию.');
					}
				}
			});
		}
		else
		{
			alert('Некорректно введен логин/пароль.');
			return;
		}
	},
	
	Authorize: function( login, password )
	{
		var reg = /[0-9a-z_]+@[0-9a-z_^.]+.[a-z]{2,3}/i 
		if( reg.test(login) == true && password.length > 0 )
		{
			new Ajax.Request('/ajax/authorize.php', {
				asynchronous: true,
				method: 'post',
				postBody: 'ajax_login=' + login + '&ajax_password=' + password,
				onSuccess: function (r) { 
					var authResponse = r.responseText;
					if( authResponse == '0')
						alert('Неверный логин/пароль');
					if( authResponse == '1')
						alert('Неверный логин/пароль');
					if( authResponse == '2')
					{
						//alert('авторизован');
						document.getElementById('submit_button').click();
					}
				}
			});
		}
		else
		{
			alert('Неверный логин/пароль');
		}
	},
	
	IsAuthorized: function()
	{
		var Btn = document.getElementById('submit_btn');
		var Tag = document.getElementById('submit_form');
		if( Tag.addEventListener ) 
		{
			Tag.addEventListener('submit', function() {return false;}, true);
		} // Firefox или Opera
		else 
		{
			Tag.detachEvent( "onsubmit", Auth.ReturnFalse );
			Tag.attachEvent('onsubmit', Auth.ReturnFalse);
		} // MSIE
		
		new Ajax.Request('/ajax/is_authorized.php', {
			asynchronous: true,
			method: 'post',
			postBody: '',
			onSuccess: function (r) { 
				var authResponse = r.responseText;
				if( authResponse == 'yes')
				{
					Popup.hide();
					if( Tag.addEventListener ) 
					{
						Tag.removeEventListener('submit', function() {}, true);
						Btn.click();
					}
					else 
					{
						Tag.detachEvent( "onsubmit", Auth.ReturnFalse );
						Btn.click();
					} 
				}
				else
				{
					Popup.show();
				}
			}
		})
	},
	
	ReturnFalse: function()
	{
		return false;
	}
};