function lib_bwcheck(){ //Browsercheck (needed)
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=this.agent.indexOf("Opera 5")>-1
	this.opera=this.agent.indexOf("Opera")>-1
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.ver.indexOf("MSIE 5.5")==-1 && this.dom && !this.opera5)?1:0;
	this.ie55=(this.ver.indexOf("MSIE 5.5")>-1 && this.dom && !this.opera5)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie55||this.ie6
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}

var bw=new lib_bwcheck()

function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
			if(getStyle(obj, "position")=="absolute") break;
		}
	}
	else if (obj.x){
        curleft += obj.x;
    }
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			if(bw.opera) {	//doesn't work
				curtop+= parseInt(getStyle(obj, 'margin-top'));
				curtop+= parseInt(getStyle(obj, 'padding-top'));
			}
			obj = obj.offsetParent;
			if(getStyle(obj, "position")=="absolute") break;
		}
	}
	else if (obj.y)	curtop += obj.y;	// NS4
	return curtop;
}

function getStyle(x,styleProp){
	//var x = document.getElementById(el);
	if (window.getComputedStyle)
	var y = window.getComputedStyle(x,null).getPropertyValue(styleProp);
	else if (x.currentStyle)
	var y = eval('x.currentStyle.' + styleProp);
	return y;
}

function ifOverlap(e1, e2){
	/*e1 = getRefById(id1);
	e2 = getRefById(id2);*/
	var ax1 = findPosX(e1);
	var ay1 = findPosY(e1);
	var ax2 = ax1+e1.offsetWidth;
	var ay2 = ay1+e1.offsetHeight;
	var bx1 = findPosX(e2);
	var by1 = findPosY(e2);
	var bx2 = bx1+e2.offsetWidth;
	var by2 = by1+e2.offsetHeight;

	if(by2<ay1 || ay2<by1 || bx2<ax1 || ax2<bx1) return false;
	else return true;
}

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 or higher.
{
	for(var i=0; i<document.images.length; i++)
	{
		var img = document.images[i]
		var imgName = img.src.toUpperCase()
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		{
			var imgID = (img.id) ? "id='" + img.id + "' " : ""
			var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
			var imgStyle = "display:inline-block;" + img.style.cssText
			var imgAttribs = img.attributes;
			for (var j=0; j<imgAttribs.length; j++)
			{
				var imgAttrib = imgAttribs[j];
				if (imgAttrib.nodeName == "align")
				{
					if (imgAttrib.nodeValue == "left") imgStyle = "float:left;" + imgStyle
					if (imgAttrib.nodeValue == "right") imgStyle = "float:right;" + imgStyle
					break
				}
			}
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
			strNewHTML += " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			strNewHTML += "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
			img.outerHTML = strNewHTML
			i = i-1
		}
	}
}

function getRefById(id) {
	return document.getElementById ? document.getElementById(id) : document.all ? document.all[id] : null;
}

function addClass(ele, cname){
	if(!ele) return;
	if(ele.className && ele.className!='') {
		var names = ele.className.split(" ");
		for(var i=0; i<names.length; i++) {
			if (names[i]==cname) return;
		}
		ele.className += ' ' + cname;
	}
	else ele.className=cname;
}

function removeClass(ele, cname){
	if(!ele || !hasClassName(ele, cname)) return;

	if(ele.className == cname) ele.className='';
	else{
		var re = new RegExp("\\s*\\b" + cname + "\\b");	//\b: word boundary
		ele.className = ele.className.replace(re, '');
	}
}

// bug: cause open menu unclosed
function hasClassName(ele, c){
	if(!ele.className) return false;
	re = new RegExp("\\b" + c + "\\b");	//\b: word boundary
	if(re.test(ele.className)) return true;
	else return false;
}

//toggle between login and SMS panel
function toggleLoginSMS(t){
	var l=document.getElementById('login_pane');
	var s=document.getElementById('sms_panel');
	if(t=='login'){
		l.style.display='block';
		s.style.display='none';
	}
	else {
		l.style.display='none';
		s.style.display='block';
	}
}

