/* (C) DBStudio, ghljj.com */


//+----------------------------------------------------------------------------
// last updated at 2005.3.25
//
// 函数速查表 ::.
//
// o (id)						取 document.getElementById(id)
// disp (id, state)				设置指定 id 的 style.display
// display (obj, state)			设置指定 obj 的 style.display
// collapse (obj)				切换 style.display
// newid ()						生成不重复的 id
//
// query (key, url, value)		取 url 中的参数
// diffurl (url)				为 url 添加随机 tmp 参数，以便逃过 IE 缓存
// unique (anchor)				为 A 标记的 href 添加随机 tmp 参数
//
// toInt (s)					转换为整数
// ltrim (s)					除开始空格
// rtrim (s)					除结束空格
// trim (s)						除空格
// prefix (s, len, ch)			字符串加前导符 0 或指定 ch
// inlist (list, s)				检测 s 是否包含在用逗号分隔的 list 中
//
// cookie_on () 				检测是否接受cookie，不同于 navigator.cookieEnabled, test method is: set->get->del a test cookie
// cookie_live (name, value, day)							创建带生存期的 cookie
// cookie_set (name, value, expires, path, domain, secure)	创建 cookie
// cookie_get (name)										读取 cookie
// cookie_del (name)										删除 cookie
//
// throwStr (text, [es])		去掉 text 中包含在 es 数组的字符串
// prepareFormToPost (frm)		提交表单，提交前禁用 submit 按钮，并且过滤掉输入文本中某些特殊字符（例如xml不直接支持的字符）
//
// make_radio (名称, 值, 单击事件, 值1|标签1, ...)			合成 radio 组
// make_checkbox (名称, 值, 标签, 单击事件)					合成 checkbox 组
// radio_value (o)											取 radio 组的值，未选中时返回空串
// checkbox_value (o)										取 checkbox 值，0 或 1
//
// option_list (list)										根据 list 生成 option 串
// option_fill (obj, list, value)							根据 list 生成 options 插入 select 对象，并默认选择指定 value 的 option
// option_fit (obj, value)									根据 value 选择 select 的 option
// option_valid (obj)										检测选择的 option 是否有 value，若无则提示
//-----------------------------------------------------------------------------



//+----------------------------------------------------------------------------
//	函数:	o
//	说明:	根据 id 读取元素，不检测元素是否存在
//	参数:	id - 元素 id
//	返回:	元素
//-----------------------------------------------------------------------------
function o(id)
{
	return document.getElementById(id);
}


//+----------------------------------------------------------------------------
//	函数:	disp
//	说明:	根据 id 设置对象的 display 状态
//	参数:	id - 操作对象的 id
//			state - true/false，未指定时等同于 true
//	返回:	无
//-----------------------------------------------------------------------------
function disp(id, state)
{
	display(o(id), state);
}


//+----------------------------------------------------------------------------
//	函数:	display
//	说明:	设置对象的 display 状态
//	参数:	obj - 操作对象
//			state - true/false，未指定时等同于 true
//	返回:	无
//-----------------------------------------------------------------------------
function display(obj, state)
{
	if (!obj) return;
	obj.style.display = (state || typeof(state) == "undefined") ? "" : "none";
}


//+----------------------------------------------------------------------------
//	函数:	collapse
//	说明:	在显示和隐藏之间循环切换对象的 display 状态
//	参数:	obj - 操作对象
//	返回:	无
//-----------------------------------------------------------------------------
function collapse(obj)
{
	if (!obj) return;
	obj.style.display = (obj.style.display == "none") ? "" : "none";
}


//+----------------------------------------------------------------------------
//	函数:	newid
//	说明:	生成新id
//	参数:	无
//	返回:	不重复的id
//-----------------------------------------------------------------------------
function newid()
{
	var id
	do
		id = 'id' + Math.random().toString().substr(2, 10)
	while (document.getElementById(id))
	return id
}



