/////////////////////////////////////////////////////////////////////////
//
//		AJAXCore.js - librairie de fonctions AJAX
//		Dernière mise à jour : v0.21
//		Par Mathieu de Lorimier
//
/////////////////////////////////////////////////////////////////////////


function GetXmlHttpObject()
{
	var xmlHttp;
	try
  	{  
		// Firefox, Opera 8.0+, Safari, IE 7  
		xmlHttp = new XMLHttpRequest(); 
	}
	catch (e)
	{  
		// Internet Explorer 
		try
    	{    
			xmlHttp = new ActiveXObject("MSXML2.XMLHTTP.3.0");
		}
  		catch (e)
    	{    
			try
      		{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");      
			}
    		catch (e)
      		{      
				return null;
						

			}    
		}  
	} 

	return xmlHttp;
}

var _id;
var _ALoadPage_xmlHttp;
var _AControlUpdater_objRef;


////////////////////////////////////////////////////////////////////////
// Classe ALoadPage
function ALoadPage(Id, url)
{
    this._id = Id;
    _id = Id;
    this._url = url;
    this._xmlHttp = GetXmlHttpObject();
	
	if(this._xmlHttp == null)
	{
		alert("Votre navigateur ne supporte pas AJAX. \n Configuration du site web en fonction de votre navigateur.");
		document.location.href = "./noajax.php";
		return;
	}
	
	this._CallBack = function()
    {	
	    var xmlHttp;
    	
		// compatibilité firefox 2.x
	    if(this.readyState)
	        xmlHttp = this;
	    else
	        xmlHttp = _ALoadPage_xmlHttp;
        
	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
	    { 
		    var div = document.getElementById(_id);
		    if(div != null)
		    {
			    var Doc = xmlHttp.responseText;
            
			    div.innerHTML = Doc;
		    }
	    }
		else if(xmlHttp.readyState == 4 && xmlHttp.status == 404)
		{
		    var div = document.getElementById(_id);
		    if(div != null)
		    {      
			    div.innerHTML = "Erreure!";
		    }
		}

    }
}

ALoadPage.prototype._id;
ALoadPage.prototype._url;
ALoadPage.prototype._xmlHttp;

ALoadPage.prototype.GetPage = function()
{
	if(this._xmlHttp != null)
    {
		this._xmlHttp.onreadystatechange = null;
		this._xmlHttp = GetXmlHttpObject();
		this._xmlHttp.onreadystatechange = this._CallBack;
		_ALoadPage_xmlHttp = this._xmlHttp;
		_id = this._id;
		
		var div = document.getElementById(_id);
		if(div != null)
		{
			div.innerHTML = "Chargement ...";
		}
		this._xmlHttp.open("GET", this._url, true);
		this._xmlHttp.send(null);
	}
}

ALoadPage.prototype.GetPageSync = function()
{
	if(this._xmlHttp != null)
    {
		this._xmlHttp.onreadystatechange = null;
		this._xmlHttp = GetXmlHttpObject();
		this._xmlHttp.onreadystatechange = this._CallBack;
		_ALoadPage_xmlHttp = this._xmlHttp;
		_id = this._id;
		
		var div = document.getElementById(_id);
		if(div != null)
		{
			div.innerHTML = "Chargement ...";
		}
		this._xmlHttp.open("GET", this._url, false);
		this._xmlHttp.send(null);
	}
}

ALoadPage.prototype.SetID = function(id)
{
    if(id != "" && typeof(document.getElementById(id)) == object)
    {
        this._id = id;
	    _id = id;
	}
}

ALoadPage.prototype.SetURL = function(url)
{
    if(url != "")
    {
        this._url = url;
	}
}

////////////////////////////////////////////////////////////////////////
// Classe AControlUpdater
function AControlUpdater(Id, url)
{
    this._id 		= Id;
    _id 			= Id;
	this._options	= false;
    this._url 		= url;
    this._xmlHttp 	= GetXmlHttpObject();
	_AControlUpdater_objRef = this;
	
	if(this._xmlHttp == null)
	{
		alert("Votre navigateur ne supporte pas AJAX. \n Configuration du site web en fonction de votre navigateur.");
		document.location.href = "./noajax.php";
		return;
	}
}

