/* INPUT SELECTOR */
function modificar_input_selector(evt) {

	var _IE_ = navigator.appName.toLowerCase().indexOf("microsoft") > -1;
	var _MOZILLA_ = navigator.appName.toLowerCase().indexOf("netscape") > -1;

	if (document.getElementsByTagName) {
		// Obtengo los elementos INPUT
		var objs = document.getElementsByTagName("INPUT");
		
		// Los modifico para incluir el selector a los que tenga el atributo rel = "selector"
		for (var i=0; i<objs.length; i++) {
			if (objs[i].getAttribute("rel") != null && 
				objs[i].getAttribute("rel").toLowerCase().match(/^selector(\[\d+\s*,\s*\d+\])?$/) &&
			    objs[i].type.toLowerCase() == "text") {
				// Creamos id si no lo tiene
				if (!objs[i].id) {
					objs[i].id = Math.random();
				}
				
				// Le añadimos al objeto input el evento onchange
				objs[i].onchange = function() {
					if (!isNaN(parseInt(this.value))) {
						if (parseInt(this.value) < this.min) {
							this.value = this.min;
						} else if (parseInt(this.value) > this.max) {
							this.value = this.max;
						}
					} else {
						this.value = "0";
					}
					
					this.barra.firstChild.style.width = this.barra.incremento*parseInt(this.value)+"px";
				};
				
				// Hayamos el máximo y el mínimo
				var params = objs[i].getAttribute("rel").replace(/(^selector\[)|(\]$)/g, "").split(",");
				var min = params[0];
				var max = params[1];
				objs[i].max = max;
				objs[i].min = min;
				// Lo incluimos dentro de una capa
				var contenedor = document.createElement("DIV");
				contenedor.style.className = "contenedor-input-selector";
				
				// Creamos las imagenes-enlaces y las incluimos
				// Barra (lo insertamos a lo ultimo)
				var barra = document.createElement("DIV");

				// Aumentar
				var a = document.createElement("A");
				a.id = objs[i].id+"_AUMENTAR";
				a.href="#";
				a.cajaTexto = objs[i];
				a.max = max;
				a.min = min;
				a.barra = barra;
				a.onmousedown = function() {
					if (!isNaN(parseInt(this.cajaTexto.value))) {
						if (parseInt(this.cajaTexto.value) < this.max) {
							if (parseInt(this.cajaTexto.value) < this.min) {
								this.cajaTexto.value = this.min;
							} else {
								this.cajaTexto.value = parseInt(this.cajaTexto.value)+1;
							}
						} else {
							this.cajaTexto.value = this.max;
						}
					} else {
						this.cajaTexto.value = "0";
					}
					// Modificamos el progreso
					var progreso = this.barra.firstChild;
					progreso.style.width = this.barra.incremento*parseInt(this.cajaTexto.value)+"px";
					this.idEvento = setTimeout('document.getElementById("'+this.id+'").onmousedown()', 200);
				};
				a.onmouseup = function() {
					clearTimeout(this.idEvento);
				}
				a.innerHTML = "&nbsp;";
				a.className="aumentar-input-selector";
				contenedor.appendChild(a);
				
				// Disminuir
				a = document.createElement("A");
				a.id = objs[i].id+"_DISMINUIR";
				a.href="#";
				a.cajaTexto = objs[i];
				a.max = max;
				a.min = min;
				a.barra = barra;
				a.onmousedown = function(evt) {
					if (!isNaN(parseInt(this.cajaTexto.value))) {
						if (parseInt(this.cajaTexto.value) > this.min) {
							if (parseInt(this.cajaTexto.value) > this.max) {
								this.cajaTexto.value = this.max;
							} else {
								this.cajaTexto.value = parseInt(this.cajaTexto.value)-1;
							}
						} else {
							this.cajaTexto.value = this.min;
						}
					} else {
						this.cajaTexto.value = "0";
					}
					// Modificamos el progreso
					var progreso = this.barra.firstChild;
					progreso.style.width = this.barra.incremento*parseInt(this.cajaTexto.value)+"px";
					this.idEvento = setTimeout('document.getElementById("'+this.id+'").onmousedown()', 200);
				};
				a.onmouseup = function() {
					clearTimeout(this.idEvento);
				}
				a.innerHTML = "&nbsp;";
				a.className="disminuir-input-selector";
				contenedor.appendChild(a);

				// Continuamos con la barra
				barra.className="barra-input-selector";
				var progreso = document.createElement("DIV");
				progreso.className="progreso-input-selector";
				progreso.innerHTML = "&nbsp;"; // IE necesita esto para que tenga el tamaño correcto
				progreso.style.width = "0px"
				barra.max = max;
				barra.min = min;
				barra.cajaTexto = objs[i];
				barra.appendChild(progreso);
				if (objs[i].style.width) {
					barra.style.width = (parseInt(objs[i].style.width.replace("px", ""))+(_IE_?13:10))+"px";
				} else {
					barra.style.width = "32px";
				}
				barra.style.marginLeft =  "-"+(parseInt(objs[i].style.width.replace("px", ""))+(_IE_?15:13))+"px";
				barra.incremento = parseInt(barra.style.width.replace("px", ""))/(max);
				barra.drag = false;
				barra.onclick = function(e) {
					this.drag = false;
					if (!e) {
						e = window.event;
					}
					var x = (_IE_?e.offsetX:e.layerX);
					pos = (x<3)? 0: parseInt(x*this.max/this.style.width.replace("px", ""))+1;
					this.firstChild.style.width = (pos*this.incremento)+"px";
					this.cajaTexto.value = pos;
				}
				
				barra.onmousemove = function(e) {
					if (this.drag) {
						if (!e) {
							e = window.event;
						}
						var x = (_IE_?e.offsetX:e.layerX);
						pos = (x<3)? 0: parseInt(x*this.max/this.style.width.replace("px", ""))+1;
						if (pos <= this.max) {
							this.firstChild.style.width = (pos*this.incremento)+"px";
							this.cajaTexto.value = pos;
						}
					}
				}
				
				barra.onmousedown = function(e) {
					this.drag = true;
				}
				
				barra.onmouseup = function(e) {
					this.drag = false;
				}
				
				contenedor.appendChild(barra);
				objs[i].barra = barra;
				
				objs[i].parentNode.insertBefore(contenedor, objs[i].nextSibling);
			}
		}
	}
}


