/*************************************************
基础扩展prototype	Date:2008-11-20
*************************************************/
//String.Trim()   -->   去除首尾空格
String.prototype.Trim = function(){
	return this.replace(/^\s+|\s+$/, "");
};
//String.Count()  -->	取字符串长度，中文转换为两个单位
String.prototype.Count = function() {
	var x = this.replace(/[\u4e00-\u9fa5]/g, "aa");
	return x.length;
};
//String.ToInt()  -->   字符串转换为数字
String.prototype.ToInt = function(){
	return new Number(this);
};
//String.IsNull() -->   判断是否为空
String.prototype.IsNull = function(){
	return this.length == "";
}
//String.Zero()   -->   为单个数字补零
String.prototype.Zero = function(){
	return this.replace(/\b(\d)\b/g, "0$1");
}
//Number.ToStr()  -->   数字转换为字符串
Number.prototype.ToStr = function(){
	return new String(this);
};
//Date.ToStr()    -->   时间转换为字符串
Date.prototype.ToStr = function(){
	return this.getYear() + "-" + (this.getMonth() * 1 + 1) + "-" + this.getDate() + " " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
};
//$(obj)  -->   根据ID获取对象
function $(){
	return document.getElementById(arguments[0]);
};
//Firefox函数库扩展
if(typeof(HTMLElement) != "undefined"){  
	HTMLElement.prototype.contains = function(obj){  
		while(obj != null && typeof(obj.tagName) != "undefind"){
			if(obj == this){
				return true;
			};
			obj = obj.parentNode;
		};
		return false;  
	};
};
/*************************************************
by类库
*************************************************/
var by = {};
/*************************************************
by.GetName(obj)  -->   根据Name获取对象,返回数组
*************************************************/
by.GetName = function(){
	return document.getElementsByName(arguments[0]);
};
/*************************************************
by.GetTag(obj)  -->   根据TagName获取对象,返回数组
*************************************************/
by.GetTag = function(){
	return document.getElementsByTagName(arguments[0]);
};
/*************************************************
by.addEvent(Event, Function)  -->   给事件绑定函数
*************************************************/
by.AddEvent = function(){
	if (!document.all){
		arguments[0].addEventListener(arguments[1], arguments[2], false);
	}
	else {
		
		arguments[0].attachEvent("on" + arguments[1], arguments[2]);
	};
};
/*************************************************
by.DelEvent(Document, Event, Function)  -->   给事件解除绑定函数
*************************************************/
by.DelEvent = function(){
	if (!document.all){
		arguments[0].removeEventListener(arguments[1], arguments[2], false);
	}
	else {
		
		arguments[0].detachEvent("on" + arguments[1], arguments[2]);
	};
};
/*************************************************
by.body()  -->   获取body对象
*************************************************/
by.body = function() {
	return document.compatMode == "BackCompat" ? document.body : document.documentElement;
};
/*************************************************
by.Ajax()  -->   Ajax类
	Send方法的参数解释：

	Ajax.Send(Url[, Async[, CallFunc[, User[, Pass]]]]);
	Ajax：
		必选项，Ajax对象的一个实例。
	Url：
		必选项，请求的地址。
	Async：
		可选项，为空表示请求模式为GET，为具体参数（如：{key : "postkey", value : "postvalue"}）表示请求为POST。
	CallFunc：
		可选项，为空表示同步请求，为一个Function对象时表示异步请求并在Ajax的onreadystatechange事件中调用此函数。
	User：
		可选项，服务器需要验证时此参数为验证需要的用户名。
	Pass：
		可选项，服务器需要验证是此参数为验证需要的密码。

	例子：
	function getSend() {
		if(ajax.xml.readyState == 4 && ajax.xml.status == 200){
			alert(ajax.xml.responseText);
		}
		else {
			alert("wait...");
		}
	}
	var PostString = {
		classid : 1,
		search : "无忧脚本"
	}

	var ajax = new Ajax();
	ajax.Send("http://bbs.51js.com/");//GET同步请求，可用ajax.xml.responseText获取返回值
	ajax.Send("http://bbs.51js.com/", false, getSend);//GET异步请求，回调getSend()函数
	ajax.Send("http://bbs.51js.com/", PostString);//POST同步请求，可用ajax.xml.responseText获取返回值
	ajax.Send("http://bbs.51js.com/", PostString, getSend);//POST异步请求，回调getSend()函数
*************************************************/
by.Ajax = function(){
	this.xmlObject = function() {
		try { 
			return new ActiveXObject("Microsoft.XMLHTTP"); 
		}
		catch(e) { 
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) { 
				try { 
					return new XMLHttpRequest(); 
				}
				catch(e) {
					return window.createRequest();
				} ;
			};
		};
	};
	this.xml = this.xmlObject();
	this.Send = function() {
		var PostStr = !!arguments[1] ? (function() {
			var tempArr = new Array();
			for(var i in arguments.callee.caller.arguments[1]){
				tempArr.push(i + "=" + arguments.callee.caller.arguments[1][i]);
			}
			return tempArr.join("&");
		})() : null;
		this.xml.open(PostStr ? "POST" : "GET", arguments[0], !!arguments[2], arguments[3], arguments[4]);
		!!arguments[2] ? this.xml.onreadystatechange = arguments[2] : null;
		if(PostStr){
			this.xml.setRequestHeader("Content-Length", PostStr.length);   
			this.xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		};
		this.xml.send(PostStr);
	};
	this.Close = function() {
		this.xml.abort();
	};
};
/*************************************************
by.InsertHtml(Where, Element, Html)  -->   插入HTML到指定位置
*************************************************/
by.InsertHtml = function(where, el, html){
	where = where.toLowerCase();
	if(el.insertAdjacentHTML){
		switch(where){
			case "1":
				el.insertAdjacentHTML('BeforeBegin', html);
				return el.previousSibling;
			case "2":
				el.insertAdjacentHTML('AfterBegin', html);
				return el.firstChild;
			case "3":
				el.insertAdjacentHTML('BeforeEnd', html);
				return el.lastChild;
			case "4":
				el.insertAdjacentHTML('AfterEnd', html);
				return el.nextSibling;
		};
	};
	var range = el.ownerDocument.createRange();
	var frag;
	switch(where){
		case "1":
			range.setStartBefore(el);
			frag = range.createContextualFragment(html);
			el.parentNode.insertBefore(frag, el);
			return el.previousSibling;
		case "2":
			if(el.firstChild){
				range.setStartBefore(el.firstChild);
				frag = range.createContextualFragment(html);
				el.insertBefore(frag, el.firstChild);
				return el.firstChild;
			}
			else{
				el.innerHTML = html;
				return el.firstChild;
			};
		case "3":
			if(el.lastChild){
				range.setStartAfter(el.lastChild);
				frag = range.createContextualFragment(html);
				el.appendChild(frag);
				return el.lastChild;
			}
			else{
				el.innerHTML = html;
				return el.lastChild;
			};
		case "4":
			range.setStartAfter(el);
			frag = range.createContextualFragment(html);
			el.parentNode.insertBefore(frag, el.nextSibling);
			return el.nextSibling;
	};
};
/*************************************************
by.Move(obj, Html)  -->   移动对象
*************************************************/
by.Move = function(o, e){
	var event = window.event || e;
	var _sy = event.clientY;
	var _sx = event.clientX;
	var _xy = parseInt(o.style.top);
	var _xx = parseInt(o.style.left);
	document.onmouseup = function(){
		this.onmousemove = null;
	};
	if(event.preventDefault){
		event.preventDefault();
	};
	document.onmousemove = function(e){
		var event = window.event || e;
		if(document.all && event.button == 0){
			this.onmousemove = null;
			return false;
		};
		var xtop = _xy + event.clientY - _sy;
		var xleft = _xx + event.clientX - _sx;
		if(xtop > (o.parentNode.offsetHeight - o.offsetHeight)){
			xtop = o.parentNode.offsetHeight - o.offsetHeight;
		}
		if(xtop < 0){
			xtop = 0;
		}
		if(xleft > (o.parentNode.offsetWidth - o.offsetWidth)){
			xleft = o.parentNode.offsetWidth - o.offsetWidth;
		}
		if(xleft < 0){
			xleft = 0;
		}
		o.style.top = xtop + "px";
		o.style.left = xleft + "px";
	};
};
/*************************************************
by.GetOverTime(s)  -->   将秒数转换为hh:mm:ss
*************************************************/
by.GetOverTime = function(s){
	var Arr = [];
	Arr.push(Math.floor(s / 3600));
	Arr.push(Math.floor(s % 3600 / 60));
	Arr.push(Math.floor(s % 60));
	return Arr.join(":").Zero();
}
/*************************************************
by.getEvent()  -->   获取对象evnet
*************************************************/
by.getEvent = function(){
	if(document.all){
		return window.event;
	}
	func = by.getEvent.caller;
	while(func != null){
		var arg0 = func.arguments[0];
		if(arg0){
			if((arg0.constructor == Event || arg0.constructor == MouseEvent) || (typeof(arg0) == "object" && arg0.preventDefault && arg0.stopPropagation)){
				return arg0;
			}
		}
		func = func.caller;
	}
	return null;
}
/*************************************************
by.IsIE()  -->   判断浏览器是否属于IE
*************************************************/
by.IsIE = function(){
	return navigator.userAgent.indexOf("MSIE") > 0;
}
