
function openwin(src, w_name, w_width, w_height, w_opt)
{
	if(!w_opt)    w_opt    = 'resizable=1,scrollbars=1';

	if(!w_width)  w_width  = 700;
	if(!w_height) w_height = 500;

	w_opt += ',width='+w_width+',height='+w_height;

	if(typeof(screen) != 'undefined')
	{
		w_left   = parseInt((screen.width-w_width)/2);
		w_top    = parseInt((screen.height-w_height)/2);

		w_opt += ',left='+w_left+',top='+w_top;
	}

	wobj = window.open(src, w_name, w_opt);
	wobj.focus();

	return wobj;
}

function filename(url) {
	return url.substring(url.lastIndexOf('/')+1, url.length);
}

function resizewin(w_width, w_height, forceBody)
{
	/* if(forceBody == true) 會讓 document body 強制變成 w_height 的高度 */
	if(!w_width)  w_width  = 700;
	if(!w_height) w_height = 500;

	if(typeof(screen) != 'undefined')
	{
		w_left   = parseInt((screen.width-w_width)/2);
		w_top    = parseInt((screen.height-w_height)/2);
	}

	window.moveTo(w_left, w_top);
	window.resizeTo(w_width, w_height);

	if(forceBody)
	{
		barHeight = screen.height - (w_top*2) - document.body.clientHeight;

		if(typeof(screen) != 'undefined')
		{
			w_left   = parseInt((screen.width-w_width)/2);
			w_top    = parseInt((screen.height-(barHeight + w_height))/2);
		}

	window.moveTo(w_left, w_top);
		window.resizeTo(w_width, barHeight + w_height);

	}

	/*
	document.body.clientWidth
	document.body.clientHeight

	window.screenLeft
	window.screenTop
	*/
}
function formChangeInputClass(f)
{
	if(typeof(f.elements.length) == 'undefined')
	{
		return false;
	}

	for(i=0,max=f.elements.length ; i < max ; i++)
	{
		obj = f.elements[i]
		if(obj.type == 'text' || obj.type == 'textarea' || obj.type == 'password' || obj.type == 'file') {
			obj.className='tb_field_input';
		}
	}

    return true;
}

function formSubmitLock(f)
{
	if(typeof(f.elements.length) == 'undefined')
	{
		return false;
	}

	for(i=0,max=f.elements.length ; i < max ; i++)
	{
		obj = f.elements[i]
		if(obj.type == 'submit' || obj.type == 'button' || obj.type == 'reset') {
			obj.disabled=1
		}
		/*
		if(obj.type == 'text' || obj.type == 'textarea') {
			obj.readOnly=1
		}
		*/
	}

    return true;
}

function formSubmitUnlock(f)
{
	if(typeof(f.elements.length) == 'undefined')
	{
		return false;
	}

	for(i=0,max=f.elements.length ; i < max ; i++)
	{
		obj = f.elements[i]
		if(obj.type == 'submit' || obj.type == 'button' || obj.type == 'reset') {
			obj.disabled=0
		}
	}

    return true;
}


function setButtonDisabled(the_button_obj, disabled)
{
    var elts      = the_button_obj
    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;

    if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
        	alert(elts[i].type)
            elts[i].disabled = disabled;
        } // end for
    } else {
    	alert(elts.type)
        elts.disabled        = disabled;
    } // end if... else

    return true;
}

function open_epaper_varlist(f, group_id)
{
	w_opt    = 'status:false;resizable:true'

	w_width  = 300;
	w_height = 460;

	w_opt += ';dialogWidth:'+w_width+'px;dialogHeight:'+w_height+'px';

	if(typeof(screen) != 'undefined')
	{
		w_left   = 250; //parseInt((screen.width-w_width)/2);
		w_top    = parseInt((screen.height-w_height)/2);

		w_opt += ';dialogLeft:'+w_left+'px;dialogTop='+w_top+'px';
	}

	showModelessDialog('xet_epaper_varlist.php?group_id='+group_id, f, w_opt);
}

function open_epaper_preview_by_src(src)
{
	if(isBlank(src)) {
		alert('請先輸入郵件內容');
		return;
	}

	w_opt    = 'resizable=1,scrollbars=1';

	w_width  = 700;
	w_height = 500;

	w_opt += ',width='+w_width+',height='+w_height;

	if(typeof(screen) != 'undefined')
	{
		w_left   = parseInt((screen.width-w_width)/2);
		w_top    = parseInt((screen.height-w_height)/2);

		w_opt += ',left='+w_left+',top='+w_top;
	}

	ppr = window.open('', 'preview', w_opt);

	ppr.document.open();

	if(document.forms[0].field_format[1].checked)
	{
		src = '<xmp>' + src + '</xmp>';
	}

	ppr.document.writeln(src);
	ppr.document.close();

}

