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


// 忘記密碼表單
function check_forgotpwd()
{
	if ( ! check_required( 'email', '帳號' )   ) return false;
	if ( ! check_email( 'email' ) ) return false;
	return true;
}
function check_forgotpwd02()
{
	if ( ! check_required( 'answer', '您的答案' )   ) return false;
	return true;
}

// 轉寄好友
function check_forwards()
{
	if ( ! check_required( 'receiver', '收件者的姓名' )   ) return false;
	if ( ! check_required( 'receiver_email', '收件者的E-mail' )   ) return false;
	if ( ! check_email( 'receiver_email' ) ) return false;
	if ( ! check_required( 'sender', '我的姓名' )   ) return false;
	if ( ! check_required( 'sender_email', '我的E-mail' )   ) return false;
	if ( ! check_email( 'sender_email' ) ) return false;
	return true;
}

// 轉寄 E-coupon
function check_forward_ecoupon()
{
	if ( ! check_required( 'receiver1', '朋友的信箱1' )   ) return false;
	if ( ! check_email( 'receiver1' ) ) return false;
	return true;
}

// 訂單查詢
function check_login_order_ser()
{
	if ( ! check_required( 'sn', '訂單編號' )   ) return false;
	if ( ! check_required( 'account', '會員帳號' )   ) return false;
	return true;
}

// 購物流程 2 -- 第一次購物
function car_step2_first( fm )
{
	if ( ! check_required( 'real_name', '姓名' )   ) return false;
	if ( ! ( fm.gender1.checked || fm.gender2.checked ) ) {
		alert( "請勾選您的性別!" );
		fm.gender1.focus();
		return false;
	}
	if ( fm.city1.selectedIndex == 0 ) {
		alert( "請選擇您的居住區域!" );
		fm.city1.focus();
		return false;
	}
	if ( ! check_required( 'zip1', '郵遞區號' )   ) return false;
	if ( ! check_numeric( 'zip1', '郵遞區號' )   ) return false;
	if ( ! check_required( 'address1', '通訊地址' )   ) return false;
	if ( ! check_required( 'tel1', '聯絡電話' )   ) return false;
	if ( ! check_numeric2( 'tel1', '聯絡電話' )     ) return false;
	if ( ! check_required( 'email1', 'E-mail' )   ) return false;
	if ( ! check_email( 'email1' ) ) return false;
	if ( ! car_step2_mem( fm ) ) return false;
	return true;
}

// 購物流程 2 -- 會員已登入
function car_step2_mem( fm )
{
	if ( ! check_required( 'receiver', '收件者姓名' )   ) return false;
	if ( ! check_required( 'tel', '聯絡電話' )   ) return false;
	if ( ! check_numeric2( 'tel', '聯絡電話' )     ) return false;
	if ( ! check_required( 'PostNo', '郵遞區號' )   ) return false;
	if ( ! check_numeric( 'PostNo', '郵遞區號' )   ) return false;
	if ( ! check_required( 'address', '收件地址' )   ) return false;
	if( fm.invoice[1].checked ) { // 三聯式發票
		if ( ! check_required( 'inv_no', '三聯式發票的統一編號' )   ) return false;
		if ( ! check_numeric( 'inv_no', '統一編號' )   ) return false;
		if ( ! check_required( 'inv_title2', '三聯式發票的發票抬頭' )   ) return false;
	}
	return true;
}

// 購物流程 3
function car_step3( fm )
{
	if ( fm.payment.value == "1" || fm.payment.value == "3" || fm.payment.value == "5" ) {
		fm.target = "_blank";
	}
	return true;
}

// 聯絡我們
function check_contact()
{
	if ( ! check_required( 'subject', '主旨' )     ) return false;
	if ( ! check_required( 'cname', '姓名' )     ) return false;
	//if ( ! check_required( 'tel', '電話' )     ) return false;
	//if ( ! check_required( 'address', '地址' )     ) return false;
	if ( ! check_required( 'email', 'E-mail' )     ) return false;
	if ( ! check_email( 'email' ) ) return false;
	if ( ! check_required( 'content1', '內容' )   ) return false;
	if ( ! check_required( 'code', '圖片內的驗證碼' ) ) return false;
	return true;
}

