//==========================================
// 在 A 按 onCLick事件
//==========================================
function openurl(strurl){
	//alert(strurl);
	parent.MA_MidMid.frames.location.href=strurl;
	window.status='';
	return true
}
	
	
//==========================================
// 檢核欄位資料正確性
//==========================================
function DataChk(Str,Str1,Num){

var RefString="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (Count=0; Count < Str.value.length; Count++){
TempChar= Str.value.substring (Count, Count+1);
if (RefString.indexOf (TempChar, 0)==-1){
alert('『'+Str1+'』欄位資料只能填入英文或數字喔 !!');
return false;}}

if (Str.value.length<Num){
alert('『'+Str1+'』欄位至少要'+Num+'個字元，請重新輸入 !!');
return false;}

}

//==========================================
// 檢核欄位資料是否為數字
//==========================================
function NumChk(Str1,Str2){

var RefString="1234567890";
for (Count=0; Count < Str1.value.length; Count++){
TempChar= Str1.value.substring (Count, Count+1);

if (RefString.indexOf (TempChar, 0)==-1){
alert('『'+Str2+'』欄位資料只能填入數字喔 !!');
Str1.value ="";
return false;}}

}

//==========================================
// 檢核輸入值是否為Empty
//==========================================
function ChkEmpty(Str){
if (Str.value=='' || Str.value==null) return false;}


//==========================================
// 檢核輸入值是否為Empty (檢查下拉物件)
//==========================================
function ChkEmptySelect(Str){
if (Str.options[Str.selectedIndex].value=='' || Str.options[Str.selectedIndex].value==0) return false;
}


//==========================================
// 檢核Email格式是否正確
//==========================================
function EmailChk(Str) {
	re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	if (re.test(Str.value)){
			return true;}
	else{
			return false;}}

//==========================================
// 身分證字號檢核 第－碼英文，後九碼數字
//==========================================
function IDChk(Str){
	var patrn=/^[a-zA-Z]{1}[0-9]{9}$/;	//本國身份證
	var patrn2=/^[a-zA-Z]{2}[0-9]{8}$/;	//外國人護照 第1,2碼英文，後8碼數字
	if (!patrn.exec(Str.value)) {
		if (!patrn2.exec(Str.value)) {
			return false
		}else {return true}
	}
	return true
}



//==========================================
// 密碼檢核4-12字(英文、數字)
//==========================================

function PassChk(Str){
	var patrn=/^[a-zA-Z0-9]{4,12}$/;
	if (!patrn.exec(Str.value)) return false
	return true
}

//==========================================
// 電話檢核格式 0X　-　XXXXXXXXX
//               -     ---------
//               1碼    7-8碼
//==========================================
function TelChk(Str){
	var patrn=/^[0]{1}[2-9]{1}[-]{1}[0-9]{7,8}$/;
	if (!patrn.exec(Str.value)) return false
	return true
}

//==========================================
// 行動電話檢核 格式09XXXXXXXX
//==========================================
function CellChk(Str){
	var patrn=/^[0]{1}[9]{1}[0-9]{8}$/;
	if (!patrn.exec(Str.value)) return false
	return true
}

//==========================================
// 檢查輸入日期是否正確　
//==========================================
function checkDate(input){
	var validformat=/^\d{4}\/\d{2}\/\d{2}$/ //格式 yyyy/mm/dd
	var returnval=false
	if (!validformat.test(input.value))
		return false;
		//alert("輸入格式錯誤，格式為 yyyy/mm/dd ")
	else{ //Detailed check for valid date ranges
		var yearfield=input.value.split("/")[0]
		var monthfield=input.value.split("/")[1]
		var dayfield=input.value.split("/")[2]
		var dayobj = new Date(yearfield, monthfield-1, dayfield)
		if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
			return false;
			//alert("輸入日期錯誤！！")
		else
			returnval=true
	}
	if (returnval==false) input.select()
	return returnval
}


//==========================================
// 開啟視窗;參數(連結,視窗名稱,視窗長度,視窗寬度)
//==========================================
function OpenWin(Url,WinName,Width,Heigth,Top){
	window.open(Url,WinName,'menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=yes,width='+ Width +',height='+ Heigth +',top='+ Top +',left='+(screen.width-Width)/2+'');
}

//==========================================
// 開啟視窗;參數(連結,視窗名稱,視窗長度,視窗寬度)
//==========================================
function OpenWin2(Url,WinName,Width,Heigth,Top){
	//window.open(Url,WinName);}
	window.open(Url,WinName,'menubar=no,toolbar=no,location=no,directories=no,status=No,resizable=no,scrollbars=no,width='+ Width +',height='+ Heigth +',top='+ Top +',left='+ (screen.width-Width)/2 +'');
}

