var valida = '';

function imprimir(rel,cab) {
	var taction = document.frm.action;
	document.frm.action = 'conteudo_print.php?print=s&prefixo=relatorio/&menu=' + rel +'&cabecalho=' + cab;
	document.frm.target = "_blank";
	document.frm.submit();
	document.frm.target = "_self";
	document.frm.action = taction;
	return false;
}
function imprimir_pdf(rel,cab) {
	var taction = document.frm.action;
	document.frm.action = 'conteudo_print.php?print=p&prefixo=relatorio/&menu=' + rel +'&cabecalho=' + cab;
	document.frm.target = "_blank";
	document.frm.submit();
	document.frm.target = "_self";
	document.frm.action = taction;
	return false;
}

// When the page loads:
function ativaObj() {
    if (document.getElementsByTagName) {
        // Get all the tags of type object in the page.
        var objs = document.getElementsByTagName("object");
        for (i=0; i<objs.length; i++) {
            // Get the HTML content of each object tag
            // and replace it with itself.
            objs[i].outerHTML = objs[i].outerHTML;
        }
    }
}

window.onload = ativaObj;

function desativaObj() {
// When the page unloads:
    if (document.getElementsByTagName) {
        //Get all the tags of type object in the page.
        var objs = document.getElementsByTagName("object");
        for (i=0; i<objs.length; i++) {
            // Clear out the HTML content of each object tag
            // to prevent an IE memory leak issue.
            objs[i].outerHTML = "";
        }
    }
}

window.onunload = desativaObj;

function OpenWin(URL, Nome, W, H, T, L, resizable)
{
    if (T == undefined)
        T = '200';
    
    if (L == undefined)
        L = '130';
    
    if (resizable == undefined)
        resizable = 'no';
	
    var Janela = window.open(URL, Nome,'alwaysLowered=yes,alwaysRaised=no,dependent=yes,directories=no,hotkeys=no,location=no,menubar=no,personalbar=no,resizable=' + resizable + ',scrollbars=yes,status=no,titlebar=no,toolbar=no,width=' + W + ',height=' + H + ',z-lock=yes,left=' + L + ',top=' + T)
}

