
//动态导入弹出JS 与 CSS
document.write("<link rel='stylesheet' href='./Public/js/boxy/css/boxy.css?ver=20091102' type='text/css' /><script type='text/javascript' src='./Public/js/boxy/js/jquery.boxy.js?ver=20091101'></script><link href='./theme/default/css/tanchuang.css' rel='stylesheet' type='text/css' />");

var ROOT="index.php";
var VAR_MODULE="m";
var VAR_ACTION="a";

if(typeof(cartUrl)!="undefined")
	var cartUrl	= ROOT+"?"+VAR_MODULE+"=Cart&"+VAR_ACTION+"=index&";

//弹出窗
var popwindow=null;

//提示,跳转页面
function confirm_redirect(msg,url){
	if(confirm(msg)){
		location.href=url;
	}	
}
//添加到购物车
function addToCart(product_id,one_step_buy){
	var ajaxUrl=ROOT+"?"+VAR_MODULE+"=Ajax&"+VAR_ACTION+"=addToCart&";
	if(typeof(addToCartUrl)!="undefined")
		ajaxUrl	= addToCartUrl;
	$.ajax({
			type: "POST",
	        data: {pid:product_id,pno:1,one_step_buy:one_step_buy},
	        url: ajaxUrl,
			dataType: "json",
	        success: function(result) {
        		//alert(result);
        		addToCartResponse(result);
	        },
	        error: function(){alert("服务器没有返回数据，可能服务器忙，请重试"); }
	});
	
}

/* *
 * 处理添加商品到购物车的反馈信息
 */
function addToCartResponse(result)
{
  if (result.content.error > 0)
  {
    // 如果需要缺货登记，跳转
    if (result.content.error == 2)
    {
      if (confirm(result.message))
      {
    	  if(typeof(addBookingUrl)!="undefined")
    		  location.href = addBookingUrl;
    	  //else
    		  //location.href = "?"+VAR_MODULE+"=Users&"+VAR_ACTION+"=addBooking&id=" + result.content.product_id;
      }
    }
    // 没选规格，弹出属性选择框
    else if (result.content.error == 6)
    {
      alert("没选规格");
      //openSpeDiv(result.message, result.goods_id, result.parent);
    }
    else
    {
      alert(result.message);
    }
  }
  else
  {
    var cartInfo = document.getElementById('MY_CARTINFO');
    var cart_url = cartUrl;		//跳到购物车
    if (cartInfo)
    {
      cartInfo.innerHTML = result.content;
    }
    
    else
    {
      switch(result.content.confirm_type)
      {
        case '1' :
          if (confirm(result.message)) location.href = cart_url;
          break;
        case '2' :
        	var ajaxUrl=ROOT+"?"+VAR_MODULE+"=Ajax&"+VAR_ACTION+"=subcart&";
        	if(typeof(subCartUrl)!="undefined")
        		ajaxUrl	= subCartUrl;
        	$.ajax({
    			type: "POST",
    	        url: ajaxUrl,
    			dataType: "json",
    	        success: function(result) {
    	        	if (result.error == 0)
    	            {
    	        		popwindow=new Boxy('<div class="big_bg overflow"><div class="quiz_02"><div class="title white fs14 fb"><p class="fl">商品已经成功添加至购物车</p><a class="fr mt10 mr10"><img src="./theme/default/images/pop_X.gif" alt="" onclick="closePopWindow();" /></a></div><p class="green_02 tc mt20"><span>购物车共'+result.content.count+'种商品</span><span class="ml30">合计：<b class="orange_01">'+result.content.totalprice+'</b>元</span></p><input name="" type="button" value="继续选购" class="fl button_01 tc mt20" onclick="closePopWindow();" /><input name="" type="button" value="去购物车结算" class="fl button_02 tc mt20 ml10" onclick="goCart();" /></div></div>',{modal: true});
    	            }
    	        }
    		});
        	//if (!confirm(result.message)) location.href = cart_url;
          break;
        case '3' :
          location.href = cart_url;
          break;
        default :
          break;
      }
    }
  }
}
//关闭弹出框
function closePopWindow(){
	if(popwindow!=null)
		popwindow.hide();
}
//跳转到购物车
function goCart(){
	location.href = cartUrl;
}


/**
 * 获得选定的商品属性
 */
function getSelectedAttributes(formBuy)
{
  var spec_arr = new Array();
  var j = 0;

  for (i = 0; i < formBuy.elements.length; i ++ )
  {
    var prefix = formBuy.elements[i].name.substr(0, 5);

    if (prefix == 'spec_' && (
      ((formBuy.elements[i].type == 'radio' || formBuy.elements[i].type == 'checkbox') && formBuy.elements[i].checked) ||
      formBuy.elements[i].tagName == 'SELECT'))
    {
      spec_arr[j] = formBuy.elements[i].value;
      j++ ;
    }
  }

  return spec_arr;
}
//复制内容到剪切板
function copyToClipBoard(text){ 
    var clipBoardContent=""; 
    clipBoardContent+=document.title; 
    clipBoardContent+=""; 
    clipBoardContent+=this.location.href+text; 
    window.clipboardData.setData("Text",clipBoardContent); 
    alert("复制成功，请粘贴到你的QQ/MSN上推荐给你的好友"); 
} 
//添加到收藏夹
function addToFavorites(product_id){
	if(confirm('您确定要收藏此商品吗？')){
		var ajaxUrl=ROOT+"?"+VAR_MODULE+"=Ajax&"+VAR_ACTION+"=addToFavorites&";
		if(typeof(addToFavoritesUrl)!="undefined")
			ajaxUrl	= addToFavoritesUrl;
		$.ajax({
			type: "POST",
	        data: {pid:product_id},
	        url: ajaxUrl,
			dataType: "json",
	        success: function(result) {
	        	if (result.error == 0)
	            {
	        		//alert(result.content.msg);
	        		if(result.content.msg=='您还没有登录，请先登录!')
	        		{
	        			alert(result.content.msg);
	        		}
	        		else popwindow=new Boxy('<div class="big_bg overflow"><div class="quiz_02 link_a"><div class="title white fs14 fb"><p class="fl">商品已经成功添加至收藏夹</p><a class="fr mt10 mr10"><img src="./theme/default/images/pop_X.gif" alt="" onclick="closePopWindow();" /></a></div><p class="green_02 tc mt20"><span>收藏夹中共'+result.content.count+'种商品</span></p><input name="" type="button" value="确定" class="fl button_03 tc mt20" onclick="closePopWindow();"/><a href="'+myFavoritesUrl+'" class="fl ml10 mt30">查看我的收藏夹</a></div></div>',{modal: true});
	        		//<span class="ml30">合计：<b class="orange_01">'+result.content.price+'</b>元</span>
	            }
	        },
	        error: function(){alert("服务器没有返回数据，可能服务器忙，请重试"); }
		});
	}
}

//兑换免邮卡
function mianyouka(){
	if(confirm('确定要兑换吗？')){
		var ajaxUrl=ROOT+"?"+VAR_MODULE+"=Ajax&"+VAR_ACTION+"=mianyouka&";
		if(typeof(mianyouUrl)!="undefined")
			ajaxUrl	= mianyouUrl;
		$.ajax({
			type: "POST",
	        url: ajaxUrl,
			dataType: "json",
	        success: function(result) {
	        	if (result.error == 0)
	            {
	        		alert(result.content.msg);
	            }
	        },
	        error: function(){alert("服务器没有返回数据，可能服务器忙，请重试"); }
		});
	}
}
