﻿String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g,"");
}
function txtMasterOnfocu()
{
    var txtkwd=document.getElementById('textfield1');  
    if(txtkwd.value.indexOf(' 请输入商品关键字')!=-1)
    {
        txtkwd.value='';
        txtkwd.style.color='#000';
    }
}
function textMasterFocusTitle()
{
    document.getElementById('textfield1').value=' 请输入商品关键字';
    document.getElementById('textfield1').style.color='#999';
}
function txtMasterBlur()
{
    var txtkwd=document.getElementById('textfield1');    
    if(txtkwd.value.trim()==""||txtkwd.value.length==0)
    {
        document.getElementById('textfield1').value=' 请输入商品关键字';
        document.getElementById('textfield1').style.color='#999';
    }   
}   
function SearchProduct()
{
    var txtfield=document.getElementById('textfield1');
    if(txtfield.value.length==0)
    {
    alert('请输入查询条件！'); 
    event.returnvalue=false;    
    }
    else if(txtfield.value.indexOf(' 请输入商品关键字')!=-1)
    {
    alert('请输入查询条件！');
    event.returnvalue=false;   
    }
    else
    { 
    location="p_search.aspx?name="  +txtfield.value.trim();
//     window.open ("p_search.aspx?name="+txtfield.value.trim(),"_blank","toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=yes,status=yes");       
    event.returnvalue=false; 
    }
}
function AddCartPdt(pid)
{
    var url="ChangeShopCartList.aspx?pid="+pid.toString ()+"&OprtType=3&Oprt=Add&count=1";
    ajax("GET",url,null,ProcessResponse);
    function ProcessResponse(){}
} 
function SubCartPdt(pid)
{
    var url="ChangeShopCartList.aspx?pid="+pid.toString ()+"&OprtType=3&Oprt=Sub&count=1";
    ajax("GET",url,null,ProcessResponse);
    function ProcessResponse(){}
}
function DeleteCartPdt(pid)
{
    var url="ChangeShopCartList.aspx?pid="+pid.toString ()+"&OprtType=4";
    ajax("GET",url,null,ProcessResponse);
    function ProcessResponse(){}
}
function ClearShopCart()
{
    if(confirm('确定要清空购物车？'))
    {
        var url="ChangeShopCartList.aspx?OprtType=5";
        ajax("GET",url,null,ProcessResponse);
        function ProcessResponse(){}  
        document.getElementById('YDCart').innerHTML="您的购物车中没有商品！"; 
        document.getElementById('txtCartCnt').value="0";
        document.getElementById('hdnCartCnt').value="0";
    }
}
function AddCartCount(pid,price)
{   
    var txt= document.all["cart"+pid.toString()];
    var rst=parseInt( txt.value)+1;
    var stock=parseInt( document.getElementById("hid"+pid.toString()).value);   //2009-5-5 ljw    判断购买数量是否大于库存数量
    if(stock==0||rst>stock)                                                     //判断购买数量是否大于库存数量
    {
        alert('库存数量不够，暂时不能购买！');         
    }
    else
    {
        txt.value=rst.toString();
        var oldCnt=parseInt(document.getElementById('hdnCartCnt').value);
        document.getElementById('txtCartCnt').value=oldCnt+1;
        document.getElementById('txtItemCnt').value=oldCnt+1;
        document.getElementById('hdnCartCnt').value=oldCnt+1;
        document.getElementById('cart'+pid.toString()).value=rst; 
        var tcnt= document.getElementById("txtCount"+pid.toString());     
        if(tcnt!=null)
        {
            tcnt.value=rst;
            document.getElementById('hidden'+pid.toString()).value=rst;   
        }
        var total=parseFloat(document.getElementById('txtCartTotal').value);
        total=total+price;
        document.getElementById('txtCartTotal').value=total;  
        var tTotal= document.getElementById('txtTotal');
        if(tTotal!=null)
        {
            tTotal.value=total;
        }          
        AddCartPdt(pid); 
        return txt;
    }
}
function SubCartCount(pid,price)
{
    var txt= document.all["cart"+pid.toString()];
    var rst=parseInt(txt.value)-1;
    if(rst>=0)
    {       
        if(rst==0)
        {
            if(confirm('确定删除该商品？'))
            {
                DelCartProduct(pid);
            }    
            else
            {
                rst=1;
            }        
        }
        else
        {
            var total=parseFloat(document.getElementById('txtCartTotal').value);
            total=total-price;        
            if(total<=0)
            {
                total=0;
            }
            document.getElementById('txtCartTotal').value=total;
            var tTotal= document.getElementById('txtTotal');
            if(tTotal!=null)
            {
                tTotal.value=total;
            }
        }
    }    
    if(rst<0)
    {
        rst=0;
    }
    txt.value=rst.toString(); 
    if(rst>0)
    {
        document.all["cart"+pid.toString()].value=rst;   
    }
    var oldCnt=parseInt(document.getElementById('hdnCartCnt').value);
    if(oldCnt>0)
    {        
        if(oldCnt>1)
        {
            document.getElementById('txtItemCnt').value=oldCnt-1;
            document.getElementById('txtCartCnt').value=oldCnt-1;
            document.getElementById('hdnCartCnt').value=oldCnt-1;    
        }        
    }
    else
    {
        document.getElementById('txtCartCnt').value=0;
        document.getElementById('txtItemCnt').value=0;
        document.getElementById('hdnCartCnt').value=0; 
    }
    var tcnt= document.getElementById("txtCount"+pid.toString());     
    if(tcnt!=null)
    {
        tcnt.value=rst;
        document.getElementById('hidden'+pid.toString()).value=rst;       
    }
    if(rst>0)
    {
        SubCartPdt(pid);
    }        
    return txt;    
}
function DelCartProduct(pid,count,price)
{                         
    DeleteCartPdt(pid);
    document.getElementById('div'+pid).style.display='none'; 
    var rst= parseInt( document.getElementById('txtItemCnt').value);
    document.getElementById('txtCartCnt').value=rst-1;
    document.getElementById('txtItemCnt').value=rst-1;
    var total=parseFloat(document.getElementById('txtCartTotal').value);
    total=total-price*count;
    document.getElementById('txtCartTotal').value=total;
    if(rst==1)
    {       
        document.getElementById('YDCart').innerHTML="您的购物车中没有商品！"; 
    }
} 