function pics(img,ref)
{
	document.getElementById(img).src = ref;
}

function openwin(img,w,h,title)
{
 var hWnd;
 if(hWnd!=null) hWnd.close();
 hWnd=window.open(img,"","toolbar=no,location=no,directories=no,resizable=yes,scrollbars=yes,left=100,top=100,width="+w+",height="+h);
 hWnd.document.open();
 hWnd.document.write("<html><head><title>"+title+"</title></head><body topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>");
 hWnd.document.write("<img src=\""+img+"\" width="+w+" height="+h+" onclick='window.close();'>");
 hWnd.document.write("</body></html>");
 hWnd.document.close();
}


function IEPngFix( pngimg, gifimg, width, height, unique_name )
{
	var code = '<img src="'+ gifimg +'" width="' + width + '" height="' + height + '" border="0" id="'+ unique_name +'" style="background:url('+pngimg+') no-repeat 0px 0px;" alt="'+unique_name+'"/>';
	if( navigator.userAgent.indexOf("Opera") > 1 ) document.write(code);
	else
		if( navigator.userAgent.indexOf("MSIE 6") > 1 ) document.write('<img src="'+gifimg+'" width="' + width + '" height="' + height + '" border="0" alt="'+unique_name+'" id="'+ unique_name +'" style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader (src=' + pngimg + ' , sizingMethod=crop );"/>');
		else
			if(navigator.userAgent.indexOf("MSIE 5") != -1) document.write(code);
			else document.write(code);
}

var count = 0;

function but_up_press(but, pack)
{
	num = but.attr('num');
	count = $('#co_'+pack+num).attr('value');
	dx = $('#d_'+pack+num).attr('value');
	count = count - 1 + 1 + parseInt(dx);
	if(count > 9998) count = 9999;
}

function but_down_press(but, pack)
{
	num = but.attr('num');
	count = $('#co_'+pack+num).attr('value');
	dx = $('#d_'+pack+num).attr('value');
	count = count - parseInt(dx);
	if(count<0) count = 0;
}



