function optionSelect(campo, valor)
{
	for (i = 0; i < campo.length; i++)
	{
		if ( campo.options[i].value == valor )
			campo.options[i].selected = true;
	}
}

//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;
	}
	else
		return false;
}

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 false;
	}
	else
	{
		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 checked(campo, valor)
{
	if (valor==1)
		campo.checked = true;
	else
		campo.checked = false;
}


function enviaCheckApagar(objForm)
{
	if (isNaN(objForm.input_check.length))
	{
		cont = 0;
		if ( objForm.input_check.checked == true )
		{
			msg = objForm.input_check.value;
			cont = 1;	
		}
	}
	else
	{
		msg = "";
		cont = 0;
		for ( i=0; i < objForm.input_check.length; i++  )
		{
			if ( objForm.input_check[i].checked == true )
			{
				if ( msg != "" )
					msg += ",";	
				msg += objForm.input_check[i].value;
				cont++;
			}
		}
	}
	
	objForm.valores_sel.value = msg;
	objForm.valores_sel_qntd.value = cont;
	
	if ( cont > 0 )
	{
		if (cont==1)
			msg_apaga = "Um registro foi selecionado.\nApós apagá-lo, não será possível recuperá-lo.\nDeseja continuar?";
		else
			msg_apaga = "Foram selecionados "+ cont + " registros.\nApós apagá-los, não será possível recuperá-los.\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 passaValoresCheckbox(objForm)
{
	if (isNaN(objForm.input_check.length))
	{
		cont = 0;
		if ( objForm.input_check.checked == true )
		{
			msg = objForm.input_check.value;
			cont = 1;	
		}
	}
	else
	{
		msg = "";
		cont = 0;
		for ( i=0; i < objForm.input_check.length; i++  )
		{
			if ( objForm.input_check[i].checked == true )
			{
				if ( msg != "" )
					msg += ",";	
				msg += objForm.input_check[i].value;
				cont++;
			}
		}
	}
	
	objForm.valores_sel.value = msg;
	objForm.valores_sel_qntd.value = cont;
	
	if ( cont == 0 )
	{
		alert("Não é possível realizar a operação, pois nenhum registro está selecionado.");
		return false;
	}
}

function checkTodas(objForm, sit)
{
	if (isNaN(objForm.input_check.length))
	{
		objForm.input_check.checked = sit;
	}
	else
	{
		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;
}

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, 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;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}
function MascaraTelefone(objTextBox, sepTel, 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;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if (objTextBox.value.charAt(i) != sepTel) break;
    aux = '';
    if (len == 12) {
    	return false;
    }
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    switch (len) {
    	case 0:
    		objTextBox.value='';
    		break;
    	case 2:
    		objTextBox.value = aux + sepTel;
    		break;
    	case 6:
    		objTextBox.value = aux.substring(0,2) + sepTel + aux.substring(2,6) + sepTel;
    		break;
    	default:
    		ttt = objTextBox.value;
    		objTextBox.value = ttt+key;
    }
    return false;
}
function MascaraData(objTextBox, sepData, 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;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if (objTextBox.value.charAt(i) != sepData) break;
    aux = '';
    if (len == 12) {
    	return false;
    }
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    switch (len) {
    	case 0:
    		objTextBox.value='';
    		break;
    	case 2:
			var xax = aux * 1;
			if (xax < 31) {
	    		objTextBox.value = aux + sepData;
			}
			else {
				key = "";
			}
    		break;
    	case 4:
			var meses= new Array(13);
			meses[1]=31;meses[2]=29;meses[3]=31;meses[4]=30;meses[5]=31;meses[6]=30;meses[7]=31;meses[8]=31;meses[9]=30;meses[10]=31;
			meses[11]=30;meses[12]=31;
			var xaxdia = aux.substring(0,2) * 1;
			var xaxmes = aux.substring(2,4) * 1;
			if (xaxdia <= meses[xaxmes]) {
	    		objTextBox.value = aux.substring(0,2) + sepData + aux.substring(2,4) + sepData;
			}
			else {

				key = "";
			}
    		break;
    	default:
    		ttt = objTextBox.value;
    		objTextBox.value = ttt+key;
    }
    return false;
}
function MascaraIdentidade(objTextBox, sepId, 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;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if (objTextBox.value.charAt(i) != sepId && objTextBox.value.charAt(i) != "-" && objTextBox.value.charAt(i) != "/") break;
    aux = '';
    if (len == 12) {
	   	return false;
    }
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    switch (len) {
    	case 0:
    		objTextBox.value='';
    		break;
    	case 2:
    		objTextBox.value = aux + sepId;
    		break;
    	case 5:
    		objTextBox.value = aux.substring(0,2) + sepId + aux.substring(2,5) + sepId;
    		break;
		case 8:
    		objTextBox.value = aux.substring(0,2) + sepId + aux.substring(2,5) + sepId + aux.substring(5,8) + "-";
    		break;
    	default:
    		ttt = objTextBox.value;
    		objTextBox.value = ttt+key;
    }
	objTextBox.focus();
    return false;
}
function MascaraCpf(objTextBox, sepCpf, 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;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if (objTextBox.value.charAt(i) != sepCpf && objTextBox.value.charAt(i) != "-" && objTextBox.value.charAt(i) != "/") break;
    aux = '';
    if (len == 14) {
	   	return false;
    }
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    switch (len) {
    	case 0:
    		objTextBox.value='';
    		break;
    	case 3:
    		objTextBox.value = aux + sepCpf;
    		break;
    	case 6:
    		objTextBox.value = aux.substring(0,3) + sepCpf + aux.substring(3,6) + sepCpf;
    		break;
		case 9:
    		objTextBox.value = aux.substring(0,3) + sepCpf + aux.substring(3,6) + sepCpf + aux.substring(6,9) + "-";
    		break;
    	default:
    		ttt = objTextBox.value;
    		objTextBox.value = ttt+key;
    }
	objTextBox.focus();
    return false;
}
function MascaraCnpj(objTextBox, sepCnpj, 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;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if (objTextBox.value.charAt(i) != sepCnpj && objTextBox.value.charAt(i) != "-" && objTextBox.value.charAt(i) != "/") break;
    aux = '';
    if (len == 18) {
	   	return false;
    }
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    switch (len) {
    	case 0:
    		objTextBox.value='';
    		break;
    	case 2:
    		objTextBox.value = aux + sepCnpj;
    		break;
    	case 5:
    		objTextBox.value = aux.substring(0,2) + sepCnpj + aux.substring(2,5) + sepCnpj;
    		break;
		case 8:
    		objTextBox.value = aux.substring(0,2) + sepCnpj + aux.substring(2,5) + sepCnpj + aux.substring(5,8) + "/";
    		break;
		case 12:
    		objTextBox.value = aux.substring(0,2) + sepCnpj + aux.substring(2,5) + sepCnpj + aux.substring(5,8) + "/" + aux.substring(8,12) + "-";
    		break;
    	default:
    		ttt = objTextBox.value;
    		objTextBox.value = ttt+key;
    }
	cCnpj.focus();
    return false;
}
function MascaraCep(objTextBox, sepCep, 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;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if (objTextBox.value.charAt(i) != sepCep) break;
    aux = '';
    if (len == 9) {
    	return false;
    }
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    switch (len) {
    	case 0:
    		objTextBox.value='';
    		break;
    	case 5:
    		objTextBox.value = aux + sepCep;
    		break;
    	default:
    		ttt = objTextBox.value;
    		objTextBox.value = ttt+key;
			break
    }
    return false;
}
