/************************************************
   Funções JavaScript 
   
************************************************/

 function Erro(obj){
 
     obj = eval( obj )
     window.alert("Digite ou escolha um valor para o campo " + obj.name);
     obj.setfocus; 
   	 obj.select();
 
 }


 /*****************************************************************/

    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);
      }
    }


 /*****************************************************************/

 function Verificar( obj ){

    var tecla = event.keyCode;

	if ((tecla == 9) || (tecla == 13)){
			 
        if (obj.value == ""){

		     obj.focus();
		     obj.select();
             Erro(obj);
		     event.returnValue = false;
			 return false;
        }
    
	}
 }


 /*****************************************************************/

 function Verificar_Email( obj ) {

    var tecla = event.keyCode;

	if ((tecla == 9) || (tecla == 13)){

         var email = obj.value;
         if ((email.indexOf("@",0) < 1) || (email.indexOf("@",0) == email.length - 1)) {
             window.alert("O seu e-mail está incorreto!");
	         obj.focus();
	         obj.select();
             return(false);

         }

	 }
 }
 

 /*****************************************************************/

   function Validar_email() {

        var ok = true;
        if(form1.email.value.search("@") == -1 || form1.email.value.search("[.*]") == -1){
            alert("Seu email está incorreto!");
            form1.email.focus();
            ok = false;
            return(false);
        }
    }
 
 /*****************************************************************/
 
 function Enviar(){
	    document.form1.submit(); 
 }
 

 /*****************************************************************/

   function Formatar_telefone( campo ){   

    Verificar(campo);
	
	if( !campo ) return true; // caso o campo não exista desconsidera a validação
    var tecla = event.keyCode;
    var p = campo.value;
    var p1 = campo;

    if( ( tecla >= 48 && tecla <= 57  && !event.shiftKey ) || 
        ( tecla >= 96 && tecla <= 105 && !event.shiftKey ) ) {

        if( tecla > 57 )
            event.keyCode = event.keyCode - 48;

    if( p.length == 2 ) {
            pp = p;
            d4 = p.indexOf( '(' );
            d5 = p.indexOf( ')' );
            if( d4 == -1 ) {
                pp = "(" + pp;
            }
            if( d5 == -1 ) {
                pp = pp + ")";
            }
            p1.value = "";
            p1.value = pp;
        }

        if( p.length > 2 ) {
            d1 = p.indexOf( '(' )
            d2 = p.indexOf( ')' )
            if ( d2 == -1 ) {
                p30 = p.length;
                p30 = p.substring( 0, 4 );
                //alert(p30);
                p30 = p30 + ")"
                p31 = p.substring( 4, l30 );
                pp = p30 + p31;
                //alert(p31);
                p1.value = "";
                p1.value = pp;
            }
        }

        if( p.length > 5 ) {
            p11 = p.substring( d1 + 1, d2 );
            if( p11.length > 3 ) {
                p12 = p11;
                l12 = p12.length;
                l15 = p.length;
                //l12=l12-3
                p13 = p11.substring( 0, 3   );
                p14 = p11.substring( 3, l12 );
                p15 =   p.substring( d2 + 1, l15 );
                p1.value = "";
                pp = "(" + p13 + ")" + p14 + p15;
                p1.value = pp;
                //obj1.value="";
                //obj1.value=pp;
            }

            l16 = p.length;
            p16 = p.substring( d2 + 1, l16 );
            l17 = p16.length;
            if( l17 > 3 && p16.indexOf( '-' ) == -1 ) {
                p17 = p.substring( d2 + 1, d2 + 5 );
                p18 = p.substring( d2 + 5, l16 );
                p19 = p.substring( 0, d2 + 1 );
                //alert(p19);
                pp = p19 + p17 + "-" + p18;
                p1.value = "";
                p1.value = pp;
                //obj1.value="";
                //obj1.value=pp;
            }
        }

    }
    else {
        var teclaVolta   = 8;
        var teclaDelete  = 46;
        var teclaTab     = 9;
        var teclaSetaEsq = 37;
        var teclaSetaDir = 39;

        if( tecla != teclaVolta && 
            tecla != teclaDelete && 
            tecla != teclaTab &&
            tecla != teclaSetaDir &&
            tecla != teclaSetaEsq ) 
            event.keyCode = 39;
    }
}

 /*****************************************************************/

function Formatar_reais(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; 

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; 

} 

