//檢查此資料是否為空值 field:欄位名稱, name:顯示文字
function check_null( field, name , fckeditor ){
	// 判斷fckedition前 請在原本頁面加上以下兩行程式碼
	//var oEditor = FCKeditorAPI.GetInstance( 'content' ) ;
	//oEditor.UpdateLinkedField();
	if ( fckeditor == 1 ){
		if ( trim(field) == '<br type="_moz" />' || trim(field) == '<br />' )
			return name + " 不能為空白!!\n";
	}
	if ( trim(field) == '' )
		return name + " 不能為空白!!\n";
	return '';
}

// 登入檢查, thisForm::form的obj
function check_login( thisForm ){
	var alertStr = "";
	alertStr += check_null( thisForm.account.value, "帳號" );
	alertStr += check_null( thisForm.password.value, "密碼" );
	alertStr += check_null( thisForm.chk.value, "驗證碼" );
     
	if ( alertStr != '' ){
	   alert( alertStr );
	   return false;
	}else{
      thisForm.action.value = "login";
      return true;
    }
}	

// 轉頁, url:要連結的網頁
function goto( url ){
	location.href = url;
}
function goto_replace( url ){
	location.replace(url);
}
// 轉頁 有網頁參數值, url:要連結的網頁, field:參數值名稱(xxx,yyy,zzz), value:參數值(aaa,bbb,ccc)
function goto_param( url, field, value ){
	var strlink = "";
	if ( ( field != undefined ) && ( value != undefined ) ){
		var strfield = field.split( "," );
		var strvalue = value.split( "," );
		
		for ( inti = 0; inti < strfield.length; inti++ ){
			if ( strlink == "" )
				strlink = "?" + strfield[inti] + "=" + strvalue[inti];
			else
				strlink += "&" + strfield[inti] + "=" + strvalue[inti];
		}
	}
	location.href = url + encodeURI(strlink);
}

// 刪除單張圖片
function delete_pic( delyes_id, div_self_id, div_other_id ){
	if ( confirm( "您確定要刪除此圖檔?" ) ){
		document.getElementById(delyes_id).value = 1;
		if ( div_self_id != undefined )
			document.getElementById(div_self_id).style.display = 'none';
		if ( div_other_id != undefined )
			document.getElementById(div_other_id).style.display = '';
	}else
		return false;
}	

// 刪除單一檔案
function delete_file( delyes_id, div_self_id, div_other_id ){
	if ( confirm( "您確定要刪除此檔案?" ) ){
		document.getElementById(delyes_id).value = 1;

		if ( div_self_id != undefined )
			document.getElementById(div_self_id).style.display = 'none';
		if ( div_other_id != undefined )
			document.getElementById(div_other_id).style.display = '';
	}else
		return false;
}

// 確認是否要刪除
function chk_del( url, field, value ){
	if ( confirm( "您確定要刪除此筆資料?\n該筆所有相關資料將會一併刪除" ) ){
		goto_param( url, field, value );
	}else
		return false;

}

// 確認是否要多筆刪除
function chk_delall( thisForm ){
	alert( "正準備進行選取資料的刪除動作!!" );
	if ( confirm( "再次確定要刪除資料嗎?\n所有相關資料將會一併刪除" ) ){
		thisForm.action.value = "delall";
		thisForm.submit();
		return true;
	}else
		return false;
}

// 確認是否要更新
function chk_updateall( thisForm ){
	alert( "正準備進行選取資料的更新動作!!" );
	if ( confirm( "再次確定要更新資料嗎?" ) ){
		thisForm.action.value = "updateall";
		return true;
	}else
		return false;
}

// 檢查空白值, thisForm:form的obj, field:欄位名稱(aaa,bbb), name:顯示文字(xxx,yyy), fckeditor:如果在判斷是否為空值的欄位是使用fckeditor時,需再額外填入欄位名稱
function check( thisForm, field, name, fckeditor ){
	var strfckeditor = "";
	if ( fckeditor != undefined ){
		strfckeditor = fckeditor.split(",");
		for ( inti = 0; inti < strfckeditor.length; inti++){
			var oEditor = FCKeditorAPI.GetInstance( strfckeditor[inti] ) ;
			oEditor.UpdateLinkedField();
		}
	}

	var alertStr = "";
	if ( ( field != undefined ) && ( name != undefined ) ){
		var strfield = field.split( "," );
		var strname  = name.split( "," );

		for ( inti = 0; inti < strfield.length; inti++ ){
			var chkfckeditor = 0;
			for ( intj = 0; intj < strfckeditor.length; intj++ ){
				if ( strfckeditor[intj] == strfield[inti] ){
					chkfckeditor = 1; //如果此欄位為fckeditor時, 則為1
				}
			}
			var strchk = "trim(thisForm." + strfield[inti] + ".value)";

			if ( chkfckeditor == 1 ){
				if ( eval(strchk) == '<br type="_moz" />' || eval(strchk) == '<br />' )
					alertStr += strname[inti] + " 不能為空白!! \n";
				else
					alertStr += check_null( eval(strchk), strname[inti] );
			}else
				alertStr += check_null( eval(strchk), strname[inti] );
				
		}
	}

	if ( alertStr != '' ){
	    alert( alertStr );
	    return false;
	}else{
		thisForm.chkpass.value = 1;
		return true;
	}

}

