function getXhr()
{
	var xhr = null; 
	if (window.XMLHttpRequest) // Firefox et autres
		xhr = new XMLHttpRequest(); 
	else
	if (window.ActiveXObject) // Internet Explorer 
	{
		try
		{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else
	{
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest");
	   xhr = false;
	}
	return xhr;
}

function $(name)
{
	if (document.getElementById)
		return document.getElementById(name);
	else if (document.all)
		return document.all[name];
}

function panier(send_url, div)
{
	var xhr = getXhr();
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
			$(div).innerHTML = xhr.responseText;
	}
	xhr.open("POST", send_url.substring(0, (send_url.indexOf("?"))), true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send(send_url.substring((send_url.indexOf("?") + 1)));
}

function sendMail(send, nom, telephone, email, details, ref, sendto)
{
	error = 0;
	send_url = 'sendmail.php?';
	if (send == 'immo')
	{
		if (isEmptyString(nom) || isEmptyString(telephone) || isEmptyString(email) || !check_mail(email))
			error = 1;
		send_url += 'nom='+nom;
		send_url += '&telephone='+telephone;
		send_url += '&email='+email;
		send_url += '&details='+details;
		send_url += '&ref='+ref;
		send_url += '&send='+send;
		send_url += '&sendto='+sendto;
	}
	else if (send == 'ami')
	{
		if (isEmptyString(nom) || isEmptyString(telephone) || !check_mail(telephone))
			error = 1;
		send_url += 'nom='+nom;
		send_url += '&email='+telephone;
		send_url += '&id='+email;
		send_url += '&type='+details;
		send_url += '&send='+send;
	}
	if (error != 1)
	{
		var xhr = getXhr();
		xhr.onreadystatechange = function()
		{
			if (xhr.readyState == 4 && xhr.status == 200)
				$('mail'+send).innerHTML = xhr.responseText;
		}
		xhr.open("POST", send_url.substring(0, (send_url.indexOf("?"))), true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send(send_url.substring((send_url.indexOf("?") + 1)));
	}
	else
		alert('Veuillez remplir tous les champs correctement');
	return (error == 1 ? false : true);
}

function check_mail(email)
{
   var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
   return (reg.test(email))
}

function isEmptyString(str)
{
	var reg = /^\s*$/
	return reg.test(unescape(str));
}