/*-------------------------------------------------------------------------------------------------------------------
 Script Name: functions.js
 Author: Tony Wei (魏志國)  tonywei123_at_gmail.com
 Description: JavaScript 共用函式庫
 Revision History:
   1.0: original version 2008/10/23
-------------------------------------------------------------------------------------------------------------------*/

/* Sample Code

var area = new Array();
area[ 0 ] = "台北";
area[ 1 ] = "彰化";

var hospital = new Array();
hospital[ 0 ] = new Array();
hospital[ 1 ] = new Array();

hospital[ 0 ][ 0 ] = "請選擇";
hospital[ 0 ][ 1 ] = "甲";
hospital[ 0 ][ 2 ] = "乙";
hospital[ 0 ][ 3 ] = "丙";

hospital[ 1 ][ 0 ] = "請選擇";
hospital[ 1 ][ 1 ] = "AA";
hospital[ 1 ][ 2 ] = "BB";
hospital[ 1 ][ 3 ] = "CC";

*/

// 建立醫院的地區選單
function make_area()
{
	var areaMenu = document.getElementById( "area" );
	areaMenu.options.length = area.length;
	for ( var i=0 ; i < area.length  ; i++ )
	{
		areaMenu.options[ i ].text = area[ i ];
	}
}

// 建立該地區醫院名稱選單
function make_hospital()
{
	var iIndex = document.getElementById( "area" ).selectedIndex;
	var hospitalMenu;
	if ( document.getElementById( "MI_HD_Serial" ) != null ) {
		hospitalMenu = document.getElementById( "MI_HD_Serial" );
	} else {
		hospitalMenu = document.getElementById( "EJ_HD_Serial" );
	}
	hospitalMenu.options.length = hospital[ iIndex ].length;
	hospitalMenu.selectedIndex = 0;
	for ( var i=0 ; i < hospital[ iIndex ].length  ; i++ )
	{
		hospitalMenu.options[ i ].text		= hospital[ iIndex ][ i ];
		hospitalMenu.options[ i ].value	= hsnumber[ iIndex ][ i ];
	}
	if ( document.getElementById( "MI_Other" ) != null ) {
		document.getElementById( "MI_Other" ).style.display = "none";
	} else {
		document.getElementById( "EJ_Other" ).style.display = "none";
	}
}


// 預先檢查驗證碼
function check_code()
{
	var url = "home.php?fn=check_code&rand=" + Math.random() + "&code=" + document.getElementById( "code" ).value;
	var objTag = ccioo_ajax_getXmlData( url ).getElementsByTagName("item");
	var sReturn = objTag[0].firstChild.nodeValue;
	if ( parseInt( sReturn ) > 0 ) {
		alert( "請輸入正確的驗證碼!" );
		return false;
	}
	return true;
}

// 聯絡我們
function check_contact()
{
	if ( ! check_required( '姓名', '姓名' )     ) return false;
	if ( ! check_required( '聯絡電話', '聯絡電話' )     ) return false;
	if ( ! check_numeric2( '聯絡電話', '聯絡電話' )     ) return false;
	if ( ! check_required( 'email', 'email' )     ) return false;
	if ( ! check_email( 'email' ) ) return false;
	if ( ! check_required( '留言', '您的留言' )   ) return false;
	if ( ! check_required( 'code', '驗證碼' ) ) return false;
	if ( ! check_numeric2( 'code', '驗證碼' )     ) return false;
	if ( ! check_code() ) return false;
	return true;
}

