/* Project leshou
*  Author: leshou AT gmail.com
*  File: /asset/js/leshou.js
*  Usage: Client side functions
*  Format: 1 tab ident(4 spaces), LF, no-BOM
*
*/

var getObj = function(objId) {
  return document.all ? document.all[objId] : document.getElementById(objId);
}

var http_request = false; // XMLHttpRequest对象
var http_request_id = 0;  // 需要改变状态对象的ID号
var curr_id_value = 1; //当前id号

/** 
  * 初始化XMLHttpRequest对象并进行具体操作
  */
function makeRequest(url, requestid, ac) {
  http_request = false;
  http_request_id = requestid;

  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }

  if (!http_request) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
  }
  with(http_request){
    try{
      http_request.open('GET', url, true);
      http_request.onreadystatechange = ac;
      http_request.send(null);
    } catch(e){}
  }
}

/** 
 * 初始化XMLHttpRequest对象并进行具体操作（post方式）
 */
function makePostRequest(tourl, params, act) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }

  if (!http_request) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
  }
  with(http_request){
    try{
      http_request.onreadystatechange = act;
      http_request.open('POST', tourl, true);
      http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
      http_request.send(encodeURI(params));
    } catch(e){}
  }
}

/**
* 修改关键词
*/
var edittag = function(tname, act, tid, oname) {
	tourl = "/" + oname + "/tagmanger/edit," + tid;
  params = "act=" + act + "&oname=" + oname + "&tid=" + tid + "&tagname=" + tname;
  makePostRequest(tourl, params, genericStatusChanges);
}

/**
* 一般性状态操作
*/
var genericStatusChanges = function() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
    //if (http_request.getResponseHeader('CloseCommendForm')) { // 发送窗口关闭
    //  var thisDiv=document.getElementById("recommend");
    //  thisDiv.style.display = "none";
    //}
    if (http_request.responseText == 'ok') {
      location.reload();
    }
    objStat = getObj("error_line");
    objStat.style.display = "";
    objStat.innerHTML = http_request.responseText;
    setTimeout("objStat.style.display = 'none'", 3000);
    //objStat.style.visibility = "visible";
  }
}

/**
  * 检查用户名是否可用
  */
var checkUserName = function(checkid, output) {
  uname  = getObj(checkid).value;
  putmsg = getObj(output);
  tourl = "/register?act=check&uname=" + uname;
  if (uname == '') {
    putmsg.innerHTML = '<font color="#ff0000">你不会让我检查空用户名吧？:)</font>';
  } else {
    makeRequest(tourl, output, putMessage);
  }
}

/**
  * 检查用户名
  */
var putMessage = function() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
    objFav = getObj(http_request_id);
    objFav.innerHTML = http_request.responseText;
  }
}

/**
  * 交换tags
  */
var swaptags = function(tag, hittag) {
  objFav = getObj('nowtags');
  tagid  = getObj(hittag);
  if (searchKey(objFav.value, tag) > 0) {
    objFav.value = objFav.value.replace(tag + ',', '');
    newcode = '<a href="javascript://" class="post-currtags" onclick="javascript: swaptags(\'' + tag + '\', \'' + hittag + '\')">' + tag + '</a>';
    tagid.innerHTML = newcode;
  } else {
    objFav.value = objFav.value + tag + ',';
    newcode = '<a href="javascript://" class="post-currtags" onclick="javascript: swaptags(\'' + tag + '\', \'' + hittag + '\')" style="background: #0066CC; color: #FFF">' + tag + '</a>';
    tagid.innerHTML = newcode;
  }
}

/**
  * 查找一个关键词是否已经存在清单中
  */
var searchKey = function(objvalue, tag) {
  var r = 0;
  var str = objvalue;
  var matchkey  = new RegExp("^"+tag);
  var matchkey1 = new RegExp(","+tag+",","i");
  var matchkey2 = new RegExp(tag+"$","i");

  if (matchkey.exec(str)) {
    r = 1;
    return r;
  } else if (matchkey1.exec(str)) {
    r = 1;
    return r;
  } else if (matchkey2.exec(str)) {
    r = 1;
    return r;
  } else {
    return r;
  }
}

/**
  * 交换title
  */
