/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
將頁面中某編輯欄位, 置換成純文字
(目前只提供select-one, text, checkbox 其他請依需要自行加入)
[Parameter]:
	dataObj: 要處理的HTML Object對象
    dataFormat:(OPTION) 指定要格式化的方式. "D": 日期, "S": 千分號
*/
function convertObjToText(dataObj, dataFormat) {
    if (dataObj == null) {
    	return false;
    }
	var dataType = dataObj.type;
	var dataValue = null;
	if (dataType == "select-one") {
		dataValue = dataObj.options[dataObj.selectedIndex].text;
	} else if (dataType == "text") {
		dataValue = dataObj.value;
		if (dataType != null) {
			if (dataFormat == "D") {
				dataValue = dateFormat(dataValue);
				var elem = dataObj.nextSibling;
				while(elem != null)
				{
					if (elem.nodeName == "IMG") {
						elem.style.display = "none";
						break;
					}
					elem = elem.nextSibling;
				}
			}
			if (dataFormat == "S") {
				dataValue = thousandFormat(dataValue);
			}
		}
	} else if (dataType == "checkbox" || dataType == "radio") {

		if (dataObj.checked) {
			dataValue = "(v)";
		} else {
			dataValue = "( )";
		}

		dataObj.style.display = "none";
	}
	if (dataValue != null) {
		var textNode = document.createTextNode(dataValue);
		dataObj.parentNode.insertBefore(textNode,dataObj);
		dataObj.style.display = "none";
	}
	return true;
}

/**
將頁面中某表單所有編輯的欄位, 置換成純文字
(目前只提供select-one, text, checkboxk, 其他請依需要自行加入)
[Parameter]:
	fm: 要處理的Form對象
*/
function convertAllEditToText(fm) {
	if (fm == null) {
		fm = document.forms[0];
	}
	var dataObj, dataFormat, dataType, dataValue;

	for (i = 0; i < fm.elements.length; i++) {
		dataObj = fm.elements[i];
		if (dataObj.style.display == "none") {
			continue;
		}
		dataFormat = "";
		if (!isEmpty(dataObj.id)) {
			pos = dataObj.id.indexOf("|");
			if (pos > -1) {
				dataFormat = dataObj.id.substring(pos+1, dataObj.id.length);
			}
		}
		convertObjToText(dataObj, dataFormat);
	}
}

/**
將頁面中某表指定的編輯欄位, 置換成純文字
(目前只提供select-one, text, checkbox, 單一radio, 其他請依需要自行加入)
[Parameter]:
	fm: 要處理的Form對象
*/
function convertApointEditToText(fm, fdAry) {
	if (fm == null) {
		fm = document.forms[0];
	}
	var dataName;
	var dataFormat;
	var dataObj;
	for (i in fdAry) {
		dataName = fdAry[i];
		dataFormat = "";
		if ((pos = dataName.indexOf("|")) > -1) {
			dataFormat = dataName.substring(pos+1, dataName.length);
			dataName = dataName.substring(0,  pos);
		}

		var dataList = document.getElementsByName(dataName);
		if (dataList.length > 0) {
			for (j in dataList) {
				dataObj = dataList[j];
				convertObjToText(dataObj, dataFormat);
			}
		} else {
			dataObj = eval("fm."+ dataName +";");
			convertObjToText(dataObj, dataFormat);
		}
	}
}

function isEmpty(str) {
    if (str == "" || str == "　") {
        return true;
    }
    return false;
}

function NewWindow(mypage, myname, w, h, scroll) {
  var winl=(screen.width - w) / 2;
  var wint=(screen.height - h) / 2;
  winprops='height='+h+',width='+w+',top='+wint+',left='+winl+',toolbar=no,menubar=no,scrollbars=' + scroll + ',resizable=no';
  win = window.open(mypage, myname, winprops);
  if (parseInt(navigator.appVersion) >= 4) {
    win.window.focus();
  }
  return win;
}

