/**
* Functions for JavaScript RPC.
*
* Assumes that 'browser.js' is already included.
*
* @package Api
* @subpackage JavaScript
*/

/** 
* Performs asynchronous POST-request to given URL.
*
* @param string url				Given URL.
* @param object data			Data to post. Eg. {'field1':'foo','field2':'bar'}.
* @param function handler	Handler function of responseText. If undefined, then responseText 
*													will go through eval().
*/
function rpcPost( url, data, handler )
{
	var req = null;
	if ( IS_IE ) {
		try {
			req = new ActiveXObject( "Msxml2.XMLHTTP" );
		} catch(e) {
			try {
				req = new ActiveXObject( "Microsoft.XMLHTTP" );
			} catch(e) {
				req = false;
			}
		}
	} else {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = false;
		}
	}
	if ( ! req ) return;
	
	var content = '';
	for( var i in data ) {
		
		// item is Array
		if ( typeof data[i] == 'object' ) {
			for( var j in data[i] ) {
				content += ( content.length ? '&' : '' ) + i + '=' + encodeURIComponent( data[i][j] );
			}
			
		// item is scalar
		} else {
			content += ( content.length ? '&' : '' ) + i + '=' + encodeURIComponent( data[i] );
		}
	}
	
	function _rpc_onreadystatechange()
	{
		if ( req.readyState == 4 ) {
			if ( req.status == 200 ) {
				if ( typeof handler == 'function' )
					handler( req.responseText, req );
				else
					eval( req.responseText );
			} else {
				alert( 'An error has occurred: ' + req.statusText );
			}
		}
	}
	
	req.onreadystatechange = _rpc_onreadystatechange;
	
	req.open( 'POST', url, true );
	req.setRequestHeader(
		'Content-Type',
		'application/x-www-form-urlencoded; charset=UTF-8'
	);
	req.send( content );
}

/** 
* Performs asynchronous GET-request to given URL.
*
* @param string url				Given URL.
* @param function handler	Handler function of responseText. If undefined, then responseText 
*													will go through eval().
*/
function rpcGet( url, handler )
{
  var req = null;
  if ( IS_IE ) {
		try {
			req = new ActiveXObject( "Msxml2.XMLHTTP" );
		} catch(e) {
			try {
				req = new ActiveXObject( "Microsoft.XMLHTTP" );
			} catch(e) {
				req = false;
			}
		}
	} else {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = false;
		}
	}
	if ( ! req ) return;
	
  function _rpc_onreadystatechange()
	{
		if ( req.readyState == 4 ) {
			if ( req.status == 200 ) {
				if ( typeof handler == 'function' )
					handler( req.responseText, req );
				else
					eval( req.responseText );
			} else {
				alert( 'An error has occurred: ' + req.statusText );
			}
		}
	}
	
  req.onreadystatechange = _rpc_onreadystatechange;
  
  req.open( 'GET', url, true );
  req.send( null );
}

/** 
* Performs synchronous GET-request to given URL and returns content of URL.
*
* @param string url				Given URL.
* @return string
*/
function rpcGetUrlContent( url )
{
  var req = null;
  if ( IS_IE ) {
		try {
			req = new ActiveXObject( "Msxml2.XMLHTTP" );
		} catch(e) {
			try {
				req = new ActiveXObject( "Microsoft.XMLHTTP" );
			} catch(e) {
				req = false;
			}
		}
	} else {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = false;
		}
	}
	if ( ! req ) return;

  // Synchronous!
  req.open( 'GET', url, false );
  req.send( null );
  
  return ( req.status == 200 ? req.responseText : '' );
}