function init()
{
	$('.search_block input[type:text]').click(function()
	{
		if ($(this).attr('is_not_default') != 1)
		{
			$(this).val('');
			$(this).attr('is_not_default', 1);
		}
	});

	
	if( $('#yur').attr("checked") ) $('.yur_params').each(function(){ $(this).show(); });
	else $('.yur_params').each(function(){ $(this).hide(); });

	$('#yur').click(function()
	{
		if( $('#yur').attr("checked") ) $('.yur_params').each(function(){ $(this).show(); });
		else $('.yur_params').each(function(){ $(this).hide(); });
	});


	$('.only_nums').keypress(function(evt){
		 var charCode = (evt.which) ? evt.which : event.keyCode;
		 if (charCode!=13 && charCode!=9 && charCode!=8 && charCode!=46 && charCode!=36 && charCode!=35 && charCode!=37  && charCode!=39 && charCode!=0 && (charCode<48 || charCode>57) )
			 return false;
		 return true;
	});


//up, down, input
	$('.b_down').click(function()
	{
		but_down_press($(this), '');
		if(!$(this).hasClass('cart')) upd(num, '#co_', '#price_', '#sum_', '#tocart_block_')
			else upd2(num, '#co_', '#cart_price_', '#sum_', '#weight');
	});
	$('.b_up').click(function()
	{
		but_up_press($(this), '');
		if(!$(this).hasClass('cart')) upd(num, '#co_', '#price_', '#sum_', '#tocart_block_')
			else upd2(num, '#co_', '#cart_price_', '#sum_', '#weight');
	});
	$('.b_pack_down').click(function()
	{
		but_down_press($(this), 'pack_');
		if(!$(this).hasClass('cart')) upd(num, '#co_pack_', '#price_pack_', '#sum_pack_', '#tocart_block_pack_')
			else upd2(num, '#co_pack_', '#cart_price_pack_', '#sum_pack_' ,'#pack_weight');
	});
	$('.b_pack_up').click(function()
	{
		but_up_press($(this), 'pack_');
		if(!$(this).hasClass('cart')) upd(num, '#co_pack_', '#price_pack_', '#sum_pack_', '#tocart_block_pack_')
			else upd2(num, '#co_pack_', '#cart_price_pack_', '#sum_pack_', '#pack_weight');
	});
	$('.count').keyup(function()
	{
		num = $(this).attr('num');
		count = $(this).attr('value');
		if(!count) count = 0;
		if(!$(this).hasClass('cart')) upd(num, '#co_', '#price_', '#sum_', '#tocart_block_')
			else upd2(num, '#co_', '#cart_price_', '#sum_', '#weight');
	});
	$('.count_pack').keyup(function()
	{
		num = $(this).attr('num');
		count = $(this).attr('value');
		if(!count) count = 0;
		if(!$(this).hasClass('cart')) upd(num, '#co_pack_', '#price_pack_', '#sum_pack_', '#tocart_block_pack_')
			else upd2(num, '#co_pack_', '#cart_price_pack_', '#sum_pack_', '#pack_weight');
	});
// \up, down, input


	//to_cart
	$('.tocart').click(function()
	{
		$.ajax({
			url: "/inc/add_to_cart.php?prod_id="+$(this).attr("num")+"&count="+$(this).attr("count")+"&pack_type="+$(this).attr("pack_type"),
			cache: false,
			success: function(data){
				$("#cart_block").html(data);
			}
		});
	});
	// \ to_cart


	$('#b_save_spam_config').click(function()
	{
		s = "";
		$('.spam_cat').each(function(){
			if($(this).attr('checked')) s = s + $(this).attr('id') + ',';
		});

		$.ajax({
			url: "/inc/refresh_spam_block.php?action=save&checks="+s,
			cache: false,
			success: function(data){
				$("#spam_block").html(data);
				init();
			}
		});
	});


}



// пересчитывает стоимость товара в зависимости от кол-ва, прячет / показывает кнопку "в корзину"
function upd(id, cound_pref, price_pref, sum_pref, tocart_pref)
{
	$(cound_pref+num).attr('value', count);
	price = count * $(price_pref+num).html();
	$(sum_pref+num).html(price.toFixed(2));
	if(cound_pref == "#co_pack_") pack_type = "pack"; else pack_type = "piece";

	if(count)
	{
		$(tocart_pref+id).attr("num",id);
		$(tocart_pref+id).attr("count",count);
		$(tocart_pref+id).show();
	}
	else
		{
			$(tocart_pref+id).attr("num",0);
			$(tocart_pref+id).attr("count",0);
			$(tocart_pref+id).hide();
		}
}


// пересчитывает стоимость товара в зависимости от кол-ва, прячет / показывает кнопку "в корзину"
function upd2(num, cound_pref, price_pref, sum_pref, weight_pref)
{
	$(cound_pref+num).attr('value', count);
	price = count * $(price_pref+num).html();
	$(sum_pref+num).html(price.toFixed(2));
	if(cound_pref == "#co_pack_") pack_type = "pack"; else pack_type = "piece";
	if(weight_pref=="#weight")
	{
		cw = $('#crystal_weight'+num).attr('value') * count;
		$('#weight'+num).attr('value', cw);
	}
	else
	{
		cw2 = $('#crystal_weight'+num).val() * count * $('#quantity_in_pack'+num).val();
		$('#pack_weight'+num).val(cw2);
	}
	Coco();
}


var focused = false;
var focused2 = false;
var focused3 = false;


