function e(id) { return document.getElementById(id) }
function getdiv(id) { return document.getElementById(id) }


/**
	return the builtin object
 */
function nativeRequest()
{
	try
	{
		try
		{
			return new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch( e )
		{
			return new ActiveXObject("Msxml2.XMLHTTP")
		}
	}
	catch(e) 
	{
		return new XMLHttpRequest()
	}
	/*
	else
	{
		alert("Either you don't have Ajax or my code sucks")
		throw "Error .. can't create the ajax request"
	}
	*/
}

function Request()
{
	//breq is the built in request
	this.breq = nativeRequest()
	this.async = true //asyncrinous .. should be a constant or something .. please don't try to change this!
	this.url = ''
	this.params = new Array()
	this.json = function()
	{
		return eval("(" + this.response + ")")
	}
	this.check = function()
	{
		var obj = this
		var request = obj.breq
		if( request.readyState == 4 )		
		{	
			obj.response = this.breq.responseText		
            obj.onReady()
			if( request.status == 200 )
			{			
				obj.onDone()
			}
			else			
			{
				obj.onFail()
			}	
            obj.cleanUp()
		}
		else		
		{
			obj.onProgress()
		}
	}
	
	this.onDone= function(){}
	this.onFail= function(){}
	this.onProgress= function(){}
	this.onReady= function(){}
    this.cleanUp= function(){}
	
	this.data = ''
	
	this.sendPost = function()
	{			
		this.prepare()
		this.breq.open('POST', this.url, this.async)
		this.breq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8')
		this.breq.send(this.paramstr)		
	}
	
    this.postSend = this.sendPost // hehe . sometimes I forget which one was it!!
    
	this.send = function()
	{
		this.prepare()
		if( this.params )
			var p_url = this.url + '?' + this.paramstr
		else
			var p_url = this.url
		//NOTE use p_url, not this.url	
		this.breq.open( 'GET', p_url , this.async )		
		this.breq.send(null)		
	}	
	
	this.prepare = function()
	{
		var obj = this
		this.breq.onreadystatechange = function() { obj.check() }
		this.paramstr = parametrize(this.params)	
	}
}

function get_uniq_id()
{
	var rand = Math.floor( Math.random() * 10000 )
	var time = (new Date().getTime())
	return "id" + rand + "T" + time
}

function parametrize( array )
{
	var params = ''
	for( key in array )
	{
		params += key + '=' + encodeURI(array[key]) + '&'
	}	
	params += 'randomcode=' + get_uniq_id()
	
	return params
}

function form_params( form )
{
	var params = new Array()
	var length = form.elements.length
	for( var i = 0; i < length; i++ )
	{
		element = form.elements[i]
		
		if(element.tagName == 'TEXTAREA' )
		{
			params[element.name] = element.value
		}
		else if( element.tagName == 'INPUT' )
		{
			if( element.type == 'text' || element.type == 'hidden' || element.type == 'password')
			{
				params[element.name] = element.value
			}
			else if( element.type == 'radio' && element.checked )
			{
				if( !element.value )
					params[element.name] = "on"
				else
					params[element.name] = element.value

			}
			else if( element.type == 'checkbox' && element.checked )
			{
				if( !element.value )
					params[element.name] = "on"
				else
					params[element.name] = element.value
			}
		}
	}
	return params;
}


function load_url_to_div( url, div_id )
{    
    var req = new Request()
    req.url = url
    req.onDone = function()
    {
        getdiv(div_id).innerHTML = req.response;
    }
    req.onFail = function()
    { 
        if( getdiv("errors") )
            getdiv("errors").innerHTML = req.response;
    }
    req.send();
}

function load_url_params_to_div( url, params, div_id, post )
{    
    var req = new Request()
    req.url = url
    req.params = params    
    req.onDone = function()
    {
        getdiv(div_id).innerHTML = req.response;
    }
    req.onFail = function()
    { 
        if( getdiv("errors") )
            getdiv("errors").innerHTML = req.response;
    }
    if(post) req.postSend()
    else req.send();    
}

function scroll_to(query, offset, time)
{
    var targetOffset = $(query).offset().top - offset;
    $('html,body').animate({scrollTop: targetOffset}, time);
}

var timeout_handles = []    
function set_time_out( id, code, time ) /// wrapper
{
    if( id in timeout_handles )
    {
        clearTimeout( timeout_handles[id] )
    }
    
    timeout_handles[id] = setTimeout( code, time )
}

loading_bar = '<img src="/media/misc/ajax-loader-bar.gif">'
wait_gif = '<img src="/media/misc/ajax-loader.gif">'
none_gif = '<img src="/media/misc/ajax-none.gif">'

