/**
	* @comment A JavaScript string manipulation
	* tools based on PHP and SQL string functions.
	* Distributed under the BSD License.
	*
	* @sample
	* <code>
	*
	* function chunkPrice(price)
	* {
	*   return  price.strrev().chunk_split(3,',').strrev();
	* }
	*
	* alert(chunkPrice('10000')+' $');
	* </code>
	*
	* @package  OoO
	* @author Plumber <plumber@gnu-darwin.org>
	* @since  javascript 1.2
	* @version  $Id: stringUtil.js,v 0.2.0 plumber Exp $
	* @category string
*/
	
	/**
	* public string[] str2array()
	* @note Split string into array
	* @description string[] str2array(int len)
	* Returns an array of strings, each of which is a
	* substring of string formed by splitting it on length.
	*
	* @param int len
	* @return string[]
	* @acces public
	*/
	String.prototype.str2array=function(len){
		var str2array=new Array();
		var token=new String(";;;");
		var strtok=this.chunk_split(len,token);
		var strsplit=strtok.split(token);
		for(var i=0;i<strsplit.length;i++){
			str2array.push(strsplit[i]);
		}
		return str2array;
	};
    
	/**
	* public string chunk_split()
	* @note Split a string into smaller chunks
	* @description string chunk_split(int len , string end)
	* Can be used to split a string into smaller chunks which is useful for
	* e.g. converting base64_encode() output to match RFC 2045 semantics.
	* It inserts end every chunklen characters. It returns the new string
	* leaving the original string untouched.
	*
	* @param int len
	* @param string end
	* @return string
	* @acces public
	*/
	String.prototype.chunk_split=function(len,end){
		var i=0;
		var chunk_split=new String();
		
		while(i+len < this.length){
			chunk_split+=this.substring(i,i+len)+end;
			i+=len;
		}
		
		if(i<this.length){
			chunk_split+=this.substring(i);
		}
		return chunk_split;
	};
    
	/**
	* public string[] str2charAt()
	* @see Deprecated,you should use str2char
	* @note Split string into array
	* @description string[] str2charAt()
	* Returns an array of strings, each of which is a
	* substring of string formed by splitting it on length equal to 1.
	*
	* @return string[]
	* @acces public
	*/
	String.prototype.str2charAt=function(){
		var str2charAt=new Array();
		
		for(var i=0;i<this.length;i++){
			str2charAt.push(this.charAt(i));
		}
		return str2charAt;
	};
   
	/**
	* public string[] str2char()
	* @note Split string into array
	* @description string[] str2charAt()
	* Returns an array of strings, each of which is a
	* substring of string formed by splitting it on length equal to 1.
	*
	* @return string[]
	* @acces public
	*/
	String.prototype.str2char=function(){
		return this.str2array(1);
	};
   
	/**
	* public string str_repeat()
	* @note Repeat a string
	* @description string str_repeat(int x)
	* Returns input_str repeated x times. x has to be greater than or equal to 0.
	* If the x is set to 0, the function will return an empty string.
	*
	* @param int x
	* @return string
	* @acces public
	*/
	String.prototype.str_repeat=function(x){
		var strrepeat=new String();
		for(var i=0;i<x;i++){
			strrepeat+=this;
		}
		return strrepeat;
	};
	  
	/**
	* public string strrev()
	* @note Reverse a string
	* @description string strrev()
	* Returns string, reversed.
	*
	* @return string
	* @acces public
	*/
	String.prototype.strrev=function(){
		var strrev=new String();
		for(var i=this.length-1;i>=0;i--){
			strrev+=this.charAt(i);
		}
		return strrev;
	};
  
	/**
	* public string base64_encode()
	* @note Encodes data with MIME base64
	* @description string base64_encode()
	* Returns data encoded with base64. This encoding is designed to make binary
	* data survive transport through transport layers that are not 8-bit clean,
	* such as mail bodies. Base64-encoded data takes about 33% more space than the original data.
	*
	* @return string
	* @acces public
	*/
	String.prototype.base64_encode=function(){
		var ascii=new String("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
		var encode=new String();
		var bits,dual,i=0;
	
		while(this.length>=i+3){
			bits= (this.charCodeAt(i++) & 0xff) <<16
		 		|(this.charCodeAt(i++) & 0xff) <<8
		 		|(this.charCodeAt(i++) & 0xff);
	
			 encode+= ascii.charAt((bits & 0x00fc0000) >>18)
					 + ascii.charAt((bits & 0x0003f000) >>12)
					 + ascii.charAt((bits & 0x00000fc0) >> 6)
					 + ascii.charAt((bits & 0x0000003f));
		}
	
		if((this.length-i)>0 && (this.length-i)<3){
			dual=Boolean(this.length -i -1);
			bits=((this.charCodeAt(i++) & 0xff) <<16) | (dual ? (this.charCodeAt(i) & 0xff) <<8 : 0);
	
			 encode+= ascii.charAt((bits & 0x00fc0000) >>18)
					 + ascii.charAt((bits & 0x0003f000) >>12)
					 + (dual ? ascii.charAt((bits & 0x00000fc0) >>6) : '=')
					 + '=';
		}
		return encode;
	};
   
	/**
	* public string base64_decode()
	* @note Decodes data encoded with MIME base64
	* @description string base64_decode()
	* Decodes encoded64_data and returns the original data.
	*
	* @return string
	* @acces public
	*/
	String.prototype.base64_decode=function(){
		var ascii=new String("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
		var decode,buffer=new String();
		var bits,i;
		for(i=0;i<this.length; i += 4){
			bits=(ascii.indexOf(this.charAt(i)) & 0xff) <<18
		 	| (ascii.indexOf(this.charAt(i +1)) & 0xff) <<12
		 	| (ascii.indexOf(this.charAt(i +2)) & 0xff) << 6
		 	| (ascii.indexOf(this.charAt(i +3)) & 0xff);
			
			buffer+=String.fromCharCode((bits & 0xff0000) >>16,(bits & 0xff00) >> 8,bits & 0xff);
		}
		
		if(this.charCodeAt(i-2)==61){ decode=buffer.substring(0,buffer.length-2); }
		else if(this.charCodeAt(i-1)==61){ decode=buffer.substring(0,buffer.length-1); }
		else { decode=buffer; }
		return decode;
	};
   
	/**
	* public string trim()
	* @note  Strips whitespace from the beginning and end of a string
	* @description string trim()
	* This function returns a string with whitespace stripped from
	* the beginning and end of string
	*
	* @return string
	* @acces public
	*/
	String.prototype.trim=function(){
		return this.rtrim().ltrim();
	};
	/**
	* public string rtrim()
	* @note  Strips whitespace from the beginning of a string
	* @description string rtrim()
	* This function returns a string with whitespace stripped from
	* the beginning of string
	*
	* @return string
	* @acces public
	*/
	String.prototype.rtrim=function(){
		return this.replace(/[\r\t\n\0\x0B\s]+$/,'');
	};
	
	/**
	* public string rtrim()
	* @note  Strips whitespace from the end of a string
	* @description string rtrim()
	* This function returns a string with whitespace stripped from
	* the end of string
	*
	* @return string
	* @acces public
	*/
	String.prototype.ltrim=function(){
		return this.replace(/^[\r\t\n\0\x0B\s]+/,'');
	};
	
	/**
	* public string Rpad()
	* @note Returns the string value, right-padded
	* @description string Rpad(int len , string token)
	* Returns the string value, right-padded with the string token
	* to a length of int length characters. If string value is longer than int length, the
	* return value is shortened to int len characters.
	*
	* @return string
	* @acces public
	*/
	String.prototype.Rpad=function(len,token){
		return this._pad(len,token,'R');
	};
	
	/**
	* public string Lpad()
	* @note Returns the string value, left-padded
	* @description string Lpad(int len , string token)
	* Returns the string value, left-padded with the string token
	* to a length of int length characters. If string value is longer than int length, the
	* return value is shortened to int len characters.
	*
	* @return string
	* @acces public
	*/
	String.prototype.Lpad=function(len,token){
		return this._pad(len,token,'L');
	};
	
	/**
	* private string _pad()
	* @note Decodes data encoded with MIME base64
	* @description string _pad(int len , string token , string index)
	*
	* @return string
	* @acces private
	*/
	String.prototype._pad=function(len,token,index){
		_pad=new String();   
		if(this.length==len){ _pad=this; }
		else if(this.length > len){
		   if(index=='R'){ _pad=this.substr(0,len); }
		   if(index=='L'){ _pad=this.strrev().substr(0,len).strrev(); }   
		}else{
		   if(index=='R'){ _pad=this+token.str_repeat(len-this.length); }
		   if(index=='L'){ _pad=token.str_repeat(len-this.length)+this; }
		}
		return _pad;
	};
