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



// -------------------------------------------------------------------------------------------------------------------------------------------
// 附加於 windows.onload 函式
// -------------------------------------------------------------------------------------------------------------------------------------------

if ( window.attachEvent ) window.attachEvent( "onload", all_winload );	// IE
if ( window.addEventListener ) document.addEventListener( "DOMContentLoaded", all_winload, false );	 // Mozilla

// 檢查「記住我」是否勾選
function all_winload()
{
	if ( typeof( document.login_form ) != "undefined" ) {
		if ( getCookie( "remeber_me" ) == "1" ) {
			document.getElementById( "remeber_me" ).checked	= true;
			document.getElementById( "uid" ).value		= getCookie( "uid" );
			document.getElementById( "pwd" ).value	= getCookie( "pwd" );
		}
	}
}

// -------------------------------------------------------------------------------------------------------------------------------------------


// 預先檢查驗證碼
function check_code()
{
	var url = "/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_forget( fm )
{
	if ( ! check_required( 'MI_ID', '帳號' ) ) return false;
	if ( ! check_required( 'MI_Email', 'Email' ) ) return false;
	if ( ! check_email( 'MI_Email' ) ) return false;
	fm.submit();
}

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

	// 檢查「記住我」的勾選狀態
	if ( document.getElementById( "remeber_me" ).checked ) {
		setCookie( "remeber_me", "1", 90 );
		setCookie( "uid",		document.getElementById( "uid" ).value,	90 );
		setCookie( "pwd",	document.getElementById( "pwd" ).value,	90 );
	} else {
		delCookie( "remeber_me" );
		delCookie( "uid" );
		delCookie( "pwd" );
	}

	fm.submit();
}

// 會員專區 -- 登入
function check_member_login( fm )
{
	if ( ! check_required( 'uid', '帳號' ) ) return false;
	if ( ! check_required( 'pwd', '密碼' ) ) return false;
	if ( ! check_required( 'code', '驗證碼' ) ) return false;
	if ( ! check_numeric2( 'code', '驗證碼' ) ) return false;
	if ( ! check_code() ) return false;
	fm.submit();
}

