function obten_mes(mes){
	document.getElementById('dmes').value = mes;	
}

/*
function total_noches()//obtienes la diferencia de las noches
{	
var valor1=document.getElementById('fecha1').value;	
var valor2=document.getElementById('fecha2').value;

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


function addvalor(valor,campo,idcalendario){
	
	mostrarca(idcalendario);
	
	if(campo=='undefined'){
		campo=document.getElementById('zone').value;
	}	
	
	document.getElementById(campo).value = valor;
		
	realizaBusqueda();
	
}
function mostrarca(menu,campo)
{
		var enlc = document.getElementById(menu);
						
		if (enlc.style.display=="")
		{
			enlc.style.display='none';
			muestraSelects(menu);
		}
		else
		{
			enlc.style.display = "";
			ocultaSelects(menu);
		}
}


function mcal(campo, idcalendario) {
		
	nm=document.getElementById('dmes').value
	na=document.getElementById('danio').value
	document.getElementById('zone').value=campo
		
	calendario(nm,na,campo,idcalendario)	
}


function calendario(nm,na,campo,idcalendario){
	
setCal(nm,na,campo,idcalendario)

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

var now = new Date()
var mac = now.getMonth()//mes actual
var aac = now.getYear()//aņo actual comienza en 1900

var anoActual = document.getElementById('anioActual').value;

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.getElementById('dmes').value=parseInt(month);
document.getElementById('danio').value=parseInt(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)

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

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

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 = "b7b7b7" // 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 = "333" // 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 STYLE="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 STYLE="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

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 {
		
			if(digit < 10){
				var diasJ = "0"+digit;	
			}else{
				var diasJ = digit;	
			}
			
			if(mesm < 10){
				var mesesJ = "0"+mesm;	
			}else{
				var mesesJ = mesm;	
			}
			

			if (digit == date && mes==mac && year==aac ) { //para dibujar el dia actual
				
				text += '<TD HEIGHT=' + cellHeight + ' bgcolor="#acc3e0" onclick="addvalor(\'' + diasJ + '/' + mesesJ + '/' + year + '\',\''+ campo +'\',\''+idcalendario+'\');" onmouseover="this.style.cursor=\'pointer\';this.style.cursor=\'hand\'">'
				text += '<FONT COLOR="' + todayColor + '">'
				text += digit
				text += '</FONT><BR>'
				text += '</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(\'' + diasJ + '/' + mesesJ + '/' + year + '\',\''+ campo +'\',\''+idcalendario+'\');" 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(\'' + diasJ + '/' + mesesJ + '/' + year + '\',\''+ campo +'\',\''+idcalendario+'\');" onmouseover="this.style.cursor=\'pointer\';this.style.cursor=\'hand\'">' + digit + '</TD>'
							
						}
					}											
				}			
			}
		}
	digit++
	}			
text += '</TR>'			
}

//si el mes a mostrar muestra menos de 7 filas aņadimos filas en blanco para que el texto inferior no este moviendose de sitio (gaime an fer) XD
if(row < 7){
	var diferencia_filas = 7 - row; //comprobamos cuantas filas en blanco debemos insertar;
	
	for(f=0; f<diferencia_filas; f++){
		text += '<TR STYLE=" height:20px;"><TD>&nbsp;</TD></TR>';	
	}
}
		
	text += '</TABLE>'
	text += '</CENTER>'
	
	var mant=mes-1//mes anterior
	var msig=mes+1//mes siguiente
		
	text +='<div id="paso"><a onclick="obten_mes(' + mant + '); mcal(\''+ campo_actual +'\', \''+idcalendario+'\');">&lt;&lt; Anterior</a> | <a onclick="obten_mes(' + msig + ');mcal(\''+ campo_actual +'\', \''+idcalendario+'\');">Siguiente &gt;&gt;</a><br><br><a href="#" onclick="mostrarca(\''+ idcalendario +'\');">Cerrar calendario</a></div>'	
	document.getElementById(idcalendario).innerHTML= text
	
}

}
