function total_noches()//obtienes la diferencia de las noches
{	
var valor1=document.formu.entrada.value;	
var valor2=document.formu.salida.value;

	if(valor1!='dd-mm-aaaa' && valor2!='dd-mm-aaaa'){
		dia1 = valor1.split("-");
		dia2 = valor2.split("-");
		if(valor1[1]==valor2[1]){
			total=dia2[0]-dia1[0]
			parseInt(total)
			if(total>0)	
				document.formu.noches.value=total;	
			else
				document.formu.noches.value="Error";	
		}
	}	
}


function invisibilida_selects(estat_visualitzacio)
{
	var i = 0;
	var sel;
	while (sel=document.getElementById('invisible' + i))
	{
		sel.style.visibility=estat_visualitzacio;
		i++;
	}
}


function addvalor(valor,campo){
//document.write(campo);
	mostrarca('calendar');
	
//document.write(campo)
	
	if(campo=='undefined'){
		//document.write('valor vacio')
		campo=document.formu.zone.value;
		//document.write(campo)
	}	
	
	//alert(campo_actual);
	
	if(campo=='salida'){
	//alert("DENTRO1");
		document.formu.salida.value=valor;	
	}
	if(campo=='entrada'){
	//alert("DENTRO2");
		document.formu.entrada.value=valor;		
	}
}
function mostrarca(menu,campo)
	{
		var enlc = document.getElementById(menu);
				
		if (enlc.style.display=="")
		{
			enlc.style.display='none';
		}
		else
		{
			enlc.style.display = "";
		}
	}


function mcal(campo) {
	nm=document.formu.dmes.value
	na=document.formu.danio.value
	document.formu.zone.value=campo
	
	//ampo_actual = campo;
	
	//alert(campo);
		
	calendario(nm,na,campo)	
}


