//關閉主視窗後,所有子視窗一併關掉,目前如果是在網址列直接打入新網址,仍無法檔
var newwin=null;
window.attachEvent('onunload',function(){if(self.screenTop>9000 && newwin!=null && newwin.open)newwin.close()});
//網頁如果出現SCRIPT ERR 則可以擋掉錯誤訊息
/*
function killErrors() { 
return true; 
}
window.onerror = killErrors; 
*/
//開啟無邊視窗
function showDialog(endtarget,WINwidth, WINheight) { 
	showx = event.screenX - event.offsetX - 4 ; // + deltaX; 
	showy = event.screenY - event.offsetY + 20; // + deltaY; 
	newWINwidth = WINwidth + 4 + 18; 
	var features = 
	'dialogWidth:' + newWINwidth + 'px;' + 
	'dialogHeight:' + WINheight + 'px;' + 
	'dialogLeft:' + showx + 'px;' + 
	'dialogTop:' + showy + 'px;' + 
	'directories:no;localtion:no; menubar:no; status:no; toolbar:no;scrollbars:no;Resizeable:no;help:no;';
	return window.showModalDialog(endtarget, " ", features); 
}
//依內容自動調整視窗大小
function DialogSizeToContent(){
	window.dialogHeight=(document.body.scrollHeight+26)+"px";
}
//依內容自動調整視窗大小
function WinSizeToContent(){
	if (window.sizeToContent) 
     window.sizeToContent(); 
	else 
	    if (document.body 
	         && document.body.scrollHeight > 0 
	         && document.body.clientHeight > 0) 
	       window.resizeBy(0, 
	                       document.body.scrollHeight 
	                     - document.body.clientHeight);
}  

function WinOpenMax(){
	var xMax = screen.availWidth-10, yMax = screen.availHeight-30;
	return "width="+xMax+
				",height="+yMax+
				",screenX=0"+
				",screenY=0"+
				", top=0"+
				",left=0,toolbar=no,scrollbars=no,resizable=no,menubar=no,status=no";
}
function WinOpenMaxScrol(){
	var xMax = screen.availWidth-10, yMax = screen.availHeight-30;
	return "width="+xMax+
				",height="+yMax+
				",screenX=0"+
				",screenY=0"+
				", top=0"+
				",left=0,toolbar=no,scrollbars=yes,resizable=no,menubar=no,status=no";
}
function WinOpenMaxScrolResize(){
	var xMax = screen.availWidth-10, yMax = screen.availHeight-30;
	return "width="+xMax+
				",height="+yMax+
				",screenX=0"+
				",screenY=0"+
				", top=0"+
				",left=0,toolbar=no,scrollbars=yes,resizable=yes,menubar=no,status=no";
}
//視窗置中
function WinAdjustSize(W,H){
	if(H>(screen.height-60))H=screen.height-60;
	if (document.all)
		var xMax = screen.width, yMax = screen.height;
	else
		if (document.layers)
			var xMax = window.outerWidth, yMax = window.outerHeight;
		else
			var xMax = 640, yMax=480;	
	var xOffset = (xMax - W)/2, yOffset = (yMax - H-30)/2;
	window.resizeTo(W,H);
	window.moveTo(xOffset,yOffset);
}

//視窗置中,無scrollbar
function WinAdjustCenter(H,W){
	if(H>(screen.height-60))H=screen.height-60;
	if (document.all)
		var xMax = screen.width, yMax = screen.height;
	else
		if (document.layers)
			var xMax = window.outerWidth, yMax = window.outerHeight;
		else
			var xMax = 640, yMax=480;	
	var xOffset = (xMax - W)/2, yOffset = (yMax - H-30)/2;
	return "width="+W+
				",height="+H+
				",screenX="+xOffset+
				",screenY="+yOffset+
				", top="+yOffset+
				",left="+xOffset+",toolbar=no,scrollbars=no,resizable=no,menubar=no,status=no";
}
//視窗置中,有scrollbar	
function WinAdjustCenterScrol(H,W){
	if(H>(screen.height-60))H=screen.height-60;
	if (document.all)
		var xMax = screen.width, yMax = screen.height;
	else
		if (document.layers)
			var xMax = window.outerWidth, yMax = window.outerHeight;
		else
			var xMax = 640, yMax=480;	
	var xOffset = (xMax - W)/2, yOffset = (yMax - H-30)/2;
	return "width="+W+
				",height="+H+
				",screenX="+xOffset+
				",screenY="+yOffset+
				", top="+yOffset+
				",left="+xOffset+",toolbar=no,scrollbars=yes,resizable=no,menubar=no,status=no";
	}
