
function busyhour(){
	var hr=new Date();
	hr=hr.getUTCHours();
	return hr>13 && hr<18;
}
function setCookie(name, value){
	if(value==null) value='';
	var r=name+"="+escape(value)+';';
	var k;
	if(arguments[2]==null || arguments[2]==='')k=7;
	else k=parseInt(arguments[2]);
	if(k>0 || k<0){
		var n=new Date();
		nd=n.getDate();
		n.setDate(nd+k);
		r+=" expires="+n.toGMTString();
	}
	var path, domain;
	if(arguments[3]!==null) path=arguments[3];
	if(arguments[4]!==null) domain=arguments[4];
//	path=domain='';
	if(path)r+="; path="+path;
	if(domain)r+="; domain="+domain;
	document.cookie=r;
}
function getCookie(name){
	var reg=new RegExp('(?:^|\\W)'+name+"=([^;]*)");
	var r=reg.exec(document.cookie);
	if(!r)return '';
	r= RegExp.$1;
	if(!r)return '';
	r=unescape(r);
	return r ? r : '';
}

function setclassname(obj, classname, value){
	if(!obj)return;
	var reg=new RegExp("(^|\\s)"+classname+"(\\s|$)");
	var found=reg.exec(obj.className);
	if(value){
		if(found)return;
		obj.className+=' '+classname;
	}
	else{
		if(!found)return;
		obj.className=obj.className.replace(reg, '$1');
	}
}

function addOptions(obj, opt_array){
	if(opt_array==null)return;
	var l=obj.options.length;
	if(arguments[2]==1){
		for(var i=0; i<opt_array.length; i+=2)obj.options[l+parseInt(i/2)]=new Option(opt_array[i+1], decodeURI(opt_array[i]));
	}
	else{
		for(var i=0; i<opt_array.length; i+=2)obj.options[l+parseInt(i/2)]=new Option(opt_array[i+1], opt_array[i]);
	}
}

function validateemail(email){
	email=email.replace(/(^)(\s*)/g, '$1');
	email=email.replace(/(\s*)($)/g, '$2');
	var i;
	for(i=0; i<email.length; i++){
		if(email.charCodeAt(i)>127) return '';
	}
	if( email.match(/^(.+)@(.+)\.([^.\s]{2,})$/) ) return email;
	return '';
}

function refreshstat(){
	return;
	var sid=getCookie('sesid');
	var curday=new Date();
	var curtime=parseInt(curday.getTime()/60000);
	var exptime=10;
	if(!sid){
		setCookie('sesid', '-1');
		sid=getCookie('sesid');
		setCookie('sestime', curtime-exptime-1);
	}
	if(!sid)return;
	var oldtime=getCookie('sestime');
	if(!((curtime-exptime)>=oldtime))return;

	setCookie('sestime', curtime);
	var mid=getCookie('userid');
	frames['statframe'].location.href='?o=session&act=refresh&mid='+mid+'&sesid='+sid;
}
function browsercheck(name){
	var browser, blist, i, k, refname, img;
	browser=navigator.userAgent.toLowerCase();
	name=name.toLowerCase();
	return browser.indexOf(name)+1;

}
function htmlescape(str){
	var reg;
	str=str.replace(/</g, '&lt;');
	str=str.replace(/>/g, '&gt;');
	reg=new RegExp('\'', 'g');
	str=str.replace(reg, '&#039;');
	reg=new RegExp("\"", 'g');
	str=str.replace(reg, '&quot;');
	return str;

}
function stringescape(str){
	var a=new Array('\\', '\'', "\"", "\n");
	var b=new Array('\\\\', '\\\'', "\\\"", "\\\n");
	var reg;
	for(var i=0; i<a.length; i++){
		reg=new RegExp(a[i], 'g');
		str=str.replace(reg, b[i]);
	}
	return str;

}
function validateemail(email){
	email=email.replace(/(^)(\s*)/g, '$1');
	email=email.replace(/(\s*)($)/g, '$2');
	var i;
	for(i=0; i<email.length; i++){
		if(email.charCodeAt(i)>127) return '';
	}
	if( email.match(/^(.+)@(.+)\.([^.\s]{2,})$/) ) return email;
	return '';
}

function MemberGp(){
	this.getadminid=function(){
		return '1';
	}
	this.getvipid=function(){
		return '2';
	}
	this.getmemberid=function(){
		return '2';
	}
	this.getguestid=function(){
		return 3;
	}

	this.isadmin=function(id){
		return id==this.getadminid();
	}
	this.isvip=function(id){
		return id==this.getvipid() || this.isadmin(id);
	}
	this.ismember=function(id){
		return (id>0) && id!=this.getguestid();
//		return id==this.getmemberid() || this.isvip(id);
	}
}

