/*-------------------------------------------------------------------------------------------------------------------
 Script Name: functions.js
 Author: Tony Wei (魏志國)  tonywei123_at_gmail.com
 Description: JavaScript 共用函式庫
 Revision History:
   1.0: original version 2010/5/15
   1.1: 加上「會員卡號」不可重複的檢查 2011/1/6
-------------------------------------------------------------------------------------------------------------------*/



// 加入會員
function check_register()
{
	if ( ! document.getElementById( "accept1" ).checked ) {
		alert( "請先詳閱會員條款，並勾選同意!" );
		document.getElementById( "accept1" ).focus();
		return;
	}
	var fm = document.form1;
	if ( location.href.indexOf( "_dept" ) > -1 ) {
		if ( ! check_required( 'MI_CardNumber', '會員卡號' ) ) return;
		if ( ! check_MI_CardNumber() ) {
			return;
		}
		//if ( ! check_required( 'MI_Pid', '身份證字號' ) ) return;
		if ( document.getElementById( "MI_Pid" ).value != "" ) {
			if ( ! idChech( "MI_Pid" ) ) {
				return;
			}
		}
	}
	if ( ! check_required( 'MI_Name', '中文名' ) ) return;
	if ( ! ( document.getElementById( "MI_Sex1" ).checked || document.getElementById( "MI_Sex2" ).checked ) ) {
		alert( "請選擇性別!" );
		return;
	}
	if ( fm.yyyy.selectedIndex < 1 || fm.mm.selectedIndex < 1 || fm.dd.selectedIndex < 1 ) {
		alert( "請選取您的出生年月日!" );
		return;
	}
	var yyyy	= fm.yyyy.options[ fm.yyyy.selectedIndex ].text;
	var mm	= fm.mm.options[ fm.mm.selectedIndex ].text;
	var dd		= fm.dd.options[ fm.dd.selectedIndex ].text;
	if ( ! check_date( yyyy, mm, dd ) ) return;
	if ( ! check_required( 'MI_ID', '帳號' ) ) return;
	if ( fm.MI_ID.value.length < 6 ) {
		alert( "帳號至少須為 6 個字以上!" );
		fm.MI_ID.focus();
		return;
	}
	if ( ! check_member_account() ) {
		return;
	}
	if ( document.getElementById( "MI_Email" ).value != "" ) {
		if ( ! check_email( 'MI_Email' ) ) return;
		if ( ! check_member_email() ) {
			return;
		}
	}
	/*
	if ( ! check_required( 'MI_Email', 'Email' ) ) return;
	if ( ! check_email( 'MI_Email' ) ) return;
	if ( ! check_member_email() ) {
		return;
	}
	*/
	if ( ! check_required( 'MI_Password', '密碼' ) ) return;
	if ( fm.MI_Password.value.length < 6 ) {
		alert( "密碼至少須為 6 個字以上!" );
		fm.MI_Password.value = "";
		fm.MI_Password2.value = "";
		fm.MI_Password.focus();
		return;
	}
	if ( ! check_required( 'MI_Password2', '密碼確認' )   ) return;
	if ( fm.MI_Password.value != fm.MI_Password2.value ) {
		alert( "密碼與密碼確認須一致!" );
		fm.MI_Password.value = "";
		fm.MI_Password2.value = "";
		fm.MI_Password.focus();
		return;
	}
	if ( ! check_required( 'MI_Tel', '連絡電話' ) ) return;
	if ( ! check_numeric2( 'MI_Tel', '連絡電話' )     ) return;
	if ( document.getElementById( "cityarea" ).selectedIndex < 1 ) {
		alert( "請選擇縣市鄉鎮!" );
		return;
	}
	if ( ! check_required( 'MI_Address', '連絡地址' ) ) return;
	if ( ! check_required( 'code', '驗證碼' ) ) return;
	if ( ! check_numeric2( 'code', '驗證碼' )     ) return;
	if ( ! check_code( 1 ) ) return;
	fm.submit();
}

