var getDaysInMonth = function(month,year) { return new Date(year, month, 0).getDate() }
t= new Date()
var eem = new Array()
eem[0]="paciente"
eem[1]="idpaciente"
eem[2]="comunapac"
eem[3]="prestacion"
eem[4]="indpresta"
eem[5]="indexreg"
eem[6]="profesional"
eem[7]="reagendamiento"
eem[8]="rutprofesional"
eem[9]="informe"
eem[10]="cierre"
eem[11]="status"
counta=0
m1 = "m"+(t.getMonth()+1)
t1 = t.getDate()
//alert("m1="+m1 + " t1="+t1)
ddn=document.getElementById("d21")
rnn=document.getElementById(m1)
fa=rnn
rna=rnn
dda=ddn
yy=2024
fillmes(rnn)
//alert("t2="+t2)
ddn=document.getElementById(t2)
dda=ddn
function fillmes(rnn)
{
 //alert("dda="+dda.innerHTML+" rnn="+rnn.innerHTML)
        dda.style.color="#000000"
        dda.style.background="#e0e0f0"
	rna.style.color="#000000"
	rna.style.background="#e0e0f0"
	rnn.style.color="#ffff00"
	rnn.style.background="#0000f0"
	nn=rnn.id
	rna=rnn
	
	nn=nn.substr(1)
for (i=0;i<7;i++)
        document.getElementById("d6"+i).style.visibility= "visible"

	//document.getElementById("mes").innerHTML=rnn.selected.innerHTML
	fill(yy,nn)

	//alert("yy="+yy+" nn="+nn)
	
}
function clearmes()
{
	for(i=1;i<7;i++) for(j=0;j<7;j++) 
	{
		document.getElementById("d"+i+j).value=" "
		document.getElementById("d"+i+j).style.color="#000000"
		document.getElementById("d"+i+j).style.background="#e0e0ff"
	}
	cleardoc()
}