var swaptitle = function(btitle, hittitle) {
  objFav = getObj('nowtitle');
  tagid  = getObj(hittitle);
  if (objFav.value.indexOf(btitle) >= 0) {
    objFav.value = objFav.value.replace(btitle, '');
    newcode = '<a href="javascript://" class="post-currtags" onclick="javascript: swaptitle(\'' + btitle + '\', \'' + hittitle + '\')">' + btitle + '</a>';
    tagid.innerHTML = newcode;
  } else {
    objFav.value = objFav.value + btitle;
    newcode = '<a href="javascript://" class="post-currtags" onclick="javascript: swaptitle(\'' + btitle + '\', \'' + hittitle + '\')" style="background: #0066CC; color: #FFF">' + btitle + '</a>';
    tagid.innerHTML = newcode;
  }
}

/**
  * 删除一个收藏
  */
var DeleteBmark = function(bmarkid, uname, stationid) {
  objFav = getObj('station' + stationid);
  newcode = '<em>确定删除：</em><a href="javascript://" onclick="javascript: DoDeleteBmark(\'' + bmarkid + '\', \'' + uname + '\', \'' + stationid + '\')" class="shou_it">是</a><em>/</em><a href="javascript://" onclick="javascript: NoDeleteBmark(\'' + bmarkid + '\', \'' + uname + '\', \'' + stationid + '\')" class="shou_it">否</a>';
  objFav.innerHTML = newcode;
}

/**
  * 确定删除一个收藏
  */
var DoDeleteBmark = function(bmarkid, uname, stationid) {
  showStat = getObj('station' + stationid);
  url = "/user/action?act=delebmark&bid=" + bmarkid + "&oname=" + uname;
  makeRequest(url, stationid, DelBookmarkAction);
}

/**
  * 中止删除动作
  */
var NoDeleteBmark = function(bmarkid, uname, stationid) {
  showStat = getObj('station' + stationid);
  showStat.innerHTML = '<a href="javascript://" onclick="DeleteBmark(\'' + bmarkid + '\', \'' + uname + '\',\'' + stationid + '\')" class="shou_it">删除</a><em>|</em><a href="/u/' + uname + '/edit/' + bmarkid + '" class="shou_it">编辑</a>';
}

/**
  * 执行删除动作
  */
var DelBookmarkAction = function() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
    objFav = getObj("enclosure" + http_request_id);
    showStat = getObj("station" + http_request_id);
    returnMsg = http_request.responseText;
    if (returnMsg) {
      showStat.innerHTML = returnMsg;
    } else {
      //objFav.style.background = '#C0C0FF';
      objFav.style.background = '#E7E7FF';
      setTimeout("objFav.style.display = 'none'", 1000);
    }
  }
}

/**
  * 加为好友
  */
var addFriend = function(oname, fname, station) {
  url = "/user/action?act=addfriend&fname=" + fname + "&oname=" + oname;
  makeRequest(url, station, AddFriendAction);
}

/**
  * 再次为好友
  */
var andAddFriend = function(oname, fname, station) {
  url = "/user/action?act=andaddfriend&fname=" + fname + "&oname=" + oname;
  makeRequest(url, station, CurrencyFriendAction);
}

/**
  * 删除好友
  */
var deleteFriend = function(oname, fname, stationid) {
  url = "/user/action?act=deletefriend&fname=" + fname + "&oname=" + oname;
  makeRequest(url, stationid, DelFriendAction);
}

/**
  * 批准好友
  */
var agreeFriend = function(oname, fname, stationid) {
  url = "/user/action?act=agreefriend&fname=" + fname + "&oname=" + oname;
  makeRequest(url, stationid, CurrencyFriendAction);
}

/**
  * 拒绝好友
  */
var passFriend = function(oname, fname, stationid) {
  url = "/user/action?act=passfriend&fname=" + fname + "&oname=" + oname;
  makeRequest(url, stationid, CurrencyFriendAction);
}

/**
  * 关注一个收藏家
  */
var attentionUser = function(fname, stationid) {
  url = "/user/action?act=sub&fname=" + fname + "&oname=" + fname;
  makeRequest(url, stationid, CurrencyAttentionAction);
}

/** 
  * 执行加为好友动作
  */
var AddFriendAction = function() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
    objFav = getObj(http_request_id);
    returnMsg = http_request.responseText;
    objFav.innerHTML = returnMsg;
    setTimeout("objFav.style.display = 'none'", 8000);
  }
}

