
function BxFrFriends (f)
{
    this._form = f;
}

BxFrFriends.prototype.selAll = function (bool)
{
    var f = document.forms[this._form].elements;
    var n = f.length;
    for (var i=0 ; i<n ; ++i)
    {      
        if (!f[i]) continue;
        if ('checkbox' != f[i].type) continue;
        if (f[i].checked == bool) continue;
        f[i].checked = bool;
    }
    
    return false;
}

BxFrFriends.prototype.removeFromMyFriends = function (id)
{
	var ids = id ? id : this._getSelectedIds();
	var $this = this;

	var h = function (r)
	{		
		var o = new BxXmlRequest('','','');			
		var ret = o.getRetNodeValue (r, 'ret');
		if ('1' == ret)
		{
			alert ('Profiles have been successfully removed from your friends\' list');
			document.location = aBxConfig['urlRoot'] + 'friends/my_friends';
		}

		return false;
	}

	new BxXmlRequest (aBxConfig['urlRoot'] + "friends/xml_remove_from_my_friends/" + ids, h, true);

	return false;	
}

BxFrFriends.prototype.addToMyFriends = function (id, redir)
{
	var ids = id ? id : this._getSelectedIds();
	var $this = this;

	var h = function (r)
	{		
		var o = new BxXmlRequest('','','');			
		var ret = o.getRetNodeValue (r, 'ret');
		if ('1' == ret)
		{
			alert ('Profile have been successfully added to your friends\' list');
			if (redir) document.location = aBxConfig['urlRoot'] + 'friends/befriended_me';
		}
        else
        {
            alert ('This profile is already in your friend\'s list');
        }
		return false;
	}

	new BxXmlRequest (aBxConfig['urlRoot'] + "friends/xml_add_to_my_friends/" + ids, h, true);

	return false;	
}

BxFrFriends.prototype.removeFromBefriended = function (id)
{
	var ids = id ? id : this._getSelectedIds();
	var $this = this;

	var h = function (r)
	{		
		var o = new BxXmlRequest('','','');			
		var ret = o.getRetNodeValue (r, 'ret');
		if ('1' == ret)
		{
			alert ('Profiles have been successfully removed from befriended list');
			document.location = aBxConfig['urlRoot'] + 'friends/befriended_me';
		}

		return false;
	}

	new BxXmlRequest (aBxConfig['urlRoot'] + "friends/xml_remove_from_befriended/" + ids, h, true);

	return false;	
}

BxFrFriends.prototype._getSelectedIds = function ()
{
	var ids = '';
    var f = document.forms[this._form].elements;
    var n = f.length;
    for (var i=0 ; i<n ; ++i)
    {      
        if (!f[i]) continue;
        if ('checkbox' != f[i].type) continue;
        if (f[i].checked != true) continue;
        
        ids += f[i].value + ',';
    }		
    
    return ids;
}

