/***********************************************************\
|                                                           |
|          - ARENA v1 Content Management System -           |
|             Copyright (c) 2004-2008 Blest AS              |
|                  E-mail: blest@blest.no                   |
|                 Tel: (+0047) 99 37 77 88                  |
|                 Web: http://www.blest.no                  |
|                                                           |
\***********************************************************/

// A simple ajax wrapper that works in all xmlhttprequest compatible browsers
// Blest Arena JAX =)

// Helper functions
var __bajaxoriginaltitle = document.title;
var __bajaxprocesses = new Array ( );
var __bajaxkeys = 0;
var __bajaxprocessanimframe = 0;
var __bajaxprocessanimframes = Array ( '|', '/', '-', '\\', '|', '/', '-', '\\' );
var __bajaxprocessescount = 0;
function __removeBajaxProcess ( key ) // <- removes the trackable process
{
	var outar = new Array ( );
	for ( var a = 0; a < __bajaxprocesses.length; a++ )
	{
		if ( __bajaxprocesses[ a ].Key != key )
		{
			outar[ outar.length ] = __bajaxprocesses[ a ];
		}
		else
		{
			_bajaxprocesses[ a ] = 0;
		}
	}
	__bajaxprocesses = outar;
	__bajaxprocessescount--;
	if ( !__bajaxprocesses.length )
		document.title = __bajaxoriginaltitle;
}
function __addBajaxProcess ( obj ) // <- adds a trackable process
{
	var process = new Object ( );
	process.Obj = obj;
	process.Key = __bajaxkeys;
	__bajaxkeys = ( __bajaxkeys + 1 ) % 30000;
	__bajaxprocesses[ __bajaxprocesses ] = process;
	__bajaxprocessescount++;
	return process.Key;
}
function __bajaxprocessbrain ( )
{
	if ( __bajaxprocessescount > 0 )
	{
		document.title = 'Laster inn... ' + __bajaxprocessanimframes[ __bajaxprocessanimframe ];
		__bajaxprocessanimframe = ( __bajaxprocessanimframe + 1 ) % 8;
		setTimeout ( '__bajaxprocessbrain ( )', 100 );
	}
	else setTimeout ( '__bajaxprocessbrain ( )', 250 );
}
__bajaxprocessbrain ( );

// Bajax object
bajax = function ( parentObject )
{		
	var parentObject;
	var xhrObject;
	var Url;
	var Method;
	var User;
	var Pass;
	var responseXML;
	var postVars;
	
	this.postVars = new Array ();
	
	if ( parentObject )
		this.parentObject = parentObject;
	this.Url = "";
	this.Method = "";
	this.User = "";
	this.Pass = "";		
	
	
	if ( window.XMLHttpRequest ) this.xhrObject = new XMLHttpRequest();
	else if ( window.ActiveXObject )
	{	
		try { this.xhrObject = new ActiveXObject( "Msxml2.XMLHTTP" ); }
		catch ( e )
		{
			try { this.xhrObject = new ActiceXObject ( "Microsoft.XMLHTTP" ); }
			catch ( e ) { this.xhrObject = false; }
		}
	}
								
	// Default way -- looks more like javascript
	var object = this;
	this.xhrObject.onreadystatechange = function ( )
	{
		if ( object.getReadyState ( ) == 4 )
		{
			try
			{
				object.responseXML = object.xhrObject.responseXML;
				object.onload ( );
				if ( typeof ( '__removeBajaxProcess' ) != 'undefined' ) 				// clean up finished processes
					__removeBajaxProcess ( this.ProcessKey );
			}
			catch ( e ){ alert ( e ); }
		}
	}
}

bajax.prototype.addVar = function ( varName, varValue )
{
	this.postVars[ varName ] = varValue;
}

bajax.prototype.addVarStatic = function ( varName, varValue )
{
	this.postVars[ varName + '' ] = varValue + '';
}

bajax.prototype.addVarsFromForm = function ( element ) 
{
	if ( typeof ( element ) != "object" )
		element = document.getElementById ( element );
	
	if ( !element )
	{
		alert ( "Fy deg!" );
		return false;
	}
	
	
	var values = element.elements;
	for ( var i = 0; i < values.length; i++ )
	{
		if ( values[i].name && values[i].value )
		{
			this.addVar ( values[i].name, values[i].value );
		}
	}
}

bajax.prototype.setUsername = function ( user )
{
	if ( user )
		this.User = user;
}

bajax.prototype.setPassword = function ( pass )
{
	if ( pass )
		this.Pass = pass;
}

bajax.prototype.setMethod = function ( method_ )
{
	if ( method_ )
		this.Method = method_;
}

bajax.prototype.setUrl = function ( url )
{
	if ( url )
		this.Url = url;
}

bajax.prototype.abort = function ( )
{
	return this.xhrObject.abort ( );
}

bajax.prototype.getResponseHeader = function ( label_ )
{
	return this.xhrObject.getResponseHeader ( label_ );
}

