$(document).ready(function(){
                $("#cartno").click(function(){
                    $("#cartyesno").hide();
                    $("#cartofuzz").hide();
                    
                });

    
    
	$(".quantity").keypress(function(event)
		{
			if (event.keyCode==13)
			{
                
				order();
			}
		}
	);

    $(".quantity1").keypress(function(event)
		{
			if (event.keyCode==13)
			{
                recount();
			}
		}
	);

    $(".refresh").click(recount);

    $(".delete").click(deleteGood);

    $(".button").click(function(){
        if($("#q"+$(this).attr("id")).val() == "0"){
            $("#q"+$(this).attr("id")).val("1");
        }
    }
    );


    $(".button").click(order);


    $(".quantity").blur(function()
    {
        if($(this).val()=="")
            $(this).val("0");
    }
    );

    $(".quantity").click(function() //Юзабилити! :)
		{
			if($(this).val()=="0")
				$(this).val("");
		}
	);

    //$("tr.line:odd").css("background-color","#eeeeee");

    $(".refresh,.delete").hover(function()
    {
        szURL = document.URL.replace(/(\\|\/)[^\\\/]*$/, '/');
        componentList = szURL.split('/');
        url = componentList[0]+"//"+componentList[2];
        if(componentList[2]=="localhost"){url += "/"+componentList[3];}


        $(".refresh,.delete").css("cursor","pointer");
        if($(this).attr("class")=="delete")
            {
                $(this).attr("src",url+"/images/shop/002.gif");
            } else $(this).attr("src",url+"/images/shop/001.gif");
    },
    function()
    {
        szURL = document.URL.replace(/(\\|\/)[^\\\/]*$/, '/');
        componentList = szURL.split('/');
        url = componentList[0]+"//"+componentList[2];
        if(componentList[2]=="localhost"){url += "/"+componentList[3];}

        $(".refresh,.delete").css("cursor","default");
        if($(this).attr("class")=="delete")
            {
                $(this).attr("src",url+"/images/shop/002b.gif");
            } else $(this).attr("src",url+"/images/shop/001b.gif");
    }); //hover ends

});

function order()
{
	$(".quantity").attr("disabled", "disabled");
	$(".button").attr("disabled", "disabled");

	var sku = new Array();
	var quantity = new Array();

	var num = ($(".quantity")).length;

	for(i=0;i<num;i++)
	{
		quantValue = $(".quantity:eq("+i+")").val();

		if(quantValue > 0)
		{
			quantity[quantity.length] = quantValue;
			skuValue = $(".sku:eq("+i+")").val();
			sku[sku.length] = skuValue;
		}
	}

	quantity=serialize(quantity);
	sku=serialize(sku);

        szURL = document.URL.replace(/(\\|\/)[^\\\/]*$/, '/');
        componentList = szURL.split('/');
        //szDocument = componentList[componentList.length-1];
        //documentFilename = szDocument.split('.');
        //szDocumentTopic = documentFilename[0];
        //szNewURL = "http://www.google.com/search?q=" + szDocumentTopic;
        //window.location.href=szNewURL;
        //window.location.reload();
        url = componentList[0]+"//"+componentList[2];
        if(componentList[2]=="localhost"){url += "/"+componentList[3];}
        //alert(url);


	$.ajax({
		type: "POST",
		url: url+"/includes/m_cart_a.php",
		data: "sku="+escape(sku)+"&quantity="+escape(quantity)+"&do=order",
		success: cartAddResponse
	});
}

function recount()
{
    $(".quantity1").attr("disabled", "disabled");
    var sku = new Array();
	var quantity = new Array();

	num = ($(".quantity1")).length;
    
	for(i=0;i<num;i++)
	{
		quantValue = $(".quantity1:eq("+i+")").val();
		quantity[quantity.length] = quantValue;
		skuValue = $(".sku:eq("+i+")").val();
		sku[sku.length] = skuValue;
	}

	quantity=serialize(quantity);
	sku=serialize(sku);

        szURL = document.URL.replace(/(\\|\/)[^\\\/]*$/, '/');
        componentList = szURL.split('/');
        //szDocument = componentList[componentList.length-1];
        //documentFilename = szDocument.split('.');
        //szDocumentTopic = documentFilename[0];
        //szNewURL = "http://www.google.com/search?q=" + szDocumentTopic;
        //window.location.href=szNewURL;
        //window.location.reload();
        url = componentList[0]+"//"+componentList[2];
        if(componentList[2]=="localhost"){url += "/"+componentList[3];}
        //alert(url);


	$.ajax({
		type: "POST",
		url: url+"/includes/m_cart_a.php",
		data: "sku="+escape(sku)+"&quantity="+escape(quantity)+"&do=recount",
		success: cartRecountResponse
	});
}

function removeTr(obj) //удаление строки в таблице корзины
{
    var link=obj.parent().parent();
    link.css('background-color','#ff6666');
    link.fadeOut();
    obj.val("0");

}

function deleteGood() //удаление товара из корзины
{
    $(".quantity1").attr("disabled", "disabled");
	var sku = $(this).parent().siblings().children(".sku").val();

        szURL = document.URL.replace(/(\\|\/)[^\\\/]*$/, '/');
        componentList = szURL.split('/');
        //szDocument = componentList[componentList.length-1];
        //documentFilename = szDocument.split('.');
        //szDocumentTopic = documentFilename[0];
        //szNewURL = "http://www.google.com/search?q=" + szDocumentTopic;
        //window.location.href=szNewURL;
        //window.location.reload();
        url = componentList[0]+"//"+componentList[2];
        if(componentList[2]=="localhost"){url += "/"+componentList[3];}
        //alert(url);

        $.ajax({
		type: "POST",
		url: url+"/includes/m_cart_a.php",
		data: "sku="+escape(sku)+"&do=delete",
		success: cartDeleteResponse
	});

    removeTr($(this));
    //recount();
}

function showNewPrice(textVal) //отображение новой цены
{
    var tmp = textVal.split(';');
	var tovar = tmp[0];
	var summa = tmp[1];
        var discount = tmp[2];

if(!discount) discount = 0;

	$("#tovar").text(tovar);
	$("#summa").text(summa);
        $("#discount").text(discount);
        $("#summa_discount").text(summa*(100 - discount)/100);
        

    return summa; //она нам может понадобиться
}

function cartDeleteResponse(response)
{
    var summa = showNewPrice(response); //обновляем цену и кол-во в малой корзине
	
	if(summa == 0)
	{
		$("#cart_content").slideUp();
		$("#cart_content").after("Ваша корзина пуста. ").remove();
	}
	else 
	{
		$("#summa_cart").text(summa);
		$(".quantity1").attr("disabled", "");
	}
        
}

function cartAddResponse(response)
{
            $("#cartyesno").show();
            $("#cartofuzz").show();

    
	showNewPrice(response); //обновляем цену и кол-во в малой корзине

	$(".quantity").attr("disabled", "");
	$(".button").attr("disabled", "");
    
    $(".quantity").val("0");
}

function cartRecountResponse(response)
{
    var num = ($(".quantity1")).length;

    for(var i=0;i<num;i++)
    {
        var quantVal = $(".quantity1:eq("+i+")").val();

        if(quantVal==0) //удаляем строки с нулевым количеством
        {
            removeTr($(".quantity1:eq("+i+")"));
        }
        else
        {
            //далее считаем новую сумму по каждому товару
            var price = $(".price:eq("+i+")").text();
            var newSum = Number(quantVal)*Number(price);
            $(".sum:eq("+i+")").text(newSum.toFixed(2));
        }
    }

    cartDeleteResponse(response);
}

	



