function inittopicdocobj(objid, topicname, topicll, isLeaf, strTags, bLoadIntoReserved, bRefreshReCommend)
{
	topicll = topicll || "";
	isLeaf = isLeaf || 0;
	strTags = strTags || "";
	docobj = new topicobj("topic", objid, topicname, topicll, isLeaf, strTags);
	
	//2009.9.17 make sure fc is visible
	GFCShowOrNot(true);
	setTimeout("docobj.action('again', '"+bLoadIntoReserved+"', '"+bRefreshReCommend+"', 'true')",100);	
}

function displaytopic(topicid)
{
	var argv = displaytopic.arguments;
 	var argc = displaytopic.arguments.length; 	
 	
 	var bLoadIntoReserved = (argc > 1) ? argv[1] : "true";
 	var bRefreshReCommend = (argc > 2) ? argv[2] : "true";
 	var jtitle = (argc > 3) ? argv[3] : "";
 		
	xajax_showtopic("divcontent", topicid, 5, bLoadIntoReserved, bRefreshReCommend); //callback will also inital a javascript docobj for later used 2009.12.7
}

function loadtopictoreserved(topicid)
{
	if(topicid==-1)
		return;
		
	preparereservedlayer();
	
	xajax_loadinclude("reservedtopic", topicid, "topic.xslt");
	SetCookie("TopicTD", "reservedtopic");
}

//load topics while window loaded with query string, this function is called in loadtopics.php aftr topics first loaded 2007.10.19
function loadtopicsforquery()
{
	//load topics according the query string
	if(checktitlevalue())
	{
		var tv = document.getElementById("q").value;
		preparereservedlayer();
		xajax_loadtopicsforkeywords('reservedtopic', tv);
	}
}


// This function is must be implemented if you use the "code" attribute into folder elements.    // It is called every time you click on a folder
function selectFolder(folderCode, node) 
{
   	// Get the next tag (read the HTML source)
   	var nextDIV = node.nextSibling;
	// find the next DIV
   	while(nextDIV.nodeName != "DIV") 
		nextDIV = nextDIV.nextSibling;   
   
	var ar = folderCode.split(",");
	
	//redirect to topic page directly if in page.php
	if(document.location.href.indexOf("page_") != -1)
		document.location.href="/topic_"+ar[1];
	
	//correct display error by specified id of td, 2007.12.7; use cookie to record td's id 2007.12.14
	tdid = nextDIV.parentNode.id;
	SetCookie("TopicTD", tdid);
	
	//2009.8.29
	var jtitle = node.getAttribute("title");
			        	
	switch(ar[0])
        {
        	case "inc":
        		xajax_loadinclude(tdid, ar[1], "topic.xslt", "", 1);
        		
        		//break;
        	default:
			displaytopic(ar[1], "false", "true", jtitle);
        		break;	
	}
}

/* for topic area opetation, moved from content_comment.js on 2009.09.08 */

function CallbackUpdateTopic(topicid)
{
	//refresh the node
	//xajax_showtopic("divcontent", classid);
	displaytopic(topicid, "false", "partial")
}

function del_commentoftopic(classid, cotid)
{
	xajax_delcommentoftopic(classid, cotid);	
}

function submit_commentoftopic(classid, userid)
{
	var comment = document.getElementById("cotcomment").value;
	if(comment=="")
		return;
	xajax_savecommentoftopic(classid, userid, comment);	
	
}

function callback_topicnamerelated(topicid)
{
	//xajax_showtopic("divcontent", topicid);
	displaytopic(topicid, "false", "false")
}

function topicnamerelated(topicid)
{
	var btnr = document.getElementById("checktopicnamerelated").checked;
	xajax_topicnamerelatedchanged(topicid, (btnr)? 1:0);
}

function ontology_addsubtopic(classid)
{
	var newsubtopicname = "";
	newsubtopicname = prompt("請輸入要新建的子主題名稱：","");
	
	if((newsubtopicname != null)&&(newsubtopicname != ""))
	{
		
		if(newsubtopicname.length>64)
		{
			alert("名稱超過32個中文字，請重新設定。");
			return;
		}
	
		var tdid = GetCookie("TopicTD");
		xajax_newclass(classid, newsubtopicname, tdid);
	}	
}

