/*-------------------------------------------------------------------------------------------------------------------
 Script Name: functions.js
 Author: Tony Wei (魏志國)  tonywei123@gmail.com
 Description: JavaScript 共用函式庫
 Revision History:
   1.0: original version 2008/4/23
-------------------------------------------------------------------------------------------------------------------*/


// 我要發言
function check_forum_add()
{
	if ( ! check_required( 'FP_Poster', '發表人' )   ) return false;
	if ( ! check_required( 'FP_Email', 'E-mail' )   ) return false;
	if ( ! check_email( 'FP_Email' ) ) return false;
	if ( ! check_required( 'FP_Title', '標題' )   ) return false;
	if ( ! check_required( 'FP_Content', '發言內容' )   ) return false;
	return true;
}

// 我要回應
function check_forum_response()
{
	if ( ! check_required( 'FR_Poster', '發表人' )   ) return false;
	if ( ! check_required( 'FR_Email', 'E-mail' )   ) return false;
	if ( ! check_email( 'FR_Email' ) ) return false;
	if ( ! check_required( 'FR_Title', '標題' )   ) return false;
	if ( ! check_required( 'FR_Content', '回應內容' )   ) return false;
	return true;
}

// 會員登入表單
function check_login()
{
	if ( ! check_required( 'MI_ID1', '帳號' ) ) return false;
	if ( ! check_required( 'MI_Password1', '密碼' ) ) return false;
	return true;
}

// 我要回應
function check_studyjoin()
{
	if ( ! check_required( 'SJ_Name', '姓名' )   ) return false;
	if ( ! check_required( 'SJ_Email', 'E-mail' )   ) return false;
	if ( ! check_email( 'SJ_Email' ) ) return false;
	if ( ! check_required( 'SJ_Tel1', '電話區域碼' )   ) return false;
	if ( ! check_numeric2( 'SJ_Tel1', '電話區域碼' )     ) return false;
	if ( ! check_required( 'SJ_Tel2', '電話' )   ) return false;
	if ( ! check_numeric2( 'SJ_Tel2', '電話' )     ) return false;
	if ( ! check_required( 'SJ_Mobile', '手機' )   ) return false;
	if ( ! check_numeric2( 'SJ_Mobile', '手機' )     ) return false;
	return true;
}

// 問卷調查
function check_survey( fm )
{
	if ( ! check_required( 'VD_Name', '姓名' )   ) return false;
	if ( ! ( fm.VD_Sex1.checked || fm.VD_Sex2.checked ) ) {
		alert( "請勾選您的性別!" );
		fm.VD_Sex1.focus();
		return false;
	}
	if ( ! check_required( 'VD_Age', '年齡' )   ) return false;
	if ( ! check_numeric( 'VD_Age', '年齡' )    ) return false;
	if ( fm.VD_Education.selectedIndex == 0 ) {
		alert( "請選擇您的學歷!" );
		fm.VD_Education.focus();
		return false;
	}
	if ( ! ( fm.VD_Working1.checked || fm.VD_Working2.checked || fm.VD_Working3.checked ) ) {
		alert( "請勾選您的就業狀況!" );
		fm.VD_Working1.focus();
		return false;
	}
	if ( fm.VD_City.selectedIndex == 0 ) {
		alert( "請選擇您的居住地區!" );
		fm.VD_City.focus();
		return false;
	}
	if ( ! check_required( 'VD_Address', '通訊地址' )   ) return false;
	if ( ! ( fm.VD_Receive1.checked || fm.VD_Receive2.checked ) ) {
		alert( "請勾選是否願意收到職訓相關課程資訊!" );
		fm.VD_Receive1.focus();
		return false;
	}
	if ( ! check_required( 'VD_Email', '電子郵件' )   ) return false;
	if ( ! check_email( 'VD_Email' ) ) return false;
	return true;
}

// 分析介面
function go_program1()
{
	var yyyy1	=  document.getElementById( "yyyy1a" ).options[ document.getElementById( "yyyy1a" ).selectedIndex ].text;
	var mm1	=  document.getElementById( "mm1a" ).options[ document.getElementById( "mm1a" ).selectedIndex ].text;
	var dd1		=  document.getElementById( "dd1a" ).options[ document.getElementById( "dd1a" ).selectedIndex ].text;
	var yyyy2	=  document.getElementById( "yyyy1b" ).options[ document.getElementById( "yyyy1b" ).selectedIndex ].text;
	var mm2	=  document.getElementById( "mm1b" ).options[ document.getElementById( "mm1b" ).selectedIndex ].text;
	var dd2		 =  document.getElementById( "dd1b" ).options[ document.getElementById( "dd1b" ).selectedIndex ].text;
	location.href = "index.php?fn=program_result&date1=" +yyyy1 + "/" + mm1 + "/" + dd1 + "&date2=" + yyyy2 + "/" + mm2 + "/" + dd2;
}

