function AddToCart(obj)
{    
    var ppid = "";
    ppid = obj.parentNode.parentNode.parentNode.id;        /* product list */
    if(ppid != null && ppid.indexOf("div") > -1)
    {
        ppid = ppid.substring(3, ppid.length);            
    }
    ppid = "*cart*" + ppid;    
    callServerAddToCart(ppid);
}
function AddToCartProgress(ppid)
{
    if(ppid.indexOf("*cart*") > -1)
    {    
        ppid = ppid.substring(6, ppid.length);
        var obj = document.getElementById("div" + ppid);
        var progress;
        var child;     
        var child_count = obj.childNodes.length;
        /* product list */
        for(var i=0; i<child_count; i++)
        {
            if(obj.childNodes[i].className == "plistinfo")
            {       
                child = obj.childNodes[i];
                child_count = child.childNodes.length;
                                
                for(var j=0; j<child_count; j++)
                {
                    if(child.childNodes[j].className == "plistextend")
                    {
                        child = child.childNodes[j];
                        child_count = child.childNodes.length;
                        
                        for(var k=0; k<child_count; k++)
                        {
                            if(child.childNodes[k].id == "resultcart")
                            {
                                progress = child.childNodes[k];
                                progress.innerHTML = "<img alt='please wait' src='/images/progress.gif' /><font color='red'> please wait...</font>"; 
                            }
                        }
                    }
                }                  
            }
        } 
        /* product gallery */
        for(var i=0; i<child_count; i++)
        {
            if(obj.childNodes[i].className == "pgalleryextend")
            {
                child = obj.childNodes[i];
                child_count = child.childNodes.length;
                        
                for(var k=0; k<child_count; k++)
                {
                    if(child.childNodes[k].id == "resultcart")
                    {
                        progress = child.childNodes[k];
                        progress.innerHTML="<img alt='please wait' src='/images/progress.gif' />"; 
                    }
                }
            }
        }  
    }
}
function AddToCartProgressEnd(arg)
{
    var ppid = "";
    if(arg.indexOf("truecart") > -1)
    {
        ppid = arg.substring(8, arg.length);
    }
    if(arg.indexOf("falsecart") > -1)
    {
        ppid = arg.substring(9, arg.length);
    }
    
    var obj = document.getElementById("div" + ppid);
    var progress;
    var child;     
    var child_count = obj.childNodes.length;
    /* product list */
    for(var i=0; i<child_count; i++)
    {
        if(obj.childNodes[i].className == "plistinfo")
        {       
            child = obj.childNodes[i];
            child_count = child.childNodes.length;
                                
            for(var j=0; j<child_count; j++)
            {
                if(child.childNodes[j].className == "plistextend")
                {
                    child = child.childNodes[j];
                    child_count = child.childNodes.length;
                        
                    for(var k=0; k<child_count; k++)
                    {
                        if(child.childNodes[k].id == "resultcart")
                        {
                            progress = child.childNodes[k];
                            
                            if(arg.indexOf("truecart") > -1)
                            {
                                progress.innerHTML="<font color='red'> add succeeded. extra 15% off price if qty >= 10</font>"; 
                            }
                            if(arg.indexOf("falsecart") > -1)
                            {
                                progress.innerHTML="<font color='red'> Failed to add.</font>"; 
                            }
                        }
                    }
                }
            }                  
        }
    } 
    
    /* product gallery */
    for(var i=0; i<child_count; i++)
    {
        if(obj.childNodes[i].className == "pgalleryextend")
        {
            child = obj.childNodes[i];
            child_count = child.childNodes.length;
                        
            for(var k=0; k<child_count; k++)
            {
                if(child.childNodes[k].id == "resultcart")
                {
                    progress = child.childNodes[k];
                            
                    if(arg.indexOf("truecart") > -1)
                    {
                        progress.innerHTML="<font color='red'>Ok</font>"; 
                    }
                    if(arg.indexOf("falsecart") > -1)
                    {
                        progress.innerHTML="<font color='red'>Failed</font>"; 
                    }
                }
            }
        }
    }  
}

function batchAddToCart(ichkpp)
{    
    var batchPP = getCheckboxValue(ichkpp);    
    if(batchPP == "")
    {        
        alert("please select at least one product.");
    }
    else
    {
        batchPP = "*BatchAddToCart*" + batchPP;
        callServerBatchAddToCart(batchPP);
    }
}
function batchAddToCartProgress()
{
    document.getElementById("resultbatch").innerHTML="<img alt='please wait' src='/images/progress.gif' /><font color='red'> please wait...</font>"
}
function batchAddToCartProgressEnd(arg)
{
    if(arg.indexOf("true") > -1)
    {
        document.getElementById("resultbatch").innerHTML = "<font color='red'> add succeeded</font>";    
    }   
    if(arg.indexOf("false") > -1)
    {
        document.getElementById("resultbatch").innerHTML = "<font color='red'>the quantity of your shopping cart products is out of 130 range.</font>";    
    } 
}
function getCheckboxValue(ichk)
{
    var pp = "";    
    var obj = document.getElementsByTagName("input");    
    for (var i = 0; i < obj.length; i++)
    {
        if (obj[i].type == "checkbox" && obj[i].id.indexOf(ichk) > -1)        
        {
            if(obj[i].checked == true) 
            {
                if(obj[i].value != "")             // used in product detail page
                {
                    pp += ";" + obj[i].value;
                }
                
                else if(obj[i].value =="")            // used in product list, search, recently viewed products page
                {                    
                    var ppid = obj[i].parentNode.parentNode.id;                    
                    if(ppid.indexOf("div") > -1)
                    {
                        ppid = ppid.substring(3, ppid.length);
                        if(ppid != "")
                        {
                            pp += ";" + ppid;                            
                        }  
                    } 
                }           
            }
        }       
    }        
    return pp;    
}

