String.prototype.ltrim = _String_ltrim;
String.prototype.rtrim = _String_rtrim;
String.prototype.trim = _String_trim;
String.prototype.replaceAll = _String_replaceAll;
String.prototype.formatNumber = _String_formatNumber;


// 字串格式檢查
String.prototype.isDigit = _String_isDigit;
String.prototype.isAlphabet  = _String_isAlphabet;
String.prototype.isDate = _String_isDate;
String.prototype.isTime = _String_isTime;
String.prototype.isIdNo = _String_isIdNo;
String.prototype.isInvoiceNo = _String_isInvoiceNo;
String.prototype.isUidNo = _String_isUidNo;
String.prototype.isAlphabetAndDigit = _String_isAlphabetAndDigit

function _String_ltrim() {
    if ( typeof( this ) != "undefined" ) {
        return this.toString().replace(/^\s+/g, '');
    } else {
        return false;   
    }    
}

function _String_rtrim() {
    if ( typeof( this ) != "undefined" ) {
        return this.toString().replace(/\s+$/g, '');
    } else {
        return false;   
    }    
}

function _String_trim() {

    if ( typeof( this ) != "undefined" ) {
        return this.toString().replace(/^\s+/g, '').replace(/\s+$/g, ''); 
    }    
    return false;
    
}

function _String_replaceAll(strRegex, strReplacement) {
    if ( typeof( this ) != "undefined" ) {
        return this.toString().replace(new RegExp(strRegex,"g"), strReplacement);
    } else {
        return false;   
    }   
}

function _String_formatNumber() {

	var strNo = "";
	var strNo1 = "";
	var strNo2 = "";
	var strOutput = "";
	var aryNo = null;
	var i = 0;
	var iCounter = 0;
	
    if ( typeof( this ) != "undefined" ) {
		
		strNo = this.toString();
		
		if ( strNo.trim() != "" ) {
	
			aryNo = strNo.trim().split(".");

			if ( aryNo.length == 2 ) {
				strNo1 = aryNo[0];
				strNo2 = aryNo[1];
			} else {
				strNo1 = aryNo[0];
				strNo2 = "";
			}
		
			for ( i = strNo1.length - 1; i >= 0; i-- ) {
				strOutput = strNo1.substring(i, i + 1) + strOutput;
				if ( iCounter % 3 == 2 && i != 0) {
					strOutput = "," + strOutput
				} 
				iCounter++;			
			}
		
			if ( strNo2 != "" ){
				strOutput = strOutput + "." + strNo2;
			}
		
			return strOutput;

		} else {
			return "";
		}
    
    } else {
        return false;   
    } 
}

// 字串格式檢核

// 是否為數字
function _String_isDigit() {

    var strDigit = "";
    
    if ( typeof( this ) != "undefined" ) {
        
        strDigit = this.toString().trim();
        
        re = /^\d+$/g;
        
        if ( re.test( strDigit ) ) {
            return true;   
        }
                
    }
    
    return false;    
    
}

// 是否為英文字母
function _String_isAlphabet() {
    
    var re;
    var strChar = "";
    
    if ( typeof( this ) != "undefined" ) {
        
        strChar = this.toString().trim();
        
        re = /^[a-zA-Z]+$/g;
        
        if ( re.test( strChar ) ) {
            return true;    
        }
        
    }
    
    return false;
    
}

// 是否為英文字母或數字..
function _String_isAlphabetAndDigit() {
    
    var re;
    var strChar = "";
    
    if ( typeof( this ) != "undefined" ) {
        
        strChar = this.toString().trim();
        
        re = /^[a-zA-Z\d]+$/g;
        
        if ( re.test( strChar ) ) {
            return true;    
        }
        
    }
    
    return false;
    
}

