// JavaScript Document

function makeProperty(o, name, getpredicate, setpredicate){
    var value;
	if (G_Parent == null)
		G_Parent = getParent(window);
	//G_Parent = window; //便于调试
    o["get"+name] = function(){
		if (G_Parent != window)
		{
			return G_Parent.GLOBAL_VARIABLES["get"+name].call(this);
		}
		else
		{
			if (getpredicate != null)
			{
				return getpredicate(value);
			}
			else
			{
				return value;
			}
		}
	};// 这里有两个scope对象call object(谁调用makeProperty), global


    o["set"+name] = function(v){// 这里有两个scope对象call object(谁调用makeProperty),
								// global
		if (G_Parent != window)
		{
			return G_Parent.GLOBAL_VARIABLES["set"+name].call(this,v);
		}
		else
		{
			if (setpredicate && !setpredicate(v))
			{
				throw "set error";
			}
			else
			{
				value = v;
			}
		}
	};
}

// 全局的对象
var GLOBAL_VARIABLES = {};

function mailjs() {
	makeProperty(GLOBAL_VARIABLES, "Attach", null,null);
	GLOBAL_VARIABLES.setAttach("");
	makeProperty(GLOBAL_VARIABLES, "Seq", null,null);
	GLOBAL_VARIABLES.setSeq("");
	makeProperty(GLOBAL_VARIABLES, "CurrWin", null,null);
	GLOBAL_VARIABLES.setCurrWin(window);
	makeProperty(GLOBAL_VARIABLES, "CurrCtl", null,null);
	GLOBAL_VARIABLES.setCurrCtl("");
	makeProperty(GLOBAL_VARIABLES, "Write", null,null);
	var rtnType = GetVersion();
	if (rtnType == "0")
	{
		GLOBAL_VARIABLES.setWrite("writemail.htm");
	}
	else
	{
		GLOBAL_VARIABLES.setWrite("businesscard.htm");
	}
	makeProperty(GLOBAL_VARIABLES, "Change", null,null);
	GLOBAL_VARIABLES.setChange("True");
}


// 获取消息中心版本
function GetVersion()
{
	var data = f_client_call(
		"G_MSG_VERSION",""); 
	return data;
}

// 获取账户信息
function getAccount()
{
	var data = f_client_call(
		"G_SYS_UserName");
	var OAccount = new Object();
	if (data != null)
	{
		var datas = data.split("\r\n");
		OAccount["account"] = datas[0];
		OAccount["name"] = datas[1];
	}
	else
	{
		OAccount["account"] = "";
		OAccount["name"] = "";
	}
	return OAccount;
}

// 获取账户
function getAccountid()
{
	var data = f_client_call(
		"G_SYS_UserName");
	if (data != null)
	{
		var datas = data.split("\r\n");
		return datas[0];
	}
	else
	{
		return "";
	}
}

// 新邮件
function NewMail()
{
	var data = f_client_call(
		"S_MSG_NEW"); 
	return data;
}

// 设置邮件属性
function SetAttrib(attrib)
{
	var data = f_client_call(
		"S_MSG_SETATTRIB",attrib); 
	return data;
}


// 保存邮件
function SaveMail(xml)
{
		var today = new Date();
		document.getElementById("showtime").innerHTML = document.getElementById("showtime").innerHTML + today.getYear() + "-" + today.getMonth() + "-" + today.getDay() + ":" + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds() + ":" + today.getMilliseconds() + "(start save)</br>";
	var data = f_client_call(
		"S_MSG_SAVE",xml);
	return data;
}

// 获取邮件
function GetMail(seq)
{
	var data = f_client_call(
		"G_MSG_GET",seq); 
	return data;
}

// 发送邮件
function SendMail(seq)
{
		var today = new Date();
		document.getElementById("showtime").innerHTML = document.getElementById("showtime").innerHTML + today.getYear() + "-" + today.getMonth() + "-" + today.getDay() + ":" + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds() + ":" + today.getMilliseconds() + "(start send)</br>";
	var data = f_client_call(
		"S_MSG_SEND",seq); 
	return data;
}

// 彻底删除邮件 (maillist 专用)
function DeleteMail(seqs)
{
	var data = f_client_call(
		"S_MSG_DELETE",seqs); 
	return data;

}


// 取消息中心版本 popmail maillist
function ShowVersion()
{
	var data = f_client_call(
		"G_MSG_VERSION");
	return data;
}

//收邮件
function ReceiveMail()
{
	f_client_call("S_MSG_RECV"); 
	return getMailLists();
}

