/*-------------------------------------------------------------------------------------------------------------------
 Script Name: functions.js
 Author: Tony Wei (魏志國)  tonywei123@gmail.com
 Description: JavaScript 共用函式庫
 Revision History:
   1.0: original version 2008/1/17
-------------------------------------------------------------------------------------------------------------------*/

	
	
	
// 進階搜尋1
function check_advanced1()
{
	if ( ! check_required( 'keyword', '關鍵字' )   ) return false;
	location.href = "index.php?fn=form&keyword=" + encodeURI( document.getElementById( "keyword" ).value );
	return false;
}

// 進階搜尋3
function check_advanced3()
{
	if ( document.getElementById( "city" ).value == "%" ) {
		alert( "請選擇縣市!" );
		return false;
	}
	/*if ( document.getElementById( "cityarea" ).value == "請選擇" ) {
		alert( "請選擇行政區!" );
		return false;
	}*/
	location.href = "index.php?fn=form&city=" + encodeURI( document.getElementById( "city" ).value ) + "&cityarea=" + encodeURI( document.getElementById( "cityarea" ).value );
	return false;
}

// 進階搜尋4
function check_advanced4()
{
	var sUrl = "";
	if ( document.getElementById( "city" ) != null ) {
		if ( document.getElementById( "city" ).value != "%" ) {
			sUrl += "&city=" + encodeURI( document.getElementById( "city" ).value );
		}
		if ( document.getElementById( "cityarea" ).value != "請選擇" ) {
			sUrl += "&cityarea=" + encodeURI( document.getElementById( "cityarea" ).value );
		}
	}

	var sCat = "";
	if ( document.getElementById( "cat_id0" ) != null ) {
		if ( document.getElementById( "cat_id0" ).checked ) {
			sCat = "_";
		} else {
			for ( var i=1 ; i <= parseInt( document.getElementById( "cat_count" ).value ) ; i++ )	{
				if ( document.getElementById( "cat_id" + i ).checked ) {
					sCat += document.getElementById( "cat_id" + i ).value + "_";
				}
			}
		}
	}
	if ( sCat != null && sCat != "" ) {
		sUrl += "&cat=" + sCat;
	} else {
		if ( sCats != null && sCats != "{cats}" ) {
			sUrl += "&cat=" + sCats;
		}
	}

	if ( sLayout != null && sLayout != "{layout}" ) {
		sUrl += "&layout=" + sLayout;
	}
	if ( sOrder != null && sOrder != "{order}" ) {
		sUrl += "&order=" + sOrder;
	}
	if ( document.getElementById( "price1" ) != null ) {
		sUrl += "&price1=" + document.getElementById( "price1" ).options[ document.getElementById( "price1" ).selectedIndex ].text + "&price2=" + document.getElementById( "price2" ).options[ document.getElementById( "price2" ).selectedIndex ].text;
	}

	var qs = new Querystring();
	var fn = ( qs.get("fn") == null ? "form" : qs.get("fn") );
	if ( fn != "choice" && fn != "morehot" ) {
		fn = "form";
	}
	location.href = "index.php?fn=" + fn + sUrl;
}

// 聯絡我們
function check_contact()
{
	if ( ! check_required( 'account', '會員帳號 (非會員請填姓名)' )     ) return false;
	if ( ! check_required( 'code', '圖片內的驗證碼' ) ) return false;
	if ( ! check_numeric( 'code', '驗證碼' )     ) return false;
	if ( ! check_required( 'mobile', '手機或聯絡電話' )     ) return false;
	if ( ! check_numeric2( 'mobile', '手機或聯絡電話' )     ) return false;
	if ( ! check_required( 'email', '電子郵件地址' )     ) return false;
	if ( ! check_email( 'email' ) ) return false;
	if ( ! check_required( 'content', '您的意見' )   ) return false;
	return true;
}

// 網友留言
function check_commend()
{
	if ( ! check_required( 'content', '您的留言' )   ) return false;
	return true;
}

//後台餐廳管理
function check_data1_new()
{
	if ( ! check_member_account2() ) {
		return false;
	}
	if ( ! check_required( 'name', '店名' )   ) return false;
	if ( ! check_required( 'spec1', '地址' )   ) return false;
	if ( ! check_required( 'spec2', '電話' )   ) return false;
	if ( ! check_required( 'price1', '價位' )   ) return false;
	if ( ! check_numeric( 'price1', '價位' )   ) return false;
	if ( ! check_required( 'price2', '價位' )   ) return false;
	if ( ! check_numeric( 'price2', '價位' )   ) return false;
	if ( ! check_required( 'intro1', '簡短介紹' )   ) return false;
	return true;
}

