var isOpera, isIE, isNc, isFF = false;
if(navigator.userAgent.indexOf("Opera") != -1) {
	isOpera = true;
}else if(navigator.userAgent.indexOf("Firefox") != -1) {
	isFF = true;
}else if(navigator.appName == "Microsoft Internet Explorer") {
	isIE = true;
}else if(navigator.appName == "Netscape") {
	isNc = true;
}

Object.extend = function(target,src) {
	for (var property in src) {
		target[property] = src[property];
	}
	return target;
}

function extend_method(obj) {
	Object.extend(obj,method);
	return obj;
}

function $(id) {
	var obj = document.getElementById(id);
	if(!obj) return null;
	return extend_method(obj);
}

function $$(tag) {
	var obj = document.createElement(tag);
	return extend_method(obj);
}

var method = {
	getWidth: function() {
		return this.offsetWidth;
	},
	getHeight: function() {
		return this.offsetHeight;
	},
	getLeft: function() {
		var obj = this;
		var aLeft = obj.offsetLeft;
		while(obj = obj.offsetParent) {
			aLeft += obj.offsetLeft;
		}
		return aLeft;
	},
	getTop: function() {
		var obj = this;
		var aTop = obj.offsetTop;
		while(obj = obj.offsetParent) {
			aTop += obj.offsetTop;
		}
		return aTop;
	},
	show: function() {
		this.style.display = 'block';
	},
	hide: function() {
		this.style.display = 'none';
	},
	toggle: function() {
		this.visible() ? this.hide() : this.show();
	},
	visible: function() {
		return this.style.display != 'none';
	},
	remove: function() {
		this.parentNode.removeChild(this);
	},
	updateHtml: function(content) {
		if(typeof(content)=='object') {
			if(content.innerHTML!='') this.innerHTML = content.innerHTML;
			else if(content.value!='') this.innerHTML = content.value;
			if(content.constructor==Array) this.innerHTML = content.join('');
		}
		if(typeof(content)=='string') this.innerHTML = content.toString();
	},
	setClass: function(className) {
		this.className = className;
	},
	toggleClass: function(className1,className2) {
		this.className!=className1 ? this.setClass(className1) : this.setClass(className2);
	},
	setWidth: function(w) {
		this.style.width = w + 'px';
	},
	setHeight: function(h) {
		this.style.height =  h + 'px';
	},
	setPosition: function(x,y) {
		var pos = this.style.position;
		if(pos == 'static' || !pos) {
			this.style.position = 'relative';
		}
		this.style.left = x + 'px';
		this.style.top = y + 'px';
	},
	addListener: function(type,fn) {
		if(this.addEventListener) this.addEventListener(type,fn,false);
		else this.attachEvent('on' + type,fn);
		return this;
	},
	removeListener: function(type,fn) {
		if(this.removeEventListener) this.removeEventListener(type,fn,false);
		else this.detachEvent('on' + type,fn);
		return this;
	},
	$T: function(el) {
		var obj = this.getElementsByTagName(el);
		var n = obj.length;
		for(var i=0;i<n;i++) {
			extend_method(obj[i]);
		}
		return obj;
	}
};

function $F(id) {
	var obj = $(id);
	var method = obj.tagName.toLowerCase();
	return Form[method](obj);
}

var Form = {
	input: function(element) {
		switch (element.type.toLowerCase()) {
			case 'checkbox':
				return element.checked ? element.value : null;
			case 'radio':
				var radioes = document.getElementsByName(element.name);
				for(var i=0; i<radioes.length; i++) {
					if(radioes[i].checked) return radioes[i].value;
				}
				return null;
			default:
				return Form.textarea(element);
		}
	},
	textarea: function(element) {
		return element.value;
	},
	select: function(element) {
		return this[element.type == 'select-one' ? 'selectOne' : 'selectMany'](element);
	},
	selectOne: function(element) {
		var index = element.selectedIndex;
		return index >= 0 ? this.optionValue(element.options[index]) : null;
	},
	selectMany: function(element) {
		var values, length = element.length;
		if(!length) return null;
		for(var i=0, values=[]; i<length; i++) {
			 var opt = element.options[i];
			 if(opt.selected) values.push(this.optionValue(opt));
		}
		return values;
	},
	optionValue: function(opt) {
		return opt.value ? opt.value : opt.text;
	}
};