// 分析介面
function go_program2()
{
	var yyyy1	=  document.getElementById( "yyyy2a" ).options[ document.getElementById( "yyyy2a" ).selectedIndex ].text;
	var mm1	=  document.getElementById( "mm2a" ).options[ document.getElementById( "mm2a" ).selectedIndex ].text;
	var dd1		=  document.getElementById( "dd2a" ).options[ document.getElementById( "dd2a" ).selectedIndex ].text;
	var yyyy2	=  document.getElementById( "yyyy2b" ).options[ document.getElementById( "yyyy2b" ).selectedIndex ].text;
	var mm2	=  document.getElementById( "mm2b" ).options[ document.getElementById( "mm2b" ).selectedIndex ].text;
	var dd2		 =  document.getElementById( "dd2b" ).options[ document.getElementById( "dd2b" ).selectedIndex ].text;
	location.href = "index.php?fn=program_result2&date1=" +yyyy1 + "/" + mm1 + "/" + dd1 + "&date2=" + yyyy2 + "/" + mm2 + "/" + dd2;
}


// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*-------------------------------------------------------------------------------------------------------------------
 Function: bookmark()
 Description: 加入我的最愛
 Input: 網址，網站名稱
 Output: 是否驗證通過 true or false
 Example: 
	bookmark( 'http://www.hinet.net/', 'Hinet' );
Revision History:
   1.0: original version 2008/4/17
-------------------------------------------------------------------------------------------------------------------*/
function bookmark( address, sitename ) {
	if ( window.sidebar ) {
		window.sidebar.addPanel( sitename, address,"" );
	} else if( document.all ) {
		window.external.AddFavorite( address, sitename );
	} else if( window.opera && window.print ) {
		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_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_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_range()
 Description: 檢查表單欄位值是否介於最小值與最大值之間, 並轉換為整數
 Input: 物件ID, 最小值, 最大值, 訊息
 Output: 是否驗證通過 true or false
 Example: 
	check_range( "amount", 1, 100, "數量" );
Revision History:
   1.0: original version 2007/1/8
-------------------------------------------------------------------------------------------------------------------*/
function check_range( objID, iMin, iMax, sMessage )
{
	var objField = document.getElementById( objID );
	objField.value = parseInt( objField.value );
	iMin  = parseInt( iMin );
	iMax = parseInt( iMax );
	if ( objField.value < iMin || objField.value > iMax ) {
		alert( sMessage + "必須介於 " + iMin + " 與 " + iMax + " 之間!" );
		objField.select();
		objField.focus();
		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()
-------------------------------------------------------------------------------------------------------------------*/
function check_email2( sValue )
{
	var filter= /^([\w-]+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if ( ! filter.test( sValue ) ) {
		alert( "朋友的email " + sValue + " ---> 這個不是正確的Email信箱!" );
		return false;
	}
	return true;
}

/*-------------------------------------------------------------------------------------------------------------------
 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()
{
	var url = "index.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( "此帳號已經有人使用，請您換一個帳號!" );
		document.getElementById("MI_ID").value = "";
		return false;
	}
	return true;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: change_select_index()
 Description: 更改 <SELECT> 下拉選單的選取項目
 Input: 物件ID, 被選取值
 Output: N/A
 Example: 
	change_select_index( "education", "{education}" );
Revision History:
   1.0: original version 2007/1/5
-------------------------------------------------------------------------------------------------------------------*/
function change_select_index( objID, sValue )
{
	var coll = document.getElementById( objID );
	for ( i=0; i< coll.options.length ; i++ ) {
		if ( coll.options(i).value == sValue ) {
			coll.selectedIndex = i;
		}
	}
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: jump_form()
 Description: 換頁, 使用 POST
 Input: 新頁碼
 Output: N/A
 Example: 
	jump_form(2);
Revision History:
   1.0: original version 2007/1/8
-------------------------------------------------------------------------------------------------------------------*/
function jump_form(p) {
	document.form1.page.value = p;
	document.form1.submit();
}

/*-------------------------------------------------------------------------------------------------------------------
 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
-------------------------------------------------------------------------------------------------------------------*/
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=" + encodeURI( document.getElementById('keyword').value );
	}
	if ( qs.get("search") != null ) {
		tail += "&search=" + encodeURI( document.getElementById('search').value );
	}
	location.href = "index.php?" + fn + "&no=" + qs.get("no") + "&p=" + p + tail + "#anchor";
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: jump_prod()
 Description: 換頁, 使用 GET
 Input: cat_id, 新頁碼
 Output: N/A
 Example: 
	jump_prod( 5, 2 );
Revision History:
   1.0: original version 2007/1/27
-------------------------------------------------------------------------------------------------------------------*/
function jump_prod( cno, p) {
	location.href = location.pathname + "?cno=" + cno + "&p=" + p;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: thesame_click()
 Description: "勾選"同訂購人資料
 Input: this
 Output: N/A
 Example: 
	go_search();
Revision History:
   1.0: original version 2007/1/11
-------------------------------------------------------------------------------------------------------------------*/
function thesame_click( objSelf )
{
	if ( objSelf.checked ) {
		var fm = objSelf.form;
		for ( var i=0 ; i < fm.elements.length ; i++ )	{
			if ( fm.elements[i].value1 ) {
				if ( fm.elements[i].type == "text" ) {
				  fm.elements[i].value = fm.elements[i].value1;
				}
				if ( fm.elements[i].type == "select-one" ) {
				  fm.elements[i].selectedIndex = fm.elements[i].value1;
				}
			}
		}
	}
}

/*-------------------------------------------------------------------------------------------------------------------
 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 null;
}

function delCookie( name )
{
	setCookie(name,"",-1);
}