function viewerinfo(){
	var viewer=new Object();
	var mgp=new MemberGp();
	viewer.gp=getCookie('membergp');
	if(!viewer.gp)viewer.gp=mgp.getguestid();
	viewer.id=getCookie('userid');
	viewer.isa=mgp.isadmin(viewer.gp);
	viewer.isvip=mgp.isvip(viewer.gp);
	viewer.ism=mgp.ismember(viewer.gp);
	return viewer;
}

function brokenimgcheck(name){
	var img=document.getElementsByName(name);
	for(var i=0; i<img.length; i++){
		if(!(img[i].complete) && img[i].fileSize && !(img[i].fileSize>2)){
			img[i].className='hidden';
		}
		else img[i].className='shown';
	}
}


function toggletr(id){
	var color1='#ffffff';
	var color2='#eeeeee';
	var i=0;
	for(var s=document.getElementById(id).firstChild; s!=null; s=s.nextSibling){
		if((++i)%2==1)s.style.backgroundColor=color1;
		else s.style.backgroundColor=color2;
	}
}
function toggletrneibr(id){
	var color1='#E4E4FA';
	var color2='#EDEDF9';
	var tr=document.getElementById(id);
//	if(arguments[1])color1=arguments[1];
//	if(arguments[2])color1=arguments[2];
	var color=color2;
	if(tr==null)return;
	for(tr=tr.nextSibling; tr!=null; tr=tr.nextSibling){
		color=color==color1 ? color2 : color1;
		tr.style.backgroundColor=color;
	}
}
function confirmgo(msg, url){
	if(confirm(msg))window.location.href=url;
}
function setSelect(s, v){
	if(!(s && s.options)) return;
	for(var i=0; i<s.options.length; i++){
		if(s.options[i].value==v){
			s.selectedIndex=i;
			return;
		}
	}
	return;
	if(arguments[2]!==null && arguments[2]!=''){
		s.options[s.options.length]=new Option(arguments[2], v);
		s.selectedIndex=s.options.length-1;
	}
}
function refreshSelect(s, v, t){
	if(v==null)return;
	if(s.value!=v) {
		s.options[s.options.length]=new Option(t, v);
		s.selectedIndex=s.options.length-1;
	}
	else if(s.options[s.selectedIndex].text!=t) s.options[s.selectedIndex].text=t;
}
function fitPic(p){
	window.open('http://www.gocarsite.com/picture.html?'+p,'fitWindow','height=100,width=100,top=0,left=0,z-lock=1,resizable=1');
}
function fitDialog(){
	var b=document.body;
	window.dialogWidth=parseInt(window.dialogWidth.substring(0, window.dialogWidth.length-2))+b.scrollWidth-b.clientWidth+'px';
	window.dialogHeight=parseInt(window.dialogHeight.substring(0, window.dialogHeight.length-2))+b.scrollHeight-b.clientHeight+'px';
	var x, y;
	x=(window.screen.width-parseInt(window.dialogWidth.substring(0, window.dialogWidth.length-2)))/2;
	y=(window.screen.height-parseInt(window.dialogHeight.substring(0, window.dialogHeight.length-2)))/2;
	if(x>=0)window.dialogLeft=x;
	else window.dialogLeft=0;
	if(y>=0)window.dialogTop=y;
	else window.dialogTop=0;
}
function winCenter(){
	var w, h;

	if(document.body.clientWidth){
		w=document.body.clientWidth;
		h=document.body.clientHeight;
	}
	else if(window.outerWidth){
		w=window.outerWidth;
		h=window.outerHeight;
	}
	else if(window.documentElement && window.documentElement.clientWidth){
		w=window.documentElement.clientWidth;
		h=window.documentElement.clientHeight;
	}
	else return;
	var x, y;
	//x=(screen.width-w)/2;
	//y=(screen.height-h)/2;
	x=(parent.width-w)/2;
	y=(parent.height-h)/2;
	if(!(x>0))x=0;
	if(!(y>0))y=0;
	window.moveTo(x,y);

}
function chksubmit(f){
	var fds, alt;
	if(arguments[1]!=null && arguments[1]!=''){
		if(arguments[2]==null || arguments[2]=='') arguments[2]=arguments[1];
		fds=arguments[1].split(',');
		alt=arguments[2].split(',');
		for(var i=0; i<fds.length; i++){
			p1=eval('f.'+fds[i]);
			if(p1.value==''){
				alert('請輸入'+alt[i]);
				return;
			}
		}
	}
	if(arguments[3]!=null && arguments[3]!=''){
		esc=arguments[3].split(',');
		for(var i=0; i<esc.length; i++){
			if(f[esc[i]] && f[esc[i]].value) f[esc[i]].value=htmlescape(f[esc[i]].value);
		}

	}
	f.submit();
	return true;
}
function modalOpen(url){
	return showModalDialog(url, window, "status:0;resizable:1;");
}
function clickwin(url){
	var w, h, n='clickwin';
	if(arguments[1]!=null)w=",width="+arguments[1];
	if(arguments[2]!=null)h=",height="+arguments[2];
	if(arguments[3]!=null)n=arguments[3];
	var win=window.open(url, n, 'toolbar=0'+w+h+',resizable=1,personalbar=0,status=0,scrollbars=1,menubar=0');
	win.focus();
}
function keySearch(ar, v){
	for(var i=0; i<ar.length; i+=2) {
		if(ar[i]==v) return i;
	}
	return -1;
}

