/**************************************************************************
* functions.js 2006/12/07 v1.1
===========================================================================
* 常用的javascript功能
===========================================================================
* Edited by Pai in GOGOTDI
* Copyright (C) 2006 Tang Digital Integration
***************************************************************************/

/**
 * openCenterWin()
 * 螢幕置中視窗
 *
 * @param	string	url		內容網址
 * @param	int		Winname	視窗名稱
 * @param	int		w		視窗寬
 * @param	int		h		視窗長
 * @param	int		s		視窗捲軸
 *
 * @return	void
 */
function openCenterWin(url, Winname, w, h, s)
{
  //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);
    popupWin = window.open(url, Winname, 'height=' + h + ',width=' + w + ',scrollbars=' + s + ',resizable=1,menubar=0,toolbar=0,status=0,location=0,directories=0,left=' + cLeft + ',top=' + cTop);
  }
  else {
    popupWin = window.open(url, Winname, 'height=' + h + ',width=' + w + ',scrollbars=' + s + ',resizable=1,menubar=0,toolbar=0,status=0,location=0,directories=0,left=150,top=100');
  }
  popupWin.focus();
}

function openCenterWin2(url, Winname, w, h, s)
{
  //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);
    popupWin = window.open(url, Winname, 'height=' + h + ',width=' + w + ',scrollbars=' + s + ',resizable=1,menubar=1,toolbar=0,status=0,location=0,directories=0,left=' + cLeft + ',top=' + cTop);
  }
  else {
    popupWin = window.open(url, Winname, 'height=' + h + ',width=' + w + ',scrollbars=' + s + ',resizable=1,menubar=1,toolbar=0,status=0,location=0,directories=0,left=150,top=100');
  }
  popupWin.focus();
}

/**
 * switchAllChecked()
 * 切換全選的checkBox
 * 相關function refElement、checkbox_all_checked、is_single_element
 *
 * @param	string	oSrc	表單名稱
 * @param	array	oDes	要切換的checkBox陣列
 * @return	void
 */
function switchAllChecked(oSrc, oDes)
{
	if(!oSrc) return;
	if(!oDes) return;

	if (typeof(oDes) == "undefined" || oDes == null) {
		oDes = refElement('listchk[]', oSrc.form);
	}
	checkbox_all_checked(oDes, oSrc.checked);
}

function refElement(rE, rF)
{
	var oF;
	if (typeof(rE) == "object") {
		return rE;
	} else if (typeof(rE) == "string" || typeof(rF) == "number") {
		if (oF = refForm(rF)) {
			return oF.elements[rE];
		} else {
			return false;
		}
	} else {
		return false;
	}
}

/**
 * checkbox_all_checked()
 * 判斷是否為陣列
 *
 * @param	array	$o	要切換的checkBox陣列
 * @param	array	$b	表單名稱.checked
 * @return	bool
 */
function checkbox_all_checked(o, b)
{
	//var b = new Boolean(b);
	if (is_single_element(o)) {
		o.checked = b;
	} else {
		var i;
		for (i=0; i<o.length; i++) {
			o[i].checked = b;
		}
	}
}

/**
 * is_single_element()
 * 判斷是否為陣列
 *
 * @param	array	$o	動作確認訊息
 * @return	bool
 */
function is_single_element(o)
{
	return (typeof(o.length) == "undefined");
}

/**
 * check_Delete()
 * 動作確認訊息
 *
 * @param	string	$msg	動作確認訊息
 * @return	string	$msg	傳回確認訊息
 */
function check_Delete(msg)
{
	return window.confirm(msg);
}
 
/**
 * goURL()
 * 網址導向
 *
 * @param	string	$url_link	要導向的網址
 * @param	string	$url_target	目標 (top.parent.self)
 * @return	void
 */
function goURL(url_link, url_target)
{
	url_target.location = url_link;
}

/**
 * checkbox_switch()
 * 針對某個checkbox陣列做全選、或全部取消的動作
 *
 * @param	array	checkbox	要切換的 checkbox 陣列
 * @param	int		type		使用的功能, 1為全選, 0為全部取消
 * @return	void
 */
function checkbox_switch(checkbox, type)
{
	//判斷是否為陣列，否則以單筆資料方式做處理
	if (typeof(box.length) == "undefined"){
		if(box.type == 'checkbox' && type == 1){
			box.checked = true;
		}
		else if(box.type == 'checkbox' && type == 0){
			box.checked = false;
		}			
	} 
	else {
		var boxLen = box.length;
		for(var i=0;i<boxLen;i++){
			//全選
			if(box[i].type == 'checkbox' && type == 1){
				box[i].checked = true;
			}
			else if(box[i].type == 'checkbox' && type == 0){
				box[i].checked = false;
			}
		}	
	}
}
