function FormataValor(cur,len)
{
   n='__0123456789';
   d=cur.value;
   l=d.length;
   r='';
   if (l > 0)
   {
    z=d.substr(0,l-1);
    s='';
    a=2;
    for (i=0; i < l; i++)
    {
        c=d.charAt(i);
        if (n.indexOf(c) > a)
        {
            a=1;
            s+=c;
        };
    };
    l=s.length;
    t=len-1;
    if (l > t)
    {
        l=t;
        s=s.substr(0,t);
    };
    if (l > 2)
    {
        r=s.substr(0,l-2)+','+s.substr(l-2,2);
    }
    else
    {
        if (l == 2)
        {
            r='0,'+s;
        }
        else
        {
            if (l == 1)
            {
                r='0,0'+s;
            };
        };
    };
    if (r == '')
    {
        r='0,00';
    }
    else
    {
        l=r.length;
        if (l > 6)
        {
            j=l%3;
            w=r.substr(0,j);
            wa=r.substr(j,l-j-6);
            wb=r.substr(l-6,6);
            if (j > 0)
            {
                w+='.';
            };
            k=(l-j)/3-2;
            for (i=0; i < k; i++)
            {
                w+=wa.substr(i*3,3)+'.';
            };
            r=w+wb;
        };
    };
   };
   if (r.length <= len)
   {
    cur.value=r;
   }
   else
   {
    cur.value=z;
   };
   return 'ok';
}

function FormataValor(campo,tammax,teclapres) 
{
	/*MAU>
	 origem desconhecida
	 modificada por MAU (MAURICIO BARROS)
	 function FormataValor(campo,tammax,teclapres,form): trasforma o conteudo de um campo em formato de moeda 
	 separao com "." e "," (1.222.222,01).OBS. chamada: onKeyDown = "return FormataValor(this.id, 13, event);"
	 */
	
	var tecla = teclapres.keyCode;
	vr = document.getElementById(campo).value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;
	
	if(vr == '')
		vr = 0;
	
	if(tam >= tammax)
	{
		ok = 'false';
		
		if(!( tecla == 8 || tecla == 39 || tecla == 37 || tecla == 46))
		{
			return false;
			
		}
	}
	
	
	if (tam < tammax && tecla != 8)
	{ 
		tam = vr.length + 1; 
	}
	if (tecla == 8 )
	{ 
	tam = tam - 1; 
	}
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
		if ( tam <= 2 )
		{
			document.getElementById(campo).value = vr; 
		}

		if ( (tam > 2) && (tam <= 5) )
		{
			document.getElementById(campo).value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); 
		}

		if ( (tam >= 6) && (tam <= 8) )
		{
			document.getElementById(campo).value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
		}

		if ( (tam >= 9) && (tam <= 11) )
		{
			document.getElementById(campo).value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
		}

		if ( (tam >= 12) && (tam <= 14) )
		{
			document.getElementById(campo).value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
		}

		if ( (tam >= 15) && (tam <= 17) )
		{
			document.getElementById(campo).value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
		}
	} 
	else 
	{
		return false;
	}
}

