﻿/*
* 多功能多阶阵列
* 2007/3/20 Danny vmix.clan@msa.hinet.net
* 项目 [value, text]
* 群组 [项目, 子群组1, 子群组2, ...]
* 末群组 项項目, 子项目1, 子项目2, ...]
*/

var xaArrows='/images/arrows.gif'
if (location.hostname=='192.168.0.86') xaArrows='/168ClubWeb/images/arrows.gif'

// 视窗资讯
function winW() {
	return (window.innerHeight ? innerWidth + (document.width > innerWidth ? 20 : 0) : document.body.clientWidth);
}
function winH() {
	return (window.innerHeight ? innerHeight + (document.height > innerHeight ? 20 : 0) : document.body.clientHeight);
}

// 取自 MacroMedia Dreamweaver寻找物件
function MM_findObj(n, d) { //v4.03
  if(typeof(n)=='object') return n;
  var p,i,x,a;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(n.indexOf('.')!=-1) { a=n.split('.'); x=d; for (i=0;i<a.length;++i) if (!(x=x[a[i]])) break; }
  if(!x) x=d[n];
  if(!x && d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

// 鼠标绝对位置
// ex : onmouseover="status=m_point"
if (!window.m_point) { m_point = new Object(); m_point.x = m_point.y = 0; }
if (typeof(getMouseLoc_bak)=='undefined') {
	getMouseLoc_bak=document.onmousemove
	document.onmousemove=getMouseLoc
}
function getMouseLoc(e) {
  if (getMouseLoc_bak) getMouseLoc_bak(e)
  if (window.innerHeight) {	// !IE	Nav DOM
    m_point.x = e.pageX;
    m_point.y = e.pageY;
  } else {			// IE	!Nav DOM
    m_point.x = event.clientX + document.body.scrollLeft;
    m_point.y = event.clientY + document.body.scrollTop;
  }
  return true;
}

// 物件绝对位置
function abspos(o) {
  this.x = o.offsetLeft
  this.y = o.offsetTop
  while (o = o.offsetParent) {
    this.x += o.offsetLeft
    this.y += o.offsetTop
  }
}

// 建立功能表 : xaBuildMenus(选单名称, 上层(TD,DIV,other), XA阵列, 样式式[, 主功能表Class][, 子功能表Calss])
// 样式 : 直1 , 横0 , 浮动(偏移)[x偏移,y偏移] , 浮动(靠右)[null,0] , 浮动(靠下)[0,null]
// ex : xaBuildMenus('myMenu_1', 'myMenu', aMenu, [0,25], 'style1', 'style2')
function xaBuildMenus(zn,pn,a,vt,c0,c1,zidx) {
	var i, s, v, d, g, z, a2=[]
	var p=MM_findObj(pn)
	if (!p) return

	if (vt.constructor==Array) {
		z=document.body.insertBefore(document.createElement("DIV"), document.body.firstChild)
		z.style.position='absolute'
		z.style.visibility='hidden'
		z.style.zIndex=zidx
		z.Parent=p
		z.xaMenu=p.xaMenu
		z.X=vt[0]
		z.Y=vt[1]
		vt=1
		z.onmouseover=function() {Menu(z.id)}
		z.onmouseout=function() {Menu(z.id,-1)}
	} else {
		z=p.appendChild(document.createElement("DIV"))
		z.Parent=null
		z.xaMenu=null
	}
	z.style.padding=0
	z.id=zn
	s='<table cellspacing=0 cellpadding=0 class="'+c0+'">'
	if (!vt) s+='<tr>'
	for (i=1;i<a.length;++i) {
		g=(a[i][0].constructor==Array)
		d=(g?a[i][0]:a[i])
		s+=(vt?'<tr'+(g?' id="'+z.id+'_p'+i+'" onmouseover="Menu(\''+z.id+'_'+i+'\')" onmouseout="Menu(\''+z.id+'_'+i+'\',-1)"':'')+'>':'')+'<td'+(g&&!vt?' id="'+z.id+'_p'+i+'" onmouseover="Menu(\''+z.id+'_'+i+'\')" onmouseout="Menu(\''+z.id+'_'+i+'\',-1)"':'')+'>'+(d[0]>''?'<a href="'+d[0]+'">':'')+d[1]+(d[0]>''?'</a>':'')+'</td>'+(z.xaMenu?'<td>'+(g?'<img src="'+xaArrows+'">':'')+'</td>':'')+(vt?'</tr>':'')
	}
	if (!vt) s+='</td>'
	s+='</table>'
	z.innerHTML=s

	for (i=1;i<a.length;++i)
		if (a[i][0].constructor==Array) {
			MM_findObj(z.id+'_p'+i).xaMenu=z
			o=xaBuildMenus(z.id+'_'+i,z.id+'_p'+i,a[i],(vt==0?[0,null]:[null,0]),c1,c1,(zidx?zidx+1:10))
			if (o) a2[a2.length]=o
		}
	z.aChild=a2
	return z
}

// 显示隐藏浮动选单 : Menu(选单名[,动作(1显示(预设),0立即隐藏,-1延迟隐藏)
// ex : onmouseover="Menu('myMenu')" onmouseout="Menu('myMenu', -1)"
function Menu(n,m) {
	var o=MM_findObj(n)
	if (!o) return
	if (m==-1) {
		if (!o.Timer) {
			o.Timer=setTimeout('Menu("'+(o.id?o.id:n)+'",0)', 50)
			if (o.xaMenu && o.xaMenu.xaMenu) Menu(o.xaMenu,-1)
		}
		return
	}
	if (o.Timer) {clearTimeout(o.Timer);o.Timer=0}
	if (o.xaMenu && m!=0) {
		var ppos=new abspos(o.Parent)
		var opos=new abspos(o)
		var cpos=new abspos(o.firstChild.firstChild.firstChild)
		//status=p.x+','+p.y+','+o.Parent.offsetWidth+','+o.Parent.offsetHeight+','+o.X+','+o.Y
		//OuterHTML.value='0['+o.Parent.id+','+ppos.x+','+ppos.y+']['+o.id+','+opos.x+','+opos.y+']['+cpos.x+','+cpos.y+']'+o.style.top+','+o.offsetTop+'\n\n'+OuterHTML.value

		var MaxMenuLeft = winW() - o.clientWidth + (window.innerHeight?pageXOffset:document.body.scrollLeft);
		var MaxMenuTop = winH() - o.clientHeight + (window.innerHeight?pageYOffset:document.body.scrollTop);

		var _x=o.offsetLeft+ppos.x-(o.xaMenu.xaMenu?cpos.x:opos.x)
		var _y=o.offsetTop+ppos.y-(o.xaMenu.xaMenu?cpos.y:opos.y)

		var x=_x+(o.X==null?(o.xaMenu.offsetWidth-3):o.X)
		var y=_y+(o.Y==null?(o.xaMenu.offsetHeight):o.Y)
		if (x>MaxMenuLeft) x=_x-(o.X==null?(o.offsetWidth-3):o.X)
		if (y>MaxMenuTop) y=_y-(o.Y==null?(o.offsetHeight-3):o.Y)

		o.style.left=x
		o.style.top=y

		if (o.xaMenu.xaMenu) {Menu(o.xaMenu)}
	}
	if (m==0) {
		if (o.HideSelects && o.HideSelects.length>0) {
			var a=o.HideSelects
			for (var i=0;i<a.length;++i) {
				if (!--a[i].CapLength) a[i].style.visibility='visible'
			}
		}
		o.HideSelects=null
	} else {
		if (o.HideSelects==null) o.HideSelects=[]
		var p
		var opos2=new abspos(o)
		var a=document.getElementsByTagName('SELECT')
		for (var i=0;i<a.length;++i) {
			p=new abspos(a[i])
			if ((a[i].style.visibility!='hidden'||a[i].CapLength)&&p.x<(opos2.x+o.offsetWidth)&&opos2.x<(p.x+a[i].offsetWidth)&&p.y<(opos2.y+o.offsetHeight)&&opos2.y<(p.y+a[i].offsetHeight)) {
				a[i].style.visibility='hidden'
				a[i].CapLength=(a[i].CapLength?a[i].CapLength+1:1)
				o.HideSelects[o.HideSelects.length]=a[i]
			}
		}
	}
	o.style.visibility=(m==0?'hidden':'visible')
}

// 建立下拉选单选项 : xaBuildOptions(XA阵列, 建立第几個选单(从0开始) [, 物件表字串 或 物件阵列])
// ex : xaBuildOptions(a_sel,1,'Year,Class')
function xaBuildOptions(a,n,olist) {
  var sid,i,b,z,odata, d
  var a_obj = (olist ? olist : a[0])
  if (typeof(a_obj)=='string') a_obj = a_obj.split(',')
  for (i=0;i<a_obj.length;++i) a_obj[i]=MM_findObj(a_obj[i])
  for (i=a_obj.length-1;i>=n;--i) {
    z = a_obj[i]
    if (z && z.tagName) {
      if (typeof(z.length)!='undefined') {
        while (z[1]) z[1] = null
        if (!z[0]) z[0] = new Option(' -- 請選擇 --','')
      } else if (typeof(z.value)!='undefined') {
        z.value=''
      } else if (typeof(z.innerHTML)!='undefined') {
        z.innerHTML=''
      }
    }
  }
  if (n>0 && (typeof(a_obj[n-1].selectedIndex)=='undefined' || a_obj[n-1].selectedIndex<1)) return
  var odata = a
  for (i=0;i<n;++i)
    odata = odata[a_obj[i].selectedIndex]
  if (odata.constructor!=Array || odata[0].constructor!=Array) return
  if (typeof(z.length)!='undefined') {
    for (i=1;i<odata.length;++i) {
      b=(odata[i][0].constructor==Array)
      d = (b?odata[i][0]:odata[i])
      z[i] = new Option(h2t(d[1])+(b?'...':''),d[0])
    }
  } else {
    xaSetHTML(odata,z)
  }
}

// 設定下拉选单选项: xaSetOptions(XA阵列, 选择值  [, 物件表字串 或 物件阵列])
// ex : xaSetOptions(a_sel,'3,304,3042','Year,Class,No')
function xaSetOptions(a,s,olist) {
  var i, osel
  var a_val = s.split(',')
  var a_obj = (olist ? olist : a[0])
  if (typeof(a_obj)=='string') a_obj = a_obj.split(',')
  for (i=0;i<a_val.length;++i) {
    xaBuildOptions(a,i,olist)
    osel = MM_findObj(a_obj[i])
    if (osel) {
      osel.value=a_val[i]
      if (osel.selectedIndex<0) osel.selectedIndex=0
    }
  }
  if (a_val.length < a_obj.length)
    xaBuildOptions(a,a_val.length,olist)
}

// 建立检视表格 : xaBuildTables(XA阵列, 建立第几個选單(从0開始) [, 物件表字串 或 物件阵列])
// ex : xaBuildTables(a_sel,1,'Year,Class')
function xaSetHTML(a,so) {
  var i,d,r='',o=MM_findObj(so)
  if (typeof(o.value)!='undefined') {
    for (i=1;i<a.length;++i) {
      d=(a[i][0].constructor==Array?a[i][0]:a[i])
      r='['+d[0]+'] :\n\n'+d[1]+(r?'\n----------------------\n'+r:'')
    }
    o.value=r
  } else if (typeof(o.innerHTML)!='undefined') {
    r='<table border="1" cellpadding="3" bordercolor="#CCCCCC" style="border-collapse: collapse">'
    for (i=1;i<a.length;++i) {
      d=(a[i][0].constructor==Array?a[i][0]:a[i])
      r+='<tr><td rowspan=2 width=100></td><td style="background-color: #390; color:white; white-space: nowrap; filter: Alpha(Opacity=70, FinishOpacity=100, Style=1, StartX=0, StartY=0, FinishX=0, FinishY=100);">'+d[0]+'</td></tr><tr><td width=500 height=150 valign=top>'+d[1]+'</td></tr>'
    }
    r+='</table>'
    o.innerHTML=r
  }
}

// 查询该值路径 : xaFindValue(XA阵列, 值)
// ex : xaFindValue(a_sel,'3042') ← 传回 '3,304,3042'
function xaFindValue(a,s) {
  var i, r, z
  // 不判断自己的值, 只判断子项目的值, 自己的值由父项目判断, 避免值与选单名相同出错
  if (s==null || s=='' || a.constructor!=Array || a.length < 2 || a[1].constructor!=Array || a[1].length < 2) return ''
  for (i=1;i<a.length;++i) {
    z=a[i]
    if (z.constructor!=Array || z.length < 2) continue
    if (z.length>1 && z[0].constructor!=Array && z[1].constructor!=Array) { // 子项目
      if (s==z[0]) return s
    } else if (z[0].constructor==Array && z[0].length>1 && z[0][0].constructor!=Array && z[0][1].constructor!=Array) { // 子群组
      if (s==z[0][0]) {
        return s
      } else {
        r = xaFindValue(z,s)
        if (r > '') return z[0][0]+','+r
      }
    }
  }
  return ''
}

// HTML转纯文字
function h2t(s) {
  var r=s
  var re=/&#(\d+);/
  while (re.exec(r)) {r=r.replace(re,String.fromCharCode(RegExp.$1))}
  r=r.replace(/<br>/gi,'\r\n')
  r=r.replace(/<\/?p>/gi,'\r\n')
  r=r.replace(/&nbsp;/g,' ')
  r=r.replace(/&lt;/g,'<')
  r=r.replace(/&gt;/g,'>')
  r=r.replace(/&quot;/g,'"')
  r=r.replace(/&amp;/g,'&')
  return r
}


