﻿// JavaScript Document
	
//============================================================================
function GetElement(ele)
{
	if (ele && typeof ele == "string")
		return document.getElementById(ele);
	return ele;
}

function GetElementChildren(element)
{
	var children = [];
	var child = element.firstChild;
	while (child)
	{
		if (child.nodeType == 1 /* Node.ELEMENT_NODE */)
			children.push(child);
		child = child.nextSibling;
	}
	return children;
};

function GetIndex(ele, arr)
{
	if (ele && arr && arr.length)
	{
		for (var i = 0; i < arr.length; i++)
		{
			if (ele == arr[i])
				return i;
		}
	}
	return -1;
};

function hasClassName (ele, className) {
	if (!ele || !className || !ele.className || ele.className.search(new RegExp("\\b" + className + "\\b")) == -1)
	{
		return false;
	}
	return true;
}	

function AddClassName(ele, className)
{
	if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) != -1))
		return;
	ele.className += (ele.className ? " " : "") + className;
};

function RemoveClassName(ele, className)
{
	if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) == -1))
		return;
	ele.className = ele.className.replace(new RegExp("\\s*\\b" + className + "\\b", "g"), "");
};

//============================================================================
function GetGroup(t,url){
	var gn = showModalDialog(url,"","resizable=0;status=0;help=0;dialogWidth=350px;dialogHeight=180px");
	if(gn!=undefined) t.value=gn;
}

//============================================================================
function SetCwinHeight(obj)
{
	var cwin=obj;
	if (document.getElementById)
	{
		if (cwin && !window.opera)
		{
			if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight)
				cwin.height = cwin.contentDocument.body.offsetHeight + 10;
			else if(cwin.Document && cwin.Document.body.scrollHeight)
				cwin.height = cwin.Document.body.scrollHeight + 10;
		}
	}
}
//============================================================================
function FormSubmit(formid, url, callback)
{
	if (!confirm("請確認提交！")) return false;
	var f = document.getElementById(formid);
	f.action = url;
	if(typeof(eval(callback))=="function")  
	{
		callback();
	}
	f.submit();
}

//============================================================================
function OperateSelected(formid, selname, url)
{
	if (!IsHaveChecked(selname)) {
		alert("沒有選擇任何記錄！");
		return false;
	}
	if (!confirm("請確認提交？")) return false;
	var f = document.getElementById(formid);
	f.action = url;
	f.submit();
}

//選擇下拉列表框中的項目
function SelectOption(objectid, value) {
	var obj = document.getElementById(objectid);
	for (var i=0; i<obj.options.length; i++) {
		if (obj.options[i].value == value) {		
			obj.options[i].selected = true;
		} else {
			obj.options[i].selected = false;
		}
	}
}

//============================================================================
function CheckAll(c,selname){
	var carr = document.getElementsByName(selname);
	if (c.checked){
		for (var i=0; i<carr.length; i++) 
			if (!carr[i].disabled) carr[i].checked=true;
	}else{
		for (var i=0; i<carr.length; i++) carr[i].checked=false;
	}
}

//============================================================================
function IsHaveChecked(controlid)
{
	for (var i=0;i<document.getElementsByName(controlid).length;i++)
	{
		if (document.getElementsByName(controlid)[i].checked)
			return(true);
	}
	return(false);
}

//============================================================================
function BrowseServer( startupPath, functionData )
{
	var finder = new CKFinder() ;
	finder.BasePath = '/ckfinder/' ;
	finder.SelectFunction = SetFileField ;
	finder.StartupPath = startupPath ;
	finder.SelectFunctionData = functionData ;
	//finder.SelectThumbnailFunction = ShowThumbnails ;
	finder.Popup() ;
}

function SetFileField( fileUrl, data )
{
	document.getElementById( data["selectFunctionData"] ).value = fileUrl;
}

// This is a sample function which is called when a thumbnail is selected in CKFinder.
function ShowThumbnails( obj, fileUrl, moveFlag, notImg )
{
	var oController = typeof obj == 'string' ? $(obj) : obj;
	HideThumbnails();
	if ( fileUrl!='' ) 
		if (notImg)
			oController.prepend('<div class="thumbnailcell">' + fileUrl + '"</div>');
		else
			oController.prepend('<div class="thumbnailcell"><img src="' + fileUrl.replace( /userfiles/i, 'userfiles/_thumbs' ) + '" /></div>');
	else
		oController.prepend('<div class="thumbnailcell thumbnailnull">暫無圖片</div>');
	if ( moveFlag ) 
	{
		var event = window.event || arguments.callee.caller.arguments[0]; 
		$('.thumbnailcell').css({
			position:      'absolute',
			display:       'block',
			zIndex:        '1000',
			top:           event.clientY + ___getPageScroll()[1] + 20,
			left:          event.clientX + ___getPageScroll()[0] + 10
		});
	}
}
function HideThumbnails()
{
	$('.thumbnailcell').remove();
}

//============================================================================
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,''); }

//============================================================================
function AjaxProcess(type,url,data,fn,obj) {
	ShowLoading();
	_this = $(obj);
	$.ajax({
		type: type,
		url: url,
		data: data,
		success: fn
	});
}

ShowValue = function(msg) {
	HideLoading();
	_this.html(msg);
}