// 檢查email是否合法 val:欄位值, name:顯示文字
function check_email( val ,name ){
	if ( name == undefined )
		name = "電子信箱";
	var len = val.length;
	if( len == 0 )
		return name + " 不能為空白!!\n";

	var re = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	if ( !re.test(val) )
		return name + " 不合法!!\n";
	return '';
}

// 檢查帳號 val:欄位值, name:顯示文字
function check_account( val ,name ){
	if ( name == undefined )
		name = "帳號";
	var len = val.length;
	if( len == 0 )
		return name + " 不能為空白!!\n";
	var re = /[^a-zA-Z0-9]+/;
	if ( re.test(val) )
		return name + " 限用英文，數字組合!!\n";
	return "";
}


// 檢查是否為數值, num:變數, name:顯示中文名稱, $chkblank:是否檢查空白值
function check_number( num, name, chkblank ){
	var len = num.length;
	if ( chkblank == 1 ){
		if ( len == 0 )
			return name + " 不能為空白!!\n"
	}
	for ( var i = 0; i < len; i++ ){
		var c = num.charAt(i);
		if ( !( c >= 0 && c <= 9 ) )
			return name + " 只能是數字!!\n";
	}
	return "";
}

// 檢查密碼和確認密碼是否輸入正確
function check_password( pw1, pw2 ){
	if ( pw1 == '' ) {
		return "密碼 不能為空白!!\n";
	}
	var re = /[^a-zA-Z0-9]+/;
	if ( re.test(pw1) )
		return "密碼 限用英文，數字組合!!\n";

	if ( pw1.length < 6)
		return "密碼 長度必須大於 6 個字母!!\n";
	if ( pw1 != pw2 )
		return "密碼 二次輸入不一樣,請重新輸入 !\n";
	return "";
}
// 檢查密碼和確認密碼是否輸入正確
function check_password( pw1, pw2 ){
	if ( pw1 == '' ) {
		return "密碼 不能為空白!!\n";
	}
	var re = /[^a-zA-Z0-9]+/;
	if ( re.test(pw1) )
		return "密碼 限用英文，數字組合!!\n";

	if ( pw1.length < 6)
		return "密碼 長度必須大於 6 個字母!!\n";
	if ( pw1 != pw2 )
		return "密碼 二次輸入不一樣,請重新輸入 !\n";
	return "";
}

// 檢查新密碼和確認新密碼是否輸入正確
function check_newpassword( oldpw, pw1, pw2 ){
	if ( oldpw.length != 0 ){
		if ( pw1 == '' ) {
			return "新密碼 不能為空白!!\n";
		}
		var re = /[^a-zA-Z0-9]+/;
		if ( re.test(pw1) )
			return "密碼 限用英文，數字組合!!\n";

		if ( pw1.length < 6)
			return "新密碼 長度必須大於 6 個字母!!\n";
		if ( pw1 != pw2 )
			return "新密碼 二次輸入不一樣,請重新輸入!!\n";
		return "";
	}else
		return "";
}
// 檢查核取方塊
function check_checkbox( obj ,name ){
	var len = obj.length;
	if ( obj.length == undefined ){
		if ( obj.checked == false ){
			return name + ' 必須選取!!\n';	
		}
	}else{
		var chk = 0;
		for ( var i = 0; i < obj.length; i++ ){
			if ( obj[i].checked == true ){ chk = 1; }
		}
		if ( chk == 0 )
			return name + ' 必須選取!!\n';	
	}
	return "";
}
function check_radio( obj ,name ){
	var len = obj.length;
	if ( obj.length == undefined ){
		if ( obj.checked == false ){
			return name + ' 必須選取!!\n';	
		}
	}else{
		var chk = 0;
		for ( var i = 0; i < obj.length; i++ ){
			if ( obj[i].checked == true ){ chk = 1; }
		}
		if ( chk == 0 )
			return name + ' 必須選取!!\n';	
	}
	return "";
}