function fill(yy,nn)
{
clearmes()
const dn = getDaysInMonth(nn,yy)
//alert(yy+" " + nn+" "+dn)
d1 = new Date();
//alert("a"+d1 +" "+nn)
//alert("d"+nn+" "+d1)
z=1
dd=1
counta=0
d1.setYear(yy);
d1.setMonth(nn-1);
while(dd<=dn)
{
d1.setDate(dd)
 let day = d1.getDay()
	if(dd==t1) t2="d"+z+day
	aa=dd
	if(nn<10) nnx="0"+nn
	else nnx=nn
	if(dd<10) ddx="0"+dd
	else ddx=dd
	indx=yy+nnx+ddx
//alert("yy="+yy+" dd="+dd+ " z="+z+" day="+day+" nn="+nn +" indx="+indx+" iagend="+iagend[indx])
	if(iagend[indx])
	{
		document.getElementById("d"+z+day).value=aa+"*"
		//if(aa) document.getElementById("d"+z+day).style.background="#FFFF00"
	}
	else document.getElementById("d"+z+day).value=aa
	dd++
	//alert(day +" z="+z)
	if(day==0) z++
}
 let day = d1.getDay()
//alert(z+" DAY=" + day)
if(z<6 || !day) for (i=0;i<7;i++)
        document.getElementById("d6"+i).style.visibility="hidden"
}
function agen(ddn)
{
clearmes()
//alert("yy="+yy+" nn="+nn)
	fill(yy,nn)
	ddnc=ddn
	if(ddn.value!="")
	{
		dda.style.color="#000000"
		dda.style.background="#e0e0f0"
		ddn.style.color="#f0f000"
		ddn.style.background="#0000FF"
		dd=ddn.id
		dda=ddn
	}
	dds=ddn.value
	if(dds.indexOf("*")> -1) dds=dds.substr(0,dds.indexOf("*"))
	else dds=ddn.value
	if(nn <10) nd="0"+nn
	else nd=nn
	if(dds/1 <10) dn="0"+dds
	else dn=dds
	
	zz=yy+nd+dn
	zz1=yy+"-"+nd+"-"+dn
	cleardoc()
	count=iagend[zz]
	counta=count
	rqz=0
	for(ar=0;ar<count;ar++)
	{
		rzz=zz+ar
		dc=agend[rzz]
		doc=dc.split("^")
		//if(ar==0) alert(rzz+" dc="+dc + " prof="+ proff[0])
		//if(zz=="20240601") alert(rzz+" "+dc+"\n"+doc[6]+" 7="+doc[7])
		document.getElementById("tr"+ar).hidden=false
		if(ar <10) ix="0"+ar
		else ix=ar
		pr=doc[0]+doc[5]
		if(!doc[6] && doc[7]=="")
		{
			pr1=pr.substr(0,7)
			eq1=eqp[pr1]
			//if(ar==0) alert("NO CIERRE"+pr1+" eq1="+eq1)
			rqq=""
			if(eq1==undefined) eqq="XXXXXXXXXXXXXXXXXXXXXXXXXX"
			else 
			{
				rqq=eq1.substr(0,12)
				eqq=eq1.substr(13)
				//if(rqz==0) alert(rqq+" "+eqq)
				rqz=1
			}
		}
		else
		{
			 //if(ar==0) alert("CERRADO rzz="+rzz+" pr="+pr+" dc="+dc)
			eq1=doc[7]
				rqq=eq1.substr(0,12)
				eqq=eq1.substr(13)
			//if(!ar) alert(dc+ " " +pr + " " + pr1 + " " + rqq+"-"+eqq)
		}
	for (ind =0 ; ind<6; ind++)
	{
		txt=eem[ind]+ix
		//rzz=zz+ind
		//dc=agend[rzz]
		//if(!ar) alert(ind+ " " +txt + " " + doc[ind])
		elm=document.getElementById(txt)
		elm.value=doc[ind]
	}
		txt=eem[ind]+ix
		//if(!ar) alert(ind+ " 1=" +txt + " " + eqq)
		elm=document.getElementById(txt)
		elm.value=eqq
		document.getElementById(eem[8]+ix).value=rqq
                //document.getElementById(eem[9]+ix).style.font-bold
                //document.getElementById(eem[10]+ix).style.font-bold
		document.getElementById(eem[11]+ix).value=doc[8]+doc[6]
		if(doc[8])
			document.getElementById(eem[9]+ix).style.backgroundColor="#00ff00"
		if(doc[6])
			document.getElementById(eem[10]+ix).style.backgroundColor="#ff0000"
			
	}
}
function cleardoc()
{
	for(ar=0;ar<counta;ar++)
	{
		if(ar <10) ix="0"+ar
		else ix=ar
		for (ind =0 ; ind<8; ind++)
		{
			txt=eem[ind]+ix
			elm=document.getElementById(txt)
			elm.value=""
		}
                document.getElementById("tr"+ar).hidden=true
		document.getElementById(eem[ind]+ix).value=""
		document.getElementById(eem[ind+1]+ix).value=""
		document.getElementById(eem[ind+2]+ix).value=""
		document.getElementById(eem[ind+3]+ix).value=""
		document.getElementById(eem[ind+2]+ix).style.backgroundColor="#ffffff"
		document.getElementById(eem[ind+1]+ix).style.backgroundColor="#ffffff"
	}
}
//function ver(fi) { 
//if(fa.id!=rnn.id)
//{
//alert(fa.id + "  " + rnn.id)
//fa.style.color="black" 
//fa.style.background="#e0e0f0"
//}
//fi.style.color="yellow" 
//fi.style.background="blue"
//fa=fi
//}
function report(repo)
{
	r1=repo.id
        ll=r1.length-2
        in1=r1.substr(ll)
        stt=document.getElementById(eem[11]+in1).value
        if(scierre==0 &&(stt==1 || stt==21))
	{
		alert("\n\n\nStatus de Visita ya se encuentra Cerrada!!!\n\nEl informe final es desplegado y opcionalmente lo puede imprimir")
	}
        pac=document.getElementById(eem[0]+in1).value
        prt=document.getElementById(eem[4]+in1).value
        prr=document.getElementById(eem[8]+in1).value
        idx=document.getElementById(eem[5]+in1).value
	grep=document.getElementById(eem[9]+in1)
	rrep=document.getElementById(eem[11]+in1)
	idx=idx.substr(0,2)
	if(pac=="")
        {
                document.getElementById(eem[7]+in1).value=""
                return
        }
	vari="https://eqt.inhedo.com/cgi-bin/report.cgi?"+prr+ "+" +pac +"+" + zz1+"+"+prt+"+"+idx+"+"+stt+"+"+repo.id
	if(stt==2 || !stt)
	if(confirm(" Procede a elaborar Reporte de Visita") == false) return
	//parm="width=1,height=1,top=0,left=0,menubar=no,titlebar=no,scrollbars=no,resizable=no,mimimizable=no,dialog=no"
	parm=""
	var obj_calwindow = window.open(vari,"","")
	if(stt=="") { stt=2;agend[zz+in1/1]=agend[zz+in1/1]+2}
	//grep.style.backgroundColor="#00ff00"
	//if(scierre)
	//{
		//ENVIA CORREO
		//stt=21
		//vari="http://ihd.homelinux.com/cgi-bin/repo.cgi?"+prr+ "+" +pac +"+" + zz1+"+"+prt+"+"+idx+"+"+stt
		//var obj_calwindow = window.open(vari,"",parm)
	//}
	return
}

