// JavaScript Document
function csCombo(){
	var C={};
	var tempo;
	var dummy_ant;
	var conta=0;
	var timer;
	C.inicia=function(){
		var s=document.getElementsByTagName('select');
		var l=s.length;
		for(var i=0;i<l;i++){
			var sel=s[i];
			agregaCombo(sel);
		}
		//window.setTimeout(observa,1000);
	}
	C.comboliza=function(sel){
		agregaCombo(document.getElementById(sel));
	}
	C.imageCombo=function(images,name,id,className,style,where){
		var sel={}, opt=[];
		for(var i=0; i<images.length; i++){
			var op={innerHTML:"<img src=\""+images[i].img+"\">", value:images[i].valor};
			if(images[i].default){
				op.selected=true;
			}
			opt.push(op);
		}
		sel={options:opt,name:name,id:id,className:className,style:style,nextSibling:where};
		agregaCombo(sel);
	}
	C.htmlCombo=function(html,name,id,className,style,where){
		var sel={}, opt=[];
		for(var i=0; i<html.length; i++){
			var op={innerHTML:html[i].html, value:html[i].valor};
			if(html[i].default){
				op.selected=true;
			}
			opt.push(op);
		}
		sel={options:opt,name:name,id:id,className:className,style:style,nextSibling:where,onchange:where.onchange};
		agregaCombo(sel);
	}
	function agregaCombo(sel){
		var op=sel.options;
		if(!sel.id)sel.id="csiCS"+conta++;
		var id=sel.id;
		var va=null;
		var vals=Array();
		for(var j=0;j<op.length;j++){
			var te=op[j].innerHTML;
			var vl=op[j].value;
			var cl=op[j].className;
			if(op[j].selected) va=vl;
			vals.push({texto:te, valor:vl, class:cl});
		}
		var hi=document.createElement('input');
		hi.type="hidden";
		hi.name=sel.name;
		hi.id="csCombo"+id;
		hi.value=va;
		var se=cCombo(id,vals,va,sel);
		se.style.cssText=sel.style.cssText;
		if(sel.className.length>0) se.classList.add(sel.className);
		sel.style.display='none';
		sel.name=sel.name+'_dummy';
		var pa=(sel.nextSibling)?sel.nextSibling.parentNode:sel.parentNode;
		pa.appendChild(hi);
		pa.insertBefore(se,sel.nextSibling);
	}
	function cCombo(id,vals,v,original){
		var combo=gD('');
		combo.classList.add('csCombo');
		combo.tabIndex=1;
		var le=gD('');
		var l=gD('Elegir');
		le.appendChild(l);
		l.dataset.id=id;
		var desp=gD('');
		desp.style.display='none';
		le.onclick=function(e){
			if(desp.style.display==='block'){
				desp.style.display='none';
				l.className='';
			}else{
				desp.style.display='block';
				l.className='sel';
			}
			tempo='';
			combo.focus();
			e.stopPropagation();
		};
		combo.onkeypress=function(e){
			var c=e.keyCode;
			tempo+=String.fromCharCode(c).toLowerCase();
			busca(desp,tempo);
		}
		combo.appendChild(le);
		for(var i in vals){
			var dummy=gD(vals[i].texto);
			dummy.dataset.valor=vals[i].valor;
			dummy.dataset.id=id;
			dummy.className=vals[i].class;
			if(v===vals[i].valor){
				l.innerHTML=vals[i].texto;
				dummy.classList.add("sel");
			}
			dummy.onclick=function(e){
				e.stopPropagation();
				var du=this;
				if(timer) clearTimeout(timer);
				timer=setTimeout(function(){
					l.innerHTML=du.innerHTML;
					l.dataset.valor=du.dataset.valor;
					desp.scrollTop=du.offsetTop;
					if(original.value!=du.dataset.valor){
						original.value=du.dataset.valor;
						document.getElementById("csCombo"+du.dataset.id).value=du.dataset.valor;
						if(original.onchange) original.onchange();
					}
					var ant=combo.getElementsByClassName("sel");
					while(ant.length>0){
						ant[0].classList.remove("sel");
					}
					du.classList.add("sel");
					window.onclick();
				},250);
			};
			if(document.getElementById(id).ondblclick){
				dummy.ondblclick=function(e){
					clearTimeout(timer);
					var original=document.getElementById(this.dataset.id);
					original.ondblclick(e,this.dataset.valor);
					window.onclick();
				};
			}
			desp.appendChild(dummy);
		}
		document.getElementById(id).cambioManual=function(){
			cambiaValor(this.value,desp,l);
		};
		document.getElementById(id).addEventListener('change',function(e){
			var du=combo.querySelectorAll('[data-valor="'+e.target.value+'"]')[0];
			l.innerHTML=du.innerHTML;
			l.dataset.valor=du.dataset.valor;
			desp.scrollTop=du.offsetTop;
			var ant=combo.getElementsByClassName("sel");
			while(ant.length>0){
				ant[0].classList.remove("sel");
			}
			du.classList.add("sel");
		});
		combo.appendChild(desp);
		if(window.onclick){
			var t=window.onclick;
		}
		window.onclick=function(e){if(t){t();} desp.style.display='none';l.className='';}
		return combo;
	}
	function gD(v){
		var d=document.createElement('div');
		d.innerHTML=v;
		return d;
	}
	function observa(){
		var observer = new MutationObserver(function(mutations,observer) {
			mutations.forEach(function(mutation) {
				for(var i=0; i<mutation.addedNodes.length; i++){
					var a=mutation.addedNodes[i];
					if(a.nodeName!=='#text'){
						var elms = a.getElementsByTagName('select');
						if(elms.length>0){
							for(var j=0; j<elms.length;j++){
								agregaCombo(elms[j]);
							}
						}
					}
				}
			});
		});
		var config = {childList: true, subtree: true};
		observer.observe(document.documentElement, config);
	}
	function busca(d,t){
		for(var i=0;i<d.childNodes.length;i++){
			//alert(d.childNodes[i].innerHTML);
			if(d.childNodes[i].innerHTML.toLowerCase().indexOf(t)==0){
				d.scrollTop=d.childNodes[i].offsetTop;
				return;
			}
		}
	}
	function cambiaValor(val,d,l){		
		for(var i=0;i<d.childNodes.length;i++){
			//alert(d.childNodes[i].innerHTML);
			if(d.childNodes[i].dataset.valor==val){
				l.innerHTML=d.childNodes[i].innerHTML;
				l.dataset.valor=val;
				return;
			}
		}
	}
	return C;
}
// JavaScript Document
function cCal(valores){
	var C={};
	var t=valores.dias;
	var ms=valores.meses;
	var ini=valores.inicia;
	var anos=valores.years;
	var inline=valores.muestra;
	var funcion=valores.funcion;
	var day=valores.dia;
	var donde=valores.donde;
	var clase=valores.clase;
	var after=valores.after;
	var fechasCal='';
	var cal=Array();
	C.muestra=function(index,id,funcion,boton){
		var d1=ini?ini:new Date();
		if(cal[index]){
			cal[index].style.display=(cal[index].style.display=='none')?'block':'none';
		}else{
			C.muestraMes(d1.getFullYear(),d1.getMonth(),index,id,funcion,boton);
		}
	};
	C.muestraMes=function(a,m,index,id,funcion,boton){
		if(cal[index]){
			if(boton){
				document.body.removeChild(cal[index]);
			}else{
				document.getElementById(id).removeChild(cal[index]);
			}
		}
		cal[index]=document.createElement('div');
		cal[index].className='csCal';
		if(clase) cal[index].classList.add(clase);
		var mes=document.createElement('div');
		mes.className='csMes';
		var selMes=comboMes(a,m,index,id,funcion,boton);
		var selAno=comboAno(a,m,index,id,funcion,boton);
		var tit=gD('');
		tit.appendChild(selAno);
		tit.appendChild(selMes);
		cal[index].appendChild(tit);
		var dummy=document.createElement('div');
		var d1=new Date(a, m, 1);
		var i=d1.getDay();
		var d2=new Date(a, parseInt(m)+1, 0);
		var o=d2.getDate();
		var n=o+i+1;
		var f=Math.ceil(n/7);
		var dact=0;
		for(var s=0;s<7;s++){
			dummy.appendChild(gD(t[s]));
		}
		mes.appendChild(dummy);
		var ps=pascua(a);
		//alert(ps);
		for(var k=0;k<f;k++){
			dummy=document.createElement('div');
			for(var j=0;j<7;j++){
				dact=k*7+j;
				if(dact>=i && dact<o+i){
					var di=gD(dia(dact-i+1));
					di.dataset.dia=a+"-"+dia(parseInt(m)+1)+"-"+dia(dact-i+1);
					di.id="TD"+di.dataset.dia;
					if(ps.indexOf(di.dataset.dia)>=0) di.className='csFeriado';
					di.dataset.idTarea=verificaHayTarea(di);
					if(di.dataset.dia==day){
						di.classList.add("sel");
					}
					di.onclick=function(){
						var ant=this.parentNode.parentNode.querySelectorAll(".sel")[0];
						if(ant){
							ant.classList.remove("sel");
						}
						this.classList.add("sel");
						if(id) document.getElementById(id).value=this.dataset.dia;
						if(funcion && funcion!='undefined') eval(funcion+"('"+this.dataset.dia+"','"+this.dataset.idTarea+"');");
						if(!inline) cal[index].style.display='none';
					};
					dummy.appendChild(di);
				}else{
					dummy.appendChild(gD(''));
				}
			}
			mes.appendChild(dummy);
		}
		cal[index].appendChild(mes);
		if(boton){
			var pos=getPos(boton);
			//cal[index].style.visibility='hidden';
			document.body.appendChild(cal[index]);
			if(pos[0]+cal[index].offsetWidth>document.body.clientWidth){
				pos[0]=pos[0]-cal[index].offsetWidth-boton.offsetWidth;
			}
			if(pos[1]+cal[index].offsetHeight>document.body.clientHeight){
				pos[1]=pos[1]-cal[index].offsetHeight-boton.offsetHeight;
			}
			cal[index].style.top=pos[1]+"px";
			cal[index].style.left=pos[0]+"px";
			//document.getElementById('loco').appendChild(cal);
		}else{
			document.getElementById(id).appendChild(cal[index]);
		}		
		if(after){
			after('csAno'+index);
			after('csMes'+index);
		}
	};
	function comboAno(a,m,index,id,funcion,boton){
		var s=document.createElement("select"),inia;
		s.dataset.index=index;
		s.dataset.id=id;
		s.dataset.m=m;
		s.dataset.funcion=funcion;
		s.onchange=function(){C.muestraMes(this.value,this.dataset.m,this.dataset.index,this.dataset.id,this.dataset.funcion,boton);};
		s.id='csAno'+index;
		if(!ini){
			var d1=new Date();
			inia=d1.getFullYear();
		}else{
			inia=ini.getFullYear();
		}
		inia++;
		if(!anos) anos=100;
		for(var i=inia;i>=inia-anos;i--){
			var o=document.createElement("option");
			if(a==i) o.selected=true;
			o.value=i;
			o.innerHTML=i.toString();
			s.appendChild(o);
		}
		s.style.width='70px';
		return s;
	}	
	function comboMes(a,m,index,id,funcion,boton){		
		var s=document.createElement("select");
		s.onchange=function(){C.muestraMes(a,this.value,index,id,funcion,boton);};
		s.id='csMes'+index;
		for(var i=0;i<ms.length;i++){
			var o=document.createElement("option");
			if(m==i) o.selected=true;
			o.value=i;
			o.innerHTML=ms[i];
			s.appendChild(o);
		}
		return s;
	}
	function pascua(ano){
		var M,N,a,b,c,d,e,f,di,mes,pascua1,pascua2;
		if(ano>1583 && ano<1699) { M=22; N=2; } 
		else if (ano>1700 && ano<1799) { M=23; N=3; } 
		else if (ano>1800 && ano<1899) { M=23; N=4; } 
		else if (ano>1900 && ano<2099) { M=24; N=5; } 
		else if (ano>2100 && ano<2199) { M=24; N=6; } 
		else if (ano>2200 && ano<2299) { M=25; N=0; } 
		a = ano % 19;
		b = ano % 4;
		c = ano % 7;
		d = ((19*a) + M) % 30;
		e = ((2*b) + (4*c) + (6*d) + N) % 7;
		f = d + e;
		if(f<10){ 
			di = f + 22;
			mes = 3;
		}else{
			di = f - 9;
			mes = 4;
		}
		if(di===26 && mes===4){ 
			di = 19;
		}
		if (di===25 && mes===4 && d===28 && e===6 && a>10){
			di = 18;
		}
		pascua1 = new Date(ano,parseInt(mes)-1,parseInt(di)-3);
		pascua2 = new Date(ano,parseInt(mes)-1,parseInt(di)-2);
		return(Array(ano+"-"+dia(pascua1.getMonth()+1)+"-"+dia(pascua1.getDate()),ano+"-"+dia(pascua2.getMonth()+1)+"-"+dia(pascua2.getDate())));
		//pascua = new Date(ano,mes-1,di-3);
		//return pascua;
	}
	function gD(v){
		var d=document.createElement('div');
		d.innerHTML=v;
		return d;
	}
	function dia(v){
		return (v<10)?"0"+v:v;
	}
	function getPos(obj){
		var xx=0, yy=0;
		if(obj.offsetParent){
			xx=obj.offsetLeft+obj.offsetWidth;
			yy=obj.offsetTop;
			while(obj=obj.offsetParent){
				xx+=obj.offsetLeft;
				yy+=obj.offsetTop;
			}
		}else{
			xx=obj.offsetLeft+obj.offsetWidth;
			yy=obj.offsetTop;
		}
		var r=new Array(xx,yy);
		return r;
	}
	function inicia(){
		if(inline){
			var f=document.getElementById(donde).dataset.fechas;
			fechasCal=JSON.parse(f);
			C.muestra(randomID(5),donde,funcion);
		}else{
			var elems=document.getElementsByClassName('csCalBoton');
			for(var i=0;i<elems.length;i++){
				elems[i].dataset.index=i;
				elems[i].onclick=function(){
					C.muestra(this.dataset.index,this.dataset.id,this.dataset.funcion,this);
				}
			}
		}
	}
	function verificaHayTarea(di){
		if(fechasCal){
			var f=di.dataset.dia,tt='',id='';
			for(var i=0; i<fechasCal.length; i++){
				if(f==fechasCal[i].fecha.substr(0,10)){
					tt+=fechasCal[i].nombre+"<br>";
					id+=fechasCal[i].id+",";
				}
			}
			if(tt.length>0 || id.length>0){				
				di.classList.add('csTarea');
				activaToolTips([{id:null,texto:tt,obj:di}]);
				return id;
			}
			return 0;
		}else{
			return 0;
		}
	}
	function randomID(l){
		var letras="QWERTYUIOPLKJHGFDSAZXCVBNMmnbvcxzlkjhgfdsaqwertyuiop",id='';
		l=(l>0)?l:8;
		for(var i=0;i<l;i++){
			var pos=Math.floor(Math.random()*letras.length);
			id+=letras.substr(pos,1);
		}
		return id;
	}
	inicia();
	return C;
}
function cInputFile(){
	var C={};
	var tempo;
	var dummy_ant;
	var conta=0;
	C.inicia=function(){
		var s=document.querySelectorAll('input[type=file]');
		for(var i=0,l=s.length;i<l;i++){
			var file=s[i];
			agregaFile(file);
		}
		//observa();
	};
	function agregaFile(file){
		if(!file.id)file.id="csiCS"+conta++;
		//var id=file.id;
		//inf.style.cssText=file.style.cssText;
		var inf=cFiles(file);
		if(file.className.length>0) inf.classList.add(file.className);
		var pa=(file.nextSibling)?file.nextSibling.parentNode:file.parentNode;
		pa.insertBefore(inf,file.nextSibling);
	}
	function cFiles(who){
		//var onc=who.onchange?who.onchange:null;
		var d1=gD('');
		var d2=gD('Elegir un archivo');
		var d3=gD('Examinar');
		d1.className='csInputFile';
		d2.className='csIvacio';
		d1.appendChild(d2);
		d1.appendChild(d3);
		d1.onclick=function(){
			who.click();
		};
		who.addEventListener('change',function(e,f){
			d2.classList.remove('csIvacio');
			d2.innerHTML=this.value.split(/(\\|\/)/g).pop();
			//if(onc) onc(e,f);
		});
		who.vacia=function(){
			this.type='text';
			this.value='';
			this.type='file';
			d2.classList.add('csIvacio');
			d2.innerHTML='Elegir un archivo';
		};
		d1.style.cssText=who.style.cssText;
		who.style.position='absolute';
		who.style.left='-10000px';
		return d1;
	}
	function gD(v){
		var d=document.createElement('div');
		d.innerHTML=v;
		return d;
	}
	return C;
}