// 活動報名
function check_event_b( fm )
{
	if ( document.getElementById( "hascode" ).value != "0" ) {
		if ( ! check_required( 'EJ_Code', '活動代號' ) ) return false;
	}
	if ( fm.EJ_Code.value != "" ) {
		if ( ! check_eventcode() ) {
			return false;
		}
	}
	if ( ! check_required( 'EJ_Name', '姓名' ) ) return false;
	if ( ! check_required( 'EJ_Pid', '身份證字號' ) ) return false;
	var yyyy	= fm.yyyy.options[ fm.yyyy.selectedIndex ].text;
	var mm	= fm.mm.options[ fm.mm.selectedIndex ].text;
	var dd		= fm.dd.options[ fm.dd.selectedIndex ].text;
	if ( ! check_date( yyyy, mm, dd ) ) return false;
	var hospital = document.getElementById( "EJ_HD_Serial" );
	if ( hospital.selectedIndex < 1 ) {
		alert( "請選擇醫院!" );
		return false;
	}
	if ( hospital.options[ hospital.selectedIndex ].text == "其他" ) {
		if ( ! check_required( 'EJ_Other', '醫院名稱' ) ) return false;
	}
	if ( ! check_required( 'EJ_Department', '科別' ) ) return false;
	if ( ! check_required( 'EJ_Title', '職稱' ) ) return false;
	if ( ! check_required( 'EJ_Email', 'email' ) ) return false;
	if ( ! check_email( 'EJ_Email' ) ) return false;
	if ( ! check_required( 'EJ_Tel', '電話' ) ) return false;
	if ( ! check_numeric2( 'EJ_Tel', '電話' )     ) return false;
	if ( ! check_required( 'EJ_Mobile', '手機' ) ) return false;
	if ( ! check_numeric2( 'EJ_Mobile', '手機' )     ) return false;
	if ( fm.city.selectedIndex == 0 || fm.cityarea.selectedIndex == 0 ) {
		alert( "請選擇地址的鄉鎮縣市!" );
		return false;
	}
	if ( ! check_required( 'EJ_Address', '地址' ) ) return false;
}

function check_eventcode()
{
	var url = "home.php?fn=eventcode_check&rand=" + Math.random() + "&no=" + document.getElementById( "EJ_ED_Serial" ).value + "&code=" + document.getElementById("EJ_Code").value;
	var objTag = ccioo_ajax_getXmlData( url ).getElementsByTagName("item");
	var sReturn = objTag[0].firstChild.nodeValue;
	var iReturn = parseInt( sReturn );
	switch ( iReturn )
	{
		case 0:
			alert( "您輸入的活動代碼錯誤!" );
			return false;
			break;
		case 1:
			alert( "您輸入的活動代碼已經使用過了!" );
			return false;
			break;
		case 2:
			break;
	}
	return true;
}

// 會員登入
function check_login()
{
	if ( ! check_required( 'uid', '帳號' ) ) return false;
	if ( ! check_required( 'pwd', '密碼' ) ) return false;
	return true;
}

// 忘記密碼
function check_member_forget()
{
	if ( ! check_required( 'MI_ID', '帳號' ) ) return false;
}

// 修改會員資料
function check_member_c( fm )
{
	if ( fm.MI_Password.value != "" ) {
		if ( fm.MI_Password.value.length < 6 ) {
			alert( "密碼須為 6~16 個英文及數字組合!" );
			fm.MI_Password.value = "";
			fm.MI_Password2.value = "";
			fm.MI_Password.focus();
			return false;
		}
		if ( ! check_required( 'MI_Password2', '確認密碼' )   ) return false;
		if ( fm.MI_Password.value != fm.MI_Password2.value ) {
			alert( "密碼與確認密碼須一致!" );
			fm.MI_Password.value = "";
			fm.MI_Password2.value = "";
			fm.MI_Password.focus();
			return false;
		}
	}
	var hospital = document.getElementById( "MI_HD_Serial" );
	if ( hospital.selectedIndex < 1 ) {
		alert( "請選擇醫院!" );
		return false;
	}
	if ( hospital.options[ hospital.selectedIndex ].text == "其他" ) {
		if ( ! check_required( 'MI_Other', '醫院名稱' ) ) return false;
	}
	if ( ! check_required( 'MI_Department', '科別' ) ) return false;
	if ( ! check_required( 'MI_Title', '職稱' ) ) return false;
	var yyyy	= fm.yyyy.options[ fm.yyyy.selectedIndex ].text;
	var mm	= fm.mm.options[ fm.mm.selectedIndex ].text;
	var dd		= fm.dd.options[ fm.dd.selectedIndex ].text;
	if ( ! check_date( yyyy, mm, dd ) ) return false;
	if ( ! check_required( 'MI_Email', 'email' ) ) return false;
	if ( ! check_email( 'MI_Email' ) ) return false;
	if ( ! check_required( 'MI_Tel', '電話' ) ) return false;
	if ( ! check_numeric2( 'MI_Tel', '電話' )     ) return false;
	if ( ! check_required( 'MI_Fax', '傳真' ) ) return false;
	if ( ! check_numeric2( 'MI_Fax', '傳真' )     ) return false;
	if ( ! check_required( 'MI_Mobile', '手機' ) ) return false;
	if ( ! check_numeric2( 'MI_Mobile', '手機' )     ) return false;
	if ( fm.city.selectedIndex == 0 || fm.cityarea.selectedIndex == 0 ) {
		alert( "請選擇地址的鄉鎮縣市!" );
		return false;
	}
	if ( ! check_required( 'MI_Address', '住址' ) ) return false;
}

