
function $(){
    for (var i = 0, rtn = [], a = arguments , length = a.length; i < length; i++) rtn.push(document.getElementById(a[i]));
    return rtn.length > 1 ? rtn : rtn[0];
}

$.create = function(parent){	
	if(typeof(parent) != 'undefined'){
		function child(){
			if(typeof(this.initialize) === 'function') this.initialize.apply(this, arguments);
		}
		child.prototype = parent.prototype;
		return child;
	}else
		return function(){
			if(typeof(this.initialize) === 'function') this.initialize.apply(this, arguments);
		}
}

$.ext = function(){
	var b=arguments[0], a=1, c;
	if(arguments.length==1) return b;
	while(c=arguments[a++]){
		for (var i in c) b[i] = c[i];
	}
	return b;
}

$.ext(Function.prototype, {
	_instanceof:function(instance){
		if(typeof(instance) === 'undefined' || instance === null) return false;
		if(instance instanceof this) return true;
		else return false;
	}
});

$.ext(String.prototype,{
decode:function(){
	return unescape(this);
},
encode:function(){
	return escape(this);
},
ends:function(suffix){
	return (this.substr(this.length-suffix.length)===suffix);
},
is_ansi:function(){
	return /^\w+$/.test(this);
},
is_email:function(){
	return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(this);
},
is_gb:function(){
	return /^[\u4e00-\u9fa5]+$/.test(this);
},
is_card:function(){
	return /^\d{15}|\d{18}$/.test(this);
},
is_int:function(){
	return /^[+-]?[0-9]+$/.test(this);
},
is_ip:function(){
	return /^\d+\.\d+\.\d+\.\d+$/.test(this);
},
is_longdate:function(){
	return /^\d{4}-\d{1,2}-\d{1,2}\s\d{2}:\d{2}:\d{2}$/.test(this);
},
is_number:function(){
	return /^(-?\d+)(\.\d+)?$/.test(this);
},
is_phone:function(){
	return /^\d{3}-\d{8}|\d{4}-\d{7}$/.test(this);
},
is_shortdate:function(){
	return /^\d{4}-\d{1,2}-\d{1,2}$/.test(this);
},
is_uniqueid:function(){
	return /^2\d{16}$/.test(this);
},
starts:function(prefix){
	return (this.substr(0, prefix.length)===prefix);
},
strip_scripts:function(){
	return this.replace(new RegExp('(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)','img'),'');
},
strip_tags:function(){
	return this.replace(/<\/?[^>]+>/gi,'');
},
trim:function(){
	return this.replace(/^\s+|\s+$/g,'');
},
trimEnd:function(){
	return this.replace(/\s+$/,'');
},
trimStart:function(){
	return this.replace(/^\s+/,'');
}
});

$.ext(Array.prototype,{
add:function(item){
	this[this.length]=item
},
clear:function(){
	this.length=0;
},
clone:function(){
	if(this.length===1)return [this[0]];
	else return Array.apply(null,this);
},
contains:function(item){
	return (this.index_of(item)>=0);
},
dequeue:function(){
	return this.shift();
},
each:function(method,callback){
	for(var i=0,l=this.length;i<l;i++){
		var elt=this[i];
		if(typeof(elt)!=='undefined')method.call(this,i,elt,callback);
	}
},
index_of:function(item,start){
	if(typeof(item)==="undefined")return -1;
	var length=this.length;
	if(length!==0){
		start=start-0;
		if(isNaN(start))start=0;
		else{
			if(isFinite(start))start=start-(start%1);
			if(start<0)start=Math.max(0,length+start);
		}
		for(var i=start;i<length;i++){
			if((typeof(this[i])!=="undefined")&&(this[i]===item))return i;
		}
	}
	return -1;
},
insert:function(index,item){
	this.splice(index,0,item);
},
merge:function(items){
	this.push.apply(this,items);
},
remove:function(item){
	var index=this.index_of(item);
	if(index>=0)this.splice(index,1);
},
removeAt:function(index){
	this.splice(index,1);
}
});

