var IE = navigator.userAgent.indexOf('MSIE') > -1 ? true: false;
var OPERA = navigator.userAgent.indexOf('Opera') > -1 ? true: false;
var IE7 = navigator.userAgent.indexOf('MSIE 7') > -1 ? true: false;
var FIREFOX = navigator.userAgent.indexOf('Firefox') > -1 ? true: false;
var GOOGLE = navigator.userAgent.indexOf('Chrome') > -1 ? true: false;
function $(_sId) {
	return typeof _sId == 'string' ? document.getElementById(_sId) : _sId;
};
function domReady(func) {
	if (!window.__load_events) {
		var init = function() {
			if (arguments.callee.done) return;
			arguments.callee.done = true;
			if (window.__load_timer) {
				clearInterval(window.__load_timer);
				window.__load_timer = null;
			};
			for (var i = 0; i < window.__load_events.length; i++) {
				window.__load_events[i]();
			};
			window.__load_events = null;
		};
		if (document.addEventListener) {
			document.addEventListener("DOMContentLoaded", init, false);
		};
		if (/WebKit/i.test(navigator.userAgent)) {
			window.__load_timer = setInterval(function() {
				if (/loaded|complete/.test(document.readyState)) {
					init();
				};
			},
			10);
		};
		window.onload = init;
		window.__load_events = [];
	};
	window.__load_events.push(func);
};
Object.extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	};
	return destination;
};
String.prototype.resetBlank = function() {
	return this.replace(/\s+/g, " ");
};
String.prototype.LTrim = function() {
	return this.replace(/^\s+/g, "");
};
String.prototype.trim = function() {
	return this.replace(/(^\s+)|(\s+$)/g, "");
};
String.prototype.getNum = function() {
	return this.replace(/[^\d]/g, "");
};
String.prototype.getEn = function() {
	return this.replace(/[^A-Za-z]/g, "");
};
String.prototype.getCn = function() {
	return this.replace(/[^\u4e00-\u9fa5\uf900-\ufa2d]/g, "");
};
String.prototype.strlen = function() {
	return this.replace(/[^\x00-\xff]/g, "--").length;
};
String.prototype.left = function(n) {
	return this.slice(0, n);
};
String.prototype.inArray = function(arr) {
	for (var i = 0; i < arr.length; i++) {
		if (this == arr[i]) return true;
	};
	return false;
};
Array.prototype.unset = function(str) {
	var _o = this;
	var len = _o.length;
	var temp = [];
	for (var i = 0; i < len; i++) {
		if (_o[i] != str) temp[temp.length] = _o[i];
	};
	return temp;
};
Array.prototype.inArray = function(str) {
	var _o = this;
	var len = _o.length;
	for (var i = 0; i < len; i++) {
		if (_o[i] == str) return true;
	};
	return false;
};
var isIE = (navigator.appName).indexOf("Microsoft") != -1;
if (window.Event && !isIE) {
	function SearchEvent() {
		var func = SearchEvent.caller;
		while (func != null) {
			var arg = func.arguments[0];
			if (arg) {
				if (String(arg.constructor).indexOf('Event') > -1) {
					return arg
				}
			}
			func = func.caller
		}
		return null
	};
	window.constructor.prototype.__defineGetter__("event",
	function() {
		return SearchEvent()
	});
	Event.prototype.__defineSetter__("returnValue",
	function(bool) {
		if (!bool) {
			this.preventDefault()
		}
		return bool
	});
	Event.prototype.__defineSetter__("cancelBubble",
	function(bool) {
		if (bool) {
			this.stopPropagation()
		}
		return bool
	});
	Event.prototype.__defineGetter__("clientX",
	function() {
		return this.pageX
	});
	Event.prototype.__defineGetter__("clientY",
	function() {
		return this.pageY
	});
	Event.prototype.__defineGetter__("keyCode",
	function() {
		return this.which
	});
	Event.prototype.__defineGetter__("button",
	function() {
		return this.which
	});
	Event.prototype.__defineGetter__("srcElement",
	function() {
		var n = this.target;
		while (n.nodeType != 1) {
			n = n.parentNode
		}
		return n
	});
	Event.prototype.__defineGetter__("offsetX",
	function() {
		return this.layerX
	});
	Event.prototype.__defineGetter__("offsetY",
	function() {
		return this.layerY
	});
	Event.prototype.attachEvent = function(type, handler) {
		this.addEventListener(type.substring(2), handler, true)
	};
	Event.prototype.detachEvent = function(type, handler) {
		this.removeEventListener(type.substring(2), handler, true)
	};
	window.constructor.prototype.attachEvent = function(type, handler) {
		this.addEventListener(type.substring(2), handler, true)
	};
	window.constructor.prototype.detachEvent = function(type, handler) {
		this.removeEventListener(type.substring(2), handler, true)
	};
	document.constructor.prototype.attachEvent = function(type, handler) {
		this.addEventListener(type.substring(2), handler, true)
	};
	document.constructor.prototype.detachEvent = function(type, handler) {
		this.removeEventListener(type.substring(2), handler, true)
	};
	Element.prototype.attachEvent = function(type, handler) {
		this.addEventListener(type.substring(2), handler, true)
	};
	Element.prototype.detachEvent = function(type, handler) {
		this.removeEventListener(type.substring(2), handler, true)
	};
	HTMLElement.prototype.attachEvent = function(type, handler) {
		this.addEventListener(type.substring(2), handler, true)
	};
	HTMLElement.prototype.detachEvent = function(type, handler) {
		this.removeEventListener(type.substring(2), handler, true)
	}
}

