<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ezine_sendmail.aspx.cs" Inherits="JumbotCms.WebFile.Admin._ezine_sendmail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html   xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta   http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta   http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title></title>
<script type="text/javascript" src="../_libs/jquery.tools.pack.js"></script>
<script type="text/javascript" src="../_data/jcmsV4.x.js"></script>
<link type="text/css" rel="stylesheet" href="../_data/jcmsV4.x.css" />
<link type="text/css" rel="stylesheet" href="css/common.css" />
<script type="text/javascript" src="../_libs/jquery.tree.js"></script>
<link   rel="stylesheet" type="text/css" href="../_libs/jquery.tree/style.css" />
<script type="text/javascript">
var _sendok = false;//已经发送成功过
$(document).ready(function(){
	getTreeJSON();
});
function getTreeJSON(){
	$('#btnSaveContent').attr("disabled",true);
	$.getJSON("email_ajax.aspx?oper=ajaxTreeJson&eid=<%=eId%>&limit=<%=site.MailOnceCount*3%>&time="+(new Date().getTime()), {}, function(treedata) {
		var o = {
			showcheck: true,
			cbiconpath: site.Dir + "_libs/jquery.tree/images/",
			oncount: function() {
				$('#EmailList').html('<ul></ul>');
				var s = $("#tree").getTSVs();
				if (s != null){
					var e = [];
                			var l = s.length;
                			for (var i = 0; i < l; i++) {
                    				if ( s[i].indexOf("@") > -1 ){
							e[e.length] = s[i];
            						$('#EmailList ul').append('<li><a href="javascript:void(0);" class="group_link" key="' + s[i] + '"><span style="float:left;">' + e.length + ': ' + s[i] + '</span><div class="icon_ready">!</div></a></li>');
						}
                			}
					$('#txtToMailList').val(e.join(","));
					if(e.length==0)
						$('#btnSaveContent').attr("disabled",true);
					else
						$('#btnSaveContent').attr("disabled",false);
					$('#EmailCount').html('目标邮件：' + e.length);
				}
				else{
					$('#txtToMailList').val('');
					$('#btnSaveContent').attr("disabled",true);
					$('#EmailList ul').html('');
					$('#EmailCount').html('目标邮件：0');
				}
			},
                        url: "email_ajax.aspx?oper=ajaxTreeJson&eid=<%=eId%>&limit=<%=site.MailOnceCount*3%>&time="+(new Date().getTime())
		};
		o.data = treedata;
		$("#tree").treeview(o);//使用
	}); 
}
function FormatMailList(id)
{
	var _val = $('#'+id).val();
	$('#'+id).val(formatmail(_val));
}
var formatmail = function(list){
	var _val = list;
	_val =_val.replace(/\r\n/g,"\n");
	_val =_val.replace(/[,\n]+/g,",");
	_val =_val.replace(/[\n]+/g,",");
	_val =_val.replace(/[,]+/g,",");
	_val =_val.replace(/ /g,"");
	return _val;
}
var ReplaceAll = function(strOrg, strFind, strReplace) {
    var index = 0;
    while (strOrg.indexOf(strFind, index) != -1) {
        strOrg = strOrg.replace(strFind, strReplace);
        index = strOrg.indexOf(strFind, index);
    }
    return strOrg;
} 