function open_epaper_preview(id)
{
	if(typeof(id) == 'undefined' || parseInt(id) <= 0) {
		alert('錯誤的預覽編號!');
		return false;
	}

	w_opt    = 'resizable=1,scrollbars=1';

	w_width  = 700;
	w_height = 500;

	w_opt += ',width='+w_width+',height='+w_height;

	if(typeof(screen) != 'undefined')
	{
		w_left   = parseInt((screen.width-w_width)/2);
		w_top    = parseInt((screen.height-w_height)/2);

		w_opt += ',left='+w_left+',top='+w_top;
	}

	window.open('xet_epaper_preview.php?id=' + id, 'preview', w_opt);
}

function URL_Encode(url)
{
	//url = url.replace(/&amp;/, '&');
	buf = '';

	max = url.length

	for(i=0 ; i < max ; i++)
	{
		c = url.charAt(i);

		if(c == '&')
			buf += '%26';
		else if(c == '/')
			buf += '%2F';
		else if(c == '\\')
			buf += '%5C';
		else if(c == '@')
			buf += '%40';
		else if(c == '\'')
			buf += '%27';
		else if(c == ';')
			buf += '%3B';
		else if(c == ' ')
			buf += '+';
		else if(c == '+')
			buf += '%2B';
		else if(c == ':')
			buf += '%3A';
		else if(c == '=')
			buf += '%3D';
		else if(c == '?')
			buf += '%3F';
		else
			buf += c;
	}

	return buf;
}


/**
 * Checks/unchecks all tables
 *
 * @param   string   the form name
 * @param   boolean  whether to check or to uncheck the element
 *
 * @return  boolean  always true
 */
function setCheckboxes(the_checkbox_obj, do_check)
{
    var elts      = the_checkbox_obj
    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;

    if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            elts[i].checked = do_check;
        } // end for
    } else {
        elts.checked        = do_check;
    } // end if... else

    return true;
} // end of the 'setCheckboxes()' function

/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;

/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 3.3 ... Opera changes colors set via HTML to rgb(r,g,b) format so fix it
    if (currentColor.indexOf("rgb") >= 0)
    {
        var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
                                     currentColor.indexOf(')'));
        var rgbValues = rgbStr.split(",");
        currentColor = "#";
        var hexChars = "0123456789ABCDEF";
        for (var i = 0; i < 3; i++)
        {
            var v = rgbValues[i].valueOf();
            currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
        }
    }

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function

function insertValueByStr(str, myQuery)
{
	//alert(myQuery);

    var chaineAj = str;

    //IE support
    if (document.selection) {
        myQuery.focus();
        sel = document.selection.createRange();
        sel.text = chaineAj;
    }

    //MOZILLA/NETSCAPE support
    else if (myQuery.selectionStart || myQuery.selectionStart == "0") {
        var startPos = myQuery.selectionStart;
        var endPos = myQuery.selectionEnd;
        var chaineSql = myQuery.value;

        myQuery.value = chaineSql.substring(0, startPos) + chaineAj + chaineSql.substring(endPos, chaineSql.length);
    } else {
        myQuery.value += chaineAj;
    }
}

function insertValue(myListBox, myQuery)
{
	//alert(myListBox.options.length);return;
    if(myListBox.options.length > 0) {
        var chaineAj = "";
        var NbSelect = 0;
        for(var i=0; i<myListBox.options.length; i++) {
            if (myListBox.options[i].selected){
                NbSelect++;
                //if (NbSelect > 1)                    chaineAj += ", ";
                chaineAj += myListBox.options[i].value;
            }
        }

        //IE support
        if (document.selection) {
            myQuery.focus();
            sel = document.selection.createRange();
            //alert(sel.text);
            sel.text = chaineAj;
            myListBox.focus();
        }
        //MOZILLA/NETSCAPE support
        else if (myQuery.selectionStart || myQuery.selectionStart == "0") {
            var startPos = myQuery.selectionStart;
            var endPos = myQuery.selectionEnd;
            var chaineSql = myQuery.value;

            myQuery.value = chaineSql.substring(0, startPos) + chaineAj + chaineSql.substring(endPos, chaineSql.length);
        } else {
            myQuery.value += chaineAj;
        }
    }
}

function setFromCheckboxValue(t, c)
{
	max = c.length;
	t.value = ''
	for(i=0 ; i < max ; i++)
	{
		if(c[i].checked)
		{
			t.value += '{' + c[i].value + '}';
		}
	}
}

function setNow(f, name, is_time)
{
  myDate1 = new TimeDate()
  f[name].value = myDate1.year + '-' + myDate1.zp_monthNum + '-' + myDate1.zp_dayNum;

  if(is_time) {
    f[name].value += ' ' + myDate1.zp_hours + ':' + myDate1.zp_minutes + ':' + myDate1.zp_seconds;
  }
}
function setClear(f, name)
{
  f[name].value = ''
}

function checkAll(obj)
{
	if(obj)
	{
		if(!obj.length)
			obj.checked=true
		for(i=0 ; i<obj.length ; i++){
			if(!obj[i].checked)
				obj[i].checked=true
		}
	}
}

