//var ajaxUrl = "http://ajax.deskcity.com/";
var ajaxUrl = "/"
//document.domain = "deskcity.com";
    
//js function
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return decodeURIComponent(c.substring(nameEQ.length,c.length));
  }
  return null;
}
function writeCookie(name, value, opt){
  value = encodeURIComponent(value);
  if(!opt) opt ={
    duration:1
  };
  if(opt && opt.duration){
    var d = new Date();
    d.setTime(d.getTime() + opt.duration*24*3600*1000);
    value += ';expires=' + d.toGMTString();
  }
  document.cookie = name + '=' + value + ';';
}

function readerLastOrder(){
  var last_order = readCookie("lasetordernum");
  if(last_order){
    alert("last_order");
    $(lastOrderId).innerHTML = "您最后购买的订单号是："+last_order;
  }
}

//#查找购物车的内容
function findCart(){
  var remorteUrl = ajaxUrl+"ajax/find_cart";   
      
  new Ajax.Request(remorteUrl,{
    mothed: "GET",
    onComplete:function(xmlhttp){          
      $("cart_info").innerHTML = xmlhttp.responseText;              
    }
  });  
}

    
//#添加商品到购物车
function addToCart(product_id){
  var reg = /^[0-9]*[1-9][0-9]*$/; 
  if (!reg.test(product_id)){ 
    alert("无效数据请求");
    return false;
  }
  //#检查购买商品数是否合法
  var sal= 1;
  //  var sal=$F("goods_num_"+product_id); 
  //  if(!reg.test(sal)){
  //    alert("只能输入大于0的套数！");
  //    return false;
  //  }
  
  var remorteUrl = ajaxUrl+"ajax/add_to_cart/"+product_id;    
  new Ajax.Request(remorteUrl,{
    mothed: "GET",
    parameters:"quantity=" + sal,
    onComplete:function(xmlhttp){   
      $("cart_info").innerHTML = xmlhttp.responseText;  
      window.scrollTo(0,0);
      linkDance();
      alert("本商品将被添加到购车，请查看页面右上角购物车栏！");
    }
  });  
  return true;      
}
function addOneToCart(product_id){

  var reg = /^[0-9]*[1-9][0-9]*$/; 
  if (!reg.test(product_id)){ 
    alert("无效数据请求");
    return false;
  }
  
  var remorteUrl = ajaxUrl+"ajax/add_to_cart/"+product_id;    
  new Ajax.Request(remorteUrl,{
    mothed: "GET",
    onComplete:function(xmlhttp){   
      alert("已将本套商品添加到购物车,现在进入购物车查看");
      //var url = "/sale/present.html"
      //var popup=window.open(url,"shoppingcart");
      //popup.focus();
      window.location.href = "/sale/present.html";
    }
  });  
  return true;      
}

var colors = new Array("black","red");
delay = .4; // seconds
link = 0;
vlink = 0;
function linkDance() {
  link = (link+1)%colors.length;
  vlink = (vlink+1)%colors.length;
  $(current_item).style.color = colors[link]; 
  setTimeout("linkDance()",delay*1000);
}

//从购物车移除
function dropFromCart(id){
  if (confirm("您真的要移除此商品吗？")){        
    var remorteUrl = ajaxUrl+"ajax/drop_from_cart"
    new Ajax.Request(remorteUrl,{
      mothed: "GET",
      parameters:"id=" + id,
      onComplete:function(xmlhttp){
        $("cart_info").innerHTML = xmlhttp.responseText;
      }
    }
  )}  
}

//#清空购物车
function emptyCart(){
  if (confirm("您真的要清空购物车里的商品吗？")){        
    var remorteUrl = ajaxUrl+"ajax/empty_cart"
    new Ajax.Request(remorteUrl,{
      mothed: "GET",
      onComplete:function(xmlhttp){
        $("cart_info").innerHTML = xmlhttp.responseText;
      }
    }
  )}
}