function reag(reagen) 
{
	r1=reagen.id
	fe=reagen.value
	fc=fe.substr(0,4)+fe.substr(5,2)+fe.substr(8,2)
	ll=r1.length-2
	in1=r1.substr(ll)
	pac=document.getElementById(eem[0]+in1)
	paci=document.getElementById(eem[1]+in1).value
	prof=document.getElementById(eem[8]+in1).value
//	alert("r1="+r1+" fe="+fe+" in1="+in1 )
	if(pac.value=="")
	{
		document.getElementById(eem[7]+in1).value=""
		return
	}
	
	idx=document.getElementById(eem[5]+in1).value
	stt=document.getElementById(eem[11]+in1).value
	//alert ("Reagenda r1="+ r1 +" fe=" + fe+" fc="+fc+" in1="+ in1/1+" pac="+pac.value+" idx="+idx+" zz="+zz+" count="+count+" iag.fc="+iagend[fc])
	if(stt)
	{
		alert("No se permite Reagendar ...\n\n\nInforme Vigente o Visita  Cerrada!!!")
		document.getElementById(eem[7]+in1).value=""
		return
	}
	if(zz>fc) {
                alert("Nueva fecha debe ser mayor a fecha actual")
                document.getElementById(eem[7]+in1).value=""
                return
        }
	if(!confirm("Reagenda Paciente: "+ paci +"\n\nDesde: "+zz.substr(0,4)+"-"+zz.substr(4,2)+"-"+zz.substr(6)+"\n\nAl: "+fe + "\n\n            Confirma Reagendamiento??")) {
	alert("Anula Reagendamiento!!!")
	document.getElementById(eem[7]+in1).value=""
	return
	}
//	alert("eem0="+eem[0])
	if(zz<fc)
	{
	zi=zz.substr(0,4)+"-"+zz.substr(4,2)+"-"+zz.substr(6,2)
var yi = new Date(zi).getTime()
var ff = new Date(fe).getTime()
var diff = (ff - yi)/(1000*60*60*24)
		
		agd=agend[zz+(in1/1)]
		ind=agd.search(idx)
		ix=idx.substr(2,2)/1+diff/1
		if(ix<10) ix="0"+ix
		nidx=idx.substr(0,2)+ix
		nagd=agd.substr(0,ind)+nidx+agd.substr(ind+4)
		
		//alert(" reagen "+agend[zz+(in1/1)]+" idx="+idx+" dif="+diff+" nagd="+nagd)
		agend[fc+iagend[fc]]=nagd
		iagend[fc]++
		iagend[zz]--
		//alert(agend[zz+in1/1]+ " " + agend[zz+iagend[zz]])
		agend[zz+in1/1]=agend[zz+iagend[zz]]
		//insert(in1)
	//	alert("idx="+idx+" in1="+in1+" ddnc="+ddnc.id)
		pac=document.getElementById(eem[0]+in1)
		prf=document.getElementById(eem[8]+in1)
//		alert(" eem0="+eem[0]+" pac="+pac.value+" prf="+prf.value)
		
		fld=pac.value +"+"+zz+"+"+fc+"+"+idx+"+"+prf.value
		vari="https://eqt.inhedo.com/cgi-bin/reag.cgi?"+fld
//		alert("vari="+vari)
		//parm="width=1,height=1,top=0,left=0,menubar=no,titlebar=no,scrollbars=no,resizable=no,mimimizable=no,dialog=no"
		var obj_calwindow = window.open(vari,"",parm)
		document.getElementById(eem[0]+in1).value=""
		cleardoc()
		agen(ddnc)
		//for(ar=0;ar<iagend[zz];ar++)
		//{
			//if(ar<10) ix="0"+ar
			//else ix=ar
                        //if(document.getElementById(eem[11]+ix).value=="1")
                        //document.getElementById(eem[10]+ix).style.backgroundColor="#ff0000"
                        //else document.getElementById(eem[10]+ix).style.backgroundColor="#ffffff"
		//}
	}
}