// 是否為日期字串 yyyy/MM/dd
function _String_isDate() {
    // yyyy/MM/dd
    var re;
    var strYear = "";
    var strMonth = "";
    var strDate = "";
    
    var iDays = 0;
    
    if ( typeof( this ) != "undefined" ) {
        re = /^[12]\d{3}\/\d{0,2}\/\d{0,2}$/g;
        if ( re.test( this.toString().trim() ) ) {
            
            strYear = this.toString().trim().split("/")[0];
            strMonth = this.toString().trim().split("/")[1];
            strDate = this.toString().trim().split("/")[2];
            
            if ( parseInt(strYear, 10) < 1900 || parseInt(strYear, 10) > 2100 ) {
                return false;   
            }
            
            if ( parseInt(strMonth, 10) < 1 || parseInt(strMonth, 10) > 12 ) {
                return false;   
            }
            
            
            switch ( parseInt(strMonth, 10) ) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    iDays = 31;
                    break;
                    
                case 2:
                    iDays = 28;
                    break;
                    
                case 4:
                case 6:
                case 9:
                case 11:
                    iDays = 30;
                    break;                                                              
            }  
            
            if ( parseInt(strMonth, 10) == 2 ) {
	            if ( parseInt(strYear, 10) % 1000 == 0 ) {
	                iDays = 29;   
	            }  
	
	            if ( parseInt(strYear, 10) % 100 == 0 ) {
	                iDays = 28;   
	            } 
	
	            if ( parseInt(strYear, 10) % 4 == 0 ) {
	                iDays = 29;   
	            } 
			}
			
            if ( parseInt(strDate, 10) < 1 || parseInt(strDate, 10) > iDays ) {
                return false;
            }
            
            return true;    
        } else { 
            return false;
        }    
    } else { 
        return false;
    }   
}

// 是否為時間字串 hh:mm:ss
function _String_isTime() {
    
    var re;
    var strTime = "";
    var strHour = "";
    var strMin = "";
    var strSecond = "";
    
    if ( typeof( this ) != "undefined" ) {  
        
        strTime = this.toString().trim();
        
        re = /^\d{1,2}:\d{1,2}:\d{1,2}$/g;
        
        if ( re.test( strTime ) ) {
            strHour = strTime.split(":")[0];
            strMin = strTime.split(":")[1];
            strSecond = strTime.split(":")[2]; 
            
            if ( parseInt( strHour, 10 ) >=0 && parseInt( strHour, 10 ) <= 24 ) {
                if ( parseInt( strMin, 10 ) >= 0 && parseInt( strMin, 10 ) < 60 ) {
                    if ( parseInt( strSecond, 10 ) >= 0 && parseInt( strSecond, 10 ) < 60 ) {
                        return true;    
                    }    
                }    
            }    
               
        }
                
    }
    
    return false;
    
}

// 是否為身份證字號
function _String_isIdNo() {

    var re;
    var strIdNo = "";
    var iCheckSum = 0;
    var strPrefixAry = new Array("10", "11", "12", "13", "14", "15", "16", "17", "34", "18", "19", "20", "21", "22", "35", "23", "24", "25", "26", "27", "28", "29", "32", "30", "31", "33" );
    var strPrefixReference = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var strPrefix = "";
    
    if ( typeof( this ) != "undefined" ) {    
        
        re = /^[A-Z]\d{9}$/g;
        
        strIdNo = this.toString().trim().toUpperCase();
        
        if ( re.test( strIdNo ) ) {
            
            strPrefix = strPrefixAry[ strPrefixReference.indexOf( strIdNo.substring(0, 1) ) ];
            
            iCheckSum = parseInt( strPrefix.substring(0, 1), 10 ) * 1 + parseInt( strPrefix.substring(1, 2), 10 ) * 9;
            
            for ( i = 1; i <= 8; i++) {
                iCheckSum += parseInt( strIdNo.substring(i, i + 1), 10 ) * ( 9 - i );    
            }
            
			iCheckSum += parseInt(strIdNo.substring(9, 10), 10);

            if (iCheckSum % 10 != 0 ) {
                return false;   
            }    
            
            return true;
            
        } else {
            return false;   
        }
    
    } else {
        return false;   
    }
    
}

