﻿/* 20090205-1907 */
//=-=-=-=-=-=-=-=-
// Função para eliminar caracteres brancos (espaços, tabulações, etc.)
// no ínicio e fim de uma determinada string
//=-=-=-=-=-=-=-=-
String.prototype.trim = function(){
	return this.replace(/\s{2,}/g,' ').replace(/^\s*|\s*$/g,'');
};

//=-=-=-=-=-=-=-=-
// Função para truncar o texto sem cortar palavras
// n = número máximo de caracteres
// c = elemento que indica uma string cortada (opcional - padrão: "...")
//=-=-=-=-=-=-=-=-
String.prototype.truncate = function(n,c){
	if (n==0) return this;
	var str =this.trim();
	c=c || "...";		
	// Ajusta a contagem em caracteres extendidos
	var ext = /&[^;\s]*;/g;
	if (str.match(ext)) {
		for(var no=0; no <str.match(ext).length;no++){
			n = n + (str.match(ext)[no].length -1);
		}
	}	
	//alert(str +'\n' + 'length: '+ str.length + 'limite: '+ n);
	if(str.length<=n){
		return str;
	}
	str=str.substr(0, n + 1);
	var p1=str.lastIndexOf(" ");
	var p2=str.lastIndexOf("\n");
	var p3=str.lastIndexOf("\t");
	var p4=str.lastIndexOf("\s");
	var pos=Math.max(p1, p2, p3, p4);
	if(pos>0){
		str=str.substr(0, pos);
	}
	str=str.trim();
	return str+c;
}

//=-=-=-=-=-=-=-=-
// Função responsável por separar os dígitos de milhar dos dígitos de centena com o ponto
//=-=-=-=-=-=-=-=-
String.prototype.milhar = Number.prototype.milhar = function(){
	var str = this + '';
	while (str.match(/^\d{4}/)){
		str = str.replace(/(\d)(\d{3}(\.|$))/, '$1.$2');
	}
	return str;
}

//=-=-=-=-=-=-=-=-
// Função para setar o número de digitos de um número
//=-=-=-=-=-=-=-=-
String.prototype.digits = Number.prototype.digits = function(numDigits){
    var str = this.toString();
    while (str.length < numDigits)
        str = "0"+str;
    return str;
}