//+----------------------------------------------------------------------------
//	函数:	query
//	说明:	取得或替换 url 中的参数
//	参数:	key - 参数名
//			url - 待分析的 uri，未指定时使用当前页面 uri
//			value - 指定 value 时，用相应的 key=value 替换 uri 中原有的 key=xxx 并返回
//			这个特性用在构造翻页 url 时非常有用
//			例如，用 query("pg", "a.aspx?pg=1", 2) 将 a.aspx?pg=1 替换为 a.aspx?pg=2
//	返回:	指定的参数值或替换后的 uri
//-----------------------------------------------------------------------------
function query(key, url, value)
{
	if (typeof(url) == "undefined") url = location.href

	// &|$ 表示匹配 & 或结束符，"ig" 会在多条目时返回带名称的值，故此处未使用。因此本方法假设 key 是不重复的

	var reg = new RegExp(key + "=(.*?)(#|&|$)")
	var arr = url.match(reg)

	if (typeof(value) != "undefined")
	{
		if (value != "") value = key + "=" + value
		return (arr == null) ? url + ((url.indexOf("?") < 0) ? "?" : "&") + value : url.replace(reg, value + "$2")
	}
	else
		return (arr != null) ? arr[1] : ""

	// 此方法不解码
	// VB 用 Escape 编码； C# 用 HttpUtility.UrlEncodeUnicode 编码
	// 使用 unescape 解码 Unicode； 使用 decodeURI 解码 UTF-8 (JS5.5+), 若遇非 UTF-8 串会出错
	// decodeURI(arr[1]); unescape(arr[1])
}


//+----------------------------------------------------------------------------
//	函数:	diffurl
//	说明:	为 uri 添加随机的 tmp=xxx 参数，以便逃过 IE 缓存
//	参数:	url - 待处理的 uri
//	返回:	附加了 tmp 参数后的 uri
//-----------------------------------------------------------------------------
function diffurl(url)
{
	if (!typeof(url) == "undefined") url = location.href

	var value = '' + Math.random()
	return query("tmp", url, value.substr(1, 6))
}


//+----------------------------------------------------------------------------
//	函数:	unique
//	说明:	为 A 锚点的 href 添加随机 tmp 参数
//	参数:	anchor - 待处理的锚点对象
//	返回:	无
//-----------------------------------------------------------------------------
function unique(anchor)
{
	anchor.href = diffurl(anchor.href)
}



//+----------------------------------------------------------------------------
//	函数:	toInt
//	说明:	转换为整数
//	参数:	s - 待转换文本
//	返回:	整数，文本不合法时返回 0
//-----------------------------------------------------------------------------
function toInt(s)
{
	var r = parseInt(s)
	if (isNaN(r)) r = 0
	return r
}


//+----------------------------------------------------------------------------
//	函数:	ltrim
//	说明:	截除起始空格
//	参数:	s - 待处理文本
//	返回:	文本
//-----------------------------------------------------------------------------
function ltrim(s)
{
	return s.replace(/^\s+/, '')
}


//+----------------------------------------------------------------------------
//	函数:	rtrim
//	说明:	截除结尾空格
//	参数:	s - 待处理文本
//	返回:	文本
//-----------------------------------------------------------------------------
function rtrim(s)
{
	return s.replace(/\s+$/, '')
}


//+----------------------------------------------------------------------------
//	函数:	trim
//	说明:	截除起始和结尾空格
//	参数:	s - 待处理文本
//	返回:	文本
//-----------------------------------------------------------------------------
function trim(s)
{
	return s.replace(/^\s+|\s+$/g, '')
}


//+----------------------------------------------------------------------------
//	函数:	prefix
//	说明:	字符串加前导符 0 或指定的字符
//	参数:	s - 待处理文本
//			len - 文本宽度，不足此宽度时加前导符
//			ch - 填充字符，未指定时用 0 填充
//	返回:	文本
//-----------------------------------------------------------------------------
function prefix(s, len, ch)
{
	s = s + ""
	if (!ch) ch = "0"
	var tmp = ""
	if (s.length < len)
	{
		for (var i = 0; i < len - s.length; i++) tmp = tmp + ch
		s = tmp + s
	}
	return (s)
}


//+----------------------------------------------------------------------------
//	函数:	inlist
//	说明:	检测指定文本是否包含在用逗号分隔的列表中
//	参数:	list - 用逗号分隔的列表文本
//			s - 待检测文本
//	返回:	true/false
//-----------------------------------------------------------------------------
function inlist(list, s)
{
	if (!list) return false

	var tmp = list.split(",")
	for (var i = 0; i < tmp.length; i++)
		if (tmp[i] == s) return true
	return false
}



//+----------------------------------------------------------------------------
//	函数:	cookie_on
//	说明:	检测是否接受cookie，此函数用设置->读取->比较->删除的方法检测
//	参数:	无
//	返回:	true/false
//-----------------------------------------------------------------------------
function cookie_on()
{
	var N = "#CookieTest#"
	var V = "1"
	var cval
	cookie_set(N, V)
	cval = cookie_get(N)
	cookie_del(N)
	return (cval != null && V == cval)
}

