$(document).ready (function()
{
	loadAjaxContent('/user/?mode=login&'+Math.round(Math.random()*10000000), 'loginPlace');
});
function find_form(id) {
	if ($('#f'+id+' #price_before_new').val()!='' && $('#f'+id+' #price_after_new').val()!=''){
	var price_b = $('#f'+id+' #price_before_new').val()/$('#f'+id+' #main_course').val();
	
	if (price_b < 1) {
		$('#f'+id+' #price_before').attr('value',price_b);
	} else {
		$('#f'+id+' #price_before').attr('value',Math.round(price_b));
	}
	
	var price_a = $('#f'+id+' #price_after_new').val()/$('#f'+id+' #main_course').val();
	
	if (price_a < 1) {
		$('#f'+id+' #price_after').attr('value',price_a);
	} else {
		$('#f'+id+' #price_after').attr('value',Math.round(price_a) );
	}
	
	return true;
}
}

function checkaddress() {
	if($('#addreadd input[name=attr_street_185]').val()!='' && $('#addreadd input[name=attr_house_175]').val()!='')
	{
	return true;
	}
	else {
		alert('Заполните пожалуйста поля адреса!');
		return false;
		}
}
function add_item(data_id)
{
	var url_1='/shcart/?data_id='+data_id+'&step=0&mode=add';
	window.top.frames["win_basket"].location = url_1;

//	var url_1='/shcart/?data_id='+data_id+'&step=2&nstep=2&mode=add';
//	document.location=url_1;

	CreateShMess();
}


function log_out()
{
	ht = document.getElementsByTagName("html");
	//ht[0].style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)"; 
	ht[0].style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
	if (confirm('Вы действительно хотите выйти?'))
	{
		return true;
	}
	else
	{
		ht[0].style.filter = "";
		return false;
	}
}


toMesHide = null;
tmSh = null;



function CreateShMess()
{
	removeShMess();
	message = "Товар добавлен в корзину";
	var text = document.createTextNode(message);

	var elem = document.createElement("div");

	elem.setAttribute('id', 'fixme');
	elem.setAttribute('onclick', 'removeShMess();');

	elem.appendChild(text);

	document.getElementsByTagName("body")[0].appendChild(elem);

	toMesHide = elem;

	tmSh = window.setTimeout(removeShMess, 3000);

}



function removeShMess()
{
	if (tmSh)	clearTimeout(tmSh);
	if (toMesHide)
	{
		document.getElementsByTagName("body")[0].removeChild(toMesHide);
		toMesHide = null;
	}
}

		function go_shcart()
		{	
			if (window.frames['win_basket'])
			{
				if(navigator.appName=="Netscape")
				{
					parent.document.location.href="/shcart/?step=2&amp;nstep=2&amp;currency_id="+window.frames['win_basket'].document.shcart.currency_id.value+"&amp;url="+escape(parent.document.location.href);
				}
				else
				{
					parent.document.location.href="/shcart/?step=2&amp;nstep=2&amp;currency_id="+document.frames['win_basket'].document.shcart.currency_id.value+"&amp;url="+escape(parent.document.location.href);
				}
			}
			else
			{
				document.location.href="/shcart/?step=2&amp;nstep=2";
			}
		}

		/*
		---------------------------------------------------------
		--------------- Авторизация пользователя ----------------
		---------------------------------------------------------
		*/


		/* загрузка блока пользователя ajax */
		function loadAjaxContent (content, place) {
			if($('#'+place).length) {
				$.ajax({
					url: content,
					dataType : "html",
					success: function (data) {
						$('#loginPlace').css('background','none');
						$(data).appendTo($('#'+place));
					}
				})
			}
		}

		/* Авторизация пользователя через блок входа */
		function shopLogin() {
			$('<div id="loading"></div>').appendTo($('.newLine'));
			$.post('/user/', {
				mode : $('#log_mode').val(),
				type : $('#log_type').val(),
				email : $('#log_email').val(),
				password : $('#log_password').val()
				},
				function(data) {
					$('#loginPlace').html(data);
				}
			)
		}

		/* функции показывающие label для полей ввода в форме авторизации */
		function hideTitle(elem) {
			$(elem).hide();
			$(elem).next().focus();
		}

		function clean(elem) {
			$(elem).prev().hide();
		}

		function showTitle(elem) {
			if ($(elem).val() == '')
			{
				$(elem).prev().show();
			}
		}


		/* Регистрация */
		function shopReg(data) {

			var email2 = $('#reg_email').val();
			var pass = $('#reg_password').val();
			var pass2 = $('#reg_password2').val();

			emailTest = "^[_\\.0-9a-z-]+@([0-9a-z][0-9a-z_-]+\\.)+[a-z]{2,4}$";
			passTest = "^.{4,}";

			var errors = '';

			var regex_email = new RegExp(emailTest, 'i');
			var regex_pass = new RegExp(passTest);

			if (regex_email.test(email2) == false) {
				errors += '<li>e-mail</li>';
			}
			if (regex_pass.test(pass) == false) {
				errors += '<li>пароль</li>';
			}
			if (pass != pass2) {
				errors += '<li>подтверждение пароля</li>';
			}
			if (data != 1)
			{
				errors += '<li>число на изображении</li>';
			}
			if (errors != '')
			{
				$('#messageArea td').html('Не правильно заполнены следующие поля:<ul>'+errors+'</ul>');
				$('#messageArea').show();
				return false;
			}
			else {

				$.post('/user/', {
					mode : 'reg',
					email : email2,
					password : pass
					},
					function(data) {
						if (data == 0)
						{
							$('<p class="redEr">Такой логин уже существует</p>').appendTo($('#emailContainer'));
							return false;
						}
						else {
							document.location = '/user/?mode=reg&klwfjfwehfjkwjhef';
						}
					}
				)
			}
		}

		/* Проверка капчи */
		function checkCaptcha() {
			$.post('/captcha/captcha.php', {
				captcha : $('#captcha').val(),
				mode : 'check'
				}, shopReg
			)
			return false;
		}

		/* Востановление пароля */
		function restorePass() {
			$('#b-restButton').css('background','url(/pics/loading_white.gif) 118px center no-repeat');
			$.post('/user/', {
				mode : 'restore',
				email : $('#res_email').val()
				},
				function(data) {
					if (data == 0)
					{
						$('#emailContainer span').hide();
						$('.errors').show();
					}
					else {
						$('#emailContainer span').hide();
						$('.good').show();
					}
					$('#b-restButton').css('background','none');
				}
			)
			return false;
		}

		/* Установка выбранного адреса в качестве основного */
		function saveaddr() {
			$.post('/profile/', {
				mode : 'address',
				data_id : $('.radioBut:checked').attr('value'),
				action : 'update',
				attr_active : '1'
			},
			function(data) {
				document.location = '/profile/?mode=address';
			}
			)
		}

		/* Удаление адреса */
		function deleteaddr(d_id) {
			$.post('/profile/', {
				mode : 'address',
				essense_name : 'address',
				data_id : d_id,
				action : 'delete'
			},
			function(data) {
				document.location = '/profile/?mode=address';
			}
			)
		}

		/* ПоказатьСкрыть блок */
		function toggleBlock(elem) {
			$(elem).toggle();
			return false;
		}
		function main (section_popt) {
			$("#special").load("/"+section_popt+"/?cmd=special");
		}