function calendario(nm,na,campo){
//document.write(nm)
setCal(nm,na,campo)

function leapYear(year) {
if (year % 4 == 0) // basic rule
return true // is leap year
return false // is not leap year
}



function getDays(month, year) {
var ar = new Array(12)
ar[0] = 31 // January
ar[1] = (leapYear(year)) ? 29 : 28 // February
ar[2] = 31 // March
ar[3] = 30 // April
ar[4] = 31 // May
ar[5] = 30 // June
ar[6] = 31 // July
ar[7] = 31 // August
ar[8] = 30 // September
ar[9] = 31 // October
ar[10] = 30 // November
ar[11] = 31 // December

return ar[month]
}

function getMonthName(month) {
var ar = new Array(12)
ar[0] = "Enero"
ar[1] = "Febrero"
ar[2] = "Marzo"
ar[3] = "Abril"
ar[4] = "Mayo"
ar[5] = "Junio"
ar[6] = "Julio"
ar[7] = "Agosto"
ar[8] = "Septiembre"
ar[9] = "Octubre"
ar[10] = "Noviembre"
ar[11] = "Diciembre"

return ar[month]
}

function setCal(nm,na,c_m) {//calcula los datos de la fecha actual modificandose para cada mes

//document.write(nm)
var now = new Date()
var mac = now.getMonth()//mes actual
var aac = now.getYear()//aņo actual comienza en 1900
if (aac < 1000)
	aac+=1900


if(nm=='' && na==''){
	var month = now.getMonth()
	var year = now.getYear()//varia la fecha en funcion al mes
	if (year < 1000)
		year+=1900
}		
else{
	if(nm>11){//para aņos superiores
		month = 0
		nm=0
		year=parseInt(na)+1
		}
	else{
			if(nm<0){//para aņos inferiores
			month = 11
			nm = 11
			year=parseInt(na)-1			
			}
			else{//para llevarme la variable a lo largo del aņo
				month = nm
				year = na
			}
		}
	}
//document.write(year)		
document.formu.dmes.value=parseInt(month);
document.formu.danio.value=parseInt(year);	
//document.write(year)
var monthName = getMonthName(month)
var date = now.getDate()
now = null

var firstDayInstance = new Date(year, month, 1)
var firstDay = firstDayInstance.getDay()-1
firstDayInstance = null

var days = getDays(month, year)
//document.write(aac)

drawCal(firstDay + 1, days, date, monthName, year, month, nm, na, mac, aac,c_m)
}

function drawCal(firstDay, lastDate, date, monthName, year, month, nm, na, mac, aac,c_m) {

var campo_actual = c_m;
var mes=parseInt(month);
var mesm=parseInt(month)+1;
var headerHeight = 50 // height of the table's header cell
var border = 0 // 3D height of table's border
var cellspacing = 2 // width of table's border
var headerColor = "midnightblue" // color of table's header
var headerSize = "+3" // size of tables header font
var colWidth = 30 // width of columns in table
var dayCellHeight = 15 // height of cells containing days of the week
var dayColor = "darkblue" // color of font representing week days
var cellHeight = 20 // height of cells representing dates in the calendar
var todayColor = "white" // color specifying today's date in the calendar
var timeColor = "purple" // color of font representing current time

var text = "" // initialize accumulative variable to empty string
text += '<CENTER>'
text += '<TABLE BORDER=' + border + ' CELLSPACING=' + cellspacing + '  WIDTH=280>' // table settings
text += '<TH COLSPAN=7 HEIGHT=' + headerHeight + '>' // create table header cell
text += '<FONT COLOR="' + headerColor + '" SIZE=' + headerSize + '>' // set font for table header
text += monthName + ' ' + year 
text += '</FONT>' // close table header's font settings
text += '</TH>' // close header cell

var openCol = '<TD WIDTH=' + colWidth + ' HEIGHT=' + dayCellHeight + '>'
openCol += '<FONT COLOR="' + dayColor + '">'
var closeCol = '</FONT></TD>'

var weekDay = new Array(7)
weekDay[0] = "Lun"
weekDay[1] = "Mar"
weekDay[2] = "Mie"
weekDay[3] = "Jue"
weekDay[4] = "Vie"
weekDay[5] = "Sab"
weekDay[6] = "Dom"

text += '<TR ALIGN="center" VALIGN="center">'
for (var dayNum = 0; dayNum < 7; ++dayNum) {
text += openCol + weekDay[dayNum] + closeCol 
}
text += '</TR>'

var digit = 1
var curCell = 1
//document.write(campo);

if(firstDay==0){//gaime an fer
	firstDay = 7;
}


for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {	
	text += '<TR ALIGN="right" VALIGN="top">'
	for (var col = 1; col <= 7; ++col) {		
		if (digit > lastDate)
			break
		if (curCell < firstDay) {
			text += '<TD></TD>';
			digit=0;   //gaime an fer
			curCell++
			}
		else {
		//document.getElementById('datosfecha').innerHTML= "aņo= " + year + " mes= " + mes + " dia= " + date + "<br> aņo actual= " + aac + " mes actual= " + mac
			//document.getElementById('datos').innerHTML= date + ' - ' +mac + ' - ' + aac
			if (digit == date && mes==mac && year==aac ) { //para dibujar el dia actual
				
				/*
				text += '<TD HEIGHT=' + cellHeight + ' bgcolor="#acc3e0" onclick="addvalor(\'' + digit + '-' + mesm + '-' + year + '\');" onmouseover="this.style.cursor=\'pointer\';this.style.cursor=\'hand\'">'
				text += '<FONT COLOR="' + todayColor + '">'
				text += digit
				text += '</FONT><BR>'
				text += '</TD>'
				*/
				
				text += '<TD HEIGHT=' + cellHeight + ' bgcolor="#acc3e0" onclick="addvalor(\'' + digit + '-' + mesm + '-' + year + '\',\''+ campo +'\');invisibilida_selects(\'visible\');total_noches()" onmouseover="this.style.cursor=\'pointer\';this.style.cursor=\'hand\'">'
				text += '<font color="#FFFFFF">	' + digit + '</font></TD>'
				}
			else{
				if(mes==mac &&  year==aac && digit > date)//para dibujar los dias siguientes del mes actual
					text += '<TD HEIGHT=' + cellHeight + ' bgcolor="#DCE6F1" onclick="addvalor(\'' + digit + '-' + mesm + '-' + year + '\',\''+ campo +'\');invisibilida_selects(\'visible\');total_noches()" onmouseover="this.style.cursor=\'pointer\';this.style.cursor=\'hand\'">' + digit + '</TD>'
				else{
					if(mes==mac &&  year==aac && digit < date)//para dibujar los dias anteriores del mes actual
						text += '<TD HEIGHT=' + cellHeight + ' bgcolor="#F2F2F2">' + digit + '</TD>'				
					else{
						if(year<aac)//para dibujar los meses anteriores al actual
							text += '<TD HEIGHT=' + cellHeight + ' bgcolor="#F2F2F2">' + digit + '</TD>'
						else{
							if(year==aac && mes < mac)
								text += '<TD HEIGHT=' + cellHeight + ' bgcolor="#F2F2F2">' + digit + '</TD>'
							else
								text += '<TD HEIGHT=' + cellHeight + ' bgcolor="#DCE6F1" onclick="addvalor(\'' + digit + '-' + mesm + '-' + year + '\',\''+ campo +'\');invisibilida_selects(\'visible\');total_noches()" onmouseover="this.style.cursor=\'pointer\';this.style.cursor=\'hand\'">' + digit + '</TD>'
							
						}
					}											
				}			
			}
		}
	digit++
	}			
text += '</TR>'			
}

//rellenamos las filas que falten para que el calendario no varie de tamaņo
if((row-1) < 6){
	for(i=0; i < 6-(row-1); i++){
		text += '<TR>';
		text += '<TD HEIGHT=' + cellHeight + ' >&nbsp;</TD>';
		text += '</TR>';
	}
}
		
	text += '</TABLE>'
	text += '</CENTER>'
	
	var mant=mes-1//mes anterior
	var msig=mes+1//mes siguiente
		
	text +='<div id="paso"><a href="#" onclick="document.formu.dmes.value=' + mant + ';mcal(\''+ campo_actual +'\');">&lt;&lt; Anterior</a> | <a href="#" onclick="document.formu.dmes.value=' + msig + ';mcal(\''+ campo_actual +'\');">Siguiente &gt;&gt;</a><br><br><a href="#" onclick="mostrarca(\'calendar\');invisibilida_selects(\'visible\');">Cerrar calendario</a></div>'	
	document.getElementById('calendar').innerHTML= text
	
}

}