﻿function sendMessage(friend, user) {
    if (friend != "") {
        if (friend == user) {
            alert('不能给自己发送消息!');
            return false;
        } else {
            var obj = document.createElement("div");
            obj.id = "filter";
            obj.innerHTML = '<div id="dmask" style="z-index:10000; display:block; background:#000; filter:alpha(opacity=70); -moz-opacity:0.7; opacity:0.7; position:absolute; visibility:hidden;"></div>\
        <div id="frmmask" style="z-index:9999; display:block; background:#000; filter:alpha(opacity=0); -moz-opacity:0; opacity:0; position:absolute; visibility:hidden;"></div>\
        <div id="frmDialog" style="background-color:#FFFFFF; vertical-align:middle;height:240px; width:300px;z-index: 10010; position: absolute; display: block; visibility: hidden;">\
        <div class="floatinga" onmouseover="divMove(this,460,260)"><div class="floatingac"></div><div class="floatingaa">发送消息</div><div class="floatingab" onclick="closeForm()" title="关闭"></div></div>\
        <div class="floatingb" style="width:416px;padding-left:50px;font-size:14px;color:#999999;">你选择了给<span style="color:#41A2D6;">' + friend + '</span>发送消息,请在下面输入你的消息内容.</div>\
	    <div class="floatingb"><div class="floatingba" style="padding-left:50px;width:60px;text-align:left;">消息内容:&nbsp;&nbsp;</div><div class="floatingbc" style="height:120px;"><textarea type="text" name="message" id="message" style="width:278px;height:118px;border:1px solid #D9E0F8;font-size:14px;color:#666666;"></textarea></div></div>\
	    <div class="floatingc"><input type="image" src="http://userim.qupan.com/Img/icons_20_1.gif" onclick="javascript:doSend(\'' + friend + '\');" style="cursor:hand; cursor:pointer" width="50" height="22" /></div>';
            document.body.appendChild(obj);
            showMask(true, 460, 260);
        }
    }
}
function doSend(friend) {
    var message = myrep(document.getElementById("message").value);
    closeForm();
    if (message == "") {
        alert('请输入你的消息内容！');
        return false;
    } else if (message.len() > 1000) {
        alert('消息内容太长！');
        return false;
    } else {
        showMessage('发送消息');
        var url = "/UserCenter.aspx?Action=sendmessage&friend=" + escape(friend) + "&val=" + Math.random();
        var data = "message=" + escape(message);
        xmlHttp.open("POST", url, true);
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    var response = xmlHttp.responseText;
                    if (response == "ok") {
                        closeForm();
                        alert('消息已发出!');
                    } else if (response == "self") {
                        closeForm();
                        alert("不能给自己发送消息!");
                    } else {
                        closeForm();
                        alert("发送消息失败!");
                    }
                }
            }
        }
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-Length", data.length);
        xmlHttp.send(data);
    }
}
function addFriend(friend, user) {
    if (user == "") {
        alert('请登录后再添加好友!');
        return false;
    }else if (friend != "") {
        if (friend == user) {
            alert('不能添加自己为好友!');
            return false;
        } else {
            var obj = document.createElement("div");
            obj.id = "filter";
            obj.innerHTML = '<div id="dmask" style="z-index:10000; display:block; background:#000; filter:alpha(opacity=70); -moz-opacity:0.7; opacity:0.7; position:absolute; visibility:hidden;"></div>\
        <div id="frmmask" style="z-index:9999; display:block; background:#000; filter:alpha(opacity=0); -moz-opacity:0; opacity:0; position:absolute; visibility:hidden;"></div>\
        <div id="frmDialog" style="background-color:#FFFFFF; vertical-align:middle;height:240px; width:300px;z-index: 10010; position: absolute; display: block; visibility: hidden;">\
        <div class="floatinga" onmouseover="divMove(this,460,260)"><div class="floatingac"></div><div class="floatingaa">添加好友</div><div class="floatingab" onclick="closeForm()" title="关闭"></div></div>\
        <div class="floatingb" style="width:416px;padding-left:50px;font-size:14px;color:#999999;">你将添加<span style="color:#41A2D6;">' + friend + '</span>为你的好友,请在下面写上你的请求信息.</div>\
	    <div class="floatingb"><div class="floatingba" style="padding-left:50px;width:60px;text-align:left;">请求信息:&nbsp;&nbsp;</div><div class="floatingbc" style="height:120px;"><textarea type="text" name="message" id="message" style="width:278px;height:118px;border:1px solid #D9E0F8;font-size:14px;color:#666666;"></textarea></div></div>\
	    <div class="floatingc"><input type="image" src="http://userim.qupan.com/Img/icons_20_1.gif" onclick="javascript:doAdd(\'' + friend + '\');" style="cursor:hand; cursor:pointer" width="50" height="22" /></div>';
            document.body.appendChild(obj);
            showMask(true, 460, 260);
        }
    }
}
function doAdd(friend) {
    var message = myrep(document.getElementById("message").value);
    closeForm();
    if (message.len() > 1000) {
        alert('请求信息太长！');
        return false;
    } else {
        showMessage('申请中');
        var url = "/UserCenter.aspx?Action=addfriend&friend=" + escape(friend) + "&val=" + Math.random();
        var data = "message=" + escape(message);
        xmlHttp.open("POST", url, true);
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    var response = xmlHttp.responseText;
                    if (response == "ok") {
                        closeForm();
                        alert('请求已发出,请等待对方回复!');
                    } else if (response == "self") {
                        closeForm();
                        alert("不能添加自己为好友!");
                    } else if (response == "has") {
                        closeForm();
                        alert("该用户已在你的好友列表中或者你已发出好友请求正在等待用户回复!");
                    } else {
                        closeForm();
                        alert("添加好友失败!");
                    }
                }
            }
        }
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-Length", data.length);
        xmlHttp.send(data);
    }
}
function myrep(str) {
    str = str.replace(/\&/g, "&amp;");
    str = str.replace(/\>/g, "&gt;");
    str = str.replace(/\</g, "&lt;");
    str = str.replace(/\"/g, "&quot;");
    str = str.replace(/\'/g, "&acute;");
    return str;
}
String.prototype.len = function() {
    return this.replace(/[^\x00-\xff]/g, "rr").length;
}