﻿//################
//   日曆按鈕
//################

/*  欄位輸入用日曆                         */
/*  需搭配 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                                    
function CHK_ID(id) {
   var chk_result = true;
   re = /^[a-zA-Z][a-zA-Z0-9_]{3,11}$/gi ;
   if (!re.test(id)){
     chk_result = false;
   }
   return chk_result;
}

//密碼檢查 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;
}

//檢查 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_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 doSomething(e) {
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	if (tg.nodeName == 'TABLE')
		//alert(tg.nodeName);
		MM_showHideLayers('org','','hide','yel','','hide','lightgreen','','hide','red','','hide','purple','','hide','blue','','hide');
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	while (reltg != tg && reltg.nodeName != 'BODY')
		reltg = reltg.parentNode
	if (reltg.nodeName == 'TABLE')
		//alert(reltg.nodeName);
		MM_showHideLayers('org','','hide','yel','','hide','lightgreen','','hide','red','','hide','purple','','hide','blue','','hide');
	// Mouseout took place when mouse actually left layer
	// Handle event
}

function MM_jumpMenu(targ,selObj,restore)
{
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