// 加入會員
function check_member_join( fm )
{
	if ( ! check_required( 'MI_ID', '帳號' ) ) return false;
	if ( fm.MI_ID.value.length < 4 ) {
		alert( "登入帳號須為 4~16 個英文及數字組合!" );
		fm.MI_ID.value = "";
		fm.MI_ID.focus();
		return false;
	}
	if ( ! check_member_account() ) {
		return false;
	}
	if ( ! check_required( 'MI_Password', '密碼' ) ) return false;
	if ( fm.MI_Password.value.length < 6 ) {
		alert( "密碼須為 6~16 個英文及數字組合!" );
		fm.MI_Password.value = "";
		fm.MI_Password2.value = "";
		fm.MI_Password.focus();
		return false;
	}
	if ( ! check_required( 'MI_Password2', '確認密碼' )   ) return false;
	if ( fm.MI_Password.value != fm.MI_Password2.value ) {
		alert( "密碼與確認密碼須一致!" );
		fm.MI_Password.value = "";
		fm.MI_Password2.value = "";
		fm.MI_Password.focus();
		return false;
	}
	if ( ! check_required( 'MI_Name', '姓名' ) ) return false;
	var hospital = document.getElementById( "MI_HD_Serial" );
	if ( hospital.selectedIndex < 1 ) {
		alert( "請選擇醫院!" );
		return false;
	}
	if ( hospital.options[ hospital.selectedIndex ].text == "其他" ) {
		if ( ! check_required( 'MI_Other', '醫院名稱' ) ) return false;
	}
	if ( ! check_required( 'MI_Department', '科別' ) ) return false;
	if ( ! check_required( 'MI_Title', '職稱' ) ) return false;
	var yyyy	= fm.yyyy.options[ fm.yyyy.selectedIndex ].text;
	var mm	= fm.mm.options[ fm.mm.selectedIndex ].text;
	var dd		= fm.dd.options[ fm.dd.selectedIndex ].text;
	if ( ! check_date( yyyy, mm, dd ) ) return false;
	if ( ! check_required( 'MI_Email', 'email' ) ) return false;
	if ( ! check_email( 'MI_Email' ) ) return false;
	if ( ! check_required( 'MI_Tel', '電話' ) ) return false;
	if ( ! check_numeric2( 'MI_Tel', '電話' )     ) return false;
	if ( ! check_required( 'MI_Fax', '傳真' ) ) return false;
	if ( ! check_numeric2( 'MI_Fax', '傳真' )     ) return false;
	if ( ! check_required( 'MI_Mobile', '手機' ) ) return false;
	if ( ! check_numeric2( 'MI_Mobile', '手機' )     ) return false;
	if ( fm.city.selectedIndex == 0 || fm.cityarea.selectedIndex == 0 ) {
		alert( "請選擇地址的鄉鎮縣市!" );
		return false;
	}
	if ( ! check_required( 'MI_Address', '住址' ) ) return false;
	if ( ! check_required( 'code', '驗證碼' ) ) return false;
	if ( ! check_code() ) return false;
}

