// pds js function
/**
*follow fans
*/
function follow(id,uid,value)
{
	$.post("index.php?s=aj&do=follow&action=get&uid="+uid, { uid: uid, value: value},
    function(data){
		returned = data.split("||"); 
		if(returned[0]==0)
		{
			$('#follow'+id).html('<a href="javascript:void(0);" onclick="follow('+id+','+uid+',1)" style="color:#f98f00;">(Follow)</a>');
			$('#followNums'+id).html(returned[1]);
		}
		else
		{
           
			$('#follow'+id).html('<a href="javascript:void(0);" onclick="follow('+id+','+uid+',0)" style="color:#f98f00;">Unfollow</a>');
			$('#followNums'+id).html(returned[1]);
		}
		 
		show_message(returned[2]);
    });	
}
/**
*查看徽章
**/
function showBadge(id,uid)
{
	jQuery.facebox({ajax: 'index.php?badgeId='+id+'&uid='+uid});	
}
/**
* post deal masterid
*/
function postDealMasterid(did)
{ 
    var master_id = $("#master_id").val();
	$.post("index.php?s=aj&do=editMasterId&action=get&cid="+did, { did: did,master_id:master_id},
    		function(data){
			  if(data==1)
			   {
			      alert("Edit Master Id is completed!");
				  window.location.reload(); 
				  jQuery(document).trigger('close.facebox');
				  return true;
			   }
			   else
			   {
			   	  alert("Operation is failed!");
				  return false;
			   }
   			 });
}
/**
* post deal StartDateId
*/
function postStartDateId(did)
{ 
    var startdate = $("#dealstartdate").val();
	var starttime = $("#dealstarttime").val();
	$.post("index.php?s=aj&do=editStartDateId&action=get&cid="+did, { startdate: startdate,starttime:starttime},
    		function(data){
			  if(data==1)
			   {
			      alert("Edit Start Date is completed!");
				  window.location.reload(); 
				  jQuery(document).trigger('close.facebox');
				  return true;
			   }
			   else
			   {
			   	  alert("Operation is failed!");
				  return false;
			   }
   			 });
}
/**
* post deal expire date
*/
function postDealExpireDate(did)
{ 
    var expiration_date_dt = $("#expiration_date_dt").val();
	$.post("index.php?s=aj&do=editDealExp&action=get&cid="+did, { did: did,expiration_date_dt:expiration_date_dt},
    		function(data){
			  if(data==1)
			   {
			      alert("Edit deal expire date  is completed!");
				  window.location.reload(); 
				  jQuery(document).trigger('close.facebox');
				  return true;
			   }
			   else
			   {
			   	  alert("Operation is failed!");
				  return false;
			   }
   			 });
}
/**
* post deal commision  configuer
*/
function postDealCommision(did)
{ 
	var is_special_pts = $("input[@name='is_special_pts']:checked").val();
	var commision = $("#commision").val();
	var commision_type = $("#commision_type").val();
	var buyer_comm = $("#buyer_comm").val();
	var poster_comm = $("#poster_comm").val();
	$.post("index.php?s=aj&do=postDealCommision&action=get&cid="+did, { did: did,commision:commision,commision_type:commision_type,buyer_comm:buyer_comm,poster_comm:poster_comm,is_special_pts:is_special_pts},
    		function(data){
			  if(data==1)
			   {
			      alert("Edit deal expire date  is completed!");
				  window.location.reload(); 
				  jQuery(document).trigger('close.facebox');
				  return true;
			   }
			   else
			   {
			   	  alert("Operation is failed!");
				  return false;
			   }
   			 });
}

/**
 * 通过JS消息提示，在显示设定时间后隐藏
 * @param show_message 提示的消息内容 如 “发布成功”；默认为空
 * @param show_time 消息显示的时间，单位为秒；默认显示 “3” 秒
 * @param show_title 提示的消息标题，默认为“提示”
 * @param tigBoxClass 消息提示层所使用的样式，默认为“tigBox_6”
 * @成功样式为默认
 * @警告信息请将 tigBoxClass 设置为alert
 * @错误信息请假 tigBoxClass 设置为erro
 */
