// JavaScript Document

function valida_text(id)
{
 if ( (document.getElementById(id).value!=null)&&(document.getElementById(id).value!="") )
   return true;
  else
   return false;   
}

function form_error(mensagem,id_div)
{
 if ( $(id_div).type != 'radio' && $(id_div).type != 'checkbox' )
    $(id_div).style.border = '1px solid red';
 return " "+mensagem;
}

function form_limpa(id_div)
{
 if ( $(id_div).type != 'radio' && $(id_div).type != 'checkbox' )
    $(id_div).style.border = '1px solid green';
}

function formLimpaCampoErro() {
	validar.each(function(s) {		
		$(s+'_erro').innerHTML = "";
		$(s).style.border = '';
	});
}

function valida_select(id)
{
 obj = document.getElementById(id);
 if ( obj.selectedIndex == 0 )
    return false;
   else
    return true;
}

function get_valor(id)
{
return document.getElementById(id).value;
}

function cancela(id,mensagem)
{
 document.getElementById(id).focus();	 
 alert(mensagem);
 return false;
}

function valida_email(id) {
        str = get_valor(id);

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){		   
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    
		    return false
		 }

 		 return true					
	}
	
function formatar(src, mask) 
{
  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);
     }
}

/**
* Formata telefone (Mascara)
* @param $campo Int id
* @param $event Evento
* @return String telefone formatado
*/
function formataTelefone(campo, e)
{
	t = typeof window.event != "undefined" ? window.event.keyCode : e.which;
	if((isNaN(String.fromCharCode(t)) || (t == 32)) && (t != 8 && t != 0)){
		return false;
	}
	campo = typeof(campo) == "object" ? campo : document.getElementById(campo);

	if(campo.value.charAt(0) != "(" && !isNaN(String.fromCharCode(t)))
		campo.value = "("+campo.value;

	if( typeof window.event != "undefined" )
	{
		if(window.event.keyCode != 40)
		{
			if(campo.value.length == 0)
			{
				campo.value += '(';
			}
		}
		if(window.event.keyCode != 41)
		{
			if(campo.value.length == 3)
			{
				campo.value += ')';
			}
		}
		if(window.event.keyCode != 45)
		{
			if(campo.value.length == 8)
			{
				campo.value += '-';
			}
		}
	}
	else
	{
		if (e.which != 8)
        {
			if(e.which != 0){
				if(e.which != 40)
				{
					if(campo.value.length == 0)
					{
						campo.value += '(';
					}
				}
			}
			if(e.which != 41)
			{
				if(campo.value.length == 3)
				{
					campo.value += ')';
				}
			}
			if(e.which != 45)
			{
				if(campo.value.length == 8)
				{
					campo.value += '-';
				}
			}
		}
	}
}

/**
* Formata Número somente numeros
* @param $campo Int id
* @param $event Evento
* @return numero
*/
function formataNumero(campo, e)
{
	t = typeof window.event != "undefined" ? window.event.keyCode : e.which;

	if((isNaN(String.fromCharCode(t)) || (t == 32)) && (t != 8 && t != 0))
    {
		return false;
	}
}

/**
* Formata a digitação do CEP
* @param $campo Int id
* @param $event Evento
* @return cep formatado
*/
function formataCep(campo, e)
{
	t = typeof window.event != "undefined" ? window.event.keyCode : e.which;

	if((isNaN(String.fromCharCode(t)) || (t == 32)) && (t != 8 && t != 0)){
		return false;
	}
	if( typeof window.event != "undefined" )
	{
		if (window.event.keyCode != 45)
		{
			if (campo.value.length == 5)
			{
				campo.value += '-';
			}
		}
	}
	else
	{
		if (e.which != 8)
        {
			if (e.which != 45)
			{
				if (campo.value.length == 5)
				{
					campo.value += '-';
				}
			}
		}
	}
}

/**
* Formata a digitação do CPF
* @param $campo Int id
* @param $event Evento
* @return CPF formatado
*/
function formataCpf(campo, e)
{
	t = typeof window.event != "undefined" ? window.event.keyCode : e.which;

	if((isNaN(String.fromCharCode(t)) || (t == 32)) && (t != 8 && t != 0)){
		return false;
	}

        if( typeof window.event != "undefined" )
        {
                if (window.event.keyCode != 46)
				{
	                if ((campo.value.length == 3) || (campo.value.length == 7))
					{
			            campo.value += '.';
					}
				}
                if (window.event.keyCode != 45)
                if (campo.value.length == 11)
                campo.value += '-';
        }
        else
        {
                if (e.which != 8)
                {
                        if (e.which != 46)
                        if ((campo.value.length == 3) || (campo.value.length == 7))
                        campo.value += '.';
                        if (e.which != 45)
                        if (campo.value.length == 11)
                        campo.value += '-';
                }
        }
}

/**
* Formata Número Decimal
* @param $campo id do campo
* @param $e event do compo
* @return boleean
*/
function formataNumeroDecimal(campo, e) {
	t = typeof window.event != "undefined" ? window.event.keyCode : e.which;

	if(isNaN(String.fromCharCode(t)) && (t != 8 && t != 0 && t != 44 && t != 46)) {
		return false;
	}
	
	if(t == 44){
		t = 46;
		if(campo.value.indexOf('.') == -1)
			campo.value += String.fromCharCode(t);
		return false;
	}
	if ((e.which == 46) && (campo.value.indexOf('.') != -1)) {
		return false;
	}
	return true;
}


