var isChrome = /Chrome/.test(navigator.userAgent);
var isOpera = /Opera/.test(navigator.userAgent), $_ID = [];
var isFF = /Gecko\/\d* Firefox/g.test(navigator.userAgent);
var isIE = /compatible; MSIE/g.test(navigator.userAgent);

function version() {
	var info = navigator.userAgent, ver;
	if (isIE) {
		ver = info.substr(info.indexOf('MSIE') + 5);
		ver = parseFloat(ver.substr(0, ver.indexOf(';')));
	}
	else if (isFF) ver = info.substr(info.lastIndexOf('/') + 1);
	else if (isOpera) ver = parseFloat(info.substr(0, info.indexOf(' ')).replace('Opera/', ''));
	else if (isChrome) {
		ver = info.substr(info.indexOf('Chrome') + 7);
		ver = ver.substr(0, ver.indexOf(' '));
	}
	return ver;
}

function ID_ini() {
	var i = j = 0, input = document.getElementsByTagName('input');
	if (! input.length) return;
	if (! $_ID.length) {
		for (; i < input.length; i++) {
			if ((input[i].name == 'ID' || input[i].name == 'ID[]') && input[i].type == 'checkbox') $_ID[j++] = input[i];
		}
		try {
			var sAll = document.getElementById('sAll');
			var change = function () {
				var sAll = document.getElementById('sAll');
				var c = sAll.checked, i = 0;
				for (; i < $_ID.length; i++) $_ID[i].checked = c;
			}
			var click = function () {
				for (var i = 0, logic = true; i < $_ID.length; i++) {
					if (! $_ID[i].checked) {logic = false; break;}
				}
				document.getElementById('sAll').checked = logic;
			}
			for (i = 0; i < $_ID.length; i++) {
				if (isIE) $_ID[i].attachEvent('onclick', click);
				else $_ID[i].addEventListener('click', click, false);
			}
			if (isIE) sAll.attachEvent('onclick', change);
			else sAll.addEventListener('click', change, false);
			change = function () { document.forms['submit'].submit(); }
			sAll = document.getElementsByName('CurrentPage')[0];
			if (isIE) sAll.attachEvent('onchange', change);
			else sAll.addEventListener('change', change, false);
		}
		catch (e) {}
	}
}

function clear_focus() {
	var a = document.getElementsByTagName('a'), i;
	var area = document.getElementsByTagName('area');
	if (isIE) {
		for (i = 0; i < a.length; i++) a[i].onfocus = function () {this.blur();}
		for (i = 0; i < area.length; i++) area[i].onfocus = function () {this.blur();}
	}
	else {
		for (i = 0; i < a.length; i++) a[i].addEventListener('focus', function () {this.blur();}, false);
		for (i = 0; i < area.length; i++) area[i].addEventListener('focus', function () {this.blur();}, false);
	}
	area = a = null;
}

function page(going) {
	try {
		var listForm = document.forms['list'];
		var obj = listForm.elements;
		if (! obj['CurrentPage'].length) return;
		var i = 0, MIN, MAX, Pages = obj['CurrentPage'].length ? +obj['CurrentPage'].value : 0;
		MAX = obj['CurrentPage'].length ? +obj['CurrentPage'][obj['CurrentPage'].length - 1].value : 0;
		MIN = obj['CurrentPage'].length ? +obj['CurrentPage'][0].value : 0;
		switch (going) {
			case 1: Pages = MIN; break; case 2: Pages++; break;
			case 3: Pages--; break; case 4: Pages = MAX; break;
		}
		if (Pages < MIN || Pages > MAX) return;
		for (; i < obj['CurrentPage'].length; i++) {
			if (+obj['CurrentPage'][i].value == Pages) {obj['CurrentPage'][i].selected = true; break;}
		}
		obj = null; listForm.submit();
	}
	catch (e) { alert('警告：未找到指定的翻页域！'); return; }
}

Function.prototype.toString = function () { return '程序内容由“昊玄”编写完成，有事请与他联系(QQ:57659930)！'; }
String.prototype.Trim = function () { return this.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); }
String.prototype.LTrim = function () { return this.replace(/^\s\s*/, ''); }
String.prototype.RTrim = function () { return this.replace(/\s\s*$/, ''); }

