﻿/***********************************************
*	samrise JavaScript framework, version 1.6
* 	author	samrise
*	update	20080828	
***********************************************/
//選擇物件
function $(e){var e = document.getElementById(e);return e;}
function selN(e){var e = document.getElementsByName(e);return e;}
function selT(e){var e = document.getElementsByTagName(e);return e;}


//區塊填滿文字
function filltext(words){
	var gibberish=["This is just some filler text", "Welcome to Dynamic Drive CSS Library", "Demo content nothing to read here"]
	for (var i=0; i<words; i++)
		document.write(gibberish[Math.floor(Math.random()*3)]+" ")
}
//下拉選單選取, 傳回第幾個被選取
function selCheck(e){	
	var len = $(e).id.length;
	for(i=0; i< len; i++){
		if (selE(e).options[i].selected){return i}		
	}
}
/*confirm視窗 - 傳回值 true or flase*/
function confirmx(txt,href){
	var tc = confirm(txt);
	if(tc){window.location=href;}
	return tc;
}
//字串轉陣
function returnArray(str){	
	var ar = str.split(',');	
	return ar;
}
//取小數第二位
function $pick2(t){t = t.toFixed(2);return t;}
//檢查是否存在
function $chk(obj){return !!(obj || obj === 0);};
//檢查obj是否定義
function $pick(obj, picked){return $defined(obj) ? obj : picked;};
//有間距的隨機變數
function $random(min, max){return Math.floor(Math.random() * (max - min + 1) + min);};
//傳回現在時間
function $time(){return new Date().getTime();};
//到最上面
function $goTop(){window.scrollBy(0,-10000);}
//連結
function $url(href){window.location.href=href;}
//測試
function $trace(){document.write($trace.arguments[0]);}
//轉換成中文數字
function chineseNum(numstr){
	numArray='零,一,二,三,四,五,六,七,八,九,十'.split(',');
	decimalArray='個,十,百,千,萬,十,百,千,億,十,百,千,兆'.split(',');
	tmpstr=new String();
	for(counti=0;counti<numstr.toString().length;counti++){           
		decimalPlace=numstr.toString().length-counti-1;
		if(!(decimalPlace==2  && numstr.toString().charAt(counti)!=1 && numstr.toString().length==2) &&  numstr.toString().charAt(counti)!=0){           
			tmpstr+=numArray[numstr.toString().charAt(counti)];
		}           
		if(decimalPlace>0 ){
		tmpstr+=decimalArray[decimalPlace];
		}           
	}
	return tmpstr;
}
//紀錄cookie
function setCookie(){
	for( i = 0; i < arguments.length; i++){
		document.cookie = arguments[i];	
	}	
}
//取cookie
function getCookie(){return document.cookie}
/*include_js*/
function include_js(path,reload){
	  var scripts = document.getElementsByTagName("script");
	  if (!reload)
	  for (var i=0;i<scripts.length;i++)
			if (scripts[i].src && scripts[i].src.toLowerCase() == path.toLowerCase() ) return;
	  var sobj = document.createElement('script');
	  sobj.type = "text/javascript";
	  sobj.src = path;
	  var headobj = document.getElementsByTagName('head')[0];
	  headobj.appendChild(sobj);
}
//清除FireFox 多餘blank box
function clean_blank_nodes(objList, iObjChildNum) {
	var tmpNode = null;
	var tmpCount = 0;
	var iTotalNodes = iObjChildNum;	  
	for(tmpCount=0; tmpCount<iTotalNodes; tmpCount++) {
		tmpNode = objList[tmpCount];
		if(tmpNode.hasChildNodes() == false) {
			tmpNode.parentNode.removeChild(tmpNode);
			tmpCount--;
			iTotalNodes--;
		}
	}
	return objList.length;
}
//註冊事件 register event
function addListener(element,e,fn){
     if(element.addEventListener){
          element.addEventListener(e,fn,false);
     } else {
          element.attachEvent("on" + e,fn);
     }
}
/*	How to use
	window.onload = function(){
		var element =  document.getElementById("btn");
		addListener(element,"click",test);
		addListener(element,"click",pig);
	}
*/
//清除timer
function $clear(timer){
	clearTimeout(timer);
	clearInterval(timer);
	return null;
};
//取得物件position XY - return array
function $posxy(e) {
	var oElement = document.getElementById(e);
	var iReturnValue1 = 0;
	var iReturnValue2 = 0;
	while( oElement != null ) {
		iReturnValue1 += oElement.offsetLeft;
		iReturnValue2 += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return [iReturnValue1,iReturnValue2];
}
//視窗大小 true & flase
function $winwh(){
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth-17;
			winH = window.innerHeight-17;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.documentElement.clientWidth + 4;
			winH = document.documentElement.clientHeight + 4;;
		}
	}
	return [winW,winH]
}
//捲軸位置
function $scroll(){
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			scrW = window.pageXOffset;
			scrH = window.pageYOffset;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			scrW = document.documentElement.scrollLeft;
			scrH = document.documentElement.scrollTop;
		}
	}
	return [scrW,scrH]
}
//傳回型態
function $type(obj){
	if (!$defined(obj)) return false;
	if (obj.htmlElement) return 'element';
	var type = typeof obj;
	if (type == 'object' && obj.nodeName){
		switch(obj.nodeType){
			case 1: return 'element';
			case 3: return (/\S/).test(obj.nodeValue) ? 'textnode' : 'whitespace';
		}
	}
	if (type == 'object' || type == 'function'){
		switch(obj.constructor){
			case Array: return 'array';
			case RegExp: return 'regexp';
			case Class: return 'class';
		}
		if (typeof obj.length == 'number'){
			if (obj.item) return 'collection';
			if (obj.callee) return 'arguments';
		}
	}
	return type;
};
/*
Function: $type
	Returns the type of object that matches the element passed in.

Arguments:
	obj - the object to inspect.

Example:
	>var myString = 'hello';
	>$type(myString); //returns "string"

Returns:
	'element' - if obj is a DOM element node
	'textnode' - if obj is a DOM text node
	'whitespace' - if obj is a DOM whitespace node
	'arguments' - if obj is an arguments object
	'object' - if obj is an object
	'string' - if obj is a string
	'number' - if obj is a number
	'boolean' - if obj is a boolean
	'function' - if obj is a function
	'regexp' - if obj is a regular expression
	'class' - if obj is a Class. (created with new Class, or the extend of another class).
	'collection' - if obj is a native htmlelements collection, such as childNodes, getElementsByTagName .. etc.
	false - (boolean) if the object is not defined or none of the above.
*/
/*
Function: $time
	Returns the current timestamp

Returns:
	a timestamp integer.
*/
/*
Function: $random
	Returns a random integer number between the two passed in values.

Arguments:
	min - integer, the minimum value (inclusive).
	max - integer, the maximum value (inclusive).

Returns:
	a random integer between min and max.
*/
/*
Function: $pick
	Returns the first object if defined, otherwise returns the second.

Arguments:
	obj - object to test
	picked - the default to return

Example:
	(start code)
		function say(msg){
			alert($pick(msg, 'no meessage supplied'));
		}
	(end)
*/
/*
Function: $chk
	Returns true if the passed in value/object exists or is 0, otherwise returns false.
	Useful to accept zeroes.

Arguments:
	obj - object to inspect
*/
/*
Function: $clear
	clears a timeout or an Interval.

Returns:
	null

Arguments:
	timer - the setInterval or setTimeout to clear.

Example:
	>var myTimer = myFunction.delay(5000); //wait 5 seconds and execute my function.
	>myTimer = $clear(myTimer); //nevermind

See also:
	<Function.delay>, <Function.periodical>
*/
