function xhr()
{
	var obj;
	if (window.XMLHttpRequest) // Firefox et autres
	{
		obj = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) // Internet Explorer
	{
		var ieversions = [	'Msxml2.XMLHTTP',
							'Microsoft.XMLHTTP',
							'Msxml2.XMLHTTP.5.0',
							'Msxml2.XMLHTTP.4.0',
							'Msxml2.XMLHTTP.3.0'];

		for (var i=0; !obj && i<ieversions.length; i++)
		{
			try
			{
				obj = new ActiveXObject(ieversions[i]);
			}
			catch (e)
			{
				obj = null;
			}
		}
	}

	return obj;
}
var xhr_object = new xhr();



function chargement(visible)
{
	var id = '';

	if (arguments.length > 1)
		id = arguments[1];

	if (id == '')
		id = 'chargement';
	
	if (visible)
		document.getElementById(id).style.display = '';
		
	else
		document.getElementById(id).style.display = 'none';	
}



var dumpInfosContact = '';
function infosContact(visible)
{
	if (dumpInfosContact == '')
	    dumpInfosContact = document.getElementById('infosContact').innerHTML;

	if (visible)
		document.getElementById('infosContact').innerHTML = dumpInfosContact;
		
	else
		document.getElementById('infosContact').innerHTML = '<div align="center"><a href="#" onClick="javascript: infosContact(true);">Voir la fiche compl&egrave;te</a></div>';
}







// RECHERCHER
// ----------

var search_elements = new Array(
    'selec', '-- Selectionner --',

	'conta', 'Contact',
	'admin', 'Utilisateur',
	'profe', 'Professionnel',
	'devis', 'n&deg; Devis',
	'affai', 'n&deg; Affaire'
);
var search_defaultValue = 'Mots clefs';
var ancien_contenu = '';


// Initialisation
function search_init()
{
	var rech = document.getElementById('rech');
	rech.innerHTML = 'Rechercher '; // initialisation de rech
	var list = document.createElement('select');
	list.name = 'search_choix';
	list.id = 'search_choix';
	list.onclick = function () {
    	search_setRecherche(this.value);
	}

 	rech.appendChild(list);
	
	for (var i=0; i+1<search_elements.length; i++)
	{
		var el = document.createElement('option');
		el.value = search_elements[i];
		el.innerHTML = search_elements[i+1];
		
		list.appendChild(el);
		i++;
	}
}

// Changement des classes du champs text de recherche
function search_input(id)
{
	var rech = document.getElementById(id);

	if (rech.className != 'focus')
	{
		rech.className = 'focus';
		if (rech.value == search_defaultValue)
			rech.value = '';
	}
	else
	{
		rech.className = 'blur';
		if (rech.value == '')
			rech.value = search_defaultValue;
	}
}

// Recherche
function search_setRecherche(elms)
{
    ancien_contenu = document.getElementById('contenu').innerHTML;
	var rech = document.getElementById('rech');
	
	switch (elms)
	{
	    case 'conta': rech.innerHTML = 'Recherche de contacts/clients ';    break;
		case 'admin': rech.innerHTML = 'Recherche des utilisateurs ';		break;
		case 'profe': rech.innerHTML = 'Recherche de professionnels ';		break;
		case 'devis': rech.innerHTML = 'Recherche de devis ';				break;
		case 'affai': rech.innerHTML = 'Recherche d\'affaires ';			break;
		// Non connu, on initialise la recherche
		default: return; break;
	}
	
	var hidden = document.createElement('input');
	hidden.setAttribute('type', 'hidden');
	hidden.name = 'search_choix';
	hidden.id = 'search_choix';
	hidden.value = elms;
	rech.appendChild(hidden);
	
	var motclef = document.createElement('input');
	motclef.setAttribute('type', 'text');
	motclef.onfocus = function () { search_input('search_q'); }
	motclef.onblur = function () { search_input('search_q'); }
	motclef.onkeyup = function () { search_resultats(this.value); }
	motclef.id = 'search_q';
	motclef.name = 'search_q';
	motclef.className = 'blur';
	motclef.value = search_defaultValue;
	rech.appendChild(motclef);
	
	var reinit = document.createElement('input');
	reinit.setAttribute('type', 'button');
	reinit.setAttribute('title', 'Nouvelle recherche');
	reinit.value = 'X';
	reinit.onclick = function () { search_init(); document.getElementById('contenu').innerHTML = ancien_contenu; if (document.getElementById('infosContact2') != null) document.getElementById('infosContact2').style.display = ''; }
	rech.appendChild(reinit);
	
	// Lancement de la recherche vide
	search_resultats('%');
	motclef.focus();
}

// Recherche des résultats par AJAX et affichage de ceux-ci
function search_resultats(motclef)
{
    if ( xhr_object == null )
	{
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		return false;
	}

	var choix = document.getElementById('search_choix');
	xhr_object.open('GET', 'tests.php?param=search&search_q='+motclef+'&search_choix='+choix.value+'&ajax=results', true);

	xhr_object.onreadystatechange = function() {
		if ( xhr_object.readyState == 4 )
		{
	        document.getElementById('contenu').innerHTML = xhr_object.responseText;
	        if ( document.getElementById('infosContact2') != 'null' )
                document.getElementById('infosContact2').style.display = 'none';
		}
	}

	xhr_object.send(null);
}
