String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");}
String.prototype.ltrim = function() {return this.replace(/^\s+/,"");}
String.prototype.rtrim = function() {return this.replace(/\s+$/,"");}
String.prototype.onlynumberletter = function() {return this.replace(/\W/g,"");}
String.prototype.onlynumber = function() {return this.replace(/\D/g,"");}
String.prototype.onlyletter = function() {return this.replace(/(\W)|(\d)/g,"");}
String.prototype.strzero = function(XTamanho) {
	_SPTexto = " 0000000000000000000000000000000000000000000000000000000" + this.replace(/\D/g,"");
	return _SPTexto.substring(_SPTexto.length,_SPTexto.length-XTamanho);
}
// JavaScript Document
function Trim(XTexto) {
	return XTexto.replace(/^\s+|\s+$/g,"");
}
function Redir(v){
	window.location.href=v;
}
function AjaxXML(){
	XAjaxXML = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		XAjaxXML = new XMLHttpRequest();
		if (XAjaxXML.overrideMimeType) {
			XAjaxXML.overrideMimeType('text/html');
		}
	}
	else if (window.ActiveXObject) { // IE
		try {
			XAjaxXML = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				XAjaxXML = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	if (!XAjaxXML) {alert('Falha ao efetuar procedimento'); return false;}
	return XAjaxXML;
}
function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}
function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}
function soNumeros(v){
    return v.replace(/\D/g,"")
}
function telefone(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca parênteses em volta dos dois primeiros dígitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
    return v
}
function cpf(v){
    v=v.replace(/\D/g,"")                    //Remove tudo o que não é dígito
    v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
    v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
                                             //de novo (para o segundo bloco de números)
    v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2") //Coloca um hífen entre o terceiro e o quarto dígitos
    return v
}
function cfop(v){
    v=v.replace(/\D/g,"")                    //Remove tudo o que não é dígito
    v=v.replace(/(\d{1})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
    return v
}
function cst(v){
    v=v.replace(/\D/g,"")                    //Remove tudo o que não é dígito
    v=v.replace(/(\d{1})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
    return v
}
function ncm(v){
    v=v.replace(/\D/g,"")                           //Remove tudo o que não é dígito
    v=v.replace(/^(\d{4})(\d)/,"$1.$2")             //Coloca ponto entre o segundo e o terceiro dígitos
    v=v.replace(/^(\d{4})\.(\d{2})(\d)/,"$1.$2.$3") //Coloca ponto entre o quinto e o sexto dígitos
    return v
}
function cep(v){
    v=v.toUpperCase()             //Maiúsculas
    v=v.replace(/\D/g,"")                //Remove tudo o que não é dígito
    v=v.replace(/^(\d{5})(\d)/,"$1-$2") //Esse é tão fácil que não merece explicações
    return v
}
function hora(v){
    v=v.replace(/\D/g,"")                //Remove tudo o que não é dígito
    v=v.replace(/^(\d{2})(\d)/,"$1:$2")             //Coloca ponto entre o segundo e o terceiro dígitos
    v=v.replace(/^(\d{2})\:(\d{2})(\d)/,"$1:$2:$3") //Coloca ponto entre o quinto e o sexto dígitos
    return v
}
function data(v){
    v=v.replace(/\D/g,"")                //Remove tudo o que não é dígito
    v=v.replace(/^(\d{2})(\d)/,"$1/$2")             //Coloca ponto entre o segundo e o terceiro dígitos
    v=v.replace(/^(\d{2})\/(\d{2})(\d)/,"$1/$2/$3") //Coloca ponto entre o quinto e o sexto dígitos
    return v
}
function cnpj(v){
    v=v.replace(/\D/g,"")                           //Remove tudo o que não é dígito
    v=v.replace(/^(\d{2})(\d)/,"$1.$2")             //Coloca ponto entre o segundo e o terceiro dígitos
    v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3") //Coloca ponto entre o quinto e o sexto dígitos
    v=v.replace(/\.(\d{3})(\d)/,".$1/$2")           //Coloca uma barra entre o oitavo e o nono dígitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")              //Coloca um hífen depois do bloco de quatro dígitos
    return v
}

function Mid(str, start, len)
{
	start=start-1;
    if (start < 0 || len < 0) return "";
    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
          iEnd = iLen;
    else
          iEnd = start + len;
    return String(str).substring(start,iEnd);
}
function OnlyNumber(campo){   
var digits="0123456789/-"   
var campo_temp   
    for (var i=0;i<campo.value.length;i++){   
        campo_temp=campo.value.substring(i,i+1)   
        if (digits.indexOf(campo_temp)==-1){   
            campo.value = campo.value.substring(0,i);   
        }   
    }   
}
function StrZero(XNumero,XTamanho){
	XNumero = " "+XNumero
	XNumero=XNumero.replace(/\D/g,"");
	XNumero="000000000000000000000000000000" + XNumero;
	XNumero=XNumero.substring(XNumero.length,XNumero.length-XTamanho);
	return(XNumero);
}
function DataAtual(){
	var XData = new Date();
	var XDataAtual = StrZero(XData.getDate(),2)+"/"+StrZero(XData.getMonth()+1,2)+"/"+(""+XData.getYear()).substr(2,2)
	return XDataAtual;
}
function HoraAtual(){
	var XHora = new Date();
	var XHoraAtual = StrZero(XHora.getHours(),2)+":"+StrZero(XHora.getMinutes(),2);
	return XHoraAtual;
}
function CalculoTotalPerc(XTotal, XValor) {
	if (parseFloat(XTotal)==0 || parseFloat(XValor)==0) {
		return 0;
	} else {
		return (XValor*100)/XTotal;
	}
}
function CalculoPercTotal(XValor, XTotal) {
	if (parseFloat(XTotal)==0 || parseFloat(XValor)==0) {
		return 0;
	} else {
		return (XTotal/100)*XValor;
	}
}
function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber; // Output the result to the form field (change for your purposes)
}
function VarSpace(XTexto){
	XTexto=XTexto.replace(/^\s+|\s+$/g,"")
	if (XTexto=="") {return ("&nbsp;")} else {return XTexto}
}
function DataId(XData){
	if (verifica_data(XData)){
		var XDataAtual = new Date();
		if (XData.substr(6,2)>(""+XDataAtual.getYear()+10)) {
			return "19"+XData.substr(6,2)+XData.substr(3,2)+XData.substr(0,2);
		} else {
			return "20"+XData.substr(6,2)+XData.substr(3,2)+XData.substr(0,2);
		}
	}else{
		return ''
	}
}
function IdData(XData) {
	var XDataValid=XData.substr(6,2)+"/"+XData.substr(4,2)+"/"+XData.substr(2,2);
	if (verifica_data(XDataValid)){
		return XDataValid;
	}else{
		return ''
	}
}
function verifica_data(digdata) 
{
    var bissexto = 0;
    var data = digdata; 
    var tam = data.length;
        var dia = data.substr(0,2)
        var mes = data.substr(3,2)
		var ano = "";
		if (tam == 8) 
		{
			var ano = data.substr(6,2)
		}
		if (tam == 10) 
		{
			var ano = data.substr(8,2)
		}
		switch (mes) 
		{
			case '01':
			case '03':
			case '05':
			case '07':
			case '08':
			case '10':
			case '12':
				if  (dia <= 31) 
				{
					return true;
				}
				break
			
			case '04':        
			case '06':
			case '09':
			case '11':
				if  (dia <= 30) 
				{
					return true;
				}
				break
			case '02':
				/* Validando ano Bissexto / fevereiro / dia */ 
				if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) 
				{ 
					bissexto = 1; 
				} 
				if ((bissexto == 1) && (dia <= 29)) 
				{ 
					return true;                 
				} 
				if ((bissexto != 1) && (dia <= 28)) 
				{ 
					return true; 
				}            
				break                        
		}  
    return false;
}
function iif(ZOperacao,XTrue,XFalse){
	if(ZOperacao){
		return XTrue;
	} else {
		return XFalse;
	}
}
function BotaoVisivel(XBotao,XVisivel){
	if (XVisivel) {
//		if (document.getElementById(XBotao).value=="Avançar" || document.getElementById(XBotao).value=="Voltar") {
//			document.getElementById(XBotao).className="BotaoA"
//		} else {
			document.getElementById(XBotao).className="Botao"
//		}
	} else {
		document.getElementById(XBotao).className="BotaoInvisivel"
	}
}
function ValidAjax(){
	if (document.getElementById('AJAXOK')==null) {return false} else {return true}
}
function setClass(XObjeto,XClasse){
	if(navigator.appName=='Netscape'){
		XObjeto.setAttribute('class', XClasse);
	}else{
		XObjeto.className= XClasse;
	}
}
function InStr(XPosicao, XTexto, XPesquisar){
	// Retorna a posição do primeiro texto de XPesquisar em XTexto
	// Se informar XPosicao, começará desse campo
	// Se não informar XPosicao, os parametros serão XTexto e XPesquisar
	var numargs=InStr.arguments.length;	
	if(numargs<3)
		return XPosicao.indexOf(XTexto)+1;
	else
		return XTexto.indexOf(XPesquisar, XPosicao)+1;
}
function NumeroDias(mes,ano) {
	if((mes<8 && mes%2==1) || (mes>7 && mes%2==0)) return 31;
	if(mes!=2) return 30;
	if(ano%4==0) return 29;
	return 28;
}
function AddData(data, dias) {
	data=data.split('/');
	XDiaFuturo=parseInt(data[0])+dias;
	mes=parseInt(data[1]);
	ano=parseInt(data[2]);
	while(XDiaFuturo>NumeroDias(mes,ano)) {
		XDiaFuturo-=NumeroDias(mes,ano);
		mes++;
		if(mes>12) {
			mes=1;
			ano++;
			}
		}
	return XDiaFuturo+"/"+mes+"/"+ano;
}
function formatar_moeda(campo, casas_decimais, tecla) {
//	if (document.selection.createRange().text != "0,00" && document.selection.createRange().text != "" || document.selection.createRange().text == "0,00") {
//		document.all("DEBUG").innerHTML = document.selection.createRange().text;
//		document.selection.clear();
//	}
	var sep = 0;
	var f = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	//var whichCode = (window.Event) ? tecla.which : tecla.keyCode;
	var whichCode = tecla.keyCode ? tecla.keyCode : tecla.which ? tecla.which : tecla.charCode;
	var tecla = campo.value;
	
	key = String.fromCharCode(whichCode); // Pegando o valor digitado
	if (whichCode >= 96 && whichCode <= 105) key = String.fromCharCode(whichCode-48);
	if (whichCode == 13) return true; // Tecla Enter
	if (whichCode == 9) return true; // Tecla Tab
	if (whichCode == 16) return true; // Tecla Shif
	if (whichCode == 8 && campo.value.length>0) {
		campo.value=campo.value.substr(0,campo.value.length-1);
		key=""
		if (campo.value=="0,0") {key="0"};
		if (campo.value=="0,00") {key="0"};
		if (campo.value=="0,000") {key="0"};
		if (campo.value=="0,0000") {key="0"};
	}
	if (strCheck.indexOf(key) == -1 && whichCode != 8) return false; // Valor inválido (não inteiro)	
	if (document.selection.createRange().text != "") {
		campo.value=0;
		document.selection.clear();
	}
	len = campo.value.length;
	for(i = 0; i < len; i++)
	if ((campo.value.charAt(i) != '0') && (campo.value.charAt(i) != ",")) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(campo.value.charAt(i))!=-1) aux += campo.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == campo.maxLength) return false;
	if (len == 0) campo.value = '';
	if (casas_decimais == 0)
		{
			aux2 = ""
			for(f=0; f<=len; f++)
			{
				aux2 += aux.substr(f,1)
				if (f == len-4) {aux2 += "."};
				if (f == len-7) {aux2 += "."};
				if (f == len-10) {aux2 += "."};
			}
			campo.value = aux2
		}
	if (casas_decimais == 2)
		{
			aux2 = ""
			if (len == 1) aux = "00" + aux;
			if (len == 2) aux = "0" + aux;
			len = aux.length;
			for(f=0; f<=len; f++)
			{
				aux2 += aux.substr(f,1)
				if (f == len-3) {aux2 += ","};
				if (f == len-6) {aux2 += "."};
				if (f == len-9) {aux2 += "."};
			}
			campo.value = aux2
		}
	if (casas_decimais == 3)
		{
			aux2 = ""
			if (len == 1) aux = "000" + aux;
			if (len == 2) aux = "00" + aux;
			if (len == 3) aux = "0" + aux;
			len = aux.length;
			for(f=0; f<=len; f++)
			{
				aux2 += aux.substr(f,1)
				if (f == len-4) {aux2 += ","};
				if (f == len-7) {aux2 += "."};
				if (f == len-10) {aux2 += "."};
			}
			campo.value = aux2
		}
//	if (whichCode == 8) return true; // Tecla Delete
	return false;
}
function verifica_data(digdata) 
{
    var bissexto = 0;
    var data = digdata; 
    var tam = data.length;
        var dia = data.substr(0,2)
        var mes = data.substr(3,2)
		var ano = "";
		if (tam == 8) 
		{
			var ano = data.substr(6,2)
		}
		if (tam == 10) 
		{
			var ano = data.substr(8,2)
		}
		switch (mes) 
		{
			case '01':
			case '03':
			case '05':
			case '07':
			case '08':
			case '10':
			case '12':
				if  (dia <= 31) 
				{
					return true;
				}
				break
			
			case '04':        
			case '06':
			case '09':
			case '11':
				if  (dia <= 30) 
				{
					return true;
				}
				break
			case '02':
				/* Validando ano Bissexto / fevereiro / dia */ 
				if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) 
				{ 
					bissexto = 1; 
				} 
				if ((bissexto == 1) && (dia <= 29)) 
				{ 
					return true;                 
				} 
				if ((bissexto != 1) && (dia <= 28)) 
				{ 
					return true; 
				}            
				break                        
		}  
    return false;
}