// 檢查行動電話是否輸入正確, num:變數, name:顯示中文名稱, $chkblank:是否檢查空
function check_mobile( val, name, chkblank){
	if ( name == undefined ) name = '行動電話';
	var len = val.length;
	if ( chkblank == 1 ){
		if ( len == 0 )
			return name + " 不能為空白!!\n"
	}
	if ( ( ( val.substring(0,2) != '09' ) ) || ( val.length != 10 ) ){
		return name + " 格式錯誤!!\n";	
	}
	return "";
}

function get_value_checked_radio( obj ){
	var len = obj.length;
	if ( obj.length == undefined ){
		if ( obj.checked == true ){
			return obj.value;	
		}
	}else{
		for ( var i = 0; i < obj.length; i++ ){
			if ( obj[i].checked == true ){ return obj[i].value; }
		}
	}
	return "";
}
// 全選或全不選 
function chkall( thisForm, checkbox ){
    var objForm = document.forms[thisForm.name];
    var objLen = objForm.length;
    for ( var iCount = 0; iCount < objLen; iCount++ ){
        if ( checkbox.checked == true ){
            if ( objForm.elements[iCount].type == "checkbox" ){
                objForm.elements[iCount].checked = true;
            }
        }else{
            if ( objForm.elements[iCount].type == "checkbox" ){
                objForm.elements[iCount].checked = false;
            }
        }
    }
}

// 頁數轉換, url:連結網頁, param:原本的網頁參數值, value:要轉換的頁數
function page_change( url, param, value ){
	var strlink = "";
	if ( param == '' )
		strlink = "?pagenum=" + value;
	else{
		arr_param = param.split('&');
		for ( var i = 0; i < arr_param.length; i++ ){
			if ( arr_param[i].substr( 0, 8 ) != 'pagenum=' ){
				strlink += ( strlink == '' ? '' : '&' ) + arr_param[i];	
			}
		}
		strlink = '?' + strlink + '&pagenum=' + value;
	}
	location.href = url + strlink;
}
// 重新排序
function chk_sort( thisForm ){
	if ( confirm("確定再重新排序?") ){
		thisForm.action.value = "sort";
		return true;
	}else
		return false;
}
// 清除空白 stringToTrim:字串
function trim( stringToTrim ) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim( stringToTrim ) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim( stringToTrim ) {
	return stringToTrim.replace(/\s+$/,"");
}

function check_search( val ){
	var alertStr = '';
	if ( val == '' )
		alertStr += '搜尋 不可為空白!!\n';
	if ( val == '%' )
		alertStr += '搜尋 不可為%字元!!\n'

	if ( alertStr == '' ){
		return true;
	}else{
	    alert(alertStr);
		return false;
	}
	return true;
}