function show_message(show_message,show_time,show_title,tigBoxClass)
{
    var show_message = (undefined==show_message ? '' : show_message);

    if(show_message)
    {
        var show_time = (undefined==show_time ? 2 : show_time);
        var show_title = (undefined==show_title ? 'Message' : show_title);
        var tigBoxClass = (undefined==tigBoxClass ? 'tigBox_6' : tigBoxClass);
        if(tigBoxClass=='alert')
		{
          var smaHTML = '<div id="tigBox" class="tigBox_6"><ul class="warnBox"><li><div class="tt1">' + show_title + '</div><div class="alert"><div class="wwsp">' + show_message + '</div></div></li></ul></div>';
		}
		else if(tigBoxClass=='erro')
		{
		   var smaHTML = '<div id="tigBox" class="tigBox_6"><ul class="warnBox"><li><div class="tt1">' + show_title + '</div><div class="erro"><div class="wwsp">' + show_message + '</div></div></li></ul></div>'; 	
		}
		else
		{
			 var smaHTML = '<div id="tigBox" class="' + tigBoxClass + '"><ul class="warnBox"><li><div class="tt1">' + show_title + '</div><div class="wWarp"><div class="wwsp">' + show_message + '</div></div></li></ul></div>';
		}

        $('#show_message_area').html(smaHTML);

        var i=0;

        setTimeout(function(){i += 1;},1000);

    	var tigBoxObj = document.getElementById("tigBox");

        tigBoxObj.style.visibility = "visible";

        setTimeout(function() {
            i += 1;
            tigBoxObj.style.visibility= "hidden";
        },(show_time * 1000));
    }
}
/***展示过期deal**/
function showExpireDeals(obj,type)
{
	if(type==1)
	{
		if(obj.checked==1)
		{ 
		 window.location.href="all-deals.html";
		 
	}
	else
	{
		 window.location.href="deals.html";
	}
	}
	else if(type==2)
	{
		if(obj.checked==1)
		{ 
		 window.location.href="all-newest-deal.html";
		 
		}
		else
		{
		 window.location.href="newest-deals.html";
		}	
	}
	else if(type==3)
	{
		if(obj.checked==1)
		{ 
		  window.location.href="all-deals-list.html";
		 
		}
		else
		{
		  window.location.href="deals-list.html";
		}	
		
	}
	else if(type=4)
	{
		if(obj.checked==1)
		{ 
		 window.location.href="all-newest-deal-list.html";
		 
		}
		else
		{
		 window.location.href="newest-deals-list.html";
		}	
    }
	else
	{
		 window.location.href="all-deals.html";
	}
}

