function	object2pd ( o ) {
	var t = '';
	var a = new Array ( );

	for ( var i in o ) {
		if ( typeof ( o [ i ] ) == 'function' ) continue;

		t = encodeURIComponent ( i ) + '=' + encodeURIComponent ( o [ i ] );
		
		a.push ( t );
	}
	
	return a . join ( '&' );
}

var jaRequest = function ( pCallback, url, auto_run, sender, method, post_params ) {
	this.hReq = null;
	this.hTimeout = null;
	this.pCallback = pCallback;
	this.url = url;
	this.auto_run = auto_run;
	this.sender = sender;
	this.method = method;
	this.post_params = post_params;

	this.initialize = function ( ) {
		this.hReq = null;

		if ( window.XMLHttpRequest ) {
			try {
				this.hReq = new XMLHttpRequest ( );
			} catch ( e ) { }
		} else if ( window.ActiveXObject ) {
			try {
				this.hReq = new ActiveXObject ( 'Msxml2.XMLHTTP' );
			} catch ( e ) {
				try {
					this.hReq = new ActiveXObject ( 'Microsoft.XMLHTTP' );
				} catch ( e ) { }
			}
		}
	},

	this.processOk = function ( ) {
		if ( this )	return pCallback ( this, eval ( this.hReq.getResponseHeader ( 'X-JSON' ) ) );
	},

	this.query = function ( url ) {
		if ( !this.hReq ) return false;

		var p = this;

		this.hReq.onreadystatechange = function ( ) {
			if ( p.hReq.readyState == 4 ) {
				clearTimeout ( p.hTimeout );

				if ( p.hReq.status == 200 ) {
					p.processOk ( );
				}
			}
		}

		this.hReq.open ( this.method, url, true );
		this.hReq.setRequestHeader ( 'If-Modified-Since', 'Mon, 12 Nov 1984 18:45:00 GMT' );
		if ( this.method == 'POST' ) {
			var params = false;
		
			if ( this.post_params != null && this.post_params != false ) {
				params = object2pd ( this.post_params );
			}

			if ( params ) {
				this.hReq.setRequestHeader ( "Content-type", "application/x-www-form-urlencoded" );
				//this.hReq.setRequestHeader ( "Content-length", params ? params.length : 0 );
				//this.hReq.setRequestHeader ( "Connection", "close" );
			}
			
			this.hReq.send ( params ? params : null );
		} else {
			this.hReq.send ( null );
		}
		this.hTimeout = setTimeout ( function ( ) { p.hReq.abort ( ); }, 10000 );
	}

	this.initialize ( );

	if ( this.auto_run ) this.query ( this.url );
}

function	loadJaContentPost ( container, url, params ) {
	new jaRequest ( function ( h, a ) { if ( container ) container.innerHTML = ( h.hReq.responseText ); }, url, true, null, 'POST', params );
}

function	loadJaContentPostOn ( container, url, params, f ) {
	new jaRequest ( function ( h, a ) { if ( container ) container.innerHTML = ( h.hReq.responseText ); if ( f ) f ( a ); }, url, true, null, 'POST', params );
}

function	evalAjaxContent ( url ) {
	new jaRequest ( function ( h, a ) { eval ( h.hReq.responseText ); }, url, true, null );
}

function	loadJaInnerPostOn ( container, url, params, f ) {
	new jaRequest ( function ( h, a ) { if ( container ) container.innerHTML = ( h.hReq.responseText ); if ( f ) f ( a ); }, url, true, null, 'POST', params );
}

function	loadJaSelectOption ( root_select, select, url, f, def ) {
	select.disabled = root_select.disabled = true;
	
	new jaRequest ( function ( h, a ) { var j; root_select.disabled = select.disabled = false; select.options.length = 0; for ( j = 0; j < a.length; j ++ ) { select.options [ j ] = new Option ( a [ j ] [ 'title' ], a [ j ] [ 'value' ] ); if ( def == a [ j ] [ 'value' ] ) select.options [ j ].selected = true; }; if ( f ) f ( root_select ); }, url, true, root_select, 'GET', null );
}