function getPosicaoElemento(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

function getPosicaoElementoParent(elemID) {
    var offsetTrail = parent.document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    
    if (navigator.userAgent.indexOf("Mac") != -1 && typeof parent.document.body.leftMargin != "undefined") {
        offsetLeft += parent.document.body.leftMargin;
        offsetTop += parent.document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

function newData(c) {
    return new Date(c.value.substr(6, 4), c.value.substr(3, 2) - 1, c.value.substr(0, 2));
}

function newDataStr(c) {
    var vet = c.split('/');
    return new Date(vet[2], vet[1] - 1, vet[0]);
}

function newDataHora(c) {
    var vet = c.split(' ');
    var d = vet[0].split('/');
    var h = vet[1].split(':');
    
    return new Date(d[2], d[1] - 1, d[0], h[0], h[1], h[1]);
}

function checkdateMA(objName)
{	var datefield = objName;
	if (datefield.value == "")
		return true;

	if (chkdate('01/' + datefield.value) == false) 
	{	//datefield.select();
		alert("Data inválida");
		datefield.value = ""
		datefield.focus();
		return false;
	}
	else
	{	//alert("Data Válida");
		return true;
   	}
}

function checkdate(objName)
{	var datefield = objName;
	if (datefield.value == "")
		return true;

	if (chkdate(datefield.value) == false) 
	{	//datefield.select();
		alert("Data inválida");
		datefield.value = ""
		datefield.focus();
		return false;
	}
	else
	{	//alert("Data Válida");
		return true;
   	}
}

function chkdate(strDate) 
{	var strDate;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	
    //concatenar os campos e verificar quantidades
	// considerando um campo só com quantidade certa.
	strDay = strDate.substr(0, 2);
	strMonth = strDate.substr(3, 2);
	strYear = strDate.substr(6, 4);
	strBarra1 = strDate.substr(2, 1);
	strBarra2 = strDate.substr(5, 1);
   	
	// Mostra os valores das variaveis
	//alert("strDay: " + strDay + "\nstrBarra1: " + strBarra1 + "\nstrMonth: " + strMonth + "\nstrBarra2: " + strBarra2 + "\nstrYear: " + strYear);

	intday = parseInt(strDay, 10);
	intday1 = parseInt(strDate.substr(0, 1), 10);
	intday2 = parseInt(strDate.substr(1, 1), 10);
	if ((isNaN(intday1)) || (isNaN(intday2)))
	{	//alert("Erro no dia");
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	intMonth1 = parseInt(strDate.substr(3, 1), 10);
	intMonth2 = parseInt(strDate.substr(4, 1), 10);
	if ((isNaN(intMonth1)) || (isNaN(intMonth2)))
	{	//alert("Erro no mês");
		return false;
	}
	
	intYear = parseInt(strYear, 10);
	intYear1 = parseInt(strDate.substr(6, 1), 10);
	intYear2 = parseInt(strDate.substr(7, 1), 10);
	intYear3 = parseInt(strDate.substr(8, 1), 10);
	intYear4 = parseInt(strDate.substr(9, 1), 10);
	if ((isNaN(intYear1)) || (isNaN(intYear2)) || (isNaN(intYear3)) || (isNaN(intYear4)) || (intYear <= 1900))
	{	//alert("Erro no ano");
		return false;
	}
	
	if ((strBarra1 != "/") ||(strBarra2 != "/"))
	{	//alert("Erro na Barra / ");
		return false;
	}
	
	if (intMonth > 12 || intMonth < 1)
	{	return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1))
	{	return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1))
	{	return false;
	}
	if (intMonth == 2)
	{	if (intday < 1)
		{	return false;
		}
		if (LeapYear(intYear) == true)
		{	if (intday > 29)
			{	return false;
			}
		}
		else
		{	if (intday > 28)
			{	return false;
			}
		}
	}
	return true;
}

function LeapYear(intYear)
{	if (intYear % 100 == 0)
	{	if (intYear % 400 == 0)
		{	return true;
		}
	}
	else
	{	if ((intYear % 4) == 0)
		{ 	return true;
		}
	}
	return false;
}

function Valida_Data_Hoje(obj, $menor, menor_txt) {
    if (checkdate(obj)) {
        if (obj.value != '') {
            if (newData(obj) - hoje > 0) {
                alert('A data tem que menor ou igual a hoje!');
                obj.value = '';
                obj.focus();
                return false;
            }
            
            if ($menor != undefined) {
                if ($menor.val() == '') {
                    alert('Favor informar a data de ' + menor_txt + '!');
                    obj.value = '';
                    $menor.focus();
                    return false;
                }
                
                if (newData(obj) - newDataStr($menor.val()) < 0) {
                    alert('A data tem que maior ou igual a ' + menor_txt + '!');
                    obj.value = '';
                    obj.focus();
                    return false;
                }
            }
        }
        
        return true;
    }
    
    return false;
}

function Formata_Data(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;
	s="";
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x);
	vr = s;
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36){
		if ( tam < 2 )
			objeto.value = vr.substr(0,2);
		if ( tam > 2 && tam < 4 )
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2);
		if ( tam > 4 )
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4); 
	}
	if (tam > 5 && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4); 
	if ((tam > 2 && tam < 6) && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2);
	if (tam < 4 && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2);
}

function Formata_DataMA(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;
	s="";
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x);
	vr = s;
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36){
		if ( tam < 2 )
			objeto.value = vr.substr(0,2);
		if ( tam > 2)
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,4); 
	}
	if (tam > 5 && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,4); 
	if (tam < 4 && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2);
}

function Valida_Email(objeto) {
	if ((objeto.value.indexOf("@")==-1 || objeto.value.indexOf(".")==-1) && objeto.value!="")
		{ objeto.select();
		  alert("e-Mail inválido");
		  objeto.value = "";
          objeto.focus();
		  return false;
		}
	return true;
}

