
var reference_url;
var book_id;
var Jflag = -1; 
var JxmlHttp;
var XxmlHttp;
var Xflag = 0;
//将书加入购物车 并跳转到购物车页面 返回页面url
var xmlHttp;
function ajax()
 {

 
 try
    {
    xmlHttp=new XMLHttpRequest();
    }
 catch (e)
    {

   try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e)
      {

      try
         {
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
      catch (e)
         {
         alert("您的浏览器不支持AJAX！");
         return false;
         }
      }
    }

}
function AddToShoppingCart(pid) {
  ajax();
  
  xmlHttp.onreadystatechange=function()
        {
          if(xmlHttp.readyState==4)
           {
		 
              if(xmlHttp.responseText.split("|")[0]=="-1"||xmlHttp.responseText.split("|")[0]=="")
		      {
			  
			    alert("添加失败！");
				return false;
		     }
			 else
			 {
			  ListCart(xmlHttp.responseText.split("|")[1],xmlHttp.responseText.split("|")[2]);     
			 }
           }
		}
	
	 var weburl = "/AJAX.aspx?fuc=add&pid=" + pid+"&t="+Math.random(); 
	 xmlHttp.open("GET",weburl,true);
     xmlHttp.send(null);
	 

}


//修改购物车中商品数量
function modcart(rid)
{
    ///判断输入是否合法.
    try
    {
        var nowpum = parseInt(document.getElementById("dcsl"+rid).value);
        if (nowpum < 500 && nowpum > 0)
        {
            pnum = nowpum;
        }
        else
        {
            alert("请输入1-500之间的整数");
            pnum = lastpum;
        }
    }
    catch(e)
    {
        pnum = 1;
    }
  ajax();
  
  xmlHttp.onreadystatechange=function()
        {
          if(xmlHttp.readyState==4)
           {
		 
              if(xmlHttp.responseText.split("|")[0]=="-1"||xmlHttp.responseText.split("|")[0]=="0")
		      {
			  
			    alert("修改数量失败！");
				return false;
		     }
			 else
			 {
			  ListCart(xmlHttp.responseText.split("|")[1],xmlHttp.responseText.split("|")[2]);     
			 }
           }
		}
	
	 var weburl = "/AJAX.aspx?fuc=mod&rid=" + rid+"&sl="+pnum+"&t="+Math.random(); 
	 xmlHttp.open("GET",weburl,true);
     xmlHttp.send(null);
	 
}



///删除购物车中商品
function delcart(rid)
{
  ajax();
  
  xmlHttp.onreadystatechange=function()
        {
          if(xmlHttp.readyState==4)
           {
		 
              if(xmlHttp.responseText.split("|")[0]=="-1"||xmlHttp.responseText.split("|")[0]=="0")
		      {
			  
			    alert("删除失败！");
				return false;
		     }
			 else
			 {
			  ListCart(xmlHttp.responseText.split("|")[1],xmlHttp.responseText.split("|")[2]);     
			 }
           }
		}
	
	 var weburl = "/AJAX.aspx?fuc=sc&rid=" + rid+"&t="+Math.random(); 
	 xmlHttp.open("GET",weburl,true);
     xmlHttp.send(null);
	 
}



///清空购物车中商品
function deleteallcart()
{
    if (!confirm('你确认清空购物车?'))
    {
        return;   
    }
    handlecookie('buildingName' , '' ,{ expires: 1, path: "/", secure: false });
    handlecookie('buildingID' , '' ,{ expires: 1, path: "/", secure: false });
    var uid = document.getElementById("hidUid").value;
    new net.ContentLoader("Ajax/TogoShoppingCart.aspx","fuc=delall&uid="+uid+"","POST",deleteallcart_deal,deleteallcart_err);
}

function deleteallcart_deal()
{
    if(this.req.responseText == "1")
    {
        ListCart();
    }
    else if(this.req.responseText == "-1")
    {
       //显示失败信息
       alert("清空购物车失败");
    }
}

function deleteallcart_err()
{
    alert("服务器忙...");
}

function ListCart(cartid,sid) {
  
    ajax();
  
  xmlHttp.onreadystatechange=function()
        {
          if(xmlHttp.readyState==4)
           {
		
              if(xmlHttp.responseText=="0")
		      {
			  
			  alert("读取失败！");
				
		     }
			 else
			 {
			 
			
			      document.getElementById('cartContent').innerHTML=xmlHttp.responseText;
			 }
           }
		}
	
	 var weburl = "/AJAX.aspx?fuc=list&cid=" + cartid+"&sid="+sid+"&t="+Math.random(); 
	 xmlHttp.open("GET",weburl,true);
     xmlHttp.send(null);
 
}

function ListCart_deal()
{
    if (this.req.responseText != "0") 
    {
        document.getElementById("cartContent").innerHTML = this.req.responseText;
    }
    else
    {
       //显示失败信息
       alert("读取失败");
    }
}

function ListCart_err()
{
    alert("服务器忙...");
    //ShowWindow("服务器忙...", false);
}

function CheckTimebusiness(str)
{
    var timelist = new Array();//得到每个时间段.
    var timeinfo = new Array();//得到每个时间.
    timelist = str.split("|");
    for (var i = 0 ; i < timelist.length ; i++)
    {
        timeinfo = timelist[i].split("_");
    }  
}


var ret = "-2";
function CheckCart() {
    var qflag = false;
    var uid = document.getElementById("hidUid").value;
    if (uid == "") {
        jtipsWindown('欢迎登陆', 'id:divShowContent', '640', '100', 'true', '', 'true', 'text');
        return false;
    }
    if (document.getElementById("cartContent").getElementsByTagName("tr").length <= 2)
   {  
       tipsWindown('提示信息','text:你的餐盒是空的,不的提交!','250','150','true','3000','true','text');
       return false;
   }
   var oid = $("#hfoid").val();
   if (oid != "") { //补单,没有金额
       return true;
   }
   //do more things :判断每个商家的餐品是否都已经达到送的标准
   var userid = document.getElementById("hidUid").value;
   jQuery.ajax(
    {
        async: false,
        type: "post",
        url: "Ajax/CheckuserQulity.aspx",
        data: "t=" + new Date().getTime() + "&uid=" + userid,
        success: function(msg) {
            if (msg != "1") {
                alert(msg);
                qflag = false;
                return false;
            }
            else {
                qflag = true;
                return true;
            }
        }
    })

    return qflag;
}
