var updateAjax=null;
var removeAjax=null;
var clearAjax = null;
var isLoaded = false;

function initAjax()
{
    updateAjax=new Ajax("ShoppingCartUpdate.aspx");
    removeAjax=new Ajax("ShoppingCartRemove.aspx");
    clearAjax=new Ajax("ShoppingCartClear.aspx");
    isLoaded = true;
}



//
//buy product deal  
//
//update
//txtProductCount num text...dealType:'buy','change'
function txtProductCountUpdate(txtProductCount,dealType)
{
    var sProductCount=txtProductCount.value.trim();// new product count
	var txtOproductCount=txtProductCount.nextSibling;//old hidden product control
	var txtProductType = txtProductCount.nextSibling.nextSibling;//get product type
	var oProductCount=txtOproductCount.value.trim();//old hidden product count

	if(!isLoaded)//no load
	{ 
	    txtProductCount.value=oProductCount;//txt add old value
	    return;
	}

	 if(dealType!='buy' && dealType!='change')
    {
       txtProductCount.value=oProductCount;//txt add old value
        return;
    }
	
	var rowIndex=txtProductCount.parentNode.parentNode.rowIndex;
	var productId=null;
	var unitprice=0;
	var unitinte=0;
	if(dealType=='buy')
	{
	   productId=tbBuy.rows[rowIndex].cells[0].childNodes[0].value;
	   unitprice=tbBuy.rows[rowIndex].cells[4].innerText;
	}
	else if(dealType=='change')
	{
	   productId=tbChange.rows[rowIndex].cells[0].childNodes[0].value;
	   unitprice=tbChange.rows[rowIndex].cells[5].innerText;
	   unitinte = tbChange.rows[rowIndex].cells[4].innerText;
	}
	
	if(sProductCount==oProductCount)//new=old return
		return;
    var iProductCount=parseInt(sProductCount);

	if(sProductCount!=iProductCount || iProductCount<1)
	{		
		txtProductCount.value=oProductCount;
		return;
	}   

	updateAjax.OnReading
    (
        function()
        {
            isLoaded = false;
        }
    );
     updateAjax.OnSucceed
    (
        
	    function(result)
	    {
	        isLoaded = true;
		    if(result!=null && result==1 )
            {
                txtProductCount.value=sProductCount;
				txtOproductCount.value = sProductCount;
                //deal unit price
                if(dealType=='buy')
	            {
	               tbBuy.rows[rowIndex].cells[6].innerHTML = parseFloat(unitprice) * parseFloat(sProductCount);
	               //重新计算合计
	               var totmoney=0;
	               var tbBuyCount = tbBuy.rows.length;
	               for(ib=1;ib<tbBuyCount;ib++)
	               {
	                   totmoney+= parseFloat(tbBuy.rows[ib].cells[6].innerText);
	               }
	               var buy_div=$("buy_div");
                   if(buy_div!=null)
                   {
                       buy_div.innerHTML="合计：￥"+ totmoney +" 元&nbsp;";
                   }
	            }
	            else if(dealType=='change')
	            {
	               productId=tbChange.rows[rowIndex].cells[0].childNodes[0].value;
	               unitprice=tbChange.rows[rowIndex].cells[5].innerText;
	               unitinte = tbChange.rows[rowIndex].cells[4].innerText;
				  
	               tbChange.rows[rowIndex].cells[7].innerText="";
	               if(parseInt(unitinte)>0)
	               {
	                  tbChange.rows[rowIndex].cells[7].innerHTML = parseFloat(unitinte) * parseFloat(sProductCount)+"积分";
	               }
	               if(parseInt(unitprice)>0)
	               {
	                  tbChange.rows[rowIndex].cells[7].innerHTML = tbChange.rows[rowIndex].cells[7].innerText + "+￥" +  parseFloat(unitprice)* parseFloat(sProductCount);
	               }
	                //重新计算合计
	               var totmoney=0;
	               var totintegral=0;
				   var unitnum=0;
	               var tbChangeCount = tbChange.rows.length;
				
	               for(ic=1;ic<tbChangeCount;ic++)
	               { 
					   unitnum = tbChange.rows[ic].cells[6].children[0].value;
	                   totmoney+= parseFloat(tbChange.rows[ic].cells[5].innerText)*parseFloat(unitnum);
	                   totintegral+= parseFloat(tbChange.rows[ic].cells[4].innerText)*parseFloat(unitnum);
					   
	               }
	               var change_div=$("change_div");
                   if(change_div!=null)
                   {
                       change_div.innerHTML="合计："+ totintegral +"积分+￥"+ totmoney +" 元&nbsp;";
                   }
	            }
            }
            else
            {
            	txtProductCount.value=oProductCount;
            }
	    }
    );
     updateAjax.OnTimeout
    (
	    function()
	    {
		    alert("网络连接超时！");
		    location.reload();
	    }
    );
    var sendData="ProductID="+escape(productId)+"&ProductCount="+sProductCount+"&ProductType="+txtProductType.value;
    updateAjax.invokeServer(sendData,'POST');
}

