var idContenedor = "miCalendario" //id del contenedor donde se insertará el calendario
var fSalidaNombreMes = false //true escribe el mes por su nombre; false por su número
var fMesAbreviado = true // abrevia el nombre del mes a sus 3 primeras letras
var separadorFecha = "-" //separador para la fecha de salida
var celda = 16 //anchura en pixels para cada cuadro del calendario
var borde = 1 //anchura en pixels para los bordes 
var colorBorde = "#000000" //color de los bordes

var hoy = new Date()
var mes = hoy.getMonth()
var dia = 1
var marcado = false
var marcaCelda =""
var mesinicio = 0
var mesfin = 0
var fechainicio = 0
var fechafin = 0
var diafinal = 0
var numerodias = 0
var anio = hoy.getFullYear()
var diasSemana = new Array ('L','M','M','J','V','S','D')
var meses = new Array('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre')
var tunIex=navigator.appName=="Microsoft Internet Explorer"?true:false;
if(tunIex && navigator.userAgent.indexOf('Opera')>=0){tunIex = false}
tunOp = navigator.userAgent.indexOf('Opera')>=0 ? true: false;
var tunSel = false
var tunSel2 = false



function calculaDias(){
numerodias = 0
for(m=0;m<=11;m++)
	{	
	for(d=1;d<=60;d++)
		{
			if(m > mesinicio-1 && m < mesfin-1)
			{
			fecha2 = new Date(anio,m,d)
			if(fecha2.getMonth() == m){
			numerodias = numerodias + 1}
			}else if(mesinicio-1 == m && mesfin-1 == m)
			{
				if(fechainicio <= d && d <= fechafin){
				fecha2 = new Date(anio,m,d)
				if(fecha2.getMonth() == m){
					numerodias = numerodias + 1
					}
				}
			}else if(mesinicio-1 == m)
			{
				if(fechainicio <= d){
				fecha2 = new Date(anio,m,d)
				if(fecha2.getMonth() == m){
					numerodias = numerodias + 1
					}
				}

			
			}else if(mesfin-1 == m)
			{
				if(d <= fechafin){
				fecha2 = new Date(anio,m,d)
				if(fecha2.getMonth() == m){
					numerodias = numerodias + 1
					}
				}

			}
		}

tuDias.value = numerodias - 1
tuCoste.value = tuDias.value*tuPrecioDia.value
}
}

function tunCalendario(){
dia2 = dia
var anCa = celda * 7
anCa += borde * 6 
if(tunIex || tunOp){anCa +=2}

tab = document.createElement('div')
tab.id = 'calendario'

tab.style.width = anCa + "px"
tab.style.padding = "1px"
tab.style.backgroundColor = colorBorde
document.getElementById(idContenedor).appendChild(tab)


fi = document.createElement('div')
fi.className = 'fila'
fi.style.clear = "both"
tab.appendChild(fi)
fi.style.height = celda + "px"
fi.style.marginBottom = borde + "px"
for(m=0;m<7;m++){
	ce = document.createElement('div')
	ce.style.width = celda + "px"
	ce.style.height = celda + "px"
	ce.style.marginRight = borde + "px"
	ce.className = "diaS"
	tunIex ? ce.style.styleFloat = "left" : ce.style.cssFloat ="left"
	ce.appendChild(document.createTextNode(diasSemana[m]))
	fi.appendChild(ce)
	if(m == 6){ce.style.marginRight = 0}
	}
	var escribe = false
	var escribe2 = true
fecha = new Date(anio,mes,dia)
var d = fecha.getDay()-1 
if(d<0){d = 6}
while(escribe2){
fi = document.createElement('div')
fi.className = 'fila'
fi.style.clear = "both"
fi.style.marginBottom = borde + "px"
fi.style.height = celda + "px"
co = 0
	for(t=0;t<7;t++){
		ce = document.createElement('div')
		ce.style.width = celda + "px"
		ce.style.background = "yellow"
		ce.style.height = celda + "px"
		ce.style.marginRight = borde + "px"
		ce.style.position = "relative"
		if(escribe && escribe2){
		fecha2 = new Date(anio,mes,dia)
			if(fecha2.getMonth() != mes){escribe2 = false;}
			else{
			ce.appendChild(document.createTextNode(dia));
			ce.id = "elce" + dia
			diafinal = dia
			dia++;
			co++;
			ce.onclick = marcaCalendario
			}
		}
		if(d == t && !escribe){
		ce.appendChild(document.createTextNode(dia))
		ce.id = "elce" + dia
		dia++;co++
		escribe = true
		ce.onclick = marcaCalendario
		}
		fi.appendChild(ce)
		if(hoy.getDate()+1 == dia && mes == hoy.getMonth() && anio == hoy.getFullYear()){ce.className = "Hoy"}
		else{ce.className = 'celda'}
		tunIex ? ce.style.styleFloat = "left" : ce.style.cssFloat ="left"
		if(t == 6){ce.style.marginRight = 0}
		}

	if(co>0){tab.appendChild(fi)}
	
	}
dia = dia2
if(mesfin != 0)
{
	if((mes+1) == mesinicio && (mes+1) == mesfin)
		{
		for(t=fechainicio;t<=fechafin;t++)
		{
			marcaCelda = "elce" + t
			document.getElementById(marcaCelda).style.background = "blue"
		}
		}else if((mes+1) > mesinicio && (mes+1) < mesfin)
		{
		for(t=1;t<=diafinal;t++)
		{
			marcaCelda = "elce" + t
			document.getElementById(marcaCelda).style.background = "blue"
		}
		}else
		{
		if((mes+1) == mesinicio)
		{
		for(t=fechainicio;t<=diafinal;t++)
		{
			marcaCelda = "elce" + t
			document.getElementById(marcaCelda).style.background = "blue"
		}

		}
		if((mes+1) == mesfin)
		{
		for(t=1;t<=fechafin;t++)
		{
			marcaCelda = "elce" + t
			document.getElementById(marcaCelda).style.background = "blue"
		}

		}
		}


}
		
}