$(document).ready(function(e){
	init();

	$('input.inp_login').focus(function(){
		if(!focused)
		{
			$(this).val('');
			focused = true;
		}
	});
	$('input.inp_pasw').focus(function(){
		if(!focused2)
		{
			$(this).val('');
			focused2 = true;
		}
	});
	$('input.inp_search').focus(function(){
		if(!focused3)
		{
			$(this).val('');
			focused3 = true;
		}
	});


	$('input[type=radio].delivery_type').click(function()
	{
		Coco2();
	});

	$('#transport_city').click(function()
	{
		$('input[value=transport].delivery_type').attr('checked','checked');
		Coco2();
	});



});



//======
function sizes_selects(prod_id, color) {
	$.ajax({
			url: "/inc/refresh_sizes_colors_block.php?prod_id="+prod_id+"&color="+color,
			cache: false,
			success: update_sizes_block
	});
}
function update_sizes_block(data) {
	$("#sizes_block").html(data);
}

//======
function sizes_table(prod_id, color, size) {
	$.ajax({
			url: "/inc/refresh_order_table_block.php?prod_id="+prod_id+"&color="+color+"&size="+size,
			cache: false,
			success: update_order_table_block
	});
}
function update_order_table_block(data) {
	$("#order_table_block").html(data);
	init();
}



////////////////------считает и выводит общую цену, количество единиц товара и вес
function Coco()
{
	var Amount = 0;
	var Price = 0;
	var Weight = 0;
	var ss = "";
	var c = 0;

	InputsCollection = document.getElementsByTagName("INPUT");

	for (i=0; i<InputsCollection.length; i+=1)
	{
		CurrentInputID = InputsCollection[i].id;
		ss = "";
		c = 0;
		if (CurrentInputID.indexOf('co_pack_') >= 0)
		{
			ss = new String(CurrentInputID); //определяем id инпута
			ss = ss.substr(8,ss.length);
			c = $('#co_pack_'+ss).val();
			p = $('#cart_price_pack_inp_'+ss).val() * c;
			Weight = Weight + parseFloat($('#pack_weight'+ss).val());
			//alert('pack_weight'+ss+parseFloat($('#pack_weight'+ss).val()));
		}
		else
		if (CurrentInputID.indexOf('co_') >= 0)
		{
			ss = new String(CurrentInputID); //определяем id инпута
			ss = ss.substr(3,ss.length);
			c = $('#co_'+ss).val();
			p = $('#cart_price_inp_'+ss).val() * c;
			Weight = Weight + parseFloat($('#weight'+ss).val());
			//alert('weight'+ss+parseFloat($('#weight'+ss).val()));
		}
		if(ss != "")
		{
			Amount += parseInt(c);
			Price += p;
			//alert(Weight);
		}
	}

	Price = Price.toFixed(3);
	//$('#price_block').html(Price);
	//$('#amount_block').html(Amount);
	//alert(Weight);

	$.ajax({
		url: "/inc/refresh_pre_price_block.php?price="+Price+"&count="+Amount+"&weight="+Weight,
		cache: false,
		success: function(data){
			$("#pre_price_block").html(data);
			Coco2();
		}
	});

}


////////////////------считает и выводит общую цену с учетом доставки
function Coco2()
{
	preprice = $('#preprice').val();
	$('input[type=radio].delivery_type').each(function(){
		if($(this).attr('checked'))
		{
			$.ajax({
			url: "/inc/refresh_total_price_block.php?preprice="+preprice+"&delivery_type="+($(this).val())+"&transport_city="+$('#transport_city').val()+"&weight="+$('#order_weight').val(),
			cache: false,
			success: function(data){
				$("#total_price_block").html(data);
			}
			});
		}
	});
}



function post_to_del()
{
	document.getElementById("show_table").value=1;
	document.cartlist.submit();
}

function check_count(id, count)
{
	var c = parseInt(document.getElementById("count_id"+id).value);
	if( document.getElementById("kolonfirm_id"+id).value < c)
	{
		document.getElementById("count_id"+id).value = count;
		alert("Количество товара недопустимо, для заказа необходимо связаться с менеджером");
	}
}
///////


function go_to(link)
{
	document.location=link;
}