// 是否為外國人統一證號
function _String_isUidNo() {

    var re;
    var strIdNo = "";
    
    if ( typeof( this ) != "undefined" ) {    
        //前兩位英文後八位數字
        re = /^[A-Z]{1}[A-D]{1}\d{8}$/g;
        
        strIdNo = this.toString().trim().toUpperCase();
        
        if ( re.test( strIdNo ) ) {

            return true;
            
        } else {
            return false;   
        }
    
    } else {
        return false;   
    }
    
}


// 是否為統一編號
function _String_isInvoiceNo() {

    var strInvNo = "";
	var pa = [1, 2, 1, 2, 1, 2, 4, 1];
	var iTp = 0;
	var iCheckSum = 0;
	var re;
	
	    
    if ( typeof( this ) != "undefined" ) {
    
        strInvNo = this.toString().trim();
        
        re = /^\d{8}$/g;
        
        if ( re.test(strInvNo) ) {
        
            for ( var i = 0; i < strInvNo.length; i++) {
                iTp = parseInt( strInvNo.substring(i, i + 1), 10) * pa[i];
                iCheckSum += Math.floor( iTp / 10) + ( iTp % 10 );
            }
            
            if ( iCheckSum % 10 == 0 || ( strInvNo.substring(6, 7) == "7" && iCheckSum % 10 == 9 ) ) {
                return true;
            }        
            
        }
        
        
    }   

    return false;

    
}



// 格式轉換

/**
日期格式轉換
yyyyMMdd, y-M-d, y/M/d => yyyy/MM/dd
*/
function formatDate(strDate) {
    
    var re;
    
    var strYear = "";
    var strMonth = "";
    var strDay = "";
    
    var strRtnDate = "";
    
    strDate = strDate.trim();
    
    re = /^\d{8}$/g;
    
    if ( re.test( strDate ) ) {
        // 格式為 yyyyMMdd
        strYear = strDate.substring(0, 4);
        strMonth = strDate.substring(4, 6);
        strDay = strDate.substring(6, 8);
        
        strRtnDate = strYear + "/" + strMonth + "/" + strDay;
  
    }
    
    
    re = /^\d{4}\/\d{1,2}\/\d{1,2}$/g;
    
    if ( re.test( strDate ) ) {
        // 格式為 y/M/d
        strYear = strDate.split("/")[0];
        strMonth = strDate.split("/")[1];
        strDay = strDate.split("/")[2];
        
        strMonth = "00" + strMonth;
        strMonth = strMonth.substring( strMonth.length - 2, strMonth.length );
        
        strDay = "00" + strDay;
        strDay = strDay.substring( strDay.length - 2, strDay.length );
        
        strRtnDate = strYear + "/" + strMonth + "/" + strDay;
  
    }
    
    re = /^\d{4}-\d{1,2}-\d{1,2}$/g;
    
    if ( re.test( strDate ) ) {
        // 格式為 y-M-d
        strYear = strDate.split("-")[0];
        strMonth = strDate.split("-")[1];
        strDay = strDate.split("-")[2];
        
        strMonth = "00" + strMonth;
        strMonth = strMonth.substring( strMonth.length - 2, strMonth.length );
        
        strDay = "00" + strDay;
        strDay = strDay.substring( strDay.length - 2, strDay.length );
        
        strRtnDate = strYear + "/" + strMonth + "/" + strDay;    
        
    }     
    
    if ( strRtnDate != "" ) {
        if ( strRtnDate.isDate() ) {
            return strRtnDate;    
        }    
    }
    
    return false;
        
}

function compareDate(beginDate, days) {
	/*
	parameter1:String yyyy/MM/dd
	parameter2:int
	*/
	var b = null;
	if(beginDate instanceof Date) {
		b = beginDate;
	}
	else {
		b = new Date(beginDate);
	}
	b.setTime(b.getTime()+days*24*60*60*1000);
	return b.getYear()+"/"+(b.getMonth()+1<10?"0"+(b.getMonth()+1):b.getMonth()+1)+"/"+(b.getDate()<10?"0"+b.getDate():b.getDate());
}