//視窗置中,有scrollbar,可調大小	
function WinAdjustResizable(H,W){
	if(H>(screen.height-60))H=screen.height-60;
	if (document.all)
		var xMax = screen.width, yMax = screen.height;
	else
		if (document.layers)
			var xMax = window.outerWidth, yMax = window.outerHeight;
		else
			var xMax = 640, yMax=480;	
	var xOffset = (xMax - W)/2, yOffset = (yMax - H-30)/2;
	return "width="+W+
				",height="+H+
				",screenX="+xOffset+
				",screenY="+yOffset+
				", top="+yOffset+
				",left="+xOffset+",toolbar=no,scrollbars=yes,resizable=yes,menubar=no,status=no";
	}
//視窗置中,無scrollbar,可調大小	
function WinResizable(H,W){
	if(H>(screen.height-60))H=screen.height-60;
	if (document.all)
		var xMax = screen.width, yMax = screen.height;
	else
		if (document.layers)
			var xMax = window.outerWidth, yMax = window.outerHeight;
		else
			var xMax = 640, yMax=480;	
	var xOffset = (xMax - W)/2, yOffset = (yMax - H-30)/2;
	return "width="+W+
				",height="+H+
				",screenX="+xOffset+
				",screenY="+yOffset+
				", top="+yOffset+
				",left="+xOffset+",toolbar=no,scrollbars=no,resizable=yes,menubar=no,status=no";
	}		
	//呼叫月曆網頁
//Div作法
var jsCalendar=null;
	if(jsCalendar==null){
		jsCalendar=document.createElement('script');
		jsCalendar.src="../js/calendar.js";
		jsCalendar.type='text/javascript';
		document.getElementsByTagName("head").item(0).appendChild(jsCalendar);
	}

function showCalendar(obj){
	objDate=obj;
	if(objDate.value!=""){
		var strDate=objDate.value.replace("-","/").replace("-","/");
		calendar_Start(new Date(strDate), '', returnDate,true);
	}else{
		calendar_Start(new Date(), '', returnDate,true);
	}
	try{
		obj.focus();
	}catch(e){
	}
}


//obj=回傳日期,obj2=回傳星期幾
function showCalendar(obj,obj2){
	objDate=obj;
	if(objDate.value!=""){
		var strDate=objDate.value.replace("-","/").replace("-","/");
		calendar_Start(new Date(strDate), '', returnDate,true,obj2);
	}else{
		calendar_Start(new Date(), '', returnDate,true,obj2);
	}
}

//obj=回傳日期,obj2=回傳星期幾,obj3=限定天數
function showCalendar3(obj,obj2,obj3){
	objDate=obj;
	if(objDate.value!=""){
		var strDate=objDate.value.replace("-","/").replace("-","/");
		calendar_Start(new Date(strDate), '', returnDate,true,obj2,obj3);
	}else{
		calendar_Start(new Date(), '', returnDate,true,obj2,obj3);
	}
}