function SendMail(){
	var Mails = $('#txtToMailList').val();
	var mailcount = Mails.replace(/[^,]/g,   "").length + 1;
	if(Mails.length<1)
		alert("请选择收件人");
	else if(mailcount > <%=site.MailOnceCount*3%>)
		alert("单次发送，收信人数超过了<%=site.MailOnceCount*3%>");
	else{
		_sendok = false;
		ajaxSendMail(Mails);
	}
}
function ajaxSendMail(Mails){
	var isOnce = true;//是否单次发送
	var thismails = Mails;
	var lastmails = "";
	var mailcount = Mails.replace(/[^,]/g,   "").length + 1;
	if(mailcount==0) return;
	if ( mailcount > <%=site.MailOnceCount%> ){//最多一次发<%=site.MailOnceCount%>
		var e = [];
                var s = Mails.split(',');;
                for (var i = 0; i < <%=site.MailOnceCount%>; i++)
			e[e.length] = s[i];
		thismails = e.join(",");
		lastmails = Mails.substring(thismails.length+1);
		isOnce = false;
	}
	JumbotCms.Loading.show("邮件发送中，请稍等...",260,80);
	$.ajax({
		type:		"post",
		dataType:	"json",
		data:		"tomail="+thismails+"&title="+encodeURIComponent($('#txtTitle').val())+"&body="+encodeURIComponent($('#txtBody').val()),
		url:		"ezine_ajax.aspx?oper=sendmail&eid=<%=eId%>&time="+(new Date().getTime()),
		error:		function(XmlHttpRequest,textStatus, errorThrown){JumbotCms.Loading.hide();alert(XmlHttpRequest.responseText); },
		success:	function(d){
			switch (d.result)
			{
			case '-1':
				JumbotCms.Alert(d.returnval, "0", "top.window.location='login.aspx';");
				break;
			case '0':
				JumbotCms.Alert(d.returnval, "0");
				break;
			case '1':
				JumbotCms.Loading.hide();
				returnResult(thismails,d.returnval);
				if(!isOnce)
					ajaxSendMail(lastmails);
				else{
					if(_sendok == true) getTreeJSON();//没发送成功就不需要刷新
				}
				break;
			}
		}
	});
}
//返回发送结果
var returnResult = function(keys,result) {
	$('#EmailList').find('a').each(function() {
		if ( (","+keys+",").indexOf(","+$(this).attr('key')+",") > -1 ){
			var n = $(this).parent().html();
			n = ReplaceAll(n, 'icon_ignore', 'icon_ready');
			n = ReplaceAll(n, 'icon_success', 'icon_ready');
			n = ReplaceAll(n, 'icon_failure', 'icon_ready');
			n = ReplaceAll(n, 'icon_ready', 'icon_'+result);
			$(this).parent().html(n);
		}
	})
	if(result == "success") _sendok = true;
}
</script>
<style>
#EmailList ul { list-style:none; padding:0px; margin:0px; }
#EmailList ul li { list-style:none; padding:0px; margin:0px; }
a.group_link{
	display:block;
	height:22px;
	line-height:22px;
}
.icon_ready{
	float:left;
	width:24px;
	height:22px;
	overflow:hidden;
	text-indent:-999em;	
	background:url(images/ready.gif) no-repeat;
}
.icon_loading{
	float:left;
	width:24px;
	height:22px;
	overflow:hidden;
	text-indent:-999em;	
	background:url(images/loading.gif) no-repeat;
}
.icon_ignore{
	float:left;
	width:24px;
	height:22px;
	overflow:hidden;
	text-indent:-999em;	
	background:url(images/ignore.gif) no-repeat;
}
.icon_failure{
	float:left;
	width:24px;
	height:22px;
	overflow:hidden;
	text-indent:-999em;	
	background:url(images/failure.gif) no-repeat;
}
.icon_success{
	float:left;
	width:24px;
	height:22px;
	overflow:hidden;
	text-indent:-999em;	
	background:url(images/success.gif) no-repeat;
}
</style>
</head>
<body>
<table class="helptable">
        <tr>
            <td>
                <ul>
                    <li>一次最多可选择的邮箱为:<%=site.MailOnceCount*3%>封，间隔周期:<%=site.MailTimeCycle%>秒</li>
                </ul>
            </td>
        </tr>
</table>
<form id="sendmailForm" runat="server">
	<table cellspacing="0" cellpadding="0" width="100%" style="display:none;">
		<tr>
			<td><asp:TextBox ID="txtTitle" runat="server" Width="97%" CssClass="ipt"></asp:TextBox></td>
		</tr>
		<tr style="display:none;">
			<td><asp:TextBox ID="txtBody" runat="server" Width="97%" Height="440" CssClass="ipt"></asp:TextBox></td>
		</tr>
		<tr style="display:none;">
			<td><textarea onblur="FormatMailList('txtToMailList');" name="txtToMailList" id="txtToMailList" style="height:97px;width:97%;" class="ipt"></textarea></td>
		</tr>
	</table>
	<table cellspacing="0" cellpadding="0" width="100%" class="formtable">
		<tr>
			<td width="250" valign="top">
				<div style="width:248px;"><a name="grouplist"></a>每个分组只显示前<%=site.MailOnceCount*3%>名</div>
				<div style="border-bottom: #c3daf9 1px solid; border-left: #c3daf9 1px solid; width: 248px; height: 380px; overflow: auto; border-top: #c3daf9 1px solid; border-right: #c3daf9 1px solid;">
					<div id="tree"></div>
				</div>
			</td>
			<td valign="top">
				<div id="EmailCount">目标邮件：0</div>
				<div id="EmailList" style="height: 380px; overflow: auto;"><ul></ul></div>
			</td>
		</tr>
	</table>
	<div class="buttonok">
		<input type="button" value="发送" id="btnSaveContent" class="btnsubmit" onClick="SendMail();" />
	</div>
</form>
</body>
</html>
