/* *
 * 优惠券
 */
var selectedCoupon=0
var CouponNum=0;
var showCouponMsg=true;
function changeCoupon(val)
{
  if (selectedCoupon == val)
  {
    return;
  }
  else
  {
    selectedCoupon = val;
  }
  var url = 'plus/ajaxs.ashx';
  $.get(url,{action:"Shop_GetCoupon",CouponId:val},function(s){
  updateOrderFee(unescape(s));
  });
}
function updateOrderFee(returnStr){
    var realmoney=0;
	var goodsMoney=0;
	var facevalue=0;
	var maxdiscount=0;
	var availablemoney=0;
	var minAmount=0;
	var rarr=null;
	var str='';
	if (returnStr!='' && returnStr !=null)
	{
	  rarr=returnStr.split('|');
	  if (rarr.length<=1)
	  {
	   if (showCouponMsg){
		 
	    
		 if (returnStr.indexOf("登录后才可以使用优惠券")!=-1){ 
		      KesionJS.Alert(returnStr,"showuserlogin()");
		 }else{
			  KesionJS.Alert(returnStr);
		 }
	   }
	   return;
	  }
	  facevalue=parseFloat(rarr[0]).toFixed(2);
	  minAmount=parseFloat(rarr[1]).toFixed(2);
	  maxdiscount=parseFloat(rarr[2]);
	  availablemoney=parseFloat(rarr[3]).toFixed(2);
	  
	  goodsMoney=parseFloat($("#goodsMoney").html()).toFixed(2);  // 查找只含商品价格
	  realmoney=parseFloat($('#orderTotalPrice').html()).toFixed(2);
	  if (goodsMoney==0 || goodsMoney=='NaN') goodsMoney=realmoney;
	  if (parseFloat(goodsMoney)<parseFloat(minAmount))
	  {
	    if (showCouponMsg)
		KesionJS.Alert('对不起,该优惠券最小订单商品金额(不含运费)为￥'+minAmount+ ' 元才可使用!' );
		else
		KesionJS.Alert('对不起,优惠券['+rarr[4]+']最小订单商品(不含运费)总金额为￥'+minAmount+ ' 元才可使用!' );
		$("#ORDER_COSTS").html('');
	    return ;
	  }else if(availablemoney<=0){
	    if (showCouponMsg) KesionJS.Alert('对不起,该优惠券已用完!');
		return;
	  }else{
	   if (maxdiscount==0){
	   if (showCouponMsg) KesionJS.Alert('恭喜,该优惠券可抵用￥'+facevalue+ ' 元!');
	   }else{
	      var allowmoney=parseFloat(goodsMoney)* (maxdiscount/100);                         //按百分比得可抵扣金额
		  if (allowmoney>availablemoney){
		     allowmoney=availablemoney;
		  }
		  if (availablemoney==facevalue || availablemoney-allowmoney!=0){
		  if (showCouponMsg) KesionJS.Alert('恭喜,该优惠券是按订单总金额的'+maxdiscount+'%抵用,最多不超过￥'+facevalue+'元,当前可用余额￥'+parseFloat(availablemoney).toFixed(2)+'元。在本订单中可抵用￥'+parseFloat(allowmoney).toFixed(2)+ ' 元,使用后该优惠券还剩￥'+(availablemoney-allowmoney).toFixed(2)+'元!');
		  }else{
		  if (showCouponMsg) KesionJS.Alert('恭喜,该优惠券还可抵用￥'+parseFloat(allowmoney).toFixed(2)+ ' 元,使用后该优惠券还剩￥'+(availablemoney-allowmoney).toFixed(2)+'元!');
		  }
		  facevalue=allowmoney;
	   }
	  realmoney=(parseFloat(realmoney)-parseFloat(facevalue)).toFixed(2);
	  str='<br />使用优惠券抵销:<font color=blue>￥<span id="couponfacemoney">'+parseFloat(facevalue).toFixed(2) +'</span></font> 元';
	  str+='<br />应付款金额:<font color=red>￥<span id="yfk">'+realmoney+'</span></font> 元';
	  $('#RealTotalPrice').val(realmoney);
	  $("#ORDER_COSTS").html(str);
	  }
	}
}
function validateCoupon()
{
  if ($("#couponnum").val()=='')
  {
	  try{
       KesionJS.Alert('请输入优惠券号!','$("#couponnum").focus();');
	  }catch(e){
		  alert('请输入优惠券号!');
	  }
   return false;
  }
  if (CouponNum == $("#couponnum"))
  {
    return;
  }
  else
  {
    CouponNum = $("#couponnum");
  }
  showCouponMsg=true;
  var url = appurl+'plus/ajaxs.ashx';
  $.get(url,{a:"validatecoupon",CouponNum:$("#couponnum").val()},function(s){
    var returnStr=unescape(s);
	if (returnStr=='' || returnStr==null){
		try{
	      KesionJS.Alert('对不起,您输入的优惠券不可用!','$("#couponnum").val("");');
		}catch(e){
			alert('对不起,您输入的优惠券不可用!');
		}
	  return ;
	}
	 else{
	updateOrderFee(returnStr);
	}
  });
}
function checkCoupon(){
  var url = 'plus/ajaxs.ashx';
  $.get(url,{a:"validatecoupon"},function(s){
    var returnStr=unescape(s);
	if (returnStr=='' || returnStr==null){
	  return ;
	}
	 else{
	updateOrderFee(returnStr);
	}
  });
}
﻿