//获取邮件列表
function getMailLists()
{
	var data = f_client_call("G_MSG_LIST");
	
	var datas = data.split("\r\n");
	datas = datas.reverse();
	var OReturn = new Object();
	OReturn["iSent"] = 0;
	OReturn["iTrashUnRead"] = 0;
	OReturn["iindexUnRead"] = 0;
	OReturn["iInbox"] = 0;
	OReturn["iTrash"] = 0;
	OReturn["iDrafts"] = 0;
	OReturn["aInbox"] = new Array;
	OReturn["aDrafts"] = new Array;
	OReturn["aSent"] = new Array;
	OReturn["aTrash"] = new Array;
	for( i=0 ; datas!=null && i<datas.length ; i++ )
	{
		if (datas[i] == "")
		{
			continue;
		}
		var mail = datas[i].split("|");
		var object = new Object();
		object["seq"] = mail[0];
		object["time"] = mail[1];
		object["attrib"] = "000000" + parseFloat(mail[2]).toString(16);
		object["mailsource"] == "";
		object["unread"] = 0;//已读
		var sattr = object["attrib"].substring(object["attrib"].length-6,object["attrib"].length);
		object["from"] = mail[3];
		var s2 = parseFloat(sattr.substring(1,2)).toString(2);
		if (s2.substring(s2.length -2,s2.length -1) == "1")
		{
			OReturn["iTrash"] = OReturn["iTrash"] + 1;
			if (s2.substring(s2.length -1) == "0")
			{
				OReturn["iTrashUnRead"] = OReturn["iTrashUnRead"] + 1;
				object["unread"] = 1;//未读
			}
			OReturn["aTrash"].push(object);
			continue;
		}
		switch(sattr.substring(3,6)) 
		{
			case "001":
				OReturn["iSent"] = OReturn["iSent"] + 1;
				OReturn["aSent"].push(object);
				break;
			case "002":
				OReturn["iInbox"] = OReturn["iInbox"] + 1;
				if (s2.substring(s2.length -1) == "0")
				{
					OReturn["iindexUnRead"] = OReturn["iindexUnRead"] + 1;
					object["unread"] = 1;//未读
				}
				OReturn["aInbox"].push(object);
				break;
			case "000":
				OReturn["iDrafts"] = OReturn["iDrafts"] + 1;
				OReturn["aDrafts"].push(object);
				break;
		}
	}
	return OReturn;
}

//获取附件
function getAttach(sSeq)
{
	var data = f_client_call(
		"G_MSG_ATTACH",sSeq);
	var iFirstAttach = data.indexOf("<attach");
	var iLastAttach = data.lastIndexOf("</attach>");
	var sAttach = ""
	if (iFirstAttach != -1 && iLastAttach != -1)
	{
		var sAttach = data.substring(iFirstAttach,iLastAttach + 9);
	}
	GLOBAL_VARIABLES.setAttach(sAttach);
}


var KEYCODE_TAB = "9" ; //TAB的键值
var KEYCODE_DEL = "46" ; //DEL的键值
var KEYCODE_ENTER = "13"; //ENTER的键值
var KEYCODE_BACKSPACE = "8"; //BACKSPACE的键值


//客户端主动调用接口
function OnCltKeyPress(KeyCode)
{
    switch (KeyCode)
    {
        case KEYCODE_TAB:
			var stype=$("input:focus").attr("type");
			if (typeof(stype) == "undefined")
			{
				stype=$("iframe:focus").attr("type"); 
				if (typeof(stype) == "undefined")
				{
					return "0";
				}
				else
				{
					stype = $("iframe:focus").attr("id");
					var oEditor = document.getElementById(stype).contentWindow;
					var txtobj=oEditor.document.selection.createRange();
					txtobj.text== ""?txtobj.text= "  ":(oEditor.document.selection.clear())&(txtobj.text= "  ") ;
					oEditor.document.selection.createRange().select();
					return "1";
				}
			}
			else
			{
				return "0";
			}
		break;
		case KEYCODE_ENTER:
			if (typeof(window.insertstock) != "undefined")
			{
				switch (GLOBAL_VARIABLES.getCurrCtl())
				{
				case "txtattstock":
					insertstock();
					return "1";
					break;
				}
			}
			var stype=$("input:focus").attr("type");
			if (typeof(stype) == "undefined")
			{
				stype=$("iframe:focus").attr("type"); 
				if (typeof(stype) == "undefined")
				{
					return "0";
				}
				else
				{
					stype = $("iframe:focus").attr("id");
					var oEditor = document.getElementById(stype).contentWindow;
					var txtobj=oEditor.document.selection.createRange();
					txtobj.text== "" ? txtobj.text= "\n":(oEditor.document.selection.clear())&(txtobj.text= "\n") ;
					oEditor.document.selection.createRange().select();
					return "1";
				}
			}
			else
			{
				return "0";
			}
		break;
		case KEYCODE_BACKSPACE:
			var stype=$("input:focus").attr("type"); 
			if (typeof(stype) == "undefined")
			{
				stype=$("iframe:focus").attr("type"); 
				if (typeof(stype) == "undefined")
				{
					return "1";
				}
				else
				{
					stype = "html";
				}
			}
			stype = stype.toLowerCase();
			if ((stype == "text") || (stype == "textarea") || (stype == "html"))
			{
				return "0";
			}
			else
			{
				return "1";
			}
		break;
    }
    return "0" ;
}