// 搜尋
function check_search()
{
	if ( ! check_required( 'q', '關鍵字' ) ) return false;
	return true;
}

// 電子報訂閱
function check_subscribe( func )
{
	if ( document.getElementById( "epaper_mail" ).value == "輸入E-mail" ) {
		document.getElementById( "epaper_mail" ).value = "";
	}
	if ( ! check_required( 'epaper_mail', 'E-mail' )   ) { void(0); return; }
	if ( ! check_email( 'epaper_mail' ) ) { void(0); return; }
	location.href = "home.php?fn=subscribe&func=" + func + "&epaper_mail=" + document.getElementById( "epaper_mail" ).value;
	return;
}

// 報名記錄，年月選單選取後跳頁
function go_member_d() {
	var yyyy = document.getElementById( "yyyy" );
	var mm = document.getElementById( "mm" );
	location.href = "home.php?fn=member_d&yyyy=" + yyyy.options[ yyyy.selectedIndex ].value + "&mm=" + mm.options[ mm.selectedIndex ].text;
}

// 我要列印
function go_print() {
	var winA = window.open( "about:blank" );
	winA.document.write( document.getElementById( "table_print" ).outerHTML );
}

// 我要報名
function show_tr()
{
	if ( document.getElementById( "hascode" ).value != "0" ) {
		document.getElementById( "tr1" ).style.display = "";
	}
	document.getElementById( "tr2" ).style.display = "";
	document.getElementById( "tr3" ).style.display = "";
	document.getElementById( "tr4" ).style.display = "";
	document.getElementById( "tr5" ).style.display = "";
}


