// JavaScript Document

// 判斷輸入值是否為中文字
function is_chinese (str) {
	for (var x = 0; x < str.length; x++) {
		if (escape (str.substring (x, x + 1)).length >= 2) return true;
	}
	
	return false;
}

// 判斷輸入值是否為純數字
function is_number (str) {
	var pattern = /^[0-9]{1,10}$/;
	
	if (str.match (pattern) == null) {
		return false;
	} else {
		return true;
	}
}

//　判斷email格式
function is_email (str) {
	var pattern = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z]{2,3})+$/;
	
	if (str.match (pattern) == null) {
		return false;
	} else {
		return true;
	}
}

function verify_frm (id) {
	$ (id).value = $F (id).gsub (' ', ''); // 去掉欄位值的空白

	switch (id) {
		case 'q_name':
			chk_name (id);
			break;
		case 'q_tel':
			chk_tel (id);
			break;
		case 'q_mobile':
			chk_mobile (id);
			break;
		case 'q_mail':
			chk_mail (id);
			break;
		case 'q_addr':
			chk_addr (id);
			break;
		default:
			break;
	}
}

// 驗證姓名
function chk_name (id) {
	var str = $F (id);
	if (str.blank ()) {
		alert ("請填寫您的真實姓名！");
		return false;
	} else if (!is_chinese (str)) {
		alert ("請填寫您的中文姓名！\r\n\r\n提示：\r\n\r\n如果您有隱私的疑慮，可以填寫「陳先生」或「林小姐」。");
		$ (id).activate ();
		return false;
	} else {
		return true;
	}
}

// 驗證電話號碼（市話號碼＆行動電話號碼）
function chk_tel (id) {
	var str = $F (id);
	
	if (str.blank ()) {
		alert ("請填寫您的聯絡電話！");
		return false;
	} else {
		var pattern = /^([0-9]{8,10}){1}(\#{0,1}[0-9]{1,5}){0,1}$/;
		var msg = "電話號碼應為數字，不可包含分隔符號。\r\n\r\n提示：\r\n\r\n正確的市話號碼應像：0229995556\r\n　填寫市話分機方式：0229995556#123\r\n正確的手機號碼應像：0912345678";
		
		if ((str.match (pattern) == null) || (!str.include ('#') && str.length > 10)) {
			alert (msg);
			$ (id).activate ();
			return false;
		} else if (!str.startsWith ('0')) {
			alert ("請填寫區域號碼！\r\n\r\n" + msg);
			$ (id).activate ();
			return false;
		} else if (str.startsWith ('01')) {
			alert ("區域號碼錯誤！");
			$ (id).activate ();
			return false;
		} else if (str.startsWith ('020') || str.startsWith ('080') || str.startsWith ('050') || str.startsWith ('094') || str.startsWith ('0951')) {
			alert ("很抱歉，系統不接受此類電話號碼！");
			$ (id).activate ();
			return false;
		} else if (str.startsWith ('09') && str.length != 10) {
			alert ("行動電話號碼錯誤！\r\n\r\n" + msg);
			$ (id).activate ();
			return false;
		} else if (str.length < 8) {
			alert ("電話號碼錯誤！\r\n\r\n" + msg);
			$ (id).activate ();
			return false;
		} else {
			return true;
		}
	}
}

// 驗證行動電話號碼
function chk_mobile (id) {
	var str = $F (id);
	
	if (str.blank()) {
		alert ("請填寫您的行動電話號碼！");
		return false;
	} else {
		var pattern = /^09[0-9]{8}$/;
		var msg = "行動電話號碼應為數字，不可包含分隔符號。\r\n\r\n提示：\r\n\r\n正確的手機號碼應像：0912345678";
		
		if (str.match (pattern) == null) {
			alert (msg);
			$ (id).activate ();
			return false;
		} else if (str.startsWith ('094') || str.startsWith ('0951')) {
			alert ("很抱歉，系統不接受此類電話號碼！");
			$ (id).activate ();
			return false;
		} else {
			return true;
		}
	}
}

// 驗證電子信箱格式
function chk_mail (id) {
	var str = $F (id);
	
	if (!str.blank() && !is_email (str)) {
		alert ("無效的電子郵件信箱！");
		$ (id).activate ();
		return false;
	} else {
		return true;
	}
}

// 驗證地址
function chk_addr (id) {
	var str = $F (id);
	
	if (str.blank ()) {
		alert ("請填寫您的收貨地址，或居住地區！");
		return false;
	} else {
		if (!is_chinese (str)) {
			alert ("請填寫正確的地址資訊！");
			$ (id).activate ();
			return false;
		} else {
			return true;
		}
	}
}