function checkSwitch(obj){
	if(obj)
	{
		if(!obj.length)
			if(obj.checked)
				obj.checked=false
			else
				obj.checked=true

		for(i=0 ; i<obj.length ; i++){
			if(obj[i].checked)
				obj[i].checked=false
			else
				obj[i].checked=true
		}
	}
}

function setSelectByOption(sel, str)
{
	str = str.replace(/(.)/gi, "\\$1");
	re = new RegExp(str, "i")

	for(i=0 ; i < sel.length ; i++)
	{
		//if(sel.options[i].text == str)
		if(re.test(sel.options[i].text))
		{
			sel.selectedIndex = i
			break;
		}
	}
	return true;
}

function setSelectById(sel, id)
{
	sel.selectedIndex = id
	return true;
}

mysql_date_re = /(\d\d\d\d)-(\d\d)-(\d\d)( (\d\d):(\d\d):(\d\d))?/

function isMySQLDateTime(s)
{
	if(!mysql_date_re.test(s)) {
		return false;
	} else {
		return true
	}
}
function setSelectByText(sel, s)
{
	for(i=0 ; i < sel.length ; i++)
	{
		if(sel.options[i].text == s)
		{
			sel.selectedIndex = i
			break;
		}
	}
	return true;
}
function setSelectByValue(sel, s)
{
	for(i=0 ; i < sel.length ; i++)
	{
		if(sel.options[i].value == s)
		{
			sel.selectedIndex = i
			break;
		}
	}
	return true;
}
function setRadioByValue(sel, s)
{
	if(typeof(sel.length) == "undefined") {
		if(sel.type != 'radio') return false;
		if(sel.value == s) {
			sel.checked = true;
		}
	}
	else
	{
		for(i=0 ; i < sel.length ; i++)
		{
			if(sel[i].type != 'radio') return false;

			if(sel[i].value == s)
			{
				sel[i].checked = true;
				break;
			}
		}
	}
	return true;
}

function setInputByValue(o, s)
{
	if(typeof(o) == "undefined") {
		return false;
	}
	if(typeof(o.length) == "undefined")
	{
		if((o.type == 'radio'|| o.type == 'checkbox') && o.value == s) {
			o.checked = true;
		}
		if((o.type == 'text' || o.type == 'textarea' || o.type == 'hidden' || o.type == 'password' )) {
			o.value = s;
			o.fireEvent("onclick");
		}
	}
	else
	{
		for(i=0 ; i < o.length ; i++)
		{
			// 當 s 為一個陣列
			if(typeof(s) == 'object' && typeof(s.length) != 'undefined')
			{
				// 先取消現有的選取
				for(j=0,jmax=s.length ; j < jmax ; j++) {
					if(o.type == 'select-multiple') {
						o.options[i].selected = false;
						o.fireEvent("onchange");
					}
					if(o[i].type == 'checkbox') {
						o[i].checked = false;
					}
				}
				// 再將需要的選取起來
				for(j=0,jmax=s.length ; j < jmax ; j++) {
					if(o.type == 'select-multiple') {
						if(o.options[i].value == s[j]) {
							o.options[i].selected = true;
							o.fireEvent("onchange");
						}
					}
					if(o[i].type == 'checkbox') {
						if(o[i].value == s[j]) {
							o[i].checked = true;
							o[i].fireEvent("onclick");
						}
					}
				}
			}
			else
			{
				if(o.type == 'select-one') {
					if(o.options[i].value == s) {
						//o.selectedIndex = i;
						o.options[i].selected = true;
						o.fireEvent("onchange");
					}
				}
				if((o[i].type == 'radio' || o[i].type == 'checkbox') && o[i].value == s) {
					o[i].checked = true;
					o[i].fireEvent("onclick");
				}
				if((o[i].type == 'text' || o[i].type == 'textarea' || o[i].type == 'hidden' || o[i].type == 'password' )) {
					o[i].value = s;
				}
			}
		}
	}
	return true;
}

function setSelectById(sel, id)
{
	sel.selectedIndex = id
	return true;
}

function focusFirstField(form)
{
	for(i=0,max=form.elements.length ; i<max ; i++)
	{
		if( (form.elements[i].type == "text" || form.elements[i].type == "file" ) &&
			form.elements[i].readOnly == false &&
			form.elements[i].disabled == false &&
			form.elements[i].style.display != 'none')
		{
			//form.elements[i].setActive();
			form.elements[i].focus();
			return true;
		}
		if( (form.elements[i].type == "select-one" || form.elements[i].type == "select-multiple" ) &&
			form.elements[i].disabled == false && form.elements[i].style.display != 'none')
		{
			form.elements[i].focus();
			return true;
		}
	}
}

function blockStatus(theRow, status, id)
{
	var theCells = null;

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

	theCells[0].innerHTML = (status % 2 == 0) ? '-': '+';
	eDiv = document.getElementById(id);
	eDiv.style.display = (status % 2 == 0) ? '': 'none';

/*
	//dump_props('eDiv.style');
	eDiv.style.display = 'inline-block';
	//alert(eDiv.style.display);
*/
}