function showhide(e, sh){
	var i, j, v;
	var d=[{display : 'none', visibility : 'hidden'}, {display : '', visibility : 'visible'}];
	if(arguments[2]) d[1].display=arguments[2];

	v=sh ? 1 : 0;
	e.style.display=d[v].display;
	e.style.visibility=d[v].visibility;
}

function confirmgo(msg, url){
	if(confirm(msg)){
		window.location.href=url;
	}

}

function ActSelect(select, act, opt){
	this.loadOptions=function(ops){
		var o=this.select.options;
		for(var i=o.length; i>0; i--)o[i]=null;
		if(ops!=null)addOptions(this.select, ops);
		else {
			if(this.predecessor==null) addOptions(this.select, this.opt);
			else {
				var v=this.predecessor.select.value;
				if(v!='') addOptions(this.select, this.opt[v]);
			}
		}
		this.selectChanged();
	}
	this.setSelect=function(v){
		setSelect(this.select, v);
		this.selectChanged();
	}

	this.refreshSelect=function(id, name){
		var a=this.select.value;
		refreshSelect(this.select, id, name);
		if(this.select.value!=a) this.selectChanged();
	}
	this.editSelect2=function(that){
		if(that.act==null) return;
		var v=that.select.value, a=that.act.value;
		var rv;

		switch(a){
			case 'new':
				var extra=that.predecessor==null ? '' : "&"+that.predecessor.select.name+"="+that.predecessor.select.value;
				rv=showModalDialog(that.handler+'?f=dialogedit&act=form&tbname='+that.select.name+extra, window,'status:0;resizable:1;');
				break;
			case 'edit':
				rv=showModalDialog(that.handler+'?f=dialogedit&act=form&tbname='+that.select.name+'&id='+v,window,'status:0;resizable:1;');
				break;
			case 'delete':
				if(confirm("Are you sure to delete \""+that.select.options[that.select.selectedIndex].text+"\" ?")) rv=showModalDialog(that.handler+'?f=dialogedit&act=delete&id='+v+'&tbname='+that.select.name,window,'status:0;resizable:1;dialogWidth:250px;dialogHeight:150px;');
				break;
			default:
				return;
		}
		if(rv!=null) {
			that.modalvalue=rv;
			that.selectEdited(rv);
		}
		that.act.selectedIndex=0;
		that.act.blur();
	}
	this.selectEdited=function(obj){
		if(this.act==null)return;
		var i=this.select.value, iname=this.select.options[this.select.selectedIndex].text, j=this.act.value;
		if(this.loadframe!='')document.frames(this.loadframe).history.go(0);
		var opt;
		if(this.predecessor==null) opt=this.opt;
		else{
			if(this.opt[this.predecessor.select.value]==null) this.opt[this.predecessor.select.value]=new Array();
			opt=this.opt[this.predecessor.select.value];
		}

		if(j=='delete'){
			var k=keySearch(opt, i);
			if(k>=0)opt.splice(k, 2);
			this.select.options[this.select.selectedIndex]=null;
		}
		else{
			if(obj.id==null)return;

			if(j=='new')opt.push(obj.id, obj.name);
			else if(obj.name!=iname){
				var k=keySearch(opt, i);
				if(k>=0)opt[k+1]=obj.name;
			}
			this.refreshSelect(obj.id, obj.name);
		}
		if(i!=this.select.value)this.selectChanged();
	}
	this.refreshAct=function(){
		if(this.act==null)return;
		var o=this.act.options, i=this.select.selectedIndex;
		if(this.select.value==''){
			o[3]=null;
			o[2]=null;
		}
		else{
			if(o[2]==null)o[2]=new Option('Edit', 'edit');
			if(o[3]==null)o[3]=new Option('Delete', 'delete');
		}
		if(this.predecessor!=null){
			if(this.predecessor.select.value=='')this.act.options[1]=null;
			else if(this.act.options[1]==null)this.act.options[1]=new Option('New', 'new');
		}
	}
	this.selectChanged2=function(that){
		that.preChange();
		that.refreshAct();
		if(that.successor!=null) that.successor.loadOptions();
		that.postChange();
	}
	this.preChange=function(){};
	this.postChange=function(){};

	this.select=select;
	this.act=act;
	this.opt=opt;
	this.predecessor=null;
	this.successor=null;
	this.vname='';
	this.handler='general.php';
	this.loadframe='';
	if(this.act!=null){
		this.act.options[0]=new Option('(Action)', '');
		this.act.options[1]=new Option('New', 'new');
	}
	this.select.options[0]=new Option('', '');
	this.modalvalue=null;
}
function ActSelect_create(vn, select, act, opt){
	eval(vn+'=new ActSelect(select, act, opt);'+vn+'.vname=vn;');
	eval(vn+".selectChanged=function(){ "+vn+".selectChanged2("+vn+");}");
	eval("select.onchange="+vn+'.selectChanged;');
	//eval("select.attachEvent('onchange', "+vn+'.selectChanged);');
	eval(vn+".editSelect=function(){ "+vn+".editSelect2("+vn+");}");
	//if(act!=null)eval("act.attachEvent('onchange', "+vn+'.editSelect);');
	if(act!=null)eval("act.onchange="+vn+'.editSelect;');
	if(arguments[4]!=null) eval(vn+".handler=arguments[4];");
}
function ActSelect_link(pre, suc){
	pre.successor=suc;
	suc.predecessor=pre;
}


