function TextCont(field, maxlimit) {
	if (field.value.length > maxlimit) 
		field.value = field.value.substring(0, maxlimit);
	
}

function FormatarReais(fld, milSep, decSep, e) {
		var sep = 0;
		var key = '';
		var i = j = 0;
		var len = len2 = 0;
		var strCheck = '0123456789';
		var aux = aux2 = '';
		var whichCode = (window.Event) ? e.which : e.keyCode;
		
		if (whichCode == 13) return true;
		
	 	//novo codigo
	 	if (fld.value.length == 14) return(true);
	 	
	 	//fim
	 	
	 	key = String.fromCharCode(whichCode);  // Valor para o código da Chave
		if (strCheck.indexOf(key) == -1) return false;  // Chave inválida
		len = fld.value.length;
		for(i = 0; i < len; i++)
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) 
 		break;
 		aux = '';
 		for(; i < len; i++)
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
		aux += key;
		len = aux.length;
		if (len == 0) fld.value = '';
		if (len == 1) fld.value = '0'+ decSep + '0' + aux;
		if (len == 2) fld.value = '0'+ decSep + aux;
		if (len > 2) {
			aux2 = '';
 			for (j = 0, i = len - 3; i >= 0; i--) {
				if (j == 3) {
					aux2 += milSep;
					j = 0;
				}
				aux2 += aux.charAt(i);
 				j++;
			}
			fld.value = '';
			len2 = aux2.length;
			for (i = len2 - 1; i >= 0; i--)
			fld.value += aux2.charAt(i);
			fld.value += decSep + aux.substr(len - 2, len);
		}
		return false;
}



	function AvaliarPlaca(obj, evt) {
		//alert(evt.keyCode);
		if (    !(evt.keyCode >= 65 && evt.keyCode <= 90) && !(evt.keyCode >= 48 && evt.keyCode <= 57) && (evt.keyCode != 8) && (evt.keyCode != 9) ) {
			//event.returnValue = false;
		}
	}


	function formatLine(obj, e) {
	
		if (navigator.userAgent.indexOf("IE") != -1) {
		//script para IE 
			var keypress = window.event.keyCode;
			if (keypress > 47 && keypress < 58 && obj.value.length < 13) {
				if (obj.value.length == 0) {
				   obj.value = "(" + obj.value;
				}   
				if (obj.value.length == 3) {
				   obj.value = obj.value + ")";
				}   
				if (obj.value.length == 8) {
				   obj.value = obj.value + "-";
				}  
			} else { 
				event.returnValue = false;
			}
		} else {
			//script para NS
			if (obj.value.length == 10 && e.which != 8 && e.which != 0) {
				return false;
			}	
			if (isNaN(String.fromCharCode(e.which)) && e.which != 8 && e.which != 0) {
				return false;
			}	
			return true;
		}
    }



function FormatarCampo(Campo, teclapres, mascara){
	//pegando o tamanho do texto da caixa de texto com delay de -1 no event
	//ou seja o caractere que foi digitado não será contado.
	strtext = Campo.value
	tamtext = strtext.length
	//pegando o tamanho da mascara
	tammask = mascara.length
	//criando um array para guardar cada caractere da máscara
	arrmask = new Array(tammask)	
	//jogando os caracteres para o vetor
	for (var i = 0 ; i < tammask; i++){
		arrmask[i] = mascara.slice(i,i+1)
	} 
	//alert (teclapres.keyCode)
	//começando o trabalho sujo
	if (((((arrmask[tamtext] == "#") || (arrmask[tamtext] == "9"))) || (((arrmask[tamtext+1] != "#") || (arrmask[tamtext+1] != "9"))))){
		if ((teclapres.keyCode >= 37 && teclapres.keyCode <= 40)||(teclapres.keyCode >= 48 && teclapres.keyCode <= 57)||(teclapres.keyCode >= 96 && teclapres.keyCode <= 105)||(teclapres.keyCode == 8)||(teclapres.keyCode == 9) ||(teclapres.keyCode == 46) ||(teclapres.keyCode == 13)){
			OrganizarCasas(Campo,arrmask[tamtext],teclapres.keyCode,strtext)		
		}
		else{
			DetonarEvent(Campo,strtext)
		}
	}
	else{//Aqui funcionaria a mascara para números mas eu ainda não implementei
		if ((arrmask[tamtext] == "A"))	{
			charupper = event.valueOf()
			//charupper = charupper.toUpperCase()
			DetonarEvent(Campo,strtext)
			masktext = strtext + charupper 
			Campo.value = masktext
		}
	}
}


function DetonarEvent(Campo,strtext){
	event.returnValue = false
	if (strtext != "") {
		Campo.value = strtext
	}
}

function OrganizarCasas(Campo,arrpos,teclapres_key,strtext){
	if (((arrpos == "/") || (arrpos == ".") || (arrpos == ",") || (arrpos == ":") || (arrpos == " ") || (arrpos == "-")) && !(teclapres_key == 8)){
		separador = arrpos
		masktext = strtext + separador
		Campo.value = masktext
	}
}


function AvaliarNumero(Objeto) {
	var x, i, ponto=0, pos_ponto, casas=0, retorno='';

	for(i=0; i<Objeto.value.length; i++) {
		x = Objeto.value.charAt(i);
		if(x=='.') 	x = ',';
		if(x==',') 	{ ponto += 1; pos_ponto = i; }
		if(ponto==1) 	casas = Objeto.value.substring(pos_ponto).length-2; 
		if(((!isNaN(x)) || (x == ',')) && (ponto <= 1) && (pos_ponto != 0) && (x != ' ') && (retorno.length < 10)) retorno = retorno + x; 
	}
	if((casas>1)) retorno = Objeto.value.substring(0, Objeto.value.length - 1);
	Objeto.value = retorno;
}

function AvaliarInteiro(Objeto) {
	var x, i, retorno='';
	for(i=0; i<Objeto.value.length; i++) { x = Objeto.value.charAt(i); if((!isNaN(x)) && (x != ' ') && (retorno.length < 10)) retorno = retorno + x; }
	Objeto.value = retorno;
}

function round_decimals2(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}


function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function ClearOptions(OptionList) {
   // Always clear an option list from the last entry to the first
   for (x = OptionList.length; x >= 0; x = x - 1) {
      OptionList[x] = null;
   }
}


function AddToOptionList(OptionList, OptionValue, OptionText) {
   // Add option to the bottom of the list
     var seloptlength = OptionList.options.length++; // Post increment
	OptionList.options[seloptlength].text = OptionText;
	OptionList.options[seloptlength].value = OptionValue;
}
