function ajas(crumbMan)
{
var xmlHttp = XmlHttpObj();
var upd = null;
var defaultdiv = "contentDiv";
var url = null;
var anim;
var loading = $('loading');
var loadingtxt = 'Cargando';
var busy = false;
var dload = true;
var forget;
var mode = 'GET';
var postData = "";
var onComplete = "";
this.version = "1.2b5";
this.crumbs = $empty;
this.usecrumbs = false;
if($type(crumbMan) != false)
{
	//console.log('using crumbs');
	this.usecrumbs = true;
	this.crumbs = crumbMan;
}

/**
Changelog:
	1.2b2 - Corrected a bug regarding ajasform and multiple forms
	1.2b3 - Removed the d=d parameter for POST requests
	1.2b4 - Added breadcrumb support
	1.2b5 - Changed history support, removed target div
	1.2b6 - Fixed div update bug, added abort method
**/


function XmlHttpObj ()
{
	var request_;
			var browser = navigator.appName;
			if(browser == "Microsoft Internet Explorer"){
			 request_ = new ActiveXObject("Microsoft.XMLHTTP");
			}else{
			 request_ = new XMLHttpRequest();
			}
			return request_;
}

this.ask = function(str)
{
	this.request(str, 
		{
			history: false,
			update: this.defaultdiv
		}
	);
}

this.setDefault = function(ndefault)
{
	this.defaultdiv = ndefault;
}

this.rcall = function(str,wr)
{
	this.request(str, 
		{
			history: false,
			update: wr
		}
	);
}


this.rask = function(str,wr,uid)
{
	this.request(str, 
		{
			history: true,
			uid: uid,
			update: wr
		}
	);
}

evalScripts= function(element)
{
	if(element.childNodes.length > 0)
	{
		element.getChildren().each(function(el){
			if(el.getTag() == 'script')
			{
				try {
				eval(el.innerHTML);	
				}
				catch(err)
				{
				
				}
				//alert(el.innerHTML);		
			}
			else
			{
				evalScripts(el);
			}
		});
	}
}

this.AsyncUpdateEvent = function() 
{
	with(this)
	{
	switch(xmlHttp.readyState) {
			case 4:
				
				//Parse response
				var response = xmlHttp.responseText;
		      	var temp2_array = response.split("||");
		      	
				$(upd).innerHTML= temp2_array[0] ;
			
				eval(temp2_array[1]);
				
				//Experimental Script code
				
				if($type(onComplete) == 'function')
				{	
					onComplete();
				}	
				
				//Animate response
				if(anim == true)
				{	
					$(upd).effect('opacity',{wait: true,duration: 500,transition: Fx.Transitions.linear, onComplete: function() { 
						evalScripts($(upd));
						}
					}).start(0,1);
				}
				else
				{
					evalScripts($(upd));
				}
				if(dload == true)
				{
				loading.effect('opacity',{
					wait:true, duration: 200, 
					transition: Fx.Transitions.linear
				}).start(0.8,0);
				loading.setStyle('display','none');
				}
				busy = false;
				break;
	}
}
} 
this.abort = function(){
	if($type(xmlHttp) != false)
	{
		xmlHttp.abort();
	}
}

this.request = function(urln,options)
{
		if(options == null)
		{
			options = {};
		}
		if(busy == true)
		{
			
			return;
		}
		busy = true;
		if($type(options['mode']) != 'string')
		{
			mode = "GET";
		}
		else
		{
			mode = options['mode'].toUpperCase();
		}
		if(mode == "POST")
		{
			postData = options['data'];
		}
		var history = false;
		if($type(options['forget']) != 'boolean')
		{
			forget = false;
		}
		else
		{
			forget = options['forget'];
		}
		if($type(options['loading']) != 'boolean')
		{
			dload = true;
		}
		else
		{
			dload = options['loading'];
		}
		if($type(options['loadingText']) == 'string')
		{
			loading.innerHTML = options['loadingText'];
		}
		else
		{
			loading.innerHTML = loadingtxt;
		}
		if($type(options['history'])=="boolean")
		{
			history = options['history'];
		}
		if($type(options['animation'])=="boolean")
		{
			anim = options['animation'];
		}
		else
		{
			anim = true;
		}
		
		if($type(options['onComplete']) == 'function')
		{
			onComplete = options['onComplete'];
		}
		else
		{
			onComplete = "none";
		}
		if($type(options['update']) != false )
		{
			if (options['update'] == 'def' || options['update'] == 'default')
			{
				upd = defaultdiv;
			}
			else
			{
				upd = options['update'];
			}
		}
		else
		{
			upd = defaultdiv;
		}
		url = urln;
		
		if($type(options['addCrumb']) != false && this.usecrumbs)
		{
			this.crumbs.add(options['addCrumb'], url);
		}
		if($type(options['replaceCrumb']) != false && this.usecrumbs)
		{
			this.crumbs.replace(options['replaceCrumb'], url, options['crumbsOptions']);		
		}
		
		if(history == true )
		{
			var rawMaterial = url;
			dhtmlHistory.add(options['uid'] ,rawMaterial);
		}
		if(dload == true)
		{
		loading.setStyle('display','block');
		loading.setStyle('top',(window.getHeight()/2)-15);
		loading.setStyle('left', (window.getWidth()/2)-40);
		loading.effect('opacity',{
					wait:true, duration: 200, 
					transition: Fx.Transitions.linear
			}).start(0,0.8);
			
		}
		if(anim == true)
		{
			$(upd).effect('opacity',{
					wait:true, duration: 500, 
					transition: Fx.Transitions.linear,
					onComplete: this.doRequest.bind(this)
			}).start(1,0);
		}
		else
		{
			this.doRequest();
		}
}

this.doRequest = function()
{		
	if(forget == false)
	{
		//alert('Play it again Sam, just like the old times');
		document.ajasLastOpen = url;
	}
	if(mode == "GET")
	{
		xmlHttp.open("GET", url , true);
		xmlHttp.onreadystatechange = this.AsyncUpdateEvent;
		xmlHttp.send(null);
	}
	if(mode == "POST")
	{
		xmlHttp.open("POST", url , true);
		xmlHttp.onreadystatechange = this.AsyncUpdateEvent;
		xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlHttp.send(postData);
	}
}

this.sendForm = function (form, surl, options) {

	if($type(form) == 'string')
	{
		form = $(form);
	}
	var fmode = "GET";
	if($type(options['mode']) == "string")
	{
		fmode = options['mode'];
	}
	
	if(fmode == "POST")
	{
		var uri = "";
	}
	else
	{
		var uri = surl+"?";
	}
	/*
	form.getFormElements().each( function(el)
		{
			var name = el.name;
			if(el.type != "checkbox" && el.type != "radio")
			{
				var val = el.value;
				if(name.length != 0)
				{
					uri = uri + "&" + name + "=" + val;
				}
			}
			else
			{
				if(el.getValue() != false)
				{
					uri = uri + "&" + name + "=" + el.getValue();
				} 
			}
		});*/
	
	uri+= form.toQueryString();
	//alert(uri);
	if(fmode == "POST")
	{
		options['data'] = uri;
		this.request(surl, options);
	}
	else
	{
		this.request(uri, options);
	}
}

this.ajasForm = function(form, options)
{
	if($type(form) == "string")
	{
		form = $(form);
	}
	if ($type(options) == false)
	{
		options = {};
	}
	furl = form.getAttribute('action');
	if($type(furl) == false)
	{
		return;
	}

	
	form.addEvent('submit',function(e){
		e = new Event(e);
		e.stop();
		fmode = this.getAttribute('method').toUpperCase();
		if($type(fmode) == false)
		{
			fmode = "GET";
		}
		options['mode'] = fmode;		
		document.ajaxer.sendForm(this, this.get("action"), options);
	});
}

}