/*-------------------------------------------------------------------------------------------------------------------
 Script Name: functions.js
 Author: Tony Wei (魏志國)  tonywei123@gmail.com
 Description: JavaScript 共用函式庫
 Revision History:
   1.0: original version 2008/5/3
-------------------------------------------------------------------------------------------------------------------*/

// 防複製
var qs = new Querystring();
if ( qs.get("fn") != null && qs.get("fn") == "link" ) {
	// 可複製
} else {
	document.oncontextmenu = function document_oncontextmenu()
	{
		return false;
	}
	document.onselectstart = function document_onselectstart()
	{
		return false;
	}
}



// 新增收件人
function add_receiver()
{
	if ( check_cartlist1() ) {
		var bUpdate = true;
		if ( document.getElementById( "OD_Name" ).value3 != "{MR_Name3}" ) {
			if ( ! confirm( "最多只能儲存三組收件人資料，您確定要取代掉第三組設定嗎?" ) ) {
				bUpdate = false;
			}
		}
		if ( bUpdate ) {
			var para1 = "&MR_Name="			+ encodeURI( document.getElementById( "OD_Name" ).value );
			var para2 = "&MR_Tel="					+ encodeURI( document.getElementById( "OD_Tel" ).value );
			var para3 = "&MR_PostZone="		+ encodeURI( document.getElementById( "OD_PostZone" ).value );
			var para4 = "&MR_Address="			+ encodeURI( document.getElementById( "OD_Address" ).value );
			var url = "index.php?fn=receiver_update" + para1 + para2 + para3 + para4;
			ccioo_ajax_setData( url );
			alert( "新增完成!" );
		}
	}
}

// 更改發票類型
function change_invoice( iSelectedIndex )
{
	switch ( iSelectedIndex )
	{
		case 0:
			document.getElementById( "OD_Title" ).value						= "";
			document.getElementById( "OD_Number" ).value				= "";
			document.getElementById( "OD_Title" ).style.display			= "none";
			document.getElementById( "OD_Number" ).style.display	= "none";
			break;

		case 1:
			document.getElementById( "OD_Title" ).value						= document.getElementById( "OD_Name" ).value0;
			document.getElementById( "OD_Number" ).value				= "";
			document.getElementById( "OD_Title" ).style.display			= "";
			document.getElementById( "OD_Number" ).style.display	= "none";
			break;

		case 2:
			document.getElementById( "OD_Title" ).value						= "";
			document.getElementById( "OD_Number" ).value				= "";
			document.getElementById( "OD_Title" ).style.display			= "";
			document.getElementById( "OD_Number" ).style.display	= "";
			break;
	}
}

// 更新圖案
function change_number()
{
	var url = "index.php?fn=login";
	ccioo_ajax_setData( url );
	document.getElementById( "img_code" ).src = "ccioo/Image/random_image.php?rand=" + Math.random();
}

