﻿function FormatValue(campo,tammax,teclapres) 
{
	var tecla = teclapres.keyCode;
	vr = 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 (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 ){ 
			campo.value = vr ; }
		if ( (tam > 2) && (tam <= 5) ){
			campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
		if ( (tam >= 6) && (tam <= 8) ){
			campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
		if ( (tam >= 9) && (tam <= 11) ){
			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) ){
			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) ){
			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 ) ;}
	}
}
function FormatDate(Campo) 
{ 
    var tecla = event.keyCode; 
    var tam = Campo.value.length + 1; 
    
    if ( tecla != 9 && tecla != 8 && tecla != 45 && tecla != 46 && (tecla < 35 || tecla > 40)) 
    { 
        if ((tecla < 48 || tecla > 57) && (tecla < 96 || tecla > 105)) 
                event.returnValue = false;      
        else 
        { 
            if( tam > 10 )
                event.returnValue = false;
            if ( tam == 3 || tam == 6) 
                Campo.value += '/' 
        } 
    }
} 
function FromatInt(KeyCode)
{
	if (KeyCode >=48 && KeyCode <= 57)
		return true;
	else event.keyCode=false;
}
function GetSelectedItemCombo(combo, text)
{
    if(text)
        return combo.options[combo.selectedIndex].text;
    return combo.options[combo.selectedIndex].value;
}
function SelectItemCombo(combo, valor)
{
    if(valor == -1)
    {
       combo.options[0].selected = true; 
       return true;
    }
    for( i=0 ; i<=combo.options.length-1 ; i++ )
    {
        if ( combo.options[i].value == valor )
        {
            combo.options[i].selected = true;
            return true;
        }
    }
    return false;
}
function trim(stringToTrim) 
{
	return stringToTrim.replace( /^\s+|\s+$/g,"" );
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.Right = function( t )
{
	var rt = this;
	if( rt.length < t )
		return rt;
	return rt.substr( rt.length-t, t );
}
String.prototype.LTrim = function(c) 
{
    if( !c )
        c = ' ';
	var rt = this;
	while(rt.substring(0,1) == c)
	{
		rt = rt.substring(1, rt.length);
	}
	return rt;
}