// 創作流程一
function check_create_step1()
{
	if ( ! check_required( 'file1', '您要上傳的檔案' )   ) return false;
	return true;
}

// 電子賀卡流程二
function check_ecard_step2()
{
	if ( ! check_required( 'sender', '寄件人姓名' )     ) return false;
	if ( ! check_required( 'sender_email', '寄件人email' )     ) return false;
	if ( ! check_email( 'sender_email' ) ) return false;
	if ( ! check_required( 'subject', '領卡通知主旨' )     ) return false;
	if ( ! check_required( 'message', '內容' )     ) return false;
	if ( ! check_required( 'receiver[]', '收件人1' )     ) return false;
	if ( ! check_required( 'receiver_email[]', '收件人email1' )     ) return false;
	if ( ! check_email( 'receiver_email[]' ) ) return false;
	return true;
}

// 我要評分
function check_grade( fm )
{
	if ( ! ( fm.star1.checked || fm.star2.checked || fm.star3.checked || fm.star4.checked || fm.star5.checked ) ) {
		alert( "請勾選您要給幾顆星!" );
		return false;
	}
	return true;
}

// 加入會員表單1
function check_join_form1( fm )
{
	if ( ! check_required( 'email1', 'Email (即會員ID)' )   ) return false;
	if ( ! check_required( 'email2', '再次確認 Email ' )   ) return false;
	if ( fm.email1.value != fm.email2.value ) {
		alert( "Email 與確認 Email 須一致!" );
		fm.email1.focus();
		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 < 4 ) {
		alert( "會員密碼須為 4 ~ 12 個英文及數字組合!" );
		fm.password1.value = "";
		fm.password2.value = "";
		fm.password1.focus();
		return false;
	}
	if ( ! check_email( 'email1' ) ) return false;
	if ( ! check_required( 'answer', '答案' )   ) return false;
	if ( ! fm.accept.checked ) {
		alert( "請確實閱讀,並勾選「我已詳細閱讀完畢 inni 服務條款」!" );
		return false;
	}
	return true;
}

// 加入會員表單2
function check_join_form2( fm )
{
	if ( ! check_required( 'real_name', '姓名' )   ) return false;
	if ( ! check_required( 'nickname', '暱稱' )   ) return false;
	if ( ! ( fm.gender1.checked || fm.gender2.checked ) ) {
		alert( "請勾選您的性別!" );
		fm.gender1.focus();
		return false;
	}
	if ( ! check_required( 'pid', '身分證字號' )   ) return false;
	if ( fm.country1.checked && ( ! check_pid( 'pid' ) ) ) return false;
	/*
	if ( fm.education.selectedIndex == 0 ) {
		alert( "請選擇您的學歷!" );
		fm.education.focus();
		return false;
	}
	if ( fm.city.selectedIndex == 0 ) {
		alert( "請選擇您的居住區域!" );
		fm.city.focus();
		return false;
	}
	if ( ! check_required( 'zip', '郵遞區號' )   ) return false;
	if ( ! check_numeric( 'zip', '郵遞區號' )   ) return false;
	if ( ! check_required( 'address', '通訊地址' )   ) return false;
	if ( ! check_required( 'tel', '通訊電話' )   ) return false;
	if ( ! check_required( 'mobile', '行動電話' )   ) return false;
	*/
	/* 選填
	if ( fm.occupation.selectedIndex == 0 ) {
		alert( "請選擇您的職業!" );
		fm.occupation.focus();
		return false;
	}
	if ( fm.jobtitle.selectedIndex == 0 ) {
		alert( "請選擇您的職稱!" );
		fm.jobtitle.focus();
		return false;
	}
	if ( fm.income.selectedIndex == 0 ) {
		alert( "請選擇您的月收入!" );
		fm.income.focus();
		return false;
	}
	if ( ! ( fm.marriage1.checked || fm.marriage2.checked ) ) {
		alert( "請勾選您的婚姻狀態!" );
		fm.marriage1.focus();
		return false;
	}
	if ( ! ( fm.children1.checked || fm.children2.checked ) ) {
		alert( "請勾選有無子女!" );
		fm.children1.focus();
		return false;
	}
	*/
	if ( ! check_required( 'code', '圖片內的驗證碼' ) ) return false;
	return true;
}

