function get_tag(n,d) {
  var p,i,x;
  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(!(x=d[n])&&d.all)
     x=d.all[n];
  for (i=0;!x&&i<d.forms.length;i++) {
     for (j=0;!x&&j<d.forms[i].elements.length;j++) {
        if (d.forms[i].elements[j].name==n) x=d.forms[i].elements[j];
     }
  }
  for(i=0;!x&&d.layers&&i<d.layers.length;i++)
     x=get_tag(n,d.layers[i].document);
  if(!x && document.getElementById)
     x=document.getElementById(n);
  return x;
}

//顯示訊息
function showMsg(msg) {
   if (msg==0 || msg=="0" || msg=="&nbsp;" || msg=="" || msg==" " || msg.length<2) return;
   alert(msg);
}

//全選 取消全選
function CheckMe(formname,tagname,xstatus) {
   obj = get_tag(formname);
   for ( i=0; i<obj.length; i++) {
      if ( obj[i].name == tagname) {
         if (xstatus=='all' || xstatus==true) obj[i].status = true;
         else if (xstatus=='verbose') obj[i].status = !obj[i].status;
         else obj[i].status = false;
      }
   }
}

//確認是否刪除
function chk_del(form1) {
  if (!confirm("確定刪除？"))
    return;
  form1.submit();
}

//確認是否刪除
function chk_del2(frm) {
  if (!confirm("確定刪除？"))
    return;
  frm.submit();
}

//送出表單	Created by 楊正誠 2003/12/15 上午 9:42:53
function Submit(formname) {
	var n = "document."+formname+".submit()";
	eval(n);
}

//開新視窗
function WinPopUp(URL,w,h,f,x,y)
{
   if(!w || w==0) var features = 'width=800';
   else var features = 'width=' + w;
   if(!h || h==0) var features = features + ',height=600';
   else var features = features + ',height=' + h;
   if (f==0 || !f)
      var features = features + ",scrollbars=yes";
   else
      var features = f;
   var POP = window.open(URL,'POPWINDOWS',features);
   POP.moveTo(x,y);
   POP.focus();
   return;
}

function ImgShow(Output,img,w,h,type,layout,link,target,alt) {
   if (!img || img=='&nbsp;') {
      if (Output==0) {return;}
      else if (Output==1) {document.write("目前無圖片");}
      else {document.write(Output);}
   }
   else {
      Iw = (w && w!='&nbsp;') ? ' width="'+w+'" ' : ''; Ih = (h && h!='&nbsp;') ? ' height="'+h+'" ' : ''; type = (type && type!='&nbsp;') ? type : 'IMAGE';
      switch(type) {
         case 'IMAGE':
            if (alt && alt!='&nbsp;') Ialt = ' alt="'+alt+'" '; else Ialt = '';
            str =  '<img src="'+img+'" border="0"'+Iw+Ih+Ialt+'>';
            if (!target || target=='&nbsp;') target= ' '; else target = ' target="'+target+'" '
            if (link && link!='0' && link!='&nbsp;') str = '<a href="'+link+'"'+target+'>'+str+'</a>';

         break;
         case 'FLASH':
            str = '<table '+Iw+Ih+' border=0 cellspacing=0 cellpadding=0><tr><td align="center"><OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+
            ' codebase="//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" '+
            Iw+Ih+'><PARAM NAME=movie VALUE="'+img+'"><param name=quality value=high>'+'<param name="wmode" value="opaque">'+
            '<EMBED src="'+img+'" quality=high '+Iw+Ih+' TYPE="application/x-shockwave-flash" '+
            'PLUGINSPAGE="//www.macromedia.com/go/getflashplayer" WMode="opaque">'+
            '</EMBED></OBJECT></td></tr></table>';
         break;
      }
      if (layout=='0') layout = false;
      if (!layout) document.write(str);
      else return str;
   }

}

