
function getToday() {
  //得到今天的年,月,日
  this.now = new Date();
  this.year = this.now.getFullYear();
  this.month = this.now.getMonth();
  this.day = this.now.getDate();
}
// ====================================================
// 字串取代
// Input:
//   str:輸入字串
//   org:原字串
//   des:欲取代字串
// Output:
//   取代後字串
// ====================================================
function str_replace(str,org,des) {
  while (str.indexOf(org)>=0) {
    str=str.substring(0,str.indexOf(org))+des+str.substring(str.indexOf(org)+org.length,str.length);
  }
  return str;
}

// ====================================================
// 數字num不滿digit位數時補"0"
// Input:
//   num:數字
//   digit:位數
// Output:
//   補"0"後字串
// ====================================================
function addZero(num,digit) {
  num=num+"";
  while (num.length<digit) {
    num="0"+num;
  }
  return num;
}

// ====================================================
// 字串轉數字（能修正數字有補"0"的轉換問題）
// Input:
//   str:字串型態值
// Output:
//   數字型態值
// ====================================================
function str2num(str) {
  str=strSubZero(str);
  if (str.length==0) {str=0;}
  return parseFloat(str);
}

// ====================================================
// 去除數字前面補零者（以便於修正數字有補"0"的轉換問題）
// Input:
//   str:原字串
// Output:
//   去零字串
// ====================================================
function strSubZero(str) {
  str+="";
  while (str.indexOf("0")==0) {
    str=str.substring(1,str.length);
  }
  if ((str.indexOf(".")==0) || (str=="")) {str="0"+str;}
  return (str);
}

// ====================================================
// 測試是否為數字
// Input:
//   str:檢測字串
// Output:
//   true/false
// ====================================================
function isNum1(str) {
  return (!isNaN(parseFloat(strSubZero(str))));
}

// ====================================================
// 自動產生start到end之下拉式選單
// Input:
//   itemName:欄位名稱
//   start:起始數字
//   end:結束數字
//   defaultValue:預設值
//   formName:表單名稱
// Output:無
// ====================================================
function writeOptions(itemName,start,end,defaultValue,formName) {
  document.write("<select name="+itemName+">");
  for (i=start; i<=end; i++) {
    document.write("<option value='"+i+"'>"+i+"</option>");
  }
  document.write("</select>");
  presetSelect(itemName,defaultValue,formName)
}

// ====================================================
// 自動設定下拉式選單select預設值
// Input:
//   itemName:欄位名稱
//   defaultValue:預設值
//   formName:表單名稱
// Output:無
// ====================================================
function presetSelect(itemName,defaultValue,formName) {
  if (defaultValue.length==0)
    return;
  itemName=(formName==null) ? eval("document.DataInput."+itemName):eval("document."+formName+"."+itemName);
  for (i=0;i<itemName.length;i++) {
    if (itemName[i].value==defaultValue) {
      itemName[i].selected=true;
      break;
    }
  }
}

// ====================================================
// 自動設定單選選項radio預設值
// Input:（同上）
// Output:無
// ====================================================
function presetRadio(itemName,defaultValue,formName) {
  if (defaultValue.length==0)
    return;
  itemName=(formName==null) ? eval("document.DataInput."+itemName):eval("document."+formName+"."+itemName);
  for (i=0;i<itemName.length;i++) {
    if (itemName[i].value==defaultValue) {
      itemName[i].checked=true;
      break;
    }
  }
}

// ====================================================
// 自動設定核選方塊checkbox預設值
// Input:（同上）
// Output:無
// ====================================================
function presetCheckbox(itemName,defaultValue,formName) {
  if (defaultValue.length==0)
    return;
  itemName=(formName==null) ? eval("document.DataInput."+itemName):eval("document."+formName+"."+itemName);
  if (itemName.value==defaultValue) {
    itemName.checked=true;
  }
}
// ====================================================
// 自動設定核選方塊checkbox預設值(選項須為二進位值1.2.4.8.16...等)
// Input:（同上）
// Output:無
// ====================================================
function presetCheckbox1(itemName,defaultValue,formName) {
  if (defaultValue.length==0)
    return;
  itemName=(formName==null) ? eval("document.DataInput."+itemName):eval("document."+formName+"."+itemName);
  for (i=0;i<itemName.length;i++) {
    	if ((itemName[i].value&defaultValue)>0) {
           itemName[i].checked=true;
    	}
  }
}
// ====================================================
// 同屬性核選方塊checkbox
// Input:
//   itemName:欄位名稱
//   numberFromTo:欄位值例：1,2,3,(畫面值須從1開始
//   formName:表單名稱
// Output:無
// ====================================================
function selectCheckbox(itemName,numberFromTo,formName) {
  if (numberFromTo.length==0)
    return;
  var tempArray=numberFromTo.split(",");
  for(i=0;i<tempArray.length;i++) {
    j=tempArray[i]-1;
    itemObject=(formName==null) ? eval("document.DataInput."+itemName+i):eval("document."+formName+"."+itemName+"("+j+")");
    itemObject.checked=true;
  }
}
// ====================================================
// 同屬性核選方塊checkbox
// Input:
//   itemName:欄位
//   numberFromTo:欄位的值 1,2,9,10,
//   formName:表單名稱
// Output:無
// ====================================================
function selectCheckbox1(itemName,numberFromTo,formName) {
  if (numberFromTo.length==0)
    return;
  var tempArray=numberFromTo.split(",");
  var val_len = tempArray.length;
  var item_len =0;
  var itemvalue = eval("document."+formName+"."+itemName).value;
  if (itemvalue==null)
  	item_len = eval("document."+formName+"."+itemName).length;

  for(i=0;i<val_len;i++) {
    val = tempArray[i];
  	for(j=0;j<item_len;j++) {
    	itemObject=(formName==null) ? eval("document.DataInput."+itemName+"("+j+")"):eval("document."+formName+"."+itemName+"("+j+")");
  		if (itemObject.value==val) {
	    	itemObject.checked=true;
	    	j=item_len;
	    }
	}
	if (item_len==0) {
  		if (itemvalue==val) {
	    	eval("document."+formName+"."+itemName).checked=true;
	    }
  	}

  }
}
// ====================================================
// 一次檢查數個欄位是否有值
// Input:
//   itemNameList:欄位名稱（例："title,email,address"）
//   formName:表單名稱
// Output:
//   true/false
// ====================================================
function chkTXT(itemNameList,formName) {
  itemNameList=itemNameList.split(",");
  for (i=0;i<itemNameList.length;i++) {
    itemName=(formName==null) ? eval("document.DataInput."+itemNameList[i]):eval("document."+formName+"."+itemNameList[i]);
    if (itemName.value=="") {
      return false;
      break;
    }
  }
  return true;
}