//==========================================
// 開啟視窗;參數(連結,視窗名稱,視窗長度,視窗寬度)
//==========================================
function OpenWin3(Url,WinName,Width,Heigth,Top){
	window.open(Url,WinName,'menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=Yes,width='+ Width +',height='+ Heigth +',top='+ Top +',left='+ (screen.width-Width)/2 +'');
}



//==========================================
// 強迫變大寫字元(onblur事件）
//==========================================
function UpperCase(tagname){
	var InputValue = document.all[tagname].value;
	var MyStr=(new String(InputValue)).toUpperCase();
	document.all[tagname].value = MyStr;
}



//==========================================
// 取的radio值
//==========================================

function CheckRadio(theRadio){
    var theRadioLen = theRadio.length;
    var theRadioValue = false;
    if (theRadioLen == undefined){
        if (theRadio.checked){
            theRadioValue = theRadio.value;
        }
    }else{
        for (theRadioI=0;theRadioI<theRadioLen;theRadioI++){
            if (theRadio[theRadioI].checked){
                theRadioValue = theRadio[theRadioI].value;
                break;
            }
        }
    }
    return theRadioValue;
}



//==========================================
// checkbox 的篩選 (傳入Checkbox ID Name 前６碼
//==========================================
function CheckSelAll(frmName,strCheckID) {
	with(document.forms[frmName]) {
		for(i=0;i<elements.length;i++) {
			thiselm = elements[i];
			if(thiselm.name.substring(0,strCheckID.length) == strCheckID)
				if (thiselm.disabled==false)
				{
					thiselm.checked = !thiselm.checked
				}
		}
	}
}


//==========================================
// checkbox 的篩選 (傳入Checkbox ID Name 前６碼
//==========================================
function CheckSelAll2(flgValue,frmName,strCheckID) {
	with(document.forms[frmName]) {
		for(i=0;i<elements.length;i++) {
			thiselm = elements[i];
			if(thiselm.name.substring(0,strCheckID.length) == strCheckID)
				thiselm.checked = flgValue
		}
	}
}


//==========================================
// 欄位只能輸入數字
//strOther=例外字，可輸入
//==========================================
function InputIsNumeric(strOther){

	var isValidKey = false;   
	var intCode = window.event.keyCode;   

	if (intCode >= 48 && intCode <= 57) isValidKey = true;   

	if (intCode == 9)  isValidKey = true; //9 Tab 鍵 (Tab)
	if (intCode == 10) isValidKey = true; //10 換行符號 (Line Feed)
	if (intCode == 13) isValidKey = true; //13 enter   
	if (intCode == 27) isValidKey = true; //27 ESC   (Escape)

	if (strOther.value!='' || strOther.value!=null) {
		if (strOther.length >0 ){
			for (Count=0; Count < strOther.length; Count++){
				TempChar= strOther.substring (Count, Count+1);
				if (intCode == 32 && TempChar==" ") isValidKey = true; //32 spacebar   
				if (intCode == 35 && TempChar=="#") isValidKey = true; //35 # 
				if (intCode == 44 && TempChar==",") isValidKey = true; //44 , 
				if (intCode == 45 && TempChar=="-") isValidKey = true; //45 - 
				if (intCode == 46 && TempChar==".") isValidKey = true; //46 . 
				if (intCode == 47 && TempChar=="/") isValidKey = true; //47 / 
			}
		}
	}

	//依照需求自行增刪合法的 key code   
    //window.event.keyCode=0;
	window.event.returnValue = isValidKey;   
}

