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 translate(){
	var np;
	var n=location.pathname.substring(0, 4);
	if(n=='/eng') np='/big5/index.php';
	else np='/eng/index.php';
	var a=location.href.split('?', 2);
	if(a[1])location=np+'?'+a[1];
	else location=np;

}
function toggletr(id, color1, color2){
	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, color1, color2){
	var tr=document.getElementById(id);
	var color=color2;
	var n;
	if(arguments[3]!=null)n=1;
	else n=arguments[3];
	if(tr==null)return;
	for(tr=tr.nextSibling; tr!=null; tr=tr.nextSibling){
		if(tr.style.display=='none' || tr.style.display=='hidden') continue;
		color=color==color1 ? color2 : color1;
		tr.style.backgroundColor=color;
	}
}
function htmlescape(s){
	var r=s.replace(/</g, '&lt;');
	return r.replace(/>/g, '&gt;');	
}

function setSelect(s, v){
	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 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]!=''){
		var e=arguments[3].split(',');
		for(var i=0; i<e.length; i++){
			f[e[i]].value=htmlescape(f[e[i]].value);	
		}
	}
	f.submit();	
	return true;
}
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 modalOpen(url){
	return showModalDialog(url, window, "status:0;resizable:1;");	
}
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;
	var d=[{display : 'none', visibility : 'hidden'}, {display : 'inline', visibility : 'visible'}];
	for(i=0; i<e.length; i++){
		for(j=0; j<e[i].length; j++){
			e[i][j].style.display=d[sh[i]].display;
			e[i][j].style.visibility=d[sh[i]].visibility;
		}
	}
}
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');
	}
	if(this.select.options[0]==null)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 p='';
		if(arguments[1]!=null)p=arguments[1]+'=';
		if(t.value!='') this.insertstr('['+p+t.value+']');
		t.selectedIndex=0;
	}
	
	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+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 urlmodified(url, name, v){
	if(url==null || url=='')return '';
	var obj=new Object();
	var a=url.split('&');
	if(a==null || a.length==null)return '';
	for(var i=0; i<=a.length; i++){
		if(a[i]==null) continue;
		var a2=a[i].split('=', 2);
		if(a2[1]!=null && a2[1]!='') obj[a2[0]]=a2[1];	
	}
	obj[name]=encodeURIComponent(v);
	var r='';
	for(var i in obj)r+="&"+i+'='+obj[i];
	return r.substring(1);
}
function locationmodify(name, v){
	var s=window.location.search.substr(1);
	if(arguments[2] && url2value(s, name)==v)v=arguments[2];	
	window.location.search='?'+urlmodified(s, name, v);	
}
function loadimage(img){	//used in <img>, called by onload, img usually is 'this'
	var w=580;
	var w_rate=0.7;
	if(arguments[1]) w=arguments[1];
	this.alt='點擊看原圖';
	this.img=img;
	
	this.img_load=function(){
		var width=w?w:screen.width*w_rate;
		if(this.img.width>width){
			var src=this.img.src;
			this.img.resized=true;
			this.img.width=width;
			this.img.alt=this.alt;
			this.img.onmourseover=this.mourse_over();
			this.img.onclick=function(){window.open(src);};
		}
	}
	this.mourse_over=function(){
		this.img.style.cursor='hand';
		this.img.alt=this.alt;
	}
	this.img_load();
}