/*
var jsCalendar=null;
	if(jsCalendar==null){
		jsCalendar=document.createElement('script');
		jsCalendar.src="../js/calendar.js";
		jsCalendar.type='text/javascript'
		document.getElementsByTagName("head").item(0).appendChild(jsCalendar);
	}
function showCalendar(obj){

var e=event.srcElement;
var popup = window.createPopup();
var popBody = popup.document.body;
	popBody.style.border="outset 1pt #cccccc";
	popBody.style.fontSize = "9pt";
	popBody.style.backgroundColor= "#c6c6c6";
	popBody.style.cursor="hand";
	popup.show(e.pixelLeft,e.clientHeight+5,196,84,e);
	objDate=obj;
	if(objDate.value!=""){
		var strDate=objDate.value.replace("-","/").replace("-","/");
		calendar_Start(new Date(strDate), popBody, returnDate);
	}else{
		calendar_Start(new Date(), popBody, returnDate);
	}
}
*/
//另開視窗作法
/*
function showCalendar(obj){
	if(!checkDateType(obj.value)){
		alert("日期格式不正確");
		return;
	}	
  var url="../common/calendar.jsp?datevalue="+obj.value;
  var chk=showDialog(url,210,198);
  if (chk!="")obj.value=chk;
}*/
/*
var objGetDate;
function showCalendar1(obj){
  objGetDate=obj;
  pObj=window.createPopup();
  pBody=pObj.document.body;  
  var today=obj.value.replace("-","/").replace("-","/");
  calendar_Start(new Date(today), pBody, returnDate);  
  pObj.show(0,0,250,220,document.body);
}
function returnDate() {
	alert(this.value);
	var d=this.value.format().replace("/","-");
    objGetDate.value=d.replace("/","-");
}
*/
//取得天數差
function getDiffDays(D1,D2){
	var date1=D1.split(/-/);
	var date2=D2.split(/-/);
	var date3=new Date(date1[0],date1[1],date1[2]);
	var date4=new Date(date2[0],date2[1],date2[2]);
	var ile = date4.getTime() - date3.getTime();
	return Math.floor(ile / (1000 * 60 * 60 * 24));
}	
//取得天數差 
function getDiffDaysOK(D1,D2){
	var date1=D1.split(/-/);
	var date2=D2.split(/-/);
	var date3=new Date(date1[0]-1900,date1[1]-1,date1[2]);
	var date4=new Date(date2[0]-1900,date2[1]-1,date2[2]);
	var ile = date4.getTime() - date3.getTime();
	return Math.floor(ile / (1000 * 60 * 60 * 24));
}	
//傳回加幾天的日期字串
function getNextDayString(D1,add){
	var date1=D1.split(/-/);
	var DATE1=new Date(date1[0]-1900,date1[1]-1,date1[2]);
	DATE1.setDate(DATE1.getDate()+add);
	return (DATE1.getYear()+1900)+"-"+addZero(DATE1.getMonth()+1,2)+"-"+addZero(DATE1.getDate(),2);
}	
//回今天的年月日字串
function getToday(){
	var today = new Date();
	return today.getYear()+"-"+(today.getMonth()+1)+"-"+today.getDate();
}	
//傳回日期物件
function getDateObj(dateStr){
	var yearStr = (dateStr).substring(0,4);
	var monthStr = (dateStr).substring(5,7);
	var dayStr = (dateStr).substring(8);
	return new Date(yearStr*1,(monthStr*1)-1,dayStr*1);
}	
//回傳日期字串	
function getToday(yy,mm,dd){
	if(yy==null || mm==null || dd==null)
		date=new Date();
	else
		date=new Date(yy,mm,dd)
		
	year=date.getYear()%1900+1900;
	month=date.getMonth()+1;
	day=date.getDate();
	if(month<10) month="0"+month;
	if(day<10) day="0"+day;
	return  year+"-"+month +"-"+ day;
}
//取得期間別之起始日及終止日
function getPeriod(datetype,obj1,obj2){
var recdays=0;
var adddays=0;
var date=new Date();
	if(datetype.value=="none"){
		obj1.value="";
		obj2.value="";
	}
	if(datetype.value=="period"){
		obj1.value=getToday();
		obj2.value=getToday();
		obj1.style.display="";
		obj2.style.display="";			
	}
	if(datetype.value=="today"){
		obj1.value=getToday();
		obj2.value=getToday();			
	}		
	if(datetype.value=="week"){	
		recdays=date.getDay();
		adddays=6-recdays;
		obj1.value=getToday(date.getYear(),date.getMonth(),date.getDate()-recdays);
		obj2.value=getToday(date.getYear(),date.getMonth(),date.getDate()+adddays);
	}
	if(datetype.value=="month"){		
		var monthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
		var year=date.getYear();
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) monthDays[1] = 29; 
		obj1.value=getToday(date.getYear(),date.getMonth(),1);
		obj2.value=getToday(date.getYear(),date.getMonth(),monthDays[date.getMonth()]);	
	}

	if(datetype.value=="upmonth"){		
		var monthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
		var year=date.getYear();
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) monthDays[1] = 29; 
		
		var month=date.getMonth()-1;
		if(month==-1){
			month=11;
			year=date.getYear()-1;
		}
		//obj1.value=getToday(date.getYear(),date.getMonth()-1,1);
		//obj2.value=getToday(date.getYear(),date.getMonth()-1,monthDays[date.getMonth()-1]);
		obj1.value=getToday(year,month,1);
		obj2.value=getToday(year,month,monthDays[month]);
	}

	if(datetype.value=="year"){		
		//var monthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
		//var year=date.getYear();
		//if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) monthDays[1] = 29; 
		obj1.value=getToday(date.getYear(),0,1);
		obj2.value=getToday(date.getYear(),11,31);	
	}	
	if(datetype.value=="3days"){	
		obj1.value=getToday(date.getYear(),date.getMonth(),date.getDate()-2);
		obj2.value=getToday(date.getYear(),date.getMonth(),date.getDate());
	}
	if(datetype.value=="5days"){	
		obj1.value=getToday(date.getYear(),date.getMonth(),date.getDate()-4);
		obj2.value=getToday(date.getYear(),date.getMonth(),date.getDate());
	}
	if(datetype.value=="7days"){	
		obj1.value=getToday(date.getYear(),date.getMonth(),date.getDate()-6);
		obj2.value=getToday(date.getYear(),date.getMonth(),date.getDate());
	}	
	if(datetype.value=="30days"){	
		obj1.value=getToday(date.getYear(),date.getMonth(),date.getDate()-29);
		obj2.value=getToday(date.getYear(),date.getMonth(),date.getDate());
	}
	if(datetype.value=="90days"){	
		obj1.value=getToday(date.getYear(),date.getMonth(),date.getDate()-92);
		obj2.value=getToday(date.getYear(),date.getMonth(),date.getDate());
	}
	if(datetype.value=="future"){	
		obj1.value=getToday(date.getYear(),date.getMonth(),date.getDate());
		obj2.value="";
	}
}
// 民國年月
function getYY_MM(data) {
   newData = new Array(2);
   if (data.length == 5) {
      newData[0] = data.substring(0,3);
      newData[1] = data.substring(3,5);
   }
   else {
      newData[0] = data.substring(0,2);
      newData[1] = data.substring(2,4);
   }
   return newData;
}