function f_formatar_moeda_ponto_virgula(vr)
{
	/*MAU>
	Origem UFG 13/09/2006- autor MAU(Mauricio barros)
	Com base em outras funceos
	
	f_formatar_moeda_ponto_virgula: retrona o vr em formato de moeda acrecentenado . e , s******/
	
	vr = new String(vr);
	
		//recolocar zero a direita.
		str_total = new String(vr);
		pos_ponto = str_total.indexOf(".");
		if(pos_ponto > -1) 
		{
			
			pri_casa = str_total.substring(pos_ponto + 1, pos_ponto + 2);
			sec_casa = str_total.substring(pos_ponto + 2, pos_ponto + 3);
			
			if(!pri_casa)
				str_total = str_total+".00";
			else  if(!sec_casa)
					str_total = str_total+"0";
					   
			vr = str_total;
		}
		else
			vr = vr+".00";

	
	
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;
	
	if(vr == '')
		vr = 0;
		
	
	if ( tam <= 2 )
	{
		vr = vr; 
	}

	if ( (tam > 2) && (tam <= 5) )
	{
		vr = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); 
	}

	if ( (tam >= 6) && (tam <= 8) )
	{
		vr = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
	}

	if ( (tam >= 9) && (tam <= 11) )
	{
		vr = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
	}

	if ( (tam >= 12) && (tam <= 14) )
	{
		vr = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
	}

	if ( (tam >= 15) && (tam <= 17) )
	{
		vr = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
	}

	return vr;
}
/****************************************************************************************************************/
function f_formatar_moeda_ponto(valor)
{
	/*MAU> 
	Origem UFG 13/09/2006 - autor MAU(mauricio Barros)
	
	function f_formatar_moeda_ponto(valor): formata valor para tipo ponto flutuante em formato 
	que pode ser interpretado de forma correta em operacoes aritmeticas em javascript, muito util 
	quando se trabalha com moedas.
	*/
	
	if(valor == '')
	{
		valor = 0;
	}
	else
	{
		vet_valor = valor.split(".");
		
		for(i = 0; i < vet_valor.length -1; i++)
		{
			
			valor = valor.replace( ".", "" );
		}
		
		valor       = valor.replace( ",", "." );
		vet_valor   = valor.split(".");
		vet_valor_0 = vet_valor[0];
		

		vet_valor_0_retorno = vet_valor_0;
		
		for( i =0 ; i < vet_valor_0.length -1; i++)
		{
			if(vet_valor_0[i] != 0 || i ==  vet_valor_0.length -2 )
			{
				vet_valor_0_retorno = vet_valor_0.substr(i);
				break;
			}
		}
		
		if(vet_valor[1])
			valor = vet_valor_0_retorno+"."+vet_valor[1];
		else
			valor = vet_valor_0_retorno+".00";

		
	}
	
	return valor; 
}

function FormataData(Campo){
	      var mdata = '';
              mdata = mdata + data;
              if (mdata.length == 2){
                  mdata = mdata + '/';
                  document.forms[0].data.value = mdata;
              }
              if (mdata.length == 5){
                  mdata = mdata + '/';
                  document.forms[0].data.value = mdata;
              }
              if (mdata.length == 10){
                  return true;
              } 

}
//usar no onkeypress e no onkeyup onkeypress="FormataData(this,event);" onkeyup="FormataData(this,event);"

// JavaScript Document

var CODE_MINUS            = 45;
var CODE_PLUS             = 43;
var CODE_DOT              = 46;
var CODE_SPACE            = 32;
var CODE_TILDE              = 126;

// key codes for onKeyPres events (ASCII codes)
var CODE_0 = 48;
var CODE_1 = 49;
var CODE_2 = 50;
var CODE_3 = 51;
var CODE_4 = 52;
var CODE_5 = 53;
var CODE_6 = 54;
var CODE_7 = 55;
var CODE_8 = 56;
var CODE_9 = 57;

var CODE_A = 65;
var CODE_B = 66;
var CODE_C = 67;
var CODE_D = 68;
var CODE_E = 69;
var CODE_F = 70;
var CODE_G = 71;
var CODE_H = 72;
var CODE_I = 73;
var CODE_J = 74;
var CODE_K = 75;
var CODE_L = 76;
var CODE_M = 77;
var CODE_N = 78;
var CODE_O = 79;
var CODE_P = 80;
var CODE_Q = 81;
var CODE_R = 82;
var CODE_S = 83;
var CODE_T = 84;
var CODE_U = 85;
var CODE_V = 86;
var CODE_W = 87;
var CODE_X = 88;
var CODE_Y = 89;
var CODE_Z = 90;

var CODE_a = 97;
var CODE_b = 98;
var CODE_c = 99;
var CODE_d = 100;
var CODE_e = 101;
var CODE_f = 102;
var CODE_g = 103;
var CODE_h = 104;
var CODE_i = 105;
var CODE_j = 106;
var CODE_k = 107;
var CODE_l = 108;
var CODE_m = 109;
var CODE_n = 110;
var CODE_o = 111;
var CODE_p = 112;
var CODE_q = 113;
var CODE_r = 114;
var CODE_s = 115;
var CODE_t = 116;
var CODE_u = 117;
var CODE_v = 118;
var CODE_w = 119;
var CODE_x = 120;
var CODE_y = 121;
var CODE_z = 122;

// maxnumber of days in months
var daysInMonth = new Array(12);
daysInMonth[0]  = 31;
daysInMonth[1]  = 29;
daysInMonth[2]  = 31;
daysInMonth[3]  = 30;
daysInMonth[4]  = 31;
daysInMonth[5]  = 30;
daysInMonth[6]  = 31;
daysInMonth[7]  = 31;
daysInMonth[8]  = 30;
daysInMonth[9]  = 31;
daysInMonth[10] = 30;
daysInMonth[11] = 31;

