/**
 *  author:		Timothy Groves - http://www.brandspankingnew.net
 *	version:	1.0 - 2006-08-04
 *
 *	requires:	nothing
 *
 */

var useBSNns;

if (useBSNns)
{
	if (typeof(bsn) == "undefined")
		bsn = {}
	_bsn = bsn;
}
else
{
	_bsn = this;
}









_bsn.Ajax = function ()
{
	this.req = {};
	this.isIE = false;
}



_bsn.Ajax.prototype.makeRequest = function (url, meth, onComp, onErr)
{
	if (meth != "POST")
		meth = "GET";

	this.onComplete = onComp;
	this.onError = onErr ? onErr : function ajaxErrorFunc(status) { alert("Une erreur AJAX s'est produite : " + status); };
	
	var pointer = this;
	
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest)
	{
		this.req = new XMLHttpRequest();
		this.req.onreadystatechange = function () { pointer.processReqChange() };
		this.req.open("GET", url, true); //
		this.req.send(null);
	// branch for IE/Windows ActiveX version
	}
	else if (window.ActiveXObject)
	{
		this.req = new ActiveXObject("Microsoft.XMLHTTP");
		if (this.req)
		{
			this.req.onreadystatechange = function () { pointer.processReqChange() };
			this.req.open(meth, url, true);
			this.req.send();
		}
	}
}


_bsn.Ajax.prototype.processReqChange = function()
{
	
	// only if req shows "loaded"
	if (this.req.readyState == 4) {
		// only if "OK"
		if (this.req.status == 200)
		{
			this.onComplete( this );
		} else {
			this.onError( this.req.status );
		}
	}
}

// populateMenu		
function populateMenu(xml, ctrl, def_val)
{
	if (!ctrl) 
		return;
	// mémorise la valeur active
	val = (ctrl.selectedIndex >= 0) ? ctrl.options[ctrl.selectedIndex].value : "";
	if (!val || !val.length)
		val = def_val;
	// ne garde que la première option
	ctrl.options.length = 1;
	// xml vide => ne rien faire
	if (!xml)
		return;
	// recréer les éléments dans l'ordre
	for (var i=0, lst=xml.getElementsByTagName("option"); i<lst.length; i++)
	{
		//ctrl.options.add(new Option(lst[i].getAttribute("title"), lst[i].getAttribute("val"), false, (lst[i].getAttribute("val") == val)));
		if(!i && ctrl.name == "h1")
			selected = true;
		else if((lst[i].getAttribute("val") == val))
			selected = true;
		else
			selected = false;
						
		ctrl.options.add(new Option(lst[i].getAttribute("title"), lst[i].getAttribute("val"), false, selected));
	
	}
}
