String.prototype.soAlgarismos = function() {
	return this.replace(/\D/g,'');// qualquer coisa diferente de dígito (0 a 9)
}
String.prototype.superTrim = function() {
	var s=this.replace(/^\s+|\s+$/g,'');// um espaço ou mais no início ou no final da linha: removemos.
	return s.replace(/\s{2,}/g,' ');// dois ou mais espaços juntos: substituimos por um único espaço.
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,'');// um espaço ou mais no início ou no final da linha: removemos.
}
String.prototype.emailValido = function() {
	return ( /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test( this ) );
}

String.prototype.cpfFormat = function() {
	var s = "00000000000" + this.soAlgarismos();
	s = s.match(/\d{11}$/, s);
 	s = (s[0] || s);
 	return s.replace( /(\d\d\d)(\d\d\d)(\d\d\d)(\d\d)$/, "$1.$2.$3-$4" );
}

String.prototype.cpfOK = function() { 
	var s = this.soAlgarismos();
 	if ( s == '' ) return false;
	while ( s.length < 11 ) s = "0" + s;
 	if ( s.length > 11 ) s = s.substr( 0, 11 );
	if ( s.cpfDVOK() ) {
	 	return s.replace( /(\d\d\d)(\d\d\d)(\d\d\d)(\d\d)/, "$1.$2.$3-$4" );
	} else { return false;} 
 };

String.prototype.cpfDVOK = function () {
	var CPFaux = this;
 	NR_CPF = CPFaux.substr( 0, 9 );
	var rcpf2 = CPFaux.substr( 9, 2 );
	if ( ( NR_CPF == null || NR_CPF == 0 ) ) return false;
	
	var strRangeInvalido = '0000000000019111111111112222222222233333333333444444444445555555555566666666666777777777778888888888899999999999';
	if ( strRangeInvalido.indexOf( CPFaux ) > -1 ) return false;

 	d1 = 0;
 	for ( i = 0; i < 9; i++ ) d1 += NR_CPF.charAt( i ) * ( 10 - i );
	d1 = 11 - ( d1 % 11 );
	if ( d1 > 9 ) d1 = 0;
	if ( rcpf2.charAt( 0 ) != d1 ) return false;

	d1 *= 2;
	for ( i = 0; i < 9; i++ ) d1 += ( NR_CPF.charAt( i ) * ( 11 - i ) );
	d1 = 11 - ( d1 % 11 );
	if ( d1 > 9 ) d1 = 0;
	if ( rcpf2.charAt( 1 ) != d1 ) return false;
	
 	return true;
}

String.prototype.toDate = function() {
	return new Date().parseBR(this);
}

