var bOpera, bExplorer, bMozilla, fBVersion;
function BrowserGet()
{
bOpera = bExplorer = bMozilla = false;
fBVersion = 0;
var sBrowser = window.navigator.userAgent.toLowerCase(), nPos;
if(-1 != (nPos = sBrowser.indexOf("opera"))){
bOpera = true;
fBVersion = parseFloat(sBrowser.slice(nPos + 6));
}else if(-1 != (nPos = sBrowser.indexOf("msie "))){
if(-1 == window.navigator.userAgent.indexOf("Mac")){
bExplorer = true;
fBVersion = parseFloat(sBrowser.slice(nPos + 5));
}
}else if(-1 != (nPos = sBrowser.indexOf("gecko/"))){
bMozilla = true;
fBVersion = parseInt(sBrowser.slice(nPos + 6));
}
}
BrowserGet();
function BrowserVer(fIEVer, fMozVer, fOpVer)
{
if((null != fIEVer) && bExplorer){
if(fIEVer <= fBVersion)
return true;
}else if((null != fMozVer) && bMozilla){
if(fMozVer <= fBVersion)
return true;
}else if((null != fOpVer) && bOpera){
if(fOpVer <= fBVersion)
return true;
}
return false;
}

function checkEmail(str) {
var pass = 0;
if (window.RegExp) {
var tempStr = "a";
var tempReg = new RegExp(tempStr);
if (tempReg.test(tempStr)) pass = 1;
}
if (!pass) 
return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
var r2 = new RegExp("^[a-zA-Z0-9\\.\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\_\\`\\{\\}\\~]*[a-zA-Z0-9\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\_\\`\\{\\}\\~]\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3})(\\]?)$");
return (!r1.test(str) && r2.test(str));
}

function checkTel(str, country) {
var pass = 0;
if (window.RegExp) {
var tempStr = "a";
var tempReg = new RegExp(tempStr);
if (tempReg.test(tempStr)) pass = 1;
}
switch(country){
case "tw":
if (!pass) 
return (str.indexOf("(")==0) && (str.indexOf(")")>2);
var r1 = new RegExp("^\\([0-9]{2,3}\\)[0-9]{7,8}(#[0-9]{1,3})*$");
return (r1.test(str));
break;
}
}

function checkMobile(str, country) {
var pass = 0;
if (window.RegExp) {
var tempStr="a";
var tempReg=new RegExp(tempStr);
if(tempReg.test(tempStr))pass=1;
}
switch(country){
case "tw":
if (!pass){
for(n=0,i=0;i<str.length;i++)n+=Number(str.charAt(i));
return (str.indexOf("0")==0)&&(str.length==10)&&(typeof n=="number");
}
var r1=new RegExp("^[0][0-9]{9}$");
return(r1.test(str));
break;
}
}


function strLen(initStr, returnType)
{
var initStrLen, escStr, ch, idx, tmp;

if (typeof initStr.length == "undefined")
return -1;

tmp = escape(initStr);
idx = tmp.lastIndexOf("%00", tmp.length);
if (idx!=-1)
initStr = unescape(tmp.substring(0, idx));

initStrLen = initStr.length;
escStr = escape(initStr);
for (ch=0; escStr.indexOf('%u')!=-1; ch++) {
idx = escStr.indexOf('%u');
escStr = escStr.substring(idx+6, escStr.length);
}
switch(returnType) {
case 'length':
case 1:
return initStrLen;
case 'chinese':
case 2:
return ch;
case 'english':
case 3:
return initStrLen - ch;
case 'bytes':
case 4:
return ch*2 + initStrLen-ch;
default:
return -1;
}
}

function chkLen(obj, MAXLEN, chkType, NULLnotValid)
{
if (typeof obj == "object") {
if (typeof obj.value == "undefined")
return alertMsg("obj error!");
str = obj.value;
} else 
str = obj;
if (NULLnotValid && str=='')
return alertMsg('empty value is not allowed!', obj, false); 
if (strLen(str, 'bytes') > MAXLEN)	{
typeof obj=="object" ? obj.focus():null;
return false;
}

return true;
}