/** 
  * 执行再次加为好友动作
  */
var CurrencyFriendAction = function() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
    objFav = getObj(http_request_id);
    returnMsg = http_request.responseText;
    objFav.innerHTML = returnMsg;
  }
}

/**
  * 执行关注动作
  */
var CurrencyAttentionAction = function() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
    objFav = getObj(http_request_id);
    returnMsg = http_request.responseText;
    objFav.innerHTML = returnMsg;
    setTimeout("objFav.style.display = 'none'", 8000);
  }
}

/** 
  * 执行删除好友动作
  */
var DelFriendAction = function() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
    objFav = getObj("friend" + http_request_id);
    showStat = getObj("action-friend" + http_request_id);
    returnMsg = http_request.responseText;
    if (returnMsg) {
      showStat.innerHTML = returnMsg;
    } else {
      //objFav.style.background = '#C0C0FF';
      objFav.style.background = '#E7E7FF';
      setTimeout("objFav.style.display = 'none'", 1000);
    }
  }
}

/**
  * 删除站内短信
  */
var deleteMessage = function(fname, mid, ltype, stationid) {
  url = "/user/action?act=delmsg&oname=" + fname + "&mid=" + mid + "&ltype=" + ltype;
  makeRequest(url, stationid, DelMessageAction);
}

/** 
  * 执行删除短信动作
  */
var DelMessageAction = function() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
    objFav = getObj("message" + http_request_id);
    showStat = getObj("action-message" + http_request_id);
    returnMsg = http_request.responseText;
    if (returnMsg) {
      showStat.innerHTML = returnMsg;
    } else {
      //objFav.style.background = '#C0C0FF';
      objFav.style.background = '#E7E7FF';
      setTimeout("objFav.style.display = 'none'", 1000);
    }
  }
}

/**
  * 确定页面初始输入框
  */
var initFocus = function(fs) {
  fs.focus();
  return;
}

/**
  * 显示或隐藏
  */
function showOtherUsingTag() {
  var thisDiv=document.getElementById('ou-tag');
  var thisButton=document.getElementById('oum');
  if (thisDiv.style.display == "none") {
    thisDiv.style.display = "";
    thisButton.innerHTML="关闭...";
  }else{
    thisDiv.style.display = "none";
    thisButton.innerHTML="更多...";
  }
}

/**
 * 关闭
 */
function closeMoreTags() {
	var thisDiv=document.getElementById('all-sect');
  var thisButton=document.getElementById('sect-tags');
  thisDiv.style.display = "none";
  thisButton.style.display = "none";
}
 
/*
 * 分类下的关键词
 */
function secttags(sid) {
  url	= "/post/secttag/" + sid;
  stationid = "sect-tags";
  makeRequest(url, stationid, showAllTags);
}
 
/**
  * 更多关键词
  */
function showAllUsingTag() {
  url = "/post/alltag";
  stationid = "all-sect";
  makeRequest(url, stationid, showAllTags);
}

/** 
  * 显示所有关键词
  */
function showAllTags() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
    thisDiv = getObj(http_request_id);
    returnMsg = http_request.responseText;
    thisDiv.style.display="";
    thisDiv.innerHTML=returnMsg;
  }
}

/**
 * 显示/关闭信息块
 */
function showHideMessage(mid) {
  var thisDiv=document.getElementById(mid);
  if (thisDiv.style.display == 'none') {
    thisDiv.style.display = "";
  } else {
  	thisDiv.style.display = "none";
  }
}

/**
 * 显示信息块
 */
function showMessage(mid) {
  var thisDiv=document.getElementById(mid);
  thisDiv.style.display = "";
}

/**
 * 隐藏信息块
 */
function hideMessage(mid) {
  var thisDiv=document.getElementById(mid);
  thisDiv.style.display = "none";
}

/**
 * 删除分类
 */
function delSection(sid, oname) {
  url = "/user/action?act=delsection&bid=" + sid + "&oname=" + oname;
  makeRequest(url, sid, DelSectionAction);
}

/** 
  * 执行删除分类动作
  */
