
/**
*	该脚本用于实现通用的评论内容加载/显示/操作等功能
* @name					:	chinesesavvy common comment util javascript
* @createdTime 			: 	2007-01-12 9:11
* @author				: 	shixin
* @using				:	nothing
*/

/****************************************************************************************/


var _refer = document.location.hostname;

var chinesesavvy_comment_server = "app.chinesesavvy.com";

var chinesesavvy_comment_loadurl = "http://"+chinesesavvy_comment_server+"/services/comment/LoadCommentDataPO.ot?resType=#resType#&resId=#resId#&pageNum=#pageNum#&perPage=#perPage#&ts=#ts#";
	
var chinesesavvy_comment_counturl = "http://"+chinesesavvy_comment_server+"/services/comment/LoadCommentCountDataPO.ot?resType=#resType#&resIds=#resIds#";
	
var chinesesavvy_comment_submiturl = "http://"+chinesesavvy_comment_server+"/services/comment/AddCommentPO.ot";
	
var chinesesavvy_comment_submitreplyurl = "http://"+chinesesavvy_comment_server+"/services/comment/LoadCommentFormPO.ot?resType=#resType#&resId=#resId#&cmtId=#cmtId#&cmtType=#cmtType#&cmtTypes=#cmtTypes#&refer="+_refer;


var chinesesavvy_country_image_path = "http://"+chinesesavvy_comment_server+"/services/comment/media/images/country/";

var chinesesavvy_comment_typer_icon_path = "http://"+chinesesavvy_comment_server+"/services/comment/media/images/cmttyper/#cmtType#.gif";

var chinesesavvy_comment_script_url = "http://"+chinesesavvy_comment_server+"/services/comment/LoadCommentScriptPO.ot";

var chinesesavvy_comment_manage_url = "http://"+chinesesavvy_comment_server+"//services/comment/ManageCommentPO.ot?event=Manage&action=#action#&cmtId=#cmtId#";

var chinesesavvy_comment_typers = 
{
	"typers":[
		{
			"typeId":"common", 
			"typeName":"评论",
			"typeIcon":"",
			"typeEnglishName":"common comment", 
			"typeToolTile":"Reply"
		},
		{
			"typeId":"translate",
			"typeName":"翻译",
			"typeIcon":"",
			"typeEnglishName":"translate comment",
			"typeToolTile":"Translate"
		}
	]
};

/** dynamic load script */
function chinesesavvy_doPost(_postUrl) {
	var _utilScript = null;
	var _number = Math.random();
	_utilScript = document.createElement("script");
	_utilScript.setAttribute("type", "text/javascript");
	_utilScript.setAttribute("language", "javascript");
	_utilScript.setAttribute("src", _postUrl);
	_utilScript.setAttribute("id", "chinesesavvy_comment_" + _number);
	document.getElementsByTagName("head").item(0).appendChild(_utilScript);
}

function chinesesavvy_linkstyle(_stylesheetUrl){
	var _number = "1_0";//Math.random();
	var _utilScript = document.getElementById("chinesesavvy_comment_style_" + _number);
	if(_utilScript==null){
		_utilScript = document.createElement("link");
		_utilScript.setAttribute("type", "text/css");
		_utilScript.setAttribute("rel", "stylesheet");
		_utilScript.setAttribute("href", _stylesheetUrl);
		_utilScript.setAttribute("id", "chinesesavvy_comment_style_" + _number);
		document.getElementsByTagName("head").item(0).appendChild(_utilScript);
	}
}


chinesesavvy_doPost(chinesesavvy_comment_script_url);

/******************** comment data ******************************************************/
// get country icon
function cs_getCountryIcon(_shortName){
	return chinesesavvy_country_image_path + _shortName + ".gif";
}


