// AJAX functions

// Make the XMLHttpRequest object
var http = (window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));

function ConfirmDelete(id)
{
	var answer = confirm("Do you really want to remove this item ("+ id +") from the watchlist?");
		
	if (answer != 0)
	{
		WatchList('delete', id);
	}
	return false;
}

var SendingRequest = false;

function WatchList(action, id)
{	
	if (SendingRequest)
	{
		alert("Please wait. Other request in progress...");
		return false;
	}
	
	switch (action)
	{
		case "add":
	
			document.getElementById(id).innerHTML = '<a href="watchlist.php?do=clear&id='+ id +'" onclick="return ConfirmDelete(\''+ id +'\');">Remove from watchlist</a>';
		
		break;
	
	
		default:
			
			if (document.getElementById(id))
			{
				document.getElementById(id).innerHTML = '<a href="#" onclick="return WatchList(\'add\', \''+ id +'\');">Add to watchlist</a>';
			}
		
		break;
	}
	
	http.open('post', 'watchlist.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = function()
	{
		if (http.readyState == 1)
		{ 
			document.getElementById('watchlist').innerHTML = "Please wait, loading..." ; 
		} 
		else if (http.readyState == 4 && http.status == 200)
		{
			document.getElementById('watchlist').innerHTML = http.responseText; 
			var SendingRequest = false;
		}
	}
	http.send('action='+ action +'&id='+ id +'&method=ajax');
	var SendingRequest = true;
	
	return false;
}

var gettingInfo = false;

function getPropertyInfo(pid)
{
	var pid = pid.replace(/property_id_/, '');
	var div = document.getElementById('propertyinfo');
	
	if (isNaN(pid))
	{
		div.innerHTML = 'An error ocurred.';
		return false;
	}
	
	if (gettingInfo)
	{
		return false;
	}
	
	http.open('get', 'ajax.php?do=propertyinfo&pid='+ pid);
	http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = function()
	{
		if (http.readyState == 1)
		{
			div.innerHTML = 'Loading, please wait...';
			gettingInfo = true;
		}
		else if (http.readyState == 4 && http.status == 200)
		{
			div.innerHTML = http.responseText;
			gettingInfo = false;
		}
	}
	http.send(null);
}