function formataDoc(obj,evento,form){
wTecla = evento.keyCode;
if (obj.value.length<12)
   { FormataCPF(form,obj.name,11,8,5,2,wTecla); }
else
   { FormataCNPJ(form,obj.name,14,12,6,2,wTecla); }
}
function FormataCNPJ(pForm,pCampo,pTamMax,pPos1,pPos2,pPosTraco,pTeclaPres){
 var wTecla, wVr, wTam;
 
 wTecla = pTeclaPres;
 wVr = pForm[pCampo].value;
 wVr = wVr.toString().replace( "-", "" );
 wVr = wVr.toString().replace( ".", "" );
 wVr = wVr.toString().replace( ".", "" );
 wVr = wVr.toString().replace( "/", "" );
 wTam = wVr.length ;

 if (wTam < pTamMax && wTecla != 8) { 
    wTam = wVr.length + 1 ; 
 }
 if (wTecla == 8 ) { 
    wTam = wTam - 1 ; 
 }
 if ( wTecla == 8 || wTecla == 88 || wTecla >= 48 && wTecla <= 57 || wTecla >= 96 && wTecla <= 105 ){
  if ( wTam <= 2 ){
    pForm[pCampo].value = wVr ;
  }
  if (wTam > pPosTraco && wTam <= pTamMax) {
        wVr = wVr.substr(0, wTam - pPosTraco) + '-' + wVr.substr(wTam - pPosTraco, wTam);
  }
  if ( wTam == pTamMax){
        wVr = wVr.substr( 0, wTam - pPos1 ) + '.' + wVr.substr(wTam - pPos1, 3)+'.' + wVr.substr(wTam - (pPos1-3), 3) + '/' + wVr.substr(wTam - pPos2, wTam);
  }
  pForm[pCampo].value = wVr;
 }
}

function FormataCPF(pForm,pCampo,pTamMax,pPos1,pPos2,pPosTraco,pTeclaPres){
 var wTecla, wVr, wTam;
 
 wTecla = pTeclaPres;
 wVr = pForm[pCampo].value;
 wVr = wVr.toString().replace( "-", "" );
 wVr = wVr.toString().replace( ".", "" );
 wVr = wVr.toString().replace( ".", "" );
 wVr = wVr.toString().replace( "/", "" );
 wTam = wVr.length ;

 if (wTam < pTamMax && wTecla != 8) { 
    wTam = wVr.length + 1 ; 
 }
 if (wTecla == 8 ) { 
    wTam = wTam - 1 ; 
 }
 if ( wTecla == 8 || wTecla == 88 || wTecla >= 48 && wTecla <= 57 || wTecla >= 96 && wTecla <= 105 ){
  if ( wTam <= 2 ){
    pForm[pCampo].value = wVr ;
  }
  if (wTam > pPosTraco && wTam <= pTamMax) {
        wVr = wVr.substr(0, wTam - pPosTraco) + '-' + wVr.substr(wTam - pPosTraco, wTam);
  }
  if ( wTam == pTamMax){
        wVr = wVr.substr( 0, wTam - pPos1 ) + '.' + wVr.substr(wTam - pPos1, 3) + '.' + wVr.substr(wTam - pPos2, wTam);
  }
  pForm[pCampo].value = wVr;
 }
}
 