// 加入會員
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_LastName', '姓' ) ) return false;
	if ( ! check_required( 'MI_Name', '名' ) ) return false;
	if ( ! ( fm.MI_Sex1.checked || fm.MI_Sex2.checked ) ) {
		alert( "請點選您的性別!" );
		fm.MI_Sex1.focus();
		return false;
	}
	if ( ! check_required( 'MI_Tel', '電話' ) ) return false;
	if ( fm.MI_Tel.value != "" ) {
		if ( ! check_numeric2( 'MI_Tel', '電話' )     ) return false;
	}
	if ( ! check_required( 'MI_Mobile', '手機' ) ) return false;
	if ( fm.MI_Mobile.value != "" ) {
		if ( ! check_numeric2( 'MI_Mobile', '手機' )     ) return false;
		if ( fm.MI_Mobile.value.length != 10 ) {
			alert( "手機號碼必須是 10 個阿拉伯數字!" );
			fm.MI_Mobile.focus();
			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 ( yyyy != "請選擇" ) {
		if ( ! check_date( yyyy, mm, dd ) ) return false;
	} else {
		alert( "請選取您的生日!" );
		fm.yyyy.focus();
		return false;
	}
	if ( fm.MI_MO_Serial.selectedIndex < 1 ) {
		alert( "請選取您的職業!" );
		fm.MI_MO_Serial.focus();
		return false;
	}
	if ( ! check_required( 'MI_Email', 'Email' ) ) return false;
	if ( ! check_email( 'MI_Email' ) ) return false;
	if ( ! check_member_email() ) {
		return false;
	}
	if ( fm.cityarea.selectedIndex < 1 ) {
		alert( "請選擇縣市鄉鎮!" );
		fm.cityarea.focus();
		return false;
	}
	if ( ! check_required( 'MI_Address', '聯絡地址' ) ) return false;


	var bIsChecked = false;
	for ( var i=1 ; i <= 50 ; i++ )
	{
		if ( document.getElementById( "MI_Show" + i ) == null ) {
			break;
		}
		if ( document.getElementById( "MI_Show" + i ).checked ) {
			bIsChecked = true;
			break;
		}
	}
	if ( ! bIsChecked ) {
		alert( "請勾選您喜愛的節目!" );
		return false;
	}


	var bIsChecked = false;
	for ( var i=1 ; i <= 50 ; i++ )
	{
		if ( document.getElementById( "MI_Star" + i ) == null ) {
			break;
		}
		if ( document.getElementById( "MI_Star" + i ).checked ) {
			bIsChecked = true;
			break;
		}
	}
	if ( ! bIsChecked ) {
		alert( "請勾選您喜愛的藝人!" );
		return false;
	}


	if ( ! check_required( 'code', '驗證碼' ) ) return false;
	if ( ! check_numeric2( 'code', '驗證碼' ) ) return false;
	if ( ! check_code() ) return false;
	fm.submit();
}

// 修改會員資料
function check_member_modify( 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;
		}
	}
	if ( ! check_required( 'MI_LastName', '姓' ) ) return false;
	if ( ! check_required( 'MI_Name', '名' ) ) return false;
	if ( ! check_required( 'MI_Tel', '電話' ) ) return false;
	if ( fm.MI_Tel.value != "" ) {
		if ( ! check_numeric2( 'MI_Tel', '電話' )     ) return false;
	}
	if ( ! check_required( 'MI_Mobile', '手機' ) ) return false;
	if ( fm.MI_Mobile.value != "" ) {
		if ( ! check_numeric2( 'MI_Mobile', '手機' )     ) return false;
		if ( fm.MI_Mobile.value.length != 10 ) {
			alert( "手機號碼必須是 10 個阿拉伯數字!" );
			fm.MI_Mobile.focus();
			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 ( yyyy != "請選擇" ) {
		if ( ! check_date( yyyy, mm, dd ) ) return false;
	} else {
		alert( "請選取您的生日!" );
		fm.yyyy.focus();
		return false;
	}
	if ( fm.MI_MO_Serial.selectedIndex < 1 ) {
		alert( "請選取您的職業!" );
		fm.MI_MO_Serial.focus();
		return false;
	}
	if ( ! check_required( 'MI_Email', 'Email' ) ) return false;
	if ( ! check_email( 'MI_Email' ) ) return false;
	if ( fm.cityarea.selectedIndex < 1 ) {
		alert( "請選擇縣市鄉鎮!" );
		fm.cityarea.focus();
		return false;
	}
	if ( ! check_required( 'MI_Address', '聯絡地址' ) ) return false;
	fm.submit();
}

// 合作提案
function check_cooperation( fm )
{
	if ( ! check_required( 'TD_Name', '姓名' ) ) return false;
	if ( ! check_required( 'TD_CompanyName', '公司名稱' ) ) return false;
	if ( ! check_required( 'TD_CompanyAddr', '公司地址' ) ) return false;
	if ( ! check_required( 'TD_Tel', '聯絡電話' ) ) return false;
	if ( ! check_numeric2( 'TD_Tel', '聯絡電話' ) ) return false;
	if ( ! check_required( 'TD_Email', '電子信箱' ) ) return false;
	if ( ! check_email( 'TD_Email' ) ) return false;
	if ( fm.TD_Fax.value != "" ) {
		if ( ! check_numeric2( 'TD_Fax', '傳真電話' ) ) return false;
	}
	if ( ! check_required( 'TD_Content', '意見內容' ) ) return false;
	if ( ! check_required( 'code', '驗證碼' ) ) return false;
	if ( ! check_numeric2( 'code', '驗證碼' ) ) return false;
	if ( ! check_code() ) return false;
	fm.submit();
}

// 聯絡我們
function check_contact( fm )
{
	if ( ! check_required( 'CD_Name', '姓名' ) ) return false;
	if ( ! check_required( 'CD_Tel', '聯絡電話' ) ) return false;
	if ( ! check_numeric2( 'CD_Tel', '聯絡電話' ) ) return false;
	if ( ! check_required( 'CD_Email', '電子信箱' ) ) return false;
	if ( ! check_email( 'CD_Email' ) ) return false;
	if ( ! check_required( 'CD_Content', '意見內容' ) ) return false;
	if ( ! check_required( 'code', '驗證碼' ) ) return false;
	if ( ! check_numeric2( 'code', '驗證碼' ) ) return false;
	if ( ! check_code() ) return false;
	fm.submit();
}

// 投票
function vote( score )
{
	var today = new Date();
	var time = Math.round( today.getTime() / 1000 / 60 / 60 );
	if ( getCookie( "vote_time" ) != "" ) {
		var oldTime = parseInt( getCookie( "vote_time" ) );
		if ( time - oldTime <= 23 ) {
			alert( "24小時之內只能投一次票喔!" );
			return false;
		}
	}
	setCookie( "vote_time", time, 90 );
	location.href = "/vote/" + score + "/";
}


/*-------------------------------------------------------------------------------------------------------------------
 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 = "/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( "此帳號已經有人使用，請您換一個帳號!" );
		document.getElementById("MI_ID").focus();
		return false;
	}
	return true;
}
function check_member_email()
{
	var url = "/member_check/?rand=" + Math.random() + "&MI_Email=" + document.getElementById("MI_Email").value;
	var objTag = ccioo_ajax_getXmlData( url ).getElementsByTagName("item");
	var sReturn = objTag[0].firstChild.nodeValue;
	if ( parseInt( sReturn ) > 0 ) {
		alert( "此 E-Mail 已經註冊過，如果您忘記密碼，請利用 '忘記密碼' 功能來查詢!" );
		document.getElementById("MI_Email").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_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;
}

/*-------------------------------------------------------------------------------------------------------------------
 cookie Functions
 lifetype\js\cookie\cookie.js
 1.0 加入 encodeURI 編碼來儲存，以解決 Mozilla 儲存中文會導致 Cookie 資料損毀的問題 Tony in 2008/2/27
-------------------------------------------------------------------------------------------------------------------*/
function setCookie( name, value, days )
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+ encodeURI( value ) +expires+"; path=/";
}

