function UpdateBuddyError(obj)
{
    var loggerEl = document.getElementById("logDiv");
    if (!loggerEl)
        loggerEl = document.getElementById("saveRangeSpan");
    if (loggerEl)
        $(loggerEl).html("<br>Error" + (obj.error?obj.error:obj.responseText));
}
function UpdateBuddy(obj)
{
	var aonclickadd=" onClick='AddBuddy("+obj.sec_user_id+");' ";
	var aonclickdel=" onClick='DelBuddy("+obj.sec_user_id+");' ";
    //NEW CODE by PI: adding link to private messages
    var innerimgcode = "";
    if (document.targetpage != "pm_settings")
    if (obj.relation != '-')
        innerimgcode +=  "<div style='padding-top: 1px; padding-bottom: 5px;'><a title='Write new message'"
            + " onclick=\"tb_show('New private message',"
            +"'/my/pm/newpm.php?height=400&width=420&modal=true&user_id="
            +obj.user_id+"&sec_user_id="+obj.sec_user_id+"',false);\""
            +"><img class='imgnewmsg' src='/img/msg.gif' alt='Write new message'></a></div>"

    if (obj.relation != '-')
        innerimgcode += "<div><a title='Add to whitelist'"
        +" onclick='"+((obj.inwhitelist==1)?"removeCustomSender(":"addCustomSender(")+obj.sec_user_id+");'>"
        +"<img class='imgwhitelist "
        +((obj.inwhitelist==1)?"whiterem":"whiteadd")+obj.sec_user_id
        +"id' alt='Add to whitelist' src='/img/wl_" + ((obj.inwhitelist==1)?"del.gif":"add.gif")+"'></a></div>";

    var buddy_plug="<table><tr><td style='vertical-align: top'>" + innerimgcode + "</td><td>";
    var buddyimg = "";
    var buddyimgaddrem = "";
	switch (obj.relation)
	{
	// Buddy
	case 'b':
        buddyimg = "<img src='/img/bd_buddy.gif' alt='Your Buddy' border=0>";
        buddyimgaddrem = "<a "+aonclickdel+" class='tiny' title='Remove buddy'><img src='/img/bd_del.gif' alt='Remove buddy' border=0></a>";
	break;

	// Mutual friends
	case 'm': 
        buddyimg = "<img src='/img/bd_friend.gif' alt='Mutual Friend' border=0>";
        buddyimgaddrem = "<a "+aonclickdel+" class='tiny' title='Remove buddy'><img src='/img/bd_del.gif' alt='Remove buddy' border=0></a>";
	break;
	// Fan
	case 'f': 
        buddyimg = "<img src='/img/bd_fan.gif' alt='Your Fan' border=0>";
        buddyimgaddrem = "<a "+aonclickadd+" class='tiny' title='Add buddy'><img src='/img/bd_add.gif' alt='Add buddy' border=0></a>";
	break;
	// No relations
	case 'n': 
        buddyimg = "<img src='/img/bd_no.gif' alt='No relations' border=0>";
        buddyimgaddrem = "<a "+aonclickadd+" class='tiny' title='Add buddy'><img src='/img/bd_add.gif' alt='Add buddy' border=0></a>";
	break;
    case '-':
        buddyimg = "<b>You</b>"
    break;
	}
    buddy_plug += buddyimg + "<br>" + buddyimgaddrem + "</td></tr></table>";
    var buddytblcode = buddyimg + buddyimgaddrem;//.replace("border=0", "border=0 style='width: 40px;'");
    var budid = "bud_" + obj.sec_user_id;
    if (document.getElementById(budid))
    {
        //alert("DEBUG: document.getElementById(budid) = " + document.getElementById(budid));
    	$("#"+budid).html(buddy_plug);
    }
    if (document.getElementById("tbl_" + budid))
    {
        //alert("DEBUG: document.getElementById('tbl_' + budid) = " + document.getElementById("tbl_" + budid));
        $("#tbl_" + budid).html(buddytblcode);
    }
    if (document.targetpage == "pm_settings")
    {
        if (document.getElementById("inset_"+budid))
            $("#inset_"+budid).html(buddy_plug);
        if (document.getElementById("inset_tbl_" + budid))
            $("#inset_tbl_" + budid).html(buddytblcode);
    }
}

function AddBuddy(id)
{
	return buddyOperation(id, 'addbuddy');
}
function DelBuddy(id)
{
    return buddyOperation(id, 'delbuddy');
}
function GetBuddyInfo(id)
{
    return buddyOperation(id, 'getinfo');
}
function buddyOperation(id, operation)
{
    if (!document.user_id)
        return false;
    var htmlwait = "Wait...";
    if (document.user_id == id)
        htmlwait = "<b>You</b>";
    if (document.getElementById("bud_" + id))
        $("#bud_"+id).html(htmlwait);
    if (document.getElementById("tbl_bud"+id))
        $("#tbl_bud"+id).html(htmlwait);
    if (document.targetpage == "pm_settings")
    {
        if (document.getElementById("inset_bud_" + id))
        	$("#inset_bud_"+id).html(htmlwait);
        if (document.getElementById("inset_tbl_bud_" + id))
        	$("#inset_tbl_bud_"+id).html(htmlwait);
    }
    //alert("DEBUG: buddyOperation = " + operation + " id = " + id);
    if (document.user_id != id)
	postJSON(
	'/inc/buddies/buddies.php',
	{
		user_id: document.user_id,
		sec_user_id: id,
		operation: operation,
        targetpage: (document.targetpage?document.targetpage:"")
	},
	UpdateBuddy, UpdateBuddyError
	);
	return false;
}
/*
function BuddyPlug($user_id)
{
	global $user;

	if (IsUser())
	{
		echo "<div style='margin-top:5px; padding:4px; border: 1px dashed #e0e0e0;' id='bud_".$user_id."'>";

		$aonclickadd=" onClick='AddBuddy(".$user_id.");' ";
		$aonclickdel=" onClick='DelBuddy(".$user_id.");' ";

		//onClick="RecalcPlans(1);
		switch (GetBuddyRelations($user['id'],$user_id))
		{
		// Buddy
		case 'b':
		echo "<img src='/img/bd_buddy.gif' alt='Your Buddy' border=0>";
		echo "<br><a ".$aonclickdel." class='tiny'><img src='/img/bd_del.gif' alt='Remove buddy' border=0></a>";
		break;
		// Mutual friends
		case 'm': echo "<img src='/img/bd_friend.gif' alt='Mutual Friend' border=0>";
		echo "<br><a ".$aonclickdel." class='tiny'><img src='/img/bd_del.gif' alt='Remove buddy' border=0></a>";
		break;
		// Fan
		case 'f': echo "<img src='/img/bd_fan.gif' alt='Your Fan' border=0>";
		echo "<br><a ".$aonclickadd." class='tiny'><img src='/img/bd_add.gif' alt='Add buddy' border=0></a>";
		break;
		// No relations
		case 'n': echo "<img src='/img/bd_no.gif' alt='No relations' border=0>";
		echo "<br><a ".$aonclickadd." class='tiny'><img src='/img/bd_add.gif' alt='Add buddy' border=0></a>";
		break;

		case '-': echo "<b>You</b>";
		break;
		}
		echo "</div>";

	}

}*/