function cpf(pcpf)
 {
  pcpf = pcpf.toString().replace( "-", "" );
  pcpf = pcpf.toString().replace( ".", "" );
  pcpf = pcpf.toString().replace( ".", "" );
  pcpf = pcpf.toString().replace( "/", "" );
  if (pcpf.length != 11 || pcpf == "00000000000" || pcpf == "11111111111" ||
	pcpf == "22222222222" ||	pcpf == "33333333333" || pcpf == "44444444444" ||
	pcpf == "55555555555" || pcpf == "66666666666" || pcpf == "77777777777" ||
	pcpf == "88888888888" || pcpf == "99999999999")	{
	sim=false;
    }
 else {sim=true}

  if (sim )  // valida o primeiro digito
  {
  for (i=0;((i<=(pcpf.length-1))&& sim); i++)
  {
   val = pcpf.charAt(i)
   if

 ((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4")

 &&    (val!="5")&&(val!="6")&&(val!="7")&&(val!="8")) {sim=false}
   }

   if (sim)
  {
    soma = 0
    for (i=0;i<=8;i++)
    {
     val = eval(pcpf.charAt(i))
     soma = soma + (val*(i+1))
    }

    resto = soma % 11
    if (resto>9) dig = resto -10
    else  dig = resto
    if (dig != eval(pcpf.charAt(9))) { sim=false }
   else   // valida o segundo digito
    {

     soma = 0
    for (i=0;i<=7;i++)
     {
     val = eval(pcpf.charAt(i+1))
      soma = soma + (val*(i+1))
    }

     soma = soma + (dig * 9)
    resto = soma % 11
     if (resto>9) dig = resto -10
     else  dig = resto
   if (dig != eval(pcpf.charAt(10))) { sim = false }
    else sim = true
   }
   }
  }

  if (sim) { return true; }
  else
  	{
    alert("Valor inválido de CPF");
	return false;
	}
 }
//---------------------------------------
function cnpj(pcgc)
 {
  // verifica o tamanho
  pcgc = pcgc.toString().replace( "-", "" );
  pcgc = pcgc.toString().replace( ".", "" );
  pcgc = pcgc.toString().replace( ".", "" );
  pcgc = pcgc.toString().replace( "/", "" );
  if (pcgc.length != 14 || pcgc == "00000000000000" || pcgc == "11111111111111" ||
	pcgc == "22222222222222" ||	pcgc == "33333333333333" || pcgc == "44444444444444" ||
	pcgc == "55555555555555" || pcgc == "66666666666666" || pcgc == "77777777777777" ||
	pcgc == "88888888888888" || pcgc == "99999999999999")	{
	sim=false;
    }
 else 
   {sim=true;}

  if (sim )  // verifica se e numero
  {
  for (i=0;((i<=(pcgc.length-1))&& sim); i++)
  {
   val = pcgc.charAt(i)
       // alert (val)
   if
((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4") &&
(val!="5")&&(val!="6")&&(val!="7")&&(val!="8")) {sim=false}
   }
   if (sim)  // se for numero continua
   {
    m2 = 2
    soma1 = 0
    soma2 = 0
    for (i=11;i>=0;i--)
    {
     val = eval(pcgc.charAt(i))
       // alert ("Valor do Val: "+val)
     m1 = m2
  if (m2<9) { m2 = m2+1}
  else {m2 = 2}
  soma1 = soma1 + (val * m1)
  soma2 = soma2 + (val * m2)
    }  // fim do for de soma

  soma1 = soma1 % 11
  if (soma1 < 2) {  d1 = 0}
   else { d1 = 11- soma1}

     soma2 = (soma2 + (2 * d1)) % 11
  if (soma2 < 2) { d2 = 0}
   else { d2 = 11- soma2}
        // alert (d1)
       // alert (d2)
    if ((d1==pcgc.charAt(12)) && (d2==pcgc.charAt(13)))
   { return true; }
   else {
   alert("Valor inválido de CNPJ");
   return false;
   }
   }
 }
  if (sim) { return true; }
  else
  	{
    alert("Valor inválido de CNPJ");
	return false;
	}
 }
var symbols = " !\"#$%&'()*+'-./0123456789:;<=>?@";
function toAscii(obj)  {
	var loAZ = "abcdefghijklmnopqrstuvwxyz";
	symbols+= loAZ.toUpperCase();
	symbols+= "[\\]^_`";
	symbols+= loAZ;
	symbols+= "{|}~";
	var loc;
	loc = symbols.indexOf(obj);
	if (loc >-1) {
		Ascii_Decimal = 32 + loc;
		return (32 + loc);
    }
	return(0);  // If not in range 32-126 return ZERO
}
function abreIndex(op){
	if(op==1){
		location.href = "../index2.php";
	}else{
		location.href = "index2.php";
	}
}
///////////////////////////////////
var message="";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
document.oncontextmenu=new Function("return false")

window.defaultStatus='www.metahospitalar.com.br';
var navegador = navigator.appName;
function inicio_pagina(){
	if((navegador=="Microsoft Internet Explorer")){
		cols=(screen.width/2)-((750/2)*1.06);
		document.write('<div style="position:relative; top:0px; left:'+cols+'px;">');
	}else{
		if((navegador=="Netscape")){
			cols=(screen.width/2)-((750/2)*1.048);
			document.write('<div style="position:relative; top:0px; left:'+cols+'px;">');
		}else{
			if((navegador=="Opera")){
				cols=(screen.width/2)-((750/2)*1.048);
				document.write('<div style="position:relative; top:0px; left:'+cols+'px;">');
			}
		}
	}
}
function fim_pagina(){
	if((navegador=="Microsoft Internet Explorer") || (navegador=="Netscape") || (navegador=="Opera")){
		if(screen.width>800){
			document.write('</div>');
		}
	}
}
function emBranco(str) {
tamanho = str.length;
contador = 0;
for (x=0; x<tamanho; x++) {
	if (str.substring(x,x+1) == " ") { ++contador;}
	}
return ((contador == tamanho) || (str == ""));
}
//
function validaMail(obj){
var email = obj.value;
var BadChars = "*|,\":<>[]{}`\'';()&$#% ";
var GoodChars = "@."; 
var posarroba = email.indexOf ('@',0);
if (email.length < 6) // o email é menor que 6 caracteres
{
   alert("\"E-mail\" inválido!"); 
   obj.focus();
   return false;
}
for (var i = 0; i < email.length; i++) 
{
   if (BadChars.indexOf(email.charAt(i)) != -1) // Contém caracteres inválidos, badchars
	{
	alert("Seu \"E-mail\" contém caracteres inválidos!");
	obj.email.value = '';
	obj.focus();
	return false;
	}
}
for (var i = 0; i < GoodChars.length; i++) 
{
	if (email.indexOf(GoodChars.charAt(i)) == -1) // não tem goodchars
	{
	alert("\"E-mail\" inválido!");
	obj.focus();
	return false;
	}
	if (email.indexOf(GoodChars.charAt(i),0) == 0) // começou com goodchars (. ou @)
	{
		alert("\"E-mail\" inválido!");
		obj.focus();
		return false;
	}
	if (email.lastIndexOf(GoodChars.charAt(i)) > email.length-3) //existe menos de 2 caracteres depois do ultimo goodchar
		{
		alert("\"E-mail\" inválido! Seu e-mail não deve terminar com @.");
		obj.focus();
		return false;
		}
}
if (email.lastIndexOf('@') > email.lastIndexOf('.')) //Não tem ponto depois do arroba
	{
	alert("\"E-mail\" inválido!");
	obj.focus();
	return false;
	}
if (email.indexOf ('@.',0) != -1 || email.indexOf ('.@',0) != -1) // . e @ colados
	{
	alert("\"E-mail\" inválido!");
	obj.focus();
	return false;
	}
if (email.indexOf ('@',posarroba+1) != -1) // Contém mais de um " @ " 
	{
	alert("\"E-mail\" inválido! Contém mais de um @");
	obj.focus();
	return false;
	}
return true;
}


function so_numero(obj){
	if (emBranco(obj) || obj.length==0){
	   return false;
	}
	obj = obj.replace( " ", "");
	for(n=0; n<obj.length; n++){
		tmpnum = toAscii(obj.substr(n,1));
		// pode espaço - ( )
		if(tmpnum<48 || tmpnum>57){
		   return false;
		}
	}
   return true;
}

//valida uma valor em R$, por exemplo
function se_valor(obj){
	if (emBranco(obj) || obj.length==0){
	   return false;
	}
	for(n=0; n<obj.length; n++){
		tmpnum = toAscii(obj.substr(n,1));
		//alert(tmpnum);
		// pode espaço - ( )
		if((tmpnum<48 && tmpnum!=0 && tmpnum!=46) || tmpnum>57){
			//alert(tmpnum);
		   return false;
		}
	}
   return true;
}
function se_fone(obj){
	if (emBranco(obj) || obj.length==0){
	   return false;
	}
	for(n=0; n<obj.length; n++){
		tmpnum = toAscii(obj.substr(n,1));
		// pode espaço - ( )
		if((tmpnum<48 && tmpnum!=32 && tmpnum!=40 && tmpnum!=41 && tmpnum!=45) || tmpnum>57){
			//alert(tmpnum);
		   return false;
		}
	}
   return true;
}