function Valida_Telefone(objeto) {
	str=objeto.value;
	tel="";

	if (str == "")
		return true
		
	if ((str.length<12 || str.length>13 || str.substring(0,1)!="(" || str.substring(3,4)!=")") && (str.indexOf("-")!=7 || str.indexOf("-")!=8)) {
		objeto.select();
		alert("Telefone inválido. Formato: (12)3456-7890");
    	objeto.value = "";
		objeto.focus();
		return false;
	}
	for (var i = 0; i < str.length; i++)
		if (str.substring(i,i+1) >= "0" && str.substring(i,i+1) <= "9")
		  	tel+=str.substring(i,i+1);	
	if (isNaN(tel)) {
		objeto.select();
		alert("Telefone inválido. Formato: (12)3456-7890");
		objeto.value = "";
		objeto.focus();
		return false;		
	}
	return true;
}

function Formata_Telefone(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;	
	s="" 
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x)
	vr = s
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36) {
	  if ( tam == 1 )
		objeto.value = vr.substr(0,1);
	  if ( tam > 1 && tam < 3 )
		objeto.value = '(' + vr;
	  if ( tam >=3 && tam < 7 )
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,4);
	  if ( tam > 6 && tam < 11 )
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,3) + '-' + vr.substr(5,4);
	  if ( tam > 10 )
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,4) + '-' + vr.substr(6,4);
	}
	if (tam > 10 && (tecla == 8 || tecla == 46))
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,4) + '-' + vr.substr(6,4);	  
	if ((tam > 6 && tam < 11) && (tecla == 8 || tecla == 46))
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,3) + '-' + vr.substr(5,4);
	if ((tam > 3 && tam < 7) && (tecla == 8 || tecla == 46))
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,4);
	if ((tam > 1 && tam < 3) && (tecla == 8 || tecla == 46))
		objeto.value = '(' + vr;
	if (tam == 1 && (tecla == 8 || tecla == 46))
		objeto.value = vr;
}

function Valida_Url(objeto) {
	if (objeto.value.substr(0,7).toLowerCase() == "http://")
		objeto.value = objeto.value.substr(7).toLowerCase();
	return true;
}

function enumero(field)
{	
 var valid = "0123456789"
 var ok = "yes";
 var temp;

 var msg = "";

 for (var i=0; i<field.value.length; i++)
 {
 	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = "no";
	
	msg += i + " - Temp: " + temp + " Ok: " + ok + "\n"
 }
 //alert(msg);
 if (ok == "no")
 	{
	alert("Neste campo só pode ter número inteiro!");
	field.value = "";
	field.focus();
    return false;
	}
 else
 	return true;
}

function enumero_zero(field, tam)
{	
 var valid = "0123456789"
 var ok = "yes";
 var temp;

 var msg = "";

 for (var i=0; i<field.value.length; i++)
 {
 	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = "no";
	
	msg += i + " - Temp: " + temp + " Ok: " + ok + "\n"
 }
 //alert(msg);
 if (ok == "no")
 	{
	alert("Neste campo só pode ter número inteiro!");
	field.value = "";
	field.focus();
    return false;
	}
 else {
    if (field.value != '') {
       tam = tam - field.value.length;
       if (tam > 0) field.value = repeat(0, tam) + field.value;
    }
 	
    return true;
 }
}

function tira_zero(field) {
    if (field.value.length > 0) field.value = parseInt(field.value, 10);
}

function repeat(repeatString, repeatNum) {
    var newString = "";
    
    for (var x=1; x<=parseInt(repeatNum, 10); x++) {
        newString = newString + repeatString;
    }
    
    return (newString);
}

//*******************************************************************
// Valida Campo de CNPJ
//Passa o objeto Text
function Valida_CNPJ(objName)
{	var datefield = objName;
	if (datefield.value == "")
		return true;

	if (CalculaCNPJ(objName.value) == false) 
	{	datefield.select();
		alert("CNPJ inválido!!!");
		datefield.value = ""
		datefield.focus();
		return false;
	}
	else
	{	
		return true;
   	}
}