//#添加赠品到购物车
function addToGift(present_id){
  var reg = /^[0-9]*[1-9][0-9]*$/; 
  if (!reg.test(present_id)){ 
    alert("无效数据请求");
    return false;
  }
  //#检查购买商品数是否合法
  var sal=$F("goods_num_"+present_id); 
  if(!reg.test(sal)){
    alert("只能输入大于0的套数！");
    return false;
  }
  //判断赠品数
  //检查赠品数是否超过商品数
  try{
    var total_item = $F("cart_item_total");  
  }catch(msg){}
  try{
    var total_gift = $F("cart_gift_total");
  }catch(msg){}

  if(!total_item){
    alert("您没有购买任何商品");
    return false;
  }
  if(total_gift){
    if((parseInt(total_gift)+parseInt(sal)) > parseInt(total_item)){
      alert("您挑选选的赠品总数已经超过您购买的商品数\n\n你还可以挑选 "+(parseInt(total_item)-parseInt(total_gift))+" 张赠品");
      return false;
    }
  }
  var remorteUrl = ajaxUrl+"ajax/add_to_gift/"+present_id;    

  new Ajax.Request(remorteUrl,{
    mothed: "GET",
    parameters:"quantity=" + sal,
    onComplete:function(xmlhttp){   
      $("cart_gift_info").innerHTML = xmlhttp.responseText;     
   
      new Effect.Highlight("current_gift"); 
    }
  });  
  return true;      
}
//从购物车移除赠品
function dropFromGift(id){
  if (confirm("您真的要移除此赠品吗？")){        
    var remorteUrl = ajaxUrl+"ajax/drop_from_gift"
    new Ajax.Request(remorteUrl,{
      mothed: "GET",
      parameters:"id=" + id,
      onComplete:function(xmlhttp){
        $("cart_info").innerHTML = xmlhttp.responseText;
      }
    }
  )}  
}
//购物车里移走商品 present.html
function dropFromItem(id){
  if (confirm("您真的要移除此商品吗？")){        
    var remorteUrl = ajaxUrl+"ajax/drop_from_item"
    new Ajax.Request(remorteUrl,{
      mothed: "GET",
      parameters:"id=" + id,
      onComplete:function(xmlhttp){
        $("cart_info").innerHTML = xmlhttp.responseText;
      }
    }
  )}  
}

function checkGift(){
  //检查赠品数是否超过商品数
  try{
    var total_item = $F("cart_item_total");  
  }catch(msg){}
  try{
    var total_gift = $F("cart_gift_total");
  }catch(msg){}

  if(!total_item){
    alert("您没有购买任何商品");
    return false;
  }
  if(total_gift){
    if(parseInt(total_gift) > parseInt(total_item)){
      alert("您挑选选的赠品总数已经超过您购买的商品数");
      return false;
    }
    //    if(parseInt(total_gift) < parseInt(total_item)){
    //       if (confirm("您还可以挑选"+(parseInt(total_item) -parseInt(total_gift))+"张赠品,如果有特殊要求请与客户联？")){
    //　　
    //    }
  }
  return true;
}
function checkOut(){
  //结账，购物车内信息可再编辑
  var remorteUrl = ajaxUrl+"ajax/checkout";   
      
  new Ajax.Request(remorteUrl,{
    mothed: "GET",
    onComplete:function(xmlhttp){          
      $("cart_info").innerHTML = xmlhttp.responseText;              
    }
  });   
}

function cartInfo(){
  //显示购物车内容，不可再编辑
  var remorteUrl = ajaxUrl+"ajax/cart_info";   
      
  new Ajax.Request(remorteUrl,{
    mothed: "GET",
    onComplete:function(xmlhttp){          
      $("cart_info").innerHTML = xmlhttp.responseText;              
    }
  }); 
}
function saveOrder(){
  var remorteUrl = ajaxUrl+"ajax/save_order/";   
      
  new Ajax.Request(remorteUrl,{
    mothed: "GET",
    onComplete:function(xmlhttp){          
      $("cart_info").innerHTML = xmlhttp.responseText;              
    }
  });    

}