function notice(params)
{
	var datas = params.split("\r\n");
	switch (datas[0])
	{
		case "1":
			//log.info("发送邮件通知的结果:" + datas[1]);
			notice_sendreturn(datas[1]);
			return;
		break;
		case "2":
			//log.info("接收邮件通知的结果:" + datas[1]);
			notice_receive(datas[1]);
			return;
		break;
		case "3":
			//log.info("删除邮件通知的结果:" + datas[1]);
			return;
		break;
		case "4":
			//log.info("转到邮件列表的结果:" + datas[1]);
			notice_list(datas[1]);
		break;
		case "5":
			//log.info("转到指定邮件的结果:" + datas[1]);
			notice_read(datas[1]);
			return;
		break;
		case "6":
			//log.info("增加附件通知的结果:" + datas[1]);
			notice_attach(datas[1]);
		break;
	}
}

function notice_sendreturn(data)
{
	if (data == "1|0")
	{
		clearTimeout(sendprocess);	
		sendMailMessage("2");//发送成功	
	}
	else
	{
		clearTimeout(sendprocess);
		sendMailMessage("-1");//发送失败		
	}
}

function notice_receive(data)
{
	//GLOBAL_VARIABLES.getCurrWin().showReceiveMail(getMailLists());
}

function notice_read(data)
{
	//GLOBAL_VARIABLES.getCurrWin().location = GLOBAL_VARIABLES.getSingle() + "?seq=" + data;
}

function notice_list(data)
{
	switch(data)
	{
		case "0":
			//GLOBAL_VARIABLES.getCurrWin().location = sTagPage + GLOBAL_VARIABLES.getIndex() + "?box=0";
		break;
		case "1":
			//GLOBAL_VARIABLES.getCurrWin().location = sTagPage + GLOBAL_VARIABLES.getIndex() + "?box=1";
		break;
		case "2":
			//GLOBAL_VARIABLES.getCurrWin().location = sTagPage + GLOBAL_VARIABLES.getIndex() + "?tab=drafts";
		break;
	}
}

function notice_attach(data)
{
	var datas = data.split(",");
	GLOBAL_VARIABLES.setSeq(datas[0]);
	getAttach(datas[0]);
	GLOBAL_VARIABLES.getCurrWin().AddAttach();
	//GLOBAL_VARIABLES.getCurrWin().ShowAttach();
}

//应用附件
function op(id,opcode)
{	
	f_client_call("S_MSG_OP",GLOBAL_VARIABLES.getSeq() + "\r\n" + id + "|" + opcode);
}

function stopBubble(e) {  
    var e = e ? e : window.event;  
    if (window.event) { // IE
        e.cancelBubble = true;   
    } else { // FF
        // e.preventDefault();
        e.stopPropagation();   
    }   
}  

function OpenMailCenter(param)
{
	var data = f_client_call(
	"S_MSG_OPENMAINWND",param);
}

function OpenPOPMail(param)
{
	var data = f_client_call(
	"S_MSG_OPEN",param);
}

function getparamter(sStr,sName)
{
	var p = new Poly9.URLParser("1.htm?" +  sStr);
	return p.getParameter(sName);  
}

//重新登录
function LoginAgain()
{
	var data = f_client_call(
	"S_View_RELOGIN","");
}

//回执
function WriteReceipt(param)
{
	var data = f_client_call(
	"S_MSG_RECEIPT",param);
}


function closeWindow()
{
	/*
	document.getElementById("showtime").style.display='block';
	var today = new Date();
	document.getElementById("showtime").innerHTML = document.getElementById("showtime").innerHTML + today.getYear() + "-" + today.getMonth() + "-" + today.getDay() + ":" + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds() + ":" + today.getMilliseconds() + "(before close)</br>";
	*/
	var data=f_client_call("S_Window_Close","");
	//history.go(-1);
}