var chinesesavvy_CommentContents = 
{
	"cscommentInfo":
		{
			"resType":"art", 
			"resId":"1000", 
			"cmtPageIndex":1, 
			"cmtCount":100, 
			"cmtPages":20, 
			"cmtPerPage":5
		}, 
	"cscomments":[
		{
			"cmtId":352, 
			"resId":"136679", 
			"resType":"art", 
			"cmtType":"cmt", 
			"cmtOrdering":1, 
			"submitTime":"2007-01-12 09:32:53", 
			"userName":"curry", 
			"submitIp":"218.240.*.*", 
			"locationId":100000, 
			"locationInfo":"china|beijing|china", 
			"yesCount":0, 
			"noCount":0, 
			"nullCount":0, 
			"content":"asdfas"
		}
	]
};
/******************** comment count ******************************************************/
var chinesesavvy_CommentCoount = {"counts":[{"resId":352, "resType":"art", "cmtCount":3}]};
/*****************************************************************************************/
/**
* common comment class
*/
function chinesesavvy_comment(res_type, res_id, node_id,_perPage,_cmtTypes) {
	/** comment content */
	this.m_comments = null;
	/** current comment page index */
	this.m_cpageindex = 0;
	/** resource id */
	this.m_res_id = res_id;
	/** resource type , default is 'art' */
	this.m_res_type = (res_type ? res_type : "art");
	/** render dom node */
	this.m_render_node_id = node_id;
	/** count of comment per page */
	this.m_perPage = (_perPage ? _perPage : 10);
	
	this.m_cmt_types = (_cmtTypes!=null ? _cmtTypes : "common" );
}
/**
* common comment toolbox
*/
function chinesesavvy_comment_toolbox() {
	/** comment array  */
	this.m_cmts = new Array();
	/** the url whitch contains comment's data  */
	//this.m_comment_loadurl = "http://test.chinesesavvy.com/services/comment/LoadCommentDataPO.ot?resType=#resType#&resId=#resId#&pageNum=#pageNum#&perPage=#perPage#&ts=#ts#";
	//this.m_comment_loadurl = "js/test_comments.js?resType=#resType#&resId=#resId#&pageNum=#pageNum#&perPage=#perPage#&ts=#ts#";
	this.m_comment_loadurl = chinesesavvy_comment_loadurl; //"http://localhost/services/comment/LoadCommentDataPO.ot?resType=#resType#&resId=#resId#&pageNum=#pageNum#&perPage=#perPage#&ts=#ts#";
	
	this.m_comment_counturl = chinesesavvy_comment_counturl; //"http://test.chinesesavvy.com/services/comment/LoadCommentCountDataPO.ot?resType=#resType#&resIds=#resIds#";
	//this.m_comment_submiturl = "http://test.chinesesavvy.com/services/comment/AddCommentPO.ot";
	
	this.m_comment_submiturl = chinesesavvy_comment_submiturl; //"http://localhost/services/comment/AddCommentPO.ot";
	
	this.m_comment_submitreplyurl = chinesesavvy_comment_submitreplyurl; //"http://localhost/services/comment/LoadCommentFormPO.ot?resType=#resType#&resId=#resId#&cmtId=#cmtId#";
	
	
	
	this.m_comment_timeout = 2500;
	
	this.m_comment_default_perpage = 10;
	
	this.cs_comment_timeoutFlag = null;
	
	this.cs_last_submit_res_id = null;
	
	this.cs_last_submit_res_type = null;
	
	this.getCommentKey = function (_resType, _resId) {
		return ("" + _resType + _resId);
	};
	this.getSubmitUrl = function () {
		return this.m_comment_submiturl;
	};
	this.setComment = function (_cmt) {
		var _key = this.getCommentKey(_cmt.m_res_type, _cmt.m_res_id);
		//alert("setComment : " + _key);
		this.m_cmts[_key] = _cmt;
	};
	this.getComment = function (_cmtKey) {
		return this.m_cmts[_cmtKey];
	};
	
	
	
	
	/**
	* 加载评论数据
	*/
	this.loadCommentsData = function (_resType, _resId, _pageNum, _comments) {
		//alert("loadCommentsData");
		var _key = this.getCommentKey(_resType, _resId);
		//alert("KEY => " + _key);
		//alert("this.getComment(_key) => "+this.getComment(_key));
		this.getComment(_key).m_comments = _comments;
		this.getComment(_key).m_cpageindex = _pageNum;
		this.renderCommentData(_resType, _resId);
		if (eval("window.on_chinesesavvy_comment_data_loaded") != null) {
			eval("window.on_chinesesavvy_comment_data_loaded")(_resType, _resId, _pageNum, _comments);
		}
	};
	/**
	* 呈现评论
	*/
	this.renderCommentData = function (_resType, _resId) {
		//alert("renderCommentData");
		var _key = this.getCommentKey(_resType, _resId);
		var _cmts = this.getComment(_key).m_comments;
		var _cmtTypes = this.getComment(_key).m_cmt_types;
		var _isMgr = chinesesavvy_isManager();		
		
		chinesesavvy_linkstyle("http://" + chinesesavvy_comment_server + "/services/comment/media/css/comments_only.css");
		//chinesesavvy_linkstyle("http://" + chinesesavvy_comment_server + "/services/csweb/media/style20070808/css/comments_only.css");
		
		var _s = '';
		// 时间标识
		_s += '<span class="left comm_smllTxt">';
		if(_cmts.cscomments.length>0){
			_s += 'All times are displayed in GMT +0800';
		}else{
			_s += 'No comments are here yet. Be the first and leave a comment.';
		}
		_s += '</span>';
		
		
		// 表头
		_s += '<table width="100%" border="0" cellspacing="0" cellpadding="0" class="posts">';
		
		// 评论条目		
		for(var i=0,n=_cmts.cscomments.length;i<n;i++){
			var _loc = _cmts.cscomments[i].locationInfo.split("|");
			var _shortName = (_loc!=null&&_loc.length>=3?_loc[2]:"00").toLowerCase();
			var _locInfo = "unknown";
			if(_loc!=null&&_loc.length>=3){
				_locInfo = _loc[0] + " , " + _loc[1];
			}
			
			var _cmtTypeInfo = "评论 , comment";
			var _cmtTypeObj = getCommentTyper(_cmts.cscomments[i].cmtType);
			if(_cmtTypeInfo!=null){
				_cmtTypeInfo = _cmtTypeObj.typeName + " , " + _cmtTypeObj.typeEnglishName;
			}
			
			var _userAvatar = _cmts.cscomments[i].userAvatar;
			if(_userAvatar==""){
				//_userAvatar = 'http://' + chinesesavvy_comment_server + '/services/comment/media/images/img_usravatar_40.gif';
				_userAvatar = "http://";
			}
			if(_userAvatar.indexOf("http://")==-1){
				_userAvatar = 'http://forums.chinesesavvy.com/images/avatars/gallery/' + _userAvatar;
			}
			
			var _userProfileLink = 'javascript:;';
			if(_cmts.cscomments[i].userId>0){
				_userProfileLink = 'http://forums.chinesesavvy.com/profile.php?mode=viewprofile&u=' + _cmts.cscomments[i].userId;
			}
			
			
			_s += '<tr class="comm_headrow">';
			_s += '	<td class="col01"><a href="javascript:;">#'+_cmts.cscomments[i].cmtOrdering+'</a></td>';
			_s += '	<td class="col02" colspan="2">'+ _cmts.cscomments[i].submitTime + (_isMgr?' <a href="javascript:;" onclick="return(chinesesavvy_cmt_deleteComment('+_cmts.cscomments[i].cmtId+',this))">delete</a>':'') +'</td>';
			_s += '</tr>';
			_s += '<tr class="comm_midrow">';
			_s += '	<td class="col01"><a href="'+_userProfileLink+'" target="_blank" class="comm_username">'+this._getRealUsername(_cmts.cscomments[i].userName,_cmts.cscomments[i].cmtOrdering)+'</a><br />';
			if(_userAvatar!="http://"){
				_s += '		<a href="'+_userProfileLink+'" target="_blank" class="comm_username"><img src="'+_userAvatar+'" width="40" alt="'+this._getRealUsername(_cmts.cscomments[i].userName,_cmts.cscomments[i].cmtOrdering)+' avatar" /></a>';
			}
			_s += ' </td>';
			_s += '	<td class="col02" colspan="2">';
			_s += '		<p>' + this._convertCommentContent(_cmts,i) + '</p>';
			_s += '	</td>';
			_s += '</tr>';
			_s += '<tr class="comm_footrow">';
			_s += '	<td class="col01">'+this._getCommentReplyTools(_cmts,i,_cmtTypes)+'</td>';
			_s += '	<td class="col02">'+ this._getCommentReplayTranslateTools(_cmts,i,_cmtTypes) +'</td>';
			_s += '</tr>';
			
			if(i<n-1){
				_s += '<tr class="comm_filler"><td>&nbsp;</td></tr>';
			}
			
		}
		if(_cmts.cscomments.length>0){
			_s += this._getNavigator(_cmts.cscommentInfo.resType,_cmts.cscommentInfo.resId);
		}
		_s += '</table><a href="#" id="cs_submit_box_pos" name="cs_submit_box_pos"></a>';
		
		_s += this._getCommentSubmitBox(_cmts.cscommentInfo.resType,_cmts.cscommentInfo.resId);
        
		
		document.getElementById(this.getComment(_key).m_render_node_id).innerHTML = _s;
	};
	
	this._convertCommentContent = function(_cmts,_cmtIdx){
		var _content = _cmts.cscomments[_cmtIdx].content;

		return _content;
	}
	
	this._getRealUsername = function(_name,_ordering){
		if(_name=="SavvyGuest" || _name == "chinesesavvier"){
			return "SavvyGuest" + (("000"+_ordering).substring((""+_ordering).length));
		}
		return _name;
	}
	
	this._getCommentReplyTools = function(_cmts,_index,_cmtTypes){
		
		var _s = '<a href="#cs_submit_box_pos" class="btnReply">Reply to this comment</a><a href="javascript:;" onclick="return showCommentReplyBox(\''+_cmts.cscommentInfo.resType+'\',\''+_cmts.cscommentInfo.resId+'\',\''+_cmts.cscomments[_index].cmtId+'\',\'common\');" class="btnQuote">Quote this comment</a>';
		return _s;
	}
	
	this._getCommentReplayTranslateTools = function(_cmts,_index,_cmtTypes){
		var _s = '<a href="javascript:;" onclick="return showCommentReplyBox(\''+_cmts.cscommentInfo.resType+'\',\''+_cmts.cscommentInfo.resId+'\',\''+_cmts.cscomments[_index].cmtId+'\',\'translate\',\'cs_cmt_trn=en\');" class="translENG">English</a><a href="javascript:;" onclick="return showCommentReplyBox(\''+_cmts.cscommentInfo.resType+'\',\''+_cmts.cscommentInfo.resId+'\',\''+_cmts.cscomments[_index].cmtId+'\',\'translate\',\'cs_cmt_trn=cn\');" class="translZH">中文</a>';
		return _s;
	}
	
	this._getCommentSubmitBox = function(_resType, _resId){
		/*
		var _s = "<form action=\""+this.m_comment_submiturl+"\" method=\"post\" target=\"chinesesavvy_submit_comment_frame\">";
		_s += "<table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
		_s += "  <tr>";
		_s += "    <td width=\"80\" class='comment_common_text'>Author : </td>";
		_s += "    <td><input type=\"text\" name=\"userName\">";
		_s += "    <input name=\"cmtType\" type=\"hidden\"  value=\"common\">";
		_s += "    <input name=\"resType\" type=\"hidden\"  value=\""+_resType+"\">";
		_s += "    <input name=\"resId\" type=\"hidden\"  value=\""+_resId+"\"></td>";
		_s += "    <td width=\"100\" rowspan=\"3\" valign=\"top\">&nbsp;</td>";
		_s += "  </tr>";
		_s += "  <tr>";
		_s += "    <td width=\"80\" valign=\"top\"  class='comment_common_text'>Text : </td>";
		_s += "    <td><textarea name=\"cmtContent\" rows=\"10\" style=\"width:90%\"></textarea></td>";
		_s += "  </tr>";
		_s += "  <tr>";
		_s += "    <td width=\"10\">&nbsp;</td>";
		_s += "    <td><input type=\"button\" name=\"Submit\" value=\"submit\" onClick=\"chinesesavvy_comment_submit('"+_resType+"','"+_resId+"',this.form)\"><span id=\"chinesesavvy_comment_wait_"+this.getCommentKey(_resType,_resId)+"\"></span></td>";
		 _s += " </tr>";
		_s += "</table>";
		_s += "</form>";
		*/
		var _s = "";
		var _url = ChinesesavvyCommentToolbox.m_comment_submitreplyurl;
		_url = _url.replace("#resType#",_resType);
		_url = _url.replace("#resId#",_resId);
		_url = _url.replace("#cmtTypes#",ChinesesavvyCommentToolbox.getComment(ChinesesavvyCommentToolbox.getCommentKey(_resType,_resId)).m_cmt_types);
		_url = _url.replace("#cmtId#","0");
		_url = _url.replace("#cmtType#","common");
		_url += "&ts=" + Math.random();
		//alert(_url);
		//_s += "<iframe src=\""+_url+"\" style=\"width:100%\"></iframe>";
		_s += "<iframe id=\"cs_comment_submitbox_frame_"+this.getCommentKey(_resType,_resId)+"\" src=\""+ _url +"\" style=\"width:100%;height:290px;background-color:transparent;\" scrolling=\"no\" frameborder=\"0\" allowtransparency=\"true\" width=\"100%\" height=\"100%\"></iframe>";
		
		//_s += "<div style='width:100%;' align='right'><a style='text-align:right' href='#comment_top_"+this.getCommentKey(_resType,_resId)+"'>Back To Top</a></div>";
		return _s;
	}
	
	this._getNavigator = function (_resType, _resId) {
		var _key = this.getCommentKey(_resType, _resId);
		var _cmts = this.getComment(_key).m_comments;
		var _wcc = 0;
		var _wpc = 0;
		var _cpn = this.getComment(_key).m_cpageindex;
		if (_cmts != null) {
			_wcc = _cmts.cscommentInfo.cmtCount;
			_wpc = _cmts.cscommentInfo.cmtPages;
			if (_cpn == 0) {
				_cpn = _wpc;
			}
		}
		var _dp_cpn = (_wpc -_cpn + 1);
		var _range = 4;
		/*
		<tr class="comm_paging comm_smllTxt">
			<td class="col01">Page 2 of 6</td>
			<td class="col02"><a href="javascript:;">&laquo;</a> <a href="javascript:;">1</a> <a href="javascript:;" class="current">2</a> <a href="javascript:;">3</a> <a href="javascript:;">4</a> ... <a href="javascript:;">10</a> <a href="javascript:;">&raquo;</a></td>
		</tr>
		*/
		
		var _str = '<tr class="comm_paging comm_smllTxt">';
		_str += '<td class="col01">Page <span>' + _dp_cpn + '</span> of <span id=\"woaika_PagesOfComment_' + _key + '">' + _wpc + '</span></td>';
        _str += '<td class="col02">';
		
		if(_wpc>0){
			
			var _first_pn = _wpc;
			var _last_pn = 1;
			
			var _startPn = Math.max(_dp_cpn - _range, 1);
			var _endPn = Math.min(_dp_cpn + _range,_wpc);
			
			_str += '<a title="go to first page" href="javascript:ChinesesavvyCommentToolbox.gotoPage(\'' + _resType + '\',\'' + _resId + '\','+_first_pn+',null)">&laquo;</a>';
			
			if(_startPn>1){
				_str += ' <a href="javascript:ChinesesavvyCommentToolbox.gotoPage(\'' + _resType + '\',\'' + _resId + '\','+(_wpc -_startPn + 2)+',null)">...</a>';
			}
			for(var pp=_startPn;pp<=_endPn;pp++){
				var p = (_wpc -pp + 1);
				_str += ' <a title="goto page '+pp+'" '+(_cpn==p?'class="current"':'')+' href="javascript:ChinesesavvyCommentToolbox.gotoPage(\'' + _resType + '\',\'' + _resId + '\','+p+',null)">'+pp+'</a>';
			}
			if(_endPn<_wpc){
				_str += ' <a href="javascript:ChinesesavvyCommentToolbox.gotoPage(\'' + _resType + '\',\'' + _resId + '\','+(_wpc -_endPn)+',null)">...</a>';
			}
			/*
			for(var pp=1;pp<=_wpc;pp++){
				var p = (_wpc -pp + 1);
				_str += ' <a title="goto page '+pp+'" '+(_cpn==p?'class="current"':'')+' href="javascript:ChinesesavvyCommentToolbox.gotoPage(\'' + _resType + '\',\'' + _resId + '\','+p+',null)">'+pp+'</a>';
			}
			*/
			_str += ' <a title="go to last page" href="javascript:ChinesesavvyCommentToolbox.gotoPage(\'' + _resType + '\',\'' + _resId + '\','+_last_pn+',null)">&raquo;</a>';
		}

		_str += '</td></tr>';
		return _str;
	};
	/**
	* if call this function, it will refresh last comment after 2500ms
	*/
	this.afterSubmitComment = function (_resType, _resId) {
		this.cs_last_submit_res_type = _resType;
		this.cs_last_submit_res_id = _resId;
		if (this.cs_comment_timeoutFlag) {
			clearTimeout(this.cs_comment_timeoutFlag);
		}
		this.cs_comment_timeoutFlag = window.setTimeout(chinesesavvy_comment_refresh, 2500);
	};
	/** refresh last submit comment */
	this.refresh = function () {
		if (this.cs_last_submit_res_id) {
			this.gotoPage(this.cs_last_submit_res_type, this.cs_last_submit_res_id, 0);
		}
	};
	/**
	* load page
	*/
	this.gotoPage = function (_resType, _resId, _pageIndex, _perPage) {
		var _postUrl = this.m_comment_loadurl;
		_postUrl = _postUrl.replace("#resType#", _resType);
		_postUrl = _postUrl.replace("#resId#", _resId);
		_postUrl = _postUrl.replace("#ts#", Math.random());
		if (_pageIndex) {
			_postUrl = _postUrl.replace("#pageNum#", _pageIndex);
		} else {
			_postUrl = _postUrl.replace("#pageNum#", "0");
		}
		if (_perPage) {
			_postUrl = _postUrl.replace("#perPage#", _perPage);
		} else {
			_postUrl = _postUrl.replace("#perPage#", /*this.m_comment_default_perpage*/ this.getComment(this.getCommentKey(_resType, _resId)).m_perPage);
		}
		chinesesavvy_doPost(_postUrl);
	};

	/** callback  fetchCommentsCounts */
	this.loadCommentsCounts = function (_countData) {
		for (var i = 0; i < _countData.counts.length; i++) {
			//alert(_countData.counts[i].cmtId + " -> " + _countData.counts[i].cmtCount);
			var _fun = eval("window.cs_setCommentCount");
			if (_fun != null) {
				_fun(_countData.counts[i].resId, _countData.counts[i].resType, _countData.counts[i].cmtCount);
			}
		}
	};
	/** fetch comments count data */
	this.fetchCommentsCounts = function (_resType, _resIds) {
		var _url = this.m_comment_counturl;
		_url = _url.replace("#resType#", _resType);
		_url = _url.replace("#resIds#", _resIds);
		chinesesavvy_doPost(_url);
	};
	
	
	this.getCommentSubmitUrl = function () {
		return this.m_comment_submiturl;
	};
	
	this.waitCommentSubmit = function(_resType, _resId){
		this.cs_last_submit_res_type = _resType;
		this.cs_last_submit_res_id = _resId;
		if (this.cs_comment_timeoutFlag) {
			clearTimeout(this.cs_comment_timeoutFlag);
		}
		this.cs_comment_timeoutFlag = window.setTimeout(chinesesavvy_comment_refresh, this.m_comment_timeout);
	}
	
	
}