Function.prototype.bindNode = function(oNode) {
	var foo = this, iNodeItem;
	if(window._bindNodes==null) _bindNodes = [];
	_bindNodes.push(oNode);
	iNodeItem = _bindNodes.length - 1;
	oNode = null;
	return function(e) {
		foo.call(_bindNodes[iNodeItem],e||event);
	}
}

String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

function strlen(str) {
	var n = str.length;
	var len = n;
//	for(var i=0;i<n;i++) {
//		if(str.charCodeAt(i)<0 || str.charCodeAt(i)>255) len++;
//	}
	return len;
}

function getPageScroll() {
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}
	pageScroll = new Array('',yScroll) 
	return pageScroll;
}

function getRequest(){
	var request = new Array();
	var locurl = location.href;
	var start = locurl.indexOf("?");
	var end = locurl.length;
	if(start!=-1) {
		var tempstr = locurl.substring(start+1,end);
		tempstr = tempstr.split("&");
		var temp;
		var n = tempstr.length;
		for(var i=0;i<n;i++) {
			temp = tempstr[i].split("=");
			request[i] = temp;
		}
	}
	return request;
}

function getParamValue(paramName) {
	var url = window.location.search;
	if(url.indexOf("?") != -1) {
		var strs = url.substr(1).split("&");
		for(var i=0; i<strs.length; i++) {
			var ss = strs[i].split("=");
			if (ss[0] == paramName)
				return decodeURIComponent(ss[1]);
		}
	}
	return null;
}

function get_time() {
	var now = new Date();
	var hh = now.getHours();
	var mm = now.getMinutes();
	var dateStr = "";
	if(hh<10) dateStr += "0";
	dateStr += hh + ":";
	if(mm<10) dateStr += "0";
	dateStr += mm;
	return dateStr;
}

function get_hms() {
	var now = new Date();
	var hh = now.getHours();
	var mm = now.getMinutes();
	var ss = now.getSeconds();
	var dateStr = "";
	if(hh<10) dateStr += "0";
	dateStr += hh + ":";
	if(mm<10) dateStr += "0";
	dateStr += mm + ":";
	if(ss<10) dateStr += "0";
	dateStr += ss;
	return dateStr;
}

function URLdecode(str) {
	var ret = "";
	for(var i=0;i<str.length;i++) {
		var chr = str.charAt(i);
		if(chr == "+") {
			ret += " ";
		}else if(chr=="%") {
			var asc = str.substring(i+1,i+3);
			if(parseInt("0x"+asc)>0x7f) {
				ret += decodeURI("%"+ str.substring(i+1,i+9));
				i += 8;
			}else {
				ret += String.fromCharCode(parseInt("0x"+asc));
				i += 2;
			}
		}else {
			ret += chr;
		}
	}
	return ret;
}

function ajaxSubmit(actionUrl,id) {
	var http_request = false;
	
	if(window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType) {
			http_request.overrideMimeType("text/xml");
		}
	}
	else if(window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {}
		}
	}
	if(!http_request) {
		alert("您的浏览器不支持创建XML对象。");
		return false;
	}
	http_request.onreadystatechange = function() {
		if(http_request.readyState == 4) {
			if(http_request.status == 200) {
//				alert(http_request.responseText);
			}else {
				alert("数据异常，或无法读取。");
			}
		}else {
			
		}
	}
	http_request.open("POST",actionUrl,true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	http_request.send(getQueryString(id));
}

function logSubmit(actionUrl,id) {
	$("log_con").value = getQueryString(id);
	$("log_con").setAttribute("name", "context");
	$("log").setAttribute("action", actionUrl);
	$("log").setAttribute("method", "post");
	$("log").setAttribute("target", "_blank");
	$("log").submit();
}

function getQueryString(id) {
	var obj = $(id);
	var queryString = "";
	var items = "context";
	var start,end;
	
	var queryArray = obj.$T("div");
	end = queryArray.length;
	if(end > 200)  start = end - 200;
	else start = 0;
	
	for(var i=start; i<end; i++) {
		queryString += queryArray[i].innerHTML;
	}
	
	queryString = items + "=" + queryString;
	
	return queryString;
}
