document.write("<script src='/icons/icons/Functions.js'></script>");	//import public available js file

function BBS_InitGSVars(psCurUser,psCurURL,psDBName,psDBTitle,psCurUNID,pbIsEdit){
	//Get public variable value from parameter
	gsCurUser 	=	psCurUser;
	gsOAURL		=	psCurURL;
	gsCurURL	=	psCurURL;
	gsCurDBName	=	psDBName;
	gsCurDBTitle	=	psDBTitle;
	gsCurUNID	=	psCurUNID;
	gbIsEdit	=	pbIsEdit;

	if(sGetCookieExt("StylePathname")!=""){
		gsImgURL	=	sGetCookieExt("StylePathname");
	}else{
		gsImgURL	=	"/icons/icons/bbs/default/";	//Default image urlpath
	}
	bSetCookie("CurBBSUser",psCurUser,0,"/");
	goForm		=	window.document.forms[0];
	gsHomeURL	=	gsCurURL+"zBBSAdm.nsf/frmWBlock?OpenForm";
	gsLoginURL	=	gsCurURL+"zBBSAdm.nsf/frmLogin?OpenForm";
	if (typeof top=="object" && typeof top.hPromptDoing=="object") top.hPromptDoing.innerHTML="";
	
	var lsBaseUrl = document.location.protocol + '//' + document.location.host;
	document.write("<base href='" + lsBaseUrl + gsImgURL + "/'>");
}
function BBS_Login(psTargetURL){
	if(psTargetURL==null || psTargetURL==""){
		var lsTargetURL=gsHomeURL;
	}else{
		var lsTargetURL=psTargetURL;
	}
	lsTargetURL="&URL="+lsTargetURL;
	window.open(gsLoginURL+lsTargetURL,"_self","");
}
function BBS_Logout(psTargetURL){
	if(typeof(top.opener)=="object"){
		top.close();
		return;
	}else{
		if(psTargetURL==null || psTargetURL==""){
			var lsTargetURL=gsHomeURL;
		}else{
			var lsTargetURL=psTargetURL;
		}
		bDelCookie("CookieDate");
		bDelCookie("UserName");
		bDelCookie("Password");
		bDelCookie("DomAuthSessId");
		bDelCookie("LtpaToken");
		location.replace(lsTargetURL);	
	}
}

function BBS_bCheckLogin(psSuccessURL,psFailURL){
	if(gsCurUser=="Anonymous"){
		if((psFailURL==null || psFailURL=="")==false){
			BBS_Login(psFailURL);
		}else{
			BBS_Login(psSuccessURL);
		}
		return false;
	}else{
		if((psSuccessURL==null || psSuccessURL=="")==false){
			window.open(psSuccessURL,"_self","");			
		}
		return true;
	}
}

function BBS_SetStyle(psStylePath){
	var lsStylePath=(psStylePath==null || psStylePath=="")?"\/icons\/icons\/bbs\/default\/":("\/icons\/icons\/bbs\/"+psStylePath+"\/");
	bSetCookieExt("StylePathname",lsStylePath,10000,"/");
	location.reload(true);
}

function BBS_PromptDoing(psMessage) {
	if (typeof top.hPromptDoing!="object" && typeof top.hPromptDoingExt!="object") return false;
	var lsTop="100",lsLeft="0";
	if (typeof top.hPromptDoingExt=="object"){
		lsTop="200";
		lsLeft="150";
	}
	var lsHTML='<div id=hPromptDoingDiv style="position:absolute; top:'+lsTop+'; left:'+lsLeft+'; z-index:10; visibility:visible;width:100%;"><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 style="width:100%;background-color: transparent; "><TR><TD align=center style="background-color: transparent;"><TABLE BORDER=0 CELLSPACING=2 CELLPADDING=0 class=HCoolBorder style="width:350;height:150;"><TR><TD style="background-color:#eeeeee;text-align:center;" id=hPromptDoingText>' + psMessage + '</td></tr></table></td></tr></table></div>	\n' +
	'<div id="hCover" style="position:absolute; top:0; left:0; z-index:9; visibility:visible;"><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 style="width:100%;height:1000;background-color: transparent; "><TR><TD style="background-color: transparent; "></td></tr></table></div>		\n';
	if (typeof top.hPromptDoing=="object") top.hPromptDoing.innerHTML=lsHTML;
	if (typeof top.hPromptDoingExt=="object") top.hPromptDoingExt.innerHTML=lsHTML;
}

