// 程式路徑：/export/home/www/php/activity/2008/03/main.js
// 程式功能：問卷模組的 js
// 程式設計：呂致緯
// 最後修改：2008.3.11

$(document).ready(function(){	
	
	// 『出現順序』欄位只可以輸入數字，禁止其他字元的輸入
	$('input[@name="ad_order"]').keypress(function(event) {
		if( event.charCode && (event.charCode <48 || event.charCode > 57 )) {
			event.preventDefault();
		}		
	});
	
	// 點擊刪除時，跳出 confirm box 請使用者進行確認
	$("a:contains('刪除')").click(function(){
		var choice = confirm('確定要刪除此筆資料?');
		if (choice == false) {
			return false;
		}		
	});
	
		
	// 點擊 "活動連結" 的輸入方塊時，自動選取文字
	$(".link_column").focus(function() {
		$(this).select();				
		});	
		
	$(".link_column").click(function() {
		$(this).select();		
		});	
	
	$('.link_column').attr('readonly', true);
		
		
	
// *******************************************
// * 『問卷系統 > 活動設定』
// *******************************************
	// 除了編輯活動資料以外，當點擊『儲存』按鈕時，將 flag 設定為 insert_main
	$('#b_save').click(function() {
		$('input[@name="flag"]').attr({value:"insert_main"});	
	});
	
	// 在編輯活動資料時，點擊『儲存』按鈕時，將 flag 設定為 update_main
	$('#b_edit_save').click(function() {
		$('input[@name="flag"]').attr({value:"update_main"});	
	});
	
	// 點擊『清空』按鈕時，將網頁重新載入以清空隱藏欄位的值
	// 並將 flag 設定為 flush，以清空暫存在 session 裡的文字方塊輸入值
	$("#b_reset").click(function() {
		$.post('main.php', {'flag': 'flush'}, function(data) {
				location.href = "main.php";
			});
		
	});
	
	// 點擊『新增』按鈕時，將 flag 設定為 insert_main
	$("#b_insert").click(function() {
		$('input[@name="flag"]').attr({value:"insert_main"});	
	});
		


// *******************************************
// * 『問卷系統 > 活動設定 > 廣告設定--廣告Banner』區塊的按鈕
// *******************************************
	// 除了編輯廣告資料以外，當點擊『儲存』按鈕時，將 flag 設定為 insert_ad_banner
	$('#b_banner_save').click(function() {
		$('input[@name="flag"]').attr({value:"insert_ad_banner"});	
	});
		
	// 點擊『清空』按鈕時，重新載入網頁
	$("#b_banner_reset").click(function() {
		location.href = "ad.php";
		return false;
	});
	
	// 在編輯活動資料時，點擊『儲存』按鈕時，將 flag 設定為 update_ad_banner
	$('#b_banner_edit_save').click(function() {
		$('input[@name="flag"]').attr({value:"update_ad_banner"});	
	});
	
	// 點擊『回活動設定』按鈕時，將網頁導回『問卷系統 > 活動設定』
	// 並將 flag 設定為 flush，以清空暫存在 session 裡的文字方塊輸入值
	$("#b_banner_main").click(function() {
		$.post('main.php', {'flag': 'flush'}, function(data) {
				location.href = "main.php";
		});
	});
	
	// 點擊『新增』按鈕時，將 flag 設定為 insert_ad_banner
	$("#b_banner_insert").click(function() {
		$('input[@name="flag"]').attr({value:"insert_ad_banner"});	
	});
	
	
		
// *******************************************
// * 『問卷系統 > 活動設定 > 廣告設定--文字廣告』區塊的按鈕
// *******************************************
	// 除了編輯廣告資料以外，當點擊『儲存』按鈕時，將 flag 設定為 insert_ad_text
	$('#b_text_save').click(function() {
		$('input[@name="flag"]').attr({value:"insert_ad_text"});	
	});
	
	// 點擊『清空』按鈕時，重新載入網頁
	$("#b_text_reset").click(function() {
		location.href = "ad.php";
		return false;
	});
	
	// 在編輯活動資料時，點擊『儲存』按鈕時，將 flag 設定為 update_ad_text
	$('#b_text_edit_save').click(function() {
		$('input[@name="flag"]').attr({value:"update_ad_text"});	
	});
	
	// 點擊『回活動設定』按鈕時，將網頁導回『問卷系統 > 活動設定』
	// 並將 flag 設定為 flush，以清空暫存在 session 裡的文字方塊輸入值
	$("#b_text_main").click(function() {
		$.post('main.php', {'flag': 'flush'}, function(data) {
				location.href = "main.php";
		});
	});
	
	// 點擊『新增』按鈕時，將 flag 設定為 insert_ad_banner
	$("#b_text_insert").click(function() {
		$('input[@name="flag"]').attr({value:"insert_ad_text"});	
	});
	
	
	
// *******************************************
// * 『問卷系統 > 活動設定 > 題目檢視』區塊的按鈕
// *******************************************
	// 在編輯活動資料時，點擊『儲存』按鈕時，將 flag 設定為 update_question
	$('#b_q_save').click(function() {
		$('input[@name="flag"]').attr({value:"update_question"});	
	});
	
	// 點擊『回活動設定』或”清空』按鈕時，將網頁導回『問卷系統 > 活動設定』
	// 並將 flag 設定為 flush，以清空暫存在 session 裡的文字方塊輸入值
	$("#b_q_main").click(function() {		
		$.post('main.php', {'flag': 'flush'}, function(data) {
				location.href = "main.php";
		});
	});
	
	$("#b_q_reset").click(function() {
		location.href = "questions.php";
		return false;
	});
	
}); // $(document).ready