function getCookie( name )
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return decodeURI( c.substring(nameEQ.length,c.length) );
	}
	return "";
}

function delCookie( name )
{
	setCookie(name,"",-1);
}

/*-------------------------------------------------------------------------------------------------------------------
 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
   3.0: rewrite 版 2009/5/16
   4.0: SEO 網址版 2009/8/27
-------------------------------------------------------------------------------------------------------------------*/
function jump_get( p ) {

	var fn = "", no = "";
	var head = location.href.substr( location.href.indexOf( "//" ) + 2 );
	head = head.substr( head.indexOf( "/" ) + 1 );
	if ( head.indexOf( "/" ) > -1 ) {
		fn = head.substr( 0, head.indexOf( "/" ) );
		head = head.substr( head.indexOf( "/" ) + 1 );
	}
	if ( head.indexOf( "/" ) > -1 ) {
		no = head.substr( 0, head.indexOf( "/" ) );
	} else {
		no = "0";
	}
	var tail = "";
	if ( document.getElementById( "keyword" ) != null ) {
		tail += "&k=" + encodeURI( document.getElementById( "keyword" ).value ); 
	}
	if ( tail != "" ) {
		tail = "?" + tail.substr( 1 );	// 去除開頭的 & 字元
	}

	var url = "/" + fn + "/" + no + "/" + p + "/" + tail;
	//alert( url );
	location.href = url;
}

