var CK_JJR_BROKER_INFO_SRVS = 'JJR_BINFO_SRVS',
	CK_JJR_CUSTOMER_INFO_SRVS_LIST = 'JJR_CINFO_SRVS_LST',
	CK_JJR_DICTIONARY = 'JJR_DICTIONARY',
	CK_JJR_LABEL_USERS = 'JJR_LABEL_USERS',
	CK_JJR_USER_LABELS = 'JJR_USER_LABELS',
	CK_JJR_EMAIL_SEND = 'JJR_EMAIL_SEND',
	CK_JJR_EMAIL_UNREAD = 'JJR_EMAIL_UNREAD',
	CK_JJR_EMAIL_REPLY = 'JJR_EMAIL_REPLY',
	CK_JJR_EMAIL_HIT = 'JJR_EMAIL_HIT',
	CK_MAILCENTER_USERNAMEKEY = 'usernamekey',
	JJR_MIN_DATE = 20100101000000;

// +---------------------------------------------------
// | 当前登录用户
// +---------------------------------------------------
var SysUser = [];
function sysUserInit()
{
	if (SysUser instanceof Array && SysUser.length > 0)
	{
		return SysUser;
	}
	SysUser = getSysUser();
	return SysUser;
}

function getSysUserId()
{
	sysUserInit();
	if (SysUser.length > 0)
	{
		return SysUser[0].toLowerCase();
	}
	throw 'Get userid error!';
}

function getQsId()
{
	sysUserInit();
	if (SysUser.length > 2)
	{
		return SysUser[2];
	}
	throw 'Get qsid error!';
}

// +---------------------------------------------------
// | 发送邮件信息存入客户端
// +---------------------------------------------------
function saveSendEMail2Client(Send)
{
	saveStr2Client(
		CK_JJR_EMAIL_SEND, Send
	);
}

// +---------------------------------------------------
// | 未读邮件信息存入客户端
// +---------------------------------------------------
function saveUnreadEMail2Client(Unread)
{
	saveStr2Client(
		CK_JJR_EMAIL_UNREAD, Unread
	);
}

// +---------------------------------------------------
// | 回复邮件信息存入客户端
// +---------------------------------------------------
function saveReplyEMail2Client(Reply)
{
	saveStr2Client(
		CK_JJR_EMAIL_REPLY, Reply
	);
}

// +---------------------------------------------------
// | 点击邮件信息存入客户端
// +---------------------------------------------------
function saveHitEMail2Client(Hit)
{
	saveStr2Client(
		CK_JJR_EMAIL_HIT, Hit
	);
}

// +---------------------------------------------------
// | 点击发送用户串存入客户端
// +---------------------------------------------------
function saveUserNames2Client(usernames)
{
	saveStr2Client(
		CK_MAILCENTER_USERNAMEKEY, usernames
	);
}

// +---------------------------------------------------
// | 从客户端获取标签用户名映射
// +---------------------------------------------------
function getLabelUsersFromClient()
{
	return getClientData(CK_JJR_LABEL_USERS);
}

// +---------------------------------------------------
// | 标签用户名映射存入客户端
// +---------------------------------------------------
function saveLabelUsers2Client(label)
{
	saveStr2Client(
		CK_JJR_LABEL_USERS, label
	);
}

// +---------------------------------------------------
// | 从客户端获取用户名标签映射
// +---------------------------------------------------
function getUserLabelsFromClient()
{
	return getClientData(CK_JJR_USER_LABELS);
}

// +---------------------------------------------------
// | 用户名标签映射存入客户端
// +---------------------------------------------------
function saveUserLabels2Client(label)
{
	saveStr2Client(
		CK_JJR_USER_LABELS, label
	);
}

// +---------------------------------------------------
// | 从客户端获取经纪人最新信息和服务列表
// +---------------------------------------------------
function getBrokerInfoSrvsFromClient()
{
	return getClientData(CK_JJR_BROKER_INFO_SRVS);
}

// +---------------------------------------------------
// | 经纪人详细信息存入客户端
// +---------------------------------------------------
function saveBrokerInfoSrvs2Client(brokerInfo)
{
	saveStr2Client(
		CK_JJR_BROKER_INFO_SRVS, brokerInfo
	);
}

// +---------------------------------------------------
// | 从客户端获取经纪人最新用户列表
// +---------------------------------------------------
function getCustomerInfoSrvsListFromClient()
{
	return getClientData(CK_JJR_CUSTOMER_INFO_SRVS_LIST);
}

// +---------------------------------------------------
// | 经纪人用户列表存入客户端
// +---------------------------------------------------
function saveCustomerInfoSrvsList2Client(customerInfoSrvsList)
{
	saveStr2Client(
		CK_JJR_CUSTOMER_INFO_SRVS_LIST, customerInfoSrvsList
	);
}