function BBS_DocAction(psActionName,psUNIDs,pvArgs){
	WebAction(null,psActionName,psUNIDs,pvArgs,null,null);
}

function BBS_SendMsg(psUserAlias){
	if(BBS_bCheckLogin(null,location.href)){
		var lsURL=gsCurURL+"zBBSMsg.nsf/frmOutMsg?OpenForm";
		if(psUserAlias!=null){
			lsURL=sSetParameter("To",escape(psUserAlias),lsURL);
		}
		window.open(lsURL,"_self","");
	}
}

function BBS_AddToFriend(psUser){
	if(BBS_bCheckLogin(location.href))
		WebAction(null,"ADDTOBOOK",null,psUser,null,null);
}

function BBS_AddToFavorite(psUNIDs){
	if(BBS_bCheckLogin(location.href)){
		BBS_DocAction("ADDTOFAVORITE",psUNIDs);
	}
}

function BBS_Maintain(psUNIDs){
	if(BBS_bCheckLogin(null,location.href))
		showMenu("menu_admin",100);
}

function BBS_vSelectUser(pvDefault){
	return vOpenModal(gsCurURL+"zBBSUser.nsf/frmSelectUser?OpenForm",pvDefault,0.4,0.75,"scroll:no;");
}

var giLinesPerPage=30;
var giTotalPages=1;
var giPageNumbers=10;
var giCurPage=1;
var goBodyWindow=window;

function BBS_InitPageVars(piLinesPerPage,piPageNumbers,poBodyWindow){
	if(typeof(ID_TotalLines)!="object"){
		return false;
	}
	var liTotalLines=ID_TotalLines.innerText-0;
	if(liTotalLines==0 && typeof(document.all.ID_VIEW)=="object"){
		document.all.ID_VIEW.innerHTML=sGetLang("BBSA_NODOCUMENTFOUND");
	}
	if(piPageNumbers!=null)giPageNumbers=piPageNumbers;
	if(poBodyWindow!=null)goBodyWindow=poBodyWindow;
	if(piLinesPerPage!=null){
		giLinesPerPage=piLinesPerPage;
	}else{
		var lsURL=sReplaceSubstring(goBodyWindow.location.href,"#","");
		var lsCount=sGetParameter("count",lsURL.toLowerCase());
		if(lsCount!="")giLinesPerPage=lsCount-0;
	}
	if(giLinesPerPage==-1){
		giTotalPages=1;
	}else{
		giTotalPages=parseInt(""+liTotalLines/giLinesPerPage);
		giTotalPages=giTotalPages*giLinesPerPage<liTotalLines?(giTotalPages+1):giTotalPages;
	}
}