//delete
function RemoveProductFromCard(removeObj,dealType){
    if(!isLoaded) return;
	//var confirm_click =function()
	//{
        removeAjax.OnReading
        (
            function()
            {
                isLoaded = false;
            }
        );
	    if(dealType!='buy' && dealType!='change')
	    {
	        return;
	    }
	    var rowIndex=removeObj.parentNode.parentNode.rowIndex;
	    var productId=null;
	    if(dealType=='buy')
	    {
	       productId=tbBuy.rows[rowIndex].cells[0].childNodes[0].value;
	    }
	    else if(dealType=='change')
	    {
	       productId=tbChange.rows[rowIndex].cells[0].childNodes[0].value;
	    }
	    var txtProductType;
	    
	    if(dealType=='buy') 
	        txtProductType= tbBuy.rows[rowIndex].cells[5].childNodes[2].value;//get product type
	    else
	        txtProductType= tbChange.rows[rowIndex].cells[6].childNodes[2].value;//get product type

	    removeAjax.OnSucceed
	    (
		    function(result)
		    {
		        isLoaded = true;	     
                if(result!=null && result==1)
                {
                    //从购物商品列表中删除 
                    if(dealType=='buy')
	                {
	                   tbBuy.deleteRow(rowIndex);  
	                    //重新计算合计
	                   var totmoney=0;
	                   var tbBuyCount = tbBuy.rows.length;
	                   for(ib=1;ib<tbBuyCount;ib++)
	                   {
	                       totmoney+= parseFloat(tbBuy.rows[ib].cells[6].innerText);
	                   }
	                   var buy_div=$("buy_div");
                       if(buy_div!=null)
                       {
                           buy_div.innerHTML="合计：￥"+ totmoney +" 元&nbsp;";
                       }
                       if(tbBuy.rows.length==1)
	                    {
	                        var buy_div=$("buy_div");
	                        if(buy_div!=null)
	                        {
                            buy_div.innerHTML="";
                            }
	                    }
	                }
	                else if(dealType=='change')
	                {
	                   tbChange.deleteRow(rowIndex);
	                    //重新计算合计
	                   var totmoney=0;
	                   var totintegral=0;
	                   var totnum=0;
	                   var tbChangeCount = tbChange.rows.length;
	                   for(ic=1;ic<tbChangeCount;ic++)
	                   {
                           totnum = tbChange.rows[ic].cells[6].children[0].value;	                   
	                       totmoney+= parseFloat(tbChange.rows[ic].cells[5].innerText)*parseFloat(totnum);
	                       totintegral+= parseFloat(tbChange.rows[ic].cells[4].innerText)*parseFloat(totnum);					   
	                   }
	                   var change_div=$("change_div");
                       if(change_div!=null)
                       {
                           change_div.innerHTML="合计："+ totintegral +"积分+￥"+ totmoney +" 元&nbsp;";
                       }  
                       if(tbChange.rows.length==1)
	                    {
                            var change_div=$("change_div");
                            if(change_div!=null)
                            {
                            change_div.innerHTML="";   
                            } 
	                    }
	                }                 
	                
	                           
                }
                else
                {
                    alert("删除失败！");
                }
                
		    }
	    );
	    
	     removeAjax.OnTimeout
        (
	        function()
	        {
		        alert("网络连接超时！");
		        location.reload();
	        }
        );
	    
	    var sendData="ProductID="+escape(productId)+"&ProductType="+txtProductType;
	    removeAjax.invokeServer(sendData,'POST');
	// }
}


function ClearShoppingCart()
{
    clearAjax.OnReading
    (
        function()
        {
            isLoaded = false;
        }
    );
     clearAjax.OnSucceed
    (
        
	    function(result)
	    {
	        isLoaded = true;
		    if(result!=null && result==1 )
            {
                //清空页面table
                //tbBuy
                //tbChange
                var buytbody = tbBuy.children[0];
                var changetbody = tbChange.children[0];
                var buytitletr = buytbody.children[0];
                var changetitletr = changetbody.children[0];
                while(buytitletr.nextSibling!=null)
                {
                    buytbody.removeChild(buytitletr.nextSibling);
                }
                while(changetitletr.nextSibling!=null)
                {
                    changetbody.removeChild(changetitletr.nextSibling);
                }
               var change_div=$("change_div");
               var buy_div=$("buy_div");
               if(change_div!=null)
	           {
                   change_div.innerHTML="";
               }
               if(buy_div!=null)
	           {
                   buy_div.innerHTML="";
               }
                
            }
            else
            {
            	alert("清空失败!");
            }
	    }
    );
     clearAjax.OnTimeout
    (
	    function()
	    {
		    alert("网络连接超时！");
	    }
    );
    clearAjax.invokeServer('','POST');
}

