﻿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();
                    //alert(varUrl);                
                    //window.location.href(varUrl);
                    //window.location.href="http://www.giftstoindia24x7.com/Cart.aspx?mode=&acti=Display";
                    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.open('POST', "ajaxCartFunction.aspx", true);
     self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     self.xmlHttpReq.onreadystatechange = function() 
     {        
        if (self.xmlHttpReq.readyState == 4) 
        {   
        //alert("add to cart");     
            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)
{
    //alert("here");
    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) 
            {   
                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")
                    {
//                        window.location="cart.aspx";
                          window.location="cart.html";
                    }
                }  
                else
                {                      
                    alert("There is some error on adding to cart. Please click browser refresh button."); 
                }
                spnAddtocart.style.display="block";
                spnload.style.display="none";
            }
            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.");
    }
}