/* CALENDARIO */

function muestraCalendario(obj) {
	if (!obj.calendario || !obj.calendario.activo) {
		var contenedor = document.createElement("DIV");
		contenedor.style.cssFloat = contenedor.style.styleFloat = "left";
		contenedor.activo = true;
	
		var calendario = document.createElement("DIV");
		calendario.id = Math.random();
		calendario.enlace = obj;
		calendario.className = "contenedor-calendario";
	
		var fecha = document.getElementById(obj.id+"fecha").innerHTML.split("/");
		calendario.innerHTML = creaCalendario(new Date(fecha[2], fecha[1]-1, fecha[0]), new Date(fecha[2], fecha[1]-1, fecha[0]), calendario.id);
		contenedor.appendChild(calendario);
		
		obj.parentNode.insertBefore(contenedor, obj.nextSibling);
		obj.calendario = contenedor;
	} else {
		obj.calendario.activo = false;
		obj.calendario.removeChild(obj.calendario.firstChild);
	}
}

function creaCalendario(_fecha, fechaActual, id) {
	var fecha = _fecha;
	var diasMeses = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var meses = new Array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
	
	if (fecha == null) {
	  fecha = new Date();
	}
	fecha = new Date(fecha.getFullYear(), fecha.getMonth(), 1);

	var anyo = fecha.getFullYear();
	// Si es bisiesto
	if (((anyo % 4 == 0) && (anyo % 100 != 0)) || (anyo % 400 == 0)) {
		diasMeses[1]++;
	}
	
	var res = "";
	
	mes1 = (fecha.getMonth()-1)%12;
	anyo1 = (mes1 > fecha.getMonth())? fecha.getFullYear()-1:fecha.getFullYear();
	
	mes2 = (fecha.getMonth()+1)%12;
	anyo2 = (mes2 < fecha.getMonth())? fecha.getFullYear()+1:fecha.getFullYear();
	
	res += '<table class="calendario">';
	res += '<tr><th><a href="#" onclick="modificaCalendario(new Date('+anyo1+', '+mes1+', 1), new Date('+fechaActual.getFullYear()+', '+fechaActual.getMonth()+', '+fechaActual.getDate()+'), '+id+')">&lt;</a></th>';
	res += '<th colspan="5">'+meses[fecha.getMonth()]+" "+fecha.getFullYear()+'</th>';
	res += '<th><a href="#" onclick="modificaCalendario(new Date('+anyo2+', '+mes2+', 1), new Date('+fechaActual.getFullYear()+', '+fechaActual.getMonth()+', '+fechaActual.getDate()+'), '+id+')">&gt;</a></th></tr>';
	res += '<tr><td class="cab">L</td><td class="cab">M</td><td class="cab">X</td><td class="cab">J</td><td class="cab">V</td><td class="cab">S</td><td class="cab">D</td></tr>';
	res += "  <tr>";
	var cont=0;
	var n = (fecha.getDay() == 0)? 6 : ((fecha.getDay()-1)%7);
	
	if (n != 0) {
		for (var i=0; i<n; i++) {
			res += "    <td>&nbsp;</td>";
			cont = (cont+1)%7;
		}		
	}
	
	var hoy = new Date();
	var actual = fecha;
	
	for (var i=1; i<=diasMeses[fecha.getMonth()]; i++) {
		actual.setDate(i);
		if (actual.getTime() < hoy.getTime()-1000*60*60*24) {
			res += '    <td class="inactivo">'+i+'</td>';
		} else if (fecha.getFullYear() == fechaActual.getFullYear() &&
		    fecha.getMonth() == fechaActual.getMonth() &&
			i == fechaActual.getDate() ) {
			res += '    <td class="sel"><a href="#" onclick="mostrarFecha(new Date('+fecha.getFullYear()+','+fecha.getMonth()+','+i+'), \''+id+'\')">'+i+'</td>';
		} else {
			res += '    <td><a href="#" onclick="mostrarFecha(new Date('+fecha.getFullYear()+','+fecha.getMonth()+','+i+'), \''+id+'\')">'+i+'</td>';
		}
		if (cont == 6) {
			res += "  </tr>";
			res += "  <tr>";
		}
		cont = (cont+1)%7;
	}		
	if (cont != 0) {
		for (var i=cont; i<7; i++) {
			res += "    <td class=amarillo>&nbsp;</td>";
		}		
	}
	res += "  </tr>";
	res += "</table>";

	return res;

}


function modificaCalendario(fecha1, fecha2, id) {
	/* INCREIBLE EL IE ME OBLIGA A ESCRIBIR ESTO */
	document.getElementById(id).removeChild(document.getElementById(id).firstChild);
	var capa = document.createElement("DIV");
	capa.innerHTML = creaCalendario(fecha1, fecha2, id);
	/* FIN DE MI ODIO A IE */
	document.getElementById(id).appendChild(capa); 
}

function mostrarFecha(fecha, id) {
	document.getElementById(document.getElementById(id).enlace.id+"fecha").innerHTML = formatoFecha(fecha);
	muestraCalendario(document.getElementById(id).enlace);
}

function formatoFecha(fecha) {
	return fecha.getDate()+"/"+(fecha.getMonth()+1)+"/"+fecha.getFullYear();
}