function BBS_ShowPageList(psStyle){
	if(typeof(ID_TotalLines)!="object" || typeof(ID_PageList)!="object" || typeof(ID_CurPage)!="object"){
		return false;
	}
	if(giTotalPages<=1){
		var lsText="&nbsp;";
		if(typeof(ID_PageList.length)!="number"){
			ID_PageList.innerHTML=lsText;
		}else{
			for (var i=0;i<ID_PageList.length;i++){
				ID_PageList[i].innerHTML=lsText;
			}
		}
 		return false;
 	}
	giCurPage=ID_CurPage.innerText-0;
	var lsPageLink="";
	var liLBound=1;
	var liUBound=giTotalPages>giPageNumbers?giPageNumbers:giTotalPages;
	if(giCurPage>1){
		var liHalf=parseInt(""+giPageNumbers/2);
		var liLBound=giCurPage-liHalf>0?giCurPage-liHalf:1;
		var liUBound=giCurPage+liHalf>giTotalPages?giTotalPages:giCurPage+liHalf;
		if(liUBound<giPageNumbers && giTotalPages>giPageNumbers)liUBound=giPageNumbers;
		if(giCurPage+liHalf>giTotalPages)liLBound=1>liLBound-(giCurPage+liHalf-giTotalPages)?1:liLBound-(giCurPage+liHalf-giTotalPages);
	}
	for(i=liLBound;i<=liUBound;i++){
		if(giCurPage==i){
			lsPageLink+="<span class=CurrentPage>&nbsp;"+i+"</span>"	//disable current page-index
		}else{
			lsPageLink+="&nbsp;<a href=# onclick='BBS_GoToPage("+i+");return false;'>"+i+"</a>";
		}
	}
	var lsLastPage="";
	if(liUBound<giTotalPages){
		if(giTotalPages>liUBound+1){
			lsLastPage="...";
		}else{
			lsLastPage=" ";
		}
		lsLastPage+="<a href=# onclick='BBS_LastPage();return false;'>"+giTotalPages+"</a>"
	}
	if(psStyle==null){ //default style
		if(giTotalPages>1){
			var lsPreDisabled=(giCurPage<=1)?"disabled":"";
			var lsNextDisabled=(giCurPage>=giTotalPages)?"disabled":"";
			var lsPrePage="&nbsp;<a href=# onclick='BBS_FirstPage();return false;' "+lsPreDisabled+"><font face=webdings style='font-weight:normal'>9</font></a>&nbsp;<a href=# onclick='BBS_PriorPage();return false;'  "+lsPreDisabled+"><font face=webdings style='font-weight:normal'>7</font></a>";
			var lsNextPage="&nbsp;<a href=# onclick='BBS_NextPage();return false;' "+lsNextDisabled+"><font face=webdings style='font-weight:normal'>8</font></a>&nbsp;<a href=# onclick='BBS_LastPage();return false;'  "+lsNextDisabled+"><font face=webdings style='font-weight:normal'>:</font></a>";
			lsPageLink=lsPrePage+lsPageLink+lsLastPage+lsNextPage;
		}
		if(typeof(ID_PageList.length)!="number"){
			ID_PageList.innerHTML="<span style='font-weight:bold;'>"+lsPageLink+"</span>";
		}else{
			for (var i=0;i<ID_PageList.length;i++){
				ID_PageList[i].innerHTML="<span style='font-weight:bold;'>"+lsPageLink+"</span>";
			}
		}
	}
}

function BBS_GoToPage(piPageNO){
	var liPageNO=piPageNO>giTotalPages?giTotalPages:piPageNO;
	var lsURL=sReplaceSubstring(goBodyWindow.location.href,"#","");
	if(giLinesPerPage==-1){
		var liStart=1;
	}else{
		var liStart=(liPageNO-1)*giLinesPerPage+1;
	}
	lsURL=sSetParameter("Start",liStart,lsURL);
	lsURL=sSetParameter("Count",giLinesPerPage,lsURL);
	goBodyWindow.open(lsURL,"_self","");
	if(goBodyWindow!=window){
		ID_CurPage.innerText=""+liPageNO;
		BBS_ShowPageList();
	}
}

function BBS_FirstPage(){
	BBS_GoToPage(1);
}

function BBS_PriorPage(){
	if(giCurPage>1)
		BBS_GoToPage(giCurPage-1)
}

function BBS_NextPage(){
	if(giTotalPages>giCurPage)
		BBS_GoToPage(giCurPage+1)
}

function BBS_LastPage(){
	BBS_GoToPage(giTotalPages);
}