// 忘記密碼表單
function check_forget()
{
	if ( ! check_required( 'uid2', '帳號' )   ) return false;
	return true;
}

// 加入會員表單
function check_join_form( fm )
{
	if ( ! check_required( 'account', '帳號' )   ) return false;
	if ( ! check_member_account() ) {
		return false;
	}
	if ( ! check_required( 'password1', '密碼' )   ) return false;
	if ( ! check_required( 'password2', '密碼確認' )   ) return false;
	if ( fm.password1.value != fm.password2.value ) {
		alert( "密碼與密碼確認須一致!" );
		fm.password1.value = "";
		fm.password2.value = "";
		fm.password1.focus();
		return false;
	}
	if ( fm.password1.value.length < 6 ) {
		alert( "密碼須為 6 ~ 12 個英文及數字組合!" );
		fm.password1.value = "";
		fm.password2.value = "";
		fm.password1.focus();
		return false;
	}
	if ( ! check_required( 'nick_name', '暱稱' )   ) return false;
	if ( ! check_required( 'real_name', '姓名' )   ) return false;
	//if ( ! check_required( 'tel', '手機或聯絡電話' )   ) return false;
	if ( document.getElementById( "tel" ).value != "" ) {
		if ( ! check_numeric2( 'tel', '手機或聯絡電話' )     ) return false;
	}
	if ( ! check_required( 'email', '電子郵件地址' )   ) return false;
	if ( ! check_email( 'email' ) ) return false;
	if ( document.getElementById( "city" ).value == "%" ) {
		alert( "請選擇縣市!" );
		return false;
	}
	return true;
}

// 修改會員資料
function check_modify_member( fm )
{
	if ( fm.password1.value.length > 0 && ( fm.password1.value != fm.password2.value ) ) {
		alert( "會員密碼與密碼確認須一致!" );
		fm.password1.value = "";
		fm.password2.value = "";
		fm.password1.focus();
		return false;
	}
	if ( fm.password1.value.length > 0 && fm.password1.value.length < 6 ) {
		alert( "會員密碼須為 6 ~ 12 個英文及數字組合!" );
		fm.password1.value = "";
		fm.password2.value = "";
		fm.password1.focus();
		return false;
	}
	if ( ! check_required( 'nick_name', '暱稱' )   ) return false;
	if ( ! check_required( 'real_name', '姓名' )   ) return false;
	//if ( ! check_required( 'tel', '手機或聯絡電話' )   ) return false;
	if ( document.getElementById( "tel" ).value != "" ) {
		if ( ! check_numeric2( 'tel', '手機或聯絡電話' )     ) return false;
	}
	if ( ! check_required( 'email', '電子郵件地址' )   ) return false;
	if ( ! check_email( 'email' ) ) return false;
	if ( document.getElementById( "city" ).value == "%" ) {
		alert( "請選擇縣市!" );
		return false;
	}
	return true;
}

// 會員登入表單
function check_member_form()
{
	if ( ! check_required( 'uid', '帳號' )   ) return false;
	if ( ! check_required( 'pwd', '密碼' ) ) return false;
	return true;
}

// 寄給朋友
function check_send2()
{
	if ( ! check_required( 'email1', '朋友的email' )     ) return false;
	var sEmail = document.getElementById( "email1" ).value.split( "," );
	if ( sEmail.length > 5 ) {
		alert( "朋友的 email 最多只能填寫 5 個喔，請刪除幾個!" );
		return false;
	}
	for ( var i=0 ; i < sEmail.length ; i++ )
	{
		if ( ! check_email2( sEmail[ i ] ) ) return false;
	}
	if ( ! check_required( 'email0', '你的email' )     ) return false;
	if ( ! check_email( 'email0' ) ) return false;
	if ( ! check_required( 'name0', '你的姓名' )   ) return false;
	if ( ! check_required( 'content', '給朋友的留言' )   ) return false;
	return true;
}

// FAQ
function go_faq()
{
	if ( ! check_required( 'keyword', '關鍵字' )   ) return false;
	location.href = "index.php?fn=faq&keyword=" + encodeURI( document.getElementById( "keyword" ).value );
}

// 餐廳評比
var stars_array = new Array();
stars_array[ 1 ] = 0;
stars_array[ 2 ] = 0;
stars_array[ 3 ] = 0;
stars_array[ 4 ] = 0;

