
var pega;
//##Funcao q faz mascaramente d acordo com a sua necessidade#######
//Passagem d valor-  OnKeyPress="formatar(this, '##/##/###')"
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);
  }
}
//Fim#############################################################

//#Funcao mascara de telefone#####################################
//Passagem de Valor - onclick="javascript:getIt(this)"
//Funcionou melhor passando onfocus
function ValidatePhone(){
p=p1.value
if(p.length==2){
	//d10=p.indexOf('(')
	pp=p;
	d4=p.indexOf('(')
	d5=p.indexOf(')')
	if(d4==-1){
		pp="("+pp;
	}
	if(d5==-1){
		pp=pp+")";
	}
	//pp="("+pp+")";
	p1.value="";
	p1.value=pp;
}
if(p.length>2){
	d1=p.indexOf('(')
	d2=p.indexOf(')')
	if (d2==-1){
		l30=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>6){
	p11=p.substring(d1+1,d2);
	if(p11.length>4){
	p12=p11;
	l12=p12.length;
	l15=p.length;
	//l12=l12-3
	p13=p11.substring(0,4);
	p14=p11.substring(4,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;
	}
}
//}
setTimeout(ValidatePhone,100)
}
function getIt(m){
n=m.name;

n=m.name;
//p1=m
//p1=document.forms[0].elements[n]
p1=m;
//alert(p1.value);

ValidatePhone(n,p1);
}
function testphone(obj1){
p=obj1.value
//alert(p)
p=p.replace("(","")
p=p.replace(")","")
p=p.replace("-","")
p=p.replace("-","")
//alert(isNaN(p))
if (isNaN(p)==true){
alert("Check phone");
return false;
}
}
//#Fim#############################################################

//#Troca TAB por ENTRER############################################
//onKeyDown="if(event.keyCode==13){document.cadPaciente.idade.focus();}"
//#Fim#############################################################

//#Calcula Diferenca Entre os dias########################################
function Dia(Data_DDMMYYYY)
{
string_data = Data_DDMMYYYY.toString();
posicao_barra = string_data.indexOf("/");
if (posicao_barra!= -1)
{
dia = string_data.substring(0,posicao_barra);
return dia;
}
else
{
return false;
}
}

function Mes(Data_DDMMYYYY)
{
string_data = Data_DDMMYYYY.toString();
posicao_barra = string_data.indexOf("/");
if (posicao_barra!= -1)
{
dia = string_data.substring(0,posicao_barra);
string_mes = string_data.substring(posicao_barra+1,string_data.length);
posicao_barra = string_mes.indexOf("/");
if (posicao_barra!= -1)
{
mes = string_mes.substring(0,posicao_barra);
mes = Math.floor(mes);
return mes;
}
else
{
return false;
}

}
else
{
return false;
}
}

function Ano(Data_DDMMYYYY)
{
string_data = Data_DDMMYYYY.toString();
posicao_barra = string_data.indexOf("/");
if (posicao_barra!= -1)
{
dia = string_data.substring(0,posicao_barra);
string_mes = string_data.substring(posicao_barra+1,string_data.length);
posicao_barra = string_mes.indexOf("/");
if (posicao_barra!= -1)
{
mes = string_mes.substring(0,posicao_barra);
mes = Math.floor(mes);
ano = string_mes.substring(posicao_barra+1,string_mes.length);
return ano;
}
else
{
return false;
}

}
else
{
return false;
}
}

function Calcula_Data(data_DDMMYYYY,dias,adicao){

Var_Dia=Dia(data_DDMMYYYY);
Var_Mes=Mes(data_DDMMYYYY);
Var_Mes=Math.floor(Var_Mes)-1;
Var_Ano=Ano(data_DDMMYYYY);

var data = new Date(Var_Ano,Var_Mes,Var_Dia);

if (adicao == true)
{
operacao = '+'
var diferenca = data.getTime() + (dias * 1000 * 60 * 60 * 24);
}
else
{
operacao = '-'
var diferenca = data.getTime() - (dias * 1000 * 60 * 60 * 24);
}
var diferenca = new Date(diferenca);
alert(string_data+operacao+dias+' dias = '+diferenca.getDate()+'/'+(parseInt(diferenca.getMonth())+1)+'/'+diferenca.getYear());

}

function Calcula_Dias(data1_DDMMYYYY,data2_DDMMYYYY){

Var_Dia1=Dia(data1_DDMMYYYY);
Var_Mes1=Mes(data1_DDMMYYYY);
Var_Mes1=Math.floor(Var_Mes1)-1;
Var_Ano1=Ano(data1_DDMMYYYY);
var data1 = new Date(Var_Ano1,Var_Mes1,Var_Dia1);

Var_Dia2=Dia(data2_DDMMYYYY);
Var_Mes2=Mes(data2_DDMMYYYY);
Var_Mes2=Math.floor(Var_Mes2)-1;
Var_Ano2=Ano(data2_DDMMYYYY);
var data2 = new Date(Var_Ano2,Var_Mes2,Var_Dia2);

var diferenca = data1.getTime() - data2.getTime();
var diferenca = Math.floor(diferenca / (1000 * 60 * 60 * 24));
var diferenca = (diferenca/365);
alert('Diferença em dias entre '+data1_DDMMYYYY+' e '+data2_DDMMYYYY+' = '+diferenca);
}
//#Fim#######################################################################

//#Script q verifica check###############################################
//Parametro passado - onclick="checkONOFF('id< echo $x;?>');"
function checkONOFF(id)
	{
		//var id='id3';
		if(document.getElementById(id).checked==false)
			{document.getElementById(id).checked=true;}
		else
			{document.getElementById(id).checked=false;}
	}
//#Fim#######################################################################

//#Script q mudar cor da linha###############################################
//Paramentro passado - onMouseOver="mudaCorLinha('onMouseOver','TRid<php echo $x;?>')" onMouseOut="mudaCorLinha('onMouseOut','TRid<php echo $x;?>')"
function mudaCorLinha(metodo,id)
	{
		if(metodo=='onMouseOver')
			{
				document.getElementById(id).className='up_linha';
			}
		if(metodo=='onMouseOverDestaque')
			{
				document.getElementById(id).className='up_linhaDestaque';
			}

		if(metodo=='onMouseOut')
			{
				var ck = id.substring(2,10);
				//alert(ck);
				if(document.getElementById(ck).checked==false)
					document.getElementById(id).className='textoTabela';
				if(document.getElementById(ck).checked==true)
					document.getElementById(id).className='up_linha';
			}
		if(metodo=='onMouseOutDestaque')
			{
				var ck = id.substring(2,10);
				//alert(ck);
				if(document.getElementById(ck).checked==false)
					document.getElementById(id).className='marcaDestaque';
				if(document.getElementById(ck).checked==true)
					document.getElementById(id).className='up_linhaDestaque';
			}


	}


//Fim##########################################################################

//#Passa Parametro#############################################################
function localizaString(pega,keyVal,site)
{
		pega = document.getElementById('localizar').value=pega;
		//document.getElementById('localizar').focus();

		if((keyVal!='32')&&(keyVal!='36'))
		{
			tudo = window.location=site+pega;
			return tudo;
		}
		if(keyVal=='36')
			document.getElementById('localizar').select();
}

function filter (phrase, _id, cellNr){
 var suche = phrase.value.toLowerCase();
 var table = document.getElementById(_id);
 var ele;
 for (var r = 0; r < table.rows.length; r++){
  ele = table.rows[r].cells[cellNr].innerHTML.replace(/<[^>]+>/g,"");
  if (ele.toLowerCase().indexOf(suche)>=0 )
   table.rows[r].style.display = '';
  else table.rows[r].style.display = 'none';
 }

}
//#Fim#########################################################################

//#Passa Parametro#############################################################

function conta_itens_selecionados(formulario, checkbox_id)
{
      var _countChecked = 0;
      var aux = 0;

      for(i=0;i<document[formulario][checkbox_id].length;i++)
      {
         if(document[formulario][checkbox_id][i].checked==true)
         {
            _countChecked++;
            aux = 1;
         }
      }
      if(aux == 0) 
      {
         if(document[formulario][checkbox_id].checked==true)
	 {
            _countChecked++;
         }
      }

      return _countChecked;
}

function valor_item_selecionado(formulario, checkbox_id)
{
   for(i=0;i<document[formulario][checkbox_id].length;i++)
   {
      if(document[formulario][checkbox_id][i].checked==true)
      {
         return document[formulario][checkbox_id][i].value;
      }
   }
   if(document[formulario][checkbox_id].checked==true)
   {
      return document[formulario][checkbox_id].value;
   }
}



function envia_formulario(formulario, checkbox_id, destino, acao)
{
   var totalChecked = conta_itens_selecionados(formulario, checkbox_id);
   if(acao == "editar" || acao == "visualizar" )
   {
      if(totalChecked == 0)
      {
         alert('Selecione uma linha.');
      }
      else
      {
         if(totalChecked > 1)
         {
            alert('Só é possível editar uma ocorrência por vez.');
         }
         else
         {
            if(acao == "editar")
            {
               document[formulario].action=destino;
               document[formulario].submit();
            }
            else
            {
               var id = valor_item_selecionado(formulario, checkbox_id);
               URL = destino+'&id='+id;
               resolucao = '800x600';
      	       window.open(URL,resolucao,'toolbar=no,status=yes,location=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,width=550,height=450,left=320,top=180');
            }
         }
      }
   }
   else
   {
      if(acao == "imprimir")
      {
           resolucao = '800x600';
           window.open(destino,resolucao,'toolbar=no,status=yes,location=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,width=700,height=600,left=320,top=70');
      }
      else
      {
         if(totalChecked == 0)
         {
            alert('Selecione uma linha.');
         }
         else
         {
            var agree=confirm("Voce tem certeza?");
            if(agree)
            {
               document[formulario].action=destino;
               document[formulario].submit();
            }
         }
      }
   }
}

function montaEndereco(end, prm, str)
{
   if(prm==0)
   {
      resultado = new String(end+'&prox_ender='+str);
      return resultado;
   }
   else
   {
       var pos=0;
       pos = str.indexOf("=");
       if(pos != -1)
       {
           return (end+"&param"+prm+"="+str.substring(0, pos)+"&val"+prm+"="+str.substring(pos+1,str.length));
       }
       else
       {
          return end;
       }
   }
}


function abandona(session,endereco_proximo)
{
	//alert(session);
	//alert(endereco_proximo);

        endereco = new String("");
        string_end = endereco_proximo.toString();
        var posf=0;
        var param=0
        var sair=0;

        while (sair != 1)
        {
            posf = string_end.indexOf("&");
            if(posf == -1)
            {
               posf = string_end.length;
               sair = 1;
            }
            endereco=montaEndereco(endereco,param, string_end.substring(0,posf));
            param++;
            string_end = string_end.substring(posf+1,string_end.length);
        }

        window.location.href='./cancelar.php?session='+session+endereco;

}

function montaEndereco(end, prm, str)
{
   if(prm==0)
   {
      resultado = new String(end+'&prox_ender='+str);
      return resultado;
   }
   else
   {
       var pos=0;
       pos = str.indexOf("=");
       if(pos != -1)
       {
           return (end+"&param"+prm+"="+str.substring(0, pos)+"&val"+prm+"="+str.substring(pos+1,str.length));
       }
       else
       {
          return end;
       }
   }
}

function mudaTamanho(img, max)
{
   var largura = img.width;
   var altura = img.height;

   if (largura >= altura)
   {
      maior = largura;
      menor = altura;
      media = maior / max;
      valor_largura = maior / media;
      valor_altura = menor / media;
   }
   else
   {
      maior = altura;
      menor = largura;
      media = maior / max;
      valor_largura = menor / media;
      valor_altura = maior / media;
   }

   img.width = valor_largura;
   img.height = valor_altura;
}

function mudaTamanhoMax(img, max)
{
   var largura = img.width;
   if (img.width > max)
   {
       mudaTamanho(img, max);
   }
}