function CalculaCNPJ(RecebeCNPJ)
{
var soma
var resultado1
var resultado2

s="" 

for (x=0; x < RecebeCNPJ.length; x++)
 	if (RecebeCNPJ.charCodeAt(x) >= 48 && RecebeCNPJ.charCodeAt(x)<=57)
		s= s + RecebeCNPJ.charAt(x)

RecebeCNPJ = s

if (RecebeCNPJ.length != 14 || RecebeCNPJ == "00000000000000")
	{
	return false
	}
else
	{
	soma = RecebeCNPJ.charAt(0) * 5 + RecebeCNPJ.charAt(1) * 4 + RecebeCNPJ.charAt(2) * 3 + RecebeCNPJ.charAt(3) * 2 + RecebeCNPJ.charAt(4) * 9 + RecebeCNPJ.charAt(5) * 8 + RecebeCNPJ.charAt(6) * 7 + RecebeCNPJ.charAt(7) * 6 + RecebeCNPJ.charAt(8) * 5 + RecebeCNPJ.charAt(9) * 4 + RecebeCNPJ.charAt(10) * 3 + RecebeCNPJ.charAt(11) * 2

	soma = soma -(11 * (parseInt(soma / 11)))

	if (soma == 0 || soma == 1)
		resultado1 = 0
	else
		resultado1 = 11 - soma

	if (resultado1 == RecebeCNPJ.charAt(12))
		{
		soma = RecebeCNPJ.charAt(0) * 6 + RecebeCNPJ.charAt(1) * 5 + RecebeCNPJ.charAt(2) * 4 + RecebeCNPJ.charAt(3) * 3 + RecebeCNPJ.charAt(4) * 2 + RecebeCNPJ.charAt(5) * 9 + RecebeCNPJ.charAt(6) * 8 + RecebeCNPJ.charAt(7) * 7 + RecebeCNPJ.charAt(8) * 6 + RecebeCNPJ.charAt(9) * 5 + RecebeCNPJ.charAt(10) * 4 + RecebeCNPJ.charAt(11) * 3 + RecebeCNPJ.charAt(12) * 2

		soma = soma - (11 * (parseInt(soma / 11)))
		
		if (soma == 0 || soma == 1)
			resultado2 = 0
		else
			resultado2 = 11 - soma

		if (resultado2 == RecebeCNPJ.charAt(13))
			return true
		else
			return false
		}
	else
		return false
	}
}

function Formata_Cnpj(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;	
	s="" 
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x)
	vr = s
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36) {
	  if (tam < 3)
		objeto.value = vr.substr(0,2);
	  if (tam > 2 && tam < 6)
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3);
	  if (tam > 5 && tam < 9)
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '.' + vr.substr(5,3);
	  if (tam > 8 && tam < 13)
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4);
	  if (tam > 12)
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4) + '-' + vr.substr(12,2);
	}
	if (tam > 12 && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4) + '-' + vr.substr(12,2);
	if ((tam > 8 && tam < 13) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4);
	if ((tam > 5 && tam < 9) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '.' + vr.substr(5,3);
	if ((tam > 2 && tam < 6) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3)
	if ((tam > 1 && tam < 3) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2)
}

function Valida_CPF(objName)
{	var datefield = objName;
	if (datefield.value == "")
		return true;

	if (checaCPF(objName.value) == false) 
	{	datefield.select();
		alert("CPF inválido!!!");
		datefield.value = ""
		datefield.focus();
		return false;
	}
	else
	{	//alert("Data Válida");
		return true;
   	}
}

function checaCPF (CPF) {
	s="" 
	for (x=0; x < CPF.length; x++)
	 	if (CPF.charCodeAt(x) >= 48 && CPF.charCodeAt(x)<=57)
			s= s + CPF.charAt(x)
	CPF = s

	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
		CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
		CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
		CPF == "88888888888" || CPF == "99999999999")
		return false;
	soma = 0;
	for (i=0; i < 9; i ++)
		soma += parseInt(CPF.charAt(i)) * (10 - i);
	var resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(9)))
		return false;
	soma = 0;
	for (i = 0; i < 10; i ++)
		soma += parseInt(CPF.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(10)))
		return false;
	return true;
}