// 修改會員資料
function check_modify_member( fm )
{
	if ( ! check_required( 'nickname', '暱稱' )   ) return false;
	/*
	if ( fm.education.selectedIndex == 0 ) {
		alert( "請選擇您的學歷!" );
		fm.education.focus();
		return false;
	}
	if ( fm.city.selectedIndex == 0 ) {
		alert( "請選擇您的居住區域!" );
		fm.city.focus();
		return false;
	}
	if ( ! check_required( 'zip', '郵遞區號' )   ) return false;
	if ( ! check_numeric( 'zip', '郵遞區號' )   ) return false;
	if ( ! check_required( 'address', '通訊地址' )   ) return false;
	if ( ! check_required( 'tel', '聯絡電話' )   ) return false;
	if ( ! check_required( 'mobile', '行動電話' )   ) return false;
	*/
	/* 選填
	if ( fm.occupation.selectedIndex == 0 ) {
		alert( "請選擇您的職業!" );
		fm.occupation.focus();
		return false;
	}
	if ( fm.jobtitle.selectedIndex == 0 ) {
		alert( "請選擇您的職稱!" );
		fm.jobtitle.focus();
		return false;
	}
	if ( fm.income.selectedIndex == 0 ) {
		alert( "請選擇您的月收入!" );
		fm.income.focus();
		return false;
	}
	if ( ! ( fm.marriage1.checked || fm.marriage2.checked ) ) {
		alert( "請勾選您的婚姻狀態!" );
		fm.marriage1.focus();
		return false;
	}
	if ( ! ( fm.children1.checked || fm.children2.checked ) ) {
		alert( "請勾選有無子女!" );
		fm.children1.focus();
		return false;
	}
	if ( ! check_required( 'intro', '自我介紹' )   ) return false;
	return true;
	*/
	
}

function check_modify_member2( fm )
{	
	
	if ( ! check_required( 'intro', '自我介紹' ) ) return false;
	return true;
}

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

// 會員創作專區 -- 留言板
function check_member_visitor_message()
{
	if ( ! check_required( 'nickname', '暱稱' )   ) return false;
	if ( ! check_required( 'content2', '訊息' ) ) return false;
	if ( ! check_required( 'code', '圖片內的驗證碼' ) ) return false;
	return true;
}

// 創作品管理
function check_member_myinni_cre_price_manage()
{
	if ( ! check_required( 'prod_name', '名稱' )   ) return false;
	if ( ! check_required( 'price', '自訂價' )   ) return false;
	if ( ! check_numeric( 'price', '自訂價' )   ) return false;
	if ( ! check_range( 'price', document.getElementById( "suggest_price" ).value, 10000, '自訂價' )   ) return false;
	if ( ! check_required( 'content2', '描述' ) ) return false;
	var menu = document.getElementById( "cat_select" );
	if ( menu.options[ menu.selectedIndex ].value == 0 ) {
		alert( "請選擇分類!" );
		menu.focus();
		return false;
	}
	return true;
}

// 圖像管理
function check_member_myinni_image_price_manage()
{
	if ( ! check_required( 'prod_name', '名稱' )   ) return false;
	if ( ! check_required( 'content2', '描述' ) ) return false;
	return true;
}

// 修改會員資料
function check_member_myinni_vip1( fm )
{
	if ( ! check_required( 'nickname', '暱稱' )   ) return false;
	if ( fm.education.selectedIndex == 0 ) {
		alert( "請選擇您的學歷!" );
		fm.education.focus();
		return false;
	}
	if ( fm.city.selectedIndex == 0 ) {
		alert( "請選擇您的居住區域!" );
		fm.city.focus();
		return false;
	}
	if ( ! check_required( 'zip', '郵遞區號' )   ) return false;
	if ( ! check_numeric( 'zip', '郵遞區號' )   ) return false;
	if ( ! check_required( 'address', '通訊地址' )   ) return false;
	if ( ! check_required( 'tel', '聯絡電話' )   ) return false;
	if ( ! check_numeric2( 'tel', '聯絡電話' )     ) return false;
	if ( ! check_required( 'mobile', '行動電話' )   ) return false;
	if ( ! check_numeric2( 'mobile', '行動電話' )     ) return false;
	return true;
}

