function percorre(obj) {
	var string = "";

	var child = obj.firstChild;
	

	
	while(child) {
		if (child.nodeType == 1) 
			if (child.tagName == "INPUT" && (child.type == "text" || child.type == "hidden") || child.type == "password" || child.tagName == "TEXTAREA") 
				string += (child.name + "=" + encodeURIComponent(child.value) + "&");
			else if (child.tagName == "INPUT" && (child.type == "radio" || child.type == "checkbox") && child.checked ) 
				string += (child.name + "=" + encodeURIComponent(child.value) + "&");
			else if (child.tagName == "SELECT")
				string += (child.name + "=" + encodeURIComponent(child.options[child.selectedIndex].value) + "&");
	
		
	
		string += percorre(child);
		child = child.nextSibling;
		}
	return string;		
	}

function envia (formulario, url) {
	formulario = document.getElementById(formulario);
	formulario.action = url;
	formulario.submit();	
	}
	
function limpar (obj) {
	document.getElementById(obj).innerHTML = "";
	}

function formatar(myfield, e, mask)
{
	var key;
	var keychar;
	var i = myfield.value.length;
	var saida = '#';
	var texto = mask.substring(i);
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	if ((key==null) || (key==0) || (key==8) || 
		(key==9) || (key==13) || (key==27) )
	   return true;
	
	else if ((("0123456789.").indexOf(keychar) > -1))
	{
		if (texto.substring(0,1) != saida )
			myfield.value += texto.substring(0,1);
	   return true;
	}
	else
	   return false;
}

function Clear(valor, validos)
{
	// retira caracteres invalidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++)
	{
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0)
		{
			result += aux;
		}
	}
	return result;
}

//Formata número tipo moeda usando o evento onKeyDown

function FormatarValor(campo,tammax,teclapres,decimal)
{
	var tecla = teclapres.keyCode;
	vr = Clear(campo.value,"0123456789");
	tam = vr.length;
	dec=decimal
	
	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 <= dec )
		{
			campo.value = vr ;
		}
		
		if ( (tam > dec) && (tam <= 5) )
		{
			campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ;
		}
		if ( (tam >= 6) && (tam <= 8) )
		{
			campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, 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 - dec, 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 - dec, 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 ConfirmLogoff()
{
	if (confirm('Tem certeza que deseja efetuar Logoff?'))
	{
		location.href='/index/logoff';
	}	
}

function ConfirmNext(controller)
{
	location.href='/'+controller+'/search';
}

function ConfirmExclude(controller, id)
{
	if (confirm('Deseja realmente excluir este Registro?\n'))
	{
		location.href='/'+controller+'/exclude/?id_'+controller+'='+id;
	}	
}