// #############################################################################
// get left position of elm
function confirm_action(msg,action)
{
	if (confirm(msg))
		window.location = action;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function get_select_value(select_obj){
	var the_select = select_obj;
	var the_index = the_select.selectedIndex;
	var the_selected = the_select.options[the_index].text;
	var the_selected_value = the_select.options[the_index].value;
	return the_selected_value;
}

// Calendar
function selectHandler(cal, date)
{
  cal.sel.value = date;
  if (cal.dateClicked)
    cal.callCloseHandler();
}

function closeHandler(cal)
{
  cal.hide();
  calendar = null;
}

function showCalendar(obj)
{
 	var calendar = new Calendar(0, null, selectHandler, closeHandler); 	
  calendar.showsTime = false;
  calendar.showsOtherMonths = true; 
  calendar.create();
  calendar.setDateFormat('%Y-%m-%d');
  calendar.parseDate(obj.value);
  calendar.sel = obj;
  calendar.showAtElement(obj.nextSibling, "Br");
}

function calendar(){
	var mdays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);	
	var now = new Date();
	var year = now.getFullYear();
	var today = now.getDate();
	
	if (((year % 4 ==0) && (year%100!=0)) || (year % 400==0)) mdays[1] = 29;
	ndays = mdays[now.getMonth()];
	firstDay = now;
	firstDay.setDate(1);
	startDay = firstDay.getDay();
	
	document.write("<center><table border cellspacing=0>");
	document.write("<tr><th colspan = 7>" + year + " 年 " + (now.getMonth()+1)+" 月<tr><th>" + "<font color=red> 週日"+
		"</font><th>週一<th>週二<th>週三<th>週四<th>週五<th>"+
		"<font color=blue>週六</font></tr><tr>");
	var column = 0;
	for (i=0; i<startDay; i++){
		document.write("<td>&nbsp;</td>");
		column++;
	}
	for(i=1; i<=ndays;i++){
		if (column==7){
			document.write("</tr><tr>");
			column = 0;
		}
		
		if (i==today)
			document.write("<td align='center' bgcolor='pink'>");
		else
			document.write("<td align=center>");
		document.write(i);
		column++;
	}
	document.write("</tr></table></center>");
}

// ------------------------------------------------------------
//身份證字號檢查器 - 累加檢查碼
function getPID_SUM(sPID) {
	var iChkNum = 0;

	// 第 1 碼
	iChkNum = ALP_STR.indexOf(sPID.substr(0,1)) + 10;
	iChkNum = Math.floor(iChkNum/10) + (iChkNum%10*9);

	// 第 2 - 9 碼
	for(var i=1; i<sPID.length-1; i++) {
		iChkNum += sPID.substr(i,1) * (9-i);
	}

	// 第 10 碼
	iChkNum += sPID.substr(9,1)*1;

	return iChkNum;
}

// ------------------------------------------------------------
// 身分證字號檢查器 - 檢查合法字元
function chkPID_CHAR(sPID) {
	var sMsg = "";
	//sPID = trim(sPID.toUpperCase());
	var iPIDLen = String(sPID).length;

	var sChk = ALP_STR + NUM_STR;
	for(i=0;i<iPIDLen;i++) {
		if (sChk.indexOf(sPID.substr(i,1)) < 0) {
			sMsg = "這個身分證字號含有不正確的字元！";
			break;
		}
	}

	if (sMsg.length == 0) {
		if (ALP_STR.indexOf(sPID.substr(0,1)) < 0) {
			sMsg = "身分證字號第 1 碼應為英文字母(A~Z)。";
		} else if ((sPID.substr(1,1) != "1") && (sPID.substr(1,1) != "2")) {
			sMsg = "身分證字號第 2 碼應為數字(1~2)。";
		} else {
			for(var i=2; i<iPIDLen; i++) {
				if (NUM_STR.indexOf(sPID.substr(i, 1)) < 0) {
					sMsg = "第 " + (i+1) + " 碼應為數字(0~9)。";
					break;
				}
			}
		}
	}

	if (sMsg.length != 0) {
		alert(sMsg);
		return false;
	} else {
		return true;
	}
}

// 身分證字號檢查器
function CheckPID(sPID) {
	var sMsg = '';
	if (sPID == '') {
		sMsg = "請輸入身分證字號";
	} else if (sPID.length != 10) {
		sMsg = "長度應為 10 ！";
	} else {
		sPID = trim(sPID.toUpperCase());
		if (!chkPID_CHAR(sPID)) return;

		var iChkNum = getPID_SUM(sPID);

		if (iChkNum % 10 != 0) {
			var iLastNum = sPID.substr(9, 1) * 1;
			for (i=0; i<10; i++) {
				var xRightAlpNum = iChkNum - iLastNum + i;
				if ((xRightAlpNum % 10) ==0) {
					//sMsg = "最後一個數應為：" + i;
					sMsg = "身份證字號錯誤";
					break;
				}
			}
		}
	}
	
	if (sMsg == '')
		return true;
	else{
		alert(sMsg);
		return false;
	}		
}