function fechaErronea(){
			for(t=1;t<=diafinal;t++)
			{
				marcaCelda = "elce" + t
				document.getElementById(marcaCelda).style.background = "yellow"
			}
			tunSel = true
			tunSel2 = true
			document.getElementById("fechaCalendarioinicio").value = ""
			document.getElementById("fechaCalendariofin").value = ""

			}
function marcaCalendario(){
var marcainicio
salidaMes = mes +1
if(fSalidaNombreMes){
	salidaMes = meses[mes] 
	if(fMesAbreviado){
		salidaMes = salidaMes.substring(0,3)
	}
}

ceSe = document.createElement('div')
ceSe.id = "tunSeleccionado"
with(ceSe.style){
	borderWidth = "3px"
	borderStyle = "solid"
	borderColor = "#ff0000"
	width = celda -6 + "px"
	height = celda -6 + "px"
	position = "absolute"
	left = 0
	top = 0
	zIndex = "200"
	}
for(t=1;t<=diafinal;t++)
	{
		marcaCelda = "elce" + t
		document.getElementById(marcaCelda).style.background = "yellow"
	}

marcado = true

	if(tunSel2 && tunSel)
	{
	for(t=fechainicio;t<fechafin+1;t++)
	{
	marcaCelda = "elce" + t
	document.getElementById(marcaCelda).style.background = "yellow"
	}
	marcado = false
	mesinicio = "Ninguno"
	mesfin = "Ninguno"
	tunSel = false
	tunSel2 = false
	}

	if(tunSel && !tunSel2)
	{
	document.getElementById("fechaCalendariofin").value = this.firstChild.nodeValue + separadorFecha + salidaMes + separadorFecha + anio
	fechafin = parseInt(this.firstChild.nodeValue)
	mesfin = salidaMes
	if(mesinicio<=salidaMes)
	{
	if(mesinicio != salidaMes)
		{
		marcainicio = 1
		}else{marcainicio = fechainicio}
	if(marcainicio<=fechafin)
		{
		for(t=marcainicio;t<=fechafin;t++)
			{
			marcaCelda = "elce" + t
			document.getElementById(marcaCelda).style.background = "blue"
			
			}
		calculaDias()
		}else{fechaErronea()}
	}else{fechaErronea()}
	tunSel2 = true
	}else{
	document.getElementById("fechaCalendarioinicio").value = this.firstChild.nodeValue + separadorFecha + salidaMes + separadorFecha + anio
	document.getElementById("fechaCalendariofin").value = ""
	fechainicio = parseInt(this.firstChild.nodeValue)
	mesinicio = salidaMes
	mesfin = 0
	tunSel = true
		for(t=1;t<=diafinal;t++)
		{
			marcaCelda = "elce" + t
			document.getElementById(marcaCelda).style.background = "yellow"
		}
	marcaCelda = "elce" + fechainicio
	document.getElementById(marcaCelda).style.background = "blue"
	
	tuDias.value = 0
	tuCoste.value = 0
	}
}



function borra(){
document.getElementById(idContenedor).removeChild(document.getElementById('calendario'))
/*document.getElementById(idContenedor).removeChild(document.getElementById("fechaCalendarioinicio"))*/
/*document.getElementById(idContenedor).removeChild(document.getElementById("fechaCalendariofin"))*/
}
function establecerFecha(){
tunFe = new Date()
document.getElementById('tuMes').options[tunFe.getMonth()].selected = true
document.getElementById('tuAno').value = tunFe.getFullYear()
}