function ImgShow2(Output,img,w,h,type,layout,link,target,alt,append) {
   if (!img || img=='&nbsp;') {
      if (Output==0) {return;}
      else if (Output==1) {document.write("目前無圖片");}
      else {document.write(Output);}
   }
   else {
      Iw = (w && w!='&nbsp;') ? ' width="'+w+'" ' : ''; Ih = (h && h!='&nbsp;') ? ' height="'+h+'" ' : ''; type = (type && type!='&nbsp;') ? type : 'IMAGE';
      switch(type) {
         case 'IMAGE':
            if (alt && alt!='&nbsp;') Ialt = ' alt="'+alt+'" '; else Ialt = '';
            if (append && append!='&nbsp;') appendstr= append;
            str =  '<img src="'+img+'" border="0"'+Iw+Ih+Ialt+' '+appendstr+'>';
            if (!target || target=='&nbsp;') target= ' '; else target = ' target="'+target+'" '
            if (link && link!='0' && link!='&nbsp;') str = '<a href="'+link+'"'+target+'>'+str+'</a>';

         break;
         case 'FLASH':
            str = '<table '+Iw+Ih+' border=0 cellspacing=0 cellpadding=0><tr><td align="center"><OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+
            ' codebase="//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" '+
            Iw+Ih+'><PARAM NAME=movie VALUE="'+img+'"><param name=quality value=high>'+'<param name="wmode" value="opaque">'+
            '<EMBED src="'+img+'" quality=high '+Iw+Ih+' TYPE="application/x-shockwave-flash" '+
            'PLUGINSPAGE="//www.macromedia.com/go/getflashplayer" WMode="opaque">'+
            '</EMBED></OBJECT></td></tr></table>';
         break;
      }
      if (layout=='0') layout = false;
      if (!layout) document.write(str);
      else return str;
   }

}

function CheckMe(formname,tagname,xstatus) {
   obj = get_tag(formname);
   for ( i=0; i<obj.length; i++) {
      if ( obj[i].name == tagname) {
         if (xstatus=='all' || xstatus==true) obj[i].checked = true;
         else if (xstatus=='verbose') obj[i].checked = !obj[i].checked;
         else obj[i].checked = false;
      }
   }
}

function chk_status(s) {
	if(s=='ON') document.write("開啟");
	else if(s=='OFF') document.write("關閉");
    else if(s=='DEL') document.write("移除");
    else document.write(s);
}

function MustFill(s) {
	if(s==1) {
		document.write ("<font color=red size=2>*(必填寫)</font>");
	}
	else if(s==2) {
		document.write ("<font color=red size=2>*</font>");
	}
	else if(s==3) {
		document.write ("<font color=red size=2>必填寫</font>");
	}
}

//btn_type =>   1 (至頂)   2(上移)   3(下移)   4(至底)  5(ALL)
function OrderBtn(p,formname,tag,btn_type) {
   var Tstr = 'input type=button value="至頂" name=T1 ';
   var Ustr = 'img border="0" src="/default/member/img/up.gif" width="42" height="14" name=U'+p+' ';
   var Dstr = 'img border="0" src="/default/member/img/down.gif" width="42" height="14" name=D'+p+' ';
   var Total = eval("document."+formname+".total.value");
   var Bstr = 'input type=button value="至底" name=B'+Total+' ';
   if (p==1) { Ustr += 'disabled '; Tstr += 'disabled ';}
   if (p==Total) { Dstr += 'disabled '; Bstr += 'disabled ';}
   Ustr += 'onclick="OrderMv('+p+',1,\''+tag+'\',\''+formname+'\')" ';
   Dstr += 'onclick="OrderMv('+p+',0,\''+tag+'\',\''+formname+'\')" ';
   Tstr += 'onclick="OrderMv('+p+',2,\''+tag+'\',\''+formname+'\')" ';
   Bstr += 'onclick="OrderMv('+p+',3,\''+tag+'\',\''+formname+'\')" ';
   switch (btn_type) {
      case '1': 
         document.write("<"+Tstr+">");
         break;
      case '2':
         document.write("<"+Ustr+">");
         break;
      case '3':
         document.write("<"+Dstr+">");
         break;
      case '4':
         document.write("<"+Bstr+">");
         break;
      case '5':
         document.write("<"+Ustr+"> <"+Dstr+"> <"+Tstr+"> <"+Bstr+">");
         break;
   }
}

function OrderMv(p,opt,tag,formname) {
   if(opt==1) p1 = p-1;
   else if(opt==0) p1 = p+1;
   else if(opt==2) p1 = 1;
   else if(opt==3) p1 = eval("document."+formname+".total.value");
   //var p1 = (opt==1) ? p-1 : p+1;
   var n  = "document."+formname+".new_disp"+p+".value";  m  = eval(n);
   var n1 = "document."+formname+".new_disp"+p1+".value"; m1 = eval(n1);
   var T  = get_tag(tag+p);  h  = T.innerHTML;
   var T1 = get_tag(tag+p1); h1 = T1.innerHTML;
   T.innerHTML = h1; T1.innerHTML = h;
   eval(n+"='"+m1+"'"); eval(n1+"='"+m+"'");
}
//by alex }}}