// 民國年月日
function getYY_MM_DD(data) {
   newData = new Array(3);
   if (data.length == 7) {
      newData[0] = data.substring(0,3);
      newData[1] = data.substring(3,5);
      newData[2] = data.substring(5,7);
   }
   else {
      newData[0] = data.substring(0,2);
      newData[1] = data.substring(2,4);
      newData[2] = data.substring(4,6);
   }
   return newData;
}

// 西元年月
function getYYYY_MM(data) {
   newData = new Array(2);
   newData[0] = data.substring(0,4);
   newData[1] = parseInt(data.substring(4,6));
   return newData;
}

// 設定當月天數 => howard 2002/02/01
function setDaysOfMonth(objY,objM,objD){
  var month_days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  var nowDate=new Date();
  if(isNaN(objY.value) || objY.value.length==0){
    objY.value=nowDate.getYear()-1911;
    if(objM.length>nowDate.getMonth()) objM[nowDate.getMonth()].selected=true;
    if(objD.length>=nowDate.getDate()) objD[nowDate.getDate()-1].selected=true;
  }
  var year = TextToInt(objY);
  month_days[1]=((year+3)%4==0)?29:28; // 是否為閏月
  var selMonthDays=month_days[objM.selectedIndex]; // 所選月份天數
  var days1=objD.length; // 目前天數
  if(days1>selMonthDays){
    var lastSel=objD.selectedIndex;
    for(i=days1-1;i>=selMonthDays;i--){
      objD.options[i]=null;
    }
    if((selMonthDays-1)<lastSel) objD.selectedIndex=objD.length-1;
  }
  else if(days1<selMonthDays){
    for(i=days1;i<selMonthDays;i++){
      objD.options[i]=new Option(i+1,i+1);
    }
  }
}

