
<script src="{PHPCMS_PATH}templates/default/message/xmlhttp.js" language="javascript" type="text/javascript"></script>
<table width="200" cellpadding="0" cellspacing="0" align="center">
  <tbody>
	<tr>
	  <td align="center"><a href="{$MOD[linkurl]}friends.php" target="_self"><strong><font color="#CC0000">我的好友</font></strong></a></td>
	</tr>
  </tbody>
</table>
<div style="width:200px;height:150px">
  <select name="select" size="10" id="frdlist" style="height:100%;width:100%;font-size:14px;margin-bottom:0px" onclick="activeDelBtn(this.value,this.selectedIndex);">

{if isset($friends)}
	{loop $friends $value}
		{if $value[1] == 0}

    <option value="{$value[0]}">{$value[0]}</option>

		{/if}
	{/loop}
{/if}

  </select>
</div>
<table width="200" cellpadding="0" cellspacing="0" class="mar_5">
  <tbody>
	<tr>
		<td align="center"><input id="username" type="text" value="输入会员用户名" style="width:100%" onclick="this.value='';"><br><input type="button" value="添加好友" onclick="addFriend();"><input id="delBtn" type="button" value="删除好友" disabled onclick="delFriend();"></td>
	</tr>
</tbody></table>
<script language="javascript" type="text/javascript">
<!--
var objText = document.getElementById("username");
var objSelect = document.getElementById("frdlist");
var strUser,intIndex;
function getResponse() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			var responseText = xmlHttp.responseText;
			switch (responseText) {
				case "0":
					alert("操作失败，请稍候重试！");
					break;
				case "1":
					alert(strUser+"已经存在！");
					objText.value = "";
					strUser = "";
					break;
				case "2":
					var newPos = objSelect.options.length;
					objSelect.options[newPos] = new Option(strUser,strUser);
					objSelect.options[newPos].selected;
					objText.value = "";
					strUser = "";
					break;
				case "3":
					var newPos = objSelect.options.length;
					objSelect.options[newPos] = new Option(strUser,strUser);
					objSelect.options[newPos].selected = true;
					objText.value = "";
					strUser = "";
					break;
				case "4":
					alert(strUser+"不是注册会员，不能添加为好友！");
					objText.value = "";
					strUser = "";
					break;
				case "5":
					objSelect.options[intIndex] = null;
					intIndex = null;
					document.getElementById("delBtn").disabled = true;
					break;
				case "6":
					alert("不能添加自己为好友！");
					objText.value = "";
					strUser = "";
			}
		}
	}
}
function sendRequest(action,username) {
	var strUrl = "{PHPCMS_PATH}{$mod}/friend.php?action="+action+"&username="+username;
	xmlHttp.open("GET",strUrl,true);
	xmlHttp.onreadystatechange = getResponse;
	xmlHttp.send(null);
}
function addFriend() {
	if (objText.value == "") {
		alert("请输入好友用户名!");
		objText.focus();
	} else if (/^\w+$/i.test(objText.value) == false) {
		alert("输入的用户名无效，请重新输入!");
		objText.value = "";
		objText.focus();
	} else {
		strUser = objText.value;
		sendRequest("add",strUser);
	}
}
function activeDelBtn(strFriend,intSelectedIndex) {
	intIndex = intSelectedIndex;
	if (document.getElementById("sendto")) {
		document.getElementById("sendto").value = strFriend;
	}
	document.getElementById("delBtn").disabled = false;
}
function delFriend() {
	var frdname = document.getElementById("frdlist").value;
	sendRequest("delete",frdname);
}
//-->
</script>