//+----------------------------------------------------------------------------
//	函数:	cookie_live
//	说明:	创建带生存期的 cookie，作用域为 /（即整个站点）
//	参数:	name - cookie 名
//			value - cookie 值
//			day - 有效天数
//	返回:	无
//-----------------------------------------------------------------------------
function cookie_live(name, value, day)
{
	var exp = null
	if (day != null)
	{
		exp = new Date()
		exp.setTime(exp.getTime() + day * 24 * 60 * 60 * 1000)
	}
	cookie_set(name, value, exp, "/")
}

//+----------------------------------------------------------------------------
//	函数:	cookie_set
//	说明:	创建 cookie
//	参数:	name - cookie 名
//			value - cookie 值
//			expires - 有效天数
//			path - 作用路径
//			doman - 作用域
//			secure - 安全
//	返回:	无
//-----------------------------------------------------------------------------
function cookie_set(name, value, expires, path, domain, secure)
{
	document.cookie =
		name + "=" + escape (value) +
		((expires != null) ? "; expires=" + expires.toGMTString() : "") +
		((path != null) ? "; path=" + path : "") +
		((domain != null) ? "; domain=" + domain : "") +
		((secure == true) ? "; secure" : "")
}

//+----------------------------------------------------------------------------
//	函数:	cookie_get
//	说明:	读取 cookie
//	参数:	name - cookie 名
//	返回:	cookie 值或 null
//-----------------------------------------------------------------------------
function cookie_get(name)
{
	var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)")
	var arr = document.cookie.match(reg)
	return (arr != null) ? unescape(arr[2]) : null
}

//+----------------------------------------------------------------------------
//	函数:	cookie_del
//	说明:	删除 cookie，通过将 cookie 生存期设置为过期的方法删除之
//	参数:	name - cookie 名
//	返回:	无
//-----------------------------------------------------------------------------
function cookie_del(name)
{
	var cval = cookie_get(name)
	if(cval != null)
	{
		var exp = new Date()
		exp.setTime(exp.getTime() - 1)
		cookie_set(name, cval, exp, "/")
	}
}



//+----------------------------------------------------------------------------
//	函数:	throwStr
//	说明:	去掉 s 中包含在 es 数组的字符串
//	返回:	文本
//-----------------------------------------------------------------------------
function throwStr(text, es)
{
	// 注意：es 的条目已经是有效的正则表达式，例如 \ 应写成 \\ （\\\\）
	// 注意：可以用 String.fromCharCode 得到不能显示的特殊字符
	if (!text) return "";
	for (var i = 0; i < es.length; i++)
	{
		var re = new RegExp(es[i], "ig");
		text = text.replace(re, "");
	}
	return text;
}

function throwCtrlChar(text) {
		var re = /[\x01-\x08\x0B-\x0C\x0E-\x1F]/ig;
		return text.replace(re, '');
}

//+----------------------------------------------------------------------------
//	函数:	prepareFormToPost
//	说明:	准备提交表单，提交前禁用 submit 按钮，并且过滤掉输入文本中某些特殊字符
//	返回:	true
//-----------------------------------------------------------------------------
function prepareFormToPost(frm)
{
	// 特殊符号（持续添加中）：
	// 从 powerpoint 复制过来的换行符号 11 （导致 xml 错误）
	//var es = new Array(String.fromCharCode("11"), String.fromCharCode("12"),
	//	String.fromCharCode("14"), String.fromCharCode("15"), String.fromCharCode("20"));
	for (var i = 0; i < frm.elements.length; i++) {
		var obj = frm.elements[i];
		if (obj.type == "text" || obj.type == "textarea") obj.value = throwCtrlChar(obj.value); //throwStr(obj.value, es);
		if (obj.type == "submit") obj.disabled = true;
	}
	return true;
}



//+----------------------------------------------------------------------------
//	函数:	make_radio
//	说明:	合成关于 radio 的 HTML 文本（带label）
//	参数:	名称, 默认选中值, 单击事件, 值1|标签1, 值2|标签2...
//	返回:	HTML 文本
//-----------------------------------------------------------------------------
function make_radio()
{
	var arg = arguments
	var radio_name = arg[0]
	var radio_def = arg[1]
	var radio_event = (arg[2]) ? (' onclick="' + arg[2] + '"') : ''

	var t = ""
	for (var i = 3; i < arg.length; i++)
	{
		var tmp = arg[i].split('|')
		var radio_value = tmp[0]
		var radio_label = tmp[1]
		var radio_id = newid() // radio_name + radio_value
		t += '<input id=' + radio_id + ' name=' + radio_name + ' type=radio class=chk value=' + radio_value +
			radio_event +
			((radio_value == radio_def) ? ' checked' : '') + '>' +
			((radio_label != '') ? ('<label for=' + radio_id + '>' + radio_label + '</label>') : '')
	}
	return (t)
}