/** submit comment */
function chinesesavvy_comment_submit(_resType, _resId , _form){
	_form.submit();
	//ChinesesavvyCommentToolbox.waitCommentSubmit(_resType, _resId);
	_form.cmtContent.value = "";
	var _closeDialogFun = eval("window.hidePopWin");
	if(_closeDialogFun){
		_closeDialogFun();
	}
	var _w = document.getElementById("chinesesavvy_comment_wait_" + ChinesesavvyCommentToolbox.getCommentKey(_resType, _resId));
	if(_w){
		_w.innerHTML = " please wait , data sending ...";
	}
}

/** refresh last comment */
function chinesesavvy_comment_refresh() {
	ChinesesavvyCommentToolbox.refresh();
	if (eval("window.on_chinesesavvy_comment_refresh") != null) {
		eval("window.on_chinesesavvy_comment_refresh")();
	}
}
/** 创建默认的评论工具类 */
var ChinesesavvyCommentToolbox = new chinesesavvy_comment_toolbox();


/** setup comment into page **/
function setupChinesesavvyComment(_resType,_resId,_nodeId,_cmtTypes,_perPage){ 
	if(!_perPage){
		_perPage = ChinesesavvyCommentToolbox.m_comment_default_perpage;
	}
	if(!_cmtTypes){
		_cmtTypes = "common";
	}
	var _cmt = new chinesesavvy_comment(_resType,_resId,_nodeId,_perPage,_cmtTypes); 
	ChinesesavvyCommentToolbox.setComment(_cmt);
	ChinesesavvyCommentToolbox.gotoPage(_resType,_resId,0);
}

