// JavaScript Document

function f_client_call(name, params) {
    var callobj = client_core_interface();
    if (callobj == null) return null;
    return callobj.r(name, params);
}

function f_client_callback(name, params, callback, callbackparams) {
    var callobj = client_core_interface();
    if (callobj == null) return null;
    return callobj.c(name, params, callback, callbackparams);
}

// 新邮件
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 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 data = f_client_call(
		"S_MSG_SEND",seq); 
	return data;
}


// 删除邮件
function TrashMail(seqs)
{
	var aseq = seqs.split("|");
	$.each(aseq,function(index){
		var text = GetMail(this);
		var iFirstContent = text.indexOf("<content><![CDATA[");
		var iLastContent = text.lastIndexOf("]]</content>");
		text = text.substring(0,iFirstContent) + "<content></content>" + text.substring(iLastContent + 12,text.length);
		var xml = ql_xml_stringtoxml(text.replace(/\r/g, ""));
		var attrib = $($(xml).find("mail")[0]).attr("attrib");
		if (parseFloat(attrib) >= 65536 || parseFloat(attrib) == 0)
		{
			SetAttrib(this + "\r\n196608");
		}
		else
		{
			SetAttrib(this + "\r\n131072");
		}
	});
}

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

}

// 删除附件
function DeleteAttach(seq,nid)
{
	var data = f_client_call(
		"S_MSG_DEL_ATTACH",seq + "\r\n" + nid); 
	return data;
}

//添加附件
function AddMailAttach(seq,ntype)
{
	var data = f_client_call(
		"S_MSG_ADD_ATTACH",seq + "\r\n" + ntype); 
	return data;
}

//转发邮件
function ForwordMail(seq)
{
	var data = f_client_call(
		"S_MSG_FORWARD",seq); 
	return data;
}

// 新建联系人组
function NewGroup(sName,sDesc)
{
	var data = f_client_call(
		"S_MSG_GROUP_ADD",sName + "|" + sDesc);
	// if (data != 0)
	// {
	// g_gid = data;
	// }
	return data;
}

// 编辑联系人组
function EditGroup(gid,sName,sDesc)
{
	var data = f_client_call(
		"S_MSG_GROUP_MODIFY",gid + "|" + sName + "|" + sDesc);
	// if (data != 0)
	// {
	// g_gid = data;
	// }
	return data;
}

// 获取联系人列表
function GetContactList()
{
	var data = f_client_call(
		"G_MSG_CONTACT_LIST");

	var datas = data.split("\r\n");
	var aContact = new Array();
	if (data == "")
	{
		return aContact;
	}
	for( i=0 ; datas!=null && i<datas.length ; i++ )
	{
		var fields = datas[i].split("|");
		var object = new Object();
		object["account"] = fields[0];
		object["name"] = fields[1];
		object["desc"] = fields[2];
		object["gid"] = fields[3];
		/*
		 * if (IsNum(object["gid"])) { object["gid"] = toGIDs(fields[3]); } else {
		 * object["gid"] = new Array('0'); }
		 */
		object["glive"] = false;
		aContact.push(object);
	}
	return aContact;
}

// 获取联系人组列表
function GetGroupList(aContact)
{
	if (aContact == null)
		aContact = GetContactList();
	var data = f_client_call(
		"G_MSG_GROUP_LIST");
	var datas;
	if (data == "")
		datas = null;
	else
		datas = data.split("\r\n");
	var aGroup = new Array();
	var object = new Object();
	object["gid"] = "0";
	object["name"] = "未分组";
	object["desc"] = "未分组联系人";
	object["Contacts"] = $.grep(aContact,function(cur,j){return checkGrp("0",cur["gid"]);});
	aGroup.push(object);
	for( i=0 ; datas!=null && i<datas.length ; i++ )
	{
		var fields = datas[i].split("|");
		var object = new Object();
		object["gid"] = fields[0];
		object["name"] = fields[1];
		object["desc"] = fields[2];
		object["Contacts"] = $.grep(aContact,function(cur,j){return checkGrp(fields[0],cur["gid"]);});
		aGroup.push(object);
	}
	return aGroup;
}

// 新建联系人
function NewContact(account,name,desc,gid)
{
	var str = account + "|" + name + "|" + desc + "|" + gid;
	var data = f_client_call(
		"S_MSG_CONTACT_ADD",str);
		return data;
}


// 删除联系人组
function DeleteGroup(gid)
{
	var data = f_client_call(
		"S_MSG_ GROUP_DELETE",gid);
		return data;
}

// 删除联系人
function DeleteContact(accounts)
{
	var data = f_client_call(
		"S_MSG_CONTACT_DEL",accounts);
		return data;
}

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


// 编辑联系人
function EditContact(account,name,desc,gid)
{
	var data = f_client_call(
		"S_MSG_CONTACT_MODIFY",account + "|" + name + "|" + desc + "|" + gid);
		return data;
}


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