DoggyInputObject = function() {};

/*
 * Usage:
 *
 *   <script>
 *     iapi = new DoggyInputObject();
 *   </script>
 *
 *   <input class="tb_field_input" type="text" name="field_contact_mobile"
 *	 	onBlur="iapi.tel(this);">
 *
 *	onBlur="iapi.email(this);"
 *	onBlur="iapi.mobile(this);"
 *	onBlur="iapi.number(this);"
 *
 */
DoggyInputObject.prototype.overlib_username = function() {
	return overlib('☉ 請設定一個唯一的帳號名稱<P>帳號必須<span class="red">輸入 3 ~ 16 個字元</span><BR>其名稱僅能包括<span class="red">英文</span>、<span class="red">數字</span>或<span class="red">底線(_)</span>', WIDTH, '300', TEXTFONT, 'Courier, Courier New', TIMEOUT, 2000);
}

DoggyInputObject.prototype.overlib_fieldname = function() {
	return overlib('☉ 請設定一個唯一的欄位名稱<P>欄位必須以<span class="red">英文字母</span>開頭<BR>其名稱僅能包括<span class="red">英文</span>、<span class="red">數字</span>或<span class="red">底線(_)</span>', WIDTH, '300', TEXTFONT, 'Courier, Courier New', TIMEOUT, 2000);
}

DoggyInputObject.prototype.overlib_tel = function() {
	return overlib('☉ 請使用標準電話格式輸入：<P>格式範例1: 02-23222480<BR>格式範例2: 02-23222480#14<BR>格式範例3: +886-2-23222480#14', WIDTH, '300', TEXTFONT, 'Courier, Courier New', TIMEOUT, 2000);
}

DoggyInputObject.prototype.overlib_mobile = function() {
	return overlib('☉ 請使用標準行動電話格式輸入：<P>格式範例1: 0939-123456<BR>格式範例2: +886-939-123456', WIDTH, '300', TEXTFONT, 'Courier, Courier New', TIMEOUT, 2000);
}

DoggyInputObject.prototype.number = function(obj, required) {
	obj.value=stripCharsNotInBag(obj.value, ' 0123456789');

	if(required && isBlank(obj.value)) {
		obj.className='tb_field_input_error';
		return;
	} else {
		obj.className='tb_field_input';
	}
}

DoggyInputObject.prototype.required = function(obj) {

	obj.value=stripLeadingTrailingBlanks(obj.value);

	if(isBlank(obj.value)) {
		obj.className='tb_field_input_error';
	} else {
		obj.className='tb_field_input';
	}
}

// 統一編號 8 碼數字
DoggyInputObject.prototype.gui = function(obj, required) {

	obj.value=stripCharsNotInBag(obj.value, '0123456789');

	if(required && isBlank(obj.value)) {
		obj.className='tb_field_input_error';
		return;
	}

	if(!isBlank(obj.value)) {
		if(obj.value.length < 8) {
			padding_zero_num = 8 - obj.value.length;
			for(i=0 ; i < padding_zero_num ; i++)
			{
				obj.value = '0' + obj.value;
			}
		}
		if(obj.value.length != 8) {
			obj.className='tb_field_input_error';
		} else {
			obj.className='tb_field_input';
		}
	}
}

DoggyInputObject.prototype.tel = function(obj, required) {

	obj.value=stripLeadingTrailingBlanks(obj.value);

	obj.value=stripCharsNotInBag(obj.value, ' +()0123456789-*#');

	obj.value=fapi.correct_tel(obj.value);

	if(required && isBlank(obj.value)) {
		obj.className='tb_field_input_error';
		return;
	}

	if(!isBlank(obj.value)) {
		if(!fapi.tel_check(obj.value)) {
			obj.className='tb_field_input_error';
		} else {
			obj.className='tb_field_input';
	   		obj.value=stripCharsNotInBag(obj.value, '+0123456789-#');
	    }
	} else {
		obj.className='tb_field_input';
	}
}

DoggyInputObject.prototype.mobile = function(obj, required) {

	obj.value=stripLeadingTrailingBlanks(obj.value);

	obj.value=stripCharsNotInBag(obj.value, ' +()0123456789-#');

	obj.value=fapi.correct_tel(obj.value);

	if(required && isBlank(obj.value)) {
		obj.className='tb_field_input_error';
		return;
	}

	if(!isBlank(obj.value)) {
		if(!fapi.mobile_check(obj.value)) {
			obj.className='tb_field_input_error';
		} else {
			obj.className='tb_field_input';
	   		obj.value=stripCharsNotInBag(obj.value, '+0123456789-#');
	    }
	} else {
		obj.className='tb_field_input';
	}
}

