﻿	String.prototype.replaceAll = function (AFindText,ARepText){
	  var raRegExp = new RegExp(AFindText.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g,"\\$1"),"ig");  
		return this.replace(raRegExp,ARepText);  
	} 
	
	String.prototype.cfword = function(){	    
		var matchWord = new Array('&','\'','"','<','>');
		var repWord = new Array("&amp;","&##39;","&quot;","&lt;","&gt;");
		var str = this;
		for(i=0;i<matchWord.length;i++){
			str = str.replaceAll(matchWord[i],repWord[i]);
	   }
		return str;
	} 
	
	//特殊字元置換 &=>&amp;
	String.prototype.reword = function(){	   
	    var matchWord = new Array('&','\'','"','<','>');
		var repWord = new Array("&amp;","&#39;","&quot;","&lt;","&gt;");
		var str = this;
		for(i=0;i<matchWord.length;i++){
			str = str.replaceAll(repWord[i],matchWord[i]);
	   }
		return str;
	}
	
	String.prototype.strlen = function (){
		var totalBytes = 0 ;
	    for(i=0;i<this.length;i++){
	        chr = this.charCodeAt(i);
	        if (chr > 256)
	            totalBytes += 2;
	        else
	            totalBytes++;
	    }
	    return totalBytes;
	}
	
	String.prototype.isUrl = function () { 
		var rx = new RegExp("^http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-\\+ ./?%:&=#\\[\\]]*)?$"); 
		var matches = rx.exec(this); 
		return (matches != null && this == matches[0]); 
	}
	
	//可檢查全形與半形空白
	String.prototype.trim = function(){ 
		return this.replace(/^[ 　]*|[ 　]*$/g, "");
	}
	
	function strCheck(id,type,alt,max){
		var max = arguments[3];
		var str = document.getElementById(id).value.trim();
		switch(type){
			case 1://判斷空值
				if( str == "" ){
					return alt + " 欄位不得為空，請重新輸入\n";
				}
				break;	
			case 2://判斷上限
				if( str.cfword().strlen() > max ){
					msg =  alt + " 欄位字數超過"+ max/2 +"個字("+ max +"字元)，請重新輸入\n";
					if( str.cfword() != str.trim() )
						msg += "    (請注意，「\"」為6字元，「& '」為5字元，「< >」為4字元)\n";
					return msg;
				}
				break;
		}
		return "";
	}
	
	Date.prototype.getDayMilliseconds = function () {
		return 24 * 60 * 60 * 1000;
	}; 
	
	function DateDiff(interval,d1,d2) {
		var n = new Date(d1);
		var d = new Date(d2);
		switch (interval) {
			case 'yyyy':
				return d.getFullYear() - n.getFullYear();
				break;
			case 'q':
				return Math.ceil((d.getMonth() + 1) / 3) - Math.ceil((n.getMonth() + 1) / 3) ;
				break;
			case 'm':
				return d.getMonth() - n.getMonth();
				break;
			case 'y':
			case 'd':
			case 'w':
				return Math.floor((d.getTime() - n.getTime()) / n.getDayMilliseconds());
				break;
			case 'ww':
				var fWeek = new Date(n.DateAdd('d', (7 - n.getDay()) % 7));
				return Math.floor(fWeek.DateDiff('d', d2) / 7);
				break;
			case 'h':
				return Math.floor((d.getTime() - n.getTime()) / (n.getDayMilliseconds() / 24));
				break;
			case 'n':
				return Math.floor((d.getTime() - n.getTime()) / (n.getDayMilliseconds() / 24 / 60));
				break;
			case 's':
				return Math.floor((d.getTime() - n.getTime()) / 1000);
				break;
			default:
				break;
		}
	}