if(!window.XMLHttpRequest){
	window.XMLHttpRequest = function(){
		var b = ["Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
		for(var a = 0; a < b.length; a++){
			try{
				var c = new ActiveXObject(b[a]);
				return c;
			}catch(ex){}
			return null;
		}
	}
}

$.ajax = function(u){
	var x = new XMLHttpRequest, c=false;
	if(!x) return false;
	var m, v, d, arg=arguments;
	m = arg.length > 1 ? arg[1].toUpperCase() : 'GET';
	v = arg.length > 2 ? arg[2] : '';
	d = arg.length > 3 ? arg[3] : null;
	try {
		if(m == "GET"){
			x.open(m, u+"?"+v, true);
			v = '';
		}else{
			x.open(m, u, true);
			x.setRequestHeader("Method", "POST "+u+" HTTP/1.1");
			x.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		}
		x.onreadystatechange=function(){
			if(x.readyState==4 && !c) {
				c=true;
				if(typeof(d) == 'function') d(x);
			}
		};
		x.send(v);
	}catch(ex) {return false;}
	return true;
}

var text = $.create();

$.ext(text.prototype, {
	_parts:null,
	_value:null,
	_len:null,
	initialize:function(txt){
		this._parts = (typeof(txt) !== 'undefined'&& txt !== null && txt !== '') ? [txt.print()] : [];
		this._value = {};
		this._len = 0;
	},
	append:function(txt){
		this._parts[this._parts.length] = txt;
	},
	clear:function(){
		this._parts = [];
		this._value = {};
		this._len = 0;
	},
	empty:function(){
		if(this._parts.length === 0) return true;
		return this.toString() === '';
	},
	print:function(sep){
		sep = sep || '';
		var parts = this._parts;
		if(this._len !== parts.length){
			this._value = {};
			this._len = parts.length;
		}
		var val = this._value;
		if(typeof(val[sep]) === 'undefined'){
			if(sep !== ''){
				for(var i = 0; i < parts.length; ){
					if((typeof(parts[i]) === 'undefined') || (parts[i] === '') || (parts[i] === null)) parts.splice(i, 1);
					else i++;
				}
			}
			val[sep] = this._parts.join(sep);
		}
		return val[sep];
	}
});
$._stringregex = new RegExp('["\b\f\n\r\t\\\\\x00-\x1F]', 'i');
$._serialize_withtext = function(object, txt, sort) {
    var i;
    switch (typeof object) {
        case 'object':
            if (object) {
                if (Array._instanceof(object)) {
                    txt.append('[');
                    for (i = 0; i < object.length; ++i) {
                        if (i > 0) txt.append(',');
                        $._serialize_withtext(object[i], txt);
                    }
                    txt.append(']');
                } else {
                    if (Date._instanceof(object)) {
                        txt.append('"\\/Date(');
                        txt.append(object.getTime());
                        txt.append(')\\/"');
                        break;
                    }
                    var properties = [];
                    var propertyCount = 0;
                    for (var name in object) {
                        if (name.starts('$')) continue;
                        properties[propertyCount++] = name;
                    }
                    if (sort) properties.sort();
                    txt.append('{');
                    var needComma = false;
                    for (i = 0; i < propertyCount; i++) {
                        var value = object[properties[i]];
                        if (typeof (value) !== 'undefined' && typeof (value) !== 'function') {
                            if (needComma) txt.append(',');
                            else needComma = true;
                            $._serialize_withtext(properties[i], txt, sort);
                            txt.append(':');
                            $._serialize_withtext(value, txt, sort);
                        }
                    }
                    txt.append('}');
                }
            } else txt.append('null');
            break;
        case 'number':
            if (isFinite(object)) txt.append(String(object));
            break;
        case 'string':
            txt.append('"');
            if ($._stringregex.test(object)) {
                var length = object.length;
                for (i = 0; i < length; ++i) {
                    var curChar = object.charAt(i);
                    if (curChar >= ' ') {
                        if (curChar === '\\' || curChar === '"') txt.append('\\');
                        txt.append(curChar);
                    } else {
                        switch (curChar) {
                            case '\b':
                                txt.append('\\b');
                                break;
                            case '\f':
                                txt.append('\\f');
                                break;
                            case '\n':
                                txt.append('\\n');
                                break;
                            case '\r':
                                txt.append('\\r');
                                break;
                            case '\t':
                                txt.append('\\t');
                                break;
                            default:
                                txt.append('\\u00');
                                if (curChar.charCodeAt() < 16) txt.append('0');
                                txt.append(curChar.charCodeAt().toString(16));
                        }
                    }
                }
            } else txt.append(object);
            txt.append('"');
            break;
        case 'boolean':
            txt.append(object.toString());
            break;
        default:
            txt.append('null');
            break;
    }
}

$.serialize=function(object){
	var txt = new text();
	$._serialize_withtext(object, txt, false);
	return txt.print();
}

$.deserialize=function(data){
	try{
		var exp = data.replace(new RegExp('(^|[^\\\\])\\"\\\\/Date\\((-?[0-9]+)\\)\\\\/\\"','g'),"$1new Date($2)");
		return eval('('+exp+')');
	}catch(e){}
}

var _ua = navigator.userAgent.toLowerCase();

$.browser ={
version:(_ua.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/)||[])[1],
safari:/webkit/.test(_ua),
opera:/opera/.test(_ua),
msie:/msie/.test(_ua) && !/opera/.test(_ua),
mozilla: /mozilla/.test(_ua) && !/(compatible|webkit)/.test(_ua)
}