function big_ImgShow(Output,img,w,h,type,layout,link,target,alt,append_str) {
   if (!img || img=='&nbsp;') {
      if (Output==0) {return;}
      else if (Output==1) {document.write("目前無圖片");}
      else {document.write(Output);}
   }
   else {
      Iw = (w && w!='&nbsp;') ? ' width="'+w+'" ' : ''; Ih = (h && h!='&nbsp;') ? ' height="'+h+'" ' : ''; type = (type && type!='&nbsp;') ? type : 'IMAGE';
      switch(type) {
         case 'IMAGE':
            if (alt && alt!='&nbsp;') Ialt = ' alt="'+alt+'" '; else Ialt = '';
            str =  '<img src="'+img+'" border="0"'+Iw+Ih+Ialt+' '+append_str+'>';
            if (!target || target=='&nbsp;') target= ' '; else target = ' target="'+target+'" '
            if (link && link!='0' && link!='&nbsp;') str = '<a href="'+link+'"'+target+'>'+str+'</a>';

         break;
         case 'FLASH':
            str = '<table '+Iw+Ih+' border=0 cellspacing=0 cellpadding=0><tr><td align="center" '+append_str+'><OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+
            ' codebase="//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" '+
            Iw+Ih+'><PARAM NAME=movie VALUE="'+img+'"><param name=quality value=high>'+
            '<EMBED src="'+img+'" quality=high '+Iw+Ih+' TYPE="application/x-shockwave-flash" '+
            'PLUGINSPAGE="//www.macromedia.com/go/getflashplayer">'+
            '</EMBED></OBJECT></td></tr></table>';
         break;
      }
    
      if (layout=='0') layout = false;
      if (!layout) document.write(str);
      else return str;
   }

}


function show_img(div_id,index) {
	var str= '';
	obj = document.getElementById(div_id); 
	obj.style.display = '';
}

//關閉圖層
function noshow_img(div_id) {
	obj = document.getElementById(div_id); 
	obj.style.display = 'none';
}


//AJAX FUNCTION 
var http_request = false;
var ajax_id;

//id: 為欲傳回文字寫入的html標籤 ,  url:執行的網址(需要在同一個網址底下
//method: 
//1. GET (param不用傳)    
//2. POST (param參數為必填,需為格式 name=value&anothername=othervalue&so=on )   
//3. FORM 利用原本html中form表單的tag ,使用者填入的值直接傳進去 (param為必填form 裡要傳的tag name，需為格式 tag_name1,tag_name2,so_on)

function makeRequest(id,url,method1,param,debug) {
    http_request = false;
    ajax_id = id;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        if(debug==1) {
        	alert('Giving up :( Cannot create an XMLHTTP instance');
        }
        return false;
    }
    
    var method2='';
	if(method1=='GET') {
		param = null;
		method2 = method1;
	}
	else {
		method2 = 'POST';
		if(method1=='FORM') {
			var frm ,obj,new_str='';
			var tag_ary = param.split(',');
			for(i=0;i<tag_ary.length;i++) {
				obj = get_tag(tag_ary[i]);
				if(obj) {
					if(new_str.length>0) new_str+='&';
					new_str+=tag_ary[i]+'='+obj.value;
				}
			}
			param = new_str;
		}
		if(param.length==0) param=null;
	}
    
    http_request.onreadystatechange = writeContents;
    http_request.open(method2, url, true);
    if(method2=='POST') {
    	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    }
    http_request.send(param);

}

function writeContents() {
	obj = document.getElementById(ajax_id);
	if(obj) {
		if (http_request.readyState == 1) {
			obj.innerHTML = '資料讀取中，請稍待...';
		}
		else if (http_request.readyState == 2) {
			obj.innerHTML = '已讀取...';
		}
		else if (http_request.readyState == 3) {
			obj.innerHTML = '資料交換中...';
		}
	    else if (http_request.readyState == 4) {
	        if (http_request.status == 200) {
	        	obj.innerHTML =http_request.responseText;
	        }
	    }
	}
}

function DivShow(DivName, status) {
   var Obj = get_tag(DivName);
   if (status=='0') status = false;
   if (status) Obj.style.display = '';
   else Obj.style.display = 'none';
}