//==========================================
// 欄位只能輸入數字 OR 英文字
//strOther=例外字，可輸入
//==========================================
function InputIsNumericEngNum(strOther){

	var isValidKey = false;   
	var intCode = window.event.keyCode;   

	if (intCode >= 48 && intCode <= 57) isValidKey = true;   
	if (intCode >= 65 && intCode <= 90) isValidKey = true;   //A-Z
	if (intCode >= 97 && intCode <= 122) isValidKey = true;  //a-z

//alert(intCode)
	if (intCode == 9)  isValidKey = true; //9 Tab 鍵 (Tab)
	if (intCode == 10) isValidKey = true; //10 換行符號 (Line Feed)
	if (intCode == 13) isValidKey = true; //13 enter   
	if (intCode == 27) isValidKey = true; //27 ESC   (Escape)

	if (strOther.value!='' || strOther.value!=null) {

		if (strOther.length >0 ){
			for (Count=0; Count < strOther.length; Count++){
				TempChar= strOther.substring (Count, Count+1);
				if (intCode == 32 && TempChar==" ") isValidKey = true; //32 spacebar   
				if (intCode == 35 && TempChar=="#") isValidKey = true; //35 # 
				if (intCode == 44 && TempChar==",") isValidKey = true; //44 , 
				if (intCode == 45 && TempChar=="-") isValidKey = true; //45 - 
				if (intCode == 46 && TempChar==".") isValidKey = true; //46 . 
				if (intCode == 47 && TempChar=="/") isValidKey = true; //47 / 
				if (intCode == 95 && TempChar=="_") isValidKey = true; //95 _ 
			}
		}

	}
	//依照需求自行增刪合法的 key code   
    //window.event.keyCode=0;
	window.event.returnValue = isValidKey;   
}



//==========================================
// 將傳入的數字含有逗號去掉
//==========================================
function fn_StringToNumeric(value){
	value=value.replace(",","")
	return value;
}

//==========================================
// 將數字加上逗號傳回 (最多加3個逗號, 123,456,789,000
//==========================================
function fn_NumericToString(strNum){
	strNum=strNum +"";
	if (strNum.length>3){
		if (strNum.length>9){
				if (strNum.length==10) {strNum=strNum.substr(0,1) +","+ strNum.substr(1,3)+","+ strNum.substr(4,3)+","+ strNum.substr(7,3);return strNum;}
				if (strNum.length==11) {strNum=strNum.substr(0,2) +","+ strNum.substr(2,3)+","+ strNum.substr(5,3)+","+ strNum.substr(8,3);return strNum;}
				if (strNum.length==12) {strNum=strNum.substr(0,3) +","+ strNum.substr(3,3)+","+ strNum.substr(6,3)+","+ strNum.substr(9,3);return strNum;}
		}
		else if (strNum.length>6){
				if (strNum.length==7) {strNum=strNum.substr(0,1) +","+ strNum.substr(1,3)+","+ strNum.substr(4,3);return strNum;}
				if (strNum.length==8) {strNum=strNum.substr(0,2) +","+ strNum.substr(2,3)+","+ strNum.substr(5,3);return strNum;}
				if (strNum.length==9) {strNum=strNum.substr(0,3) +","+ strNum.substr(3,3)+","+ strNum.substr(6,3);return strNum;}
		
		} else {
				if (strNum.length==4) {strNum=strNum.substr(0,1) +","+ strNum.substr(1,3);return strNum;}
				if (strNum.length==5) {strNum=strNum.substr(0,2) +","+ strNum.substr(2,3); return strNum;}
				if (strNum.length==6) {strNum=strNum.substr(0,3) +","+ strNum.substr(3,3);return strNum;}
		}
	}
	return strNum;
}

//Format 數字有逗號小數點, ex: 123,456.00
function outputMoney(number) {
    return outputDollars(Math.floor(number-0) + '') + outputCents(number - 0);
}
//Format 整數部份
function outputDollars(number) {
    if (number.length <= 3)
        return (number == '' ? '0' : number);
    else {
        var mod = number.length%3;
        var output = (mod == 0 ? '' : (number.substring(0,mod)));
        for (i=0 ; i < Math.floor(number.length/3) ; i++) {
            if ((mod ==0) && (i ==0))
                output+= number.substring(mod+3*i,mod+3*i+3);
            else
                output+= ',' + number.substring(mod+3*i,mod+3*i+3);
        }
        return (output);
    }
}
//Format 小數部份
function outputCents(amount) {
    amount = Math.round( ( (amount) - Math.floor(amount) ) * 100);
    return (amount < 10 ? '.0' + amount : '.' + amount);
}



// 宣告字串型有以下三種屬性
// 要使用以下三種屬性,ex: strValue=strValue.trim();
String.prototype.trim=trim;  //傳回去除前後空白的值
String.prototype.ltrim=ltrim;  //傳回去除左邊空白的值
String.prototype.rtrim=rtrim;  //傳回去除右邊空白的值
function trim() {
  return this.replace(/^\s+|\s+$/g, "");
} 
function ltrim() {
  return this.replace(/(^\s*)/g, "");
}
function rtrim() {
  return this.replace(/(\s*$)/g, "");
}


//==========================================
// 結束
//==========================================