// 會員登入
function member_login()
{
	var fm = document.form1;
	if ( fm.uid.value == "帳號" ) {
		fm.uid.value = "";
	}
	if ( ! check_required( 'uid', '帳號' ) ) return;
	if ( ! check_required( 'pwd', '密碼' ) ) return;
	fm.action = "index.aspx?fn=member_login";
	fm.submit();
}

// 忘記密碼
function member_forget()
{
	if ( ! check_required( 'uid', '帳號' ) ) return;
	if ( ! check_required( 'email', 'Email' ) ) return;
	if ( ! check_email( 'email' ) ) return;
	document.form1.submit();
}

// 修改密碼
function member_edit_password()
{
	var fm = document.form1;
	if ( ! check_required( 'MI_Password0', '舊密碼' ) ) return;
	if ( ! check_password() ) return;
	if ( ! check_required( 'MI_Password', '新密碼' ) ) return;
	if ( fm.MI_Password.value.length < 6 ) {
		alert( "密碼至少須為 6 個字以上!" );
		fm.MI_Password.value = "";
		fm.MI_Password2.value = "";
		fm.MI_Password.focus();
		return;
	}
	if ( ! check_required( 'MI_Password2', '新密碼確認' )   ) return;
	if ( fm.MI_Password.value != fm.MI_Password2.value ) {
		alert( "密碼與密碼確認須一致!" );
		fm.MI_Password.value = "";
		fm.MI_Password2.value = "";
		fm.MI_Password.focus();
		return;
	}
	document.form1.func.value = "password";
	document.form1.submit();
}

// 修改會員資料
function member_edit()
{
	var fm = document.form1;
	if ( ! check_required( 'MI_Name', '中文名' ) ) return;
	if ( fm.yyyy.selectedIndex < 1 || fm.mm.selectedIndex < 1 || fm.dd.selectedIndex < 1 ) {
		alert( "請選取您的出生年月日!" );
		return;
	}
	var yyyy	= fm.yyyy.options[ fm.yyyy.selectedIndex ].text;
	var mm	= fm.mm.options[ fm.mm.selectedIndex ].text;
	var dd		= fm.dd.options[ fm.dd.selectedIndex ].text;
	if ( ! check_date( yyyy, mm, dd ) ) return;
	if ( ! check_required( 'MI_Tel', '連絡電話' ) ) return;
	if ( ! check_numeric2( 'MI_Tel', '連絡電話' )     ) return;
	if ( document.getElementById( "cityarea" ).selectedIndex < 1 ) {
		alert( "請選擇縣市鄉鎮!" );
		return;
	}
	if ( ! check_required( 'MI_Address', '連絡地址' ) ) return;
	document.form1.func.value = "data";
	document.form1.submit();
}

// -------------------------------------------------------------------------------------------------------------------------------------------

// 切換地圖
function change_map( addr )
{
	var url = "http://maps.google.com.tw/maps?hl=zh-TW&q=" + encodeURI( addr ) + "&ie=UTF8&output=embed&z=16";
	document.getElementById( "iframe1" ).src = url;
}

// 切換地址
function go_area()
{
	var city	= document.getElementById( "city" );
	var area	= document.getElementById( "cityarea" );
	var sno	= document.getElementById( "sno" );
	/*
	if ( area.selectedIndex < 1 ) {
		alert( "請選擇鄉鎮市區!" );
		return;
	}
	*/
	location.href = "index.aspx?fn=store&city=" + encodeURI( city.options[ city.selectedIndex ].text ) + "&area=" + encodeURI( area.options[ area.selectedIndex ].text ) + "&sno=" + sno.options[ sno.selectedIndex ].value;
}

// 產品內容 -- 加入收藏
function add_favorite( no )
{
	var url = "index.aspx?fn=add_favorite&rand=" + Math.random() + "&no=" + no;
	var objTag = ccioo_ajax_getXmlData( url ).getElementsByTagName("item");
	var sReturn = objTag[0].firstChild.nodeValue;
	if ( parseInt( sReturn ) == 1 ) {
		alert( "已加入!" );
	} else {
		alert( "請先登入，再進行本功能操作!" );
	}
}