// Mode is if the connection should be asyncronous
bajax.prototype.open = function ( mode_ )
{				
	var Mode = false;
	
	if ( mode_ )
	{
		if ( mode_ == "asyncronous" || mode_ == "async" || mode_ == true )
			Mode = true;
	}		
	
	if ( this.Method && this.Url )
	{
		if ( this.User || this.Pass )
			return this.xhrObject.open ( this.Method, this.Url, Mode, this.User, this.Pass );
		else
			return this.xhrObject.open (this.Method, this.Url, Mode );
	}		
	return false;
}

bajax.prototype.openUrl = function ( url_, method_, mode_ )
{		
	var Mode = false;
	
	if ( mode_ )
	{
		if ( mode_ == "asyncronous" || mode_ == "async" || mode_ == true )
			Mode = true;
	}		
				
	if ( url_ )
	{
		// Store url
		this.Url = url_;
		
		if ( method_ )
		{				
			if ( this.User || this.Pass )
				return this.xhrObject.open ( method_, url_, Mode, this.User, this.Pass );
			else			
				return this.xhrObject.open ( method_, url_, Mode );
		}
		else
		{
			if ( this.User || this.Pass )
				return this.xhrObject.open ( this.Method, url_, Mode, this.User, this.Pass );
			else
				return this.xhrObject.open ( this.Method, url_, Mode );
		}
	}
	return false;
}		

bajax.prototype.send = function ( data_ )
{			
	this.ProcessKey = __addBajaxProcess ( this );
	var contentType = "application/x-www-form-urlencoded; charset=utf-8";
	var query;
	
	var arcount = 0; for ( var a in this.postVars ) arcount++;
	
	if ( arcount )
	{
		// we are posting a form
		var pairs = new Array ();
		for ( varName in this.postVars )
		{
			pairs.push ( varName + "=" + encodeURIComponent ( this.postVars [ varName ] ) );
		}
		this.xhrObject.setRequestHeader( "Content-Type", contentType );
		
		query = pairs.join ( "&" );
		
		if ( data_ )
		{
			data_ += "&" + query;
		}
		else
			data_ = query;
	}
	
	if ( data_ )
		return this.xhrObject.send ( data_ );
	return this.xhrObject.send ( this.getNull ( ) );
}

bajax.prototype.getNull = function ( )
{
	try
	{
		if ( null )
		{
			return null;
		}
	}
	catch ( e )
	{
		if ( NULL )
		{
			return NULL;
		}
	}
}

bajax.prototype.setRequestHeader = function ( label_, value_ )
{
	return bajax.prototype.setRequestHeader ( label_, value_ );
}

// Set function to run when we have a response (optional)
bajax.prototype.setResponseFunction = function ( function_ )
{	
	if ( typeof ( function_ ) != "undefined" )
		this.xhrObject.onreadystatechange = function_;				
}		
		
// 
bajax.prototype.getReadyState = function ( )
{
	return this.xhrObject.readyState;
}

//
bajax.prototype.getResponseText = function ( )
{								
	return this.xhrObject.responseText;		
}

// Check if a keyword is in the text or return keyword
bajax.prototype.getResponseKeyword = function ( varKey )
{
	if ( typeof ( varKey ) == "undefined" ) varKey = false;
	
	var response = this.getResponseText ( );
	
	if ( 
		( response.substr ( 0, varKey.length ) == varKey ||
		( response.indexOf ( varKey ) > 0 ) ) && varKey
	)
	{
		return true;
	}
	else
	{
		var answer = response;
		answer = str_replace ( " ", "", answer );
		answer = str_replace ( "\n", "", answer );
		answer = str_replace ( "\r", "", answer );
		answer = str_replace ( "\t", "", answer );
		if ( answer.length > 0 )
			return answer;
	}
	return false;
}

//
bajax.prototype.getResponseXML = function ( )
{	
	return this.xhrObject.responseXML;
}

bajax.prototype.getStatus = function ( )
{
	return this.xhrObject.status;
}

bajax.prototype.getStatusText = function ( )
{
	return this.xhrObject.statusText;
}


// Replace all occurrances of a string
function str_replace ( varSource, varDestination, varValue )
{		
	varValue += "";
	varValue = varValue.split ( varSource );
	varValue = varValue.join ( varDestination );
	return varValue;		
}

// To be used to retrieve values from dom by tagname
function getXMLValue ( varName, obj )
{		
	var Value;
	if ( Value = obj.getElementsByTagName ( varName ).item ( 0 ) )
	{
		Value = Value.firstChild.data;
		return Value;		
	}
	return false;
}


// bajax the lazy way - replace innerHTML on an element via ajax
HTMLElement.prototype.getBajax = function ( url, data )
{
	var req = new bajax();
	req.element = this;
	req.onload = function ()
	{
		this.element.innerHTML = this.getResponseText ();
	}
	ajax.openUrl( url, "get", true );
	ajax.send( data );
}

/**
 * url is target url
 * varfunc is an optional function to run when the target is loaded
 * divid (optional) is the resulting div to load result into 
**/
function jload ( url, varfunc, divid )
{
	if ( !divid ) divid = false;
	if ( !varfunc ) varfunc = false;
	var j = new bajax ( );
	j.object = document.getElementById ( divid );
	j.openUrl ( url, 'get', true );
	j.func = varfunc;
	j.onload = function ( )
	{
		if ( this.object ) this.object.innerHTML = this.getResponseText ( );
		if ( this.func ) this.func ( );
	}
	j.send ( );
}