Date.prototype.formatBRExtended = function(format) {
/*
%d	Displays the day as a number without a leading zero (for example, 1).

%dd	Displays the day as a number with a leading zero (for example, 01).

%ddd	Displays the day as an abbreviation (for example, Sun).

%dddd	Displays the day as a full name (for example, Sunday).

%M	Displays the month as a number without a leading zero 
		(for example, January is represented as 1).

%MM	Displays the month as a number with a leading zero (for example, 01/12/01).

%MMM	Displays the month as an abbreviation (for example, Jan).

%MMMM	Displays the month as a full month name (for example, January).

%yy	Displays the year in two-digit numeric format with a leading zero, if applicable.

%yyyy	Displays the year in four-digit numeric format.
 
%h	Displays the hour as a number without leading zeros using the 12-hour clock
		(for example, 1:15:15 PM).

%hh	Displays the hour as a number with leading zeros using the 12-hour clock
		(for example, 01:15:15 PM).

%H	Displays the hour as a number without leading zeros using the 24-hour clock
		(for example, 1:15:15).

%HH	Displays the hour as a number with leading zeros using the 24-hour clock
		(for example, 01:15:15).

%m	Displays the minute as a number without leading zeros
		(for example, 12:1:15).

%mm	Displays the minute as a number with leading zeros
		(for example, 12:01:15).

%s	Displays the second as a number without leading zeros
		(for example, 12:15:5).

%ss	Displays the second as a number with leading zeros
		(for example, 12:15:05).

Utilização
---------- 
var s = new Date().formatBRExtended("São Paulo, %d de %MMMM de %yyyy.")
* produz: São Paulo, 2 de maio de 2009.

var s = new Date().formatBRExtended("%s %d-%N.")
* produz: Sab 2-maio.

Se precisar utilizar alguma das sequências especiais
como literal, utilize contra-barra antes do percent:
var s = new Date().formatBRExtended("\%S é o dia da semana: %S")
* para 2/maio/2009 produz: %S é o dia da semana: sábado.

*/

	var debug = new String();
	format = format.toString();
	var d = this;
	var s = new String();
//	var s = new Array( d.getDate(), d.getMonth() + 1, d.getFullYear(), d.getHours(), d.getMinutes, d.getSeconds() );
	var arr = Array();
	
	// buscando os caracteres % marcados como literal: \%
	// substituímos por "\% "
	format = format.replace(/\\%/g, "\\% ");

	// procurando o dia da semana em 'formato'
	if (/%d{3,4}/.test(format)) {
		if (/%dddd/.test(format)) {
			arr = "domingo segunda terça quarta quinta sexta sábado".split(" ");
			format = format.replace(/%dddd/g, arr[d.getDate()]);
		} 
		if (/%ddd/.test(format)) {
			arr = "dom seg ter qua qui sex sáb".split(" ");
			wd = arr[d.getDate];
			format = format.replace(/%ddd/g, arr[d.getDate()]);
		} 
	}
	// procurando o dia do mês em 'formato'
	if (/[%d{1,2}]/.test(format)) {
		s = d.getDate();
		if (/%dd/.test(format)) { // 01 to 31
			if (s.length < 2 ) s = '0' + s;
			format = format.replace(/%dd/g, s);
		} 
		if (/%d/.test(format)) { // 1 to 31
			format = format.replace(/%d/g, s);
		} 
	}

	// procurando o mês em 'formato'
	if (/%M{3,4}/.test(format)) {
		// formato texto
		if (/%MMMM/.test(format)) { 
			arr = "janeiro fevereiro março abril maio junho julho agosto setembro outubro novembro dezembro".split(" ");
			mes = arr[d.getMonth()]; 
			format = format.replace(/%MMMM/g, mes);
		} 
		if (/%MMM/.test(format)) { 
			arr = "jan fev mar abr mai jun jul ago set out nov dez".split(" ");
			mes = arr[d.getMonth()]; 
			format = format.replace(/%MMM/g, mes);
		} 
	}
	if (/%M{1,2}/.test(format)) {
		// formato numérico
		s = d.getMonth().toString();
		if (/%MM/.test(format)) { // 01 to 12
			if (s.length < 2 ) s = '0' + s;
			format = format.replace(/%MM/g, s);
			debug += format + "\n";
		} 
		if (/%M/.test(format)) { // 1 to 12
			format = format.replace(/%M/g, s);
		} 
	}

	// procurando o ano em 'formato'
	if (/[%yy|%yyyy]/.test(format)) {
		s = d.getFullYear().toString();
		if (/%yyyy/.test(format)) { // ano com 4 dígitos
			format = format.replace(/%yyyy/g, s);
		} 
		if (/%yy/.test(format)) { // ano com 2 dígitos
			format = format.replace(/%yy/g, s.toString().substr(2));
		} 
	}
	
	// procurando a hora em 'formato'
	if (/%h{1,2}/.test(format)) {
		s = d.getHours().toString();
		if (/%hh/.test(format)) { // hora com 2 dígitos
			if (s.length < 2 ) s = '0' + s;
			format = format.replace(/%hh/g, s);
		} 
		if (/%h/.test(format)) { // hora com 1 ou mais dígitos
			format = format.replace(/%h/g, s);
		} 
	}

	// procurando os minutos em 'formato'
	if (/%m{1,2}/.test(format)) {
		s = d.getMinutes().toString();
		if (/%mm/.test(format)) { // hora com 2 dígitos
			if (s.length < 2 ) s = '0' + s;
			format = format.replace(/%mm/g, s);
		} 
		if (/%m/.test(format)) { // hora com 1 ou mais dígitos
			format = format.replace(/%m/g, s);
		} 
	}
	
	// procurando os segundos em 'formato'
	if (/%s{1,2}/.test(format)) {
		s = d.getSeconds().toString();
		if (/%ss/.test(format)) { // hora com 2 dígitos
			if (s.length < 2 ) s = '0' + s;
			format = format.replace(/%ss/g, s);
		} 
		if (/%s/.test(format)) { // hora com 1 ou mais dígitos
			format = format.replace(/%s/g, s);
		} 
	}
	
	format = format.replace(/\\%\s/g, "%");
	return format;


}


Date.prototype.formatBR = function(format) {
	format = (format || "")
	if ((format || "") == "") {
		var s = new Array( this.getDate(), this.getMonth() + 1, this.getFullYear() );
		if (s[0].toString().length < 2 ) s[0] = '0' + s[0].toString();
		if (s[1].toString().length < 2 ) s[1] = '0' + s[1].toString();
		return s.join("/");
	}
	// else...
	return this.formatBRExtended(format);
}

Date.prototype.parseBR = function( s ) {
  var re = /^((0?[1-9]|[12][0-9]|3[01])\D+(0?[1-9]|1[012])(\D+((19|20)?\d\d))?|\d{4}|\d{6}|\d{8})$/;
  if ( !re.test( s ) || s=='' ){ return null; };
  re = /\D/;								//qualquer coisa que não seja um algarismo
  if ( !re.test(s) ) {			// não colocou delimitador, apenas formatamos
		re = /(\d\d)(\d\d)(\d\d)?(\d\d)?/;	//isto formata a data com barras !!!
    s = s.replace( re, "$" + "1/$" + "2/$" + "3$4" ); // evitando o bug do Joomla
  } else {									// colocou delimitadores, substituímos por '/'		
    re = /\D+/g;						// qualquer coisa que não seja um algarismo, uma ou mais vezes
    s = s.replace(re,'/');	// substituímos por uma barra
  };
  s = s.split( '/' );
  s[2] = ( ( s[2] || '') == '' ? this.getFullYear() : s[2] );
  if ( s[2] < 100 ) {
	  s[2] = Number( s[2] );
  	s[2] += ( s[2] < 30 ? 2000 : 1900 )
  }
  this.setFullYear( s[2], s[1] - 1, s[0] );
  return this;
}

var Url = {
	/**
	*
	* URL encode / decode
	* http://www.webtoolkit.info/
	*
	*	para utilizar:
	* onkeyup="this.form.hash.value = Url.encode(this.value)"
	**/

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
	  var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = 0;
		for (var i = 0; i < utftext.length; i++) {
	    c = utftext.charCodeAt(i);
	    if (c < 128) { string += String.fromCharCode(c);}
	    else if ((c > 191) && (c < 224)) {
				string += String.fromCharCode( ((c & 31) << 6) | (utftext.charCodeAt(++i) & 63)	);
			}
	    else {
				string += String.fromCharCode(
					((c & 15) << 12) | ((utftext.charCodeAt(++i) & 63) << 6) | (utftext.charCodeAt(++i) & 63)
				);
			}
		}
		return string;
	}
}