//if zeroValid == true then 0000-00-00 or 0000/00/00 valid
function checkDateTime(str, zeroValid)
{
var spStr = str.split(' ');
if (spStr[0]=='' || !checkDate(spStr[0], zeroValid))
return false;

if (typeof spStr[1]!="undefined" && spStr[1]!='' && !checkTime(spStr[1]))
return false;

if (spStr[1]=='')
return false;

if (spStr.length>2)
return false;

return true;
}

function checkTime(str) // valid format => hh:mm:ss
{
if (typeof str=="undefined")
return false;
var spStr = str.split(':');
hh = Number(spStr[0]);
mm = Number(spStr[1]);
ss = Number(spStr[2]);

if ( isNaN(hh) || isNaN(mm) || isNaN(ss) || hh>23 || hh<0 || mm>59 || mm<0 || ss>59 || ss<0) {
return false;
}

return true;
}

//if zeroValid == true then 0000-00-00 or 0000/00/00 valid
// valid format => yyyy-mm-dd or yyyy/mm/dd 
// 2001/5/13  or 1974/08/1	are valid
function checkDate(myValue, zeroValid) 
{
if (typeof myValue=="undefined")
return false;

if (zeroValid && (myValue=="0000-00-00" || myValue=="0000/00/00"))
return true;

//variable declarations!
var mValues = "312831303130313130313031"

//get the no. of arguments passed to this function
var splitChr = myValue.indexOf('-')==-1 ? '/' : '-';
var spStr = myValue.split(splitChr);
myYYYY	= Number(spStr[0]);	// extract Year
myMM   	= Number(spStr[1]);	// extract Month
myDD   	= Number(spStr[2]);	// extract Date

if (spStr[0].length!=4)
return false;

if ( isNaN(myDD) || isNaN(myMM) || isNaN(myYYYY) ){
return false;
}

if ( myMM > 12 || myMM <= 0 ){
return false;
}

var lastDate = 0

if (myMM == 2){
if (isLeapYear(myYYYY)) {
lastDate = 29
}
else {
lastDate = 28
}
} 
else {
lastDate = mValues.substring((myMM-1)*2, (myMM-1)*2+2)
}

if ( myDD>lastDate || myDD<=0 ){
return false;
}

if (!myYYYY) 
return false;

return true;
}

function isEmpty(str) 
{
var c, i, strtmp;

strtmp = ''+ str;
if (strtmp != '')	{
for(i=0; i<strtmp.length; i++) {
c = strtmp.charAt(i);
if ((c != ' ') && (c != '\n') && (c != '\r') && (c != '\t')) 
return false;
}
}	
return true;
}

function isInt(str, NULLValid) 
{
var strtmp, i, oneChar;

strtmp = ''+ str;
if (NULLValid && strtmp=='')
return true;
else if (!NULLValid && strtmp=='')
return false;
for (i=0; i<strtmp.length; i++)	{
oneChar = strtmp.charAt(i);
if (oneChar < '0' || oneChar > '9') {
return false;
}
}
return true;
}

function isLeapYear (Year) { 
if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) { 
return true;
} else 	{ 
return false;
}
}

