Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
}

var $A = Array.from = function(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0; i < iterable.length; i++)
      results.push(iterable[i]);
    return results;
  }
}

//Ajax Object
/////////////

function Ajax() {
	this.avoidCache = true // true|false
	this.async = true // true|false
	this.contentType = "application/x-www-form-urlencoded; charset=utf-8"
	this.method = "get" // post|get
	this.responseType = "html" // html|xml|js|none
	this.response = false
	this.divLoadingImg = "";
	this.url = ""
	this.xmlhttp = 0
	this.pairName = new Array()
	this.pairValue = new Array()

	if (window.XMLHttpRequest) this.xmlhttp = new window.XMLHttpRequest
	else if (window.ActiveXObject) this.xmlhttp = new window.ActiveXObject("Microsoft.XMLHTTP")

	this.AddPair = function(varName, varValue) {
		this.pairName[this.pairName.length] = varName
		this.pairValue[this.pairValue.length] = varValue
	}
	
	this.GetPair = function(varName){
		for(i=0;i<this.pairName.length;i++){
				if(this.pairName[i] == varName){
				
				return this.pairValue[i]
			
			}
		
		}
	
		
	}
	
	this.Sufix = function(urlQuery) {
		if(this.avoidCache) {
			if(urlQuery.indexOf("?") >= 0) {
				return "&"+Math.random()
			} else {
				return "?"+Math.random()
			}
		} else {
			return ""
		}
	}

	this.Send = function() {
		var pairs = new Array()
		for (var i = 0; i < this.pairName.length; i++) {
				pairs.push(this.pairName[i] + "=" + encodeURIComponent(this.pairValue[i]))
		} query = pairs.join("&");
		
		if(this.method=="get" && query) urlQuery = this.url+"?"+query; else urlQuery = this.url
		this.xmlhttp.open(this.method,urlQuery+this.Sufix(urlQuery),this.async)
		
		
		this.xmlhttp.setRequestHeader("Content-Type", this.contentType)
		
		switch(this.responseType) {
			case "xml":
				this.xmlhttp.onreadystatechange = this.handleXML.bind(this)
				break;
			case "html":
				this.xmlhttp.onreadystatechange = this.handleHTML.bind(this)
				break;
			case "js":
				this.xmlhttp.onreadystatechange = this.handleJS.bind(this)
				break;
		}
		this.xmlhttp.send(query||null)
		return true
	}

	this.setContentType = function(p) {
		this.contentType = p;
	}
	
	this.handleXML = function() {
		if(this.xmlhttp.readyState == 1) {
			document.getElementById(this.divLoadingImg).innerHTML = '<div style="background:#FF0000"><font face="verdana" color="#ffffff" size="1">Carregando...</font></div>';
		}
		if(this.xmlhttp.readyState == 4) {
			document.getElementById(this.divLoadingImg).innerHTML = '';
			if(window.ActiveXObject) {
				this.response=new ActiveXObject("Microsoft.XMLDOM")
				this.response.async="false"
				this.response.loadXML(this.xmlhttp.responseText)
				
			} else {
			 
				this.response = this.xmlhttp.responseXML
			}
			this.handler()
		}
	}
	
	this.handleHTML = function() {
		if(this.xmlhttp.readyState == 1) {
			document.getElementById(this.divLoadingImg).innerHTML = '<div style="background:#FF0000"><font face="verdana" color="#ffffff" size="1">Carregando...</font></div>';
		}
		if(this.xmlhttp.readyState == 4) {
			document.getElementById(this.divLoadingImg).innerHTML = '';
			this.response = this.xmlhttp.responseText
			this.handler()
		}
	}
	
	this.handleJS = function() {
		if(this.xmlhttp.readyState == 1) {
			document.getElementById(this.divLoadingImg).innerHTML = '<div style="background:#FF0000"><font face="verdana" color="#ffffff" size="1">Carregando...</font></div>';
		}
		if(this.xmlhttp.readyState == 4)	 {
			document.getElementById(this.divLoadingImg).innerHTML = '';
			eval((this.xmlhttp.responseText))
		}
	}
	
	this.handler = function() {
		//alert("Ajax Response:\n"+this.response)
		return this.response;
	}
}