$(document).ready(function() {	
	$('#txtRPostalcode').keyup(function(){
		var curobj = $(this);
		curobj.val(curobj.val().replace(/\D/g,""));   //替换非数字
	});
	$('#txtRTelephone').keyup(function(){
		var curobj = $(this);
		curobj.val(curobj.val().replace(/\D/g,""));   //替换非数字
	});
	$('#txtQuantity').keyup(function(){
		var curobj = $(this);
		curobj.val(curobj.val().replace(/\D/g,""));   //替换非数字
	});
	$('#btnSure').click(function(){
		var flag = true;

		
//		$.ajax({
//			type:"get",
//			url:(shopFolder + 'TGShow.aspx?NoCopyRight=1'),
//			data:'type=CheckMember&MemberGroup=' + memberGroup,
//			cache:false,
//			async:false,
//			success:function(msg){
//				switch(msg)
//				{
//					case '^1':
//						alert('您还没有登录');
//						flag = false;
//						break;
//					case '^2':
//						alert('您不在允许参与的会员级别范围内');
//						flag = false;
//						break;
//				}
//				
//			},
//			error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){flag = false;alert('身份已过期,请重新登陆!')}else{flag = false;alert('error')};}
//		});
		if(!flag) return flag;
		
		if($.trim($('#txtRAddress').val()) == '')
		{
			alert('收货地点必填');return false;
		}
		if($.trim($('#txtRName').val()) == '')
		{
			alert('收货人姓名必填');return false;
		}
		if($.trim($('#txtRPostalcode').val()) == '')
		{
			alert('邮政编码必填');return false;
		}
		if($.trim($('#txtRPhone').val()) == '')
		{
			alert('收货人电话必填');return false;
		}
		
		var num = $.trim($('#txtQuantity').val());
		if(num == ''){
			alert('购买的货品数量必填');return false;
		}else if(isNaN(num)){
		    alert('购买数量填写有误');return false;
		};
		
        var rid=$('#rid').val();
        var pid=$('#pid').val();
        var gid=$('#gid').val();
        var rname=$('#txtRName').val();
        var raddr=$('#txtRAddress').val();
        var rcode=$('#txtRPostalcode').val();
        var rphone=$('#txtRPhone').val();
        var rmobile=$('#txtRMobile').val();
        var qty=$('#txtQuantity').val();
        var data="&rid="+rid+"&pid="+pid+"&gid="+gid+"&rname="+rname+"&raddr="+raddr+"&rcode="+rcode+"&rphone="+rphone+"&rmobile="+rmobile+"&qty="+qty	
        $.ajax({
          type: "post",
          dataType:"json",
          url: "TGShow.aspx?NoCopyRight=1&type=ajax&action=doTuanGou",
          data:data,
          cache:false, 
          success: function(data){
            var errorflag=data["errorflag"];
            var errormsg=data["errormsg"];
            switch(errorflag)
            {
                case "0":
                    alert("已提交团购申请");
                    break;
                case "1":
                    if(confirm('请先登录,是否现在就登录?')){
                        window.location.href= 'login.aspx?ReturnUrl='+window.location.href;
                    };
                    break;
                case "2":
                    alert(errormsg);//秒杀还未开始，请耐心等待
                    break;
                case "3":
                    alert(errormsg);//秒杀已结束，请等待下一期
                    $("#"+obj2).siblings("input").removeClass("start").addClass("end");
                    break;
                case "4":
                    alert(errormsg);//对不起，您已超出限制数量
                    break;
                case "5":
                    alert(errormsg);//对不起，已秒完
                    $("#"+obj2).siblings("input").removeClass("start").addClass("end");
                    break;
                case "6":
                    alert(errormsg);//对不起，已秒完
                    $("#"+obj2).siblings("input").removeClass("start").addClass("end");
                    break;
                default:
                    alert(errormsg);
                    break;
            }
          }
        });		
	});	
});
function calculagraph(){
	this._id=null;
	this._sT=null;
	this._cT=null;
	this._eT=null;
	//this._xT=null;//剩余天数
	this._lT=null;
	this._flag=true;
	this._gT=function(){
		if (this._lT==null){
		    if(this._flag){
		        this._sT=new Date(this._sT.replace(/-/g,"/"));
		        this._cT=new Date(this._cT.replace(/-/g,"/"));
		        this._eT=new Date(this._eT.replace(/-/g,"/"));
		    }
		    if(this._sT>this._cT)
		    {
		        this._lT=this._sT-this._cT;
		    }
		    else
		    {
		        this._lT=this._eT-this._cT;
		    }
		    this._lT=this._lT/1000;
		}
		if (this._lT>=0){
		    var _H=Math.floor(this._lT/3600);
		    var _M=Math.floor((this._lT-_H*3600)/60);
		    var _S=(this._lT-_H*3600)%60;
		    if(this._sT>this._cT)
			{
			    document.getElementById(this._id).innerHTML="<span class='qgwords'>倒计时</span><span>"+ _H +"</span>小时<span>"+ _M +"</span>分<span>"+ _S +"</span>秒";
			}
			else
			{
			    $("#"+this._id).siblings("input").removeClass("wait").addClass("start");
			    document.getElementById(this._id).innerHTML="<span class='qgwords'>剩余</span><span>"+ _H +"</span>小时<span>"+ _M +"</span>分<span>"+ _S +"</span>秒";
			}
			this._lT=this._lT-1;
			if(this._lT==0 && this._sT>this._cT)
			{
			    this._cT=this._sT;
			    this._lT=null;
			    this._flag=false;
			}
		}
		else
		{
		    $("#"+this._id).siblings("input").removeClass("start").addClass("end");
//			document.getElementById(this._id).innerHTML="<strong style='font-size:14px;'>秒杀结束</strong>";
			clearInterval(this._interval);
		}
	}
	this._interval=function(){
		var o = this;
		this._interval = setInterval(function(){o._gT()},1000);
	}
}

function DoTuanGou(rid,pid,gid,obj1,obj2)
{
    if($("#"+obj1).hasClass("wait")){alert('秒杀还未开始，请耐心等待');return;};
    if($("#"+obj1).hasClass("end")){alert('秒杀已结束');return;};
    $.ajax({
      type: "post",
      dataType:"json",
      url: "TGShow.aspx?NoCopyRight=1",
      data: "type=ajax&action=doTuanGou&rid=" + rid + "&pid="+pid+"&gid="+gid+"&fresh=" + Math.random(),
      cache:false, 
      success: function(data){
        var errorflag=data["errorflag"];
        var errormsg=data["errormsg"];
        switch(errorflag)
        {
            case "0":
                alert("已提交团购申请");
                break;
            case "1":
                if(confirm('请先登录,是否现在就登录?')){
                    window.location.href= 'login.aspx?ReturnUrl='+window.location.href;
                };
                break;
            case "2":
                alert(errormsg);//秒杀还未开始，请耐心等待
                break;
            case "3":
                alert(errormsg);//秒杀已结束，请等待下一期
                $("#"+obj2).siblings("input").removeClass("start").addClass("end");
                break;
            case "4":
                alert(errormsg);//对不起，您已超出限制数量
                break;
            case "5":
                alert(errormsg);//对不起，已秒完
                $("#"+obj2).siblings("input").removeClass("start").addClass("end");
                break;
            case "6":
                alert(errormsg);//对不起，已秒完
                $("#"+obj2).siblings("input").removeClass("start").addClass("end");
                break;
            default:
                alert(errormsg);
                break;
        }
      }
    });
}