/** show comment reply box */
function showCommentReplyBox(_resType,_resId,_cmtId,_cmtType,_addArgs){
	var _url = ChinesesavvyCommentToolbox.m_comment_submitreplyurl;
	_url = _url.replace("#resType#",_resType);
	_url = _url.replace("#resId#",_resId);
	_url = _url.replace("#cmtId#",_cmtId);
	_url = _url.replace("#cmtTypes#",ChinesesavvyCommentToolbox.getComment(ChinesesavvyCommentToolbox.getCommentKey(_resType,_resId)).m_cmt_types);
	if(_cmtType){
		_url = _url.replace("#cmtType#",_cmtType);
	}else{
		_url = _url.replace("#cmtType#","");
	}
	if(_addArgs && _addArgs!=""){
		_url += "&" + _addArgs;
	}
	//alert(_url);
	
	document.getElementById("cs_comment_submitbox_frame_" + ChinesesavvyCommentToolbox.getCommentKey(_resType,_resId)).src = _url;
	
	try{
		document.getElementById("cs_comment_submitbox_frame_" + ChinesesavvyCommentToolbox.getCommentKey(_resType,_resId)).scrollIntoView();
	}catch(ex){}
	
	return false;
}

// comment submit callback
function on_comment_submit_callback(_ret,_msg,_resType,_resId){
	//close comment box dialog
	var _closeDialogFun = eval("window.hidePopWin");
	if(_closeDialogFun){
		_closeDialogFun();
	}
	//refresh comment list
	ChinesesavvyCommentToolbox.gotoPage(_resType,_resId,null,null);
}