function ontology_deletetopic(classid, parentid)
{
	var tdid = GetCookie("TopicTD");
	//show the deleted node' parent node after the node deleted, this was done in deleteclass callback, 2009.12.17
	xajax_deleteclass(classid, parentid);
		
	if(tdid != "reservedtopic")
	{
		var node = document.getElementById(tdid);
		var uTABLE = node.parentNode;
		while(uTABLE.nodeName != "TABLE") {
			uTABLE = uTABLE.parentNode;
   		}
		
		var currtable = uTABLE;
		var parentdiv = currtable.parentNode;
		//set current td to the parent of table's td, set cookie before table being removed
		SetCookie("TopicTD", parentdiv.parentNode.id);
		parentdiv.removeChild(currtable);
	}	
}

function ontology_addnewkwrelation(classid)
{
	var keyword = "";
	keyword = prompt("為主題關鍵字可產生與其他主題的關聯，請在關鍵字之間加上','或是空白做區隔，例如\"陽明山,小油坑\"","");
	
	if((keyword != null)&&(keyword != ""))
	{
		
		if(keyword.length>64)
		{
			alert("關鍵字字串超過32個中文字，請重新設定。");
			return;
		}
		
		
		xajax_addnewkwrelation(classid, keyword);
	}
}

function ontology_deletekwrelation(kwralationid, classid)
{
	var bProcess = confirm("是否要刪除此關鍵字？");
	if(bProcess)
	{
		xajax_deletekwrelation(kwralationid, classid);
		//refresh the node after its kw been deleted
		//xajax_showtopic("divtopic", classid);
	}
}

function ontology_movetopic(classid)
{
	xajax_setsystemcookie("ontology", "cm,"+classid);
	var tdid = GetCookie("TopicTD");
	if((tdid == null) || (tdid==""))
		tdid = "reservedtopic";
	SetCookie("CopiedTopicTD", tdid);
	alert("請再點選要移動到目的主題，做貼上的動作。");
}


function ontology_pastetopic(classid, ontologydata)
{
	var tdid = GetCookie("TopicTD");
	var copiedtdid = GetCookie("CopiedTopicTD");
	xajax_classpaste(classid, ontologydata, tdid, copiedtdid);
	//reset cookie
	SetCookie("CopiedTopicTD", "");
	xajax_setsystemcookie("ontology", "");
}
//ontology call
function dspreparetoedit(parah)
{
	parah.style.background='#718F41';
	setTimeout("document.getElementById('"+(parah.id)+"').style.background=''", 2000);
//	document.getElementById("topicdsparag").style.background='#718F41';
//	setTimeout("document.getElementById('topicdsparag').style.background=''", 2000);
}

