var current_id = 0;

function addFriend() {
  var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]{2,}\.[a-z]{2,4}$/;
  
  if(regex.test(document.getElementById('if-mail').value)) {
    var no_flood = true;
    var span = document.getElementById('list-friends').getElementsByTagName('span');
	
	var i = 0;
	while(i < span.length && no_flood) {
	  var value = span[i].firstChild.nodeValue.split('(');

	  if(document.getElementById('if-mail').value + ' ' == value[0])
	    no_flood = false;

	  i++;
	}

	if(no_flood) {
      if(document.getElementById('list-friends').firstChild.tagName.toLowerCase() == 'em')
	    document.getElementById('list-friends').removeChild(document.getElementById('list-friends').firstChild);
  
	  var span = document.createElement('span');
	  span.setAttribute('id', 'mail-'+current_id);
	  span.appendChild(document.createTextNode(document.getElementById('if-mail').value + ' ('));
	
	  var link = document.createElement('a');
	  link.setAttribute('href', '#');
	  link.appendChild(document.createTextNode('Remove'));
	  link.style.fontStyle = 'italic';
      link = attributeFunction(link, current_id);

	  span.appendChild(link);
	
  	  span.appendChild(document.createTextNode(')'));
      document.getElementById('list-friends').appendChild(span);
      document.getElementById('list-friends').appendChild(document.createElement('br'));

	  current_id++;
	  document.getElementById('if-mail').value = '';
	}
	else
	  alert('You\'ve already added this mail address.');
  }
  else
    alert('That\'s not a mail address !');
}

function attributeFunction(element, id) {
  element.onclick = function() { removeFriend(id); return false; };
  return element;
}

function removeFriend(id) {
  var span = document.getElementById('mail-'+id);

  document.getElementById('list-friends').removeChild(span.nextSibling);
  document.getElementById('list-friends').removeChild(span);

  if(!document.getElementById('list-friends').hasChildNodes() || document.getElementById('list-friends').firstChild.tagName.toLowerCase() != 'span') {
    var italique = document.createElement('em');
	italique.appendChild(document.createTextNode('Mailing list empty...'));
	document.getElementById('list-friends').appendChild(italique);
  }
}

function invite() {
  var regex = /\S/gi;
  var friends = document.getElementById('list-friends').getElementsByTagName('span');

  if(regex.test(document.getElementById('if-name').value) && friends.length > 0) {
	var i = 0;
    var list = '';

    while(i < friends.length) {
	  if(i > 0)
	    list += '/';

	  friend = friends[i].firstChild.nodeValue.split('(');
	  list += friend[0].substr(0, friend[0].length - 1);

	  i++;
	}

	document.getElementById('if-list').value = list;
	document.getElementById('invite-friends').submit();
  }
  else {
    if(!regex.test(document.getElementById('if-name').value) && friends.length == 0)
      alert('Please type your name and add one mail address at least to the mailing list.');
	else if(!regex.test(document.getElementById('if-name').value))
	  alert('Please type your name.');
	else
	  alert('Please add one mail address at least to the mailing list.');
  }
}

function detectEnterAddFriend(event) {
  if((document.all && event.keyCode == 13) || event.which == 13)
    addFriend();
}

function addContacts() {
  document.getElementById('search-contacts-name').value = document.getElementById('if-name').value;
  document.getElementById('search-contacts-contacts').value = document.getElementById('list-friends').innerHTML;
  document.getElementById('search-contacts-source').value = document.getElementById('source').value;
  document.getElementById('search-contacts-login').value = document.getElementById('login').value;
  document.getElementById('search-contacts-pwd').value = document.getElementById('pwd').value;

  setTimeout('document.getElementById(\'search-contacts\').submit();', 200);
}