function Formata_Cpf(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;	
	s="" 
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x)
	vr = s
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36) {
	  if ( tam < 10 )
		objeto.value = vr.substr(0,9);
	  if ( tam > 9 )
		objeto.value = vr.substr(0,9) +  '-' + vr.substr(9,2);
	}
	if (tam >= 11 && (tecla == 8 || tecla == 46)) 
		objeto.value = vr.substr(0,9) +  '-' + vr.substr(9,2);  
    if ( tam <= 10 && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,tam)
}

function Formata_Cep(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;	
	s="" 
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x)
	vr = s
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36) {
	  if ( tam < 2 )
		objeto.value = vr.substr(0,2);
	  if (tam > 2 && tam < 5)
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3);
	  if (tam > 5)
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '-' + vr.substr(5,3);
	}
	if ((tam > 5 && tam < 9) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '-' + vr.substr(5,3);
	if ((tam > 2 && tam < 6) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3)
	if ((tam > 1 && tam < 3) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2)
}

function money(field)
{
 if (field.value == "")
 	return true;

 if (validateNumber(field))
 {  
  	//alert("VALIDO!!!");
	return true;
 }
 else
 {	alert("Informe apenas valor no formato numérico!");
	field.value = "";
	field.focus();
	return false;
 }
}

function validateNumber(field) {
    var valid = "0123456789,"
    var ok = "yes";
    var ponto = 0;
    var temp;
    var numponto = 0;
    var vFinal = "";
    var msg = "";
    
    //Retira os Pontos
    field.value = field.value.replace(/\./gi,'');
    
    for (var i=0; i<field.value.length; i++) {
        temp = "" + field.value.substring(i, i+1);
        
        if (temp == ",") {
            numponto++;
            if (ponto == 0)
                ponto = i;
        }
        
        if (valid.indexOf(temp) == "-1") ok = "no";
        
        msg += i + " - Temp: " + temp + " Ok: " + ok + " NumPonto: " + numponto + " Ponto: " + ponto + "\n"
    }
    
    if (ponto != 0)
        temp = field.value.length - (ponto + 1) - 2;
    else
        temp = 0;
    
    msg += "\nPonto: " + ponto + " Temp: " + temp + "\n" 
    
    //alert(msg);
    
    if (ok == "no") return false;
    if (numponto >= 2) return false;
    if (temp == -2 && numponto == 1) return false;
    if (temp >= 1) return false;
    if (ponto == 0 && numponto == 1) return false;
    
    //Coloca os pontos
    msg = "";
    
    if (ponto > 0) vFinal = field.value.substr(ponto);
    if (ponto == 0) ponto = field.value.length;
    
    for (var i = ponto - 3; i > -3; i = i - 3) {
        temp = "" + field.value.substring(i, i + 3);
        vFinal = "." + temp + vFinal;
        msg += i + " - Temp: " + temp +  "  vFinal: " + vFinal + "\n"
    }
    //alert(msg);
    
    if (vFinal.substring(0, 1) == ".") vFinal = vFinal.substr(1);
    field.value = vFinal;
    return true;
}

function Limita_Tamanho(campo, tam) {
    if (campo.value.length >= tam) {
        return false;
    } else {
        return true;
    }
}

function Trunca_Campo(campo,tam) {
    if (campo.value.length > tam) {
        alert("O campo tem um limite de " + tam + " caracteres!");
        campo.value = campo.value.substring(0,tam);
    }
}

function troca_caracter(campo) {
    var estranha = "áéíóúàèìòùâêîôûäëïöüãõ@#$%^&*()_+=-~` ç";
    var correta  = "aeiouaeiouaeiouaeiouao________________c";
    var re;
    
    campo = campo.toLowerCase();
    
    for (var i = 0; i < estranha.length; i++) {
        re = new RegExp('\\' + estranha.charAt(i), "g");
        campo = campo.replace(re, correta.charAt(i));
    }
    
    campo = campo.replace(/_/gi, "");
    
    return campo;
}