function CaixaAlta(campo) {	
	texto = campo.value;
	texto = texto.toUpperCase();
	campo.value = texto;
	//event.keyCode=0;
}

// isDay(STRING day [, BOOLEAN emptyOK])
//
// isDay returns true if "day" is a number between 1 and 31.
//
// For explanation of optional argument emptyOK,
// see comments of function isUnsignedInteger.
//
function isDay(day) {
  var result;
  if (isEmpty(day)) {
    result = (isDay.arguments.length == 2 ?
              isDay.arguments[1] : defaultEmptyOK);
  }
  else {
    result = isIntegerInRange(day, 1, 31);
  }
  return result;
}

// isMonth(STRING month [, BOOLEAN emptyOK])
//
// isMonth returns true if "month" is a number between 1 and 12.
//
// For explanation of optional argument emptyOK,
// see comments of function isUnsignedInteger.
//
function isMonth(month) {
  if (isEmpty(month)) {
    result = (isMonth.arguments.length == 2 ?
              isMonth.arguments[1] : defaultEmptyOK);
  }
  else {
    result = isIntegerInRange(month, 1, 12);
  }
  return result;
}


// isDigit(int code [, BOOLEAN emptyOK])
//
// Returns true if ASCII code represents a number.
//
// For explanation of optional argument emptyOK,
// see comments of function isUnsignedInteger.
//
function isDigit(code) {
  var result = true;
  if (isEmpty(code)) {
    result = (isDigit.arguments.length == 2 ?
              isDigit.arguments[1] : defaultEmptyOK);
  }
  else {
//  var chr = String.fromCharCode(code);
//  result = (chr >= '0' && chr <= '9');
    result = (code >= CODE_0 && code <= CODE_9);
  }
  return result;
}


// isUnsignedInteger(STRING str [, BOOLEAN emptyOK])
//
// Returns true if all characters in string str are numbers.
//
// Accepts non-signed integers only. Does not accept floating
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if str is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true),
//      the function will return false if str is empty.
// If emptyOK is true, the function will return true if str is empty.
//
// EXAMPLE FUNCTION CALL:            RESULT:
// isUnsignedInteger("5")            true
// isUnsignedInteger("")             defaultEmptyOK
// isUnsignedInteger("-5")           false
// isUnsignedInteger("", true)       true
// isUnsignedInteger("", false)      false
// isUnsignedInteger("5", false)     true
//
function isUnsignedInteger(str) {
  var result = true;
  if (isEmpty(str)) {
    result = (isUnsignedInteger.arguments.length == 2 ?
              isUnsignedInteger.arguments[1] : defaultEmptyOK);
  }
  else {
    var code;
    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.
    for (var i = 0; i < str.length; i++) {
      // Check if the current character is number.
      code = str.charCodeAt(i);
      if (!isDigit(code)) {
        result = false;
        break;
      }
    }
  }
  return result;
}

// isInteger(STRING str [, BOOLEAN emptyOK])
//
// Returns true if string str encodes a valid integer number.
//
// For explanation of optional argument emptyOK,
// see comments of function isUnsignedInteger.
//
function isInteger(str) {
  var result;
  if (isEmpty(str)) {
    result = (isInteger.arguments.length == 2 ?
              isInteger.arguments[1] : defaultEmptyOK);
  }
  else {
    var code = str.charCodeAt(0);
    result = isUnsignedInteger(code == CODE_MINUS || code == CODE_PLUS ?
                               str.substring(1, str.length) : str);
  }
  return result;
}

// isIntegerInRange(STRING str, INTEGER a, INTEGER b [, BOOLEAN emptyOK])
//
// isIntegerInRange returns true if string str is an integer
// within the range of integer arguments a and b, inclusive.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
function isIntegerInRange(str, a, b) {
  var result = false;
  if (isEmpty(str)) {
    result = (isIntegerInRange.arguments.length == 2 ?
              isIntegerInRange.arguments[1] : defaultEmptyOK);
  }
  else if (isInteger(str, false)) {
    // Catched non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt(str, 10);
    result = (num >= a && num <= b);
  }
  return result;
}

// isYear(STRING year [, BOOLEAN emptyOK])
//
// isYear returns true if "year"is a valid calendar year.
//  Must be 2 or 4 digits only.
//
// For Year 2000 compliance, you are advised
// to use 4-digit year numbers everywhere.
//
// And yes, this function is not Year 10000 compliant, but
// because I am giving you 7999 years of advance notice,
// I don't feel very guilty about this ...
//
// For B.C. compliance, write your own function. ;->
//
// For explanation of optional argument emptyOK,
// see comments of function isUnsignedInteger.
//

