/*
$import("com.tiexue.BaseCookie",true);
$import("com.yui.YAHOO.util.Connect",true);
$import("com.yui.YAHOO.widget.Dialog",true);
friend.js
*/

document.domain = "tiexue.net";

if (typeof(TopPanel)!='function'){
	document.write("<script type=\"text/javascript\" src=\"http://js.tiexue.net/com/tiexue/TopPanel.js\"></script>");
}

//短信配置
var MsgConfig={
	NewInfoUrl	:"http://msg.tiexue.net/Out/ToolBar.aspx?FunName=GetMyNewNote",	//获取是否有新短信
	//短信服务链接地址
	ReadNewMsgUrl	:"http://msg.tiexue.net/Out/ReadNote.aspx",	//读新短信
	SendMsgUrl	:"http://msg.tiexue.net/Out/SendMsg.aspx",		//短信发送链接地址
	ValidateUrl	:"http://msg.tiexue.net/Dlg/Validate.aspx",		//验证码

	RefreshMsgTimeLength:10,	//取新短信的时间间隔(分钟)
	RefreshDataTime	: 10	//从缓冲区获取新数据的时间间隔(秒)
}

document.write("<div id='TxCustomDiv' style='z-Index:998;position:absolute;display:none;' class='dd-demo'></div>");

//自定义对话框
function CustomDlg(CustomDivID,DragObjID){
	this.CustomObj = null;
	this.CustomDivID = CustomDivID;
	if (typeof CustomDivID == 'string'){
		this.CustomObj = document.getElementById(CustomDivID);
	}else{
		this.CustomObj = CustomDivID
		this.CustomDivID = this.CustomObj.getAttribute("id");
	}
	this.DragObjID = DragObjID;
	this.moveObj = null;
	this.orig_scroll = null;
	this.DraggableFlag = true;
	var arr = navigator.appVersion.split("MSIE");
	if (arr.length==2){
		var version = parseFloat(arr[1]);
		if (version<7){
			this.DraggableFlag= false;
		}
	}
}
CustomDlg.prototype = {
	Show : function(html,IsFixCenter){
		with(this.CustomObj){
			innerHTML = html;
			style.display = "";
			if (IsFixCenter) TxTopPanel.FixCenter(this.CustomDivID);
			if (this.DraggableFlag) drag(this.CustomDivID,this.DragObjID,true);
		}
	},
	Close:function(){
		if (this.CustomObj.innerHTML=="")return;

		if (this.orig_scroll!=null){
			window.onscroll = this.orig_scroll;
		}

		this.CustomObj.innerHTML = "";
		this.CustomObj.style.display = "none";
		
		this.moveObj = null;
		CollectGarbage();
	},
	YUIShow:function(Subject,Content){
		var temp = "<div style='visibility:visible;'>";	//z-index:2;top:3px;bottom:-3px;right:-3px;left:3px;background-color:#000;opacity:.12;filter:alpha(opacity=12);

		temp += "<div class='MsgNoteDiv yui-module yui-overlay yui-panel' style='visibility:inherit;width:430px;height:100%;'>";
		temp += "<div id='TxMsgDialog_h' class='hd'";
		if (this.DraggableFlag) temp += " style='cursor: move'";
		temp += ">";
		temp += "		<div class='tl'></div>";
		temp += "		<div style='text-align: left'>";
		temp += "			<font size='2'>" + Subject + "</font>";
		temp += "		</div>";
		temp += "		<div class='tr'></div>";
		temp += "	</div>";
		temp += "	<div class='bd'>";
		temp += Content;
		temp += "	</div>";
		temp += "	<div class='container-close' id='YUICloseBt'>&nbsp;</div>";
		temp += "</div>";

		temp += "<div class='underlay'></div></div>";
		this.Show(temp,true);
		var o=this; 
		YAHOO.util.Event.onAvailable("YUICloseBt",function(){var obj = document.getElementById("YUICloseBt");obj.onclick = function(){o.Close();}});
		temp=null;
	}
}
var TxCustomDlg=null;

var DlgInfo = null;	//短信管理实例
	
