function getVal(elements) {
	var pairs = new Array();
	for (i = 0; i < elements.length; i++){
		pairs.push(elements[i].name + "=" + encodeURIComponent(elements[i].value));
	}
	return pairs.join("&");
}

function getElement() {
	
	this.scope = new Array();
	this.scope.push(document);
	
	this.by = function(data) {
		for (key in data) {
			if (key == 'id') {
				this.bufferId = new Array();
				
				for (i = 0; i < this.scope.length; i++) {
					this.bufferId.push(this.scope[i].getElementById(data.id));
				}
				this.scope = this.bufferId;
			
			} else if (key == 'tag') {
				this.bufferTag = new Array();
				
				for (i = 0; i < this.scope.length; i++) {
					if (data.tag.indexOf(',') != -1) {
						tags = data.tag.split(',');
						this.scope = this.scope[i].getElementsByTagName(tags[0]);
						this.by({tag : data.tag.substr(data.tag.indexOf(',') + 1).trim()});
					} else {
						elements = this.scope[i].getElementsByTagName(data.tag);
						for (j = 0; j < elements.length; j++){
							this.bufferTag.push(elements[j]);
						}
					}
				}
				this.scope = this.bufferTag;
			
			} else if (key = 'mtag') {
				this.bufferMTag = new Array();
				data.mtag = (data.mtag = 'form') ? 'input, textarea, select' : data.mtag;
				
				for (i = 0; i < this.scope.length; i++) {
					if (data.mtag.indexOf(',') != -1) {
						tags = data.mtag.split(',');
						for (j = 0; j < tags.length; j++) {
							elements = this.scope[i].getElementsByTagName(tags[j].trim());
							for (k = 0; k < elements.length; k++) {
								this.bufferMTag.push(elements[k]);
							}
						}
					}
				}
				this.scope = this.bufferMTag;
			}
		}
		return this;
	}
	
	this.get = function(key) {
		if (this.scope.length == 1) {
			return this.scope[0];
		} else if (!isNaN(key)) {
			return this.scope[key];
		} else {
			return this.scope;
		}
	}
	
	this.style = function(data) {
		for (key in data) {
			for (i = 0; i < this.scope.length; i++) {
				this.scope[i].style[key] = data[key];
			}
		}
		return this;
	}
	
	this.values = function(data) {
		this.bufferValues = new Array();
		
		for (i = 0; i < this.scope.length; i++) {
			if (this.scope[i].name) {
				this.bufferValues.push(this.scope[i].name + "=" + encodeURIComponent(this.scope[i].value));
			}
		}
		return this.bufferValues;
	}
}
function $(data) { return new getElement().by(data); }

function request() {
	var request = window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false;
		
	request.fn = function(fn) {
		if(typeof fn=='string') return new Function(fn);
		if(typeof fn=='function') return fn;
		if(typeof fn=='undefined') return false;
	}
		
	this.submit = function(data) {
		if (!data.loading) data.loading = "";
		if (!data.success) data.success = "";
		if (!data.error) data.error = "";
		if (!data.method) data.method = "GET";
		if (!data.params) data.params = "";
		
		request.fn(data.loading).call(this, request);
		
		if (data.method == "POST") {
			data.params = (data.params instanceof Array) ? data.params.join('&') : data.params;
			request.open("POST", data.url, true);
			request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			request.send(data.params);
		} else {
			if (data.url.indexOf('?') != -1) {
				data.url = data.url+'&'+data.params;
			}else{
				data.url = data.url+'?'+data.params;
			}
				
			request.open("GET", data.url, true);
			request.send(null);
		}

		request.onreadystatechange = function() {
			if (request.status == 200){
				if (request.readyState == 4) {
					request.fn(data.success).call(this, request);
				}
			} else {
				if (request.readyState == 4) {
					request.fn(data.error).call(this, request);
				}
			}
		}
	}
}
function ajax(data) { return new request().submit(data); }

String.prototype.trim = function() { return this.replace(/^\s\s*/, "").replace(/\s\s*$/, ""); }
