// 換圖效果
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_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_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 sbar(st,color) {
	if (color=='') {color='#f5f5f5';}
	st.style.backgroundColor = color;
}

function cbar(st,color) {
	if (color=='') {color='#ffffff';}
	st.style.backgroundColor = color;
}

// 客製決定上下左右位置，沒設定的話就置中
var popupAdminWin;
function openWin2(url, winName, w, h, t, l, s)
{
  var cTop  = screen.height/2 - (h/2);
  var cLeft = screen.width/2 - (w/2);
  var t = (t === (void 0)) ? cTop : t;
  var l = (l === (void 0)) ? cLeft : l;
  var s = (s === (void 0)) ? 'no' : 'yes';
  if (!popupAdminWin) {
    popupAdminWin = window.open(url, winName, 'width=' + w + ',height=' + h + ',scrollbars=' + s + ',top=' + t + ',left=' + l + ',resizable=1');
  } else if (!popupAdminWin.closed) {
    popupAdminWin.location.href = url;
  } else {
    popupAdminWin = window.open(url, winName, 'width=' + w + ',height=' + h + ',scrollbars=' + s + ',top=' + t + ',left=' + l + ',resizable=1');
  }
  popupAdminWin.focus();
}

// 螢幕置中視窗
function openCenterWin(url, nameW, w, h, scroll)
{
  var s = (scroll === (void 0)) ? 0 : 1;
  if (navigator.appVersion.indexOf('4') != -1) {
    var cTop  = screen.height/2 - (h/2);
    var cLeft = screen.width/2 - (w/2);
    popupAdminWin = window.open(url, nameW, 'height=' + h + ',width=' + w + ',scrollbars=' + s + ',resizable=1,menubar=0,toolbar=0,status=0,location=0,directories=0,left=' + cLeft + ',top=' + cTop);
  }
  else {
    popupAdminWin = window.open(url, nameW, 'height=' + h + ',width=' + w + ',scrollbars=' + s + ',resizable=1,menubar=0,toolbar=0,status=0,location=0,directories=0,left=150,top=200');
  }
  popupAdminWin.focus();
}

function NewpdfUrl_ToWin(aspFile, Title, UrlFile){

	wTop = 0;
	wLeft= 0;
	if (screen.width == 1024)
		{hTop = 50;hLeft=100;}
	else
	 	{hTop = 25;hLeft=50;}
	 	
	var option = "menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizeable=1,top=" + hTop + ",left=" + hLeft + ",width=800,height=600";
	window.open(aspFile + '?UrlFile=' + UrlFile + '&Title=' + Title, 'OpenPDF', option);
}


function viewfileCount_ToWin(aspFile, Title, tn, nsn){
	wTop = 0;
	wLeft= 0;
	if (screen.width == 1024)
		{hTop = 50;hLeft=100;}
	else
	 	{hTop = 25;hLeft=50;}

	var option = "menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizeable=1,top=" + hTop + ",left=" + hLeft + ",width=800,height=600";
	//alert(tn);
	window.open(aspFile + '?tn=' + tn + '&nsn=' + nsn + '&Title=' + Title, 'OpenFile',option);
}

function NewimgUrl_ToWin(aspFile, Title, UrlFile){
	var img = new Image(); 
	img.src = UrlFile;

	wTop = 0;
	wLeft= 0;
	iWidth = 0;
	iHeight = 0;
	
	if (screen.width == 1024)
		{hTop = 100;hLeft=100;}
	else
	 	{hTop = 50;hLeft=50;}
	 	
	var size = ""
	if (img.width == 0 )
		//第一次抓到圖片一定是 0 
		{
			//size = "" 會 Disable size 的定義
			iWidth = 620;
			iHeight=440;
		}
	else
		{
			iWidth = img.width + 30;
			iHeight= img.height + 30;
		}
		
	size = ",top=" + hTop + ",left=" + hLeft + ",width=" + iWidth + ",height=" + iHeight ;

	var option = "menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizeable=1" + size;
	window.open(aspFile + '?UrlFile=' + UrlFile + '&Title=' + Title,'OpenImage',option);
}