//写信短信，编辑新短信，回复
MsgDlgInfo = function(AutoDetect,callBackFun){	//自动检测新短信标志
	this.CallBack = callBackFun;
	this.oIntervalCache = null;	//新短信检测时钟
	this.NewInfoCount = 0;	//新短信总数
	this.UpdateInfo=true;	//显示需要更新

	//新短信总数更新服务
	this.Tx_GetMsgCount_Cache();
	if (FriendInfo==null) FriendInfo=new GoodFriend();
}

//服务器段的反馈信息,同时设置数据的失效时间
function Tx_NewMsgFeedback(Count){
	if (DlgInfo==null) return;
	DlgInfo.NewInfoCount = Count;
	//更新缓冲Cookie值
	BaseCookie.SetCookie("TxMsgNew",Count,MsgConfig.RefreshMsgTimeLength*60);//3600*24
	if (typeof DlgInfo.CallBack == 'function'){
		DlgInfo.CallBack(Count);
	}
}

MsgDlgInfo.prototype = {
	RunTimer:function(AutoDetect){
		if (AutoDetect){
			if (this.oIntervalCache==null){
				DlgInfo.Tx_GetMsgCount_Cache();
				this.oIntervalCache=window.setInterval("DlgInfo.Tx_GetMsgCount_Cache()",MsgConfig.RefreshDataTime * 1000);	//取缓冲新短信数据(15秒)
			}
		}else{
			if (this.oIntervalCache!=null){
				window.clearInterval(this.oIntervalCache);
				this.oIntervalCache=null;
			}
		}
	},
	//读取一条新短信消息
	ReadOneInfo:function(){
		var temp = BaseCookie.GetCookie("TxMsgNew");
		//如果Cookie获取为空值,立即从服务器段获取
		if (temp==null){
			BaseCookie.SetCookie("TxMsgNew","0",MsgConfig.RefreshMsgTimeLength*60);
			this.Tx_GetMsgCount_Server();
			return;
		}
		if (temp=="")temp="0";
		this.NewInfoCount = parseInt(temp);
		if (this.NewInfoCount>0) this.NewInfoCount--;

		//更新缓冲Cookie值
		BaseCookie.SetCookie("TxMsgNew",this.NewInfoCount,MsgConfig.RefreshMsgTimeLength*60);
		if (typeof this.CallBack == 'function'){
			this.CallBack(this.NewInfoCount);
		}
	},
	ClearCookie:function(){
		this.NewInfoCount = 0;
		//删除缓冲Cookie值
		BaseCookie.ClearCookie("TxMsgNew");

	},
	//初始化短信Panel
	InitPanel:function(){},
    GetJsObj:function(JsName){
		var objJs = document.getElementById(JsName);
		if (objJs==null){
			var IsFinish = true;
			if(document.all){
				if (document.readyState!="complete") IsFinish=false;
			}
			if (!IsFinish) return null;

			var objJs = document.createElement("script");
			objJs.setAttribute("id",JsName);
			objJs.setAttribute("language","javascript");
			try{document.body.appendChild(objJs);}catch(e){return;}
		}
		return objJs;
    },
	//从服务器端获取新短信数据
	Tx_GetMsgCount_Server:function(){
		this.ClearCookie();

		var objJs = this.GetJsObj("TxMsgJs");
		if (objJs==null)return;

		var TxTB_UserID = BaseCookie.TxBBS_CurrentUserID();
		if (TxTB_UserID==0) return;

		var Url = MsgConfig.NewInfoUrl + "?tmp=" + Math.round(Math.random()*10000) ;
		objJs.src = Url;
	},
	//从缓冲区中取判断是否有新短信
	Tx_GetMsgCount_Cache:function(){
		//读取Cookie
		var temp = BaseCookie.GetCookie("TxMsgNew");
		//如果Cookie获取为空值,立即从服务器段获取
		if (temp==null){
			BaseCookie.SetCookie("TxMsgNew","0",MsgConfig.RefreshMsgTimeLength*60);
			this.Tx_GetMsgCount_Server();
			return;
		}
		if (temp=="")temp="0";
		var Count = parseInt(temp);
		var NeedUpdate = (this.NewInfoCount != Count || this.UpdateInfo);
		this.NewInfoCount = Count;

		if (NeedUpdate){
			this.UpdateInfo=true;
			//if (typeof this.CallBack == 'function'){
				try{
					this.CallBack(Count);
					this.UpdateInfo=false;
				}catch(e){}
			//}
		}
	},

	//从服务器获取最新的短信数量
	GetNewInfo:function(){
		this.Tx_GetMsgCount_Server();
	},

	//网页调用获取新短信的总条数
	NewInfoSum : function(){
		return this.NewInfoCount;
	},

	//创建、转发、回复短信对话框
	CreateInfo : function(UserName,Title,Content)
	{
		if (TxCustomDlg==null) TxCustomDlg = new CustomDlg("TxCustomDiv","TxMsgDialog_h");

		var inputBoxHeight=20;
		if (xmlns1999) inputBoxHeight = 15;
		//短信对话框网页脚本数据
		var inputstyle = "";
		var temp = "<style>";
		temp += '.inputInfo{BORDER-LEFT: #84a1bd 1px solid;BORDER-TOP:#84a1bd 1px solid;BORDER-RIGHT:#84a1bd 1px solid;BORDER-BOTTOM: #84a1bd 1px solid;FONT-SIZE:12px;PADDING-LEFT:2px;PADDING-TOP:2px;PADDING-RIGHT:2px;PADDING-BOTTOM:0px;text-valign: middle;}\r\n';
		temp += '#msgContent{';
		temp += 'font-size:10pt;SCROLLBAR-HIGHLIGHT-COLOR: #ffffff; SCROLLBAR-SHADOW-COLOR: #cccccc; COLOR: #000000; SCROLLBAR-3DLIGHT-COLOR: #cccccc;SCROLLBAR-FACE-COLOR: #ffffff;SCROLLBAR-ARROW-COLOR: #666666;SCROLLBAR-TRACK-COLOR: #ffffff;SCROLLBAR-DARKSHADOW-COLOR: #fff; FONT-FAMILY: 宋体;';
		temp += 'BORDER-RIGHT: #333 1px dashed; PADDING-RIGHT: 6px; BORDER-TOP: #333 1px dashed; PADDING-LEFT: 6px; PADDING-BOTTOM: 6px; BORDER-LEFT: #333 1px dashed; PADDING-TOP: 6px; BORDER-BOTTOM: #333 1px dashed';
		temp += '}';
		temp += '</style>';
		temp += '<form id="thisForm" name="thisForm" method="post" action="return false">';	//SendFlag MsgID
		temp += '<table cellSpacing="0" cellPadding="0" width="97%" height="100%" border="0" align="center">';
		temp += '<tr>';
		temp += '	<td noWrap width="52">收件人：</td>';
		temp += '	<td noWrap width="214"><input title="收件人" onKeyUp="SendTo_onkeyup()" style="WIDTH:210px;HEIGHT:' + inputBoxHeight + 'px" maxLength="60" size="100" ID="UserList" NAME="UserList" class="inputInfo"></td>';
		temp += '	<td>';
		temp += '		<select onChange="return SelGoodFriend()" ID="SelUser" NAME="SelUser" class="inputInfo">';
		temp += '		<option value="-1" selected>--选好友--</option>';
		temp += '		</select>';
		temp += '	</td>';
		temp += '</tr>';
		temp += '<tr><td colspan="3"></td></tr>';
		temp += '<tr>';
		temp += '	<td noWrap>主题：</td>';
		temp += '	<td colspan="2" noWrap align="left"><input id="Title" style="WIDTH:210px;HEIGHT:' + inputBoxHeight + 'px" maxLength="80" NAME="Title" class="inputInfo"></td>';
		temp += '</tr>';
		temp += '<tr>';
		temp += '	<td align="center" colspan="3">';
		temp += '		<textarea title="键入短信文本内容" style="WIDTH:97%;height:180px;" name="msgContent" rows="12" cols="60" ID="msgContent"></textarea>';
		temp += '	</td>';
		temp += '</tr>';
		temp += '<tr>';
		temp += '	<td colspan="3" align="left"><input type="checkbox" value="on" ID="ToSendedCheck" NAME="ToSendedCheck">将发送短信保存到“已发送的”中&nbsp;</td>';
		temp += '</tr>';
		temp += '<tr>';
		temp += '	<td height="12" colspan="3" valign="bottom" align="left">';
		inputBoxHeight=22;
		if (xmlns1999) inputBoxHeight = 17;
		temp += '		验证码:<INPUT id="ValidateCode" style="WIDTH:72px;HEIGHT:' + inputBoxHeight + 'px" NAME="ValidateCode" class="inputInfo"> <IMG id="MsgCodeImage" alt="" src="" align="absMiddle" border="0">&nbsp;';
		temp += '		<INPUT type="button" value="刷新验证码" onClick="RefreshCode()" ID="RefreshCodeBt" class="inputInfo">';
		temp += '	</td>';
		temp += '</tr>';
		temp += '<tr><td colSpan="3" align="left"><b>说明</b>：&nbsp;&nbsp;标题最多<b>50</b>个字符，内容最多<b>500</b>个字符</td></tr>';
		temp += '<tr>';
		temp += '	<td vAlign="middle" align="center" colSpan="3">';
		temp += '		<INPUT id="SendMsgBt" type="button" value="发送短信" onClick="SendSubmit(null);" class="inputInfo">&nbsp;';
		temp += '		<INPUT id="SaveMsgBt" type="button" value="保存短信" onClick="SendSubmit(-1);" class="inputInfo">&nbsp;';
		temp += '	</td>';
		temp += '</tr>';
		temp += '</table>';
		temp += '</form>';

		TxCustomDlg.YUIShow("<font size ='2'>发送短信</font>",temp);
		
		RefreshCode();

		if (UserName!=null){
			YAHOO.util.Event.onAvailable("UserList",function(){thisForm.UserList.value = UserName;});
		}
		if (Title!=null){
			YAHOO.util.Event.onAvailable("Title",function(){thisForm.Title.value = Title;});
		}
		if (Content!=null){
			YAHOO.util.Event.onAvailable("msgContent",function(){thisForm.msgContent.value = Content;});
		}
		//取好友信息
		var temp = FriendInfo.GetMyFriendsFromCookie();
		if (temp!=""){
			Tx_FillFriendData(temp);
		}
		temp = null;
		
	},

	ReadNoteDlg:function(MsgID){
		if (TxCustomDlg==null) TxCustomDlg = new CustomDlg("TxCustomDiv","TxMsgDialog_h");

		var temp = MsgConfig.ReadNewMsgUrl;
		if (typeof(MsgID)!='undefined')	temp = MsgConfig.ReadNewMsgUrl + "?MsgID=" + MsgID;
		TxProcessShow("正在读取,请等待...");
		temp = "<iframe src=\"" + temp + "\" scrolling=\"no\" width=\"100%\" height=\"330px\" frameborder=\"0\" onload=\"TxProcessHide()\"></iframe>";
		TxCustomDlg.YUIShow("<font size ='2'>读短信</font>",temp);
	},

	//读新短信消息
	ReadNewMsg : function(){
		if (TxCustomDlg==null) TxCustomDlg = new CustomDlg("TxCustomDiv","TxMsgDialog_h");

		//判断是否有新短信
		if (this.NewInfoCount==0){
			TxAlert('没有新的短信消息！');
			return;
		}
		
		this.ReadNoteDlg();
		/*TxProcessShow("正在读取,请等待...");
		with(YAHOO.example.container.MsgDialog){
			setHeader("<div class='tl'></div><font size ='2'>读短信</font><div class='tr'></div>");
			setBody("<iframe src=\"" + MsgConfig.ReadNewMsgUrl + "\" scrolling=\"no\" width=\"100%\" height=\"330px\" frameborder=\"0\" onload=\"TxProcessHide()\"></iframe>");
			show();
		}*/
	},
	//读指定编号的短信
	ReadMsg : function(MsgID){
		if (TxCustomDlg==null) TxCustomDlg = new CustomDlg("TxCustomDiv","TxMsgDialog_h");

		this.ReadNoteDlg(MsgID);
	},

	//关闭短信对话框
	CloseWindow : function(){
		if (TxCustomDlg!=null) TxCustomDlg.Close();
	}
}
//短信对话框关闭
function TxMsgCloseWindow(){
	DlgInfo.CloseWindow();
}
//短信脚本内容

