/************************************************************************
JavaScript.net
File: System.Http.js
Author: Scott Wood
Version: 2.4.0.1
Description: Provides base http ajax functionalty
This software is provided under the Open Software License v. 2.1 Agreement
=========================================================================
* 2.4.0.1
+ Updated method cases
+ Added enumerated types
* 2.4.0.2
- Removed asynchronous from class will add in later implementation
+ Added fast request methods
*************************************************************************/
using('System.Diagnostics');
namespace('System.Http',2,4,0,2);
System.Http.RequestMethod = new Enum()
{
	with(System.Http)
	{
		RequestMethod.Get = 'GET';
		RequestMethod.Put = 'PUT';
		RequestMethod.Post = 'POST';
		RequestMethod.Delete = 'DELETE';
	}
}
System.Http.ResponseType = new Enum()
{
	with(System.Http)
	{
		ResponseType.Unknown = 'unknown';
		ResponseType.Xml = 'xml';
		ResponseType.Text = 'text';
	}
}
//Represents an http request
System.Http.Request = function(url)
{
	if(!url)
	{
		throw new ArgumentNullException('url');
	}
    var xmlHttpRequest = (window.ActiveXObject) ? new ActiveXObject("MSXML2.XMLHTTP") : new XMLHttpRequest;
	this.url = url;
	this.username = null;
	this.password = null;
	this.onSendRequestBegin = new Event(this);
	this.onSendRequestComplete = new Event(this);
	this.onSendRequestError = new Event(this);
    with(System.Http)
	{
		if(!Request.initialize())
		{
			//Send the http request
			Request.prototype.send = function(method,data)
			{
				//Assert method parameters
				if(!RequestMethod.contains(method))
				{
					throw new ArgumentException('method '+ method.toString() +' is not a valid RequestMethod value');
				}
				data = data || null;
				//Initialize local variables
				var result = null;
				var responseType = ResponseType.Unknown;
				try
				{
					//Raise the begin request event
					System.Diagnostics.Trace.writeLine('Sending '+ this.url + ' the following data was sent with the request:');
					System.Diagnostics.Trace.writeLine(data);
					this.onSendRequestBegin.raise();
					//Open an http connection and send the data
					xmlHttpRequest.open(method.toString(),this.url,false,this.username,this.password);
					xmlHttpRequest.send(data);
					System.Diagnostics.Trace.writeLine('Response from '+ this.url);
					//If the response is xml set the result to the xml document
					if(xmlHttpRequest.responseXML && xmlHttpRequest.responseXML.hasChildNodes())
					{	
						responseType = ResponseType.Xml;
						result = xmlHttpRequest.responseXML;
						System.Diagnostics.Trace.writeLine(result.xml);
					}
					//Else if the response is text set the result to the responseText
					else
					{
						responseType = ResponseType.Text;
						result = xmlHttpRequest.responseText;
						System.Diagnostics.Trace.writeLine(result);
					}
					//Raise the on complete event
					this.onSendRequestComplete.raise(result,responseType);
				}
				//Catch any exceptions and raise the request error event
				catch(Exception)
				{
					this.onSendRequestError.raise(Exception);
					System.Diagnostics.Trace.writeLine('Response failed '+ this.url);
					System.Diagnostics.Trace.writeLine(Exception.toString())
				}
				return result;
			}
			//Provides a quick method of sending a GET request
			Request.prototype.get = function(data)
			{
				return this.send(RequestMethod.Get,data);
			}
			//Provides a quick method of sending a PUT request
			Request.prototype.put = function(data)
			{
				return this.send(RequestMethod.Put,data);
			}
			//Provides a quick method of sending a POST request
			Request.prototype.post = function(data)
			{
				return this.send(RequestMethod.Post,data);
			}
			//Proviides a quick method of sending a DELETE request
			Request.prototype.$delete = function(data)
			{
				return this.send(RequestMethod.Delete,data);
			}
		}
	}
}
