//------------------------------------------------------------------
//字串相關之function
//------------------------------------------------------------------


//=============================================
//function rtrim(str)
//desc:		去右邊空白
//input:	str
//output:	str
//=============================================
function rtrim(str) 
{
	if (str == null) return "";
	while(''+str.charAt(str.length-1) ==' ')
		str = str.substring(0,str.length-1);
	return str;
}

//=============================================
//function ltrim(str)
//desc:		去右邊空白
//input:	str
//output:	str
//=============================================
function ltrim(str) 
{
	if (str == null) return "";
	while(''+str.charAt(0) ==' ')
		str = str.substring(1,str.length);
	return str;
}

//=============================================
//function trim(str)
//desc:		字串去前後空白
//input:	str
//output:	str
//=============================================
function trim(str) 
{
	return ltrim(rtrim(str));
}

//=============================================
//function strTran(value,oldStr,newStr)
//desc:		字串取代 
//input:	value 	- 原始字串
//          oldStr  - 舊字串
//          newStr  - 新字串
//output:	str
//=============================================
function strTran(value,oldStr,newStr)
{
		var rv="";
		var str=value.split(oldStr);
		rv=str.join(newStr);
		return rv;
}

//=============================================
//function fillStr(source, len, fillStr)
//desc:		將傳入的內容不足位時，於內容前方補足指定的字
//input:	source 	- 要檢核長度的內容
//          len   - 要檢核的長度
//          fillStr- 不足位時，於前方補足長度的指定字
//output:	str
//=============================================
function fillStr(source, len, fillStr)
{
		if (source.length >= len)
			return source;
		var	tmpStr	= source;
		while(tmpStr.length != len)
		{
			tmpStr	= '' + fillStr + tmpStr;
		}
		return tmpStr;
}


//=============================================
//function fillzero(num,limit)
//desc:		依輸入欄位設定的長度,不足長度時在資料前補零
//input:	num 數字
//			limit 長度
//output:	null
//=============================================
function fillzero(num,limit) {
	var s = '';
	var temp='';
	s = num+'';
	temp= s;
	if(s.length<limit) {
	   for(i=0;i<(limit - s.length);i++) {
		temp = '0'+temp;
	   } 
	}
	return temp;
}

//=============================================
//function fillBackzero(source, len)
//desc:		將傳入的內容不足位時，於內容後方補足指定的字
//input:	source 	- 要檢核長度的內容
//          len   - 要檢核的長度
//output:	str
//=============================================
function fillBackzero(source, len)
{
		if (source.length >= len)
			return source;
		var	tmpStr	= source;
		while(tmpStr.length != len)
		{
			tmpStr	= '' + tmpStr + '0' ;
		}
		return tmpStr;
}

//=============================================
//function fillBackspace(source, len)
//desc:		將傳入的內容不足位時，於內容後方補足指定的字
//input:	source 	- 要檢核長度的內容
//          len   - 要檢核的長度
//output:	str
//=============================================
function fillBackspace(source, len)
{
		if (source.length >= len)
			return source;
		var	tmpStr	= source;
		while(tmpStr.length != len)
		{
			tmpStr	= '' + tmpStr + ' ' ;
		}
		return tmpStr;
}

//=============================================
//function getBStr(str, len)
//desc:		傳回指定長度字串(中文為兩碼)
//input:	str   - 原始字串
//          len   - 截取長度
//output:	str
//=============================================
function getBStr(str, len)
{
		var	bStr	= "";
		var	bLength	= 0;
		
		for (var i = 0; i < str.length; i++)
		{
			if(str.charCodeAt(i) == 13 || str.charCodeAt(i) == 10)
			{
				bStr	= bStr + str.substr(i, 1);
				continue;
			}
				
			if (str.charCodeAt(i) >= 128 || str.charCodeAt(i) <= -128)
				bLength	= bLength + 2;
			else
				bLength++;
	
			if (bLength > len)
				return bStr;
			else
				bStr	= bStr + str.substr(i, 1);
		}
		return bStr;
}