var base = new Object();
var bases = function() {
	var _o = this;
	var _t = '';
	this.divIndex = 0;
	this.dialogTop = 80;
	this.listenTimer = 0;
	this.closeTimer = 0;
	this.addClassName = function(obj, name) {
		if (obj == null) return true;
		if (obj.className.indexOf(name) > -1) return true;
		var n = obj.className;
		obj.className = n + ' ' + name;
	};
	this.removeClassName = function(obj, name) {
		var n = obj.className;
		obj.className = obj.className.replace(name, '');
	};
	this.fetchOffset = function(obj) {
		var leftOffset = obj.offsetLeft;
		var topOffset = obj.offsetTop;
		while ((obj = obj.offsetParent) != null) {
			leftOffset += obj.offsetLeft;
			topOffset += obj.offsetTop;
		};
		return {
			'left': leftOffset,
			'top': topOffset
		};
	};
	this.setPosition = function(id, o) {
		var obj = $(id);
		var pos = _o.fetchOffset(obj);
		var menu = $(o);
		menu.style.left = pos.left + 'px';
		menu.style.top = pos.top + obj.offsetHeight + 'px';
		menu.style.display = 'block';
	};
	this.getElementsByClassName = function(ele, className) {
		if (document.all) {
			var children = ele.all;
		} else {
			var children = ele.getElementsByTagName('*');
		};
		var elements = new Array();
		for (var i = 0; i < children.length; i++) {
			var child = children[i];
			var classNames = child.className.split(' ');
			for (var j = 0; j < classNames.length; j++) {
				if (classNames[j].trim() == className) {
					elements[elements.length] = child;
					break;
				}
			}
		};
		return elements;
	};
	this.appendscript = function(txt) {
		eval(txt);
	};
	this.evalscript = function(s) {
		if (s.indexOf('<script') == -1) return s;
		var p = /<script[^\>]*?src=\"([^\>]*?)\"[^\>]*?(reload=\"1\")?(?:charset=\"([\w\-]+?)\")?><\/script>/ig;
		var arr = new Array();
		while (arr = p.exec(s)) _o.appendscript(arr[1], '', arr[2], arr[3]);
		p = /<script (?!src)[^\>]*?( reload=\"1\")?>([^\x00]+?)<\/script>/ig;
		while (arr = p.exec(s)) _o.appendscript(arr[2]);
		return s;
	};
	this.encode = function(string) {
		return encodeURIComponent(string);
	};
	this.getFile = function(url) {
		var index = url.lastIndexOf('/');
		return url.substr(index + 1);
	};
	this.evaljson = function(resp) {
		var json;
		try {
			eval("json = " + resp);
		} catch(e) {
			var l = resp.indexOf('{');
			var r = resp.lastIndexOf('}');
			eval("json = " + resp.substr(l, r - l + 1));
		};
		return json;
	};
	this.preloadImg = function(arr) {
		var imgs = new Array();
		for (var i = 0; i < arr.length; i++) {
			imgs[i] = new Image();
			imgs[i].src = arr[i];
		}
	};
	this.backGroundDis = function() {
		if ($('shield') != null) {
			$('shield').style.display = 'block';
			return true;
		};
		var shield = document.createElement("DIV");
		shield.id = "shield";
		shield.style.width = "100%";
		shield.style.height = (Math.max(document.body.clientHeight, window.screen.availHeight)) + 'px';
		shield.style.textAlign = "center";
		shield.style.zIndex = "500";
		shield.style.filter = "Alpha(Opacity=50)";
		shield.style.opacity = 0.5;
		document.body.appendChild(shield);
	};
	this.backGroundDisClose = function() {
		if ($('shield') != null) return $('shield').style.display = 'none';
	};
	this.getElementsByName = function(name, tag) {
		var _o = document.getElementsByName(name);
		if (_o.length > 0) return _o;
		var temp = [];
		tag = tag || 'DIV';
		var _o = document.getElementsByTagName(tag.toUpperCase());
		for (var i = 0; i < _o.length; i++) {
			if (_o[i].getAttribute('name') == name) {
				temp[temp.length] = _o[i];
			}
		};
		return temp;
	};
	this.closeDoDiv = function(timer) {
		var timer = timer || 0;
		if (timer > 0) {
			this.closeTimer = setTimeout(function() {
				base.closeDoDiv(0);
			},
			timer * 1000);
			return false;
		};
		_o.dialogTop = 80;
		if ($('dialog-advanced') == null) return false;
		$('dialog-advanced').style.display = 'none';
		window.onscroll = null;
		window.onresize = null;
		_o.backGroundDisClose();
		if (_o.listenTimer) clearTimeout(_o.listenTimer);
		if (this.closeTimer) clearTimeout(this.closeTimer);
		return false;
	};
	this.closeDiv = function(timer) {
		var timer = timer || 0;
		base.closeDoDiv(timer);
		return false;
	};
	this.alert = function(title, message) {
		_o.dialog(title, message);
		_o.setOk(false);
	};
	this.setOk = function(_type, _value, _function) {
		if ($('dialog-submit') == null) return false;
		var obj = $('dialog-submit');
		if (_type == true) {
			obj.className = obj.className.replace('hidden', '').trim();
		} else {
			obj.className = obj.className.replace('hidden', '').trim() + ' hidden';
		};
		if (_value) $('dialog-submit').value = _value;
		if (_function) $('dialog-submit').onclick = _function;
	};
	this.setCancel = function(_value, _function) {
		if ($('dialog-cancel') == null) return false;
		var obj = $('dialog-cancel');
		if (_value) $('dialog-cancel').value = _value;
		if (_function) $('dialog-cancel').onclick = _function;
	};
	this.setTop = function(top) {
		base.dialogTop = top;
	};
	this.setMessage = function(message) {
		if ($('dialog-content') != null) $('dialog-content').innerHTML = message;
		else base.alert('提示信息', message);
	};
	this.dialog = function(title, message) {
		base.backGroundDis();
		var html = "<div class='hd' id='dialog-title'><h3>" + title + "</h3></div><div class='bd' id='dialog-content'>" + message + "</div><div class='ft' id='dialog-ft'><input type='button' id='dialog-submit' class='f-button' value='&#30830;&#23450;'/><input type='button' id='dialog-cancel' onclick='return base.closeDiv()' class='f-button' value='&#21462;&#28040;'/><div class='dialog-close'><a href='#' onclick='return base.closeDiv()'></a></div></div><div class='underlay' id='dialog-underlay' style='width:500px;height:120px;'></div>";
		var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
		if ($('dialog-advanced') != null) {
			$('dialog-advanced').innerHTML = html;
			$('dialog-advanced').style.top = base.dialogTop + scrollTop + 'px';
			$('dialog-advanced').style.display = 'block';
		} else {
			var _div = document.createElement('DIV');
			_div.className = 'dialog-advanced';
			_div.id = 'dialog-advanced';
			_div.style.top = base.dialogTop + scrollTop + 'px';
			_div.innerHTML = html;
			document.body.appendChild(_div);
		};
		this.listen();
	};
	this.iframe = function(title, url) {
		var title = title || '数据';
		this.dialog(title, "<iframe src='" + url + "' id='ajaxIframe' name='ajaxIframe' frameborder=0 style='width:100%;height:100%;border:0;overflow:hidden;' onload='base.reHeight();'></iframe>");
	},
	this.reHeight = function(h) {
		var c = parseInt($('ajaxIframe').contentWindow.document.body.clientHeight);
		var h = h || c;
		$('ajaxIframe').style.height = h + 'px';
	},
	this.listen = function() {
		if ($('dialog-advanced') == null) return false;
		var extend = (IE && !IE7) ? 2 : 0;
		$('dialog-underlay').style.width = ($('dialog-advanced').offsetWidth).toString() + 'px';
		$('dialog-underlay').style.height = ($('dialog-advanced').offsetHeight + extend).toString() + 'px';
		_o.listenTimer = setTimeout(_o.listen, 200);
	};
	this.showResult = function(title, url) {
		_o.alert(title, '<p class="loading">&#25968;&#25454;&#21152;&#36733;&#20013;&#65292;&#35831;&#31561;&#24453;&#46;&#46;&#46;&#46;&#46;&#46;</p>');
		_o.showResultAjax(url);
	};
	this.showResultAjax = function(url) {
		AJAX.request(url, {
			method: 'get',
			onComplete: function(resp) {
				if ($('dialog-content') == null) return false;
				$('dialog-content').innerHTML = resp;
				_o.evalscript(resp);
			}
		});
	};
	this.ajaxGet = function(url, callBack) {
		AJAX.request(url, {
			method: 'get',
			onComplete: function(resp) {
				if (callBack) callBack(resp);
			}
		});
	};
	this.textCounter = function(obj, limit, id) {
		var len = obj.value.trim().length;
		if (len > limit) {
			obj.value = obj.value.trim().substr(0, limit);
		} else {
			$(id).innerHTML = limit - len;
		}
	};
	this.doSubmit = function(e, id) {
		var e = e || event;
		if (e.keyCode == 13 && e.ctrlKey == true) {
			$(id).onclick();
		}
	};
	this.checkAll = function(obj, name) {
		var elements = obj.getElementsByTagName('input');
		var flag = $('chkall').checked;
		for (var i = 0; i < elements.length; i++) {
			if (elements[i].type.toLowerCase() == 'checkbox' && elements[i].disabled == false && elements[i].name.indexOf(name) == 0) {
				elements[i].checked = flag;
			}
		}
	};
};
myTips = {
	init: function(q, for_q) {
		if (!$(q).value) {
			$(for_q).style.display = '';
		}
		$(q).onfocus = function() {
			$(for_q).style.display = 'none';
			if (this.getAttribute('rel') && this.value == '') {
				this.value = this.getAttribute('rel');
				this.select();
			}
		};
		$(q).onblur = function() {
			if ($(q).value.trim().length == 0) $(for_q).style.display = 'block';
			else $(for_q).style.display = 'none';
		};
		$(for_q).onclick = function() {
			$(for_q).style.display = 'none';
			$(q).focus();
		};
		$(q).onblur();
	}
};
var base = new bases();
var AJAX = {
	activeRequestCount: 0,
	getTransport: function() {
		if (window.ActiveXObject && !window.XMLHttpRequest) {
			var msxmls = ['Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'];
			for (var i = 0; i < msxmls.length; i++) {
				try {
					return new window.ActiveXObject(msxmls[i]);
				} catch(e) {}
			};
			return null;
		} else {
			return new XMLHttpRequest();
		}
	},
	request: function(url, options) {
		if (AJAX.activeRequestCount > 0) return setTimeout(function() {
			AJAX.request(url, options);
		},
		50);
		AJAX.activeRequestCount++;
		this.transport = this.getTransport();
		this.url = url;
		this.options = {
			method: 'POST',
			asynchronous: true,
			contentType: 'application/x-www-form-urlencoded',
			parameters: '',
			onLoading: function() {},
			onLoaded: function() {},
			onInteractive: function() {},
			onComplete: function() {}
		};
		this.requestHeaders = ['X-Requested-With', 'XMLHttpRequest', 'Accept', 'text/javascript, text/html, application/xml, text/xml, */*'];
		options = options || {};
		Object.extend(this.options, options);
		if (this.options.method.toUpperCase() == 'GET' && this.options.parameters.length > 0) {
			this.url += (this.url.match(/\?/) ? '&': '?') + this.options.parameters;
		};
		/*this.url += (this.url.match(/\?/) ? '&' : '?') + 'random=' + (new Date()).toString();*/
		this.transport.open(this.options.method.toUpperCase(), this.url, this.options.asynchronous);
		if (this.options.asynchronous) {
			var self = this;
			this.transport.onreadystatechange = function() {
				switch (self.transport.readyState) {
				case 1:
					self.options.onLoading(self.transport);
					break;
				case 2:
					self.options.onLoaded(self.transport);
					break;
				case 3:
					self.options.onInteractive(self.transport);
					break;
				case 4:
					AJAX.activeRequestCount--;
					try {
						self.options.onComplete(self.transport.responseXML.lastChild.firstChild.nodeValue);
						evalscript(self.transport.responseXML.lastChild.firstChild.nodeValue);
					} catch(e) {
						self.options.onComplete(self.transport.responseText);
						evalscript(self.transport.responseText);
					}
					break;
				}
			};
		};
		if (this.options.method.toUpperCase() == 'POST') {
			this.requestHeaders.push('Content-type', this.options.contentType);
			if (this.transport.overrideMimeType) {
				this.requestHeaders.push('Connection', 'close');
			}
		};
		this.requestHeaders.push('Referer', window.location.href);
		this.requestHeaders.push('Charset', 'utf-8');
		if (this.options.requestHeaders) {
			this.requestHeaders.push.apply(this.requestHeaders, this.options.requestHeaders);
		};
		for (var j = 0; j < this.requestHeaders.length; j += 2) {
			this.transport.setRequestHeader(this.requestHeaders[j], this.requestHeaders[j + 1]);
		};
		this.transport.send(this.options.method.toUpperCase() == 'POST' ? this.options.parameters: null);
		if (!this.options.asynchronous) {
			this.options.onComplete(this.transport);
		};
	}
};
var log = {
	'debug': true,
	'out': function(msg) {
		if (this.debug) window.alert(msg);
		return false;
	}
};
var Form = {
	'serialize': function(name) {
		var obj = document.forms[name];
		if (typeof obj == 'undefined') return log.out('Form ' + name + ' not exists');
		var para = "";
		var arr = obj.elements;
		var elem = {};
		for (var i = 0,
		j; j = arr[i]; i++) {
			if (j.disabled || !j.name) {
				continue;
			};
			if (j.type && j.type.toLowerCase().inArray(["radio", "checkbox"]) && !j.checked) {
				continue;
			};
			var na = j.name;
			if (typeof elem[na] == "undefined") {
				elem[na] = [];
			};
			elem[na].push(encodeURIComponent(j.value));
		};
		var para = [];
		for (var name in elem) {
			for (var i = 0; i < elem[name].length; i++) {
				para[para.length] = name + "=" + elem[name][i];
			}
		};
		return para.join("&");
	},
	'save': function(name) {
		try {
			cache.set(name, this.serialize(name));
		} catch(e) {};
	},
	'load': function(name) {
		var o = document.forms[name];
		if (typeof o == 'undefined') return log.out('Form ' + name + ' not exists');
		var d = cache.get(name);
		if (d == false || d == null || typeof d == 'undefined') return true;
		var t = d.toString().split('&');
		var p = {};
		var tmp = [];
		for (var i = 0; i < t.length; i++) {
			tmp = t[i].split('=');
			if (tmp.length != 2) continue;
			if (tmp[0].indexOf('[]') > -1) {
				if (typeof p[tmp[0].toString()] == 'undefined') {
					p[tmp[0].toString()] = [];
					p[tmp[0].toString()][0] = decodeURIComponent(tmp[1]);
				} else {
					p[tmp[0].toString()][p[tmp[0].toString()].length] = decodeURIComponent(tmp[1]);
				}
			} else {
				p[tmp[0].toString()] = decodeURIComponent(tmp[1]);
			}
		};
		for (var i in p) {
			type = this.getType(o, i);
			switch (type.tag) {
			case 'input':
				switch (type.type) {
				case 'text':
				case 'radio':
					for (var j = 0; j < o[i].length; j++) {
						if (o[i][j].value == p[i]) {
							o[i][j].checked = true;
							break;
						}
					}
					break;
				case 'checkbox':
					for (var j = 0; j < o[i].length; j++) {
						o[i][j].checked = false;
						for (var k = 0; k < p[i].length; k++) {
							if (p[i][k] == o[i][j].value) {
								o[i][j].checked = true;
								break;
							}
						}
					}
					break;
				}
			case 'textarea':
			case 'select':
				o[i].value = p[i];
				break;
			}
		}
	},
	'getType': function(obj, name) {
		var o = obj[name];
		if (typeof o == 'undefined') return false;
		if (typeof o.tagName == 'undefined') {
			o = o[0];
		};
		return {
			'tag': o.tagName.toLowerCase(),
			'type': o.type.toLowerCase()
		};
	},
	'disabledCtrl': function(e) {
		var e = e || window.event;
		if (e.keyCode == 13 || e.keyCode == 17) return false;
	},
	'submitDo': function(name) {
		var obj = document.forms[name];
		if (typeof obj == 'undefined') return log.out('Form ' + name + ' not exists');
		var _url = obj.getAttribute('action');
		var _method = obj.getAttribute("method");
		var _parameters = Form.serialize(name);
		base.alert('数据请求中', '<p class="loading">数据提交中，请等待......</p>');
		AJAX.request(_url, {
			method: _method,
			parameters: _parameters + '&myFormAc=update',
			onComplete: function(resp) {
				base.alert('提示信息', '<p>' + resp + '</p>');
				base.evalscript(resp);
				return true;
			}
		});
		return false;
	}
};
