// For common.js only
function $(id) {
	return document.getElementById(id);
}

// Show/Hide Elements
function display(id){
  try{
      if($(id).style.display=='none'){
        $(id).style.display='block';
      }else{
        $(id).style.display='none';
      }
  }catch(e){}
}

// Show/Hide Sidebar
function showHideSidebar(){
  try{
    var objSidebar=$("sidebar");
    var objContent=$("content");
    if(objSidebar.className!="sidebar-hide"){
      objSidebar.className="sidebar-hide";
      objSidebar.style.display="none";
      objContent.className="content-wide";
    }else{
      objSidebar.className="sidebar";
      objSidebar.style.display="block";
      objContent.className="content";
    }
  }catch(e){}
}

// Show/Hide Login Panel
function showLogin(){
  try{
    var panel=$("panelUser");
    if(panel){
      if(panel.style.display=='none'){
        panel.style.display='block';
      }else{
        panel.style.display='none';
      }
    }else{
      document.location="login.asp";
    }
  }catch(e){}
}

// Dynamic write object tag
function ubbShowObj(strType,strID,strURL,intWidth,intHeight)
{
	var objPrefix="bShow";
	var tmpstr="";
	var bSwitch = false;
	//Reverse the State
	bSwitch = $(objPrefix+strID).value;
	bSwitch	=~bSwitch;
	$(objPrefix+strID).value = bSwitch;
	if(bSwitch){
		//Code for already shown
		$(strID).innerHTML = "<a href=\""+strURL+'" target="_blank">'+strURL+"</a>";
	}else{
		//Code for not shown
		switch(strType){
			case "swf":
				tmpstr=strURL+'<br /><object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+intWidth+'" height="'+intHeight+'"><param name="movie" value="'+strURL+'" /><param name="quality" value="high" /><param name="AllowScriptAccess" value="never" /><embed src="'+strURL+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+intWidth+'" height="'+intHeight+'" /></object>';
				break;
			case "wmp":
				tmpstr=strURL+'<br /><object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902" type="application/x-oleobject" standby="Loading..." width="'+intWidth+'" height="'+intHeight+'"><param name="FileName" VALUE="'+strURL+'" /><param name="ShowStatusBar" value="-1" /><param name="AutoStart" value="true" /><embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="'+strURL+'" autostart="true" width="'+intWidth+'" height="'+intHeight+'" /></object>';
				break;
			case "rm":
				tmpstr=strURL+'<br /><object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="'+intWidth+'" height="'+intHeight+'"><param name="SRC" value="'+strURL+'" /><param name="CONTROLS" VALUE="ImageWindow" /><param name="CONSOLE" value="one" /><param name="AUTOSTART" value="true" /><embed src="'+strURL+'" nojava="true" controls="ImageWindow" console="one" width="'+intWidth+'" height="'+intHeight+'"></object>'+
        '<br /><object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="'+intWidth+'" height="32" /><param name="CONTROLS" value="StatusBar" /><param name="AUTOSTART" value="true" /><param name="CONSOLE" value="one" /><embed src="'+strURL+'" nojava="true" controls="StatusBar" console="one" width="'+intWidth+'" height="24" /></object>'+'<br /><object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="'+intWidth+'" height="32" /><param name="CONTROLS" value="ControlPanel" /><param name="AUTOSTART" value="true" /><param name="CONSOLE" value="one" /><embed src="'+strURL+'" nojava="true" controls="ControlPanel" console="one" width="'+intWidth+'" height="24" autostart="true" loop="false" /></object>';
				break;
			case "qt":
				tmpstr=strURL+'<br /><embed src="'+strURL+'" autoplay="true" loop="false" controller="true" playeveryframe="false" cache="false" scale="TOFIT" bgcolor="#000000" kioskmode="false" targetcache="false" pluginspage="http://www.apple.com/quicktime/" />';
		}
		$(strID).innerHTML = tmpstr;
	}
}