/*-------------------------------------------------------------------------------------------------------------------
 Function: check_member_account()
 Description: 檢查會員帳號是否已經有人使用
 Input: N/A
 Output: true or false
 Example: 
	check_member_account()
 Revision History:
   1.0: original version 2008/1/11
   1.1: 加入亂數，避免快取 2008/1/17
-------------------------------------------------------------------------------------------------------------------*/
function check_member_account()
{
	if ( ! check_required( 'MI_ID', '登入帳號' ) ) return false;
	var url = "home.php?fn=member_check&rand=" + Math.random() + "&MI_ID=" + document.getElementById("MI_ID").value;
	var objTag = ccioo_ajax_getXmlData( url ).getElementsByTagName("item");
	var sReturn = objTag[0].firstChild.nodeValue;
	if ( parseInt( sReturn ) > 0 ) {
		alert( "此帳號已經有人使用，請您換一個帳號!" );
		return false;
	}
	return true;
}
function check_member_account2()
{
	if ( check_member_account() ) {
		alert( "此帳號可以使用!" );
	}
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: check_required()
 Description: 檢查表單欄位是否有填寫
 Input: 物件ID, 訊息
 Output: 是否驗證通過 true or false
 Example: 
	check_required( "username", "姓名" );
Revision History:
   1.0: original version 2007/1/5
-------------------------------------------------------------------------------------------------------------------*/
function check_required( objID, sMessage )
{
	var objField = document.getElementById( objID );
	if ( objField.value.replace( /\s/g, "" ).length == 0 ) {
		alert( "請填寫" + sMessage + "!" );
		objField.focus();
		return false;
	}
	return true;
}
function check_required2( objID, sMessage )
{
	var objField = document.getElementById( objID );
	if ( objField.value.replace( /\s/g, "" ).length == 0 ) {
		alert( "Please Input " + sMessage + "!" );
		objField.focus();
		return false;
	}
	return true;
}

 /*-------------------------------------------------------------------------------------------------------------------
 Function: check_numeric()
 Description: 檢查表單欄位是否是數字
 Input: 物件ID, 訊息
 Output: 是否驗證通過 true or false
 Example: 
	check_numeric( "amount", "數量" );
Revision History:
   1.0: original version 2007/1/8
-------------------------------------------------------------------------------------------------------------------*/
function check_numeric( objID, sMessage )
{
	var objField = document.getElementById( objID );
	if ( isNaN( parseInt( objField.value ) ) || parseInt( objField.value ) != objField.value ) {
		alert( sMessage + "必須是數字!" );
		objField.select();
		objField.focus();
		return false;
	}
	return true;
}

 /*-------------------------------------------------------------------------------------------------------------------
 Function: check_numeric2()
 Description: 檢查表單欄位是否是數字
 Input: 物件ID, 訊息
 Output: 是否驗證通過 true or false
 Example: 
	check_numeric2( 'mobile', '手機或聯絡電話' );
Revision History:
   1.0: original version 2007/1/8
-------------------------------------------------------------------------------------------------------------------*/
function check_numeric2( objID, sMessage )
{
	var objField = document.getElementById( objID );
	var filter= /^([0-9\-\(\)#]+)$/i;
	if ( ! filter.test( objField.value ) ) {
		alert( sMessage + "必須是數字!" );
		objField.select();
		objField.focus();
		return false;
	}
	return true;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: check_date()
 Description: 檢查日期格式是否正確
 Input: 年, 月, 日
 Output: 是否驗證通過 true or false
 Example: 
	check_date( 2008, 1, 29 );
Revision History:
   1.0: original version 2008/3/7
-------------------------------------------------------------------------------------------------------------------*/
function check_date( yyyy, mm, dd )
{
	mm = parseInt( mm ) - 1;
	var day = ( new Date( yyyy, mm, dd ) ).getDate();
	if ( day != dd ) {
		if ( mm == 1 && dd == 29 ) {
			alert( yyyy + " 年不是閏年，2 月沒有 29 天喔!" );
		} else {
			alert( ( mm + 1 ) + " 月沒有 " + dd + " 天喔!" );
		}
		return false;
	}
	return true;
}

 /*-------------------------------------------------------------------------------------------------------------------
 Function: check_email()
 Description: 檢查表單欄位是否是合法 Email 帳號
 Input: 物件ID, 訊息
 Output: 是否驗證通過 true or false
 Example: 
	check_email( "email" );
Revision History:
   1.0: original version 2007/1/8
-------------------------------------------------------------------------------------------------------------------*/
function check_email( objID, sMessage )
{
	var objField = document.getElementById( objID );
	var filter= /^([\w-]+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if ( ! filter.test( objField.value ) ) {
		alert( "請填寫正確的Email信箱!" );
		objField.select();
		objField.focus();
		return false;
	}
	return true;
}
function check_email2( objID, sMessage )
{
	var objField = document.getElementById( objID );
	var filter= /^([\w-]+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if ( ! filter.test( objField.value ) ) {
		alert( "Please input the correct E-mail." );
		objField.select();
		objField.focus();
		return false;
	}
	return true;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: jump_get()
 Description: 換頁, 使用 GET
 Input: 新頁碼
 Output: N/A
 Example: 
	jump_get(2);
Revision History:
   1.0: original version 2007/1/10
   2.0: fn 參數版 2007/6/23 2008/5/30
-------------------------------------------------------------------------------------------------------------------*/
function jump_get( p ) {
	var qs = new Querystring();
	var tail = "";
	var fn = "";
	if ( qs.get("fn") != null ) {
		fn += "fn=" + qs.get("fn");
	}
	if ( qs.get("keyword") != null ) {
		tail += "&keyword=" + qs.get("keyword");
	}
	if ( qs.get("year") != null ) {
		tail += "&year=" + qs.get("year");
	}
	if ( qs.get("cno") != null ) {
		tail += "&cno=" + qs.get("cno");
	}
	if ( qs.get("no") != null ) {
		tail += "&no=" + qs.get("no");
	}
	if ( qs.get("k") != null ) {
		tail += "&k=" + qs.get("k");
	}
	location.href = "home.php?" + fn + "&p=" + p + tail + "#anchor";
}