// 產品內容 -- 切換銷售據點
function change_store( sno )
{
	var city	= document.getElementById( "city" );
	var area = document.getElementById( "cityarea" );
	// 取回銷售據點資料
	$.getJSON( "index.aspx?fn=change_store&sno=" + sno + "&city=" + encodeURI( city.options[ city.selectedIndex ].text ) + "&area=" + encodeURI( area.options[ area.selectedIndex ].text ) + "&rand=" + Math.random(),
		function( data ) {
			document.getElementById( "div_store" ).innerHTML = "<select name='store' id='store' class='MAP_SHOP'>" + data.store_menu + "</select>";
	} );
}

// 產品內容 -- 移至銷售據點
function go_store()
{
	var store = document.getElementById( "store" );
	if ( store.selectedIndex < 0 ) {
		alert( "請先選取銷售據點!" );
		return;
	}
	var url = "http://maps.google.com.tw/maps?hl=zh-TW&q=" + encodeURI( store.options[ store.selectedIndex ].value ) + "&ie=UTF8&output=embed&z=16";
	window.open( url );
}

// 客服信箱
function check_service()
{
	if ( ! check_required( 'CU_Name', '大名' ) ) return;
	if ( ! check_required( 'CU_Email', 'Email' ) ) return;
	if ( ! check_email( 'CU_Email' ) ) return;
	if ( ! check_required( 'CU_Subject', '主旨' ) ) return;
	if ( ! check_required( 'CU_Content', '內容' ) ) return;
	if ( ! check_required( 'code', '驗證碼' ) ) return;
	if ( ! check_numeric2( 'code', '驗證碼' )     ) return;
	if ( ! check_code( 1 ) ) return;
	document.form1.submit();
}

// 客服信箱 - 英文版
function check_service_eng()
{
	if ( ! check_required2( 'CU_Name', 'Name' ) ) return;
	if ( ! check_required2( 'CU_Email', 'Email' ) ) return;
	if ( ! check_email2( 'CU_Email' ) ) return;
	if ( ! check_required2( 'CU_Subject', 'Subject' ) ) return;
	if ( ! check_required2( 'CU_Content', 'Content' ) ) return;
	if ( ! check_required2( 'code', 'Verify Code' ) ) return;
	if ( ! check_code( 2 ) ) return;
	document.form1.submit();
}

// 轉寄好友
function check_forward()
{
	if ( ! check_required( 'sender', '你的姓名' ) ) return;
	if ( ! check_required( 'email', '你的EMAIL' ) ) return;
	if ( ! check_email( 'email' ) ) return;
	if ( ! check_required( 'receiver', '好友姓名' ) ) return;
	if ( ! check_required( 'email1', '好友的EMAI' ) ) return;
	if ( ! check_email( 'email1' ) ) return;
	document.form1.submit();
}

// 刪除收藏
function remove_favorite( fid )
{
	url = "index.aspx?fn=remove_favorite&no=" + fid + "&rand=" + Math.random();
	ccioo_ajax_setData( url );
	location.reload();
}

// 電子報訂閱
function check_subscribe()
{
	if ( document.getElementById( "epaper_mail" ).value == "E-mail (訂閱電子報)" ) {
		document.getElementById( "epaper_mail" ).value = "";
	}
	if ( ! check_required( 'epaper_mail', 'E-mail' ) ) return;
	if ( ! check_email( 'epaper_mail' ) ) return;
	location.href = "index.aspx?fn=subscribe&func=1&epaper_mail=" + document.getElementById( "epaper_mail" ).value;
}

// 電子報訂閱 / 退訂
function check_subscribe2()
{
	if ( ! check_required( 'epaper_mail2', 'E-mail' ) ) return;
	if ( ! check_email( 'epaper_mail2' ) ) return;
	var func = "1";
	if ( document.getElementById( "subscribe2" ) != null ) {
		if ( document.getElementById( "subscribe2" ).checked ) {
			func = "2";
		}
	}
	location.href = "index.aspx?fn=subscribe&func=" + func + "&epaper_mail=" + document.getElementById( "epaper_mail2" ).value;
}