// 修改會員密碼
function check_pwd_modify( fm )
{
	if ( ! check_required( 'password1', '舊密碼' )   ) return false;
	if ( fm.password2.value.length < 6 ) {
		alert( "新密碼須為 6 ~ 12 個英文及數字組合!" );
		fm.password2.value = "";
		fm.password3.value = "";
		fm.password2.focus();
		return false;
	}
	if ( ! check_required( 'password2', '新密碼' )   ) return false;
	if ( ! check_required( 'password3', '密碼確認' )   ) return false;
	if ( fm.password2.value != fm.password3.value ) {
		alert( "新密碼與密碼確認須一致!" );
		fm.password2.value = "";
		fm.password3.value = "";
		fm.password2.focus();
		return false;
	}
	return true;
}

// 合作提案
function check_proposal()
{
	if ( ! check_required( 'cname', '姓名' )     ) return false;
	//if ( ! check_required( 'contact1', '聯絡資料' )     ) return false;
	//if ( ! check_required( 'contact2', '方便聯絡的時間' )     ) return false;
	if ( ! check_required( 'contact3', '提案簡介與說明' )     ) return false;
	if ( ! check_required( 'code', '圖片內的驗證碼' ) ) return false;
	return true;
}

// 搜尋表單
function check_search_form()
{
	if ( ! check_required( 'keyword', '關鍵字' )   ) return false;
	objField = document.getElementById( "keyword" );
	if ( objField.value == "關鍵字" ) {
		alert( "請輸入關鍵字!" );
		objField.value = "";
		objField.focus();
		return false;
	}
	return true;
}

// 購物清單表單
var info_body  = new Array();
var info_count = new Array();
function addInfo( pid )
{
	info_count[ pid ]++;
	document.getElementById( "div_" + pid ).innerHTML += info_body[ pid ].replace( /\_x/g, "_" + info_count[ pid ] );
}

function check_shopping_list_form( fm )
{
	var radio = new Array();
	for ( var i=0 ; i < fm.elements.length ; i++ )	{
		var em = fm.elements[i];
		if ( em.type == "text" ) { 
			if ( em.name.substr( 0, 1 ) == "p" ) {
				if ( ! check_numeric( em.name, '數量' )   ) return false;
				if ( ! check_range( em.name, 0, 100, '數量' )   ) return false;
				
				// 產品型號
				var sum = 0;
				var hasInfo = false;
				for ( var j=1 ; j <11 ; j++ )
				{
					var objName = "infov_" + em.name.substr( 1 ) + "_" + j;
					var objAmount = document.getElementById( objName );
					if ( objAmount ) {
						hasInfo = true;
						sum += parseInt( objAmount.value );
						
					}
				}
				if ( hasInfo && sum != parseInt( document.getElementById( "p" + em.name.substr( 1 ) ).value ) ) {
					alert( "左邊的商品數量加起來應該與右邊填寫的數量相等!" );
					em.select();
					return false;
				}
			}
		}
	}
	return true;
}

// 索取發票
function get_infoice( func )
{
	if ( func == "1" ) {
		document.getElementById( "tr1" ).style.display = 'none';
		document.getElementById( "tr2" ).style.display = 'none';
		document.getElementById( "tr3" ).style.display = 'none';
		document.getElementById( "tr4" ).style.display = 'none';
	} else {
		document.getElementById( "tr1" ).style.display = '';
		document.getElementById( "tr2" ).style.display = '';
		document.getElementById( "tr3" ).style.display = '';
		document.getElementById( "tr4" ).style.display = '';
	}
} 

// 刪除購物清單內的某項目
function remove_item( number )
{
	var objField = document.getElementById( "p" + number );
	objField.value = "0";
	objField.form.submit();
}