var DelSectionAction = function() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
    objFav = getObj("section" + http_request_id);
    returnMsg = http_request.responseText;
    if (returnMsg) {
      alert(returnMsg);
    } else {
      //objFav.style.background = '#C0C0FF';
      objFav.style.background = '#E7E7FF';
      setTimeout("objFav.style.display = 'none'", 1000);
    }
  }
}

/**
 * 分类置顶
 */
function upToTop(sid, oname) {
  url = "/user/action?act=upsection&bid=" + sid + "&oname=" + oname;
  makeRequest(url, sid, upSectionAction);
}

/** 
  * 执行分类置顶动作
  */
var upSectionAction = function() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
    objFav = getObj("section" + http_request_id);
    returnMsg = http_request.responseText;
    if (returnMsg) {
      alert(returnMsg);
    } else {
      var oldid = http_request.getResponseHeader("specialActionId");
      var firstNode = document.getElementById("section" + oldid);
      var newNode = document.createElement('div');
      newNode.id = 'section' + http_request_id;
      newNode.style.border = "solid 1px #D5D5D5";
      newNode.innerHTML = objFav.innerHTML;
      document.getElementById('user-bookmark-lists').insertBefore(newNode,firstNode);
      objFav.style.display = 'none';
    }
  }
}

/**
 * 关键词展开/关闭
 */
var showSectTags = function(sid, oname) {
  url = "/user/sess?act=settagshow&sid=" + sid + "&oname=" + oname;
  makeRequest(url, sid, showSectTagsAction);
}

/** 
  * 执行关键词展开/关闭
  */
var showSectTagsAction = function() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
    objFav = getObj("s-tag-" + http_request_id);
    thisDiv = getObj("s-" + http_request_id);
    if (objFav.style.display == "none") {
      objFav.style.display='';
      thisDiv.innerHTML = '<img src="/asset/images/users/closesect.gif" />';
    } else {
      objFav.style.display="none";
      thisDiv.innerHTML = '<img src="/asset/images/users/opensect.gif" />';
    }
  }
}

/**
 * 等级
 */
var rater = function(bid, values, oname) {
  url = "/user/action?act=rater&bid=" + bid + "&oname=" + oname + "&va=" + values;
  makeRequest(url, bid, updateBookmarkOrder);
}

var updateBookmarkOrder = function() {
  if ((http_request.readyState == 4) && (http_request.status == 200)) {
    objFav = getObj("vote" + http_request_id);
    voteValues = http_request.responseText;
    objFav.style.width=voteValues+"px";
    if (voteValues == 0) {
      thisDiv = getObj('unit_ulid21' + http_request_id);
      thisDiv.style.width='75px';
      showDiv = getObj('raterdiv' + http_request_id);
      showDiv.style.display='none';
    } else {
      thisDiv = getObj('unit_ulid21' + http_request_id);
      thisDiv.style.width='90px';
      showDiv = getObj('raterdiv' + http_request_id);
      showDiv.style.display='';
    }
  }
}

/**
* 发送信息
*/
var sendmsgto = function(rname, fixid) {
	thisDiv = getObj(fixid);
	thisDiv.value=rname;
}

/**
 * 用户显示设置
 */
var setRow = function(row, showid, oname) {
	url = "/" + oname + "/row/" + row;
  makeRequest(url, showid, updateUserShowSet);
  thisDiv = getObj(showid);
  thisDiv.innerHTML="处理中";
}

var setOrder = function(otype, showid, oname) {
	url = "/" + oname + "/order/" + otype;
  makeRequest(url, showid, updateUserShowSet);
  thisDiv = getObj(showid);
  thisDiv.innerHTML="处理中";
}

var setRefresh = function(oname, showid, tag) {
	url = "/" + oname + "/tags/refresh/" + tag;
	makeRequest(url, showid, updateUserShowSet);
}


var updateUserShowSet = function() {
	if ((http_request.readyState == 4) && (http_request.status == 200)) {
		location.reload();
	}
}

var gotopage = function(bmarkid, page) {
	url = "/url/posts/" + bmarkid + "/?page=" + page + '&type=1';
	showid = '';
	makeRequest(url, showid, gotoSpecPage);
}

var gotoSpecPage = function() {
	if ((http_request.readyState == 4) && (http_request.status == 200)) {
		listDiv  = getObj('user_list');
		listDiv.innerHTML = http_request.responseText;
	}
}

