﻿String.prototype.trim=function(){
    var _re,_argument = arguments[0] || " ";
	typeof(_argument)=="string"?(_argument == " "?_re = /(^\s*)|(\s*$)/g : _re = new RegExp("(^"+_argument+"*)|("+_argument+"*$)","g")) : _re = _argument;
    return this.replace(_re,"");
}
String.prototype.ltrim=function(){
    var _re, _argument = arguments[0] || " ";
	typeof(_argument)=="string"?(_argument == " "?_re = /(^\s*)/g : _re = new RegExp("(^"+_argument+"*)","g")) : _re = _argument;
    return this.replace(_re,"");
}
String.prototype.rtrim=function(){
    var _re, _argument = arguments[0] || " ";
	typeof(_argument)=="string"?(_argument == " "?_re = /(\s*$)/g : _re = new RegExp("("+_argument+"*$)","g")) : _re = _argument;
    return this.replace(_re,"");
}
String.prototype.stripTags=function(){
    return this.replace(/<\/?[^>]+>/gi, '');
}
String.prototype.cint=function(){
    return this.replace(/\D/g,"")-0;
}
String.prototype.camelize = function(){
	return this.replace(/(-\S)/g,function($1){return $1.toUpperCase().substring(1,2)})
}
String.prototype.hasSubString = function(s,f){ //substring, filter
	if(!f) f="";
	return (f+this+f).indexOf(f+s+f)==-1?false:true;
}

function NameSpace(){
}

function copyProperty(src,target){
	for (var key in target){
		src[key] = target[key];
	}
	return src;
}
var Element = { Method : {  // 方法库
	hide : function(){this.style.display="none"; return this},
	show : function(){this.style.display=""; return this},
	getStyle : function(s){
		var value = this.style[s.camelize()];
		if (!value){
			if (this.currentStyle) {
				value = this.currentStyle[s.camelize()];
   		    }else if (document.defaultView && document.defaultView.getComputedStyle) {
				var css = document.defaultView.getComputedStyle(this, null);
				value = css ? css.getPropertyValue(s) : null;
		    }
		}
		return value;
	},
	setStyle : function(s,v){ // s= {"margin-bottom":"1px", "list-style":"none"}
		if (typeof s == "string"){
			this.style[s.camelize()] = v;
		}else{
			for (var k in s){
				this.style[k.camelize()] = s[k];
			}
		}
		return this;
	},
	toggle : function(){this.getStyle("display")=="none"?this.setStyle("display",""):this.setStyle("display","none"); return this},
	hasClassName : function(c){return this.className.hasSubString(c," ")?true:false;},
	addClassName : function(c){if(!this.hasClassName(c)){this.className.trim()+=c};return this},
	removeClassName : function(c){if(this.hasClassName(c)){this.className = (" "+this.className+" ").replace(" "+c+" "," ").trim(); return this}},
	getElementsByClassName : function(c){getElementsByClassName(c,this)},
	getElementsByAttribute : function(n,v){ //name,value;
		var elems = this.getElementsByTagName("*");
		var elemList = [];
		for (var i=0,j; j=elems[i]; i++){
			var att = j.n || j.getAttribute(n);
			if (att==v){
				elemList.push(j);
			}
		}
		return elemList;
	},
	nextElement : function(){},
	previousElement : function(){},
	moveAhead : function(){},
	moveBack : function(){}
}
};

function $(){
	var elem = typeof(arguments[0])=="string"?document.getElementById(arguments[0]):arguments[0];
	var t = arguments[1];
	if (typeof t == "undefined" || t==true) copyProperty(elem,Element.Method);
	return elem;
};

var Ajax={
	xmlhttp:function (){
		try{
			return new ActiveXObject('Microsoft.XMLHTTP');
		}catch(e){
			try{
				return new ActiveXObject('Msxml2.XMLHTTP');
			}catch(e){
				return new XMLHttpRequest();
			}
		}
	}
};
Ajax.Request=function (){
	if(arguments.length<2)return ;
	var _p = {asynchronous:true,method:"GET",parameters:""};
	copyProperty(_p,arguments[1]);
	var _x=Ajax.xmlhttp();
	var _url=arguments[0];
	if(_p["parameters"].length>0) _p["parameters"]+='&_=';
	if(_p["method"].toUpperCase()=="GET") _url+=(_url.match(/\?/)?'&':'?')+_p["parameters"];
	_x.open(_p["method"].toUpperCase(),_url,_p["asynchronous"]);
	_x.onreadystatechange=function (){
		if(_x.readyState==4){
			if(_x.status==200)
				_p["onComplete"]?_p["onComplete"](_x):"";
			else{
				_p["onError"]?_p["onError"](_x):function(){
					var ErrNum = {100:"Continue",101:"Switching protocols",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Request Entity Too Large",414:"Request-URI Too Long",415:"Unsupported Media Type",416:"Requested Range Not Suitable",417:"Expectation Failed",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Timeout",505:"HTTP Version Not Supported"};
					ErrNum[_x.status]?alert(ErrNum[_x.status]):"";
					}();
			}
		}
	};
	//_x.setRequestHeader("If-Modified-Since","0");
	if(_p["method"].toUpperCase()=="POST")_x.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

	for (var ReqHeader in _p["setRequestHeader"]){
		_x.setRequestHeader(ReqHeader,_p["setRequestHeader"][ReqHeader]);
	}
	_x.send(_p["method"].toUpperCase()=="POST"?(_p["postBody"]?_p["postBody"]:_p["parameters"]):null);

};

function StringBuffer(){
    this.data = [];
};
StringBuffer.prototype.append = function(){
    this.data.push(arguments[0]);
	return this;
};
StringBuffer.prototype.toString = function(){
    return this.data.join(arguments[0]||"");
};
StringBuffer.prototype.length = function(){
	return this.data.length;
};


Array.prototype.indexOf = function(){
	for (i=0; i<this.length; i++){
		if (this[i]==arguments[0])
			return i;
	}
	return -1;
}
Array.prototype.lastIndexOf = function(){
	for (i=this.length-1; i>=0; i--){
		if (this[i]==arguments[0])
			return i;
	}
	return -1;
}

function $E(str){
	return encodeURIComponent(str);
}

function request(paras){  //获取 url 的参数值，不区分大小写,如无此参数，返回空字符串.
	var url = location.href;
	var paraString = "&"+url.substring(url.indexOf("?")+1,url.length)+"&";
	if (paraString.indexOf("&"+paras+"=")==-1){return ""};
	paraString = paraString.substring(paraString.indexOf("&"+paras+"=")+paras.length+2,paraString.length);
	return paraString.substring(0,paraString.indexOf("&"));
}

function getElementsByClassName(cN, parentElement){
	var elems = (parentElement?$(parentElement):document.body).getElementsByTagName("*");
	var result=[];
	for (i=0; j=elems[i]; i++){
		j.hasClassName = Element.Method.hasClassName;
		if (j.hasClassName(cN)){
			result.push(j);
		}
	}
	return result;
}
document.getElementsByClassName = getElementsByClassName;