//文字輸入欄位只能輸入整數
function TextToInt(obj){
  var newInt = isNaN(parseInt(obj.value))?0:parseInt(obj.value);
  obj.value = newInt;
  return newInt;
}

//文字輸入欄位能輸入浮點數
function TextToFloat(obj){
  var newFloat = isNaN(parseFloat(obj.value))?0:parseFloat(obj.value);
  obj.value = newFloat;
  return newFloat;
}

//文字輸入欄位補零
function zeroize(obj,len){
  var str=obj.value;
  var zz=len-str.length;
  for(i=0;i<zz;i++){
    str="0"+str;
  }
  obj.value=str;
}
//值補零
function addZero(v,len){
  var str=""+v;
  var zz=len-str.length;
  for(i=0;i<zz;i++){
    str="0"+str;
  }
  return str;
}

// 拆解字串
function splitData(data) {
	var regexp = /-/;
	return data.split(regexp);
}

// 新增選取資料 (for 設定頁面)
/*
function addItem(objfrom, objto) {
	from_len = objfrom.options.length;
	alert(objfrom.options.length);
	for (i=0; i<from_len; i++) {
		append = true;
		if (objfrom.options[i].selected) {
			to_len = objto.options.length;
			if (to_len != 0) {
				for(j=0;j<to_len;j++) {
					if(objto.options[j].text== objfrom.options[i].text)
						append=false;
				}
			}
            //data = splitData(objfrom.options[sel].value);
			if (append){  //Jason Update
			    objto.options[i] = new Option(objfrom.options[i].text,objfrom.options[i].value);
			    objto.options[i].selected=true;  
			} 
		}
	}
}*/

//移動選取資料
function addNewItem(objSelect,objValue,objText){
	var len=objSelect.length;
	objSelect.options[len]=new Option(objText,objValue);
	//objSelect.options[len].selected=true;
}