function Close() {
	if (isIE) {
		var ua = navigator.userAgent;
		var version = parseFloat(ua.substring(ua.indexOf('MSIE ') + 5, ua.indexOf(';', ua.indexOf('MSIE '))), 10);
		if (version < 5.5) {
			var obj = '<object id="jmClose" classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">\r\n';
			obj += ' <param name="Command" value="Close" />\r\n</object>';
			document.body.insertAdjacentHTML('beforeEnd', obj);
			document.getElementById('jmClose').Click();
		}
		else {window.opener = null; window.close();}
	}
	else window.close();
}

function swf_move(width, visual_height, text_height, pics, links, texts, ID, src) {
  if (width == null) return 1;
  if (visual_height == null) return 2;
  if (text_height == null) return 3;
  if (pics == null) return 4;
  src = src == null ? 'focus.swf' : src;
  var height = visual_height + text_height;
  var swf = '\r\n<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="' + width + '" height="' + height + '">\r\n';
  swf += '  <param name="allowScriptAccess" value="sameDomain" />\r\n  <param name="movie" value="' + src + '" />\r\n  <param name="quality" value="high" />\r\n  <param name="bgcolor" value="#FFFFFF" />\r\n  <param name="menu" value="false" />\r\n  <param name="wmode" value="opaque" />\r\n';
  swf += '  <param name="FlashVars" value="pics=' + pics + '&links=' + links + '&texts=' + texts + '&borderwidth=' + width + '&borderheight=' + visual_height + '&textheight=' + text_height + '" />\r\n';
  swf += '  <embed src="' + src + '" wmode="opaque" FlashVars="pics=' + pics + '&links=' + links + '&texts=' + texts + '&borderwidth=' + width + '&borderheight=' + visual_height + '&textheight=' + text_height + '" menu="false" bgcolor="#F0F0F0" quality="high" width="' + width + '" height="' + visual_height + '" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\r\n</object>\r\n';
  if (ID == null || ID == '') document.write(swf); else document.getElementById(ID).innerHTML = swf;
}

window.onload = function () {
	ID_ini(); clear_focus();
	if (typeof(autorun) == 'function') autorun();
}

function jmYMD(Y, M, D) {
	Y.options.length = M.options.length = D.options.length = 0;
	var month = [32, 29, 32, 31, 32, 31, 32, 32, 31, 32, 31, 32], year = new Date().getFullYear();
	for (var i = year - 10; i > year - 20; i--) Y.options.add(new Option(i, i));
	for (i = 1; i < 13; i++) M.options.add(new Option(i < 10 ? '0' + i : i, i));
	for (i = 1; i < 32; i++) D.options.add(new Option(i < 10 ? '0' + i : i, i));
	var cY = function () {
		var year = +Y.value, isRun = false;
		if ((! (year % 100) && ! (year % 400)) || (year % 100 && ! (year % 4))) isRun = true;
		if (+M.value == 2) {
			month[1] = isRun ? 30 : 29; D.options.length = 0;
			for (var i = 1; i < month[1]; i++) D.options.add(new Option(i < 10 ? '0' + i : i, i));
		}
	}
	var cM = function () {
		var year = +Y.value, isRun = false, i = 1;
		if ((! (year % 100) && ! (year % 400)) || (year % 100 && ! (year % 4))) isRun = true;
		if (+M.value == 2) month[1] = isRun ? 30 : 29; D.options.length = 0;
		for (; i < month[+M.value - 1]; i++) D.options.add(new Option(i < 10 ? '0' + i : i, i));
	}
	if (isIE) Y.attachEvent('onchange', cY);
	else Y.addEventListener('change', cY, false);
	if (isIE) M.attachEvent('onchange', cM);
	else M.addEventListener('change', cM, false);
}

function Racs(obj) {
	var txt = '汉,壮,满,回,苗,维吾尔,土家,彝,蒙古,藏,布依,侗,瑶,朝鲜,白,哈尼,哈萨克,黎,傣,畲,傈僳,仡佬,东乡,拉祜,水,佤,纳西,羌,土,仫佬,锡伯,柯尔克孜,达斡尔,景颇,毛南,撒拉,布朗,塔吉克,阿昌,普米,鄂温克,怒,京,基诺,德昂,保安,俄罗斯,裕固,乌孜别克,门巴,鄂伦春,独龙,塔塔尔,赫哲,高山,珞巴'.split(',');
	if (obj) {
		obj.options.length = 0; var i = 0;
		obj.options.add(new Option('-请选择-', 0));
		for (; i < txt.length; i++) obj.options.add(new Option(txt[i] + '族', txt[i]));
	}
}