function ShowLoading() {
	HideLoading();
	$("body").prepend('<div class="loading"><img src="/images/loading.gif" align="absmiddle" /> &nbsp; 加載中, 請稍等......</div>');
	
	var arrPageScroll = ___getPageScroll();
	$('.loading').css({
		opacity: '0.8',
		zIndex:  1000,
		top:     arrPageScroll[1]
	});
}

function HideLoading() {
    $('.loading').remove();
}

//============================================================================
function CreateOverlay( minWidth, minHeight, overLayer, noIframe )
{
	//if (noIframe)
		$('body').prepend('<div id="jquery-overlay"></div>');
	//else
		//$('body').prepend('<div id="jquery-overlay"><iframe style="position:relative; left:50%; width:475px;height:300px; filter:alpha(opacity=0);-moz-opacity:0"></iframe></div>');
		
	var arrPageSizes = ___getPageSize();
	$('#jquery-overlay').css({
		backgroundColor:	'#EEE',
		opacity:			'0.8',
		position:			'absolute',
		top:		    	'0px',
		left:		    	'0px',
		zIndex:		    	'80',
		width:				arrPageSizes[0]<minWidth ? minWidth : arrPageSizes[0],
		height:				arrPageSizes[1]<minHeight ? minHeight : arrPageSizes[1]
	}).fadeIn().click(function() {
		//HideLoading();
		//$(this).remove();
		//var oOverLayer= typeof(overLayer) == 'string' ? $(overLayer) : overLayer;
		//if (oOverLayer) oOverLayer.remove();
	});
}
function RemoveOverlay()
{
	if ( document.getElementById('jquery-overlay')!=null )
	{
		$('#jquery-overlay').remove();
		return true;
	}
	else
	    return false;	  
}

//============================================================================
/** 
 * 格式化數字顯示方式  
 */  
function FormatNumber(srcStr, nAfterDot){
　　var srcStr,nAfterDot;
　　var resultStr,nTen;
　　srcStr = ""+srcStr+"";
　　strLen = srcStr.length;
　　dotPos = srcStr.indexOf(".",0);
　　if (dotPos == -1){
　　　　resultStr = srcStr+".";
　　　　for (i=0;i<nAfterDot;i++){
　　　　　　resultStr = resultStr+"0";
　　　　}
　　　　return resultStr;
　　}
　　else{
　　　　if ((strLen - dotPos - 1) >= nAfterDot){
　　　　　　nAfter = dotPos + nAfterDot + 1;
　　　　　　nTen =1;
　　　　　　for(j=0;j<nAfterDot;j++){
　　　　　　　　nTen = nTen*10;
　　　　　　}
　　　　　  resultStr = Math.round(parseFloat(srcStr)*nTen+0.01) / (nTen);
　　　　　　return resultStr;
　　　　}
　　　　else{
　　　　　　resultStr = srcStr;
　　　　　　for (i=0;i<(nAfterDot - strLen + dotPos + 1);i++){
　　　　　　　　resultStr = resultStr+"0";
　　　　　　}
　　　　　　return resultStr;
　　　　}
　　}
}

//============================================================================
/*
* javaScript限制<INPUT TYPE=TEXT>只能輸入數
* checkNumber(obj,type,unsigned)
* 限制文本框只允許輸入數字
* obj: 要檢查的文本框對像,一般調用的時候請填this檢查本身,
* type:要輸入的數據類型,整數請輸入integer,小數請輸入 double
* unsigned:限制數據是非負數請填true,允許輸入負數請填false
* 例:
* <input style="ime-mode:disabled" onpropertychange="checkNumber(this,'integer',true)">
* 為限制只允許輸入非負整數
* 其中的style="ime-mode:disabled"必須添加,因為部分輸入法會與腳本衝突使IE崩潰,而且數字文本框也沒有輸入中文的必要
*/
var _isChecked = false;
function checkNumber(obj,type,unsigned){
	obj.style.imeMode='disabled';
	if(type.toUpperCase()!=new String("integer").toUpperCase())
		type="double";
	else
		type="integer";
		
	if(unsigned)
		unsigned=true;
	else
		unsigned=false;
		
	if(!_isChecked) {
		_isChecked=true;
		var str=new String(obj.value);
		var num=new Number(obj.value);
		var ok=true;
		if(unsigned)
			ok=str.match("-")==null;
		if(type=="integer"&&ok)
			ok=str.match("\\.")==null;
		/* \.是正則表達式中代替小數點的方法，但是因為match函數中要求輸入一個字符串參數，所以要用\\來代替\，所以最終用\\.來代替小數點 */
		if(num.toString()=="NaN"&&str!="-")
			ok=false;
		if(obj.value.match("\\-0\\d|\\b^\\.?0\\d"))
			ok=false;//避免輸入01,-0000.01之類的數字
		if(!ok)
			obj.value=obj.backupValue==undefined?"":obj.backupValue;
		else{
			matchStr=obj.value.match("\\-?\\b[0-9]*\\.?[0-9]*\\b\\.?|\\-");//用正則表達式清空前後空格
			if(matchStr!=obj.value){
				obj.value=matchStr==null?"":matchStr;
				_isChecked=true;
			}
			obj.backupValue=obj.value;
		}
	}
	else{
		_isChecked=false;//避免無限遞歸
		return;
	}
} 

//============================================================================
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
/**
 / THIRD FUNCTION
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset || self.pageXOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft)) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};

