function d(elemento) {
	//return eval("document.getElementById('"+gg+"')");
}

function switchViews(mode) {
	var formulario = document.getElementById("form_nuevoEvento");
	var citas= document.getElementById("cita_exp");
	var bt = document.getElementById("btnuevo");
	if(mode==0) {	
		citas.style.display="block";
		bt.style.display="block";
		formulario.style.display="none";
		}
	else {
		citas.style.display="none";
		bt.style.display="none";
		formulario.style.display="block";
		}
}
		  
		  
		  
		  
		  
		  
		  

function abrirCita(dia, tipo) {
	
	//alert('Abro Cita');
	switchViews(0);	
	var dd = new String(dia);
	if(dd.charAt(0)=="a") {
		dd=dd.substr(1);
	} 
	//alert('dd: '+dd);
	
	var fechaArray = dd.split("-");
	var fechaDia = (fechaArray[2].length<2)?"0"+fechaArray[2]: fechaArray[2];
	var fechaMes = (fechaArray[1].length<2)?"0"+fechaArray[1]: fechaArray[1];
	var aMeses = ['Enero','Febero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'];
	fechaEsp = fechaArray[2]+" de "+aMeses[fechaArray[1]-1]+" de "+fechaArray[0];
 	fechaSQL = fechaArray[0]+"-"+fechaMes+"-"+fechaDia;
	//alert('fechaSQL: '+fechaSQL);
	
	var res = syncGet('mod/5/ajaxCalendar.php',"fech="+fechaSQL+"&t="+tipo,'Error');	
	var cita=document.getElementById("cita");
	var dia=document.getElementById("cita_titulo");
	cita.style.visibility="visible";
	dia.innerHTML="<strong>"+fechaEsp+"</strong>";
	document.nuevacita.hDia.value=fechaSQL;
	var rArray = res.split("#");
	var citas=document.getElementById("citas");var citad=document.getElementById("cita_exp");
	citas.innerHTML="";citad.innerHTML="";
	if (res.length<1) {
		citas.innerHTML="<span>No hay eventos</span>";
	}
	else {		
		
		for(j=0;j<(rArray.length-1);j++) {
		
	
			cc = rArray[j].split("|");
			imgIcon="grup_small2.gif";
	  		if (cc[3]=='0') {
				imgIcon="priv_small2.gif";
			}
			
			Ti=cc[1].split('.-- ');
				
			//citas.innerHTML+="<div id='cita_evt"+j+"'  class='txtUltraPetit'><span class='txtUltraPetit cursor' onclick=\"asyncGet('mod/5/ajaxCalendar2.php','cid="+cc[0]+"','error','cita_exp','text');switchViews(0);\"><img src='img/0/mod/calendar/"+imgIcon+"' align='absmiddle' style='padding-right:3px;'/>"+cc[1]+" ("+cc[2]+")</span></div>";	
			citas.innerHTML+="<div id='cita_evt"+j+"' class='citalista'><span class='ico'><img src='img/0/mod/calendar/"+imgIcon+"' align='absmiddle'/></span><span class='hour'>"+Ti[0]+"</span><a class='title' href=\"javascript:void(0);\" onclick=\"asyncGet('mod/5/ajaxCalendar2.php','cid="+cc[0]+"','error','cita_exp','text');switchViews(0);\">"+Ti[1]+"</a></div>";	
		}
			
	}
}







function nuevoEvento() {
	switchViews(1);	
	for(i=0;i<(document.nuevacita.elements.length-2);i++) {
		var formElement = document.nuevacita.elements[i];
		if (formElement.type=="text" || formElement.type=="textarea") {	formElement.value=""; }
		if (formElement.name=="cld_end_date") { formElement.value=fechaSQL; }
		if (formElement.name=="cld_init_date") { formElement.value=fechaSQL; }
		if (formElement.type=="checkbox") {	formElement.checked=0; }
	}
	document.nuevacita.mod_id.value="";
	PopulateTimes("hora_inicio",8,20);
	PopulateTimes("hora_fin",8,20);
	document.nuevacita.evento.focus();
}

function modEvento(eid) {
	switchViews(1);
	var ev = syncGet('mod/5/ajaxCalendar6.php','cid='+eid,'error');
	document.nuevacita.mod_id.value=eid;
	var evArray = ev.split("|");
	var o = eval("document.nuevacita.elements");
	o[0].value=evArray[0];
	longdescr = evArray[1].split("<br>");
	o[1].value=longdescr[0];
	if(longdescr.length>1) {
		o[2].value=longdescr[1];
	}	
	longlugar = evArray[3].split("<br>");
	o[3].value=longlugar[0];
	if(longlugar.length>1) {
		o[4].value=longlugar[1];
	}
	o[5].value=evArray[4];
	o[7].value=evArray[5];
	var hi=document.getElementById("hora_inicio");
	var hf=document.getElementById("hora_fin");
	xhiv=evArray[2].substring(0,2);
	xhfv=evArray[6].substring(0,2);
	hi.options[hi.selectedIndex].text=xhiv;
	hf.options[hf.selectedIndex].text=xhfv;
	document.nuevacita.cld_end_date.value=evArray[5];
	var mi=document.getElementById("minutos_inicio");
	var mf=document.getElementById("minutos_fin");
	xmiv=evArray[2].substring(3,5);
	xmfv=evArray[6].substring(3,5);
	switch (xmiv) {
		case "00":
			mi.selectedIndex=0;
			break
		case "15":
			mi.selectedIndex=1;
			break
		case "30":
			mi.selectedIndex=2;
			break			
		case "45":
			mi.selectedIndex=3;
			break	
		default:
			mi.selectedIndex=0;
	}
	
	switch (xmfv) {
		case "00":
			mf.selectedIndex=0;
			break
		case "15":
			mf.selectedIndex=1;
			break
		case "30":
			mf.selectedIndex=2;
			break			
		case "45":
			mf.selectedIndex=3;
			break	
		default:
			mf.selectedIndex=0;
	} 

}  