// Quote Comment Text
function doQuote(objID,strAuthor){
  var obj=$(objID);
  var text=obj.innerHTML;
  text=text.replace(/\<DIV class=(\"|)editmark(\"|)[\s\S]+\/DIV\>/gi,"");		// for 2nd quote
  text=text.replace(/\<DIV class=(\"|)quote(\"|)[\s\S]+\/DIV\>/gi,"");		// for 2nd quote
  text=text.replace(/alt\=(\"|)([^\"\s]*)(\"|)/g,"> $2 <");
  text=text.replace(/\<[^\<\>]+\>/g,"\n");
  text=text.replace(/ +/g," ");		//text=text.replace(/ +/g,"\n"); for disable 2nd quote smiles \n
  text=text.replace(/\n+/g,"\n");
  text=text.replace(/^\n*/gm,"");
  text=text.replace(/^\s*/gm,"");
  text=text.replace(/\n*$/gm,"");
  text=text.replace(/\s*$/gm,"");
	document.inputform.message.value += "[quote="+strAuthor+"]\n"+text+"\n[/quote]\n";
	window.location.hash="commentbox";
}

// Search
function doSearch(){
  var form=$("searchForm");
  if(lengthW(form.q.value)<3){
    alert("Keyword length must greater than 3 chars.");
    return false;
  }
  switch(form.searchType.value){
    case "comments":
      form.action="comment.asp";
      return true;
      break;
    case "guestbook":
      form.action="gbook.asp";
      return true;
    case "tags":
      form.action="tags.asp";
      return true;
    default:
      form.action="default.asp";
      return true;
      break;
  }
}

// String Length with unicode support
function lengthW(str){
  if(str==undefined){ return 0; }
  str=String(str);
  var tLen=0;
  for(var i=0;i<str.length;i++){
    charCode=str.charCodeAt(i);
    if(charCode<0||charCode>255){ tLen+=2 }else{ tLen++ }
  }
  return tLen;
}

// Set Article Font Size
function setFontSize(px){
  try{
    var t=$("textboxContent");
    if(t){
      t.style.fontSize=px+"px";
    }
  }catch(e){}
}

// Check multidelete form
var mxCheckbox = {
  // Method
  selectAll : function(strName){
    var el = document.getElementsByName(strName);
    for(var i=0;i<el.length;i++){
      el[i].checked = true;
    }
  },
  selectNone : function(strName){
    var el = document.getElementsByName(strName);
    for(var i=0;i<el.length;i++){
      el[i].checked = false;
    }
  },
  selectInvert : function(strName){
    var el = document.getElementsByName(strName);
    for(var i=0;i<el.length;i++){
      if(el[i].checked)
        el[i].checked = false;
      else
        el[i].checked = true;
    }
  }
};

// For Navs New
function assembleMenu(uLParentNode) {
	var theUL = uLParentNode.getElementsByTagName("ul")[0];
	var theULChilds = theUL.childNodes;

	for (var i=0; i<theULChilds.length; i++) {
		if (theULChilds[i].tagName == "LI") {
			var theLINode = theULChilds[i];
			if (hasNextLevelMenu(theLINode)) {
				setMouseActions(theLINode);
				theLINode.firstChild.className = theLINode.firstChild.className+(uLParentNode.tagName == "DIV" ? "" : " rightMenu");
				assembleMenu(theLINode);
			}
		}
	}
	function hasNextLevelMenu(node) {
		return node.getElementsByTagName("ul").length > 0;
	}
	function setMouseActions(node) {
		node.onmouseover = function() {
			this.getElementsByTagName("ul")[0].style.display = "block";
			if(this.firstChild.className.indexOf("topn")==0){this.firstChild.className=this.firstChild.className+" uhover";}
			if(this.firstChild.className.indexOf("topn")<0){this.firstChild.className="rightMenu";}
		};
		node.onmouseout = function() {
			this.getElementsByTagName("ul")[0].style.display = "none";
			if(this.firstChild.className.indexOf("topn")==0){this.firstChild.className=this.firstChild.className.replace(new RegExp("( ?|^)uhover\\b"),"");}
		};
	}
}

// For Navs Old
function menuFix() {
	var sfEls = $("nav").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=(this.className.length>0? " ": "") + "uhover";
		}
		sfEls[i].onMouseDown=function() {
			this.className+=(this.className.length>0? " ": "") + "uhover";
		}
		sfEls[i].onMouseUp=function() {
			this.className+=(this.className.length>0? " ": "") + "uhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp("( ?|^)uhover\\b"),"");
		}
	}
}

// For index.asp tabs
function $get(el) {
	if(el) {
		if(el.nodeType) {
			return el;
		}else if(typeof(el) === 'string') {
			return document.getElementById(el);
		}else if(length in el) {
			var els = [];
			for(var i=0, l = el.length; i < l; ++i) {
				els.push($get(el[i]));
			}
			return els;
		}
		return el;
	}
	return null;
}
function $class(classname,parentWrap){
	var rs=[],
		myclass = new RegExp("\\b"+classname+"\\b"),
		elem = ($get(parentWrap) || document).getElementsByTagName("*");
	for(var h=0,l=elem.length;h<l;++h){
		var classes = elem[h].className;
		if(myclass.test(classes)){rs.push(elem[h]);}
	}
	return rs;
}
function addClass(ele,v) {
	if(!ele.className) {
		ele.className = v;
	} else if(!hasClass(ele,v)) {
		ele.className += ' '+v;
	} else {
		return false;
	}
}
function removeClass(ele,v) {
	var cn = ele.className;
	if(cn) {
		var delclass = new RegExp('\\b'+v+'\\b','ig');
		if(delclass.test(cn)) {
			ele.className = cn.replace(delclass,"");
		}
	}
}
function hasClass(ele,classname) {
	var cn = ele.className;
	var rgExp = new RegExp('\\b'+classname+'\\b');
	return cn.match(rgExp);
}
function insertAfter(newElement,tarElement) {
	var pnode = tarElement.parentNode;
	if (pnode.lastChild == tarElement) {
		pnode.appendChild(newElement);
	} else {
		pnode.insertBefore(newElement,tarElement.nextSibling);
	}
}
function prepend(element,tarElement) {
	if(tarElement.firstChild) {
		tarElement.insertBefore(element,tarElement.firstChild);
	}else {
		tarElement.appendChild(element);
	}
}
function wrap(newparent,ele) {
	if(ele.nextSibling) {
		var n = ele.nextSibling;
		n.parentNode.insertBefore(newparent,n);
	}else if(ele.previousSibling) {
		insertAfter(newparent,ele.previousSibling);
	}else {
		ele.parentNode.appendChild(newparent);
	}
	newparent.appendChild(ele);
}
function clearDefault(ev) {
	ev=window.event?window.event:ev;
	if(ev.preventDefault) {
		ev.preventDefault();
	}else {
		ev.returnValue = false;
	}
}
function addEvent(tar,ev,fn) {
	if(document.attachEvent) {
		tar.attachEvent('on'+ev,fn);
	}else if(document.addEventListener) {
		tar.addEventListener(ev,fn,false);
	}
}
function removeEvent(tar,ev,fn) {
	if(document.detachEvent) {
		tar.detachEvent('on'+ev,fn);
	}else if(document.removeEventListener) {
		tar.removeEventListener(ev,fn,false);
	}
}
function CatTab(el,args) {
	this.el = $get(el);
	if(!this.el) {return false;}
	this.links = this.el.getElementsByTagName('a');
	this.focusClass = args.focusClass || 'lh';
	this.bindEv = args.mode || 'mouseover';
	this.nTab = new Object();

	this.ini();
}
CatTab.prototype = {
	ini:function (e) {
		var self = this;
		for(var i=0, l = this.links.length; i < l; ++i) {
			var lk = this.links[i],
				lc = hasClass(lk,this.focusClass),
				lts = lk.getAttribute('href',2).split('#'),
				lt = lts[1],
				wel = $get(lt);
			if(lc) {this.nTab = lk;}
			if(wel) {
				if(!lc) {
					wel.style.display = 'none';
				}else {
					wel.style.display = '';
				}
			}
			if((!lts[0] || this.bindEv == 'click') && lt) {
				addEvent(lk,'click',clearDefault);
			}
			if(!e) {
					lk.setAttribute('href',lts[0]);
					lk.v_catTab = lt;
					addEvent(lk,this.bindEv,function (e) {self.showTab(e)});
			}
		}
		if(!this.nTab.nodeType){
			this.nTab = this.links[0];
		}
		this.showTab(this.nTab);
	},
	getObj:function (e) {
		var o;
		var e = window.event || e;
		if(!$get(e).nodeType) {
			o = e.srcElement || e.target;
			while(o.tagName.toLowerCase() != 'a') {
				o = o.parentNode;
			}
		}else {
			o = $get(e);
		}
		return o;
	},
	showTab:function (e) {
		var obj = this.getObj(e);
		var oldtb = $get(this.nTab.v_catTab),
			newtb = $get(obj.v_catTab);
		if(oldtb && newtb) {
			oldtb.style.display = 'none';
			removeClass(this.nTab,this.focusClass);
		}
		if(newtb) {
			addClass(obj,this.focusClass);
			newtb.style.display = '';
			this.nTab = obj;
		}
	}
}
function catTab(el,args) {
	if(typeof(el) !== 'string' && (length in el)) {
		for(var i=0, l = el.length; i < l; ++i) {
			(function () {
				var v_catTab = new CatTab(el[i],args);}
			)();
		}
	}else {
		(function () {
			var v_catTab = new CatTab(el,args);
		})();
	}
}

// For index.asp images turn
function addBtn() {
	if(!$('focus_turn')||!$('focus_pic')||!$('focus_tx')) return;
	var focusList = $('focus_pic').getElementsByTagName('li');
	if(!focusList||focusList.length==0) return;
	var btnBox = document.createElement('div');
	btnBox.setAttribute('id','focus_btn');
	var SpanBox ='';
	for(var i=1; i<=focusList.length; i++ ) {
		var spanList = '<span class="normal">'+i+'</span>';
		SpanBox += spanList;
	}
	btnBox.innerHTML = SpanBox;
	$('focus_turn').appendChild(btnBox);
	$('focus_btn').getElementsByTagName('span')[0].className = 'current';
}
function classNormal(){
	var focusList = $('focus_pic').getElementsByTagName('li');
	var btnList = $('focus_btn').getElementsByTagName('span');
	var txList = $('focus_tx').getElementsByTagName('li');
	for(var i=0; i<focusList.length; i++) {
		focusList[i].className='normal';
		btnList[i].className='normal';
		txList[i].className='normal';
	}
}
function classCurrent(n){
	var focusList = $('focus_pic').getElementsByTagName('li');
	var btnList = $('focus_btn').getElementsByTagName('span');
	var txList = $('focus_tx').getElementsByTagName('li');
	focusList[n].className='current';
	btnList[n].className='current';
	txList[n].className='current';
}
var autoKey = false;
function btnTurn() {
	if(!$('focus_turn')||!$('focus_pic')||!$('focus_tx') || !$('focus_btn')) return;
	$('focus_turn').onmouseover = function(){autoKey = true};
	$('focus_turn').onmouseout = function(){autoKey = false};
	var focusList = $('focus_pic').getElementsByTagName('li');
	var btnList = $('focus_btn').getElementsByTagName('span');
	var txList = $('focus_tx').getElementsByTagName('li');
	for (var m=0; m<btnList.length; m++){
		btnList[m].onmouseover = function() {
			classNormal();
			this.className='current';
			var n=this.childNodes[0].nodeValue-1;
			focusList[n].className='current';
			txList[n].className='current';
		}
	}
}
function autoTurn() {
	if(!$('focus_turn')||!$('focus_pic')||!$('focus_tx')) return;
	if (autoKey) return;
	var focusList = $('focus_pic').getElementsByTagName('li');
	var btnList = $('focus_btn').getElementsByTagName('span');
	var txList = $('focus_tx').getElementsByTagName('li');
	for(var i=0; i<focusList.length; i++) {
		if (focusList[i].className == 'current') {
			var currentNum = i;
		}
	}
	if (currentNum==focusList.length-1 ){
		classNormal();
		classCurrent(0);
	} else {
		classNormal();
		classCurrent(currentNum+1);
	}
}
function setautoturn() {
	setInterval('autoTurn()', 3000);
}

// For index.asp scroll text
function scroll(){
	setInterval('scrollup($("box"),28,0)',3000); 
}
function scrollup(o,d,c){
	if(d==c){
		var t=getFirstChild(o.firstChild).cloneNode(true);
		o.removeChild(getFirstChild(o.firstChild));
		o.appendChild(t);
		t.style.marginTop="0px";
	}else{
		c+=2;
		getFirstChild(o.firstChild).style.marginTop=-c+"px";
		window.setTimeout(function(){scrollup(o,d,c)},20);
	}
}
function getFirstChild(node){
	while (node.nodeType!=1){
		node=node.nextSibling;
	}
	return node;
}

// Set images
function imgfixlen() { 
	obj=document.getElementsByTagName("img");
	for(i=0;i<obj.length;i++) {
		if(obj[i].className=="ubbimg") {
			if(obj[i].width>2000) {
				obj[i].width=2000;
			}
		}
		if(obj[i].className=="outimg") { // for default.asp output images
			if(obj[i].width>200&&(obj[i].width>obj[i].height)) {
				obj[i].width=200;
			}
			if(obj[i].height>200&&(obj[i].width<obj[i].height)) {
				obj[i].width=(obj[i].width/obj[i].height)*200;
			}
		}
		if(obj[i].className=="indeximg") { // for index.asp output images
			if(obj[i].width>120&&(obj[i].width>obj[i].height)) {
				obj[i].width=120;
			}
			if(obj[i].height>120&&(obj[i].width<obj[i].height)) {
				obj[i].width=(obj[i].width/obj[i].height)*120;
			}
		}
	}
}

// Delete focus
function bluring() { 
	var aObj=document.getElementsByTagName("a");
	var length=aObj.length;
	for(var i=0;i<length;i++){
		aObj[i].onfocus=function(){this.blur();}
	}
}

// Multiple function load
function multiload(f){
	if (window.onload){
		var o=window.onload;
		window.onload=function(){
			f();o();
		}
	}else{
		window.onload=f;
	}
}
multiload(function(){imgfixlen();});//bluring();