function take_star( no, star )
{
	for ( var i=1 ; i <= 10 ; i++ )
	{
		if ( i % 2 == 0 ) {
			document.getElementById( "star" + no + "_" + i ).src = ( i <= star ) ? "images/staron_02.gif" : "images/staroff_02.gif";
		} else {
			document.getElementById( "star" + no + "_" + i ).src = ( i <= star ) ? "images/staron_01.gif" : "images/staroff_01.gif";
		}
	}
	stars_array[ no ] = star;
}

function go_appraise( no )
{
	for ( var i=1 ; i <= 4 ; i++ )
	{
		if( stars_array[ i ] == 0 ) {
			alert( "您還有項目沒有評比呢!" );
			return false;			
		}
	}
	location.href = "index.php?fn=appraise_ok&no=" + no + "&stars=" + stars_array[ 1 ] + "_" + stars_array[ 2 ] + "_" + stars_array[ 3 ] + "_" + stars_array[ 4 ]; 
}
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


/*-------------------------------------------------------------------------------------------------------------------
 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 == undefined || 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;
	}
	if(objField.value.length < 8)
	{
		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=check_member&rand=" + Math.round( ( Math.random() * 90000 )+1 ) + "&account=" + document.getElementById("account").value;
	ccioo_ajax_xmlHttp = ccioo_ajax_createHttp();
	ccioo_ajax_xmlHttp.open( "GET", url, false ); // 不使用非同步, 直接等待取回
    ccioo_ajax_xmlHttp.send( null );
	var objTag = ccioo_ajax_xmlHttp.responseXML.getElementsByTagName("item");
	var sReturn = objTag[0].firstChild.nodeValue;
	if ( parseInt( sReturn ) > 0 ) {
		alert( "此帳號已經有人使用，請您換一個帳號!" );
		document.getElementById("account").value = "";
		return false;
	}
	return true;
}

var oldAccount = "";
function check_member_account2()
{
	if ( document.frm.account.value != oldAccount  ) {
		var url = "/index.php?fn=check_member2&rand=" + Math.round( ( Math.random() * 90000 )+1 ) + "&account=" + document.frm.account.value;
		ccioo_ajax_xmlHttp = ccioo_ajax_createHttp();
		ccioo_ajax_xmlHttp.open( "GET", url, false ); // 不使用非同步, 直接等待取回
		ccioo_ajax_xmlHttp.send( null );
		var objTag = ccioo_ajax_xmlHttp.responseXML.getElementsByTagName("item");
		var sReturn = objTag[0].firstChild.nodeValue;
		if ( parseInt( sReturn ) > 0 ) {
			alert( "此帳號已經有餐廳管理者使用，請您換一個帳號!" );
			document.frm.account.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 = "";
	if ( qs.get("keyword") != null ) {
		tail += "&keyword=" + encodeURI( document.getElementById('keyword').value );
	}
	if ( qs.get("city") != null ) {
		tail += "&city=" + encodeURI( document.getElementById('city').value );
	}
	if ( qs.get("cityarea") != null ) {
		tail += "&cityarea=" + encodeURI( document.getElementById('cityarea').value );
	}
	if ( qs.get("price1") != null ) {
		tail += "&price1=" + qs.get("price1");
	}
	if ( qs.get("price2") != null ) {
		tail += "&price2=" + qs.get("price2");
	}
	if ( qs.get("layout") != null ) {
		tail += "&layout=" + qs.get("layout");
	}
	if ( qs.get("order") != null ) {
		tail += "&order=" + qs.get("order");
	}
	if ( qs.get("cat") != null ) {
		tail += "&cat=" + qs.get("cat");
	}
	location.href = "index.php?fn=" + qs.get("fn") + "&no=" + qs.get("no") + "&p=" + p + tail;
}

/*-------------------------------------------------------------------------------------------------------------------
 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: go_search()
 Description: 搜尋
 Input: N/A
 Output: N/A
 Example: 
	go_search();
Revision History:
   1.0: original version 2007/1/10
-------------------------------------------------------------------------------------------------------------------*/
function go_search()
{
	var words = document.getElementById('keyword');
	if ( words.value == "" || words.value == "產品關鍵字" ) {
		alert( "請輸入產品關鍵字!" );
		words.focus();
		return false;
	}
	location.href = "index.php?fn=search_list&keyword=" + encodeURI( words.value ) + "&range=" + document.getElementById('range').selectedIndex;
}

/*-------------------------------------------------------------------------------------------------------------------
 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);
}

function check_required_column(objID, sMessage){
    var objField = document.getElementById( objID );
    if ( objField == undefined || objField.value.replace( /\s/g, "" ).length == 0 ) {
        alert(sMessage);
        objField.focus();
        return false;
    }
    return true;	
}

function check_email_column(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(sMessage);
        objField.select();
        objField.focus();
        return false;
    }
    return true;
}