function RenderCalendar(mode) {
	 //alert('curmonth: '+curmonth+' curyear: '+curyear);
	var markedDays=syncGet('mod/5/ajaxCalendar4.php','m='+curmonth+'&y='+curyear+'&t='+mode,'error');
	//alert('markedDays: '+markedDays);
	
	
	var lastDay="";
	var dies = markedDays.split("||");
	//alert('dies:'+dies);
	var exp_dies;
	
	
	
	
	for(j=0;j<(dies.length-1);j++) {
		exp_dies = dies[j].split(";");
		//alert('exp_dies:'+exp_dies);
		s = document.getElementById(exp_dies[0]);
		if (s) {
			//s.style.fontWeight="bold";
			s.className="cat3";
		}
		b=exp_dies[0].split("-");
		if(b[1]==curmonth) {
			//alert('b[1]:'+b[1]);
			bt = document.getElementById("a"+exp_dies[0]);
			dt = document.getElementById("da"+exp_dies[0]);
			//bt.style.backgroundColor="#ffffcc";
			//bt.className="cat3";
			if(mode==0) {
				dt.innerHTML="<img src='img/0/mod/calendar/priv_small.gif' class='cursor' onclick=abrirCita('a"+exp_dies[0] +"',2);>"; 
			}
			else {
				if (lastDay!=exp_dies[0])
				  dt.innerHTML+="<img src='img/0/mod/calendar/grup_small.gif' class='cursor' onclick=abrirCita('a"+exp_dies[0] +"',2);>"; 
				  lastDay=exp_dies[0];
			}
		}	
	}
	
}




// renderCalendlets: Renders the appoinments for the small calendars.
function renderCalendlets(m,y) {
	//alert('curmonth: '+m+' curyear: '+y);
	var markedDays=syncGet('mod/5/ajaxCalendar7.php','m='+m+'&y='+y,'error');
	//alert('markedDays: '+markedDays);
	var dies = markedDays.split("||");
	for(j=0;j<dies.length;j++) {
		var exp_dies = dies[j].split(";");
		s = document.getElementById(exp_dies[0]);
		//alert('s: '+s);
		if (s) {
			//s.style.fontWeight="bold";
			s.className="cat3";
		}
		//b=exp_dies[0].split("-");
	}
}






function PopulateTimes(list,start,end) {
	var lista = document.getElementById(list);
	for(i=0;i<lista.options.length;i++) {
		lista.remove(i);
	}
	for(k=0;k<=(end-start);k++) {
		hora=start+k;
		if(hora<10) { 
			lista.options[k]=new Option("0"+hora,"0"+hora);
		}
		else {
			lista.options[k]=new Option(hora,hora);
		}
	}
}

function aDay() {
	var adcheck = document.getElementById("allDay");
	if(adcheck.checked==true) {
		var hora_ini = document.getElementById("hora_inicio");
		var hora_fin = document.getElementById("hora_fin");
		hora_ini.selectedIndex=1;
		hora_fin.selectedIndex=hora_fin.options.length-1;
	}
}
function drawCalendars(m,y,p) {
	switch(p) {
		case 0:
			if(m>1) {
				document.getElementById("calend_mesAnterior").innerHTML=buildCal(m-1 ,y, hoy,"cat2","cat1",165,"anterior",0);
				}
			else {
				document.getElementById("calend_mesAnterior").innerHTML=buildCal(12 ,y-1, hoy,"cat2","cat1",165,"anterior",0);
				}
			document.getElementById("calend_mesPresente").innerHTML=buildCal(m ,y, hoy, "cat2", "cat1",165,"presente",0);
			if(m>11) {
				document.getElementById("calend_mesSiguiente").innerHTML=(buildCal(1 ,y+1, hoy, "cat2", "cat1",165,"siguiente",0));
			}
			else {
				document.getElementById("calend_mesSiguiente").innerHTML=(buildCal(m+1 , y, hoy, "cat2", "cat1",165,"siguiente",0));
			}
			//RenderCalendar();
			break;
		case 1:
			document.getElementById("calend_Actual").innerHTML = buildCal(m ,y, hoy, "cat2", "cat1",589,"actual",1);
			//RenderCalendar(2);
			break;
		case 2:
			document.getElementById("smallCalend1").innerHTML=buildCal(m ,y, hoy, "calend_celda_small", "cat1",140,"picker1",2);
			//RenderCalendar();
			break;
		}
		
}

function datePicker(fecha) {
	document.nuevacita.cld_end_date.value=fecha;
	document.getElementById("smallCalend1").style.visibility="hidden";
}




function moveCalendars(direction,tipus) {
	//alert('En moveCalendars --> curmonth: '+curmonth);
	if(direction=='back') {
		curmonth--;
		if(curmonth==0) {
			curyear--;
			curmonth=12;
		}
	} else {
		curmonth++;
		if(curmonth==13) {
			curyear++;
			curmonth=1;
			}
	}
	//alert('En moveCalendars --> curyear: '+curyear+' curmonth: '+curmonth);
	drawCalendars(curmonth,curyear,tipus);
}


 

function popupCalendar() {
	document.getElementById('smallCalend1').style.visibility='visible';
	fechaActual=fechaSQL.split("-");
	curmonth = fechaActual[1];
	curYear = fechaActual[0];
	drawCalendars(curmonth,curYear,2);
}