function sGetObjPath(poObj, psName){
	if (poObj==null) return null;
	while(poObj=poObj.parentElement){
		if (poObj.UNID!=null && poObj.UNID!="" && poObj.URLPath!=null && poObj.URLPath!="" && poObj.attachNames!=null && poObj.attachNames.indexOf("|"+psName+"|")!=-1){
			return poObj.URLPath;
		}
	}
	return null;
}

function bIsRtfObj(poObj){
	if (poObj==null) return null;
	while(poObj=poObj.parentElement){
		if (poObj.UNID!=null && poObj.UNID!="" && poObj.URLPath!=null && poObj.URLPath!="") return true;
	}
	return false;
}

function bUpdateUrl(){
	var lsName,lsPath;
	var lodoc = window.document;
	//deal image src
	for (i=0; i<lodoc.images.length;i++){
		lsName = "/"+lodoc.images[i].src;
		lsName = lsName.substr(lsName.lastIndexOf("/")+1);
		lsPath = sGetObjPath(lodoc.images[i],lsName);
		if (lsPath!=null) lodoc.images[i].src=lsPath+lsName;
	}
	//deal attach file href
	var attFiles = lodoc.getElementsByName("D_attFiles");
	if (attFiles!=null){
		for (i=0; i<attFiles.length;i++){
			lsName = attFiles[i].href.substr(attFiles[i].href.lastIndexOf("/")+1);
			lsPath = sGetObjPath(attFiles[i],lsName);
			if (lsPath!=null) attFiles[i].href = lsPath + lsName;
		}
	}
	//deal embed file src
	var embFiles = lodoc.getElementsByName("D_embFiles");
	if (embFiles!=null){
		for (i=0; i<embFiles.length;i++){
			lsName = embFiles[i].src.substr(embFiles[i].src.lastIndexOf("/")+1);
			lsPath = sGetObjPath(embFiles[i],lsName);
			if (lsPath!=null) embFiles[i].src = lsPath + lsName;
		}
	}
	//deal flash file link
	var flashFiles = lodoc.getElementsByName("D_flashFiles");
	if (flashFiles!=null){
		for (i=0; i<flashFiles.length;i++){
			lsName = flashFiles[i].movie;
			lsPath = sGetObjPath(flashFiles[i],lsName);
			if (lsPath!=null && lsName.indexOf(lsPath)==-1){
				var lsHTML = flashFiles[i].outerHTML;
				flashFiles[i].outerHTML = sReplaceSubstring(lsHTML,lsName, lsPath+lsName);
			}
		}
	}
	//deal link target
	for (i=0; i<lodoc.links.length;i++){
		if (lodoc.links[i].href!="" && lodoc.links[i].href!="#" && lodoc.links[i].onclick==null && lodoc.links[i].target!="_blank" && bIsRtfObj(lodoc.links[i])==true) lodoc.links[i].target="_blank";
	}
}



function SelectAll(pbSelect,poWindow){
	if(typeof(poWindow)=="object")
		var loframe=poWindow;
	else{
		if (typeof top.document!="unknown")
			var loframe= top.ViewIFrame;
		else
			var loframe= top.ViewIFrame.MailIframe;
	}
	var loForm = loframe.document.forms[0];
	for (var i = 0; i < loForm.elements.length; i++)
		if (loForm.elements[i].type == "checkbox" && loForm.elements[i].name == "$$SelectDoc") 
			loForm.elements[i].checked = pbSelect;
	for (var i=0;i<loframe.document.all.length;i++){
		if (loframe.document.all[i].id!=null && loframe.document.all[i].id.indexOf("DivisionalIframe")!=-1){
			loForm=eval("loframe."+loframe.document.all[i].id+".document.forms[0]");
			for (var k = 0; k < loForm.elements.length; k++){
				if (loForm.elements[k].type == "checkbox" && loForm.elements[k].name == "$$SelectDoc"){ 
					loForm.elements[k].checked = pbSelect;
				}
			}
		}
	}
}