$(function(){
		   
/**评论投票按钮 顶**/		   
$(".DDCCVoteAdd").one('click',function(){
	var uid = $("#user_id").val();
	var $parent = $(this).parent();
    var cid = $parent.attr("id");
	if(uid>0)
	{
		$.post("index.php?s=aj&do=doComVote&action=get&cid="+cid, { uid: uid, vote: 1},
    	function(data){
		if(data !=-1)
		{
			var str = '<span class="DDCCVoteAdd1"></span><span class="DDCCVoteNum">'+data+'</span><span class="DDCCVoteLess1"></span>';
			$parent.html(str);
		}
		else
		{
		    alert("Vote Failed!");	
		}
     });	
	}
	else
	{
		fb('signin');
	}
	
});

/**评论投票按钮 减**/		   
$(".DDCCVoteLess").one('click',function(){
	var uid = $("#user_id").val();
	var $parent = $(this).parent();
    var cid = $parent.attr("id");
	if(uid>0)
	{
		$.post("index.php?s=aj&do=doComVote&action=get&cid="+cid, { uid: uid, vote: 0},
    	function(data){
		if(data !=-1)
		{
			var str = '<span class="DDCCVoteAdd1"></span><span class="DDCCVoteNum">'+data+'</span><span class="DDCCVoteLess1"></span>';
			$parent.html(str);
		}
		else
		{
		    alert("Vote Failed!");	
		}
     });	
	}
	else
	{
		fb('signin');
	}
	
});

//首页优惠券投票功能 +
$(".dealVoteDui").click(function(){
		var userID = $("#user_id").val();		
		if(userID>0)
		{
			var $dd = $(this).parent().parent();
			var cid = $dd.attr("id");
			$.post("index.php?s=aj&do=vote&cid="+cid, { cid: cid, userID: userID, vote:1  },
			function(data){ 
			returned = data.split("||"); 
			var rate = returned[3];
			//显示成功率
			var reStr = '<div class="dealVoteShu">'+rate+'%<div class="dealVoteShuFloat">'+rate+'% the use of success</div></div>';
			reStr += '<div class="dealVoteDuiHuise"></div><div class="dealVoteChaHuise"></div>';
			//$("#rate"+cid).html(rate+'%');
			$("#vote"+cid).html(reStr);
			
			});
			
			
	    }
		else
		{
		   fb('signin');
		   return false;
		
		} 
		
		//$(this).attr("class","scouponVoteIs1")
	});

//首页优惠券投票功能 -
$(".dealVoteCha").click(function(){
		var userID = $("#user_id").val();
		if(userID>0)
		{
			var $dd = $(this).parent().parent();
			var cid = $dd.attr("id");
			$.post("index.php?s=aj&do=vote&cid="+cid, { cid: cid, userID: userID, vote:0  },
			function(data){ 
			returned = data.split("||"); 
			var rate = returned[3];
			//显示成功率
			var reStr = '<div class="dealVoteShu">'+rate+'%<div class="dealVoteShuFloat">'+rate+'% the use of success</div></div>';
			reStr += '<div class="dealVoteDuiHuise"></div><div class="dealVoteChaHuise"></div>';
			//$("#rate"+cid).html(rate+'%');
			$("#vote"+cid).html(reStr);
			
			});
	    }
		else
		{
		   fb('signin');
		   return false;
		
		} 
		
		//$(this).attr("class","scouponVoteIs1")
	});

});
//编辑deal
function doDealEdit(did)
{
	jQuery.facebox({ajax: 'index.php?editDealId='+did});
}
//admin edit deal
function doAdminDealEdit(eid) {jQuery.facebox({ajax: 'admin/ajax.php?do=4&action=userEdit&type=admin&id='+eid}); return false;}
//deal 过期设置
function doDealExpired(did)
{
	 
	 if(confirm("Are you sure you want this deal is set to expire ?"))
	 {
		 $.post("index.php?s=aj&do=doDealExpired&action=get&did="+did,{did:did},function(data){
			alert(data);
			window.location.reload(); 
	 	});
	 }
	 else
	 {
		  return false;
	  }
}
/**
*积分兑换栏目js
**/
function doExchange()
{
	var user_point = $("#user_point").val();
	 var change_point = $("#change_point").val();
	 var reg =  /^[1-9]+[0-9]*]*$/;
	 var card_id = $("#card_id").val();
	 if(!isNaN(change_point))
	 {
	 	if(change_point*1>0&&reg.test(change_point))
		{
	 		if(change_point*1>user_point*1)
	 		{
	     		alert("你当前积分"+user_point+"点积分,不能兑换"+change_point+"积分!");
		 		$(this).val(user_point);
		 		$("#change_poin").focus();
		 		 $("#user_point").attr('class','inputPoints3');
				change_point = user_point;
				return false;
	 		}
		}
		else
		{
		     alert("您要兑换的积分必须为大于零的整数");
			  $("#user_point").val(user_point);
			  $("#user_point").attr('class','inputPoints3');
			 change_point = user_point;
			 return false;
		}
	  }
	  else
	  {
	     alert("您要兑换的积分必须为数值");
		 $("#user_point").val(user_point);
		  $("#user_point").attr('class','inputPoints3');
		 change_point = user_point;
		 return false;
	  }
	 jQuery.facebox({ajax: 'index.php?card_id='+card_id+'&point='+change_point}); 
	 return false;
}
function subExchange()
{
	var card_id = $("#card_id").val();
	var change_point = $("#change_point").val();
	var user_paypal = $("#user_paypal").val();
	var update = 0;
	if($("#update").attr("checked")==true)
	{
		update = 1;
	}
	
	
	var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	   if(!myreg.test(user_paypal))
          {
			  alert('Please enter the correct paypal account.');
			  $("#user_paypal").focus();
			  return false;
		}
		
	 $.post("index.php?s=aj&do=exchange_cash_card", { change_point: change_point,card_id:card_id,user_paypal:user_paypal,update:update},
		function(data){
			if(data!=0)
			{
				 window.location.href="success/"+data; 
			}
			else
			{
				show_message("Exchange failed,please contact website administrator",5,"Warning","alert");
			}
		});
}
//gift卡兑换
function doGiftExchange()
{
	var gift_card_id = $("#card_id").val();
	var card_nums = $("#QTY").val();
 	 jQuery.facebox({ajax: 'index.php?gift_card_id='+gift_card_id+'&nums='+card_nums}); 
	 return false;
}
function subGiftExchange()
{
	var card_id = $("#card_id").val();
	var nums = $("#nums").val();
	var email = $("#email").val();
	var address = '';
	var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	if(email.length>1)
	{
	  if(!myreg.test(email))
      {
			  alert('Please enter the correct email address.');
			  $("#email").focus();
			  return false;
	 }
	 address = email;
	}
	 $.post("index.php?s=aj&do=exchange_gift_card", {card_id:card_id,nums:nums,address:address},
		function(data){
			if(data!=0)
			{
				 window.location.href="success/"+data; 
			}
			else
			{
				alert("Exchange failed,please contact website administrator");
			}
		});
}
//保留两位小数点
function changeTwoDecimal(x)
{
var f_x = parseFloat(x);
if (isNaN(f_x))
{
alert('function:changeTwoDecimal->parameter error');
return false;
}
var f_x = Math.round(x*100)/100;

return f_x;
}
/**
*积分兑换栏目js结束
**/
//管理员屏蔽deal
function unApproveDeal(did)
{
	 if(confirm("Are you sure you want this deal is set to unapprove ?"))
	 {
		 $.post("index.php?s=aj&do=unApproveDeal&action=get&did="+did,{did:did},function(data){
		  alert(data);
			top.location='deals.html'; 
	 	});
	 }
	 else
	 {
		  return false;
	  }
}
//js 日期格式化函數
Date.prototype.format = function(format)
{
var o =
{
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(),  //day
"h+" : this.getHours(),  //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3),  //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format))
format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
return format;
}
//优惠券评论
function postNewCouponComment(cid){ 
  if($('#comment'+cid).val()=='Your New Comment will be written here'||$('#comment'+cid).val()=='')
  {
	  alert('Please input your comment content!');
	  return false;
  }
    $.post("index.php?s=aj&do=cmntAdd&action=get&cid="+cid, { commentUname: $('#commentUname').val(), commentUid: $('#commentUid').val(), comment: $('#comment'+cid).val()},
    function(data){
		if(data !='wait')
		{
        	$('#comments'+cid).append(data);
        	$('#comments'+cid).show();
			$('#comment'+cid).val('');
		}
		else if(data=='wait')
		{
			show_message("Please your comment is being reviewed later.");
			$('#comment'+cid).val('');
		}
		else
		{
			alert("Comment failed!");
		}
    }); 
	return true;   

   //jQuery(document).trigger('close.facebox');
}