DoggyInputObject.prototype.email = function(obj, required) {

	obj.value=stripLeadingTrailingBlanks(obj.value);

	obj.value=obj.value.toLowerCase();

	//kevin_chang@abacus.com.ｔｗ

	obj.value=fapi.correct_email(obj.value);

	//obj.value=stripCharsNotInBag(obj.value, 'abcdefghijklmnopqrstuvwxyz@0123456789-_.');

	if(required && isBlank(obj.value)) {
		obj.className='tb_field_input_error';
		return;
	}

	if(!isBlank(obj.value)) {
		if(!fapi.email_check(obj.value)) {
			obj.className='tb_field_input_error';
		} else {
			obj.className='tb_field_input';
	    }
	} else {
		obj.className='tb_field_input';
	}
}

DoggyInputObject.prototype.username = function(obj, required) {

	obj.value=obj.value.toLowerCase();
	obj.value=obj.value.replace(/-/, "_");
	obj.value=stripCharsNotInBag(obj.value, 'abcdefghijklmnopqrstuvwxyz0123456789_.');

	if(required && isBlank(obj.value)) {
		obj.className='tb_field_input_error';
		return;
	}

	if(!isBlank(obj.value)) {
		if(!fapi.username_check(obj.value)) {
			obj.className='tb_field_input_error';
		} else {
			obj.className='tb_field_input';
	    }
	} else {
		obj.className='tb_field_input';
	}
}

DoggyInputObject.prototype.fieldname = function(obj, required) {

	obj.value=obj.value.toLowerCase();
	obj.value=obj.value.replace(/-/, "_");
	obj.value=stripCharsNotInBag(obj.value, 'abcdefghijklmnopqrstuvwxyz0123456789_');

	if(required && isBlank(obj.value)) {
		obj.className='tb_field_input_error';
		return;
	}

	if(!isBlank(obj.value)) {
		if(!fapi.fieldname_check(obj.value)) {
			obj.className='tb_field_input_error';
		} else {
			obj.className='tb_field_input';
	    }
	} else {
		obj.className='tb_field_input';
	}
}

DoggyInputObject.prototype.date = function(obj, required) {

	obj.value=stripCharsNotInBag(obj.value, '0123456789-');

	if(required && isBlank(obj.value)) {
		obj.className='tb_field_input_error';
		return;
	}

	if(!isBlank(obj.value)) {
		if(!fapi.date_check(obj.value)) {
			obj.className='tb_field_input_error';
		} else {
			obj.className='tb_field_input';
	    }
	} else {
		obj.className='tb_field_input';
	}
}
DoggyInputObject.prototype.time = function(obj, required) {

	obj.value=stripCharsNotInBag(obj.value, '0123456789:');

	if(required && isBlank(obj.value)) {
		obj.className='tb_field_input_error';
		return;
	}

	if(!isBlank(obj.value)) {
		if(!fapi.time_check(obj.value)) {
			obj.className='tb_field_input_error';
		} else {
			obj.className='tb_field_input';
	    }
	} else {
		obj.className='tb_field_input';
	}
}
DoggyInputObject.prototype.datetime = function(obj, required) {

	obj.value=stripLeadingTrailingBlanks(obj.value);

	obj.value=stripCharsNotInBag(obj.value, '0123456789-: ');

	if(required && isBlank(obj.value)) {
		obj.className='tb_field_input_error';
		return;
	}

	if(!isBlank(obj.value)) {
		if(!fapi.datetime_check(obj.value)) {
			obj.className='tb_field_input_error';
		} else {
			obj.className='tb_field_input';
	    }
	} else {
		obj.className='tb_field_input';
	}
}


DoggyFormatObject = function() {};