// create comment submit iframe

try{
/*	if(_submitFrame==null){
		_submitFrame = document.createElement("iframe");
		_submitFrame.setAttribute("src", "about:blank");
		_submitFrame.setAttribute("name", "chinesesavvy_submit_comment_frame");
		_submitFrame.setAttribute("width", "0");
		_submitFrame.setAttribute("height", "0");
		_submitFrame.setAttribute("height", "display:none");
		_submitFrame.setAttribute("id", "chinesesavvy_submit_comment_frame");
		document.getElementsByTagName("head").item(0).appendChild(_submitFrame);
	}
*/
	var _submitFrame = document.getElementById("chinesesavvy_submit_comment_frame");
	if(_submitFrame==null){
		document.write("<iframe src='about:blank' name='chinesesavvy_submit_comment_frame' id='chinesesavvy_comment_submit_frame' width='0' height='0' style='display:none'></iframe>");
	}
/*
	var _replyDialog = document.getElementById("chinesesavvy_submit_comment_dialog");
	if(_replyDialog==null && false){
		var _s = "<div id=\"chinesesavvy_submit_comment_dialog\" style='display:none'>";
		_s += "<div id=\"popupMask\">&nbsp;</div>";
		_s += "<div id=\"popupContainer\">";
		_s += "	<div id=\"popupInner\">";
		_s += "		<div id=\"popupTitleBar\" style=\"cursor:move\" onMouseDown=\"catchTitleBar()\" onSelectStart=\"return false\">";
		_s += "			<div id=\"popupTitle\"></div>";
		_s += "			<div id=\"popupControls\">";
		_s += "				<img src=\"close.gif\" onclick=\"hidePopWin(false);\" />";
		_s += "			</div>";
		_s += "		</div>";
		_s += "		<iframe src=\"commentbox.html\" style=\"width:100%;height:100%;background-color:transparent;\" scrolling=\"auto\" frameborder=\"0\" allowtransparency=\"true\" id=\"popupFrame\" name=\"popupFrame\" width=\"100%\" height=\"100%\"></iframe>";
		_s += "	</div>";
		_s += "</div>";
		_s += "</div>";
		document.write(_s);
	}
	document.getElementById("chinesesavvy_submit_comment_dialog").style.display = "";
*/

}catch(ex){}