// ====================================================
// e-mail格式檢查
// Input:
//   email:e-mail字串
// Output:
//   true/false
// ====================================================
function chkEmail(email) {
  var chkList=new Array("@",".");
  if (email.length>0) {
    for (i=0;i<chkList.length;i++) {
      if (email.indexOf(chkList[i])<0) {return false;}
    }
  }
  return true;
}

// ====================================================
// 關閉視窗
// Input:
//   winName:視窗名稱（非必要）
// Output:無
// ====================================================
function closeMe(winName) {
  winName=(winName==null) ? "self":winName;
  eval(winName+".close()");
}

// ====================================================
// 檢查欄位資料時，顯示錯誤訊息並跳至該欄位
// Input:
//   itemName:欄位名稱
//   msg:錯誤訊息
//   formName:表單名稱
// Output:false
// ====================================================
function setItemFocus(itemName,msg,formName) {
  alert(msg);
  itemName=(formName==null) ? eval("document.DataInput."+itemName):eval("document."+formName+"."+itemName);
  itemName.focus();
  return false;
}

// ====================================================
// 點選某欄位時清除該欄位內容
// Input:
//   itemName:欄位DOM名稱（填入this即可）
// Output:無
// ====================================================
function clearItem(itemName) {
  if (itemName.value==itemName.defaultValue) {itemName.value="";}
}

// ====================================================
// 自一路徑檔名字串中取得檔名（適合於<input type="file">時使用）
// Input:
//   fileStr:檔案與路徑字串
// Output:檔名
// ====================================================
function getFilename(fileStr) {
  if (fileStr=="") {return "";}
  else {return fileStr.substring(fileStr.lastIndexOf("\\")+1,fileStr.length);}
}

// ====================================================
// 自一路徑檔名字串中取得副檔名
// Input:
//   fileStr:檔案與路徑字串
// Output:副檔名
// ====================================================
function getFilenameExt(fileStr) {
  if (fileStr=="") {return "";}
  else {return fileStr.substring(fileStr.lastIndexOf("."),fileStr.length);}
}

// ====================================================
// 檢查日期格式是否正確
// Input:
//   itemName:欄位名稱
//   defaultValue:預設值
//   formName:表單名稱
//   dFormat:格式（null,1:YYMMDD、2:YYYYMMDD）
// Output:無
// ====================================================
function chkDate(itemName,defaultValue,formName,dFormat) {
  itemName=(formName==null) ? eval("document.DataInput."+itemName):eval("document."+formName+"."+itemName);

}

// ====================================================
// 檢查日期格式是否正確
// Input:
//   itemName:欄位名稱
//   formName:表單名稱
// Output:錯誤訊息
// ====================================================
function chkDate(itemName,formName) {
  var itemName;
  var itemYLength;
  var itemArray=new Array("","","");
  var itemNumArange=new Array(3000,12,31);
  var itemFormat;
  var errMsg;
  var isPass;

  isPass=true;
  itemName=(formName==null) ? eval("document.DataInput."+itemName):eval("document."+formName+"."+itemName);
  itemYLength=itemName.value.length-4;
  itemFormat=(itemYLength==4) ? "YYYYMMDD":"YYYMMDD";
  errMsg="對不起，日期格式錯誤，格式為："+itemFormat
  if ((itemYLength!=3) && (itemYLength!=4)) {return errMsg;}
  itemArray[0]=itemName.value.substring(0,itemYLength);
  itemArray[1]=itemName.value.substring(itemYLength,itemYLength+2);
  itemArray[2]=itemName.value.substring(itemYLength+2,itemYLength+4);
  for (var i=0;i<itemArray.length;i++) {
    if (isNum(itemArray[i])) {
      itemArray[i]=str2num(itemArray[i]);
      if ((itemArray[i]==0) || (itemArray[i]>itemNumArange[i])) {isPass=false;}
    }
    else {
      isPass=false;
    }
  }

  if (isPass) {return "";}
  else {return errMsg;}
}
