
function addFriend(user_name)
{
    var friend_name = $('#friend_name').val();
    if (friend_name.length == 0 ) {
        alert("用户名字不能为空");
        $('#friend_name').focus();
        return false;
    }

    var group_id = $('#group_id').val();
    var valid_msg = $('#valid_msg').val();
    if( valid_msg.length == 0){
        alert("验证信息不能为空");
        $('#group_id').focus();
        return false;
    } 
        
    var params = $('input,select').serialize();
    params = params+ '&user='+user_name+'&act=add';
    $.ajax({
        url: 'friend.php',
        type: 'post',
        data: params,
        success: function(response) {
            if(response == 'add_ok')
            {
                $('#hint').innerHTML = "添加好友成功！";
                alert("添加好友成功！");
                hideBox();
                window.location.reload();
            }else if(response == 'no_user')
            {
                alert("您要添加的用户不存在!");
                return false;
            }
            else 
            {
                alert(response);
                $('#hint').innerHTML = response;
                return false;
            }
        }
    });

    return false;
}

function createGroup(user_name){
    var friend_name = $('#group_name').val();
    if (friend_name.length == 0 ) {
        alert("分组名不能为空");
        $('#group_name').focus();
        return false;
    }
	
	if(friend_name.length > 20)
	{
	    alert("分组名字不能超过20个字符");
        $('#group_name').focus();
        return false;
	}


    var params = $('input,select').serialize();
    params = params+ '&user='+user_name+'&act=cg';
    $.ajax({
        url: 'friend.php',
        type: 'post',
        data: params,
        success: function(response) {
            
            if(response == 'cg_ok')
            {
                alert("创建好友分组成功！");
                hideBox();
                window.location.reload();
            }
            else 
            {
                alert(response);
                return false;
            }
        }
    });

    return false;
}


function moveFriend(user_name){
    var params = $('input,select').serialize();
    params = params+ '&user='+user_name+'&act=move';
    $.ajax({
        url: 'friend.php',
        type: 'post',
        data: params,
        success: function(response) {

            if(response == 'move_ok')
            {
                alert("移动好友成功！");
                hideBox();
                window.location.reload();
            }
            else
            {
                alert(response);
                return false;
            }
        }
    });
}

function renameGroup(user_name){
    var params = $('input').serialize();
    params = params+ '&user='+user_name+'&act=renameGroup';
	var new_group_name = $('#new_gn').val();
	if(new_group_name.length > 20)
	{
	    alert("分组名字不能超过20个字符");
        $('#new_gn').focus();
        return false;
	}
    $.ajax({
        url: 'friend.php',
        type: 'post',
        data: params,
        success: function(response) {
            if(response == 'rename_ok')
            {
                alert("重命名分组成功！");
                hideBox();
                window.location.reload();
            }
            else
            {
                alert(response);
                return false;
            }
        }
    });
}
