//Função para qualquer tipo de máscara
function formatar_mascara(src, mask, somentenumero) 
{
	if(event.keyCode<32) //numero
		return
	if(somentenumero && (event.keyCode<48 || event.keyCode>57))  //letra
		event.keyCode=0
	else 
   	{
		 var i = src.value.length
		 var saida = mask.substring(0,1)
		 var texto = mask.substring(i)
		 if (texto.substring(0,1) != saida) 
			src.value += texto.substring(0,1)
	}
}

// Validação de data
function valida_data(str)
{  
	var situacao = true;
	var str = str.split("/");
	var dia = str[0];  
	var mes = str[1];
	var ano = str[2];
	
	if ( ano.length < 4 )
		return false;

	if (isNaN(dia) || isNaN(mes) || isNaN(ano) )
		return false;
		


	// Verifica se dia e mês são válidos
	if ((dia < 1 || dia > 31) || (mes < 1 || mes > 12 ))
		return false;
	else
	{
		// verifica o dia valido para cada mes
		if ((dia > 30) && ( mes == 04 || mes == 06 || mes == 09 || mes == 11 ))
			return false;
	} 
	
	// verifica se e ano bissexto  
	if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4))))
	    return false;
		
	if (situacao == true)
		return true;

}

// tipo: n (numéricos), a (alfabeto), an (alfanuméricos)
function verificaTipo(objCampo, tipo)
{
	var msg;	
	if ( tipo == "n" )
		var reTipo = /[^0-9]/;
	else if ( tipo == "an" )
		var reTipo = /[^a-zA-Z0-9_]/;
	else
		var reTipo = /[^a-zA-Z0-9_]/;
	
	if ( reTipo.test(objCampo.value) )
	{
		alert("Campo com caracteres inválidos!");
		objCampo.focus();
		return true;
	}
	else
		return false;		
}

function verificaSeVazio(objCampo, nome)
{
	if (objCampo.value == "#"  || objCampo.value == "" )
	{
		alert("O campo "+ nome +" deve ser preenchido")
		if (objCampo.type != "hidden")
			objCampo.focus();
		return true;
	}
}

function verificaTamanho(objCampo, nome, t_min, t_max)
{
	if (objCampo.value.length < t_min || objCampo.value.length > t_max)
	{
		if ( t_min == t_max )
			alert("O campo "+ nome +"  deve ter exatamente "+ t_max +" caracteres.")
		else
			alert("O campo "+ nome +"  deve ter no mínimo "+ t_min +"  e no máximo "+ t_max +" caracteres.")
		
		if (objCampo.type != "hidden")
			objCampo.focus();
		return true;
	}
}

function verificaCampo(objCampo, nome, t_min, t_max, tipo)
{
	if (verificaSeVazio(objCampo, nome) == true)
		return false;	
	else if (verificaTamanho(objCampo, nome, t_min, t_max) == true)
		return false;
	else if (verificaTipo(objCampo, tipo) == true)
		return false;
	else
		return true;	
}

function validaEmail(objCampo)
{
	var reEmail1 = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;	
	if ( reEmail1.test(objCampo.value) )
		return true;
	else
	{
		alert("E-mail inválido");
		objCampo.focus();
		return false;	
	}	
}

function verificaSeValorFinanceiro(objCampo)
{
	reMoeda = /^\d{1,3}(\.\d{3})*\,\d{2}$/;
	if ( reMoeda.test(objCampo.value) )
		return true;
	else
	{
		alert("Valor inválido! Digite corretamente um valor financeiro.");
		objCampo.focus();
		return false;	
	}
}

function comparaSenhas(objSenha,objSenhaConfirma)
{
	if (objSenha.value != objSenhaConfirma.value)
	{
		alert("A senha e sua confirmação não correspondem.");
		objSenha.focus();
		return false;
	}
}

function check(campo, valor)
{
	campo.value = valor;
}

function optionSelect(campo, valor)
{
	for (i = 0; i < campo.length; i++)
	{
		if ( campo.options[i].value == valor )
			campo.options[i].selected = true;
	}
}

