function 	getContactGrabber(id)
{
	var formElement = document.getElementById('formu_'+id);
	var waitingElement = document.getElementById('waiting_'+id);
	var errorElement = document.getElementById('error_'+id);
	var provider;

	//on récupère le provider
	if (id == 'fb')
		provider = 'facebook';
	else
		provider = formElement.provider.value;
	
	// On affiche ou cache les messages
	errorElement.style.display = 'none';
	waitingElement.style.display = 'block';
	
	//Appelle au script Ajax (action: grabber)
    $.post("http://" + location.host + "/ajax/grabber",{
                email: formElement.email.value,
                password: formElement.password.value,
				provider: provider,
				type: id
            }, function(data){
                obj = window.eval(data);
				waitingElement.style.display = 'none';
				checkAjaxResponse(obj, id);
            }, "json"); //this returns JSON.
}

function	checkAjaxResponse(obj, id)
{
	var divElement = document.getElementById('form-'+(id == 'fb'? 'facebook' : id));
	var errorElement = document.getElementById('error_'+id);
	var resultLabel = document.getElementById('label-'+id);
	var resultDiv = document.getElementById('tab-'+id);
	if (id != 'fb')
		var divImgElement = document.getElementById('img-'+id);

	if (obj['type'] == 'success')
	{
		divElement.style.display = 'none';
		errorElement.style.display = 'none';
		resultLabel.style.display = 'block';
		resultDiv.style.display = 'block';
		showResult(obj['contacts'], id);
	}
	else
	{
		errorElement.style.display = 'block';
		errorElement.innerHTML = 'Vos identifiants sont incorrectes';
	}
}

function	showResult(contacts, id)
{
	var MAIL_MAXLENGTH = 33;
	var tableElement = document.getElementById(id+'_result');
	var newCell;
	var newRow;
	var i;
	
	$('#label-webmail').hide();
	
	if (typeof this.save == 'undefined')
	{
		this.save = new Array();
		this.save['webmail'] = 0;
		this.save['fb'] = 0;
		this.save['social'] = 0;
	}
	
	i = 1;
	while (this.save[id] > 0)
	{
		tableElement.deleteRow(this.save[id]);
		this.save[id]--;
	}
	for (key in contacts)
	{
		newRow = tableElement.insertRow(i);
		if (id == 'fb')
		{
			newCell = newRow.insertCell(0);
			newCell.style.width = "20px";
			newCell.innerHTML = i;	
			newCell = newRow.insertCell(1);
			newCell.innerHTML = '<input type="checkbox" name="'+contacts[key]+'" value="'+key+'"onclick="limitCheckBox(this)"/>';
			newCell.style.width = "50px";
			newCell = newRow.insertCell(2);
			newCell.style.width = "250px";
			if(contacts[key].length > MAIL_MAXLENGTH)
			{
				var textLink = document.createElement("A");
				textLink.innerHTML = contacts[key].substr(0, (MAIL_MAXLENGTH-3)) + '...';
				textLink.setAttribute("title", contacts[key]);
				newCell.appendChild(textLink);
			}
			else
			{
				newCell.innerHTML = contacts[key];
			}		
		}
		else
		{
			newCell = newRow.insertCell(0);
			newCell.innerHTML = '<input type="checkbox" name="'+contacts[key]+'" checked="checked" value="'+key+'"/>';
			newCell = newRow.insertCell(1);
			newCell.setAttribute('class', 'autre');
			if(contacts[key].length > MAIL_MAXLENGTH)
			{
				var textLink = document.createElement("A");
				textLink.innerHTML = contacts[key].substr(0, (MAIL_MAXLENGTH-3)) + '...';
				textLink.setAttribute("title", contacts[key]);
				newCell.appendChild(textLink);
			}
			else
			{
				newCell.innerHTML = contacts[key];
			}
		}
		if (id != 'fb')
		{
			newCell = newRow.insertCell(2);
			newCell.setAttribute('class', 'autre');
			if(key.length > MAIL_MAXLENGTH)
			{
				var textLink = document.createElement("A");
				textLink.innerHTML = key.substr(0, (MAIL_MAXLENGTH-3)) + '...';
				textLink.setAttribute("title", key);
				newCell.appendChild(textLink);
			}
			else
			{
				newCell.innerHTML = key;
			}		
		}
		//newCell.innerHTML = key;
		i = i + 1;
	}
	this.save[id] = i - 1;
}