// Variable used to lock users actions while AJAX works
var autorisation = true;

// AJAX management
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 // XMLHttpRequest non supporté par le navigateur
		{
			alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
			xhr = false; 
		} 
	}
	return xhr;
}

// Manage saved ad
function manageSavedAd(url, action, ad_div_id, param)
{
	if(autorisation)
	{
		autorisation = false;

		var xhr = getXhr();

		xhr.onreadystatechange = function()
		{
			if(xhr.readyState == 4 && xhr.status == 200)
			{
				document.getElementById("notification").style.filter = "alpha(opacity=0)";
				document.getElementById("notification").style.MozOpacity = 0;
				document.getElementById("notification").style.opacity = 0;
				document.getElementById("notification").innerHTML = xhr.responseText;
				document.getElementById("notification").style.display = "inline";

				afficher_notification();
				
				if(action == 1)
				{
					document.getElementById("ad"+ad_div_id).onclick=function(){manageSavedAd(url, 3, ad_div_id, param)};
					$("#ad"+ad_div_id).attr({"class":"save-ad-delete"});
					$("#ad"+ad_div_id).text(ad_delete_label);

				}
				else
				{
					document.getElementById("ad"+ad_div_id).onclick=function(){manageSavedAd(url, 1, ad_div_id, param)};
					$("#ad"+ad_div_id).attr({"class":"save-ad-add"});
					$("#ad"+ad_div_id).text(ad_save_label);
				}
			}
		}
		xhr.open("POST","http://" + location.hostname + url + "/ajax/ajax_savedAds.php",true);

		xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");

		xhr.send("act=" + action + "&" + param);
	}
}

// Manage alert
function manageAlert(url, action, param)
{
	if(autorisation)
	{
		autorisation = false;
		
		var xhr = getXhr();
	
		xhr.onreadystatechange = function()
		{
			if(xhr.readyState == 4 && xhr.status == 200)
			{
				if(xhr.responseText == "ok")
				{
				document.getElementById('notification').style.filter = "alpha(opacity=0)";
				document.getElementById('notification').style.MozOpacity = 0;
				document.getElementById('notification').style.opacity = 0;
				document.getElementById('notification').innerHTML = alert_save_label;
				document.getElementById('notification').style.display = "inline";
				i = 0;
				afficher_notification();
				}
			}
		}

		xhr.open("POST","http://" + location.hostname + url + "ajax/ajax_alert.php",true);

		xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");

		xhr.send("act="+action+"&"+param);
	}
}

// Load departments
function loadDepartments(url, param)
{
	var xhr = getXhr();

	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			document.getElementById("departments-select").innerHTML = xhr.responseText;
			loadCities(url, document.getElementById("departments-select").value);
		}
	}

	xhr.open("POST","http://" + location.hostname + url + "ajax/ajax_departments.php",true);

	xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");

	xhr.send("reg="+param);
}

// Load cities
function loadCities(url, param)
{
	var xhr = getXhr();

	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			document.getElementById("cities-select").innerHTML = xhr.responseText;
		}
	}

	xhr.open("POST","http://" + location.hostname + url + "ajax/ajax_cities.php",true);

	xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");

	xhr.send("dep="+param);
}