//移動選取資料
function addItem(objfrom, objto){
	for(i=objfrom.options.length-1; i>=0; i--){
		if(objfrom.options[i].selected){
			append = true;
			for(j=0;j<objto.options.length;j++){
				if(objto.options[j].text==objfrom.options[i].text)append=false;
			}	
			if(append){
				objto.options[objto.options.length] = new Option(objfrom.options[i].text,objfrom.options[i].value);
				objfrom.options[i]=null;  
			}
		} 
	}
}
//全選資料
function ChoiceAll(obj){
	for(i=0; i<obj.options.length; i++) {
		obj.options[i].selected=true;
	}
}
//全移
function addAllItem(objfrom, objto) {
	for(i=objfrom.options.length-1; i>=0; i--){
		append = true;
		for(j=0;j<objto.options.length;j++){
			if(objto.options[j].text==objfrom.options[i].text)append=false;
		}	
		if(append){
			objto.options[objto.options.length] = new Option(objfrom.options[i].text,objfrom.options[i].value);
			objfrom.options[i]=null;  
		}
	}
}
function copyAllItem(objfrom, objto) {
	for(i=objfrom.options.length-1; i>=0; i--){
		append = true;
		for(j=0;j<objto.options.length;j++){
			if(objto.options[j].text==objfrom.options[i].text)append=false;
		}	
		if(append){
			objto.options[objto.options.length] = new Option(objfrom.options[i].text,objfrom.options[i].value);
		}
	}
}
// 移除選取資料 (for 設定頁面)
function removeItem(objto) {
	for (x=objto.options.length-1;x>=0;x--) {
		 if (objto.options[x].selected)
			objto.options[x] = null;
	}
}
// 移除選取資料 (for 設定頁面)
function removeItembyValue(obj,val) {
	for (x=obj.options.length-1;x>=0;x--) {
		 if (obj.options[x].value==val)
			obj.options[x] = null;
	}
}
// 移除 Select 所有資料
function removeAllItem(obj) {
	for (x=obj.options.length-1;x>=0;x--) {
		 obj.options[x] = null;
	}
}
//加入新選項
function AddNewItem(Text,Value,tbox) {
	var i = 0;
	var no = new Option();
	no.value = Value;
	no.text    = Text;
	tbox.options[tbox.length] = no;
}

//-----------------------------------------------------------
// 檢查是否有選取資料
function checkSelected(obj) {
    isSelected = false;
    document.frm.action.value = "delete";
    for (i=0; i<obj.options.length;i++) {
        if (obj.options[i].selected)
            isSelected = true;
    }
    if (!isSelected)
        alert("您未選取任何資料, 無法刪除 !!");
    else
        document.frm.submit();
}

// send request (for 設定頁面)
function sendRequest(m_obj, obj, jsp_page) {
    len = obj.options.length;
    m_id = m_obj.options[m_obj.selectedIndex].value;
    tmp_id = "m_id=" + m_id + "&";
    if (len > 0) {
        for(i=0;i<len;i++) {
            // 判斷是否為最後一筆, 最後一筆不用加 '&'
            if (i==len-1)
                tmp_id = tmp_id + "id=" + obj.options[i].value;
            else
                tmp_id = tmp_id + "id=" + obj.options[i].value + "&";
        }
    }
    else {
        tmp_id = tmp_id + "id=empty";
    }
    window.location = jsp_page+"?"+tmp_id;
}

//金額加入逗號 ex: 1234567.89  => 1,234,567.89
function comma(money) {
  var len=money.length;
  var ret="";
  var dot="";
  var dotLen=money.indexOf(".");
  if(dotLen>-1) {
    dot=money.substring(dotLen,dotLen+3);
    money=money.substring(0,dotLen);
    len=money.length;
  }
  while(len>3) {
    if(ret.length>0) ret=","+ret;
    ret=money.substring(len-3,len)+ret;
    money=money.substring(0,len-3);
    len-=3;
  }
  if(len>0 && ret.length>0) ret=money+","+ret;
  else ret=money;
  return ret+dot;
}

// Create By Amy
function replaceString(String1,FrmStr,ToStr) {
	var strDest="";
	var intFromLen = FrmStr.length;
	var intPos;
	if(String1!=null){
	    while((intPos=String1.indexOf(FrmStr))!=-1){
			strDest = strDest + String1.substring(0,intPos);
			strDest = strDest + ToStr;
			String1 = String1.substring(intPos+intFromLen);
	    }
	    strDest = strDest + String1;		    
	}
	return strDest;   
}
//限制只能key數字 0~9
//Create By Amy
function NumberType(){
	if (event.keyCode<48||event.keyCode>57)
	return false;
}
//限制只能key enter
function calendarType(){
	if (event.keyCode!=13) return false;
}	

