
var border = null;
var id_user = null;

function add_friends_addBlock(ident, text, parent)
{
	var newBlock = document.createElement('div');
	if(ident!= "")newBlock.setAttribute('id', ident);
	if(text != "")newBlock.innerHTML = text;
	
	parent.appendChild(newBlock);
	
	return newBlock;

	
}

function add_friends_addTextArea(ident, rows, cols, text, parent)
{
	var newTA = document.createElement('textarea');
	if(ident != "")newTA.setAttribute('id', ident);
	if(rows != "")newTA.setAttribute('rows', rows);
	if(cols != "")newTA.setAttribute('cols', cols);
	if(text != "")newTA.innerHTML = text;
	
	parent.appendChild(newTA);
	
	return newTA;
}

function add_friends_addParagraphe(ident, text, parent)
{
	var newParagraphe= document.createElement('p');
	if(ident != "")newParagraphe.setAttribute('id', ident);
	newParagraphe.innerHTML = text;
	
	parent.appendChild(newParagraphe);
	
	return newParagraphe;
}

function add_friends_addInput(ident, type, value, onclick, parent)
{
	var newInput = document.createElement('input');
	if(ident != "")newInput.setAttribute('id', ident);
	newInput.setAttribute('type', type);
	newInput.setAttribute('value', value);
	if(onclick != "")newInput.onclick = onclick;
	
	parent.appendChild(newInput);
	
	return newInput;
}

function add_friends_draw()
{
	var body = document.getElementsByTagName('body')[0];
	
	border = add_friends_addBlock("border_friend", "", body);
	
		add_friends_addBlock("header_friend", "", border);//Creation du header
		
		var content = add_friends_addBlock("content_friend", "", border);//Creation du contenu de la box
		
			var ins = add_friends_addBlock("ins", "", content);
		
				add_friends_addParagraphe("", "Ajouter un message qui sera joint &agrave; votre demande de mise en relation.", ins);//Creation du text descriptif
			
				add_friends_addTextArea("messageToFriend", "5", "50", "", ins);//Creation du textarea permetant de recevoir le commentaire
			
				var posInput = add_friends_addParagraphe("", "", ins);
			
				add_friends_addInput("submit", "button", "Valider", add_friends_makeRequest, posInput);
				add_friends_addInput("reset", "button", "Annuler", add_friends_myc_close, posInput);
		
		add_friends_addBlock("footer_friend", "", border);//Creation du footer
		
		// positionnement du block ....
		border.style.left = (screen.availWidth - content.offsetWidth) / 2 + "px";	
		border.style.bottom = (screen.availHeight - content.offsetHeight) / 2 + "px";
}

function add_friends_myc_open(user_id)
{
	id_user = user_id;
	add_friends_draw();
}

function add_friends_myc_close()
{
	var body = document.getElementsByTagName('body')[0];
	
	while (border.firstChild) {
  		border.removeChild(border.firstChild);
	}
	
	body.removeChild(border);
}

var add_friends_handleSuccess = function(o){
	add_friends_myc_close();
//	window.location.href = "/auth/profilefriends";
//document.getElementById('paginatorForm').submit();
location.reload();
//	document.history.go(0);
	return false;
	
};

var add_friends_callback =
{
  success:add_friends_handleSuccess
};

function add_friends_makeRequest(){
	//alert(document.getElementById('messageToFriend').value);
	var postData = "id_friend=" + id_user +"&message=" + document.getElementById('messageToFriend').value;
	YAHOO.util.Connect.asyncRequest('POST', "/auth/profilefriends", add_friends_callback, postData);

}