// 設定 JavaScript 時鐘
$(document).ready(show_clock);






// =======================================================================================================================
// = 自行定義的 function
// =======================================================================================================================
// 函數名稱: valid_url
// 函數功能: 檢查是否為合法的 url
// 傳入參數: URL
// 回傳的值: true, false
	function valid_url(myurl) {
			// 檢查是否為合法的 URL
			// 1. 不可以是 IP
			// 2. 不可以是 kenny.books.com.tw
			// 3. 不可以是 db.books.com.tw
			
			// 設定 Regular Expression
			no_ip = /^(http\:\/\/)([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(\/\S*)?$/ ;
			kenny = /^http\:\/\/kenny\.books\.com\.tw/;
			db = /^http(s?)\:\/\/db\.books\.com\.tw/;
			if ( no_ip.test(myurl) == false || kenny.test(myurl) == true || db.test(myurl) == true ) {
				return false;
			}			
			return true;
	} // function



// 函數名稱: check_data_main
// 函數功能: 檢查『問卷系統 > 活動設定』表單使用者輸入的資料之合法性
// 傳入參數: void
// 回傳的值: true, false
	function check_data_main(){
		// ===== 活動標題 =====
		if (document.act_form.title.value == ''){
			alert("請輸入活動標題！");
			document.act_form.title.focus();
			return false;
		} 
		
		/*
		if (document.act_form.title.value.length < 5){
			alert("活動標題不可以少於5個字！");
			document.act_form.title.focus();
			return false;
		}
		*/
		// ===== // 活動標題 =====
	
	
		// ===== 活動說明 =====
		if (document.act_form.description.value == ''){
			alert("請輸入活動說明！");
			document.act_form.description.focus();
			return false;
		} 
		
		/*
		if (document.act_form.description.value.length < 10){
			alert("活動說明不可以少於10個字！");
			document.act_form.description.focus();
			return false;
		}	
		*/	
		// ===== // 活動說明 =====
			
	
		// ===== 上線日期 =====
		if (document.act_form.start_date.value == '' ){
			alert("請輸入上線日期！");
			document.act_form.start_date.focus();
			return false;
		} // if
		
		var str_start_date = document.act_form.start_date.value;
		var start_date = new Date(str_start_date);
		if ( start_date == 'Invalid Date' ) {
			alert('請輸入正確的上線時間！');
			document.act_form.start_date.focus();
			return false;
		} // if
		// ===== // 上線日期 =====
		
		
		// ===== 下線日期 =====
		if (document.act_form.expire_date.value == '' ){
			alert("請輸入下線日期！");
			document.act_form.expire_date.focus();
			return false;
		} // if
		
		var str_expire_date = document.act_form.expire_date.value;
		var expire_date = new Date(str_expire_date);
		if ( expire_date == 'Invalid Date' ) {
			alert('請輸入正確的下線時間！');
			document.act_form.expire_date.focus();
			return false;
		} // if
	
		// 下線時間不能早於上線時間
		if (expire_date.getTime() < start_date.getTime()) {
			alert("下線時間不能早於上線時間！");
			document.act_form.expire_date.focus();
			return false;
		}
		// ===== // 下線日期 =====
				
		// 匯入題目
		if ( document.act_form.questions_file != null) {
			if ( document.act_form.questions_file.value == '' ){
				alert("請選擇要匯入的題目！");
				document.act_form.questions_file.focus();
				return false;
			} // if
		} // if
			
		// 級距的標題1
		if ( document.act_form.interval_label_1.value == '' && document.act_form.radio_interval.checked == true ){
			alert("請輸入級距的標題1！");
			document.act_form.interval_label_1.focus();
			return false;
		} // if
		
		if ( document.act_form.interval_label_1.value.length > 5 ){
			alert("級距的標題請勿超過5個字！");
			document.act_form.interval_label_1.focus();
			return false;
		} // if
	
		// 級距的標題2
		if ( document.act_form.interval_label_2.value == '' && document.act_form.radio_interval.checked == true ){
			alert("請輸入級距的標題2！");
			document.act_form.interval_label_2.focus();
			return false;
		} // if
		
		if ( document.act_form.interval_label_2.value.length > 5 ){
			alert("級距的標題請勿超過5個字！");
			document.act_form.interval_label_2.focus();
			return false;
		} // if
	
		// 級距的標題3
		if ( document.act_form.interval_label_3.value == '' && document.act_form.radio_interval.checked == true ){
			alert("請輸入級距的標題3！");
			document.act_form.interval_label_3.focus();
			return false;
		} // if
		
		if ( document.act_form.interval_label_3.value.length > 5 ){
			alert("級距的標題請勿超過5個字！");
			document.act_form.interval_label_3.focus();
			return false;
		} // if
	
		// 級距的標題4
		if ( document.act_form.interval_label_4.value == '' && document.act_form.radio_interval.checked == true ){
			alert("請輸入級距的標題4！");
			document.act_form.interval_label_4.focus();
			return false;
		} // if
		
		if ( document.act_form.interval_label_4.value.length > 5 ){
			alert("級距的標題請勿超過5個字！");
			document.act_form.interval_label_4.focus();
			return false;
		} // if
	
		// 級距的標題5
		if ( document.act_form.interval_label_5.value == '' && document.act_form.radio_interval.checked == true ){
			alert("請輸入級距的標題5！");
			document.act_form.interval_label_5.focus();
			return false;
		} // if
		
		if ( document.act_form.interval_label_5.value.length > 5 ){
			alert("級距的標題請勿超過5個字！");
			document.act_form.interval_label_5.focus();
			return false;
		} // if
		
		
		// ===== 在商品列表上方顯示的訊息 =====
		if (document.act_form.msg_above_item_list.value == ''){
			alert("請輸入要在商品列表上方顯示的訊息！");
			document.act_form.msg_above_item_list.focus();
			return false;
		} 
		
		/*
		if (document.act_form.msg_above_item_list.value.length < 5){
			alert("要在商品列表上方顯示的訊息不可以少於5個字！");
			document.act_form.msg_above_item_list.focus();
			return false;
		}
		*/
		// ===== // 在商品列表上方顯示的訊息 =====
		
			
		// ===== 大Banner圖檔 =====
		if ( document.act_form.big_banner_img.value == '' ){
			alert("請輸入大 Banner 圖檔的 URL！")
			document.act_form.big_banner_img.focus();
			return false;
		} // if
	
		if ( valid_url(document.act_form.big_banner_img.value) == false ) {
			alert("『大Banner圖檔』請輸入合法的網址，並請勿使用以下類型的 URL：\n 1.IP \n 2.http://kenny.books.com.tw \n 3.http://db.books.com.tw");
			document.act_form.big_banner_img.focus();
			return false;
		}
		// ===== // 大Banner圖檔 =====			
	
			
		// ===== 大Banner連結 =====
		if ( document.act_form.big_banner_link.value == '' ){
			alert("請輸入大 Banner 的連結！");
			document.act_form.big_banner_link.focus();
			return false;
		} // if
	
		if ( valid_url(document.act_form.big_banner_link.value) == false ) {
			alert("『大Banner連結』請輸入合法的網址，並請勿使用以下類型的 URL：\n 1.IP \n 2.http://kenny.books.com.tw \n 3.http://db.books.com.tw");
			document.act_form.big_banner_link.focus();
			return false;
		}
		// ===== // 大Banner連結 =====
	
	
		// ===== 小貼紙的標題 ===== 
		if ( document.act_form.small_img_title.value == '' ){
			alert("請輸入小貼紙的標題！");
			document.act_form.small_img_title.focus();
			return false;
		} // if
		
		/*
		if (document.act_form.small_img_title.value.length < 3){
			alert("小貼紙的標題不可以少於3個字！");
			document.act_form.small_img_title.focus();
			return false;
		}
		*/

		if (document.act_form.small_img_title.value.length > 7){
			alert("小貼紙的標題不可以大於7個字！");
			document.act_form.small_img_title.focus();
			return false;
		}
		// ===== // 小貼紙的標題 =====
	
	
		// ===== 小貼紙的連結 =====
		if ( document.act_form.small_img_link.value == '' ){
			alert("請輸入小貼紙的連結！");
			document.act_form.small_img_link.focus();
			return false;
		} // if
	
		if ( valid_url(document.act_form.small_img_link.value) == false ) {
			alert("『小貼紙的連結』請輸入合法的網址，並請勿使用以下類型的 URL：\n 1.IP \n 2.http://kenny.books.com.tw \n 3.http://db.books.com.tw");
			document.act_form.small_img_link.focus();
			return false;
		}
		// ===== // 小貼紙的連結 =====		
	} // function
	
	
	
// 函數名稱: check_data_question
// 函數功能: 檢查『問卷系統 > 活動設定』表單使用者輸入的資料之合法性
// 傳入參數: void
// 回傳的值: true, false
	function check_data_question(){
		
		// 活動標題
		if (document.q_form.title.value == ''){
			alert("請輸入題目！");
			document.q_form.title.focus();
			return false;
		} 
		
		// 店內碼
		if (document.q_form.item_id.value == ''){
			alert("請輸入店內碼！");
			document.q_form.item_id.focus();
			return false;
		} else if ( document.q_form.item_id.value.length != 10 ) {
			alert("店內碼格式錯誤！");
			document.q_form.item_id.focus();
			return false;
		}

		// 自定連結
		if ( (valid_url(document.q_form.user_defined_link.value) == false) && (document.q_form.user_defined_link.value != '') ){
			alert("『自定連結』請輸入合法的網址，並請勿使用以下類型的 URL：\n 1.IP \n 2.http://kenny.books.com.tw \n 3.http://db.books.com.tw");
			document.q_form.user_defined_link.focus();
			return false;
		}
				
		/*
		// 『自定標題』與『自定連結』必須同時有設定，或同時不設定
		if ( document.q_form.user_defined_title.value == "" && document.q_form.user_defined_link.value == "" )
		{
			; // do nothing
		} else if ( document.q_form.user_defined_title.value != "" && document.q_form.user_defined_link.value != "" ) {
			; // do nothing
		} else {
			alert("『自定標題』與『自定連結』必須同時有設定，或同時不設定！");
			document.q_form.user_defined_title.focus();
			return false;
		}
		*/
		
	} // function
	
	
		
// 函數名稱: check_data_ad_banner
// 函數功能: 檢查『問卷系統 > 活動設定 > 廣告設定--廣告Banner』表單使用者輸入的資料之合法性
// 傳入參數: void
// 回傳的值: true, false
	function check_data_ad_banner(){
		
		// ===== 出現順序 =====
		if (document.ad_banner_form.ad_order.value == ''){
			alert("請輸入出現順序！");
			document.ad_banner_form.ad_order.focus();
			return false;
		} 
		
		if (isNaN(parseInt(document.ad_banner_form.ad_order.value))) {
			alert("『出現順序』請輸入數字！");
			document.ad_banner_form.ad_order.focus();
			return false;
		}
		// ===== // 出現順序 =====

		
		// Banner圖檔
		if ( valid_url(document.ad_banner_form.img_url.value) == false ){
			alert("『Banner圖檔』請輸入合法的網址，並請勿使用以下類型的 URL：\n 1.IP \n 2.http://kenny.books.com.tw \n 3.http://db.books.com.tw");
			document.ad_banner_form.img_url.focus();
			return false;
		}
		
		// Banner連結
		if ( valid_url(document.ad_banner_form.link.value) == false ){
			alert("『Banner連結』請輸入合法的網址，並請勿使用以下類型的 URL：\n 1.IP \n 2.http://kenny.books.com.tw \n 3.http://db.books.com.tw");
			document.ad_banner_form.link.focus();
			return false;
		}		
		
		
		// ===== 上線日期 =====
		if (document.ad_banner_form.start_date.value == '' ){
			alert("請輸入上線日期！");
			document.ad_banner_form.start_date.focus();
			return false;
		} // if
		
		var str_start_date = document.ad_banner_form.start_date.value;
		var start_date = new Date(str_start_date);
		if ( start_date == 'Invalid Date' ) {
			alert('請輸入正確的上線時間！');
			document.ad_banner_form.start_date.focus();
			return false;
		} // if
		// ===== // 上線日期 =====
		
		
		// ===== 下線日期 =====
		if (document.ad_banner_form.expire_date.value == '' ){
			alert("請輸入下線日期！");
			document.ad_banner_form.expire_date.focus();
			return false;
		} // if
		
		var str_expire_date = document.ad_banner_form.expire_date.value;
		var expire_date = new Date(str_expire_date);
		if ( expire_date == 'Invalid Date' ) {
			alert('請輸入正確的下線時間！');
			document.ad_banner_form.expire_date.focus();
			return false;
		} // if
	
		// 下線時間不能早於上線時間
		if (expire_date.getTime() < start_date.getTime()) {
			alert("下線時間不能早於上線時間！");
			document.ad_banner_form.expire_date.focus();
			return false;
		}
		// ===== // 下線日期 =====		
		
	} // function
	
	
		
// 函數名稱: check_data_ad_text
// 函數功能: 檢查『問卷系統 > 活動設定 > 廣告設定--文字廣告』表單使用者輸入的資料之合法性
// 傳入參數: void
// 回傳的值: true, false
	function check_data_ad_text(){
		
		// ===== 出現順序 =====
		if (document.ad_text_form.ad_order.value == ''){
			alert("請輸入出現順序！");
			document.ad_text_form.ad_order.focus();
			return false;
		} 
		
		if (isNaN(parseInt(document.ad_text_form.ad_order.value))) {
			alert("『出現順序』請輸入數字！");
			document.ad_text_form.ad_order.focus();
			return false;
		}
		// ===== // 出現順序 =====
		
		
		// ===== 文字標題 =====
		if (document.ad_text_form.ad_text.value == ''){
			alert("請輸入文字標題！");
			document.ad_text_form.ad_text.focus();
			return false;
		}
		
		if (document.ad_text_form.ad_text.value.length < 5){
			alert("『文字標題』不可以小於5個字！");
			document.ad_text_form.ad_text.focus();
			return false;
		}
		// ===== // 文字標題 =====
		
		
		// 文字連結
		if ( valid_url(document.ad_text_form.link.value) == false ){
			alert("『文字連結』請輸入合法的網址，並請勿使用以下類型的 URL：\n 1.IP \n 2.http://kenny.books.com.tw \n 3.http://db.books.com.tw");
			document.ad_text_form.link.focus();
			return false;
		}
		
		
		// ===== 上線日期 =====
		if (document.ad_text_form.start_date.value == '' ){
			alert("請輸入上線日期！");
			document.ad_text_form.start_date.focus();
			return false;
		} // if
		
		var str_start_date = document.ad_text_form.start_date.value;
		var start_date = new Date(str_start_date);
		if ( start_date == 'Invalid Date' ) {
			alert('請輸入正確的上線時間！');
			document.ad_text_form.start_date.focus();
			return false;
		} // if
		// ===== // 上線日期 =====
		
		
		// ===== 下線日期 =====
		if (document.ad_text_form.expire_date.value == '' ){
			alert("請輸入下線日期！");
			document.ad_text_form.expire_date.focus();
			return false;
		} // if
		
		var str_expire_date = document.ad_text_form.expire_date.value;
		var expire_date = new Date(str_expire_date);
		if ( expire_date == 'Invalid Date' ) {
			alert('請輸入正確的下線時間！');
			document.ad_text_form.expire_date.focus();
			return false;
		} // if
	
		// 下線時間不能早於上線時間
		if (expire_date.getTime() < start_date.getTime()) {
			alert("下線時間不能早於上線時間！");
			document.ad_text_form.expire_date.focus();
			return false;
		}
		// ===== // 下線日期 =====
		
	} // function
	
	

// 函數名稱: show_clock
// 函數功能: 小時鐘
// 傳入參數: void
// 回傳的值: void
	function show_clock(){
		var now,hours,minutes,seconds,timeValue;
		now = new Date();
		year = now.getFullYear();
		month = now.getMonth()+1;
		day = now.getDate();
		hours = now.getHours();
		minutes = now.getMinutes();
		seconds = now.getSeconds();
		
		timeValue = year + "/";
		timeValue += month + "/";
		timeValue += ((day < 10) ? "0" : "") + day + "  ";
		timeValue += hours + ":";
		timeValue += ((minutes < 10) ? "0" : "") + minutes + ":";
		timeValue += ((seconds < 10) ? "0" : "") + seconds;
			
		if (document.getElementById('clock1') != null) {
			document.getElementById('clock1').innerHTML = timeValue;
		}
			
		if (document.getElementById('clock2') != null ) {
			document.getElementById('clock2').innerHTML = timeValue;
		}
				
		setTimeout("show_clock()",1000);
	} // function
	
		
	
// 函數名稱: check_data_questionary
// 函數功能: 檢查是否問卷表單中是否有尚未填寫的題目，若無則送出表單，若有則終止表單送出。
function check_data_questionary() {	
	// 檢查是否有未選取答案的問題
	var flag = 2;
	var q_no = 0;
	var is_error = false;
	$('input.answer').each(function(index){
			if( index % 2 == 0 ) {
				flag = 2;			
				
				// 計算題目編號，可用在提示使用者那個題目的答案忘了選	
				q_no = q_no +1 ;
			}
						
			if ( $(this).attr("checked") != true) {
				flag = flag - 1;
			}
				
			if (flag == 0) {
				is_error = true;
				alert("請選取第" + q_no + "題的答案！");
				$(this).focus();
				return false;
			}				
	}) // $('input.answer')

	// 若發現有任何問題，則終止表單送出
	if (is_error == true) {
		return false;
	}
	
	// 檢查無誤，送出資料
	var tmp_arr = new Array();	
	$('input.answer:even').each(function(index){ 				
		if(this.checked == true) {		
			// 記錄勾選 "是" 的題目，從 1 起算								
			tmp_arr.push( index+1 )
		} // if			
	});	// $('input.answer:even')
	$('input[@name="question_answer"]').attr({value:tmp_arr.join(',')});
	document.questionary_form.submit();
	
	// 取消原有的連結動作
	return false;	
} // function