function url_decode(str) { 
    var n, strCode, strDecode = ""; 

    for (n = 0; n < str.length; n++) { 
        if (str.charAt(n) == "%") { 
            strCode = str.charAt(n + 1) + str.charAt(n + 2); 
            strDecode += String.fromCharCode(parseInt(strCode, 16)); 
            n += 2; 
        } else { 
            strDecode += str.charAt(n); 
        } 
    } 
    return strDecode; 
}  

function cursorfim(obj) {
    var range = obj.createTextRange();
    var end = obj.value.length;
    range.collapse(true);
    range.moveEnd('character', end + 1);
    range.moveStart('character', end);
    range.select();
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function calcular_idade(data){
    //calcular a idade de uma pessoa
    //recebe a data como um string em formato portugues
    //devolve um inteiro com a idade. Devolve false em caso de que a data seja incorreta ou maior que o dia atual
    
    var ano;
    var mes;
    var dia;
    var rAno;
    var rMes;
    var rDia;
    
    var array_data = data.split("/");
    if (array_data.length != 3) return false;
    
    ano = parseInt(array_data[2], 10);
    if (isNaN(ano)) return false;
    
    mes = parseInt(array_data[1], 10);
    if (isNaN(mes)) return false;
    
    dia = parseInt(array_data[0], 10);
    if (isNaN(dia)) return false;
    
    if (ano<=99) ano +=1900;

    rAno = hoje.getYear() - ano - 1; //-1 porque ainda nao fez anos durante este ano
    if ($.browser.mozilla) rAno += 1900;
    
    //se subtraio os meses e for menor que 0 entao nao cumpriu anos. Se for maior sim ja cumpriu
    //+ 1 porque os meses comecam em 0
    rMes = hoje.getMonth() + 1 - mes;
    rDia = hoje.getUTCDate() - dia;
    
    if (hoje.getMonth() + 1 - mes == 0) {
        //entao eh porque sao iguais. Vejo os dias
        //se subtraio os dias e der menor que 0 entao nao cumpriu anos. Se der maior ou igual sim que já cumpriu
        if (rDia >= 0) rAno++;
    } else if (hoje.getMonth() + 1 - mes > 0) {
        rAno++;
    }
    
    if (rDia < 0) {
        rMes--;
        var tMes = hoje.getMonth() + 1;
    	if (tMes == 1 || tMes == 3 || tMes == 5 || tMes == 7 || tMes == 8 || tMes == 10 || tMes == 12) rDia = 31 + rDia - 1;
    	if (tMes == 4 || tMes == 6 || tMes == 9 || tMes == 11)  rDia = 30 + rDia - 1;
    }
    
    if (rMes < 0) rMes = 11;
    
    return {ano: rAno, mes: rMes, dia: rDia};
}

function Valida_Url(objeto) {
	if (objeto.value.substr(0,7).toLowerCase() == "http://")
		objeto.value = objeto.value.substr(7).toLowerCase();
	return true;
}

function minCaracteres(obj, tam) {
    if (tam == undefined) tam = 7;
    
    if (obj.value.length > 0 && obj.value.length <= tam) {
        alert('Favor informar o texto com mais de ' + tam + ' caracteres!');
        obj.value = '';
        obj.focus();
        return false;
    } else {
        return true;
    }
    
}

function mudaTab(g) {
    if ($('div#grd' + g).css('display') == 'none')
        $('div#tabHtml > ul').tabs('select', '#grd' + g);
}

function queryString(parameter) {
    var loc = location.search.substring(1, location.search.length);
    var param_value = false;
    var params = loc.split("&");
    for (i=0; i<params.length;i++) {
        param_name = params[i].substring(0,params[i].indexOf('='));
        if (param_name == parameter) {
            param_value = params[i].substring(params[i].indexOf('=')+1)
        }
    }
      
    if (param_value) {
        return param_value;
    } else {
        return '';
    }
}