//限制只能key數字 0~9 和 ,和 .  
//適用於貨幣型態的欄位
//Create By Amy
function MoneyType(){
    	//alert(event.keyCode);
	if (!((event.keyCode>=48 && event.keyCode<=57) || event.keyCode==44 || event.keyCode==46))
	return false;
}
//限制只能key數字 0~9 和()和-   
//適用於電話型態的欄位
//Create By Amy
function TelType(){
    	//alert(event.keyCode);
	if (!((event.keyCode>=48 && event.keyCode<=57) || event.keyCode==40 || event.keyCode==41 || event.keyCode==45))
	return false;
}

//Enter 鍵模擬tab鍵
//Create by Jason Chiang in 2002/12/30
function tabOnEnter (field, evt) {
    var keyCode = document.layers ? evt.which : document.all ? 
    evt.keyCode : evt.keyCode;
      if (keyCode != 13)
        return true;
      else {
                getNextElement(field).focus();
        return false;
      }
}
function getNextElement (field) {
    var form = field.form;
    for (var e = 0; e < form.elements.length; e++){
        if (field == form.elements[e])
            break;
    }        
   return form.elements[++e % form.elements.length];
}
//====================================================
//子仟
//物件金額加入逗號 ex: 1234567.89  => 1,234,567.89
function ObjAddcomma(obj) {
  var newInt=replaceString(obj.value,",","");
  newInt = isNaN(parseInt(newInt))?"0":parseInt(newInt).toString();
  obj.value=comma(newInt);
  return newInt;
}
//物件金額取到小數第P位
function formatFloat(val,P){
	pos=val.indexOf(".");
	if(pos>0)
		val=val.substr(0,val.indexOf(".")+P+1);		
  return val;
}
//限制只能輸入小於32的數字
function DayNumType(obj){
  var newInt =isNaN(parseInt(obj.value))?0:parseInt(obj.value);
  if( newInt>31 || newInt<1 ){
    alert("請輸入1~31數字!!");
  	newInt="1";
  }
  obj.value = newInt;
}

