﻿function addToCart(mode, ProdId, CatId, qnty)
{     
   var ajaxDiv=document.getElementById("dropin");
   if(mode!="" && ajaxDiv!=null)
   {    
     var xmlHttpReq = false;
     var self = this;
     if (window.XMLHttpRequest) 
     {
        self.xmlHttpReq = new XMLHttpRequest();        
     }
     else if (window.ActiveXObject) 
     {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");         
     }     
     self.xmlHttpReq.open('POST', "ajaxAddToCart.aspx", true);
     self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     self.xmlHttpReq.onreadystatechange = function() 
     {        
        if (self.xmlHttpReq.readyState == 4) 
        {        
            var GetValue=new Array();
            if(self.xmlHttpReq.responseText.indexOf('~' != -1))
            {
              GetValue=self.xmlHttpReq.responseText.split("~");
                if(GetValue[0].toString()=="0")
                {
                    alert('There is some error. Please try again.');
                }
                else if(GetValue[0].toString()=="1")
                {
                    //alert("To Cart");
                    var varUrl=GetValue[1].toString();
                    window.location='Cart.aspx';
                }
            }  
            else
            {                      
                alert("There is some error on adding to cart. Please click browser refresh button."); 
            }
            ajaxDiv.style.display="none";
        }
        else
        {
            ajaxDiv.style.display="block";
            positionDivOnClick(ajaxDiv);
        }
     }
    self.xmlHttpReq.send("Mode="+mode + "&ProdId=" + ProdId + "&CatId=" + CatId + "&qnty=" + qnty); 
    }
}

function addToCartNR(obj, mode, ProdId, CatId, qnty)
{     
   var ajaxDiv=document.getElementById("dropin");
   var objMainDiv=document.getElementById("dvCartMain");
   if(objMainDiv!=null && ajaxDiv!=null)
   {    
     obj.disabled=true;
     var xmlHttpReq = false;
     var self = this;
     if (window.XMLHttpRequest) 
     {
        self.xmlHttpReq = new XMLHttpRequest();        
     }
     else if (window.ActiveXObject) 
     {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");         
     }     
     self.xmlHttpReq.open('POST', "ajaxAddToCart.aspx", true);
     self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     self.xmlHttpReq.onreadystatechange = function() 
     {        
        if (self.xmlHttpReq.readyState == 4) 
        {        
            var GetValue=new Array();
            if(self.xmlHttpReq.responseText.indexOf('~' != -1))
            {
                GetValue=self.xmlHttpReq.responseText.split("~");
                if(GetValue[0].toString()=="0")
                {
                    alert("There is some error. Please try again.");
                }
                else if(GetValue[0].toString()=="1")
                {
                    objMainDiv.innerHTML=GetValue[1].toString();
                }
                updateAlsoBought();
            }  
            else
            {                      
                alert("There is some error on adding to cart. Please click browser refresh button."); 
            }
            ajaxDiv.style.display="none";
            obj.disabled=false;
        }
        else
        {
            ajaxDiv.style.display="block";
            positionDivOnClick(ajaxDiv);
            obj.disabled=false;
        }
     }
    self.xmlHttpReq.send("Mode="+mode + "&ProdId=" + ProdId + "&CatId=" + CatId + "&qnty=" + qnty); 
    }
}

function fnCartAdd(Mode, intcounter, proID, catId)
{
    var spnAddtocart=document.getElementById("spnBuyBtn_"+intcounter);
    var spnload=document.getElementById("spnload_"+intcounter);    
    //alert("counter:" + intcounter + " add obj: " + spnAddtocart + " load obj: " + spnload);
    if(spnAddtocart!=null && spnload!=null)
    {   
        var xmlHttpReq = false;
        var self = this;
        if (window.XMLHttpRequest) 
        {
            self.xmlHttpReq = new XMLHttpRequest();        
        }
        else if (window.ActiveXObject) 
        {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");         
        }
        self.xmlHttpReq.open('POST', "ajaxAddToCart.aspx", true);             
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() 
        {        
            if (self.xmlHttpReq.readyState == 4) 
            {                   
                spnload.style.display="none";
                var GetValue=new Array();
                if(self.xmlHttpReq.responseText.indexOf('~' != -1))
                {
                    GetValue=self.xmlHttpReq.responseText.split("~");
                    if(GetValue[0].toString()=="0")
                    {
                        spnAddtocart.style.display="block";
                        alert("There is some error. Please try again.");
                    }
                    else if(GetValue[0].toString()=="1")
                    {
                        window.location="cart.aspx";
                    }
                }  
                else
                {                      
                    alert("There is some error on adding to cart. Please click browser refresh button."); 
                }                
            }
            else
            {
                spnAddtocart.style.display="none";
                spnload.style.display="block";
            }
        }                
        self.xmlHttpReq.send("ProdId="+ proID +"&CatId="+ catId +"&mode="+Mode+"&qnty=1");
    }
    else
    {
        alert("Objects not found. Try again.");
    }
}