function isYear(year) {
  var result;
  if (isEmpty(year)) {
    result = (isYear.arguments.length == 2 ?
              isYear.arguments[1] : defaultEmptyOK);
  }
  else {
    result = (isIntegerInRange(year, 1, 9999) &&
              ((year.length == 2) || (year.length == 4)));
  }
  return result;
}


function isEmpty(val) {
  return (!val || (val.length && val.length == 0));
}

// isDate(STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day
// form a valid date. The year argument may be null.
//
function isDate(year, month, day) {
  var result = false;
  // catch invalid years (not 2- or 4-digit) and invalid months and days.
  if ((isEmpty(year) || isYear(year, false)) &&
      isMonth(month, false) && isDay(day, false)) {
    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = 0;
    if (!isEmpty(year)) {
      intYear = parseInt(year, 10);
    }
    var intMonth = parseInt(month, 10);
    var intDay   = parseInt(day, 10);
    var maxDay   = daysInMonth[intMonth - 1];
    if (intMonth == 2 && intYear > 0 && ((intYear % 4) != 0 || ((intYear % 100) == 0 && (intYear % 400) != 0))) {
       maxDay--;
    }
    result = (intDay <= maxDay);
  }
  return result;
}


function FormataData(campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = campo.value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;
	
	if (tecla!=8) {
		if ( tam > 2 && tam < 5 )
			campo.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
                if ( tam >= 9 ){
                  if ( ! isDate(vr.substr( 4, 4 ), vr.substr( 2, 2 ), vr.substr( 0, 2 )) )
                  {
                    alert("  Data Inválida !  ");
                    campo.value = '';
                    return (false);
                  }
                }
	}
}

/*funcao para validacao de Campos
exemplo de chamada: onKeyPress="if(window.event && window.event.keyCode == 13 || event.keyCode == 13 || event.which == 13) return f_busca(); else return limit_keys(this, 'numeric', event);"
Objetivo= testar se um determinado compo contem tipos de dados corretos
*/
function limit_keys(selectObj, type, evt)
{
	var keyCode = 0;
	var ret_val = true;
	
	if (evt) 
	{
		keyCode = evt.keyCode || evt.which;
	}
	else 
	{
		keyCode = window.event.keyCode;
	}
	//alert(keyCode);
	// Allow special characters: BACKSPACE, TAB, RETURN, LEFT ARROW,RIGHT ARROW to go through
	if(keyCode == 8 || keyCode == 9 || keyCode == 37 || keyCode == 39)// ((keyCode == 8) || (keyCode == 9) || (keyCode == 13) || (keyCode == 37) || (keyCode == 39)) 
	{
		return (ret_val);
	}
	
	if (type == 'phone') 
	{
		// Numeric values and punctuation are OK
		ret_val = test_keycode( '0123456789()-.', keyCode);
	}
	else if (type == 'alphanum') 
	{
		ret_val = ((keyCode >= 48) && (keyCode <= 57 )) || ((keyCode >= 66) && (keyCode <= 90)) 
			   || ((keyCode >= 97) && (keyCode <= 122)) ||  keyCode == 32 ;
	}
	else if (type == 'numeric') 
	{
		// Simply test for a numeric value
		ret_val = ((keyCode >= 48) && (keyCode <= 57));
	}
	else if (type == 'float') 
	{
		// Simply test for a numeric value
		ret_val = ((keyCode >= 48) && (keyCode <= 57)) || keyCode <= 44 ;
	}
	else if (type == 'date') 
	{	
		var strtmp = '';
		var rExpr = new RegExp("[0-9/]");
		var inicio = selectObj.value.length - 1;
		var text = selectObj.value;

		if(keyCode != 8)
		{
			if(rExpr.test(text.substr(inicio, 1)) != true)
				strtmp = text.substr(0, inicio);
				
			if(text.length == 3 && text.substr(2, 1) != '/')
				strtmp = text.substr(0, 2) + '/' + text.substr(2, 1);
				
			if(text.length == 6 && text.substr(5, 1) != '/')
				strtmp = text.substr(0, 5) + '/' + text.substr(5, 1);
				
			if(strtmp != '')
				selectObj.value = strtmp;
		}
	}
	return (ret_val);
}