//para0 is to indicate if empty string is allowed, para1 indicate the item to edit, para2 is used to store additional data like classid, para3 is used to store the original parah
function showeditdstext(para, editmaxwidth, param0val, param1val, param2val, bEmptyStr)
{
	//do not check if orginal text is empty, since bEmpty was fix while page loaded that will cause mistake judgement while re-edit text at the same session 2008.10.21 
	//var edittext = (bEmptyStr) ? "" : unescape(para.innerHTML); // use unescape to decode 
	var edittext = unescape(para.innerHTML); // use unescape to decode 
	
	var editstrlen = edittext.length;
	rows = (editstrlen / editmaxwidth)+1;
	//cols = (editstrlen > editmaxwidth) ? editmaxwidth : editstrlen;
	cols = editmaxwidth;
	
	var editdiv = document.createElement('div');
	editdiv.id="editdiv";
	var editbox = document.createElement('textarea');
	editbox.id="edittextarea";
	editbox.setAttribute("rows", rows);
	editbox.setAttribute("cols", cols);
	editbox.value=edittext;
	
	//creaTextNode may encode character, such as &, space, etc., assign value direct instead, 2007.12.28
	//var textnode = document.createTextNode(edittext);
	//editbox.appendChild(textnode);
	
	var btneditsave = document.createElement('input');
	btneditsave.type="button";
	btneditsave.value="SAVE";
	btneditsave.id="editbuttonsave";
	btneditsave.onclick=saveedittext;
	var btneditcancel = document.createElement('input');
	btneditcancel.type="button";
	btneditcancel.value="CANCEL";
	btneditcancel.id="editbuttoncancel";
	btneditcancel.onclick=canceledittext;

	//to store if allowed empty string,2008.3.29	
	var param0 = document.createElement('input');
	param0.type="hidden";
	param0.value=param0val;
	param0.id="editparam0";
	
	var param1 = document.createElement('input');
	param1.type="hidden";
	param1.value=param1val;
	param1.id="editparam1";
	
	var param2 = document.createElement('input');
	param2.type="hidden";
	param2.value=param2val;
	param2.id="editparam2";
	
	var param3 = document.createElement('input');
	param3.type="hidden";
	param3.value=para.id;
	param3.id="editparam3";
	
	para.parentNode.appendChild(editdiv);
	editdiv.appendChild(editbox);
	editdiv.appendChild(document.createElement('br'));
	editdiv.appendChild(btneditsave);
	editdiv.appendChild(btneditcancel);
	editdiv.appendChild(param0);
	editdiv.appendChild(param1);
	editdiv.appendChild(param2);
	editdiv.appendChild(param3);			
	
	para.style.display="none";
	editbox.focus();
	
	
}

function saveedittext()
{
	var classds = document.getElementById("edittextarea").value;
	classds = classds.replace(/(^\s*)|(\s*$)/g, "");
		
	var bEmptyStrAllowed = document.getElementById("editparam0").value;
	var classid = document.getElementById("editparam2").value;
	var edititem = document.getElementById("editparam1").value;
	var orgparah = document.getElementById("editparam3").value;
		
	document.getElementById(orgparah).style.display="";
	if((!bEmptyStrAllowed) && (classds==""))		
	{
		alert("請勿留白");
	}	
	else
	{
		xajax_editclassds(edititem, classid, classds);
		document.getElementById(orgparah).innerHTML = (classds=="")? "按一下以增加說明":classds;	
	}
	
	var editdiv = document.getElementById("editdiv");
	var parentdiv = editdiv.parentNode;
	parentdiv.removeChild(editdiv);	
	
	
}

function changetopiclogourl(classid, oriurl)
{
	oriurl = decodeURIComponent(oriurl);//2008.12.17 to avoid some special character
	
	var newurl=prompt("請輸入新的影像位址", oriurl);
	//if((newurl != null) && (newurl != ""))
	if(newurl != null)
		xajax_editclassds("topiclogourl", classid, newurl);
} 

function canceledittext()
{
	var orgparah = document.getElementById("editparam3").value;
		
	var editdiv = document.getElementById("editdiv");
	var parentdiv = editdiv.parentNode;
	parentdiv.removeChild(editdiv);
	document.getElementById(orgparah).style.display="";
}

//2008.9.2 
function ShowTopicIteration(node, start, limit, topicid, userid, bIsShowme, func)
{
	var showdiv = (node == null) ? "divshowtopicrelaticarticle" : getDivContainerId(node);
			
        xajax_showTopicIteration(showdiv, start, limit, topicid, userid, bIsShowme, func);
}

//2010.3.1 only used in showtopic_relaticarticle_func3(getarticle.inc.php) and topicobj->action(class.docation.js)
function gajaxSearchRelaticArticlesOfTopic(w)
{
	gajaxsearchglobal(w, "www.waytogo.cc", "divshowtopicrelaticarticle", "assign", 4);
			
	var relarticlealterdiv = document.getElementById("showtopic_relaticarticle_title_alter");
	if(relarticlealterdiv)
		relarticlealterdiv.innerHTML = "";
}