// 電話號碼 自動校正 ( Auto-correct ) , 以「台北」為預設
DoggyFormatObject.prototype.tel = function(str) {

	i = 0;

	arrRegEx = new Array();
	arrRepEx = new Array();

	if(str.length < 8) {
		return str;
	}

	if(str.match(/^(080\d|020\d)[\d\-\#]+$/)) {
		//return str;
	}

	// 0800 與 0204 的電話格式
	arrRegEx[i]   = /^\(?\s*(020\d|080\d)\s*\)?[\s-]*(\d{3,4})\s*[\/ -]?\s*(\d{3,4})/;
	arrRepEx[i++] = "($1) $2$3";

	// 台灣全省各地區碼
	arrRegEx[i]   = /^\(?\s*0(2|3|37|4|49|5|6|7|8|89|836|826|823|827)\s*\)?[\s-]*(\d{3,4})\s*[\/ -]?\s*(\d{3,4})/;
	arrRepEx[i++] = "+886-$1-$2$3";

	// 台北電話號碼
	arrRegEx[i]   = /^([2358]\d\d\d)[ -]*(\d\d\d\d)/;
	arrRepEx[i++] = "+886-2-$1$2";

	// 行動電話號碼
	arrRegEx[i]   = /^\(?0(9\d\d)[\) -]*(\d\d\d)[ -]?(\d\d\d)/;
	arrRepEx[i++] = "+886-$1-$2$3";

	// 分機號碼
	arrRegEx[i]   = /\+(\d+)-(\d)-(\d{6,8})\s*(\*|ext[\.]?|#|\-|\~|分機|分機\s*:\s*)\s*(\d+)/;
	arrRepEx[i++] = "+$1-$2-$3 #$5";

	// 其他有國碼的電話
	arrRegEx[i]   = /^\+(\d{1,3})[\s-]*(\d{1,3})?[\/ -]*(\d{3,4})\s*[\/ -]?\s*(\d{3,4})/;
	arrRepEx[i++] = "+$1-$2-$3$4";

	for(i=0; i<arrRegEx.length ; i++)
	{
		str = str.replace(arrRegEx[i], arrRepEx[i]);
	}

	return str;
};

// 電話號碼 格式檢查
DoggyFormatObject.prototype.tel_check = function(str) {

	i = 0;

	arrRegEx = new Array();

	// 標準電話格式 +886-2-23222480 #123
	arrRegEx[i++]   = /^\+([1-9]\d{0,2})\-([1-9]\d{0,2})-(\d+)(#\d+)?$/;

	// 國內電話格式
	arrRegEx[i++]   = /^0(2|3|37|4|49|5|6|7|8|89|836|826|823|827)-(\d{6,8})(#\d+)?$/;

	// 其他電話格式 ( 0800 + 手機 + 其他 )
	arrRegEx[i++]   = /^(0[1-9]\d\d)-(\d{6})$/;

	is_valid = false;

	for(i=0; i<arrRegEx.length ; i++)
	{
		if(str.match(arrRegEx[i]))
		{
			is_valid = true;
			break;
		}
	}

	return is_valid;
};

// 行動電話 格式檢查
DoggyFormatObject.prototype.mobile_check = function(str) {

	i = 0;

	arrRegEx = new Array();

	// 標準電話格式 +886-939-800382 #123
	arrRegEx[i++]   = /^\+([1-9]\d{0,2})\-([1-9]\d{0,2})-(\d+)(#\d+)?$/;

	// 行動電話格式
	arrRegEx[i++]   = /^(09\d\d)-(\d\d\d\d\d\d)$/;

	is_valid = false;

	for(i=0; i<arrRegEx.length ; i++)
	{
		if(str.match(arrRegEx[i]))
		{
			is_valid = true;
			break;
		}
	}

	return is_valid;
};


// 電話號碼 自動校正 ( Auto-correct ) , 以「台北」為預設
DoggyFormatObject.prototype.correct_tel = function(str) {

	i = 0;

	arrRegEx = new Array();
	arrRepEx = new Array();

	if(str.length < 8) {
		return str;
	}

	// 把有刮號的電話格式換成沒有刮號的電話格式
	arrRegEx[i]   = /^\((0[1-9]\d{0,2})\)[\s-]*(.+)$/;
	arrRepEx[i++] = "$1-$2";

	// 把手機號碼(10碼)換成有效格式
	arrRegEx[i]   = /^\(?(09\d\d)\)?[\s-]*(\d{3})\s*[\/ -]?\s*(\d{3})$/;
	arrRepEx[i++] = "$1-$2$3";

	// 台北電話號碼
	arrRegEx[i]   = /^([2345678]\d\d\d)[ -]*(\d\d\d\d)$/;
	arrRepEx[i++] = "02-$1$2";

	// 台北電話號碼 + 分機
	arrRegEx[i]   = /^([2345678]\d\d\d)[ -]*(\d\d\d\d)\s*[#:\*](\d+)$/;
	arrRepEx[i++] = "02-$1$2#$3";

	// 0800 與 0204 與 手機 的電話格式
	arrRegEx[i]   = /^\s*(0[1-9]\d\d)[\s-]+(\d{3})\s*[\/ -]?\s*(\d{3})$/;
	arrRepEx[i++] = "$1-$2$3";

	// 台灣全省各地區碼
	arrRegEx[i]   = /^0(2|3|37|4|49|5|6|7|8|89|836|826|823|827)[\s-]*(\d{3,4})\s*[\/ -]?\s*(\d{3,4})$/;
	arrRepEx[i++] = "0$1-$2$3";

	// 台灣全省各地區碼 + 分機
	arrRegEx[i]   = /^0(2|3|37|4|49|5|6|7|8|89|836|826|823|827)[\s-]*(\d{3,4})\s*[\/ -]?\s*(\d{3,4})\s*[#:\*](\d+)$/;
	arrRepEx[i++] = "0$1-$2$3#$4";

	// 有國碼的電話號碼 +(886)-2-2322-2480
	arrRegEx[i]   = /^\+\(([1-9]\d{0,2})\)[\s-]*([1-9]\d{0,2})[\/\s-]+(\d{3,4})\s*[\/ -]?\s*(\d{3,4})$/;
	arrRepEx[i++] = "+$1-$2-$3$4";

	arrRegEx[i]   = /^\+([1-9]\d{0,2})[\s-]+([1-9]\d{0,2})[\/\s-]+(\d{3,4})\s*[\/ -]?\s*(\d{3,4})$/;
	arrRepEx[i++] = "+$1-$2-$3$4";

	// 有國碼的電話號碼 + 分機
	arrRegEx[i]   = /^\+\(?([1-9]\d{0,2})\)?[\s-](\d{1,3})[\/\s-](\d{3,4})\s*[\/ -]?\s*(\d{3,4})\s*[#:\*](\d+)$/;
	arrRepEx[i++] = "+$1-$2-$3$4#$5";

	for(i=0; i<arrRegEx.length ; i++)
	{
		if(str.match(arrRegEx[i])) {
			str = str.replace(arrRegEx[i], arrRepEx[i]);
			//alert(i);
			if(i>0) break;
		}
	}

	return str;
};

// E-Mail 自動校正 ( Auto-correct )
DoggyFormatObject.prototype.correct_email = function(str) {

	i = 0;

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)

	if (str.indexOf('@')==-1 ||
		str.indexOf('.')==-1 ||
		str.indexOf(at,(lat+1))!=-1 ||
		str.indexOf(dot,(lat+2))==-1 ){
	   return str
	}

	arrRegEx = new Array();
	arrRepEx = new Array();

	// 把中文全形的英數字改成半形的
	arrRegEx[i]   = /ａ/g;	arrRepEx[i++] = "a";
	arrRegEx[i]   = /ｂ/g;	arrRepEx[i++] = "b";
	arrRegEx[i]   = /ｃ/g;	arrRepEx[i++] = "c";
	arrRegEx[i]   = /ｄ/g;	arrRepEx[i++] = "d";
	arrRegEx[i]   = /ｅ/g;	arrRepEx[i++] = "e";
	arrRegEx[i]   = /ｆ/g;	arrRepEx[i++] = "f";
	arrRegEx[i]   = /ｇ/g;	arrRepEx[i++] = "g";
	arrRegEx[i]   = /ｈ/g;	arrRepEx[i++] = "h";
	arrRegEx[i]   = /ｉ/g;	arrRepEx[i++] = "i";
	arrRegEx[i]   = /ｊ/g;	arrRepEx[i++] = "j";
	arrRegEx[i]   = /ｋ/g;	arrRepEx[i++] = "k";
	arrRegEx[i]   = /ｌ/g;	arrRepEx[i++] = "l";
	arrRegEx[i]   = /ｍ/g;	arrRepEx[i++] = "m";
	arrRegEx[i]   = /ｎ/g;	arrRepEx[i++] = "n";
	arrRegEx[i]   = /ｏ/g;	arrRepEx[i++] = "o";
	arrRegEx[i]   = /ｐ/g;	arrRepEx[i++] = "p";
	arrRegEx[i]   = /ｑ/g;	arrRepEx[i++] = "q";
	arrRegEx[i]   = /ｒ/g;	arrRepEx[i++] = "r";
	arrRegEx[i]   = /ｓ/g;	arrRepEx[i++] = "s";
	arrRegEx[i]   = /ｔ/g;	arrRepEx[i++] = "t";
	arrRegEx[i]   = /ｕ/g;	arrRepEx[i++] = "u";
	arrRegEx[i]   = /ｖ/g;	arrRepEx[i++] = "v";
	arrRegEx[i]   = /ｗ/g;	arrRepEx[i++] = "w";
	arrRegEx[i]   = /ｘ/g;	arrRepEx[i++] = "x";
	arrRegEx[i]   = /ｙ/g;	arrRepEx[i++] = "y";
	arrRegEx[i]   = /ｚ/g;	arrRepEx[i++] = "z";

	arrRegEx[i]   = /０/g;	arrRepEx[i++] = "0";
	arrRegEx[i]   = /１/g;	arrRepEx[i++] = "1";
	arrRegEx[i]   = /２/g;	arrRepEx[i++] = "2";
	arrRegEx[i]   = /３/g;	arrRepEx[i++] = "3";
	arrRegEx[i]   = /４/g;	arrRepEx[i++] = "4";
	arrRegEx[i]   = /５/g;	arrRepEx[i++] = "5";
	arrRegEx[i]   = /６/g;	arrRepEx[i++] = "6";
	arrRegEx[i]   = /７/g;	arrRepEx[i++] = "7";
	arrRegEx[i]   = /８/g;	arrRepEx[i++] = "8";
	arrRegEx[i]   = /９/g;	arrRepEx[i++] = "9";

	arrRegEx[i]   = /＠/g;	arrRepEx[i++] = "@";
	arrRegEx[i]   = /．/g;	arrRepEx[i++] = ".";
	arrRegEx[i]   = /－/g;	arrRepEx[i++] = "-";

	// 摒除所有非法的 E-Mail 字元或是 [aaa.bbb.ccc.ddd]
	//arrRegEx[i++]   = /[^\w@._\-]+/g;
	//arrRepEx[i++] = "";

	// E-Mail 的 @ 後面只能放一些字元
	arrRegEx[i]   = /@[^a-zA-Z0-9]+/;	arrRepEx[i++] = "@";

	// E-Mail 的結尾，一定是「英文」
	arrRegEx[i]   = /[^a-zA-Z]+$/;		arrRepEx[i++] = "";

	for(i=0; i<arrRegEx.length ; i++)
	{
		str = str.replace(arrRegEx[i], arrRepEx[i]);
	}

	return str;
};


DoggyFormatObject.prototype.username_check = function(str)
{
	if(str.length < 2 || str.length > 16 || !str.match(/^[a-z][a-z0-9_\.]+[a-z0-9]$/)) {
		return false;
	}

	return true
}

DoggyFormatObject.prototype.fieldname_check = function(str)
{
	if(str.length < 2 || str.length > 16 || !str.match(/^[a-z][a-z0-9_]+[a-z0-9]$/)) {
		return false;
	}
	// 保留欄位名稱!
	switch(str)
	{
		case "id": return false;
		case "table_id": return false;

		case "ctime": return false;
		case "mtime": return false;
		case "cuserid": return false;
		case "muserid": return false;
	}

	return true
}

DoggyFormatObject.prototype.date_check = function(str)
{
	if(!str.match(/^[1-2]\d\d\d-[0-1]\d-[0-3]\d$/)) {
		return false;
	}
	return true
}

DoggyFormatObject.prototype.time_check = function(str)
{
	if(!str.match(/^[0-2]\d:[0-5]\d:[0-5]\d$/)) {
		return false;
	}
	return true
}

DoggyFormatObject.prototype.datetime_check = function(str)
{
	if(!str.match(/^[1-2]\d\d\d-[0-1]\d-[0-3]\d [0-2]\d:[0-5]\d:[0-5]\d$/)) {
		return false;
	}
	return true
}

DoggyFormatObject.prototype.email_check = function(str)
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)

	// 沒有小老鼠符號(@)
	if (str.indexOf(at)==-1){
	   return false
	}

	// 沒有小老鼠符號(@)或出現在頭尾
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	// 沒有逗點符號(.)或出現在頭尾
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	// 超過兩個 @
	if (str.indexOf(at,(lat+1))!=-1){
	   return false
	}

	// @ 前、後是 .
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	   return false
	}

	// @ 後面沒有任何的 .
	if (str.indexOf(dot,(lat+2))==-1){
	   return false
	}

	// 出現空白
	if (str.indexOf(" ")!=-1){
	   return false
	}

	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

	if(filter.test(str) == false){
		return false
	}

	return true
}


// Initialize

dformat = new DoggyFormatObject();


function check_form_email(fieldID)
{
	if(dformat.email_check(fieldID.value) == false)
	{
		fieldID.style.background = '#FF6666'
	}
	else
	{
		fieldID.style.background = '#FFFFFF'
	}
}

function check_form_tel(fieldID)
{
	if(dformat.tel_check(fieldID.value) == false)
	{
		fieldID.style.background = '#FF6666'
	}
	else
	{
		fieldID.style.background = '#FFFFFF'
	}
}



function searchOptions(obj, str, offset)
{
	str = str.replace(/([\*\(\)\?\[\]\\\+\|\{\}\.])/g, '\\$1')

	if(!offset)
		offset = 1

	if(str.length < 1)
		return false

	re = new RegExp(str, "i");

	if(obj.length > 1)
	{
		for(i=offset ; i < obj.length ; i++)
		{
			if(re.test(obj.options[i].text))
			{
				obj.selectedIndex = i
				return true
			}
			if(i >= obj.length-1)
			{
				//if(confirm('找不到任何資料\r\n\r\n是否重頭搜尋？'))
				{
					i = 1;
				}
			}
		}
		return false
	}

	return false
}

function makeapassword(passwordlength)
{
	var pwdchars = new Array();

	pwdchars[0] = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
	pwdchars[1] = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
	pwdchars[2] = new Array("0","1","2","3","4","5","6","7","8","9");

	var specialsokay = "no";

	if (specialsokay == "yes")
	{
		pwdchars[3] = new Array("!","@","#","$","%","^","&","*","(",")","+");
	}

	var rannum1;
	var rannum2;
	var rannum3;
	var pickelem;
	var loopcounter;
	var passwordelement;
	var finalpassword = "";
	var passwordarray = new Array();

	for (loopcounter = 0; loopcounter < passwordlength; loopcounter++)
	{
		do
		{
			rannum1 = Math.floor(Math.random() * pwdchars.length);

			if ((loopcounter == 0) && (specialsokay == "yes"))
			{
				rannum1=Math.floor(Math.random() * (pwdchars.length - 1))
			}

			pickelem = Math.floor(Math.random() * pwdchars[rannum1].length)

			passwordelement = pwdchars[rannum1][pickelem];

		} while ((rannum1 == rannum2) && (rannum1 == rannum3));

		finalpassword = finalpassword + passwordelement;

		rannum3 = rannum2;
		rannum2 = rannum1;

		if (rannum1 == 3)
		{
			rannum3 = rannum2;
		}
	}
	return finalpassword;
}

var fapi = new DoggyFormatObject();

var iapi = new DoggyInputObject();

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_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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];}
}

