

// Retira caracteres em branco do inicio e do fim da String
function Trim(sStr)
{
	var i = 0;
	var j = sStr.length;
	
	while(sStr.charCodeAt(i) == 32)
		i++;
	while(sStr.charCodeAt(j - 1) == 32)
		j--;

	if( i >= j )
		return("");
		
	return(sStr.substring(i, j));
}


// Função deve ser chamada no evento onKeyPress do campo
function MascararData(field)
{
    if ((event.keyCode<48)||(event.keyCode>57))
	    event.returnValue = false; 
    else 
	    if ((field.value.length==2)||(field.value.length==5))
            field.value = field.value + "/" ;
}

// Função deve ser chamada no evento onKeyPress do campo
function MascararHora(field)
{
	if ((event.keyCode<48)||(event.keyCode>57))
		event.returnValue = false; 
	else 
		if (field.value.length==2)
			field.value = field.value + ":" ;
}

// Função deve ser chamada no evento onKeyPress do campo
function MascararNumero(field, bDecimal)
{
    if ( bDecimal )
    {
        if (event.keyCode != 44)
        {
		    if ((event.keyCode<48)||(event.keyCode>57))
                event.returnValue = false;
		}
		else
		{
			if ( field.value.length == 0 )
			{
				event.returnValue = false;
			}
			else
			{
				if ( field.value.indexOf(",") != -1 ) 
				{
					event.returnValue = false;
				}
			}
		}
	       
    }
    else
    {
        if ((event.keyCode<48)||(event.keyCode>57))
            event.returnValue = false;
    }    
    
}


// Recebe uma string no formato "dd/mm/aaaa" ou "d/m/aa" ou "d/m/a" e retorna true se a data for valida
function isDate(sStr_Date)
{
	var nDia, nMes, nAno;
	var nBarras = 0;

	if ( sStr_Date.length < 5 || sStr_Date.length > 10)
		return(false);

	for(var i = 0; i < sStr_Date.length; i++)
		if(sStr_Date.charCodeAt(i) == 47)
			nBarras++;

	if(nBarras != 2)
		return(false);

	nDia = parseInt(sStr_Date.substring(0, sStr_Date.indexOf("/")), 10);
	nMes = parseInt(sStr_Date.substring(sStr_Date.indexOf("/") + 1, sStr_Date.lastIndexOf("/")), 10);
	nAno = parseInt(sStr_Date.substring(sStr_Date.lastIndexOf("/") + 1, sStr_Date.length), 10);

	if(isNaN(nDia) || isNaN(nMes) || isNaN(nAno))
		return(false)

	nAno = FormatarAno(nAno);

	if (((nDia > 31) || (nDia <= 0) || (nMes > 12) || (nMes <= 0) || (nAno > 9999) || (nAno < 0)) ||       
     ((nDia == 31) && ((nMes == 2) || (nMes == 4) || (nMes == 6) || (nMes == 9) || (nMes == 11))) ||
     ((nDia == 30) && (nMes == 2)) ||
     ((nDia == 29) && (nMes == 2) && ((nAno % 4) || ((!(nAno % 100)) && (nAno % 400)))))
		return(false);
	
	return(true);
}

function FormatarAno(nAno)
{
	if(nAno >= 0 && nAno < 100)
		if(nAno < 40)
			nAno += 2000;
		else
			nAno += 1900;
	return(nAno);
}

function Replace(expression, find, replacewith)
{
	var aux;
	var start = 0;

	if(find != "")

	while(expression.indexOf(find, start) != -1)
	{
		aux = expression.indexOf(find, start);
		expression = expression.substring(0, aux) + replacewith + expression.substring(aux + find.length, expression.length);
		start = aux + replacewith.length;
	}
	return(expression);
}

//formata um numero recebendo o proprio, a precisao de casas
//decimais, se deseja(true) completar as casa decimais com zeros
// e se o separador decimal será um ponto(default true) ou virgula(false)
//"this.value = FormatNumber(this.value,2,1,true,false);"
function FormatNumber( NUMBER, PRECISION, TAM_ESQUERDA, ZEROS, POINT)
{
    NUMBER = "0" + NUMBER;
    
	if((POINT != true && POINT != false) || POINT == true){
		POINT = ".";
	}
	else{
		POINT = ","
	}
	var ZEROS_ESQUERDA = "";
	if(isNaN(TAM_ESQUERDA) || TAM_ESQUERDA == null){
		TAM_ESQUERDA = 0;
	}
	else{
		TAM_ESQUERDA = parseInt(TAM_ESQUERDA);
	}
	if(ZEROS == null){
		ZEROS = false;
	}
	var NUM = NUMBER.toString();
	NUM = Replace(NUM,",",".");
	NUM = parseFloat(NUM);
	if(isNaN(NUM)){
		return("NaN");
	}
	if(PRECISION == null){
		PRECISION = 0;
	}
	if(PRECISION == 0){
		ZEROS = false
		NUM = Math.round(NUM);
	}
	NUM = NUM.toString();
	var SEP = NUM.indexOf(".");
	if(SEP != -1){
		var NUM_AUX = NUM.substr(SEP + 1);
		var AUX = PRECISION - NUM_AUX.length + 1;
		for(var i=0; i<AUX; i++){
			NUM = NUM + "0";
		}
		var TEMP = 0;
		var ATUAL = 0;
		var SOMA = 0;
		var ANT;
		var TOT = SEP+PRECISION;
		for(var w=NUM.length - 2; w>=TOT; w--){
			ANT = 1;
			if(NUM.charAt(w) == "."){
				ANT = 2;
				w--;
				TOT--;
			}
			TEMP = parseInt(NUM.charAt(w + ANT)) + SOMA;
			if(TEMP >= 5){
				ATUAL = parseInt(NUM.charAt(w)) + 1;
				if(ATUAL >= 10){
					if(w == TOT){
						TOT--;
					}						
					if(w == 0){
						NUM = "10" + NUM.substr(1);
						SEP++;
						break;
					}
					else{
						NUM = NUM.substring(0,w) + "0" + NUM.substr(w + 1);
						SOMA = 10;
					} 
				}
				else{
					SOMA = 0;
					NUM = NUM.substring(0,w) + ATUAL + NUM.substr(w + 1);
				}
			}
		}
		for(var y=SEP; y<TAM_ESQUERDA; y++){
			ZEROS_ESQUERDA += "0"
		}
		NUM = NUM.substr(0,SEP + PRECISION + 1)
		if(ZEROS){
			return(ZEROS_ESQUERDA + NUM.substr(0,SEP) + POINT + NUM.substr(SEP + 1));
		}
		NUM = parseFloat(NUM);
		return(ZEROS_ESQUERDA + Replace(NUM.toString(),".",POINT));
	}
	for(var e=NUM.length; e<TAM_ESQUERDA; e++){
		ZEROS_ESQUERDA += "0"
	}
	if(ZEROS){
		NUM = NUM + POINT;
		for(var j=0; j<PRECISION; j++){
			NUM += "0";
		}
	}
	return(ZEROS_ESQUERDA + NUM);
}