function makeFadingBorder(){
	var bt=document.getElementById('f_border_t');
	var bb=document.getElementById('f_border_b');
	var bl=document.getElementById('f_border_l');
	var br=document.getElementById('f_border_r');

	if(!bt || !bb || !bl || !br) return;

	var y=findPosY(bb) - findPosY(bt);
	bl.style.height=y*0.7 + 'px';
	br.style.height=y*0.7 + 'px';
}

function setPageTitle(){
	var pageTitle = document.getElementById('pageTitle');
	if(!pageTitle || pageTitle.innerHTML!="") return;
	if(!pageTitle) return;
	var m = document.getElementById(makeS1Id(s1) +'-' +s2 +'-' +s3);
	if(!m) m = document.getElementById(makeS1Id(s1) +'-' +s2);
	if(!m) m = document.getElementById(makeS1Id(s1));
	if(!m) return;
	pageTitle.innerHTML=getAnchor(m).firstChild.nodeValue;
}

function popupWin(url,w,h,wname,x,y) {
	w = w || 540
	h = h || 455
	x = x || 0
	y = y || 0
	__win=window.open(url,wname,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,screenX='+x+',screenY='+y+',top='+y+',left='+x+',resizable=1,width='+w+',height='+h)
	setTimeout('__win.focus()', 300)
}

function popupWinWithScrollBars(url,w,h,wname,x,y) {
	w = w || 540
	h = h || 455
	x = x || 0
	y = y || 0
	__win=window.open(url,wname,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,screenX='+x+',screenY='+y+',top='+y+',left='+x+',resizable=yes,width='+w+',height='+h)
	setTimeout('__win.focus()', 300)
}
// Original JavaScript code by Duncan Crombie: dcrombie at chirp.com.au
// Please acknowledge use of this code by including this header.

var bikky = document.cookie;

function getCookie(name) { // use: getCookie("name");
	var index = bikky.indexOf(name + "=");
	if (index == -1) return null;
	index = bikky.indexOf("=", index) + 1;
	var endstr = bikky.indexOf(";", index);
	if (endstr == -1) endstr = bikky.length;
	return unescape(bikky.substring(index, endstr));
}

function setCookie(name, value) { // use: setCookie("name", value);
	if (value != null && value != "")
	var today = new Date();
	var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000); // plus 28 days
	document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString();
	bikky = document.cookie; // update bikky
}

function setCookieWithMins(name, value, mins) { // use: setCookie("name", value, mins);
	if (value != null && value != "")
	var today = new Date();
	var expiry = new Date(today.getTime() + mins * 60 * 1000);
	var expires = "";
	if (mins!=null){ //if mins = null, cookie expiry will be set at the End of Browser Session
		expires = "expires=" + expiry.toGMTString();
	}
	document.cookie=name + "=" + escape(value) + "; " + expires;
	bikky = document.cookie; // update bikky
}

function expireCookie(name) { // use: setCookie("name");
	var today = new Date();
	var expiry = new Date(today.getTime() - 28 * 24 * 60 * 60 * 1000); // minus 28 days
	document.cookie=name + "=" + "" + "; expires=" + expiry.toGMTString();
	bikky = document.cookie; // update bikky
}

//for debug only
var debugCounter = 0;
function debugWithTextArea(s){
	if(bw.opera) opera.postError(s);
	if(true) {
	//else if(bw.mac && bw.ie) {
		if(!document.getElementById('postError')){
			var d = document.createElement('div');
			d.setAttribute('style', 'position:absolute; top:0; left:765px; z-index:500;');
			var f = document.createElement('form');
			var ele = document.createElement('textarea');
			ele.id = 'postError';
			ele.setAttribute('rows', '20');
			ele.setAttribute('cols', '80');
			ele.setAttribute('style', 'font-family:sans-serif;');
			f.appendChild(ele);

			/*
			ele = document.createElement('input');
			ele.setAttribute('type', 'reset');
			ele.setAttribute('value', 'Clear');
			f.appendChild(ele);
			*/
			d.appendChild(f);
			try{document.getElementsByTagName('body')[0].appendChild(d);}
			catch(e){}
		}

		if(document.getElementById('postError')){
			document.getElementById('postError').value += '[' + debugCounter++ + '] '+ s + "\n";
		} else alert(s);
	}
}

function debug(s){
	if(bw && bw.opera) opera.postError(s);
	else window.alert(s);
}