// 電子賀卡 -- 選擇文字顏色
function choose_color( objSelf )
{
	if ( objSelf.parentElement.bgColor ) {
		document.getElementById( "color" ).value              = objSelf.parentElement.bgColor;
		document.getElementById( "message" ).style.color = objSelf.parentElement.bgColor;
	}
}

// 確認是否刪除
function confirm_delete( did )
{
	if ( confirm("確定刪除?") ) {
		location.href = "/member_myinni_response_delete?did=" + did;
	}
}

function confirm_delete2( did )
{
	if ( confirm("確定刪除?") ) {
		location.href = location.pathname + "?did=" + did;
	}
}

// 變更圖像或創作品的開啟狀態
function confirm_show( sid, bShow )
{
	location.href = location.pathname + "?sid=" + sid + "&show=" + bShow;
}

// 看我能不能收到信?
function send_me()
{
	if ( ! check_required( 'email1', 'Email' )   ) return false;
	if ( ! check_email( 'email1' ) ) return false;
	var sEmail = document.getElementById( "email1" ).value;
	var url = "/send_me?email=" + sEmail;
	ccioo_ajax_setData( url );
	alert( "信件已經寄往 " + sEmail + "，若您在三分鐘內仍未收到，建議您換一個信箱提出申請!" );
}

// 圖像管理 -- 顯示訂價管理
function show_price()
{
	var bShow = document.getElementById( "p_management" ).value;
	if ( bShow == "0" ) {
		document.getElementById( "tr1" ).style.display = '';
		document.getElementById( "tr2" ).style.display = '';
		document.getElementById( "p_management" ).value = "1";
	}
	else {
		document.getElementById( "tr1" ).style.display = 'none';
		document.getElementById( "tr2" ).style.display = 'none';
		document.getElementById( "p_management" ).value = "0";
	}
}

// 後端--新增空白物
function check_product_new()
{
	if ( document.getElementById( "content" ).value == "" ) {
		alert( "請輸入說明!" );
		return false;
	}
	if ( ! check_required( 'sn', '編號' )    ) return false;
	if ( ! check_required( 'prod_name', '名稱' )    ) return false;
	if ( ! check_required( 'cost', '基本價' )    ) return false;
	if ( ! check_required( 'price', '建議價' )    ) return false;
	if ( ! check_range( 'cost', 10, 10000, '基本價' )   ) return false;
	if ( ! check_range( 'price', document.getElementById( "cost" ).value, 10000, '建議價' )   ) return false;
	if ( ! check_required( 'ratio', '真實比例' )    ) return false;
	if ( ! check_required( 'image', '圖片(正面)' )    ) return false;
	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 + " 放入購物車");
	location.href = "car_step1";
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: do_not_buy()
 Description: 從購物車中移除
 Input: 物件本身, 商品ID
 Output: N/A
 Example: 
	do_not_buy( this, 1 );
Revision History:
   1.0: original version 2007/4/28