//alertMsg(msg [, return returnValue [, focus on focusObj ] ])
function alertMsg(msg, focusObj, returnValue)
{
if (!msg) msg = "Error!";
alert(msg);
if (typeof (focusObj) == "object")
focusObj.focus();
if (!returnValue) returnValue = false;
return returnValue;
}
function winClose()
{
if(window.top)
top.close();
else if(window.dialogArguments)
self.close();
}
//begin IE 4+ And NS6 dHTML Outlines
function hideshow(which, act)
{
if (!document.getElementById|document.all)
return
else
if (document.getElementById)
oWhich = eval ("document.getElementById('" + which + "')")
else
oWhich = eval ("document.all." + which)
window.focus()
if (typeof (act) == "undefined")
if (oWhich.style.display=="none")
oWhich.style.display=""
else
oWhich.style.display="none"
else
oWhich.style.display=act
}//
function encodeData(inp)
{
var keyStr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var out="";
var chr1,chr2,chr3="";
var enc1,enc2,enc3,enc4="";
var i=0;
do {
chr1=inp.charCodeAt(i++);
chr2=inp.charCodeAt(i++);
chr3= inp.charCodeAt(i++);
enc1=chr1>>2;
enc2=((chr1&3)<<4)|(chr2>>4);
enc3=((chr2&15)<<2)|(chr3>>6);
enc4=chr3&63;
if (isNaN(chr2)){
enc3=enc4=64;
} else if(isNaN(chr3)){
enc4=64;
}
out=out+keyStr.charAt(enc1)+keyStr.charAt(enc2)+keyStr.charAt(enc3)+keyStr.charAt(enc4);
chr1=chr2=chr3="";
enc1=enc2=enc3=enc4="";
}while(i<inp.length);
return out;
}//
//usage:obj2.style.left=getposOffset(obj1, "left")+"px"
//usage:obj2.style.top=getposOffset(obj1, "top")+"px"
function getposOffset(overlay,offsettype){
var totaloffset=(offsettype=="left")?overlay.offsetLeft:overlay.offsetTop;
var parentEl=overlay.offsetParent;
while(parentEl!=null){
totaloffset=(offsettype=="left")?totaloffset+parentEl.offsetLeft:totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}//
function popup_overlay(curobj, subobj, left_offset, top_offset){
subobj=document.getElementById(subobj);
left_offset = (''+parseInt(left_offset))=='NaN'?0:parseInt(left_offset);
top_offset = (''+parseInt(top_offset))=='NaN'?0:parseInt(top_offset);
subobj.style.left=left_offset+parseInt(getposOffset(curobj, "left"))+"px";
subobj.style.top=top_offset+parseInt(getposOffset(curobj, "top"))+"px";
subobj.style.display="block"
}//
function checkbox_select_all(form,obj,eleName)
{
s=obj.checked;
for(i=0;i<form.elements.length;i++) {
if(form.elements[i].name==eleName)
form.elements[i].checked=s;
}
}//
function div_display(div,state)
{document.getElementById(div).style.display=state;}//
function set_objValue(obj,value)
{document.getElementById(obj).value=value;}//
function set_innerHTML(obj,content)
{document.getElementById(obj).innerHTML=content;}//
function toggle(obj) {
var el=document.getElementById(obj);
if(el.style.display!='none')
el.style.display='none';
else
el.style.display='';
}//
function getElementsByClassName(oElm, strTagName, strClassName){
var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/\-/g, "\\-");
var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
var oElement;
for(var i=0; i<arrElements.length; i++){
oElement = arrElements[i];
if(oRegExp.test(oElement.className)){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
}//
function strip_html_tags(str)
{
reg = /<[\/\!]*?[^<>]*?>/gi
return str.replace(reg, '');
}//
function addslashes(str)
{return str.replace("'", "\'");}
//http://www.scottandrew.com/weblog/jsjunk#events
// roundTo()
// Round p to nearest n
function roundTo(p,n)
{
return (Math.round(n/p))*p;
}

// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla

function myAddEvent(elm, evType, fn, useCapture)
{
if (elm.addEventListener){
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent){
var r = elm.attachEvent("on"+evType, fn);
return r;
} else {
alert("Handler could not be added");
}
} 

function myRemoveEvent(elm, evType, fn, useCapture)
{
if (elm.removeEventListener){
elm.removeEventListener(evType, fn, useCapture);
return true;
} else if (elm.detachEvent){
var r = elm.detachEvent("on"+evType, fn);
return r;
} else {
alert("Handler could not be removed");
}
}

// getParams

function getQueryArgs(global)
{
var args = {};
var loc = window.location.href;
var q = loc.indexOf("?");
if (q==-1) return false;
loc = loc.substring(q+1);
var pairs = loc.split("&");
for (var i=0; i<pairs.length;i++){
if (global) eval(pairs[i]);
var keyval = pairs[i].split("=");
args[keyval[0]] = unescape(keyval[1]);
}
return args;
}


//http://www.dustindiaz.com/top-ten-javascript/
//addEvent(window,'load',func1,false);
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
}
else {
window.onload = function() {
oldonload();
func();
}
}
}