function visit(vis) 
{
	alert ("Confirma Visita " + vis.id)
}


function cier(cier) 
{
	r1=cier.id
        ll=r1.length-2
        in1=r1.substr(ll)
	ll=in1/1
        cr=document.getElementById(eem[11]+in1).value
	if(!cr)
        {
        alert("No ha generado reporte de la visita!!!")
        return
        }
	if(cr==21||cr==1 )
	{
	alert("La visita ya se encuentra Cerrada!!!")
	return
	}
	
        pac=document.getElementById(eem[0]+in1).value
        paci=document.getElementById(eem[1]+in1).value
        prof=document.getElementById(eem[8]+in1).value
        prt=document.getElementById(eem[4]+in1).value
        idx=document.getElementById(eem[5]+in1).value
        idpr=document.getElementById(eem[6]+in1).value
	fpr=zz.substr(6)+"/"+zz.substr(4,2)+"/"+zz.substr(0,4)
	if(!confirm("  Cierre de Visita\n\n      El último reporte emitido se considera como informe final!!\n\n     Paciente: "+ paci +"\n\n     Fecha: "+fpr+ "\n\n                      Confirma Cierre??")) {
	alert("Anula Cierre de Visita!!!")
	return
	}
	fin="01/"+zz.substr(4,2)+"/"+zz.substr(0,4)
	vari1="https://eqt.inhedo.com/cgi-bin/cierre.cgi?"+pac+"+"+fin+"+"+idx+"+"+prof+"+"+fpr+"+"+idpr
	
	document.getElementById(eem[11]+in1).value="21"
	//alert("VARI="+vari)
	document.getElementById(eem[10]+in1).style.backgroundColor="#ff0000"
	
	//alert("a cerrar "+agend[zz+ll])
	dxx=agend[zz+ll]
	dx1=dxx.indexOf("^^")
	dxx=dxx.substr(0,dx1)+"^1"+dxx.substr(dx1+1)+1
	parm="width=1,height=1,top=0,left=0,menubar=no,titlebar=no,scrollbars=no,resizable=no,mimimizable=no,dialog=no"
	var obj_calwindow = window.open(vari1,"",parm)
	//alert(agend[zz+ll]+"\r"+dxx)
	agend[zz+ll]=dxx
	repo=document.getElementById(eem[9]+in1)
	scierre=1
                //ENVIA CORREO
	stt=21
	//vari="http://ihd.homelinux.com/cgi-bin/repo.cgi?"+prof+ "+" +pac +"+" + zz1+"+"+prt+"+"+idx.substr(0,2)+"+"+stt
	vari="http://eqt.inhedo.com/cgi-bin/mail.cgi?"+prof+ "+" +pac +"+" + zz1+"+"+prt+"+"+idx.substr(0,2)+"+"+stt
	var obj_calwindow = window.open(vari,"",parm)

	//report(repo)
	//scierre=0
}
ti1= new Date().getTime()
function timeclick()
{
	timevar="click"
ti2=ti1
ti1= new Date().getTime()
//document.getElementById("d11").innerHTML = timevar+"<br>"+(ti1-ti2)/(10000)
return
}
function timefocus()
{
	ti2=ti1
	timevar="focus"
ti1= new Date().getTime()
//document.getElementById("d11").innerHTML = timevar+"<br>"+(ti1-ti2)/(10000)
return
}
function timeblur()
{
	ti2=ti1
	timevar="blur"
ti1= new Date().getTime()
//document.getElementById("d11").innerHTML = timevar+"<br>"+(ti1-ti2)/(10000)
return
}
scierre=0
tim=0
setInterval(function() {
if(tim++ >20) fclose()}, 60000)
function fclose() {if(!confirm("Este formulario cerrará por inactividad")) tim=0
else  window.location.href = "../index.html"
 }