function checkSSN(m_sSSN) {
	var m_ichkDigit = "1987654321";
	var m_iEnglishCode = new Array();
	var m_iCode = new Array();
	var i, m_iTotal = 0, m_iCheck;

	if(m_sSSN.length != 10){
		alert("'身分證號' 須為10位");
		return false;
	}
	m_sSSN = m_sSSN.toUpperCase();
	if(!IsUpperCharacter(m_sSSN.charAt(0))){
		alert("'身分證號' 第1位須為文字");
		return false;
	}
	if((m_sSSN.charAt(1) != "1") && (m_sSSN.charAt(1) != "2")){
		alert("'身分證號' 第2位須為 '1' 或 '2'");
		return false;
	}
	if(!IsNumeric(m_sSSN.substring(1))){
		alert("'身分證號' 後9位須為數字");
		return false;
	}
	m_iEnglishCode[0] = 10;
	m_iEnglishCode[1] = 11;
	m_iEnglishCode[2] = 12;
	m_iEnglishCode[3] = 13;
	m_iEnglishCode[4] = 14;
	m_iEnglishCode[5] = 15;
	m_iEnglishCode[6] = 16;
	m_iEnglishCode[7] = 17;
	m_iEnglishCode[8] = 34;
	m_iEnglishCode[9] = 18;
	m_iEnglishCode[10] = 19;
	m_iEnglishCode[11] = 20;
	m_iEnglishCode[12] = 21;
	m_iEnglishCode[13] = 22;
	m_iEnglishCode[14] = 35;
	m_iEnglishCode[15] = 23;
	m_iEnglishCode[16] = 24;
	m_iEnglishCode[17] = 25;
	m_iEnglishCode[18] = 26;
	m_iEnglishCode[19] = 27;
	m_iEnglishCode[20] = 28;
	m_iEnglishCode[21] = 29;
	m_iEnglishCode[22] = 32;
	m_iEnglishCode[23] = 30;
	m_iEnglishCode[24] = 31;
	m_iEnglishCode[25] = 33;

	m_iCode[0] = Math.floor(m_iEnglishCode[parseInt(m_sSSN.charCodeAt(0))-65] / 10);
	m_iCode[1] = (m_iEnglishCode[parseInt(m_sSSN.charCodeAt(0))-65]) % 10;
	for(i=1; i<9; i++){
		m_iCode[i+1] = parseInt(m_sSSN.charAt(i));
	}
	for(i=0; i<10; i++){
		m_iTotal += m_iCode[i] * parseInt(m_ichkDigit.charAt(i));
	}
	m_iCheck = (10 - (m_iTotal % 10)) % 10;
	if(m_iCheck != parseInt(m_sSSN.charAt(9))){
		alert("身分證號碼不合法");
		return false;
	}
	return true;
}

function IsUpperCharacter(m_string) {
	for(var i=0; i<m_string.length; i++){
		if((m_string.charAt(i) >= "A") && (m_string.charAt(i) <= "Z"))
			continue;
		else
			return false;
	}
	return true;
}

function IsNumeric(m_string) {
	if(m_string.length == 0)
		return false;
	try{
		var intValue = new Number(m_string);
		if(isNaN(intValue)){
			return false;
		}
	}
        catch(e){
                return false;
        }
	return true;
}

function getDateInput(yName, mName, dName, valY, valM, valD) {
    var today = new Date();
    var cy = today.getFullYear();
    var cm = today.getMonth()+1;
    var cd = today.getDate();
    if (isEmpty(valY)) valY = cy-28;
    if (isEmpty(valM)) valM = 1;
    if (isEmpty(valD)) valD = 1;
    document.write('<select name="'+ yName +'">');
    document.write('<option value="">--</option>');
    getIntOption(0, cy-99, cy, valY, 4);
    document.write('</select>年');
    document.write('<select name="'+ mName +'">');
    document.write('<option value="">--</option>');
    getIntOption(0, 1, 12, valM, 2);
    document.write('</select>月');
    document.write('<select name="'+ dName +'">');
    document.write('<option value="">--</option>');
    getIntOption(0, 1, 31, valD, 2);
    document.write('</select>日');
}

function getDateInput1(yName, mName, dName, defDate) {
   var p1 = defDate.indexOf("-");
   var p2 = defDate.lastIndexOf("-");
   var valY = defDate.substring(0,p1);
   var valM = defDate.substring(p1+1,p2);
   var valD = defDate.substring(p2+1,defDate.length);
   getDateInput(yName, mName, dName, valY, valM, valD);
}

function getIntOption(base, start, end, def, len) {
    var chked = "";
    var no = "";
    for (var i = (base+start); i <= (base+end); i++) {
        no = (isEmpty(len))? i:addLeftZero(new String(i), len);
        chked = (no == def)? "selected" : "";
        document.write('<option value="'+ no +'" '+ chked +'>'+ no +'</option>');
    }
}

function addLeftZero(str, len) {
    while (str.length < len) {
        str = "0" + str;
    }
    return str
}

function getDateInputValue(fm, yName, mName, dName) {
    return eval("'"+ getSelectValue(fm, yName) +"-"+ getSelectValue(fm, mName) +"-"+ getSelectValue(fm, dName) +"'");
}

function getSelectValue(fm, inputName) {
    var result = eval("fm."+ inputName +".options[fm."+inputName+".selectedIndex].value;");
    return result;
}

function setCookie(name, value, expiry, path, domain, secure)
{
    /* expiry 單位是小時 */
    var nameString = name + "=" + value;
    var date=new Date();
    if(expiry!=null)
        date.setTime(date.getTime() + expiry * 3600 * 1000);
    var expiryString = (expiry == null) ? "" : " ;expires = "+ date.toGMTString();
    var pathString = (path == null) ? "" : " ;path = "+ path;
    var domainString = (path == null) ? "" : " ;domain = "+ domain;
    var secureString = (secure) ?";secure" :"";
   
    document.cookie = nameString + expiryString + pathString + domainString + secureString;
}

function deleteCookie(name)
{
    var expires = new Date();
    expires.setTime (expires.getTime() - 1);

    setCookie( name , "Delete Cookie", expires,null,null,false);
}