﻿//################
//   日曆按鈕
//################

/*  欄位輸入用日曆                         */
/*  需搭配 dynCalendar 模組的 js 一併使用  */
function dynCalendar(objName, targetItem, date_Format){
   this.objName     = objName;
   this.targetItem  = targetItem;
   this.date_Format = arguments[2] ? arguments[2] : 'Y-m-d';
   //Public Methods
   this.show  = dynCalendar_show;
   this.erase = dynCalendar_erase;

   //document.write('<img src="../../i/dynCalendar_erase.gif" onClick="' + this.objName + '.erase();" style="cursor:pointer;" border="0" align="middle" alt="清除日期" />');
   document.write('<img src="/admin/dynCalendar/images/dynCalendar.gif" onClick="' + this.objName + '.show();" style="cursor:pointer;" border="0" align="middle" alt="開啟行事曆" />');
   
   //firefox
   if(!document.all){
     document.captureEvents(Event.MOUSEDOWN);
     document.onmousedown = function(e){
        dynCalendar_mouseX = e.screenX;
        dynCalendar_mouseY = e.screenY;
     }
     document.releaseEvents(Event.MOUSEDOWN);
   } 
}

function dynCalendar_show(){
   objName = this.objName;
   targetItemID = this.targetItem;
   date_Format = this.date_Format;
   var CalendarUrl = "/admin/dynCalendar/Calendar.php?objName=" + objName + "&targetItemID=" + targetItemID + "&date_Format=" + date_Format;

   if(document.all){
     //IE
     dynCalendar_mouseX = event.screenX;
     dynCalendar_mouseY = event.screenY;
   }else{
     //firefox
     document.onmousedown = function(e){
        dynCalendar_mouseX = e.screenX;
        dynCalendar_mouseY = e.screenY;
     }
     document.releaseEvents(Event.MOUSEDOWN);
   }
   window.open(CalendarUrl,"Calendar","top="+dynCalendar_mouseY+",left="+dynCalendar_mouseX+",width=230,height=200,resizable=no,scrollbars=no");
}

function dynCalendar_erase(){
   var targetElement = document.getElementById(this.targetItem);
   var aaa = document.all[this.objName];
   targetElement.value = '';
}


//################
//   視窗 & Frame
//################

/* iframe隨內容自動調整高度 */
// <iframe name="iframe1" id="iframe1" onload="javascript:dyniframesize('iframe1');" frameborder="0" style="border: 1px solid gray"  width="100%" src=""></iframe>

function dyniframesize(iframename) {
  var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
  //extra height in px to add to iframe in FireFox 1.0+ browsers
  var FFextraHeight=getFFVersion>=0.1? 16 : 0 

  var pTar = null;
  if (document.getElementById){
    pTar = document.getElementById(iframename);
  }else{
    eval('pTar = ' + iframename + ';');
  }
  if (pTar && !window.opera){
    //begin resizing iframe
    pTar.style.display="block"
    
    if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){
      //ns6 syntax
      pTar.height = pTar.contentDocument.body.offsetHeight+FFextraHeight; 
    } else if (pTar.Document && pTar.Document.body.scrollHeight){
      //ie5+ syntax
      pTar.height = pTar.Document.body.scrollHeight;
    }
  }
}


//################
//   檢查欄位
//################

// 檢查ID是否符合規則                                             
// 第一個字元必須為字母，其他字元需為字母或數字或_ , 長度4~12字元 
// INPUT: 字串                                                    
// OUTPUT: boolean, true/false                                    

//身分證檢查程式開始
var local=new Array(36)
local[10]='A'
local[11]='B'
local[12]='C'
local[13]='D'
local[14]='E'
local[15]='F'
local[16]='G'
local[17]='H'
local[18]='J'
local[19]='K'
local[20]='L'
local[21]='M'
local[22]='N'
local[23]='P'
local[24]='Q'
local[25]='R'
local[26]='S'
local[27]='T'
local[28]='U'
local[29]='V'
local[32]='W'
local[30]='X'
local[31]='Y'
local[33]='Z'
local[34]='I'
local[35]='O'

