var isNN = (navigator.appName.indexOf("Netscape")!=-1);
var ie = (document.all)? true:false

function confirma()
{
	var form = document.forms.frm_cadastro;
	if( form.no_cadastro.value == '' )
	{
		alert("O campo [Nome:] é de preenchimento obrigatório!");
		form.no_cadastro.focus();
		return false;
	}
	if( form.dt_nascimento.value == '' )
	{
		alert("O campo [Data de Nascimento:] é de preenchimento obrigatório!");
		form.dt_nascimento.focus();
		return false;
	}
	if( form.nr_cpf.value == '' )
	{
		alert("O campo [CPF:] é de preenchimento obrigatório!");
		form.nr_cpf.focus();
		return false;
	}
	if( form.ds_endereco.value == '' )
	{
		alert("O campo [Endereço:] é de preenchimento obrigatório!");
		form.ds_endereco.focus();
		return false;
	}
	if( form.ds_numero.value == '' )
	{
		alert("O campo [Número:] é de preenchimento obrigatório!");
		form.ds_numero.focus();
		return false;
	}
	if( form.ds_complemento.value == '' )
	{
		alert("O campo [Complemento:] é de preenchimento obrigatório!");
		form.ds_complemento.focus();
		return false;
	}
	if( form.ds_bairro.value == '' )
	{
		alert("O campo [Bairro:] é de preenchimento obrigatório!");
		form.ds_bairro.focus();
		return false;
	}
	if( form.ds_cidade.value == '' )
	{
		alert("O campo [Cidade:] é de preenchimento obrigatório!");
		form.ds_cidade.focus();
		return false;
	}
	if( form.ds_estado.value == '' )
	{
		alert("O campo [Estado:] é de preenchimento obrigatório!");
		form.ds_estado.focus();
		return false;
	}
	return true;
}

function VCPF( obj )
 {		
	var wVr, wVrCPF, wVrSEQ, wTam, wSoma, wSoma2, i, j, wDig1, wDig2,aux,
	    wVETOR_CC = new Array(11),
	    wVETOR_PESO = new Array(11);
 	var Erro = false;
	wVrCPF = obj.value;
	
	if (wVrCPF != '')
	{
	
  	aux = obj.value;	  
  	wVr= aux.toString().substring(0,3) + aux.toString().substring(4,7) + aux.toString().substring(8,11) + aux.toString().substring(12,14) ;     
  	wTam = wVr.length + 1;
    if (wTam < 11)
    {
    	  alert("Nº de dígitos do CPF menor que o normal. Redigite !!!");
    	  obj.value = "";
    	  obj.focus();
    	  return false;
    }    
  	for (i = 0; i < wVr.length; i++)
  	{
  	   if (isNaN(parseInt(wVr.charAt(i))) )
  		 {
    	   alert("O CPF contém dígitos inválidos. Corrija-o !!!");
  	     obj.value = "";
  	     obj.focus();
  	     return false;
  	   }
  	}
    wSoma = 0;
  	wSoma2 = 0;
    j = 2;
    for (i = 0; i < 11; i++)
    {
      wVETOR_CC[i] = wVr.charAt(i);
      wVETOR_PESO[i] = j;
      j++; 
    } 
    i = 0;	  
    while (i < 9) 
    {
      i++;
      if (i < 10)
      {
            wSoma += wVETOR_CC[9 - i] * wVETOR_PESO[i - 1];
      }
      wSoma2 += wVETOR_CC[10 - i] * wVETOR_PESO[i - 1];
    }
  	wDig1 = (wSoma * 10) % 11;
  	wDig2 = (wSoma2 * 10) % 11;
  	if (wDig1 == 10)
  	{ 
  	    wDig1 = 0;
  	}	
  	if (wDig2 == 10)
  	{ 
  	    wDig2 = 0;
  	}		
    if (parseInt(wVr.charAt(9)) != wDig1 || parseInt(wVr.charAt(10)) != wDig2)
  	{
    	 alert("CPF com Dígito Verificador inválido. Redigite-o !!!");
  	   obj.focus();
  	   return false;
    }	
    obj.value = wVrCPF;
  	return true;
	}
	else
	{
	  return true;
	}
}

function FormataData(campo)
{
  if (isNN) return;
  campo.value = FiltraCampo(campo);
  vr = campo.value;
  tam = vr.length;
  if (tam > 2 && tam < 5)
	  campo.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
  if ( tam >= 5 && tam <= 10 ) 
	  campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
}