// 產品搜尋
function go_search()
{
	if ( document.getElementById( "k" ).value == "SEARCH" ) {
		document.getElementById( "k" ).value = "";
	}
	if ( ! check_required( 'k', '關鍵字' ) ) return;
	location.href = "index.aspx?fn=search_result&k=" + encodeURI( document.getElementById( "k" ).value );
}

// 預先檢查驗證碼
function check_code( lang )
{
	var url = "index.aspx?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 ) {
		if ( lang == 1 ) {
			alert( "請輸入正確的驗證碼!" );
		} else {
			alert( "Please input the correct Verify Code!" );
		}
		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()
{
	if ( ! check_required( 'MI_ID', '登入帳號' ) ) return false;
	var url = "index.aspx?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").focus();
		return false;
	}
	return true;
}
function check_member_email()
{
	var url = "index.aspx?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( "此 E-Mail 已經註冊過，如果您忘記密碼，請利用 '忘記密碼' 功能來查詢!" );
		document.getElementById("MI_Email").focus();
		return false;
	}
	return true;
}
function check_MI_CardNumber()
{
	var url = "index.aspx?fn=member_check&rand=" + Math.random() + "&MI_CardNumber=" + document.getElementById("MI_CardNumber").value;
	var objTag = ccioo_ajax_getXmlData( url ).getElementsByTagName("item");
	var sReturn = objTag[0].firstChild.nodeValue;
	if ( parseInt( sReturn ) > 0 ) {
		alert( "此會員卡號已經註冊過，如果您忘記密碼，請利用 '忘記密碼' 功能來查詢!" );
		document.getElementById("MI_CardNumber").focus();
		return false;
	}
	return true;
}
function check_password()
{
	var url = "index.aspx?fn=password_check&rand=" + Math.random() + "&MI_Password=" + document.getElementById("MI_Password0").value;
	var objTag = ccioo_ajax_getXmlData( url ).getElementsByTagName("item");
	var sReturn = objTag[0].firstChild.nodeValue;
	if ( parseInt( sReturn ) == 0 ) {
		alert( "您輸入的舊密碼錯誤，請您重新輸入!" );
		document.getElementById("MI_Password0").value = "";
		document.getElementById("MI_Password0").focus();
		return false;
	}
	return true;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: check_required()
 Description: 檢查表單欄位是否有填寫
 Input: 物件ID, 訊息
 Output: 是否驗證通過 true or false
 Example: 
	check_required( "username", "姓名" );
Revision History:
   1.0: original version 2007/1/5
-------------------------------------------------------------------------------------------------------------------*/
function check_required( objID, sMessage )
{
	var objField = document.getElementById( objID );
	if ( objField.value.replace( /\s/g, "" ).length == 0 ) {
		alert( "請填寫" + sMessage + "!" );
		objField.focus();
		return false;
	}
	return true;
}
function check_required2( objID, sMessage )
{
	var objField = document.getElementById( objID );
	if ( objField.value.replace( /\s/g, "" ).length == 0 ) {
		alert( "Please input " + 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_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( 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( "Please input the correct Email!" );
		objField.select();
		objField.focus();
		return false;
	}
	return true;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: check_date()
 Description: 檢查日期格式是否正確
 Input: 年, 月, 日
 Output: 是否驗證通過 true or false
 Example: 
	check_date( 2008, 1, 29 );
Revision History:
   1.0: original version 2008/3/7
-------------------------------------------------------------------------------------------------------------------*/
function check_date( yyyy, mm, dd )
{
	mm = parseInt( mm ) - 1;
	var day = ( new Date( yyyy, mm, dd ) ).getDate();
	if ( day != dd ) {
		if ( mm == 1 && dd == 29 ) {
			alert( yyyy + " 年不是閏年，2 月沒有 29 天喔!" );
		} else {
			alert( ( mm + 1 ) + " 月沒有 " + dd + " 天喔!" );
		}
		return false;
	}
	return true;
}

/*-------------------------------------------------------------------------------------------------------------------
 Function: 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
   3.0: c21 專用 2010/1/13
-------------------------------------------------------------------------------------------------------------------*/
function jump_get( p, sortNumber ) {
	var qs = new Querystring();
	var tail = "";
	if ( qs.get("no") != null ) {
		tail += "&no=" + qs.get("no");
	}
	if ( qs.get("city") != null ) {
		tail += "&city=" + encodeURI( document.getElementById('hcity').value );
	}
	if ( qs.get("area") != null ) {
		tail += "&area=" + encodeURI( document.getElementById('hcityarea').value );
	}
	if ( qs.get("sno") != null ) {
		tail += "&sno=" + qs.get("sno");
	}
	if ( qs.get("k") != null ) {
		tail += "&k=" + encodeURI( document.getElementById('k1').value );
	}
	if ( sortNumber != 0 ) {
		tail += "&sort=" + sortNumber;
	} else {
		if ( qs.get("sort") != null ) {
			tail += "&sort=" + qs.get("sort");
		}
	}
	location.href = "index.aspx?fn=" + qs.get("fn") + "&page=" + p + tail + "#anchor";
}


/*-------------------------------------------------------------------------------------------------------------------
身份證字號檢查
-------------------------------------------------------------------------------------------------------------------*/
function firstlettererr(id){    
var fl=id.value.substr(0,1);    
var T="ABCDEFGHJKLMNOPQRSTUVWXYZ"; //*24個*//    
var smp=id.value.substr(0,1)    
if (T.indexOf(smp) == -1) { return false; } else { return true; }    
}    
  
function checkid(id) {    
if(id.value.length==10)    
{ return true; } else { return false; }    
}    
  
function chfastid(id) {    
var c = id.value.charAt(0);    
if(c<"A" || c> "Z")    
{ return false; } else { return true; }    
}    
  
function ch12fastid(id) {    
var c = id.value.charAt(1);    
if(c!="1" && c!="2")    
{ return false; } else { return true; }    
}    
  
function idmanber(id) { //=====後九碼為數字//    
var bmp;    
var d="0123456789";    
var bab=id.value.length-1;    
for (var i=1;i<=bab;i++){    
bmp=id.value.substr(i,1)    
if (d.indexOf(bmp) == -1) { return false; }    
}    
if(id.value != "0")    
{ return true; } else    
{ return false; }    
}    
  
function idchackok(id) { //規則//    
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(i=0;i<alph.length;i++)    
if(id.value.charAt(0)==alph[i])    
n=i;    
var tot1 = parseFloat(num[n].charAt(0)) + (parseFloat(num[n].charAt(1)) * 9);    
var tot2 = 0;    
for(i=1;i<id.value.length-1;i++)    
tot2 = tot2 + parseFloat(id.value.charAt(i))*(9-i);    
var tot3 = parseFloat(id.value.charAt(9));    
var tot4 = tot1 + tot2 + tot3;    
if((tot4 % 10)!=0)    
{ return false; } else { return true; }    
}    
  
  
function idChech( objID ){    

  	var id = document.getElementById( objID );

if ( id.value.toLowerCase() == "a123456789" ) {
alert( "身份證字號錯誤，請您重新檢查 !");    
return false;    
}

if(checkid(id) == false){    
alert( "身份證字號必須是十個字元!");    
return false;    
}    
  
if(chfastid(id) == false){    
alert( "身份證字號第一碼必須是大寫的英文字母!");    
return false;    
}    
  
if(ch12fastid(id) == false){    
alert( "身份證字號第二碼錯誤 !");    
return false;    
}    
  
if(firstlettererr(id) == false){    
alert( "身份證字號錯誤!!");    
return false;    
}    
  
if(idmanber(id) == false){    
alert( "身份證字號後9碼應為數字!!");    
return false;    
}    
  
if(idchackok(id) == false){    
alert( "身份證字號錯誤，請您重新檢查 !");    
return false;    
}    
  return true;
}    