//+----------------------------------------------------------------------------
//	函数:	make_checkbox
//	说明:	合成关于 checkbox 的 HTML 文本（带label），checkbox 的 value = 1
//	参数:	名称, 1/0（选中/未选）, 标签, 单击事件
//	返回:	HTML 文本
//-----------------------------------------------------------------------------
function make_checkbox()
{
	var arg = arguments
	var t =
		'<input id=' + arg[0] + ' name=' + arg[0] + ((arg[3]) ? (' onclick="' + arg[3] + '"') : '') +
		' type=checkbox class=chk value=1' + ((arg[1] == '1') ? ' checked' : '') + '>' +
		'<label for=' + arg[0] + '>' + arg[2] + '</label>'
	return (t)
}

//+----------------------------------------------------------------------------
//	函数:	radio_value
//	说明:	取 radio 组的值，未选中时返回空串
//	参数:	o - radio 组对象
//	返回:	文本
//-----------------------------------------------------------------------------
function radio_value(o)
{
	var i
	for (i = 0; i < o.length; i++)
	{
		if (o[i].checked) return o[i].value
	}
	return ('')
}

//+----------------------------------------------------------------------------
//	函数:	checkbox_value
//	说明:	取 checkbox 的值，返回 1 或 0
//	参数:	o - check 对象
//	返回:	整数
//-----------------------------------------------------------------------------
function checkbox_value(o)
{
	return (o.checked) ? 1 : 0
}



//+----------------------------------------------------------------------------
//	函数:	option_list
//	说明:	根据用 | 分隔的文本生成一个 option 的 HTML 文本
//	参数:	list - 用 | 分隔的文本，格式为“标签|值”
//	返回:	HTML 文本
//-----------------------------------------------------------------------------
function option_list(list)
{
	// value 不加引号可能会导致 option 设置错误，可导致 IE 崩溃
	// 注意 value 中不应该再出现单引号了，否则会出错
	var rst = ""
	for (var i = 0; i < list.length; i++)
	{
		var tmp = list[i].split("|")
		rst += "<option value='" + tmp[1] + "'>" + tmp[0] + "</option>"
	}
	return (rst)
}

//+----------------------------------------------------------------------------
//	函数:	option_fill
//	说明:	根据 list 数组生成 options 插入 select 对象，并默认选择指定 value 的 option
//	参数:	obj - 待操作的 select 对象
//			list - 文本数组，每一条记录格式为“标签|值”
//			value - 默认选中的值
//	返回:	无
//-----------------------------------------------------------------------------
function option_fill(obj, list, value)
{
	for (var i = 0; i < list.length; i++)
	{
		var tmp = list[i].split("|")
		var o = document.createElement("OPTION")
		obj.options.add(o)
		o.innerHTML = tmp[0]
		o.value = tmp[1]
		if (o.value == value) o.selected = true
	}
}

//+----------------------------------------------------------------------------
//	函数:	option_fit
//	说明:	根据 value 调整 select 的选项
//	参数:	obj - 待操作的 select 对象
//			value - 需选择的值
//	返回:	无
//-----------------------------------------------------------------------------
function option_fit(obj, value)
{
	for (var i = 0; i < obj.options.length; i++)
	{
		if (obj.options[i].value == value)
		{
			obj.options[i].selected = true
			return
		}
	}
}

//+----------------------------------------------------------------------------
//	函数:	option_valid
//	说明:	检测选择的 option 是否有 value，若无则提示
//	参数:	obj - 待操作的 select 对象
//	返回:	无
//-----------------------------------------------------------------------------
function option_valid(obj)
{
	var r = (obj.options[obj.selectedIndex].value == "");
	if (r) alert("您选的是一个目录，请选择目录下的具体项目。")
	return r;
}


function realOffset(el) {
	var x = 0; y = 0;
	do {
		x += el.offsetLeft;
		y += el.offsetTop;
		el = el.offsetParent;
	} while(el);
	return [x, y];
}