function textFormat(objField,evtKeyPress,mask) {
  var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

  if(ie) { // Internet Explorer
    nTecla = evtKeyPress.keyCode; }
  else  { // Nestcape
    nTecla = evtKeyPress.which;
  }
	
  //objField.form.procedimento_tabela_cd_tab_amb.value = nTecla+evtKeyPress.type;
  if ((nTecla==9)) 
      return true;
	if (evtKeyPress.type=='keydown'){ 
	   if ((nTecla==37) || (nTecla==39) || (nTecla==9)) //Seta para a direita seta para a esquerda
		    return false;
	return true;			
	}

  sValue = objField.value;
	
	var valor=false;
  if (mask.charAt(0)=='$'){
     mask = mask.substr(1);
		 valor = true;
	}	  
  
	if (evtKeyPress.type=='keypress') {
		 var digMask=mask.charAt(sValue.length);
     if ( digMask== "9") { // apenas números...
       return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
     else if (digMask == "a") { // alfa-numerico convertendo para minusculo...
		 if ( nTecla>=65 && nTecla<=90 ) 
       {
          key = String.fromCharCode(nTecla); 
          key = key.toLowerCase();
          if(ie) { // Internet Explorer
            event.keyCode=key.charCodeAt();}
          else {
            event.which=key.charCodeAt();
          }
       }
       return true; } 
    else if (digMask == "A") { // alfa-numerico convertendo para maiusculo...
		 if ( nTecla>=97 && nTecla<=122 ) 
       {
          key = String.fromCharCode(nTecla); 
          key = key.toUpperCase();
          if(ei) { // Internet Explorer
            event.keyCode=key.charCodeAt();}
          else {
            event.which=key.charCodeAt();
          }
       }
      return true; } 
    else if (digMask == "l") { // apenas letra convertendo para minusculo...
		 if (( nTecla>=65 && nTecla<=90 ) || ( nTecla>=97 && nTecla<=122 ))
       {
          key = String.fromCharCode(nTecla); 
          key = key.toLowerCase();
          if(ie) { // Internet Explorer
            event.keyCode=key.charCodeAt();}
          else {
            event.which=key.charCodeAt();
          }
      		return true;  
       }
       return false;}  
    else if (digMask == "L") { // apenas letra convertendo para maiusculo...
		 if (( nTecla>=65 && nTecla<=90 ) || ( nTecla>=97 && nTecla<=122 ))
       {
          key = String.fromCharCode(nTecla); 
          key = key.toUpperCase();
          if(ie) { // Internet Explorer
            event.keyCode=key.charCodeAt();}
          else {
            event.which=key.charCodeAt();
          }
      		return true;  
       }
       return false;}  
    else { // qualquer caracter...
      return true;
    } 
	}

  // Limpa todos os caracteres de formatação que
  // já estiverem no campo.
	var sai;
	var typeMask = new RegExp ("[-.,\:/() ]");
	while ((sai=typeMask.exec(sValue)) != null) sValue = sValue.toString().replace( sai, "" );

  fldLen = sValue.length;
  mskLen = fldLen;

	if (nTecla == 8) mskLen--; //se for backspace tamanho-1

  sCod = "";
	if (!valor) {
    //formata deacordo com a mascara da esqueda para a direita
    nCount = 0;
  	for (var i=0; i<=mskLen; i++){
      if (typeMask.exec(mask.charAt(i))!=null) {
        sCod += mask.charAt(i);
        mskLen++; }
      else{
        sCod += sValue.charAt(nCount);
        nCount++;
      }
    }
	}else{
    //formata deacordo com a mascara da direita para a esqueda
		sValue *= 1;
		if (sValue==0) {
		   sValue = '';
		}else{
		   sValue += '';
		}
    fldLen = sValue.length;
		//objField.form.paciente_nr_ident.value = sValue+'-'+fldLen;
		/*if (fldLen==1){
		   sValue = '00'+sValue;
		}
		if (fldLen==2){
		   sValue = '0'+sValue;
		}*/
    fldLen = sValue.length-1;
		mskLen = mask.length-1;
  	for (var i=fldLen; i>=0; i--){
      if (typeMask.exec(mask.charAt(mskLen))!=null) {
        sCod = mask.charAt(mskLen)+sCod;
				i++;
			}else{
        sCod = sValue.charAt(i)+sCod;
			}
			mskLen--;
    }
	}
  objField.value = sCod;

  return true;
}