-------------------------------------------------------------------------------------------------------------------*/
function do_not_buy( objSelf, product_ID )
{
	if ( confirm("確定刪除?") ) {
		call_cart( "remove", product_ID + ":99999" );
		location.reload();
	} else {
		objSelf.checked = false;
	}
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: favorite()
 Description: 加入我的最愛
 Input: 商品ID, 商品名稱
 Output: N/A
 Example: 
	favorite( 1, '棒球手套' );
 Revision History:
   1.0: original version 2007/1/7
-------------------------------------------------------------------------------------------------------------------*/
function favorite( product_ID, prod_name, uid )
{
	if ( uid == "" ) {
		//alert( "請先登入才能使用本功能!" );
		var u = location.href.substring( 8 );
		u = u.substring( u.indexOf( "/" ) + 1 );
		location.href = "login?page=" + encodeURI( u );
		return;
	}
	var url = "favorite_add?pid=" + product_ID;
	ccioo_ajax_xmlHttp = ccioo_ajax_createHttp();
	ccioo_ajax_xmlHttp.open( "HEAD", url, true ); // 送出動作之後就不處理了
    ccioo_ajax_xmlHttp.send( null );
	alert("已將 " + prod_name + " 加入我的最愛");
	return;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: favorite_del()
 Description: 從我的最愛中刪除
 Input: product_ID
 Output: N/A
 Example: 
	del( 23 );
 Revision History:
   1.0: original version 2007/1/7
-------------------------------------------------------------------------------------------------------------------*/
function favorite_del( product_ID )
{
	if ( confirm("確定刪除?") ) {
		var url = "member_myinni_favorite?did=" + product_ID;
		location.href= url ;
	}
	return;
}

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


/*-------------------------------------------------------------------------------------------------------------------
 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 ) ) ) {
		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 )
{
	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_pid()
 Description: 檢查表單欄位是否是合法的身分號字號
 Input: 物件ID, 訊息
 Output: 是否驗證通過 true or false
 Example: 
	check_pid( "pid" );
Revision History:
   1.0: original version 2007/1/8
-------------------------------------------------------------------------------------------------------------------*/
function check_pid( objID )
{
	var objField = document.getElementById( objID );
	var pid = objField.value.toUpperCase();
	if ( pid == "A123456789" ) {
		alert( "請填寫正確的身分號字號!" );
		objField.select();
		objField.focus();
		return false;
	}
	if( /^[A-Z][1-2]\d{8}\d?$/.test(pid) ) {
		pid = pid.substring(0,10);
	}
	else {
		alert( "請填寫正確的身分號字號!" );
		objField.select();
		objField.focus();
		return false;
	}
	var alph = new Array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","X","Y","W","Z","I","O");
	var num = new Array("10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35");
	var n=0;
	for(var i=0;i<alph.length;i++) {
		 if(pid.charAt(0)==alph[i])
			n=i;
	}
	var tot1 = parseFloat(num[n].charAt(0)) + (parseFloat(num[n].charAt(1)) * 9);
	var tot2 = 0;
	for(var i=1;i<pid.length-1;i++)
		tot2 = tot2 + parseFloat(pid.charAt(i))*(9-i);
	var tot3 = parseFloat(pid.charAt(9));
	var tot4 = tot1 + tot2 + tot3;
	if((tot4 % 10)!=0) {
		alert( "請填寫正確的身分號字號!" );
		objField.select();
		objField.focus();
		return false;
	}
	return true;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: change_cat()
 Description: 更改分類次選單
 Input: 主分類 id
 Output: N/A
 Example: 
	change_cat( this.value );
Revision History:
   1.0: original version 2007/3/8
-------------------------------------------------------------------------------------------------------------------*/
function change_cat( cat1_id )
{
	var menu = document.getElementById( "cat_select" );
	menu.options.length = 0;
	var i = 0;
	if ( cat1_id == 0 ) {
			menu.options[0] = new Option ( "次分類選擇", 0 );
	}
	else {
		for ( key in cat1[ cat1_id ] )   {
			menu.options[i] = new Option ( cat1[ cat1_id ][ key ], key );
			i++;
		}
	}
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: change_status()
 Description: 背景處理, 變更圖像或創作品的狀態
 Input: 欄位名稱, Record id, 欄位值
 Output: N/A
 Example: 
	change_status( 'adult', 33, 1 );
 Revision History:
   1.0: original version 2007/2/14
-------------------------------------------------------------------------------------------------------------------*/
function change_status( field_name, record_id, status_value )
{
	var url = "change_status?f=" + field_name + "&rid=" + record_id + "&v=" + status_value;
	ccioo_ajax_xmlHttp = ccioo_ajax_createHttp();
	ccioo_ajax_xmlHttp.open( "HEAD", url, true ); // 送出動作之後就不處理了
    ccioo_ajax_xmlHttp.send( null );
	return;
}
function change_adult( objSelf, record_id )
{
	var status_value = ( objSelf.checked ) ? 1 : 0;
	change_status( 'adult', record_id, status_value );
}
function change_color( record_id, status_value, color_string )
{
	change_status( 'color', record_id, status_value );
	document.getElementById( "td_color" ).bgColor = color_string;
	if ( status_value == 18 ) {
		document.getElementById( "td_color" ).innerHTML = "<img src='images/img_bg.gif' width='35' height='16' border='0'>";
	} else {
		document.getElementById( "td_color" ).innerHTML = "<img src='images/spacer.gif' width='35' height='16' border='0'>";
	}
}
function change_show( record_id, status_value, status_name )
{
	if ( status_value == 1 ) {
		var menu = document.getElementById( "cat_select" );
		
		if ( menu != null ) {
			if ( menu.options[ menu.selectedIndex ].value == 0 ) {
				alert( "開放販售前, 必須先選擇分類!" );
				menu.focus();
				return false;
			}
			
			if ( ! check_required( 'price', '自訂價' )   ) return false;
			if ( ! check_numeric( 'price', '自訂價' )   ) return false;		
			if ( ! check_range( 'price', document.getElementById( "cost" ).value, 10000, '自訂價' )   ) return false;
		}
	}
	document.getElementById( "show_status" ).innerHTML = ( status_value == 1 ) ? "<span class='w6'>開啟</span>" : "<span class='w1'>關閉</span>";
	change_status( 'show', record_id, status_value );
}

/*-------------------------------------------------------------------------------------------------------------------
 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
-------------------------------------------------------------------------------------------------------------------*/
function jump_get(p) {
	location.href = location.pathname + "?p=" + p;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: jump_get2()
 Description: 換頁, 使用 GET
 Input: 新頁碼
 Output: N/A
 Example: 
	jump_get2(2);
Revision History:
   1.0: original version 2007/5/3
-------------------------------------------------------------------------------------------------------------------*/
function jump_get2( p, other_parameters ) {
	location.href = location.pathname + "?p=" + p + other_parameters;
}

function jump_get3( p, m,c ) {
	location.href = location.pathname + "?p=" + p + "&mmid="+m+"&cat_id="+c;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: jump_guestbook()
 Description: 換頁, 使用 GET
 Input: mmid, 新頁碼
 Output: N/A
 Example: 
	jump_guestbook( 1, 2 );
Revision History:
   1.0: original version 2007/2/8
-------------------------------------------------------------------------------------------------------------------*/
function jump_guestbook( mmid, p ) {
	location.href = location.pathname + "?mmid=" + mmid + "&p=" + p;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: jump_prod()
 Description: 換頁, 使用 GET
 Input: 主分類, 次分類, 新頁碼, pid
 Output: N/A
 Example: 
	jump_prod( 1, 2, 3, 4 );
Revision History:
   1.0: original version 2007/2/6
-------------------------------------------------------------------------------------------------------------------*/
function jump_prod( no, sno, p, pid ) {
	location.href = location.pathname + "?no=" + no + "&sno=" + sno + "&p=" + p + "&pid=" + pid;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: jump_cat(id)
 Added by jul
 Description:
 Input: catlog id
 Output: N/A
 Example: 
	jump_cay( 1, 2, 3, 4 );
Revision History:
   1.0: original version 2007/2/6
-------------------------------------------------------------------------------------------------------------------*/
function jump_cat( cid ) {
	
	srh=location.search;
	lp=location.pathname;
	
	lp=lp.replace('ea_member_visitor_creation_d',"ea-member_visitor_index");
	lp=lp.replace('ea_member_visitor_image_d',"ea-member_visitor_index");
	lp=lp.replace('ea_member_visitor_intro',"ea-member_visitor_index");
	lp=lp.replace('ea-member_visitor_message',"ea-member_visitor_index");
	lp=lp.replace('ea-member_visitor_guessbook',"ea-member_visitor_index");
	lp=lp.replace('ea-member_visitor_index2',"ea-member_visitor_index");

	lp=lp.replace('member_myinni_cre_price_manage',"member_visitor_index");
	lp=lp.replace('member_visitor_creation_d',"member_visitor_index");
	lp=lp.replace('member_visitor_favorite',"member_visitor_index");
	lp=lp.replace('member_visitor_guessbook',"member_visitor_index");
	lp=lp.replace('member_visitor_image_d',"member_visitor_index");
	lp=lp.replace('member_visitor_intro',"member_visitor_index");
	lp=lp.replace('member_visitor_message',"member_visitor_index");
	lp=lp.replace('member_visitor_index2',"member_visitor_index");

	lp=lp.replace('member_myinni_intro',"member_myinni_index");
	lp=lp.replace('member_myinni_password',"member_myinni_index");
	lp=lp.replace('member_myinni_response',"member_myinni_index");
	lp=lp.replace('member_myinni_sells_report',"member_myinni_index");
	lp=lp.replace('member_myinni_edit',"member_myinni_index");
	lp=lp.replace('member_myinni_edit2',"member_myinni_index");
	lp=lp.replace('member_myinni_favorite',"member_myinni_index");
	lp=lp.replace('member_myinni_guessbook',"member_myinni_index");
	lp=lp.replace('member_myinni_index2',"member_myinni_index");
	
	srh=srh.replace(/\?cat_id=(\d+)|&cat_id=(\d+)/g,"");
	if(srh=="")
		location.href = lp +'?cat_id='+cid;
	else
		location.href = lp +srh+'&cat_id='+cid;
}
/*-------------------------------------------------------------------------------------------------------------------
 Function: jump_cat(id)
 Added by jul
 Description:
 Input: catlog id
 Output: N/A
 Example: 
	jump_cay( 1, 2, 3, 4 );
Revision History:
   1.0: original version 2007/2/6
-------------------------------------------------------------------------------------------------------------------*/
function jump_fav( cid ) {
	
	srh=location.search;
	lp=location.pathname;
	
	window.open('member_visitor_index'+ "?mmid=" + cid,'mywindow');
		//location.href = 'member_visitor_index'+ "?mmid=" + cid;
	
}
/*-------------------------------------------------------------------------------------------------------------------
 Function: jump_cat(id)
 Added by jul
 Description:
 Input: catlog id
 Output: N/A
 Example: 
	jump_cay( 1, 2, 3, 4 );
Revision History:
   1.0: original version 2007/2/6
-------------------------------------------------------------------------------------------------------------------*/
function addfav() {
	srh=location.search;
	lp=location.pathname;	
	location.href = lp + srh + "&fav=add";
}
/*-------------------------------------------------------------------------------------------------------------------
 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('words');
	if ( words.value == "" || words.value == "請輸入關鍵字" ) {
		alert( "請輸入關鍵字!" );
		words.focus();
		return false;
	}
	location.href = location.pathname + "?w=" + encodeURI( words.value );
}

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

/*-------------------------------------------------------------------------------------------------------------------
 Function: thesame_click2()
 Description: "勾選"同訂購人資料 -- 第二種版本
 Input: this
 Output: N/A
 Example: 
	go_search();
Revision History:
   1.0: original version 2007/2/9
-------------------------------------------------------------------------------------------------------------------*/
function thesame_click2( objSelf )
{
	if ( objSelf.checked ) {
		var fm = objSelf.form;
		fm.receiver.value = fm.real_name.value;
		fm.tel.value         = fm.tel1.value;
		fm.address.value = fm.address1.value;
		var post = fm.zip1.value;
		if ( post.length > 3 ) {
			post = post.substr( 0, 3 );
		}
 		fm.PostNo.value  = post;
		//showZone(mgr.City, mgr.Canton, post );
	}
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: file_click()
 Description:  瀏覽上傳檔案
 Input: 上傳檔案編號
 Output: N/A
 Example: 
	file_click(1);
Revision History:
   1.0: original version 2007/2/6
-------------------------------------------------------------------------------------------------------------------*/
function file_click( number )
{
	var file          = document.getElementById( 'file' + number );
	var filename = document.getElementById( 'filename' + number );
	file.click();
	filename.value = file.value;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: jump_member_myinni_cre_price_manage(no)
 Description:  no
 Input: no
 Output: N/A
   1.0: original version 2008/09/07
-------------------------------------------------------------------------------------------------------------------*/
function jump_member_myinni_cre_price_manage(no,p) {
	location.href = location.pathname + "?no=" + no+'&pg='+p;
}