function addToCartNew(objImgName, objAjaxName, ProdId, CatId, qnty)
{     
   var oi=document.getElementById(objImgName);
   var od=document.getElementById(objAjaxName);
   if(oi!=null && od!=null)
   {      
     oi.disabled=true;
     oi.style.display="none";
     od.style.display="block";
     var xmlHttpReq = false;
     var self = this;
     if (window.XMLHttpRequest) 
     {
        self.xmlHttpReq = new XMLHttpRequest();        
     }
     else if (window.ActiveXObject) 
     {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");         
     }     
     self.xmlHttpReq.open('POST', "ajaxAddToCart.aspx", true);
     self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     self.xmlHttpReq.onreadystatechange = function() 
     {        
        if (self.xmlHttpReq.readyState == 4) 
        {                    
            od.style.display="none";
            var GetValue=new Array();
            if(self.xmlHttpReq.responseText.indexOf('~' != -1))
            {
                GetValue=self.xmlHttpReq.responseText.split("~");
                if(GetValue[0].toString()=="0")
                {
                    oi.disabled=false;
                    oi.style.display="block";
                    alert(GetValue[1].toString());
                }
                else if(GetValue[0].toString()=="1")
                {
                    var varUrl=GetValue[1].toString();
                    window.location.href ='cart.aspx';
                }
                else
                {
                    oi.disabled=false;
                    oi.style.display="block";
                }
            }  
            else
            {                      
                alert("There is some error on adding to cart. Please click browser refresh button."); 
            }
        }
        else
        {
            oi.disabled=true;
            oi.style.display="none";
            od.style.display="block";
        }
     }
     self.xmlHttpReq.send("Mode=1&ProdId=" + ProdId + "&CatId=" + CatId + "&qnty=" + qnty);
    }
}

function addtowl(spn, ajx, p, c)
{    
    var os=document.getElementById(spn);
    var oa=document.getElementById(ajx);
    if(os!=null && oa!=null)
    {
         var xmlHttpReq = false;
         var self = this;
         if (window.XMLHttpRequest) 
         {
            self.xmlHttpReq = new XMLHttpRequest();        
         }
         else if (window.ActiveXObject) 
         {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");         
         }     
         self.xmlHttpReq.open('POST', "ajaxAddToCart.aspx", true);
         self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
         self.xmlHttpReq.onreadystatechange = function() 
         {        
            if (self.xmlHttpReq.readyState == 4) 
            {         
                //obj.disabled=false;
                //ol.disabled=false;
                os.style.display="block";
                oa.style.display="none";
                if(self.xmlHttpReq.responseText.toString()!="")
                {
                    if(self.xmlHttpReq.responseText.indexOf('~' != -1))
                    {
                      GetValue=self.xmlHttpReq.responseText.split("~");
                    }                        
                    if(GetValue[0]=="1")
                    {
                        os.innerHTML=GetValue[1].toString();
                        alert("Added to your wishlist.");
                    }
                    else if(GetValue[0]=="2")
                    {
                        var msg=confirm(GetValue[1].toString());
                        if(msg)
                        {
                            window.location.href=GetValue[2].toString();
                        }
                    }
                    else
                    {  
                        alert(GetValue[1].toString());
                    } 
                }
                else
                {
                    alert("Sorry, we cannot you let you logout this time. Please try later.");
                }                
            }
            else
            {
                os.style.display="none";
                oa.style.display="block";
            }
         }
        self.xmlHttpReq.send("Mode=addtowishlist&pid=" +p+ "&cid="+c); 
    }   
    else
    {
        alert("Sorry, this product cannot be added to your wish list now. Please try later.");
    }         
}
function removeWL(spn, ajx, i, p)
{
    var os=document.getElementById(spn);
    var om=document.getElementById("li_"+p);
    var oa=document.getElementById(ajx);
    if(os!=null && om!=null && oa!=null)
    {    
        var msg=confirm("Are you sure to delete this product from your wishlist?");
        if(msg)
        {
            var xmlHttpReq = false;
             var self = this;
             if (window.XMLHttpRequest) 
             {
                self.xmlHttpReq = new XMLHttpRequest();        
             }
             else if (window.ActiveXObject) 
             {
                self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");         
             }     
             self.xmlHttpReq.open('POST', "ajaxAddToCart.aspx", true);
             self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
             self.xmlHttpReq.onreadystatechange = function() 
             {        
                if (self.xmlHttpReq.readyState == 4) 
                {         
                    om.disabled=false;
                    os.style.display="block";
                    oa.style.display="none";
                    if(self.xmlHttpReq.responseText.toString()!="")
                    {
                        if(self.xmlHttpReq.responseText.indexOf('~' != -1))
                        {
                          GetValue=self.xmlHttpReq.responseText.split("~");
                        }                        
                        if(GetValue[0]=="1")
                        {
                            om.style.display="none";
                            alert(GetValue[1].toString());
                        }
                        else
                        {  
                            alert(GetValue[1].toString());
                        } 
                    }
                    else
                    {
                        alert("Sorry, this product cannot be removed from your wish list now. Please refresh the page and try again.");
                    }                
                }
                else
                {
                    om.disabled=true;
                    os.style.display="none";
                    oa.style.display="block";
                }
             }
            self.xmlHttpReq.send("Mode=removewishlist&pid="+i); 
        }
    }
    else
    {
        alert("Sorry, this product cannot be removed from your wish list now. Please refresh the page and try again.");
    }  
}