var Net = new Object();

Net.READY_STATE_UNINITIALIZED = 0;
Net.READY_STATE_LOADING = 1;
Net.READY_STATE_LOADED = 2;
Net.READY_STATE_INTERACTIVE = 3;
Net.READY_STATE_COMPLETE = 4;

Net.PILOT_SERVICE_ERROR = 1;

Net.APPLICATION_EXCEPTION = "L'exécution du service a provoqué une exception";
Net.APPLICATION_ERROR = "Une erreur applicative est survenue lors de l'exécution du service";
Net.CONNECTION_ERROR  = "Impossible de se connecter au serveur";

Net.ContentLoader = function ()
{
	this.url = null;
	this.req = null;
	this.errorMessage = "";
	this.errorMnemo   = "";
	this.errorType = 0;
	this.redirection = false;
	this.onload = null;
	this.onerror = this.defaultError;
}

Net.ContentLoader.prototype = 
{
	loadXMLDoc: function(url, onload)
	{
		this.url = url;
		if (window.XMLHttpRequest)
		{
			this.req = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if (this.req)
		{
			try
			{
				if (onload)
				{
					this.onload = onload;
					this.req.onreadystatechange = function ()
					{
						this.onReadyState.call(this);
					}
				}
				
				// prevent IE from using its cache - append pseudo-random fake parameter
				if (url.indexOf("?") == -1)
					url+= "?_net_date_=" +  new Date().valueOf();
				else
					url+= "&_net_date_=" +  new Date().valueOf();
					
				this.req.open('GET', url, onload ? true : false);
				
				// prevent IE from using its cache - not working
				/*this.req.setRequestHeader("Cache-Control","no-store, no-cache, must-revalidate");
				this.req.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
				this.req.setRequestHeader("Pragma", "no-cache");
				this.req.setRequestHeader("If-Modified-Since", "Wed, 31 Dec 1980 00:00:00 GMT");
   			this.req.setRequestHeader("Expires", "Wed, 31 Dec 1980 00:00:00 GMT"); */
				this.req.send(null);
			}
			catch(err)
			{
				this.onerror.call(this, err);
			}
		}
	},
	
	/*
	
	var url = "data:"+mtype+";charset=utf-8,"+encodeURIComponent(xml);
		var req = new XMLHttpRequest();
		req.open("GET", url, false);
		req.overrideMimeType(mtype);
		
	*/
	
	/* Use the argument postXml = true when <post type="xml"/> 
     Else the method is used to post a form */
	saveXMLDoc: function(url, content, onload, postXml)
	{
		this.url = url;
		if (window.XMLHttpRequest)
		{
			this.req = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if (this.req)
		{
			try
			{
				if (onload && onload != '')
				{
					this.onload = onload;
					this.req.onreadystatechange = function ()
					{
						this.onReadyState.call(this);
					}
				}
				this.req.open('POST', url, onload ? true : false);
				//this.req.setRequestHeader("Content-Type", "text/xml;charset=iso-8859-1");
				if( postXml )
          this.req.setRequestHeader("Content-Type", "text/xml");
	      else
          this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				this.req.send(content);
			}
			catch(err)
			{
				this.onerror.call(this, err);
			}
		}
	},
	
	onReadyState: function()
	{
		var req = this.req;
		var ready = req.readyState;
		
		if (ready == Net.READY_STATE_COMPLETE)
		{
			//alert("onReadyState ! " + req.status);
			var httpStatus = req.status;
			if (httpStatus == 200 || httpStatus == 0)
				this.onload.call(this);
			else
			{
				alert("httpStatus = " + httpStatus);
				this.onerror.call(this);
			}
		}
	},
	
	defaultError: function()
	{
		alert( Net.APPLICATION_ERROR + "\n\n"
					+ "readyState : " + this.req.readyState + "\n"
					+ "status : " + this.req.status + "\n"
					+ "headers: " + this.req.getAllResponseHeaders());
	},
	
	getStatus: function()
	{	
    this.errorMessage = "";
		this.errorMnemo = "";
		this.errorType = 0;
		this.redirection = false;
		
		if (this.req)
		{	
    	if(this.req.status == 200 )
		 	{
				// server answer nothing - assume everything went well
				if (!this.req.responseXML)
				{
					return true;
				}
				
				if (document.implementation && document.implementation.createDocument)
					Document.prototype.__defineGetter__("xml", function () {	return (new XMLSerializer()).serializeToString(this);	});	
				
				var firstNode = null;
				
				// IE
				if (window.ActiveXObject)
					firstNode = this.req.responseXML.childNodes[1];
				// others
				else
					firstNode = this.req.responseXML.childNodes[0];
					
				if (firstNode.nodeName != "output")
				{
					this.errorMessage = Net.APPLICATION_EXCEPTION + " :\n" + this.req.responseXML.xml;
					return false;
				}
				else if ( firstNode.hasChildNodes() && firstNode.childNodes.length > 0 )
				{
				  if( firstNode.childNodes[0].nodeName == "error" )
				  {
            this.errorMessage = Net.APPLICATION_ERROR + " :\n" + this.req.responseXML.xml;
            this.errorType  = Net.PILOT_SERVICE_ERROR;
            this.errorMnemo = firstNode.getElementsByTagName("error")[0].firstChild.data;
            return false;	
					}
					else if( firstNode.childNodes[0].nodeName == "redirection" )
				  {
				    this.redirection = true;
				    window.location.href = firstNode.getElementsByTagName("redirection")[0].firstChild.data;
				    return false;	
				  }
				  return true;
				}
				else
				{
					return true;
				}
			}
		}
		this.req.errorMessage = Net.CONNECTION_ERROR;	
		return false;
	}
}