// +---------------------------------------------------
// | 更新经纪人用户列表中用户信息有变更的用户
// +---------------------------------------------------
function updateCustomerInfoSrvs(remoteCustomerInfoSrvsList, clientCustomerInfoSrvsList)
{
	if (!remoteCustomerInfoSrvsList)
	{
		return;
	}
	if (!clientCustomerInfoSrvsList)
	{
		return remoteCustomerInfoSrvsList;
	}
	for (var ritem in newCustomerInfoSrvsList)
	{
		var rcusid = ritem.info.id;
		for (var citem in clientCustomerInfoSrvsList)
		{
			var ccusid = citem.info.id;
			if (rcusid == ccusid)
			{
				citem.info = rcusid.info;
				break;
			}
		}
	}
	return clientCustomerInfoSrvsList;
}

// +---------------------------------------------------
// | 从客户端获取数据字典
// +---------------------------------------------------
function getDictionaryFromClient()
{
	return getClientData(CK_JJR_DICTIONARY);
}

// +---------------------------------------------------
// | 从客户端获取数据字典
// +---------------------------------------------------
function saveDictionary2Client(dictionary)
{
	saveStr2Client(
		CK_JJR_DICTIONARY, dictionary
	);
}

// +---------------------------------------------------
// | cookie相关的方法
// +---------------------------------------------------
function setCookie(c_name, value, expiredays)
{
	if (expiredays !== null)
	{
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie = c_name + '=' + escape(value)
			+ ((expiredays === null) ? '' : ';expires=' + exdate.toGMTString()) + ";path=/";
		return;
	}
	document.cookie = c_name + '=' + escape(value) + ";path=/";
	return;
}

function getCookie(c_name)
{
	var tmpcookie = document.cookie;
	if (!c_name || tmpcookie.length === 0)
	{
		return '';
	}
	var c_start = tmpcookie.indexOf(c_name + '=');
	if (c_start != -1)
	{
		c_start = c_start + c_name.length + 1;
		var c_end = tmpcookie.indexOf(
			';', c_start
		);
		if (c_end == -1)
		{
			c_end = tmpcookie.length;
		}
		return unescape(tmpcookie.substring(
			c_start, c_end
		));
	}
	return '';
}

function alterTab(seq)
{
	if (seq === 0)
	{
		$('.behavioral_data_bodyl ul li:eq(0)').addClass('behavioral_data_tab');
		$('.behavioral_data_bodyl ul li:eq(0)').children('img').attr(
			'src', 'img/grxw_data_online.gif'
		);
		$('.behavioral_data_bodyl ul li:eq(1)').css(
			'cursor', 'pointer'
		);
		$('.behavioral_data_bodyl ul li:eq(1)').addClass('behavioral_data_btn');
		$('.behavioral_data_bodyl ul li:eq(1)').children('img').attr(
			'src', 'img/ztxw_data.gif'
		);
		$('#frmContact').attr(
			'src', 'grxw.htm'
		);
	}
	else
	{
		$('.behavioral_data_bodyl ul li:eq(1)').addClass('behavioral_data_tab');
		$('.behavioral_data_bodyl ul li:eq(1)').children('img').attr(
			'src', 'img/ztxw_data_online.gif'
		);
		$('.behavioral_data_bodyl ul li:eq(0)').css(
			'cursor', 'pointer'
		);
		$('.behavioral_data_bodyl ul li:eq(0)').addClass('behavioral_data_btn');
		$('.behavioral_data_bodyl ul li:eq(0)').children('img').attr(
			'src', 'img/grxw_data.gif'
		);
		$('#frmContact').attr(
			'src', 'ztxw.htm'
		);
	}
}

/*
 * pDiv:父div mDiv:滚动div img_right:右按钮图片 img_left:左按钮图片 changeVal:滚动值
 * fn:按钮有效时调用函数
 */
function changeOffset_right(pDiv, img_right, img_left, changeVal, fn)
{
	if (pDiv.scrollLeft < pDiv.scrollWidth - pDiv.offsetWidth)
	{
		pDiv.scrollLeft += changeVal;
		if (pDiv.scrollLeft == pDiv.scrollWidth - pDiv.offsetWidth)
		{
			img_right.attr(
				'src', img_right.attr('src').replace(
					'_online', ''
				)
			);
			img_right.removeAttr('onclick');
			img_right.css(
				'cursor', ''
			);
		}
		if (img_left.attr('src').indexOf('_online') < 0)
		{
			img_left.attr(
				'src', img_left.attr('src').replace(
					'.gif', '_online.gif'
				)
			);
			img_left.get(0).onclick = fn;
			img_left.css(
				'cursor', 'hand'
			);
		}
	}
}