// 縮放
function toggle ( id ){
	var obj = document.getElementById(id)

	if ( obj.style.display == 'none' ){
        	obj.style.display = '';           
        }else{    
           obj.style.display = 'none';
	}
		
}
function check_idnumber( id , field,  chkgender, gender ){
	if (　field == undefined ){ 	field = '身份證字號'; }
	if ( id.length != 10 )
		return field + ' 不合法!!\n';
	
	var chk = 0;
	
	for ( var i = 1; i < 10; i++ ){
		if ( check_number(id.charAt(i), '身份證字號') != '' )
			chk = 1;
	}

	if ( chk == 1 )
		return field + ' 不合法!!\n';
	var id2 = '';
	str_city = id.charAt(0).toUpperCase();
	
	switch (str_city){
		case 'A':
			id2 = '10';
			break;
		case 'B':
			id2 = '11';
			break;
		case 'C':
			id2 = '12';
			break;
		case 'D':
			id2 = '13';
			break;
		case 'E':
			id2 = '14';
			break;
		case 'F':
			id2 = '15';
			break;
		case 'G':
			id2 = '16';
			break;
		case 'H':
			id2 = '17';
			break;
		case 'I':
			id2 = '34';
			break;
		case 'J':
			id2 = '18';
			break;
		case 'K':
			id2 = '19';
			break;
		case 'L':
			id2 = '20';
			break;
		case 'M':
			id2 = '21';
			break;
		case 'N':
			id2 = '22';
			break;
		case 'O':
			id2 = '35';
			break;
		case 'P':
			id2 = '23';
			break;
		case 'Q':
			id2 = '24';
			break;
		case 'R':
			id2 = '25';
			break;
		case 'S':
			id2 = '26';
			break;
		case 'T':
			id2 = '27';
			break;
		case 'U':
			id2 = '28';
			break;
		case 'V':
			id2 = '29';
			break;
		case 'W':
			id2 = '32';
			break;
		case 'X':
			id2 = '30';
			break;
		case 'Y':
			id2 = '31';
			break;
		case 'Z':
			id2 = '33';
			break;
	}
	
	for ( var i = 1; i < 10; i++ ){
		id2 += id.charAt(i);
	}

	var total = 0;
	
	total += parseInt(id2.charAt(0));
	total += parseInt(id2.charAt(1)) * 9;
	total += parseInt(id2.charAt(2)) * 8;
	total += parseInt(id2.charAt(3)) * 7;
	total += parseInt(id2.charAt(4)) * 6;
	total += parseInt(id2.charAt(5)) * 5;
	total += parseInt(id2.charAt(6)) * 4;
	total += parseInt(id2.charAt(7)) * 3;
	total += parseInt(id2.charAt(8)) * 2;
	total += parseInt(id2.charAt(9)) * 1;
	
	var chk_number =  10 - ( total % 10 );
	if ( chk_number ==  10 )
		chk_number = 0;

	if ( parseInt(id2.charAt(10), 10) != chk_number )
		return field + ' 不合法!!\n';

	if ( ( chkgender == 1 ) && ( ( gender == '1' ) || ( gender == '0' ) ) ){
		 if ( gender == '1' ){
			 if ( id.charAt(1) != '1' )
			 	return field + ' 不合法!!\n';
		 }else if ( gender == '0' ){
			 if ( id.charAt(1) != '2' )
			 	return field + ' 不合法!!\n';
		 }
	}
	return '';

}
function goto_back(){
	history.back();
}
function check_sr_search( thisForm ){
	var alertStr = '';
	var nochoose = true;
	var type   = thisForm.sr_type.value;
	var gender = thisForm.sr_gender.value;
	var city   = thisForm.sr_city.value;
	var area   = thisForm.sr_area.value;

	var specialty = document.getElementsByName('sr_specialty[]');
	var age = document.getElementsByName('sr_age[]');
	var str = type + '.php?type=' + type;
	
	if ( gender != '' ){ str += '&sr_gender=' + gender; nochoose = false; }
	if ( city != '' ){ str += '&sr_city=' + encodeURI(city); nochoose = false; }
	if ( area != '' ){ str += '&sr_area=' + encodeURI(area); nochoose = false; }
	
	if ( check_checkbox( specialty, '服務需求' )	== '' ){
		var str_specialty = '';
		for ( var i = 0; i < specialty.length; i++ ){
			if ( specialty[i].checked == true ){
				str_specialty += ( str_specialty == '' ) ? specialty[i].value : ',' + specialty[i].value;
			}
		}
		str += '&sr_specialty=' + encodeURI(str_specialty);
		nochoose = false;
	}

	var chk_age = 0;
	for ( var i = 0; i < age.length; i++ ){
		if ( age[i].checked == true && age[i].value == age.length ){
			chk_age = 1;
		}
	}
	if ( chk_age == 0 ){
		if ( check_checkbox( age, '年齡限制' ) == '' ){
			var str_age = '';
			for ( var i = 0; i < age.length; i++ ){
				if ( age[i].checked == true ){
					str_age += ( str_age == '' ) ? age[i].value : ',' + age[i].value;
				}
			}
			str += '&sr_age=' + encodeURI(str_age);
			nochoose = false;
		}
	}
	if ( nochoose == true )
		alertStr += '至少選擇一項搜尋條件!!\n';	

	if ( alertStr != '' )
		alert(alertStr);
	else{
		goto(str);
	}

	return false;
}
function change_sr_age( obj ){
	var age = document.getElementsByName('sr_age[]');
	var num = 0;
	for ( var i = 0; i < age.length; i++ ){
		if ( parseInt(obj.value, 10) == age.length ){
			if ( age[i].value != age.length ){ age[i].checked = false; }
		}else{
			if ( parseInt(age[i].value, 10 ) == age.length ){
				age[i].checked = false;
			}else{
				if ( age[i].checked == true ) num++;
			}
		}
	}
	if ( num == age.length - 1 ){
		for ( var i = 0; i < age.length; i++ ){
			if ( age[i].value != age.length ){ age[i].checked = false; }
			if ( age[i].value == age.length ){ age[i].checked = true; }
		}
	}
}