// 確認收件人資料
function check_cartlist1()
{
	if ( ! check_required( 'OD_Name', '姓名' )   ) return false;
	if ( ! check_required( 'OD_Tel', '電話' )   ) return false;
	if ( ! check_numeric2( 'OD_Tel', '電話' )     ) return false;
	if ( ! check_required( 'OD_PostZone', '郵遞區號' )   ) return false;
	if ( ! check_numeric( 'OD_PostZone', '郵遞區號' )     ) return false;
	if ( ! check_required( 'OD_Address', '地址' )   ) return false;
	switch ( document.getElementById( "OD_Invoice" ).selectedIndex )
	{
		case 0:
			document.getElementById( "OD_Title" ).value			= "";
			document.getElementById( "OD_Number" ).value	= "";
			break;

		case 1:
			if ( document.getElementById( "OD_Title" ).value == "" ) {
				document.getElementById( "OD_Title" ).value = document.getElementById( "OD_Name" ).value0;
			}
			break;

		case 2:
			if ( ! check_required( 'OD_Title', '發票抬頭' )   ) return false;
			if ( ! check_required( 'OD_Number', '統一編號' )   ) return false;
			if ( ! check_numeric( 'OD_Number', '統一編號' )     ) return false;
			if ( document.getElementById( "OD_Number" ).value.length != 8 ) {
				alert( "統一編號必須有 8 碼!" );
				document.getElementById( "OD_Number" ).focus();
				return false;
			}
			break;
	}
	// 儲存 cookie
	setCookie( "OD_Invoice",		document.getElementById( "OD_Invoice" ).selectedIndex, 0.01 );
	setCookie( "OD_Name",		document.getElementById( "OD_Name" ).value, 0.01 );
	setCookie( "OD_Tel",				document.getElementById( "OD_Tel" ).value, 0.01 );
	setCookie( "OD_PostZone", document.getElementById( "OD_PostZone" ).value, 0.01 );
	setCookie( "OD_Address",	document.getElementById( "OD_Address" ).value, 0.01 );
	setCookie( "OD_Title",			document.getElementById( "OD_Title" ).value, 0.01 );
	setCookie( "OD_Number",	document.getElementById( "OD_Number" ).value, 0.01 );
	setCookie( "OD_Memo",		document.getElementById( "OD_Memo" ).value, 0.01 );

	for ( var i=1 ; i <= 4  ; i++ )
	{
		if ( document.getElementById( "OD_Payment" + i ).checked ) {
			setCookie( "OD_Payment",	i, 0.01 );
		}
	}
	return true;
}