function changeOffset_left(pDiv, img_right, img_left, changeVal, fn)
{
	if (pDiv.scrollLeft > 0)
	{
		pDiv.scrollLeft -= changeVal;
		if (pDiv.scrollLeft == 0)
		{
			img_left.attr(
				'src', img_left.attr('src').replace(
					'_online', ''
				)
			);
			img_left.removeAttr('onclick');
			img_left.css(
				'cursor', ''
			);
		}
		if (img_right.attr('src').indexOf('_online') < 0)
		{
			img_right.attr(
				'src', img_right.attr('src').replace(
					'.gif', '_online.gif'
				)
			);
			img_right.get(0).onclick = fn;
			img_right.css(
				'cursor', 'hand'
			);
		}
	}
}
/*
 * seq:当前排序td在集合obj中序号 obj:排序column集合 data:排序前数据数组
 * sortID:排序字段，如排序字段在json数据第二层属性，属性名用'.'分割； 如：{aa:11,bb:{cc:22}}
 * 需按cc排序时，sortID为：bb.cc
 */
function SortColumn(seq, obj_column, data, sortID, page)
{
	var tmpArray = [],
		kk = 0;
	while (kk < data.length)
	{
		tmpArray.push(data[kk++]);
	}

	var sorted_data, key0, key1,
		dotFlag = false;
	if (sortID.indexOf('.') >= 0)
	{
		dotFlag = true;
	}

	if (dotFlag)
	{
		key0 = sortID.split('.')[0];
		key1 = sortID.split('.')[1];
		sortID = key1;
	}

	$(obj_column).each(
		function(i)
		{
			if (i != seq)
			{
				if ($(this).text())
				{
					$(this).html($(this).html().substring(
						0, $(this).html().length - 1
					));
				}
			}
			else
			{
				if ($(this).text() === '↓')
				{
					switch (sortID)
					{
						case 'rzf' :
							sortID = 'rzfA';
							break;
						case 'zzf' :
							sortID = 'zzfA';
							break;
						case 'yzf' :
							sortID = 'yzfA';
							break;
						case 'zxg' :
							sortID = 'zxgA';
							break;
						case 'qjzf' :
							sortID = 'qjzfA';
							break;
						case 'zrzf' :
							sortID = 'zrzfA';
							break;
						case 'lRank' :
							sortID = 'lRankA';
							break;
						case 'mailrank' :
							sortID = 'mailrankA';
							break;
					}
				}
				else
				{
					switch (sortID)
					{
						case 'rzf' :
							sortID = 'rzfD';
							break;
						case 'zzf' :
							sortID = 'zzfD';
							break;
						case 'yzf' :
							sortID = 'yzfD';
							break;
						case 'zxg' :
							sortID = 'zxgD';
							break;
						case 'qjzf' :
							sortID = 'qjzfD';
							break;
						case 'zrzf' :
							sortID = 'zrzfD';
							break;
						case 'lRank' :
							sortID = 'lRankD';
							break;
						case 'mailrank' :
							sortID = 'mailrankD';
							break;
					}
				}

				if (dotFlag)
				{
					key1 = sortID;
				}
				if ($(this).text() === '')
				{
					$(this).html($(this).html() + '↓');
					sorted_data = tmpArray.sort(
						function(a, b)
						{
							if (dotFlag)
							{
								if (b[key0][key1] > a[key0][key1])
								{
									return 1;
								}
								else if (b[key0][key1] < a[key0][key1])
								{
									return -1;
								}
								return 0;
							}
							if (b[sortID] > a[sortID])
							{
								return 1;
							}
							else if (b[sortID] < a[sortID])
							{
								return -1;
							}
							return 0;
						}
					);
				}
				else
				{
					if ($(this).text() === '↓')
					{
						$(this).html($(this).html().replace(
							'↓', '↑'
						));
						sorted_data = tmpArray.sort(
							function(a, b)
							{

								if (dotFlag)
								{
									if (a[key0][key1] > b[key0][key1])
									{
										return 1;
									}
									else if (a[key0][key1] < b[key0][key1])
									{
										return -1;
									}
									return 0;
								}
								if (a[sortID] > b[sortID])
								{
									return 1;
								}
								else if (a[sortID] < b[sortID])
								{
									return -1;
								}
								return 0;
							}
						);
					}
					else
					{
						$(this).html($(this).html().replace(
							'↑', '↓'
						));
						sorted_data = tmpArray.sort(
							function(a, b)
							{

								if (dotFlag)
								{
									if (b[key0][key1] > a[key0][key1])
									{
										return 1;
									}
									else if (b[key0][key1] < a[key0][key1])
									{
										return -1;
									}
									return 0;
								}
								if (b[sortID] > a[sortID])
								{
									return 1;
								}
								else if (b[sortID] < a[sortID])
								{
									return -1;
								}
								return 0;
							}
						);
					}
				}

				if (page === 'khlb')
				{
					G_filter_condition.sort = seq + '|' + $(this).text();
				}
			}
		}
	);

	if (sortID == "lRankA" || sortID == "lRankD" || sortID == "mailrankA" || sortID == "mailrankD")
	{
		sorted_data.reverse();
	}

	return sorted_data;
}

function getColor(zf)
{
	if (zf > 0)
	{
		return 'font_red';
	}
	if (zf < 0)
	{
		return 'font_green';
	}
	return 'font_gray';
}