////////////////- comment typer -////////////////////////////////////////////////////


function getCommentTyper(_typerId) {
	for(var i=0,n=chinesesavvy_comment_typers.typers.length;i<n;i++){
		if(chinesesavvy_comment_typers.typers[i].typeId==_typerId){
			return chinesesavvy_comment_typers.typers[i];
		}
	}
	return null;
}

// install comment box by default setting.
function installCommentBoxDefault(_nodeId) {
	installCommentBox(_nodeId, null);
}

// install comment box .
function installCommentBox(_nodeId, _typerIds) {
	var _vtids = null;
	if (_typerIds != null) {
		_vtids = _typerIds;
	} else {
		_vtids = chinesesavvy_comment_default_typer_ids;
	}
	//var _tids = _vtids.split(",");
}

function getCommentTyperIcon(_cmtType){
	return getCommentTyper(_cmtType).typeIcon;
}

/////////////////////////////////////////////////////////////
// manage
/////////////////////////////////////////////////////////////

function chinesesavvy_cmt_deleteComment(_cmtId,_linkObj){
	if(confirm("Do you really want to delete this comment?")){
		try{
			var _url = chinesesavvy_comment_manage_url.replace("#action#","del").replace("#cmtId#",_cmtId);
			chinesesavvy_doPost(_url);
			_linkObj.innerHTML = "deleted!";
			_linkObj.onclick = function(){alert("deleted!");};
		}catch(ex){alert(ex);}
	}
	return false;
}

/////////////////////////////////////////////////////////////
// member check
/////////////////////////////////////////////////////////////
function chinesesavvy_isManager(){
	var _group = chinesesavvy_getCookie("CS_U_GROUP");
	return _group!=null && _group!="" && (_group.indexOf("manage")!=-1 || _group.indexOf("admin")!=-1);
}

/////////////////////////////////////////////////////////////
// cookie
/////////////////////////////////////////////////////////////
function chinesesavvy_getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

function chinesesavvy_setCookie(name, value) {
	var expdate = new Date();
	var argv = chinesesavvy_setCookie.arguments;
	var argc = chinesesavvy_setCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	if (expires != null) {
		expdate.setTime(expdate.getTime() + (expires * 1000));
	}
	document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expdate.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}

function chinesesavvy_delCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = chinesesavvy_getCookie(name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function chinesesavvy_getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return chinesesavvy_getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) {
			break;
		}
	}
	return null;
}


/////////////////////////////////////////////////////////////////////////////////////////////////
// guojia info
/////////////////////////////////////////////////////////////////////////////////////////////////