// 获取组信息
function GetGroup(gid,aGroup)
{
	var Groups;
	if (aGroup == null)
	{
		Groups = GetGroupList();
	}
	else
	{
		Groups = aGroup;
	}
	var alist = $.grep(Groups,function(cur,j){return cur["gid"] == gid;});
	if (alist.length > 0)
	{
		return alist[0];
	}
	else
	{
		return null;
	}
}

//去除相同Array数组元素
function myArray_Unique(myArray){
　　for(var i=0;i<myArray.length-1;i++){
　　　　var temp=myArray[i];
　　　　for(var j=i+1;j<myArray.length;j++){
　　　　　　if(temp==myArray[j]){
　　　　　　myArray.splice(j--,1);
　　　　　　}
　　　　}
　　}
　　return myArray;
}

function obj2str(o){
  var r = [];
  if(typeof o == "string" || o == null) {
    return o;
  }
  if(typeof o == "object"){
    if(!o.sort){
      r[0]="{"
      for(var i in o){
        r[r.length]=i;
        r[r.length]=":";
        r[r.length]=obj2str(o[i]);
        r[r.length]=",";
      }
      r[r.length-1]="}"
    }else{
      r[0]="["
      for(var i =0;i<o.length;i++){
        r[r.length]=obj2str(o[i]);
        r[r.length]=",";
      }
      r[r.length-1]="]"
    }
    return r.join("");
  }
  return o.toString();
}


function ExpContact()
{
	var data = f_client_call(
	"S_MSG_CONTACT_EXPORT");
	alert("导出成功！");
}

function ImpContact()
{
	var data = f_client_call(
	"S_MSG_CONTACT_IMPORT");
	alert("导入成功！");
}

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);
}

//去除相同Array数组元素
function myArray_Unique(myArray){
　　for(var i=0;i<myArray.length-1;i++){
　　　　var temp=myArray[i];
　　　　for(var j=i+1;j<myArray.length;j++){
　　　　　　if(temp==myArray[j]){
　　　　　　myArray.splice(j--,1);
　　　　　　}
　　　　}
　　}
　　return myArray;
}

function op(seq,id,opcode)
{
	f_client_call("S_MSG_OP",seq + "\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 IsNum(s)
{
    if(s!=null){
        var r,re;
        re = /\d*/i; // \d表示数字,*表示匹配多个数字
        r = s.match(re);
        return (r==s)?true:false;
    }
    return false;
}

//将二进制分组列表转换成分组数组
function toGIDs(s)
{
	var gids = s.toString(2);
	var agid = new Array();
	if (s == "0" || s == "")
	{
		agid.push("0");
	}
	else
	{
		var gid2 = "";
		for(var i=gids.length;i>=0;i--) 
		{
			gid2=gid2+gids.charAt(i);
		}
		gids = gid2;
		for (var i=gids.length - 1;i >=0; i--)
		{
			if (gids.substring(i,i + 1) == "1")
			{
				agid.push((i + 1).toString());
			}
		}
	}
	return agid;
}

//将分组数组转换成二进制分组值
function Array2gid(arr)
{
	if ($.inArray("0",arr) > -1)
	{
		return 0;
	}
	var tmpGid = 0;
	for (var i = 0; i < arr.length ; i++ )
	{
		var gid2 = (1 << parseInt(arr[i]) - 1);
		tmpGid = tmpGid | gid2;
	}
	return tmpGid;	
}


//检查是否在分组列表中存在该分组
function checkGrp(gid,gids)
{
	if (gids == "" || gids == null || gids == "0")
	{
		if (gid == "0")
			return true;
		else
			return false;
	}
	
	if (gid == "0")
	{
		if (gids == "" || gids == null || gids == "0")
			return ture;
		else
			return false;
	}
	var gids2 = parseInt(gids);
	var gid2 = (1 << parseInt(gid) - 1);
	return (gids2 | gid2) == gids2;
}

function joinGroup(gid,gids)
{
	if (gids=="0" || gids == "")
	{
		if (gid == "0")
			return "0";
		else
			return (1 << parseInt(gid) - 1);
	}
	var gids2 = parseInt(gids);
	var gid2 = (1 << parseInt(gid) - 1);
	return gids2 | gid2;
}

//gids 原先组;gid 需删除组
function delGroup(gid,gids)
{
	var arrGroup = toGIDs(parseInt(gids));
	var tmpGroup = $.grep(arrGroup,function(g,i){ return g != gid });
	if ( tmpGroup.length == 0 )
	{
		return 0;
	}
	var tmpGid = 0;
	for (var i = 0; i < tmpGroup.length ; i++ )
	{
		var gid2 = (1 << parseInt(tmpGroup[i]) - 1);
		tmpGid = tmpGid | gid2;
	}
	return tmpGid;	
}

function isEmail(strEmail)
{
	var regEx = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
	return regEx.test(strEmail);
}

function isMobileNum(n)
{
	var regEx = /^13[0-9]{9}$/;
	return regEx.test(n);
}

function isZipcode(n)
{
	var regEx = /^[1-9][0-9]{5}$/;
	return regEx.test(n);
}