function RefreshCode(){
	document.getElementById("MsgCodeImage").src = MsgConfig.ValidateUrl + '?tmp=' + Math.random();
}

function Tx_FillFriendData(value){
	var FriendID,Friend;
	if (value!=""){
		var tempArr;
		var arr=value.split(";");
		try{
		with(thisForm){
			while(SelUser.options.length>0) SelUser.remove(0);
			SelUser.options.add(new Option("--选择好友--",-1));
			for (var i=0;i<arr.length;i++){
				tempArr=arr[i].split(",");
				FriendID=tempArr[0];
				Friend=tempArr[1];
				SelUser.options.add(new Option(Friend,FriendID));
			}
		}
		}catch(e){}
	}
}

function SelGoodFriend(){
	with(thisForm){
		if (SelUser.selectedIndex==0){
			if (SelUser.options[SelUser.selectedIndex].value==-1) return;
		}
		FriendName=SelUser.options[SelUser.selectedIndex].text;
		if (UserList.value=="")
			UserList.value=FriendName;
		else{
			if (UserList.value.indexOf(FriendName)==-1)
				UserList.value+=","+FriendName;
		}
	}
}
function SendTo_onkeyup() {
	with(thisForm){
		var indata=UserList.value;
		if (indata==""){
			if (event.keyCode ==13 && SelUser.selectedIndex!=0){
				UserList.value = SelUser.options[SelUser.selectedIndex].text;
			}
			return;
		}
		for(i=0;i<SelUser.length;i++){
			temp=SelUser.options[i].text;
			if (temp==indata){
				SelUser.options[i].selected=true;
				break;
			}
			if (temp.indexOf(indata)!=-1){
				SelUser.options[i].selected=true;
			}
		}
	}
}

