/* -*- C -*- */

/* --------------------------------------------------------------------
 * Shine: The Become Interactive Client Application Framework
 * @(#) $Id: com.uk.become.shine.request.js,v 1.1 2010/10/28 06:18:02 ultrasis\xiongjiping Exp $
 * --------------------------------------------------------------------
 * Copyright (c) 2006 Become Interactive
 * http://www.becomeinteractive.co.uk
 * All rights reserved.
 * --------------------------------------------------------------------
 * This software is the confidential and proprietary information of
 * Become Interactive ("Confidential Information").
 *
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * with the terms of the license agreement you entered into with
 * Become Interactive.
 * --------------------------------------------------------------------
 */

/**
 * com.uk.become.shine.Request
 *
 * Scripted HTTP requests on demand
 */
com.uk.become.shine.Request = function(method, uri)
{
	var r;
	var progids = [
				   'MSXML2.XMLHTTP.5.0',
				   'MSXML2.XMLHTTP.4.0',
				   'MSXML2.XMLHTTP.3.0',
				   'MSXML2.XMLHTTP',
				   'Microsoft.XMLHTTP'
				   ];
	
	this._method = method;
	this._uri = uri;
	this._req = false;
	this._loaded = false;
	this.onload = false;
	this.onerror = false;
	this.method = false;
	this.params = new Array();
	this.formVars = new Object();
	this._headers = new Object();
	try
		{
			r = new XMLHttpRequest();
			this._req = r;
		}
	catch(e)
		{
			for(i = 0; i < progids.length; i++)
				{
					try
						{
							r = new ActiveXObject(progids[i]);
							this._req = r;
							break;
						}
					catch(e)
						{
						}
				}
		}
	finally
		{
			return this;
		}
}
com.uk.become.shine.Request.prototype.header = function(n, v)
{
	this._headers[n] = v;
}
com.uk.become.shine.Request.prototype._readyStateChangeHandler = function(Shine, sender, ev, data)
{
	if(sender.readyState == 4)
		{			
			if(this._loaded)
				{
					return false;
				}
			this._loaded = true;
			ev = new Object();
			ev.status = sender.status;
			ev.statusText = sender.statusText;
			
			if(sender.status == 200)
				{
					if(this.onload)
						{
							ev.responseXML = sender.responseXML;
							ev.responseText = sender.responseText;
							this.onload(ev);
						}
					else
						{
							Shine.debug('Request::_readyStateChangeHandler:', 'Success: status = ' + sender.status + ' (' + sender.statusText + ')');
						}
				}
			else
				{
					if(this.onerror)
						{
							this.onerror(ev);
						}
					else
						{
							Shine.debug('Request::_readyStateChangeHandler:', 'Failed: status = ' + sender.status + ' (' + sender.statusText + ')');
						}
				}
		}
}
com.uk.become.shine.Request.prototype.open = function()
{
	var h, payload, hfv, _this = this;
	
	if(!this._req)
		{
			Shine.debug('Request:', 'Request object has no XMLHttpRequest (' + this._method + ' ' + this._uri + ')');				
			return false;
		}
	hfv = false;
	payload = '';
	if(this.method)
		{
			this._method = 'POST';
			this.header('Content-Type', 'application/vnd.become.rpc+xml');
			payload = '<?xml version="1.0" encoding="UTF-8" ?>' + "\n";
			payload += '<methodCall>' + "\n";
			payload += ' <methodName>' + this.method + '</methodName>' + "\n";
			payload += '  <params>' + "\n";
			for(h = 0; h < this.params.length; h++)
				{
					payload += '   <param><value>';
					if(typeof this.params[h] == 'number')
						{
							payload += '<i4>' + this.params[h] + '</i4>';
						}
					else if(typeof this.params[h] == 'boolean')
						{
							payload += '<boolean>' + (this.params[h] ? '1' : '0') + '</boolean>';
						}
					else
						{
							payload += '<string>' + this.params[h] + '</string>';
						}
					payload += '</value></param>' + "\n";
				}
			payload += '  </params>' + "\n";
			payload += '</methodCall>' + "\n";
		}
	else
		{
			for(h in this.formVars)
				{
					hfv = true;					
					payload += encodeURIComponent(h) + '=' + encodeURIComponent(this.formVars[h]) + '&';					
				}
			if(hfv)
				{
					payload = payload.substr(0, payload.length - 1);
					this.header('Content-Type', 'application/x-www-form-urlencoded');
				}
		}
	Shine.debug('Request:', this._method + ' ' + this._uri);
	this._req.open(this._method, this._uri, true);	
	this._req.onreadystatechange = function() { _this._readyStateChangeHandler(Shine, _this._req, false, false); }
	/* Work around WebKit bug */
	this._req.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT');
	for(h in this._headers)
		{
			this._req.setRequestHeader(h, this._headers[h]);
		}
	if(payload)
		{
			this._req.send(payload);
		}
	else
		{
			this._req.send(null);
		}
}

