﻿//判断是否允许使用优惠券
function getActivityCoupon()
{
	var userId= $("userId").value;	
	preferentialImpl.getNoCouponActivity( userId,{
		callback:function(data){
	 	showCoupon(data);		 		 	 	
		}
	 });
}
//显示使用优惠券框
function showCoupon(data) 
{	
	if(data)
	{
		document.getElementById("noCouponDiv").style.display='';
		document.getElementById("yesCouponDiv").style.display='none';
	}
	else {
		document.getElementById("noCouponDiv").style.display='none';
		document.getElementById("yesCouponDiv").style.display='';
	}
}
//使用优惠券
function useCoupon()
{
	var couponId = $("couponId").value;
	var couponTypeId = $("couponTypeId").value;
	var couponDiscount = $("couponDiscount").value;
	var ordersSumamount = $("ordersSumamount").value;
	var ordersPoint = $("ordersPoint").value;
	if(couponId>0){
		if(couponTypeId==1)//现金减免
		{
			$("ordersCoupondiscount").value = couponDiscount;
			var str = '<table width="760" border="0" cellspacing="0" cellpadding="0"><tr><td colspan="4" align="left"></td><td colspan="1" align="right"><font color="red">使用优惠券优惠金额:￥-'+$("ordersCoupondiscount").value+'元</font></td></tr></table>';
			if($('couponDiv')) $('couponDiv').innerHTML = str;
		}
		else if(couponTypeId==2)//打折销售
		{
			$("ordersCoupondiscount").value = tofloat((ordersSumamount-couponDiscount*ordersSumamount),2);
			var str = '<table width="760" border="0" cellspacing="0" cellpadding="0"><tr><td colspan="4" align="left"></td><td colspan="1" align="right"><font color="red">使用优惠券优惠金额:￥-'+$("ordersCoupondiscount").value+'元</font></td></tr></table>';
			if($('couponDiv')) $('couponDiv').innerHTML = str;
		}
		else if(couponTypeId==3)//赠送积分
		{
			$("ordersPoint").value= ordersPoint+couponDiscount;
			var str = '<table width="760" border="0" cellspacing="0" cellpadding="0"><tr><td colspan="4" align="left"></td><td colspan="1" align="right"><font color="red">使用优惠券获得积分:+'+couponDiscount+'</font></td></tr></table>';
			if($('couponDiv')) $('couponDiv').innerHTML = str;
		}
		else
		{
			return false;
		}
		
	}
	
}
//获取优惠活动
function getPreferential()
{

	var userId = $("userId").value;
	preferentialImpl.getActivityByDate( userId,{
		callback:function(data){
	 	showActivity(data);		 		 	 	
		}
	 });	
	
}

//显示优惠活动信息
function showActivity(data)
{
	var str = '';
    var index = 0;
    var startTime = '';
    var endTime = '';
	for(var i=0;i<data.length;i++)
	{
		startTime =  dateToStr(data[i].preferentialActivityStartdatetime);
		endTime =  dateToStr(data[i].preferentialActivityEnddatetime);
		index = i+1;
		str += '<table width="760" border="0" cellspacing="0" cellpadding="0"><tr><td colspan="5" align="right" width="50%">&nbsp;</td><td colspan="1" align="left" width="50%"><b>'+index+'.'+data[i].preferentialActivityName+'。</b>&nbsp;'+startTime+'~'+endTime+'</td></tr></table>';
	}	
	
	if($('activity')){
		 $('activity').innerHTML = str;	
		// $('activity').style.display='';	 
	}
    return str;
}

function dateToStr(date){   
    if(typeof date=="string"){   
        return date;   
    }   
    if(date==null||date=="")   
    return null;   
    var y=date.getYear();   
    if(y<10) y="0"+y;   
    var m=date.getMonth()+1;   
    if(m<10) m="0"+m;   
    var d=date.getDate();   
    if(d<10) d="0"+d;   
    var redate=y+"-"+m+"-"+d;   
    return redate;   
}  
