/**
 * Classe para Ajax melhorada
 * @author Hugo Ferreira da Silva
 * @link http://www.hufersil.com.br/ HUFERSIL.WEBDESIGN
 */

function Ajax () {
	this.obj = false;
	this.listeners = [];
	this.parameters =[];
	this.method = 'POST';
	this.id = new Date().getTime();
}

Ajax.prototype = {
	_ : function(o)
	{
		var list = [];
		for(var i=0; i<o.length; i++)
		{
			list.push(o[i]);
		}
		return list;
	},
	
	createObj : function () {
		var xmlhttp = false;
		try{
			xmlhttp = new XMLHttpRequest();
		}catch(ee){
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(E){
					xmlhttp = false;
				}
			}
		}
		return xmlhttp;
	},

	addParameter : function(name, value) {
		this.parameters.push( escape(name)+'='+escape(value) );
	},

	removeParameter : function (n) {
		n=escape(n);
		var l=[];
		for(var i=0;i<this.parameters.length; i++) {
			var p=this.parameters[i].split('=')[0];
			if(p!=n) {
				l.push(this.parameters[i]);
			}
		}
		this.parameters=l;
	},

	send : function(url,data) {
		var params = this.parameters.join('&');
		if(this.method.toLowerCase() == 'get') {
			url = url + '?' + params;
			data = null;
		} else {
			if(data == undefined) {
				data = params;
			}
		}
	
		this.obj = this.createObj();
		this.obj.open(this.method, url, true);
		if(this.method.toLowerCase()=='post') {
			this.obj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		}
		this.obj.onreadystatechange = this._delegate(this, this.callback);
		this.obj.send( data );
	},
	
	_delegate : function(escope, fnc)
	{
		var args = this._(arguments).slice(2,arguments.length);
		return function(e)
		{
			if(window.event)
			{
				e=window.event;
			}
			args.push(e);
			fnc.apply(escope, args);
		}
	},
	
	callback : function()
	{
		if(this.obj.readyState == 2)
		{
			this.loading(this.obj, this);
			this._dispatch('loading', this.obj, this);
		}
		if(this.obj.readyState == 4)
		{
			this.response(this.obj, this);
			this._dispatch('load', this.obj, this);
		}
	},
	
	response : function(obj, ref)
	{
	},
	
	loading : function(obj, ref)
	{
	},
	
	addListener : function(type, escope, action)
	{
		var item = {
			type   : type,
			escope : escope,
			action : action
		};
		
		this.listeners.push(item);
	},
	
	removeListener : function(type, escope, action)
	{
		var item = {
			type   : type,
			escope : escope,
			action : action
		};
		
		var newlist = [];
		for(var i=0; i<this.listeners.length; i++)
		{
			if(this.listeners[i] != item)
			{
				newlist.push(this.listeners[i]);
			}
		}
		
		this.listeners = newlist;
	},
	
	_dispatch : function (ev)
	{
		var args = this._(arguments).slice(1, arguments.length);
		for(var i=0; i<this.listeners.length; i++) {
			if(this.listeners[i].type == ev) {
				this.listeners[i].action.apply( this.listeners[i].escope, args );
			}
		}
	},
	
	resetParameters : function()
	{
		this.parameters=[];
	},
	
	resetListeners : function()
	{
		this.listeners = [];
	},
	
	addForm : function ( theForm ) {
		var list=theForm.elements;
		for(var i=0; i<list.length; i++) {
			if( list[i].type )
			{
				switch(list[i].type.toLowerCase()) {
					case "select-one":
					case "hidden":
					case "text":
					case "password":
					case "textarea":
						this.addParameter(list[i].name, list[i].value);
					break;
					case "radio":
					case "checkbox":
						if(list[i].checked == true) {
							this.addParameter(list[i].name, list[i].value);
						}
					break;
				}
			}
		}
	},
	
	fillCombo : function (combo, url, key, value, selected, keep)
	{
		if( keep == undefined )
		{
			keep = 1;
		}
		
		var old_string;
		
		while( combo.length > keep )
		{
			combo.remove( combo.length - 1 );
		}
		
		this.response = function( o )
		{
			combo.options[0].text = old_string;
			var list = eval( o.responseText );
			for(var i=0; i<list.length; i++)
			{
				var o = document.createElement('OPTION');
				combo.appendChild( o );
				o.text = list[i][value];
				o.value = list[i][key];
				
				if( selected == list[i][key] )
				{
					o.selected = true;
				}
			}
		}
		if( combo.options[0] )
		{
			old_string = combo.options[0].text;
			combo.options[0].text = '-- Carregando --';
		}
		this.send( url );
		
	}
}