function CHK_ID(id){
	id=id.toUpperCase()
	if(firstlettererr(id))
	{
		return false;
	}
	else if(numerr(id))
	{
		return false;
	}
	else if(checkerr(id))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function firstlettererr(id)
{
	var fl=id.substring(0,1)
	var haserr=1
	for(i=10;i<=35;i++)
	{
		if(local[i]!=fl)
		continue
		else
		{
			haserr=0
			break
		}
	}
	if(haserr==1)
	return 1
	else
	return 0
}

function numerr(id){
	var haserr=0
	for(i=1;i<=9;i++)
	{
		if(parseInt(id.substring(i,i+1))>0 || id.substring(i,i+1)=='0')
		continue
		else
		{
			haserr=1
			break
		}
		if(haserr==1)
		return 1
		else
		return 0
	}
}

function checkerr(id){
	var se=new Array(10)
	var we=0
	var checkcode=0
	for(i=10;i<=35;i++){
		if(local[i]==id.substring(0,1))
		{
			se[0]=parseInt((i+'0').substring(0,1))
			se[1]=parseInt((i+'0').substring(1,2))
			break
		}
	}
	for(i=1;i<=9;i++)
	{
		se[i+1]=parseInt(id.substring(i,i+1))
	}
	for(i=0;i<=10;i++)
	{
		if(i==0)
		we=we+se[i]
		else
		we=we+(se[i]*(10-i))
	}
	if (mod(we,10)==0)
	{
		checkcode=0
	}
	else
	{
		checkcode=((10-mod(we,10))+'0').substring(0,1)
	}
	if(checkcode!=id.substring(9,10))
	return 1
	else
	return 0
}

function mod(a,b){
	var r
	r=Math.round(a/b)
	if((b*r)>a)
	r-=1
	return (a-(b*r))
}
//身分證檢查程式結束

//密碼檢查 4~20字元
function CHK_PASSWD(passwd) {
   var chk_result = true;
   re = /^.{4,20}$/gi ;
   if (!re.test(passwd)){
     chk_result = false;
   }
   return chk_result;
}

//檢查email是否符合格式
function CHK_EMAIL(email) {
   var chk_result = true;
   //re = /^.+@.+\..{2,3}$/gi ;
   var re = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/ ;
   
   if (!re.test(email)){
     chk_result = false;
   }
   return chk_result;
}

function CHK_Text(Obj)
{
	for(var i=0 ; i<Obj.length ; i++)
	{
		if(Obj[i].value) return true;
	}
	return false;
}

//檢查 checkbox radio 是否點選
function CHK_CHECKED(Obj) {
   var itemChecked = null;
   for(var i=0 ; i<Obj.length ; i++) {
      if(Obj[i].checked)
         return true;
   }
   return false;
}

//檢查資料夾或檔名是否符合格式
//符號僅能使用.-_ 但不得為首字
//不得使用繁體等多位元字
function CHK_FILENAME(filename) {
   var chk_result = true;
 	 re = /^[a-zA-Z0-9][a-zA-Z0-9\.\-_]*$/gi ;
   if (!re.test(filename)){
     chk_result = false;
   }
   return chk_result;
}

//檢查資料夾或檔名是否符合格式
//符號僅能使用.-_
//允許使用繁體等多位元字
function CHK_mbFILENAME(filename) {
   var chk_result = true;
 	 re = /^[^\(\)\[\]\{\}\/\\ \?\+\*\|\^\$\,~#@<>":;'&%!`=　]+$/gi;
   if (!re.test(filename)){
     chk_result = false;
   }
   return chk_result;
}

//################
//   應用工具
//################

//加入最愛
function addBookmarkForBrowser(sTitle, sUrl)
{
	if (window.sidebar)
	{
		window.sidebar.addPanel(sTitle, sUrl, "");
	}
	else if (window.external)
	{
		window.external.AddFavorite(sUrl, sTitle);
	}
	else
	{
		alert("Your browser does not support this feature！");
	}
}

//跑馬燈
//main 目標ID，height 高度，direct 移動方向(up 或 down)，pause 靜止時間，speed 轉動速度。
function scrollObject(main, height, direct, pause, speed) {
	var self = this;
	this.main = main;
	this.height = height;
	this.direct = direct;
	this.pause = pause;
	this.speed = Math.max(1.001, Math.min((direct == "up" || direct == "down") ? height : width, speed));
	this.block = new Array();
	this.blockprev = this.offset = 0;
	this.blockcurr = 1;
	this.mouse = false;
	this.scroll = function() {
		if (!document.getElementById) return false;
		this.main = document.getElementById(this.main);
		while (this.main.firstChild) this.main.removeChild(this.main.firstChild);
		this.main.style.overflow = "hidden";
		this.main.style.position = "relative";
		this.main.style.height = this.height + "px";
		for (var x = 0; x < this.block.length; x++) {
			var table = document.createElement('table');
					table.cellPadding = table.cellSpacing = table.border = "0";
					table.style.position = "absolute";
					table.style.left = table.style.top = "0px";
					table.style.height = this.height + "px";
					table.style.overflow = table.style.visibility = "hidden";
				var tbody = document.createElement('tbody');
					var tr = document.createElement('tr');
						var td = document.createElement('td');
								td.innerHTML = this.block[x];
							tr.appendChild(td);
						tbody.appendChild(tr);
					table.appendChild(tbody);
			this.main.appendChild(this.block[x] = table);
		}
		if (this.block.length > 1) {
			this.main.onmouseover = function() { self.mouse = true; }
			this.main.onmouseout = function() { self.mouse = false; }
			setInterval(function() {
				if (!self.offset && self.scrollLoop()) self.block[self.blockcurr].style.visibility = "visible";
			}, this.pause);
		} this.block[this.blockprev].style.visibility = "visible";
	}
	this.scrollLoop = function() {
		if (!this.offset) {
      if (this.mouse) return false;
			this.offset = (this.direct == "up" || this.direct == "down") ? this.height : this.width;
    } else this.offset = Math.floor(this.offset / this.speed);
		if (this.direct == "up" || this.direct == "down") {
			this.block[this.blockcurr].style.top = ((this.direct == "up") ? this.offset : -this.offset) + "px";
			this.block[this.blockprev].style.top = ((this.direct == "up") ? this.offset - this.height : this.height - this.offset) + "px";
		} else {
			this.block[this.blockcurr].style.left = ((this.direct == "left") ? this.offset : -this.offset) + "px";
		}
		if (!this.offset) {
			this.block[this.blockprev].style.visibility = "hidden";
			this.blockprev = this.blockcurr;
			if (++this.blockcurr >= this.block.length) this.blockcurr = 0;
		} else setTimeout(function() { self.scrollLoop(); }, 30);
    return true;
	}
}

function MM_findObj(n, d) { //v4.01
  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++) 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;
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

function MM_showHideLayers() { //v6.0
	var i,p,v,obj,args=MM_showHideLayers.arguments;
	for (i=0; i<(args.length-2); i+=3)
	if ((obj=MM_findObj(args[i])) != null)
	{
		v = args[i+2];
		if (obj.style)
		{
			obj=obj.style;
			v = (v == 'show') ? 'visible' : (v=='hide') ? 'hidden' : v;
		}
		obj.visibility = v;
	}
}

function MM_displayLayers() {
   //v6.0
   var i, p, v, obj, args = MM_displayLayers.arguments;
   for (i=0; i<(args.length-2); i += 3) {
      if ((obj=MM_findObj(args[i])) != null) {
         v = args[i+2];
         if (obj.style) {
            obj = obj.style;
            v = (v == 'show') ? '' : (v == 'hidden') ? 'none' : v;
         }
         obj.display = v;
      }
   }
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function getObj(id,d)
{
	var i,x;  if(!d) d=document; 
	if(!(x=d[id])&&d.all) x=d.all[id]; 
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][id];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=getObj(id,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(id); 
	return x;
}

function MM_jumpMenu(targ,selObj,restore)
{
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}


function setDisabledSelect()
{
	if(navigator.appName != 'Microsoft Internet Explorer')
		return; // only for bogus IE
	if(document.getElementsByTagName)
	{
		var s = document.getElementsByTagName("select");
		if (s.length > 0)
		{
			window.select_current = new Array();
			for (var i=0, select; select = s[i]; i++)
			{
				select.onfocus = function(){ window.select_current[this.id] = this.selectedIndex; }
				select.onchange = function(){ restore(this); }
				emulate(select);
			}
		}
	}
}

function restore(e)
{
	if (e.options[e.selectedIndex].disabled)
	{
		e.selectedIndex = window.select_current[e.id];
	}
}

function emulate(e)
{
	for (var i=0, option; option = e.options[i]; i++)
	{
		if (option.disabled)
		{
			option.style.color = "graytext";
		}
		else
		{
			option.style.color = "menutext";
		}
	}
}