var nombres_meses = new Array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
var nombres_dias = new Array("Lun", "Mar", "Mie", "Jue", "Vie", "Sab", "Dom");
var nombres_enteros_dias = new Array("Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado", "Domingo");
		
var dia;
var mes;
var anio;

var campo_destino;
var campo_legible_destino;
var div_combos;
var div_calendario;

var clock_set = 0;

// Abre la ventana de calendario.
function abrirCalendario(ruta_hasta_raiz, campo, campo_legible) {
    abrirPopup(ruta_hasta_raiz + "/includes/calendario.php?campo=" + campo + "&campo_legible=" + campo_legible, "Calendario", 370, 273, "status=no", true);
}

// Pinta el calendario
function pintarCalendario() {
	if(window.opener.document.getElementById) {
		campo_destino = window.opener.document.getElementById(campo);
		campo_legible_destino = window.opener.document.getElementById(campo_legible);
    }
	else if(window.opener.document.all) {
		campo_destino = window.opener.document.all[campo];
		campo_legible_destino = window.opener.document.all[campo_legible];
    }
	
	campo_destino = window.opener.document.getElementById(campo);
	campo_legible_destino = window.opener.document.getElementById(campo_legible);
	
	// Primera llamada.
	if(!anio && !mes && !dia) {
		
		// Si hay valor en el campo destino.
        if(campo_destino.value) {
            valor_campo_destino = campo_destino.value;

			date       	= valor_campo_destino.split("-");
			dia         = parseInt(date[2],10);
			mes       	= parseInt(date[1],10) - 1;
			anio        = parseInt(date[0],10);
        }
		
        if(isNaN(anio) || isNaN(mes) || isNaN(dia) || dia == 0) {
            nueva_date	= new Date();
            anio    	= nueva_date.getFullYear();
            mes   		= nueva_date.getMonth();
            day     	= nueva_date.getDate();
        }
    }
	
	// Cuando nos movemos entre los combos.
	else {
        if(mes > 11) {
			mes = 0;
            anio++;
        }
        if(mes < 0) {
            mes = 11;
            anio--;
        }
    }

    if(document.getElementById) {
		div_calendario = window.document.getElementById("div_calendario");
		div_combos = window.document.getElementById("div_combos");
    }
	else if(document.all) {
        div_calendario = document.all["div_calendario"];
		div_combos = document.all["div_combos"];
    }
	
    div_calendario.innerHTML = "";
    cadena_html = "";

    // Pintamos la tabla.
	// (Combos de mes y aņo)
	cadena_html += '\n';
    cadena_html += '			<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n';
	cadena_html += '				<tr>\n';
	cadena_html += '					<td align="center">';
    cadena_html += '						<form onSubmit="return false;">\n';
    cadena_html += '							<select id="select_mes" name="select_mes" class="inputSelect_default" tabindex="1" onChange="mes = parseInt(this.value); pintarCalendario();">\n';
    
	for(k = 0; k < 12; k++) {
        if(k == mes) selected = ' selected="selected"';
        else selected = '';
        cadena_html += '								<option value="' + k + '" ' + selected + '>' + nombres_meses[k] + '</option>\n';
    }
	
    cadena_html += '							</select>\n';
	cadena_html += '							<input type="button" class="inputButton_flecha_abajo" tabindex="2" onClick="mes--; pintarCalendario();" onMouseOver="this.style.cursor=\'hand\'" />\n';
	cadena_html += '							<input type="button" class="inputButton_flecha_arriba" tabindex="3" onClick="mes++; pintarCalendario();" onMouseOver="this.style.cursor=\'hand\'" />\n';	
	cadena_html += '						</form>\n';
	cadena_html += '					</td>\n';
	cadena_html += '					<td width="30" />\n'
	cadena_html += '					<td align="center">\n';
    cadena_html += '						<form onSubmit="return false;">\n';
    cadena_html += '							<select id="select_anio" name="select_anio" class="inputSelect_default" tabindex="4" onChange="anio = parseInt(this.value); pintarCalendario();">\n';
    
	for(k = anio - 50; k < anio + 50; k++) {
        if(k == anio) selected = ' selected="selected"';
        else selected = '';
        cadena_html += '								<option value="' + k + '" ' + selected + '>' + k + '</option>\n';
    }
    
	cadena_html += '							</select>\n';
    cadena_html += '							<input value="&nbsp;" type="button" class="inputButton_flecha_abajo" tabindex="5" onClick="anio--; pintarCalendario();" onMouseOver="this.style.cursor=\'hand\'" />\n';
    cadena_html += '							<input type="button" class="inputButton_flecha_arriba" tabindex="6" onClick="anio++; pintarCalendario();" onMouseOver="this.style.cursor=\'hand\'" />\n';	
    cadena_html += '						</form>\n';
    cadena_html += '					</td>\n';
	cadena_html += '				</tr>\n';
	cadena_html += '			</table>\n';
	
	div_combos.innerHTML = cadena_html;
	
	cadena_html = "";

	// (Tabla de dias)
    cadena_html += '			<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n';
	cadena_html += '				<tr>\n';
	
    for(k = 0; k < 7; k++) {
		if(k > 0)
			cadena_html += '					<td width="9" height="22" background="../imagenes/tabla_cabecera_separador.jpg"><img src="../imagenes/espacio.gif" width="9" height="1" border="0" /></td>\n';
		else
			cadena_html += '					<td width="9" height="22" background="../imagenes/tabla_cabecera_rep.jpg"><img src="../imagenes/espacio.gif" width="9" height="1" border="0" /></td>\n';
		cadena_html += '					<td width="36" background="../imagenes/tabla_cabecera_rep.jpg" style="background-repeat:repeat;" class="Tabla_titulo">' + nombres_dias[k] + '</td>\n';
    }
	
    cadena_html += "				</tr>\n";
	cadena_html += "				<tr>\n";
	cadena_html += '					<td height="3" colspan="14" valign="bottom" background="../imagenes/tabla_separador_rep.jpg" style="background-repeat:repeat;"><img src="../imagenes/espacio.gif" width="1" height="1" border="0" /></td>\n';
	cadena_html += "				</tr>\n";

    var primer_dia = new Date(anio, mes, 1).getDay();
	if(primer_dia == 0) primer_dia = 7;
    var ultimo_dia = new Date(anio, mes + 1, 0).getDate();

    cadena_html += "				<tr>\n";

    dia_semana = 1;
    for(k = 1; k < primer_dia; k++) {
        cadena_html += "					<td colspan=\"2\">&nbsp;</td>\n";
        dia_semana++;
    }
    for(k = 1; k <= ultimo_dia; k++) {
        if(dia_semana == 8) {
            cadena_html += "				</tr>\n";
			cadena_html += "				<tr>\n";
            dia_semana = 1;
        }

		valor_actual = formatNum4(anio) + "-" + formatNum2((mes + 1), 'mes') + "-" + formatNum2(k, 'dia');
		valor_actual_legible = /*nombres_enteros_dias[dia_semana - 1] + ", " + */formatNum2(k, 'dia') + " de " + nombres_meses[mes] + " de " + formatNum4(anio);

        if(k == dia) {
            style = ' class="celda_dia_seleccionado"';
            //current_date = valor_actual;
        }
		else {
            style = ' class="celda_dia"';
        }
        cadena_html += "			<td colspan=\"2\" class=\"celda_dia\"><a " + style + " href=\"javascript:devolverFecha('" + valor_actual + "', '" + valor_actual_legible + "');\">" + k + "</a></td>\n"
        dia_semana++;
    }
    for (k = dia_semana; k < 8; k++) {
        cadena_html += "			<td colspan=\"2\">&nbsp;</td>\n";
    }

    cadena_html += "			</tr>\n";
	cadena_html += "		</table>\n";

    div_calendario.innerHTML = cadena_html;
}

		
function devolverFecha(fecha, fecha_legible) {
    campo_destino.value = fecha;
	campo_legible_destino.value = fecha_legible;
    window.close();
}

function formatNum2(i, valtype) {
    f = (i < 10 ? '0' : '') + i;
    if (valtype && valtype != '') {
        switch(valtype) {
            case 'month':
                f = (f > 12 ? 12 : f);
                break;

            case 'day':
                f = (f > 31 ? 31 : f);
                break;

            case 'hour':
                f = (f > 24 ? 24 : f);
                break;

            default:
            case 'second':
            case 'minute':
                f = (f > 59 ? 59 : f);
                break;
        }
    }

    return f;
}

/**
 * Formats number to four digits.
 *
 * @param   int number to format.
 */
function formatNum4(i) {
    i = parseInt(i, 10)
    return (i < 1000 ? i < 100 ? i < 10 ? '000' : '00' : '0' : '') + i;
}