function insertAfter(parent, node, referenceNode) {
parent.insertBefore(node, referenceNode.nextSibling);
}

Array.prototype.inArray = function (value) {
var i;
for (i=0; i < this.length; i++) {
if (this[i] === value) {
return true;
}
}
return false;
};

function setCookie (name, value, expires, path, domain, secure) {
   var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
   document.cookie = curCookie;
}

function getCookie (name) {
   var prefix = name + '=';
   var c = document.cookie;
   var nullstring = '';
   var cookieStartIndex = c.indexOf(prefix);
   if (cookieStartIndex == -1)
       return nullstring;
   var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
   if (cookieEndIndex == -1)
       cookieEndIndex = c.length;
   return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
   if (getCookie(name))
       document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function fixDate (date) {
   var base = new Date(0);
   var skew = base.getTime();
   if (skew > 0)
       date.setTime(date.getTime() - skew);
}
function textSizer()
{
	var tgs;
	var szs;
	var startSz;
	
	this.tgs = new Array( 'div','span','td','a','p' );
	//Specify spectrum of different font sizes:
	this.szs = new Array( '8px','10px','11px','12px','14px','16px','18px','20px','22px' );
	this.startSz = 2;
}
textSizer.prototype = {
	ts : function ts( trgt,inc,idx ) {
		if (!document.getElementById) return
		var d,sz,i,j,cTags,cEl;
		d = document;
		cEl = null;
		sz = this.startSz;
		
		if (inc==0) {
			if (typeof idx=="number")
				sz = idx;
			else
				sz = 2;
		} else {
			sz += inc;
			if ( sz < 0 ) sz = 0;
			if ( sz > 8 ) sz = 8;
		}
		this.startSz = sz;
		
		if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];
	
		cEl.style.fontSize = this.szs[ sz ];
	
		for ( i = 0 ; i < this.tgs.length ; i++ ) {
			cTags = cEl.getElementsByTagName( this.tgs[ i ] );
			for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = this.szs[ sz ];
		}
	}
}

function IncludeJavaScript(jsFile)
{
  document.write('<script type="text/javascript" src="'
    + jsFile + '"></script>'); 
}
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function()
{
return this.replace(/(^\s*)/g, "");
}
String.prototype.RTrim = function()
{
return this.replace(/(\s*$)/g, "");
}
function GetLangCode() {
var lct="en";
if (navigator.language) {
lct=navigator.language.toLowerCase();
} else if (navigator.userLanguage) {
lct=navigator.userLanguage.toLowerCase();
} else if (navigator.userAgent.indexOf("[")!=-1) {
var debut=navigator.userAgent.indexOf("[");
var fin=navigator.userAgent.indexOf("]");
lct=navigator.userAgent.substring(debut+1, fin).toLowerCase();
}
return lct;
} 

// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini

// http://dean.edwards.name/weblog/2005/10/add-event/

function addEvent(element, type, handler) {
	if (element.addEventListener) {
		element.addEventListener(type, handler, false);
	} else {
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			}
		}
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = handleEvent;
	}
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener) {
		element.removeEventListener(type, handler, false);
	} else {
		// delete the event handler from the hash table
		if (element.events && element.events[type]) {
			delete element.events[type][handler.$$guid];
		}
	}
};

function handleEvent(event) {
	var returnValue = true;
	// grab the event object (IE uses a global event object)
	event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};