function NewHtmlUrl_ToWin(aspFile, Title, showAspFile, iWidth, iHeight)
{
	var hTop = 0;
	var hLeft= 0;
	if (screen.width == 1024)
		{hTop = 50;hLeft=100;}
	else
	 	{hTop = 25;hLeft=50;}

	var option = "menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizeable=1,top=" + hTop + ",left=" + hLeft + ",width=" + iWidth + ",height=" + iHeight ;
	window.open(aspFile + '?UrlFile=' + showAspFile + '&Title=' + Title,'OpenImage', option);
}

/**
* 表單輸入欄位 MouseFouse 時，清除預設提示文字
*
* Here is an inline example:
* <code>
* <input type="text" name="k" value="請輸入關鍵字" style="width:90px;"
*        onfocus="clearMe(this);" onblur="restore(this, '請輸入關鍵字');" />
* </code>
*
* 預設的提示文字 ('請輸入關鍵字')，可在 submit 查詢後，由程式動態變化
*
* @param object the form input element object
*/
function clearMe(control)
{
  control.value = '';
}

/**
* 表單輸入欄位 MouseBlur 時，回復預設提示文字
*
* @param object the form input element object
* @param string the string to prompt
*/
function restore(control, str)
{
  if (control.value == '') control.value = str;
}

/**
* 過濾欄位字串前後空白用方法(並去除不合法字元)
* 此函式將在稍後宣告為 String Object's prototype method
*/
function trim()
{
  if (!this) return this;

  // 濾掉字串前後的空白
  for (var begin = 0; begin < this.length; begin++) {
    if (this.charAt(begin) != " ") break;
  }
  for (var end = this.length; end > 0; end--) {
    if (this.charAt(end - 1) != " ") break;
  }

  return this.slice(begin, end);
}

/**
* 檢查欄位是否只包含空白字元
* 此函式將在稍後宣告為 String Object's prototype method
*/
function isEmpty()
{
  if (!this) return true;

  for (var len = 0; len < this.length; len++) {
    if (this.charAt(len) != " ") return false;
  }

  return true;
}

// 將以上 trim 及 isEmpty 兩函式宣告為 String 的屬性
String.prototype.trim = trim;
String.prototype.isEmpty = isEmpty;

/**
* Submit 防呆功能
*
* 在送出表單且 JS 驗證無誤後，
* 將送出及重置的按鈕 disable 起來，
* 以防止傳輸時間過久，使用者懷疑未送出，
* 再重覆送一次資料給程式。
*
* @param object the form object
*/
function submitOnce(fm)
{
    // if IE 4+ or NS 6+
    if (document.all || document.getElementById) {
        // hunt down "submit" and "reset"
        for (var i = 0; i < fm.length; i++) {
            var tempobj = fm.elements[i];
            if (tempobj.type.toLowerCase() == "submit"
                    || tempobj.type.toLowerCase() == "reset") {
                //disable it
                tempobj.value = '傳輸中…';
                tempobj.disabled = true;
            }
        }
    }
}

function getLayerObj(name)
{
    if (document.getElementById)
    {
        return document.getElementById(name);
    }
    else if (document.all)
    {
        return document.all(name);
    }
    else if (document.layers)
    {
        return document.layers[name];
    }
}

/*
* Checks if a value exists in an array
*
* Input: strVal [string] -> the value wanna find
*        objArr [array]  -> search target array data
*
* Return: searches objArr for strVal and returns True
*         if it is found in the array, False otherwise
*/
function inArray(strVal, objArr)
{
    var hasVal = false;
    var size = objArr.length;
    
    if (size > 0)
    {
        for (var i = 0; i < size; i++)
        {
            if (strVal == objArr[i])
            {
                hasVal = true;
                break;
            }
        }
    }

    return hasVal;
}