function enviaCheckApagar(objForm)
{
	cont = 0;
	for ( i=0; i < objForm.input_check.length; i++  )
	{
		if ( objForm.input_check[i].checked == true )
			cont++;
	}
	
	if ( cont > 0 )
	{
		msg = "";
		n = 0;
		for ( i=0; i < objForm.input_check.length; i++  )
		{
			if ( objForm.input_check[i].checked == true )
			{
				msg += objForm.input_check[i].value;
				n++;
				if ( n < cont )
					msg += ",";				
			}
		}
		objForm.valores_sel.value = msg;
		objForm.valores_sel_qntd.value = cont;
		
		if ( cont > 1 )
			msg_apaga = "Foram selecionados "+ cont + " registros.\nApós apagá-los, não será possível recuperá-los.\nDeseja continuar?";
		else
			msg_apaga = "Um registro foi selecionado.\nApós apagá-lo, não será possível recuperá-lo.\nDeseja continuar?";
		
		if (confirm (msg_apaga))
			objForm.submit();
		else 
			return false
	}
	else
		alert("Não é possível apagar, pois nenhum registro está selecionada.");
	return false;
}

function enviaCheck(objForm)
{
	cont = 0;
	for ( i=0; i < objForm.input_check.length; i++  )
	{
		if ( objForm.input_check[i].checked == true )
			cont++;
	}
	
	if ( cont > 0 )
	{
		msg = "";
		n = 0;
		for ( i=0; i < objForm.input_check.length; i++  )
		{
			if ( objForm.input_check[i].checked == true )
			{
				msg += objForm.input_check[i].value;
				n++;
				if ( n < cont )
					msg += ",";				
			}
		}
		objForm.valores_sel.value = msg;
		objForm.valores_sel_qntd.value = cont;
		//alert(objForm)
		
		objForm.submit();
	}
	else
		alert("Não é possível mudar status, pois nenhum registro está selecionado.");
	return false;
}

function checkTodas(objForm, sit)
{
	for ( i=0; i < objForm.input_check.length; i++  )
		objForm.input_check[i].checked = sit;
	
	if ( sit == false )
		objForm.check_todas.situacao = true;
	else
		objForm.check_todas.situacao = false;
}

function contarCaracteres(objCampo, num_caracteres, objContador)
{
	if( (num_caracteres - objCampo.value.length) < 0 )
	{
		alert('Atenção, você atingiu o limite máximo de caracteres:'+ num_caracteres +'.');
		objCampo.value=objCampo.value.substr(0,num_caracteres);
	}
	objContador.innerHTML = num_caracteres - objCampo.value.length;
}

function add_sel(oOrig, oDest)
{
	  // Recupera objetos
	  //oOrig = document.formpasso3.elements[sOrig];
	  //oDest = document.formpasso3.elements[sDest];
	  // Varre itens da origem
	  for (i = 0; i < oOrig.length; i++)
	  {
		   // Item na origem selecionado e valido
		   if (oOrig.options[i].selected && !oOrig.options[i].disabled)
		   {
		    // Recupera valores da origem
		    sText  = oOrig.options[i].text;
		    sValue = oOrig.options[i].value;
		    // Cria item no destino
		    oDest.options[oDest.length] = new Option(sText, sValue);
		    // Desabilita item na origem
		    oOrig.options[i].style.color = "#A0A0A0";
		    oOrig.options[i].disabled    = true;
		    oOrig.options[i].selected    = false;
		   }
	  }
	  // Reset combos
	  oOrig.selectedIndex = -1;
	  oDest.selectedIndex = -1;
}

function del_sel(oOrig, oDest)
{
  // Recupera objetos
  //oOrig = document.formpasso3.elements[sOrig];
  //oDest = document.formpasso3.elements[sDest];
  aSel  = new Array();
  atxt  = new Array();
  solt  = new Array();
  j     = 0;
  z     = 0;
  // Remove itens selecionados na origem
  for (i = oOrig.length - 1; i >= 0; i--)
  {
   // Item na origem selecionado
   if (oOrig.options[i].selected)
   {
    aSel[j] = oOrig.options[i].value;
    atxt[j] = oOrig.options[i].text;
    j++;
    oOrig.options[i] = null;
   }
  }
  // Habilita itens no destino
  for (i = 0; i < oDest.length; i++)
  {
   if (oDest.options[i].disabled && in_array(aSel, oDest.options[i].value))
   {
    oDest.options[i].disabled    = false;
    oDest.options[i].style.color = "";
    solt[z] = oDest.options[i].value;
    z++;
   }
  }
  for (i = 0; i < aSel.length; i++)
  {
   if (!in_array(solt, aSel[i]))
   {
    oDest.options[oDest.length] = new Option(atxt[i], aSel[i]);
   }
  }
  // Reset combos
  oOrig.selectedIndex = -1;
  oDest.selectedIndex = -1;
}

function in_array(aArray, sElem)
{
  for (iCount = 0; iCount < aArray.length; iCount++)
  {
   if (sElem == aArray[iCount])
	    return true;
  }
  return false;
}