AControlUpdater.prototype._id;
AControlUpdater.prototype._url;
AControlUpdater.prototype._options;
AControlUpdater.prototype._xmlHttp;


AControlUpdater.prototype._CallBack = function()
{
	var xmlHttp;
	
	// compatibilité firefox 2.x
	if(this.readyState)
		xmlHttp = this;
	else
		xmlHttp = _xmlHttp;
				 
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
	{ 
		var list = document.getElementById(_id);
		if(list != null)
		{
			if(list.type == "select-one")
			{
				var xmlDoc = xmlHttp.responseXML;
				var sel = list.value;
				
				while (list.length > 0) 
				{
					list.remove(0);
				} 
				
				if(_AControlUpdater_objRef._options)
				{
					nElement = document.createElement('option');
					
					nElement.text = _AControlUpdater_objRef._options['text'];
					nElement.setAttribute("value", _AControlUpdater_objRef._options['val']);
					
					try
					{
						list.add(nElement, null);
					}
					catch(ex)
					{
						list.add(nElement);	
					}						
				}

				for(i = 0; i < xmlDoc.getElementsByTagName("ListItem").length; i++)
				{
					nElement = document.createElement('option');
					
					nElement.text = xmlDoc.getElementsByTagName("ListItem")[i].getAttribute("Text");
					nElement.setAttribute("value",xmlDoc.getElementsByTagName("ListItem")[i].getAttribute("Valeur"));
					if(xmlDoc.getElementsByTagName("ListItem")[i].getAttribute("Class"))
					{
						nElement.className = xmlDoc.getElementsByTagName("ListItem")[i].getAttribute("Class");
					}	
					
					try
					{
						list.add(nElement, null);
					}
					catch(ex)
					{
						list.add(nElement);	
					}
				}
				if(xmlDoc.getElementsByTagName("ListItem").length == 1)
				{
					list.selectedIndex = 0;
				}
				else
				{
					list.value = sel;	
				}
			}
			else if(list.rows)
			{
				while(list.rows.length > 0)
				{
					list.deleteRow(0);
				}
				
				var xmlDoc = xmlHttp.responseXML;

				for(i = 0; i < xmlDoc.getElementsByTagName(_id)[0].childNodes.length; i++)
				{	
					var c = [];
					var xNode = xmlDoc.getElementsByTagName(_id)[0].childNodes[i]
					if(xNode.getAttribute("Etiquette") != "")
					{
						row = list.insertRow(list.rows.length);
						
						c[0] = row.insertCell(0);
						c[1] = row.insertCell(1);
						c[0].innerHTML = xNode.getAttribute("Etiquette");		
						c[1].innerHTML = xNode.childNodes[0].nodeValue;
					}
				}

			}
		}
	}
}

AControlUpdater.prototype.GetPage = function()
{
	if(this._xmlHttp != null)
    {
		this._xmlHttp = GetXmlHttpObject();
		this._xmlHttp.onreadystatechange = this._CallBack;
		_xmlHttp = this._xmlHttp;
		_id = this._id;
		_AControlUpdater_objRef = this;
		this._xmlHttp.open("GET", this._url, true);
	    this._xmlHttp.send(null);
	}
}

AControlUpdater.prototype.SetID = function(id)
{
    if(id != "" && typeof(document.getElementById(id)) == object)
    {
        this._id = id;
	    _id = id;
	}
}

AControlUpdater.prototype.SetURL = function(url)
{
    if(url != "")
    {
        this._url = url;
	}
}

AControlUpdater.prototype.AddOption = function(val, text)
{
    if(text != '')
    {
		if(this._options)
		{
			// multi options
		}
		else
		{
			this._options 			= Array();
			this._options['val'] 	= val;
			this._options['text'] 	= text;
		}
	}
}