function SendSubmit(MsgID){
	with(thisForm){
		if (UserList.value == ""){
			TxAlert('收件人不能为空值!');
			return;
		}
		if (Title.value == ""){
			TxAlert('标题不能为空值!');
			return;
		}
		if (msgContent.value == ""){
			TxAlert('正文不能为空值');
			return;
		}
		if (ValidateCode.value==""){
			TxAlert("验证码不能为空值");
			return;
		}
		SendMsgExecute(MsgID,UserList.value,Title.value,msgContent.value,ValidateCode.value,ToSendedCheck.checked);
	}
}

//发送短信执行过程
function SendMsgExecute(MsgID,UserList,Title,Content,ValidateCode,ToSended){
	var SendMsgCallback =
	{
		success:function(o){
			TxProcessHide();
			if(o.responseText !== undefined){
				if (o.status==200){
					var temp = o.responseText;
					if (temp!="true"){
						TxAlert(temp);
					}else 
					{DlgInfo.CloseWindow();TxAlert('短信发送成功！');}
				}
			}
		},
		failure:function(o){
			//DlgInfo.CloseWindow();
			TxProcessHide();
			if(o.responseText !== undefined){
				TxAlert(o.statusText);
			}
		}
	};
	var sUrl = "agent.aspx?desturl=" + encodeURIComponent(MsgConfig.SendMsgUrl);
	var postData = "UserList=" + encodeURIComponent(UserList);
	if (MsgID!=null) postData += "&MsgID=" + MsgID;
	postData += "&ToSendedCheck=" + ToSended;
	postData += "&Title=" + encodeURIComponent(Title);
	postData += "&Content=" + encodeURIComponent(Content);
	postData += "&ValidateCode=" + ValidateCode;

	var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, SendMsgCallback, postData);
	TxProcessShow("发送中,请等待...",true);
}
function OpenSendDlg(UserName,Title,Content){
	DlgInfo.CloseWindow();
	DlgInfo.CreateInfo(UserName,Title,Content);
}