/**
 * Type functions
 */


function isSet( variable ) {

	return( typeof( variable ) != 'undefined' );

}

function isObject( variable ) {

	return( ( typeof( variable ) == 'object' ) && ( variable !== null ) );

}

function isString( variable ) {

	return( typeof( variable ) == 'string' );

}

function isFunction( variable ) {

	return( typeof( variable ) == 'function' );

}

function emptyFunction() {
}


/**
 * Extensions
 */


String.prototype.stripTags = function() {

	return( this.replace( /<\/?[^\>]+\>/gi, '' ) );

}


String.prototype.escapeHTML = function() {

    var oDiv 	= document.createElement( 'div' );
    var oText 	= document.createTextNode( this );

	oDiv.appendChild( oText );

    return( oDiv.innerHTML );

}

String.prototype.unescapeHTML = function() {

    var oDiv = document.createElement( 'div' );

	oDiv.innerHTML = this.stripTags();

    return( oDiv.childNodes[0].nodeValue );

}

isSet( Array.prototype.push ) || (

	Array.prototype.push = function() {

		var length_curr = this.length;

		for( var idx = 0; idx < arguments.length; idx++ ) {
			this[length_curr++] = arguments[idx];
		}

		return( this.length );

	}

);


isSet( Function.prototype.apply ) || (

	Function.prototype.apply = function( object, params ) {

		var params_strings = [];

		object || ( object = window );
		params || ( params = [] );

		for( var idx = 0; idx < params.length; idx++ ) {
		  params_strings[idx] = 'params[' + idx + ']';
		}

		object.__apply__ = this;

		var ret_val = eval( 'object.__apply__( ' + params_strings.join( ', ' ) + ' )' );

		object.__apply__ = null;

		return( ret_val );

	}

);


Function.prototype.delegate = function( object ) {

	var oFunction = this;

	var ret_val = function() {
		oFunction.apply( object, arguments );
	}

	return( ret_val );

}


function $( element ) {

	return( isString( element ) ? document.getElementById( element ) : ( isObject( element ) ? element : null ) );

}


/**
 * Ajax
 */

var Ajax = {

	getTransport : function() {

		var ret_val = null;

		try {
			ret_val = new ActiveXObject( 'Msxml2.XMLHTTP' );
		} catch( e ) {
			try {
				ret_val = new ActiveXObject( 'Microsoft.XMLHTTP' );
			} catch( e ) {
				try {
					ret_val = new XMLHttpRequest();
				} catch( e ) {
					// None available
				}
			}
		}

		return( ret_val );

	}

};



Ajax.JSONRequest = function( url, onComplete ) {

	this._oTransport = Ajax.getTransport();

	this._onComplete = isFunction( onComplete ) ? onComplete : null;

	try {

		this._oTransport.open( 'get', url, true );
		this._oTransport.onreadystatechange = this._onReadyStateChange.delegate( this );
		this._oTransport.send( null );

	} catch( e ) {

	}

}



Ajax.JSONRequest.prototype._onReadyStateChange = function() {

	if( this._oTransport.readyState == 4 ) {

		var oResponse = null;

		try {
			oResponse = this._oTransport.responseText;
		} catch( e ) {
		}

		isFunction( this._onComplete ) && this._onComplete( oResponse );

		this._oTransport.onreadystatechange = emptyFunction;

	}

}