function TextEditor(ta){
	this.ta=ta;

	this.addurl=function(){
		var u=prompt("Pls enter the url:", 'http://');
		if(u=='' || u==null) return 0;
		var t=prompt("pls enter the link text:", 'Link');
		if(t!='' && t!=null) this.insertstr("[url="+u+"]"+t+"[/url]");
	}
	this.addimg=function(){
		var u=prompt("Pls enter the url:", 'http://');
		if(u=='' || u==null) return 0;
		this.insertstr("[img]"+u+"[/img]");
	}
	this.addtag=function(t){
		var l, r;
		l='['+t;
		r='[/'+t+']';
		if(arguments[1])l+="="+arguments[1]
		l+=']';
		this.insertstr(l, r);
	}

	this.matchnum=function(exp, st){
		var reg=new RegExp(exp, 'gi');
		var r=st.match(reg);
		return (r && r.length) ? r.length : 0;
	}

	this.closealltag=function(){
		var c=this.ta.value;
		var a=['u', 'i', 'b', 'font=(?:\\w*?)', 'size=(?:\\w*?)', 'color=(?:\\w*?)'];
		var endt, d, d1;

		for(var i in a){
			endt=a[i].split('=', 2);
			d=matchnum('\\['+a[i]+'\\]', c)-matchnum('\\[/'+endt[0]+'\\]', c);
			if(d>0){
				for(d1=d; d1>0; d1--)c+='[/'+endt[0]+']';
			}
		}
		this.ta.value=c;
		this.insertstr('');
	}

	this.insertstr=function(str){
		this.ta.focus();
		var str1='';
		if(arguments[1])str1=arguments[1];
		if(document.selection){
			var range=document.selection.createRange();
			range.text=str+range.text+str1;
			if(str1){
				range.moveStart('character', -str1.length);
				range.moveEnd('character', -str1.length);
			}
			range.select();
		}
		else if(this.ta.selectionStart!=null){
			var sstart=this.ta.selectionStart;
			this.ta.value=this.ta.value.substr(0, this.ta.selectionStart)+str+str1+this.ta.value.substr(this.ta.selectionEnd);
			this.ta.selectionStart=this.ta.selectionEnd=sstart+str.length;

		}
		else{
			this.ta.value+=str+str1;
		}
	}
}

function Permission(sid){
	this.subj;
	this.gp={};

	var allgp=getdtmembergp();
	var viewer=viewerinfo();
	var gpid=viewer.gp;
	if(gpid && allgp[gpid])this.gp=allgp[gpid];
	var allsubj=getdtsubject();
	if(sid>0)this.subj=allsubj[sid];

	this.cando=function(act){
		if(!this.subj)return 1;
		return this.gp && this.subj && parseInt(this.gp['power']) >= parseInt(this.subj['apow'+act]);
	}
}
