/***********************/
function show_loader(div, text)
{
		$('#'+div).html(text);
}
/***********************/
function ajax_start()
{
		$('#loading_grey').fadeIn('slow');
		$('#loading_text').fadeIn('slow');
	$('#mainmenu').hide('slow');
}

/***********************/
function ajax_stop()
{
		$('#loading_grey').fadeOut('slow');
		$('#loading_text').fadeOut('slow');
}
/***********************/
function collapse_menu() {
	$('#cats ul').hide();
}
/***********************/
function set_title(title)
{
		$('#page_title').html(title);
}
/***********************/
function view_cat(cat_id, pn, pp) {
	window.location = '/index.php?page=cat&id='+cat_id+'&pp='+pp+'&pn='+pn;
}
/***********************/
function add_to_cart()
{

	var reg = /^([0-9]+)$/;
		if ( reg.test($('#quant').val()) == false ){ alert("Недопустимый формат количества товаров!"); return; }
	$.post( '/index.php',
					{
					'query': 'add_to_cart',
				'id' : $('#id').val(),
				'quant' : $('#quant').val(),
				'price' : $('#price').val()
			},
			function(data){ if ( data == "OK!" ){ get_cart_info(); cart_transfer('transfer_div', 'cart_info'); } else { alert("Ошибка добавления товара"); } }
			//function(data){ alert(data); }
				);

}
/***********************/
function to_cart_from_list(id)
{

	var reg = /^([0-9]+)$/;
		if ( reg.test($('#quant_'+id).val()) == false ){ alert("Недопустимый формат количества товаров!"); return; }
	$.post( '/index.php',
					{
					'query': 'add_to_cart',
				'id' : id,
				'quant' : $('#quant_'+id).val(),
				'price' : $('#price_'+id).val()
			},
			function(data){ if ( data == "OK!" ){ get_cart_info(); cart_transfer('item_holder_'+id, 'cart_info'); } else { alert("Ошибка добавления товара"); } }
			//function(data){ alert(data); }
				);

}
/***********************/
function cart_transfer(source, dest)
{
		$("#"+source).effect("transfer", { to: "#"+dest }, 1000);
}
/***********************/
function get_cart_info() {
		$('#cart_info').html("Загрузка данных...");
	$.post('/index.php', {'query': 'get_cart_info'}, function(data){$('#cart_info').html(data);});
}
/***********************/
function checkout()
{
		ajax_start();
	$.post('/index.php', {'query': 'checkout'}, function(data){ ajax_stop();
		if ( data == 'NOT_LOGGED' ){ window.location = '/index.php?page=enter'; }
		else { window.location='/index.php?page=thanks'; }
		});
}
/***********************/
function login()
{
	ajax_start();
	$.post('/index.php', {'query': 'login', 'email': $('#login_email').val(), 'password': $('#login_pass').val()}, function(data){
			if (data == 'OK2') { checkout(); }
			else if ( data != 'OK!' ){ ajax_stop(); $('#login_error').html(data); $('#login_error').show(); }
			else { window.location = '/index.php'; }
		}
	);
}
/***********************/
function logout()
{
		ajax_start();
	$.post('/index.php', {'query': 'logout'}, function(data){ ajax_stop(); window.location = '/index.php';});
}
/***********************/
function register()
{
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if ( reg.test($('#email').val()) == false ){ $('#register_error').html("Неправильный формат почтового адреса"); $('#register_error').show(); return; }
	if ( $('#password').val() == "" ){ $('#register_error').html("Необходимо указать пароль!"); $('#register_error').show(); return; }
	if ( $('#password').val() != $('#password2').val() ){ $('#register_error').html("Несовпадение паролей!"); $('#register_error').show(); return; }
	ajax_start();
	$.post('/index.php',
				{
				'query': 'register',
			'email': $('#email').val(),
			'password': $('#password').val(),
			'password2': $('#password2').val(),
			'name': $('#name').val(),
			'phone': $('#phone').val(),
			'city': $('#city').val()
			}, function(data){ ajax_stop();
			if (data == 'OK2') { checkout(); }
			else if ( data != 'OK!' ){ $('#register_error').html(data); $('#register_error').show(); }
			else { window.location = '/index.php?page=cart'; }
		}
	);

}
/***********************/
function qregister()
{
		ajax_start();
	$.post('/index.php',
				{
				'query': 'qregister',
			'email': $('#email').val(),
			'name': $('#name').val(),
			'phone': $('#phone').val(),
			'city': $('#city').val()
			}, function(data){ ajax_stop();
			if (data == 'OK2') { checkout(); }
			else if ( data != 'OK!' ){ $('#register_error').html(data); $('#register_error').show(); }
			else { window.location = '/index.php?page=cart'; }
		}
	);

}
/***********************/
function update_profile()
{
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if ( reg.test($('#email').val()) == false ){ $('#register_error').html("Неправильный формат почтового адреса"); $('#register_error').show(); return; }
	if ( $('#password').val() != $('#password2').val() ){ $('#register_error').html("Несовпадение паролей!"); $('#register_error').show(); return; }
	ajax_start();
	$.post('/index.php',
				{
				'id' : $('#id').val(),
			'query': 'update_profile',
			'email': $('#email').val(),
			'password': $('#password').val(),
			'password2': $('#password2').val(),
			'name': $('#name').val(),
			'phone': $('#phone').val(),
			'city': $('#city').val()
			}, function(data){
					if ( data != 'OK!' ){ ajax_stop(); $('#register_error').html(data); $('#register_error').show(); }
			else { alert("Сохранено"); window.location = '/index.php';}
		}
	);
}
/***********************/
function get_user_info()
{
		$('#user_info').html("Загрузка данных...");
	$.post('/index.php', {'query': 'get_user_info'}, function(data){$('#user_info').html(data);});
}
/***********************/
function del_cart_item(id)
{
	ajax_start();
	$.post('/index.php', {'query': 'del_cart_item', 'id': id}, function(data){get_cart_info(); reload_cart();});

}
/***********************/
function reload_cart() {
	$('#content').html("Загрузка данных...");
	$.post('/index.php', {'query': 'cart'}, function(data){$('#content').html(data);});
}
//**************************//
function switch_photo(element, dest)
{
		var pic = element.title, id=element.id;
	var big = document.getElementById(dest);

	big.src = "/pics/img_preloader.gif";
	big.src = "/pics/medium/" + pic;
	$('#other_pics td').css({'border' : '1px solid black'});
	$('#td_'+id).css({'border' : '1px solid red'})
}
//**************************//
function show_gal()
{
		$('#gal_grey').fadeIn('slow');
	$('#gal_content').fadeIn('slow');
}
//**************************//
function warranty_add() {
	$.post('/index.php',
				{
					'query': 'add_warranty',
					'recieve_date' : $('#recieve_date').val(),
					'item_id' : $('#item_id').val(),
					'user_name' : $('#user_name').val(),
					'cat_id' : $('#cat_id').val(),
					'sernom' : $('#item_serial').val(),
					'zachet_price' : $('#zachet_price').val(),
					'complect' : $('#complect').val(),
					'defect' : $('#defect').val(),
					'buy_date' : $('#buy_date').val(),
					'guarant_left' : $('#guarant_left').val(),
					'defect_state' : $('#defect_state').val(),
					'return_type' : $('#return_type').val(),
					'comments' : $('#comments').val()
				}, function(data){
					if ( data != 'OK!' ){ alert(data) }
			else { alert("Сохранено"); window.location = '/index.php';}
		}
	);
}
//**************************//
function show_popup(id) {
	window.open("/index.php?page=item_popup&id="+id, "Подробности", "width=600,height=400,scrollbars=yes,resizable=no,status=no,menubar=no,location=no,titlebar=no");
}
//**************************//
function checkEnter(e)
{
	var characterCode = 0;
		if(window.event) // IE
		{
				characterCode = e.keyCode;
		}
		else if(e.which) // Netscape/Firefox/Opera
		{
				characterCode = e.which;
		}

		if (characterCode==13){ login(); }
		return true;
}