// 預先檢查驗證碼
function check_code()
{
	var url = "index.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_login()
{
	if ( ! check_required( 'MI_Email1', 'E-mail' ) ) return false;
	if ( ! check_email( 'MI_Email1' ) ) return false;
	if ( ! check_required( 'MI_Password1', '密碼' ) ) return false;
	if ( ! check_required( 'code', '下圖文字' )   ) return false;
	if ( ! check_numeric2( 'code', '下圖文字' )     ) return false;
	if ( ! check_code() ) return false;
	return true;
}

// 商品到貨通知服務
function check_mail()
{
	if ( ! check_required( 'PN_Email1', 'E-mail' ) ) return false;
	if ( ! check_email( 'PN_Email1' ) ) return false;
	if ( ! check_required( 'PN_Email2', 'E-mail' ) ) return false;
	if ( document.getElementById( "PN_Email1" ).value != document.getElementById( "PN_Email2" ).value ) {
		alert( "兩個欄位請輸入相同的E-mail" );
		return false;
	}
	var url = "index.php?fn=mail_ok&s=" + document.getElementById( "PS_Serial" ).value + "&e=" + document.getElementById( "PN_Email1" ).value;
	ccioo_ajax_setData( url );
	alert( "登記完成!" );
	window.close();
	return false;
}

// 加入會員
function check_member_join( fm )
{

	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 ( ! check_numeric2( 'MI_Tel', '電話' )     ) return false;
	if ( ! check_required( 'MI_Email', 'E-mail' ) ) return false;
	if ( ! check_email( 'MI_Email' ) ) return false;
	if ( ! check_member_account() ) {
		return false;
	}
	if ( ! check_required( 'MI_Password', '密碼' )   ) return false;
	if ( fm.MI_Password.value.length < 6 ) {
		alert( "密碼須為 6~8 個英文及數字組合!" );
		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_Question', '密碼提醒設問' )   ) return false;
	if ( ! check_required( 'MI_Answer', '密碼提醒解答' )   ) return false;
	if ( ! check_required( 'MI_PostZone', '郵遞區號' )   ) return false;
	if ( ! check_numeric( 'MI_PostZone', '郵遞區號' )     ) return false;
	if ( ! check_required( 'MI_Address', '地址' )   ) return false;
	if ( ! ( fm.MI_Epaper1.checked || fm.MI_Epaper2.checked ) ) {
		alert( "請勾選是否訂閱電子報!" );
		fm.MI_Epaper1.focus();
		return false;
	}
	if ( ! fm.agree.checked ) {
		alert( "請確實閱讀服務條款，並勾選同意右項服務條款內容!" );
		fm.agree.focus();
		return false;
	}
	if ( ! check_required( 'code', '下圖文字' )   ) return false;
	if ( ! check_numeric2( 'code', '下圖文字' )     ) return false;
	if ( ! check_code() ) return false;
	return true;
}

// 會員資料修改
function check_member_modify( fm )
{

	if ( ! check_required( 'MI_Name', '中文全名' )   ) return false;
	if ( ! check_required( 'MI_Tel', '電話' )   ) return false;
	if ( ! check_numeric2( 'MI_Tel', '電話' )     ) return false;
	if ( ! check_required( 'MI_Email', 'E-mail' ) ) return false;
	if ( ! check_email( 'MI_Email' ) ) return false;
	if ( document.getElementById( "MI_Email" ).value != document.getElementById( "MI_Email_old" ).value ) {
		if ( ! check_member_account() ) {
			return false;
		}
	}
	if ( fm.MI_Password.value != "" || fm.MI_Password2.value != "" ) {
		if ( ! check_required( 'MI_Password', '密碼' )   ) return false;
		if ( fm.MI_Password.value.length < 6 ) {
			alert( "密碼須為 6~8 個英文及數字組合!" );
			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_Question', '密碼提醒設問' )   ) return false;
	if ( ! check_required( 'MI_Answer', '密碼提醒解答' )   ) return false;
	if ( ! check_required( 'MI_PostZone', '郵遞區號' )   ) return false;
	if ( ! check_numeric( 'MI_PostZone', '郵遞區號' )     ) return false;
	if ( ! check_required( 'MI_Address', '地址' )   ) return false;
	alert( "修改完成!" );
	return true;
}

// 選擇顏色
function choose_color( iPP_Serial, iStartKey )
{
	if ( typeof( PP_Serial[ iPP_Serial ][ 1 ] ) == "undefined" ) {
		alert( "管理者尚未建立這種顏色的商品資料!" );
		return;
	}
	var select1 = "<select onchange='choose_size( " + iPP_Serial + ", this.selectedIndex + 1 );' class='word12-gray' style='width:240px'>";
	for ( key in PP_Serial[ iPP_Serial ] )
	{
		var ss = PP_Serial[ iPP_Serial ][ key ].split( /\^/ );
		select1 += "<option" + ( key == iStartKey ? " selected" : "" ) + ">" + ss[ 1 ];
		switch ( parseInt( ss[ 4 ] ) )
		{
			case 2:
				select1 += "--已停售";
				break;

			case 3:
				select1 += "--貨到請通知我";
				break;
		}
		select1 += "</option>";
	}
	select1 += "</select>";
	document.getElementById( "div_choose_size" ).innerHTML = select1;

	// 先選取第一個尺寸及款式
	choose_size( iPP_Serial, iStartKey );
}

// 選擇尺寸及款式
function choose_size( iPP_Serial, iKey )
{
	var ss = PP_Serial[ iPP_Serial ][ iKey ].split( /\^/ );
	var button1 = "";
	switch ( parseInt( ss[ 4 ] ) )
	{
		case 1:
			button1 = "<a href=\"javascript:buy( " + ss[ 0 ] + ", '" + PD_Name + "' );\"><img src='images/btn1.gif' width='112' height='32' border='0'></a>";
			break;

		case 2:
			button1 = "<img src='images/in2-p8.jpg' width='112' height='34' border='0'>";
			break;

		case 3:
			button1 = "<img src='images/in2-p7.jpg' width='179' height='34' border='0' onClick=\"MM_openBrWindow('"
							 + "index.php?fn=mail&PS_Serial=" + ss[ 0 ] + "&pname=" + encodeURI( PD_Name + " " + ss[ 1 ] ) + "','','width=556,height=274')\" style='cursor:hand'>";
			break;
	}
	document.getElementById( "div_button1" ).innerHTML		= button1;
	document.getElementById( "span_size" ).innerHTML			= PD_Name + " " + ss[ 1 ];
	document.getElementById( "span_number" ).innerHTML	= ss[ 2 ];
	document.getElementById( "span_price" ).innerHTML		= ss[ 3 ];
}

// 忘記密碼
function check_password()
{
	if ( ! check_required( 'MI_Email', '您的原登入E-mail帳號' ) ) return false;
	if ( ! check_email( 'MI_Email' ) ) return false;
	return true;
}

// 密碼提示設問
function check_password1()
{
	if ( ! check_required( 'MI_Answer', '回答' ) ) return false;
	return true;
}

// 收件人資料修改
function check_receiver()
{
	for ( var i=1 ; i <= 3 ; i++ )
	{
		if ( document.getElementById( "MR_Name" + i ).value != "" ) {
			if ( ! check_required( "MR_Tel" + i, '第' + i +'組聯絡電話' ) ) return false;
			if ( ! check_numeric2( "MR_Tel" + i, '聯絡電話' )     ) return false;
			if ( ! check_required( "MR_PostZone" + i, '第' + i +'組郵遞區號' ) ) return false;
			if ( ! check_numeric2( "MR_PostZone" + i, '郵遞區號' )     ) return false;
			if ( ! check_required( "MR_Address" + i, '第' + i +'組地址' ) ) return false;
		}
	}
	alert( "修改完成!" );
	return true;
}

// 商品搜尋
function check_search()
{
	if ( document.getElementById( "keyword" ).value == "商品搜尋" || document.getElementById( "keyword" ).value == "" ) {
		if ( document.getElementById( "price1" ).value == "價格搜尋" || document.getElementById( "price1" ).value == "" ) {
			alert( "請輸入您想要搜尋的關鍵字或價格區間!" );
			return false;
		} else {
			if ( ! check_required( 'price2', '價格上限' )   ) return false;
			if ( ! check_numeric( 'price1', '價格下限' )     ) return false;
			if ( ! check_numeric( 'price2', '價格上限' )     ) return false;
			if ( parseInt( document.getElementById( "price2" ).value ) < parseInt( document.getElementById( "price1" ).value ) ) {
				var sTemp = document.getElementById( "price2" ).value;
				document.getElementById( "price2" ).value = document.getElementById( "price1" ).value;
				document.getElementById( "price1" ).value = sTemp;
			}
		}
	}
	if ( document.getElementById( "keyword" ).value == "商品搜尋" ) {
		document.getElementById( "keyword" ).value = "";
	}
	if ( document.getElementById( "price1" ).value == "價格搜尋" ) {
		document.getElementById( "price1" ).value = "";
	}
	return true;
}

// 購物籃加總
function count_total_freight()
{
	var a = document.getElementById( "prod_id_list" ).value.split( /,/ );
	var total = 0;
	for ( key in a )
	{
		total += parseInt( document.getElementById( "price" + a[ key ] ).value ) * parseInt( document.getElementById( "amount" + a[ key ] ).selectedIndex + 1 );
	}

	var freight = parseInt( document.getElementById( "SC_Freight" ).value );
	if ( total >= parseInt( document.getElementById( "SC_DiscountPrice" ).value ) ) {
		var discount = Math.round( total * parseInt( document.getElementById( "SC_DiscountRate" ).value ) / 100 );
		total = total - discount;
		document.getElementById( "discount" ).innerHTML = number_format( discount, 0, '.', ',' );
		document.getElementById( "tr_discount" ).style.display = "";
	} else {
		document.getElementById( "tr_discount" ).style.display = "none";
	}
	if ( total == 0 || total >= parseInt( document.getElementById( "SC_FreightFree" ).value ) ) {
		freight = 0;
	}

	document.getElementById( "freight" ).innerHTML = freight;
	document.getElementById( "total_freight" ).innerHTML = number_format( total + freight, 0, '.', ',' );
}

// 收件人資料刪除
function del_receiver( MR_Serial )
{
	if ( confirm( "確認刪除?" ) ) {
		location.href = "index.php?fn=receiver_del&no=" + MR_Serial;
	}
}

// 更改購物清單內某商品之訂購數量
function change_unit( iPS_Serial, iSelectedIndex )
{
	var subtotal = ( iSelectedIndex + 1 )  * parseInt( document.getElementById( 'price' + iPS_Serial ).value );
	document.getElementById( 'subtotal' + iPS_Serial ).innerHTML = number_format( subtotal, 0, '.', ',' );
	count_total_freight();
	update_item( iPS_Serial, iSelectedIndex + 1 );
}

// 登出
function member_logout()
{
	delCookie( "OD_Invoice" );
	delCookie( "OD_Name" );
	delCookie( "OD_Tel" );
	delCookie( "OD_PostZone" ); 
	delCookie( "OD_Address" );
	delCookie( "OD_Title" );
	delCookie( "OD_Number" );
	delCookie( "OD_Memo" );
	location.href = "index.php?fn=member_logout";
}

// 更新購物清單內的某項目
function update_item( iPS_Serial, iUnit )
{
	call_cart( "update",		iPS_Serial + ":" + iUnit		);
}

// 刪除購物清單內的某項目
function remove_item( iPS_Serial )
{
	call_cart( "remove", iPS_Serial + ":9999" );
	location.reload();
}

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

/*-------------------------------------------------------------------------------------------------------------------
 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: buy()
 Description: 放入購物車
 Input: 商品ID, 商品名稱
 Output: N/A
 Example: 
	buy( 1, '棒球手套' );
Revision History:
   1.0: original version 2007/1/7
-------------------------------------------------------------------------------------------------------------------*/
function buy( product_ID, prod_name )
{
	call_cart( "add", product_ID + ":1" );
	alert("已將 " + prod_name + " 放入購物車");
}

/*-------------------------------------------------------------------------------------------------------------------
 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_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( "此帳號已經有人使用，請您換一個帳號!" );
		document.getElementById("MI_Email").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 ) {
		if ( sKeyword != "{keyword}" ) {
			tail += "&keyword=" + encodeURI( sKeyword );
		}
	}
	if ( qs.get("price1") != null ) {
		tail += "&price1=" + qs.get("price1");
	}
	if ( qs.get("price2") != null ) {
		tail += "&price2=" + qs.get("price2");
	}
	if ( qs.get("level") != null ) {
		tail += "&level=" + qs.get("level");
	}
	if ( qs.get("cno") != null ) {
		tail += "&cno=" + qs.get("cno");
	}
	if ( qs.get("pno") != null ) {
		tail += "&pno=" + qs.get("pno");
	}
	if ( qs.get("ppno") != null ) {
		tail += "&ppno=" + qs.get("ppno");
	}
	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: 
	thesame_click( this, number );
Revision History:
   1.0: original version 2007/1/11
   2.0: 進化型，加入收件人資料的編號參數 2008/5/6
-------------------------------------------------------------------------------------------------------------------*/
function thesame_click( objSelf, number )
{
	var fm = objSelf.form;
	for ( var i=0 ; i < fm.elements.length ; i++ )	{
		if ( eval( "typeof( fm.elements[i].value" + number + " ) != 'undefined'" ) ) {
			if ( fm.elements[i].type == "text" ) {
				eval( "fm.elements[i].value = fm.elements[i].value" + number );
			}
			if ( fm.elements[i].type == "select-one" ) {
				eval( "fm.elements[i].selectedIndex = fm.elements[i].value" + number );
			}
		}
	}
}

/*-------------------------------------------------------------------------------------------------------------------
 PHP-like Functions
 http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/
-------------------------------------------------------------------------------------------------------------------*/
function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "," : dec_point;
    var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

/*-------------------------------------------------------------------------------------------------------------------
 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);
}