function NumType(obj){
    //alert(isNaN(parseInt(obj.value)));
  if(isNaN(parseInt(obj.value))){
    alert("請輸入數字!!");
    obj.value="";
    return false;
  }else{  	
  	return true;
  }
}
//限制只能輸入小於13的數字
function HourType(obj){
  if (obj.value>"01" && obj.value<="09") return;
  var newInt =isNaN(parseInt(obj.value))?0:parseInt(obj.value);
  if( newInt>12 || newInt<1 ){
    alert("請輸入1~12數字!!");
  	newInt="01";
  }else if( newInt<10 ){
  	newInt="0"+newInt;
  }
  obj.value = newInt;
}
//限制只能輸入小於60的數字
function MinType(obj){
  if (obj.value>"01" && obj.value<="09") return;
  var newInt =isNaN(parseInt(obj.value))?0:parseInt(obj.value);
  if( newInt>59 || newInt<0 ){
    alert("請輸入0~59數字!!");
  	newInt="00";
  }else if( newInt<10 ){
  	newInt="0"+newInt;
  }
  obj.value = newInt;
}
function trim(strMessage){
	var strResult;
	var charTemp;
	var i;
	strResult = "";
	//remove the left space
	for ( i = 0; i < strMessage.length; i++ ){
		charTemp = strMessage.charAt(i);
		if ( charTemp != " " ){
			strResult = strMessage.substring(i);
			break;
		}
	}
	//remove the right space
	for ( i = strResult.length-1; i >= 0; i-- ){
		charTemp = strResult.charAt(i);
		if ( charTemp != " " ){
			strResult = strResult.substring(0,i+1);
			break;
		}
	}
	return (strResult);
}
//howard的偉大傑作
function count_char(obj,who){
	var str1=obj.value;
	var spl,dee1;  
	var byte;
	spl=escape(str1).split("%u");  // 中文字數
	byte=str1.length+spl.length-1;
	if (byte>who){
	  alert("超出字數範圍");
	  dee1=byte-who;
	  for (q=1;q<=dee1;q++){
	  	str1=str1.substr(0,str1.length-1);
	  	spl=escape(str1).split("%u"); 
		byte=str1.length+spl.length-1;
		if (byte<=who)break;
	  }
	  obj.value=str1;        
	}
}
 function copy_char(obj1,obj2,num){
	var str1=obj1.value;
	var spl,dee1;  
	var byte;
	spl=escape(str1).split("%u");  // 中文字數
	byte=str1.length+spl.length-1;
	if (byte>num)
		 dee1=byte-num;
	else 
		 deel=byte;

	  for (q=1;q<=dee1;q++){
	  	str1=str1.substr(0,str1.length-1);
	  	spl=escape(str1).split("%u"); 
		byte=str1.length+spl.length-1;
		if (byte<=num)break;
	  }		
	  obj2.value=str1;        
	
}
//判斷日期格式是否為xxxx-xx-xx
function checkDateType(v){
	var arrValue=v.split("-");
	if(v!=""){
		if(arrValue.length!=3){
			return false;
		}
		if(arrValue[0].length!=4 || arrValue[1].length!=2 || arrValue[2].length!=2){
			return false;
		}	
	}	
	return true;
}	
//將時間轉換成正確可比較的模式
//參數1:日期 2:幾點 3:幾分
function changeDateType(dateStr,hourStr,minStr){
	var return_time = dateStr+" "+hourStr+":"+minStr;
	return return_time;
}
//依照上下午的選擇而顯示00 or 12
function selType(meridiem,hourObj){
	if(meridiem=="AM" || meridiem=="0") hourObj.options[0].text="00";
	if(meridiem=="PM" || meridiem=="1") hourObj.options[0].text="12";
}
//取得這個月有幾天
function getMaxDate(year,month){
  var numberDays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  n=numberDays[month-1];
  //如果遇閏年,2月天數加1
  if(month==2 && year%4==0)++n;
  return n;
}
//資料編碼
function encode(code)
{
	var encodeStr="";
	for(var i=0;i<code.length;i++){
		var _ch2=parseInt(code.charCodeAt(i));
		var _ch2_xor=_ch2^531;
		encodeStr+=_ch2_xor+"@";
	}
	encodeStr=encodeStr.substring(0,encodeStr.length-1);
	return encodeStr;
}
//資料解碼
function decode(code)
{
	var decodeStr="";
	var datas=code.split("@");
	for(var i=0;i<datas.length;i++){
		var tmpCode=datas[i]^531;
		decodeStr+=String.fromCharCode(parseInt(tmpCode));
	}
	return decodeStr;
}

//將科學計號Scientific轉成數值Num
function transScient2Num(str){
	str=""+str.toUpperCase();
	var strS=str.split("E");
	if(strS[1]==null)
		return strS[0];
	else{
		var num1=parseFloat(strS[0]);
		for(i=0;i<parseInt(strS[1]);i++)
			num1=num1*10;
		num1=parseInt(""+num1+"");
		return ""+num1;
	}
	return "0";
}

//20090320 表單欄位輸入限制
function regInput(obj, reg, inputStr){ 
	var docSel	= document.selection.createRange()
	if (docSel.parentElement().tagName != "INPUT")	return false
	oSel = docSel.duplicate()
	oSel.text = ""
	var srcRange	= obj.createTextRange()
	oSel.setEndPoint("StartToStart", srcRange)
	var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length)
	return reg.test(str)
}

// 20100212 列印指定內容
  
  function printIt(objname)
  {
	var win=null;
	var obj = document.getElementById(objname);
	var objclass = obj.className;
	
    win = window.open();
    self.focus();
    win.document.open();
    win.document.write('<'+'html'+'><'+'head'+'>');
	win.document.write(document.getElementsByTagName('head')[0].innerHTML);
    win.document.write('<'+'/'+'head'+'><'+'body'+' class='+objclass+'>');
    win.document.write(obj.innerHTML);
    win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
    win.document.close();
    win.print();
    //win.close();
  }
  