//=============================================
//function compareStr(sourceStr, searchStr)
//desc:		字串比較
//input:	sourceStr - 原始字串
//          searchStr - 搜尋字串
//output:	boolean
//例：
//	(1)
//        sourceStr   = '中華民國萬萬歲';
//        searchStr   = '%萬歲';
//
//        會 return true
//    (2)
//        sourceStr   = '中華民國萬萬歲';
//        searchStr   = '萬歲%';
//        
//        會 return false
//    (3)
//        sourceStr   = '中華民國萬萬歲';
//        searchStr   = '中華民國萬萬歲';
//        
//        會 return true
//=============================================
function compareStr(sourceStr, searchStr)
{
		if (searchStr == '%')
			return false;
			
		if (sourceStr == searchStr)
			return true;
		
		if (sourceStr.indexOf(searchStr.substring(0, searchStr.length - 1)) == 0 && searchStr.indexOf('%') != -1)
			return true;
		
		if (sourceStr.lastIndexOf(searchStr.substring(1)) == sourceStr.length - searchStr.substring(1).length && searchStr.indexOf('%') != -1)
			return true;
			
		return false;
}

//=============================================
//function getLength(str)
//desc:		傳回字串長度,有分中英文(英文=1,中文=2)
//input:	str 	
//output:	int
//=============================================
function getLength(str) {
		var s,i=0,charCount=0,result=true;
		
		while (i++<str.length)
		{
			s = str.charCodeAt(i-1);
			if (s<127)
				charCount++;
			else
				charCount+=2;
		}
		return charCount;
}


//=============================================
//function encode(str)
//desc:		處理中文問題,將中文做編碼
//input:	str 	  中文字串
//output:	encodeStr 編碼後的中文
//=============================================
function encode(str)
{
		var	encodeStr	= '';
		var	tmpChar		= '';
		
		for (var i = 0; i < str.length; i++)
		{
			tmpChar	= str.substr(i, 1);
			if (tmpChar.match(/[a-z,A-Z,0-9]/g) != null || tmpChar == '-' || tmpChar == '_' || tmpChar == '.' || tmpChar == '*')
			{
				encodeStr	+= tmpChar;
				continue;
			}
			
			if (tmpChar == ' ')
			{
				encodeStr	+= "+";
				continue;
			}
			
			if (str.charCodeAt(i) > 255)
				encodeStr	+= escape(str.substr(i, 1));
			else
				encodeStr	+= '%' + str.charCodeAt(i).toString(16).toUpperCase();
		}
		return encodeStr;
}


function ConvertStr(sBeConvert)
{
		var Result="";
		for(i=sBeConvert.length-1;i>=0;i--)
			  Result=Result+sBeConvert.charAt(i);
		return Result;
}


//=============================================
//function getBaseUrl(urlString)
//desc:		取得基本的連結	Exp: getBaseUrl("http://localhost/xx/xx.jsp?xx=xx&vv=vv")	--> http://localhost/xx/xx.jsp
//input:	urlString - URL
//output:	str
//=============================================
function getBaseUrl(urlString)
{
		if (urlString.indexOf('?') == -1)
			return urlString;
		else
			return urlString.substr(0, urlString.indexOf('?'));
}

//=============================================
//function getCurrentPage(urlString)
//desc:		取得目前的頁面名稱	Exp: getCurrentPage("http://localhost/xx/xx.jsp?xx=xx&vv=vv")	--> xx.jsp
//input:	urlString - URL
//output:	str
//=============================================
function getCurrentPage(urlString)
{
		var	baseUrl	= getBaseUrl(urlString);
	
		return baseUrl.split('/')[baseUrl.split('/').length - 1];
}

function getSplitStr(str, splitLen, getLine)
{
		if (getLine * splitLen > str.length)
			return "";
		else if ((getLine + 1) * splitLen > str.length)
			return str.substring(getLine * splitLen);
		else
			return str.substr(getLine * splitLen, splitLen);
}

function getDynSplitStr(str, splitLen, getLine)
{
		var	splitLenAry	=	splitLen.split(",");
		var	lenCount	=	0;
		for (var i = 0; i < splitLenAry.length; i++)
		{
			if (i < getLine)
				lenCount	+=	parseInt(splitLenAry[i], 10);
		}
		
		if (lenCount > str.length)
			return "";
		else if (parseInt(splitLenAry[getLine], 10) + lenCount > str.length)
			return str.substring(lenCount);
		else
			return str.substr(lenCount, parseInt(splitLenAry[getLine], 10));
}