function BgColorChanged(obj, flagObj)
{       
    if(obj.checked == true)
    {
        obj.parentNode.parentNode.className = "plistselected";         
    }
    else
    {
        obj.parentNode.parentNode.className = "productlist";
    }     
    if(flagObj == true)
    {
        AllCheck('checkall', 'ichkpp');
    }
}

function changeCheckboxStatus(parentObject, childrenObjectId)
{   
    var obj = document.getElementsByTagName("input");    
    for (var i = 0; i < obj.length; i++)
    {
        if (obj[i].type == "checkbox" && obj[i].id.indexOf(childrenObjectId) > -1)
        {
            obj[i].checked = parentObject.checked;
            //¸ü¸Ä±³¾°
            BgColorChanged(obj[i]);               
        }
    }    
    AllCheck("checkall", childrenObjectId);
}

function AllCheck(parentObjectId, childrenObjectId)
{    
    var all = true;
    var obj = document.getElementsByTagName("input");    
    for (var i = 0; i < obj.length; i++)
    {
        if (obj[i].type == "checkbox" && obj[i].id.indexOf(childrenObjectId) > -1)
        {
            if (obj[i].checked == false)
            {
                all = false;
            }
        }
    } 
    for (var j = 0; j < obj.length; j++)
    {
        if(obj[j].type == "checkbox" && obj[j].id.indexOf(parentObjectId) > -1)
        {
            if(all == false)
            {
                obj[j].checked = false;
            }
            else
            {
                obj[j].checked = true;
            }
        }
    }    
}

/* ============================  Also Add to Cart ============================ */
function AlsoAddToCart(val)
{
    var ppid = "";
    if(val != "")
    {
        ppid = "*cart*" + val;
    }    
    callServerAlsoAddToCart(ppid);
}
function AlsoAddToCartProgress(ppid)
{
    if(ppid.indexOf("*cart*") > -1)
    {
        ppid = ppid.substring(6, ppid.length);
    }
    var progress = document.getElementById("resultcart" + ppid);
    if(progress != null)
    {
        progress.innerHTML="<img alt='please wait' src='/images/progress.gif' /><font color='red'> please wait...</font>"; 
    }
}
function AlsoAddToCartProgressEnd(arg)
{
    var ppid = "";
    if(arg.indexOf("truecart") > -1)
    {
        ppid = arg.substring(8, arg.length);
    }
    if(arg.indexOf("falsecart") > -1)
    {
        ppid = arg.substring(9, arg.length);
    }
    var progress = document.getElementById("resultcart" + ppid);
    if(progress != null)
    {
        if(arg.indexOf("truecart") > -1)
        {
            progress.innerHTML="<font color='red'> add succeeded</font>"; 
        }
        if(arg.indexOf("falsecart") > -1)
        {
            progress.innerHTML="<font color='red'> failed to add</font>"; 
        } 
    }
}

/* ======================= Recently Viewed Products begin ============================== */
function batchRemove(ichkpp)
{
    var batchPP = getCheckboxValue(ichkpp);    
    if(batchPP == "")
    {
        alert("please select at least one product.");
    }
    else
    {
        batchPP = "*BatchRemoveRecentlyViewedProducts*" + batchPP; 
        callServerBatchRemove(batchPP);
    }
}
function batchRemoveProgress()
{
    document.getElementById("resultbatch").innerHTML="<img alt='please wait' src='/images/progress.gif' /><font color='red'> removing products...</font>"
}
function batchRemoveProgressEnd(arg)
{
    if(arg == "true")
    {
        location.href=location.href;
    }   
    else
    {
        document.getElementById("resultbatch").innerHTML="<font color='red'> error</font>";
    }
}
/*  ================ Recently Viewed Products end ================= */

function setRecommendDiv(ObjectId)
{    
    var obj = document.getElementsByName("div");
    var objTotal = 0;
    for (var i = 0; i < obj.length; i++)
    {
        if(obj[i].id.indexOf(ObjectId) > -1)
        {
            objTotal ++;
            if(objTotal%4 == 0)
            {
                obj[i].className = "recomend-right";                
            }   
        }        
    }
}   