<!--
// Título: Selector de fecha
// Versión: 2.0
// Fecha: 12/02/2002

function select_date(str_baseurl, str_target, str_datetime, from_year, to_year) {

	var str_imageurl = str_baseurl + "imagenes/common/"
	var arr_months = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio",
		"Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];
	var week_days = ["D", "L", "M", "X", "J", "V", "S"];
	var week_daynames = ["Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"];
	var n_weekstart = 1; // el día de la semana comienza el lunes (0 para el domingo)

	var dt_datetime = (str_datetime == null || str_datetime == "" ?  new Date() : strdt(str_datetime));
	var titlecolor = "#c0c0c0"

	var dt_prev_month = new Date(dt_datetime);
	dt_prev_month.setMonth(dt_datetime.getMonth() - 1);
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setMonth(dt_datetime.getMonth() + 1);

	var dt_prev_year = new Date(dt_datetime);
	dt_prev_year.setYear(dt_datetime.getFullYear() - 1);
	var dt_next_year = new Date(dt_datetime);
	dt_next_year.setYear(dt_datetime.getFullYear() + 1);

	var dt_firstday = new Date(dt_datetime);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1 - (7 + dt_firstday.getDay() - n_weekstart) % 7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);
	
	// Generación HTML del calendario
	// Impresión del encabezado del calendario
	var str_header = "<html>\n"
		str_header = str_header + "<head>\n"
		str_header = str_header + "	<title>Selector de fecha</title>\n"
		str_header = str_header + "</head>\n\n"
		str_header = str_header + "<body bgcolor=\"White\" leftmargin=\"0\" topmargin=\"0\" bottommargin=\"0\" marginheight=\"0\" rightmargin=\"0\" marginwidth=\"0\">\n\n"
		str_header = str_header + "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\" height=\"100%\">\n"
		str_header = str_header + "<tr>\n"
		str_header = str_header + "	<td bgcolor=\"" + titlecolor + "\" valign=\"top\">\n\n"
		str_header = str_header + "	<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\" height=\"100%\">\n"
		str_header = str_header + "	<tr>\n"
		str_header = str_header + "		<td bgcolor=\"" + titlecolor + "\" colspan=\"7\">\n\n"
		str_header = str_header + "		<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\"\">\n"
		str_header = str_header + "		<tr>\n"

	// Si estamos en el primer año del intervalo permitido no mostramos el botón "Año anterior"
	if (dt_datetime.getFullYear() > from_year)
		str_header = str_header + "			<td bgcolor=\"" + titlecolor + "\"><a href=\"javascript:window.opener.select_date('" + str_baseurl + "', '" + str_target + "', '" + dtdtstr(dt_prev_year) + "', " + from_year + ", " + to_year + ");\"><img src=\"" + str_imageurl + "prev_year.gif\" width=\"13\" height=\"16\" border=\"0\" alt=\"Año anterior\"></a></td>\n"
	else
		str_header = str_header + "			<td bgcolor=\"" + titlecolor + "\"><img src=\"" + str_imageurl + "spacer.gif\" width=\"13\" height=\"16\" border=\"0\"></td>\n"

	// Si estamos en el primer mes del primer año del intervalo permitido no mostramos el botón "Mes anterior"
	if (dt_datetime.getFullYear() != from_year || dt_datetime.getMonth() != 0)
		str_header = str_header + "			<td bgcolor=\"" + titlecolor + "\"><a href=\"javascript:window.opener.select_date('" + str_baseurl + "', '" + str_target + "', '" + dtdtstr(dt_prev_month) + "', " + from_year + ", " + to_year + ");\"><img src=\"" + str_imageurl + "prev.gif\" width=\"13\" height=\"16\" border=\"0\" alt=\"Mes anterior\"></a></td>\n"
	else
		str_header = str_header + "			<td bgcolor=\"" + titlecolor + "\"><img src=\"" + str_imageurl + "spacer.gif\" width=\"13\" height=\"16\" border=\"0\"></td>\n"

		str_header = str_header + "			<form name=\"dt_select\">\n"
		str_header = str_header + "			<td bgcolor=\"" + titlecolor + "\" width=\"100%\" align=\"center\">\n"
		str_header = str_header + "			<select name=\"dt_month\" style=\"font-family: Verdana, Arial; color: #808080; font-size: 10px; background-color: #ffffff;\" onChange=\"window.opener.select_date('" + str_baseurl + "', '" + str_target + "', String('" + dt_datetime.getDate() + "/' + (document.dt_select.dt_month.selectedIndex + 1) + '/" + dt_datetime.getFullYear() + "'), " + from_year + ", " + to_year + ");\">\n"

	// Seleccionamos el mes actual de la lista desplegable
	for (var monthindex = 0; monthindex < 12; monthindex++)
		if (monthindex == dt_datetime.getMonth())
			str_header = str_header + "			<option value=\"" + (monthindex + 1) + "\" selected>" + arr_months[monthindex] + "</option>\n"
		else
			str_header = str_header + "			<option value=\"" + (monthindex + 1) + "\">" + arr_months[monthindex] + "</option>\n"

		str_header = str_header + "			</select>\n"
		str_header = str_header + "			<select name=\"dt_year\" style=\"font-family: Verdana, Arial; color: #808080; font-size: 10px; background-color: #ffffff;\" onChange=\"window.opener.select_date('" + str_baseurl + "', '" + str_target + "', String('" + dt_datetime.getDate() + "/" + (dt_datetime.getMonth() + 1) + "/' + (" + from_year + " + dt_select.dt_year.selectedIndex)), " + from_year + ", " + to_year + ");\">\n"

	// Seleccionamos el año actual de la lista desplegable
	for (var yearindex = from_year; yearindex <= to_year; yearindex++)
		if (yearindex == dt_datetime.getFullYear())
			str_header = str_header + "			<option selected>" + yearindex + "</option>\n"
		else
			str_header = str_header + "			<option>" + yearindex + "</option>\n"

		str_header = str_header + "			</select>\n"
		str_header = str_header + "			</span>\n</td>\n"
		str_header = str_header + "			</form>\n"

	// Si estamos en el último mes del último año del intervalo permitido no mostramos el botón "Mes siguiente"
	if (dt_datetime.getFullYear() != to_year || dt_datetime.getMonth() != 11)
		str_header = str_header + "			<td bgcolor=\"" + titlecolor + "\"><a href=\"javascript:window.opener.select_date('" + str_baseurl + "', '" + str_target + "', '" + dtdtstr(dt_next_month) + "', " + from_year + ", " + to_year + ");\"><img src=\"" + str_imageurl + "next.gif\" width=\"13\" height=\"16\" border=\"0\" alt=\"Mes siguiente\"></a></td>\n"
	else
		str_header = str_header + "			<td bgcolor=\"" + titlecolor + "\"><img src=\"" + str_imageurl + "spacer.gif\" width=\"13\" height=\"16\" border=\"0\"></td>\n"

	// Si estamos en el último año del intervalo permitido no mostramos el botón "Año siguiente"
	if (dt_datetime.getFullYear() != to_year)
		str_header = str_header + "			<td bgcolor=\"" + titlecolor + "\"><a href=\"javascript:window.opener.select_date('" + str_baseurl + "', '" + str_target + "', '" + dtdtstr(dt_next_year) + "', " + from_year + ", " + to_year + ");\"><img src=\"" + str_imageurl + "next_year.gif\" width=\"13\" height=\"16\" border=\"0\" alt=\"Año siguiente\"></a></td>\n"
	else
		str_header = str_header + "			<td bgcolor=\"" + titlecolor + "\"><img src=\"" + str_imageurl + "spacer.gif\" width=\"13\" height=\"16\" border=\"0\"></td>\n"

		str_header = str_header + "		</tr>\n"
		str_header = str_header + "		</table>\n\n"
		str_header = str_header + "		</td>\n"
		str_header = str_header + "	</tr>\n"
	

	var str_buffer = new String (str_header);

	var dt_current_day = new Date(dt_firstday);
	// Impresión de los títulos de los días de semana
	str_buffer += "	<tr>\n";
	for (var n=0; n<7; n++)
		str_buffer += "		<td bgcolor=\"#dddddd\" align=\"center\"><span style=\"font-family: Verdana, Arial; color: #808080; font-size: 11px;\"><b>" + week_days[(n_weekstart + n) % 7] + "</b></span></td>\n";
	str_buffer += "	</tr>\n";

	// Impresión de la tabla del calendario
	while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		// Impresión de cada fila de la tabla del calendario
		str_buffer += "	<tr height=\"17%\">\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				if (dt_current_day.getDate() == dt_datetime.getDate() &&
					dt_current_day.getMonth() == dt_datetime.getMonth())
					// Impresión de la fecha actual
					str_buffer += "		<td bgcolor=\"#000000\" align=\"right\">"
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					// Días del fin de semana
					str_buffer += "		<td bgcolor=\"#eeeeee\" align=\"right\">";
				else
					// Días de diario del presente mes
					str_buffer += "		<td bgcolor=\"#ffffff\" align=\"right\">";

				if (dt_current_day.getMonth() == dt_datetime.getMonth())
					// Días del presente mes
					if (dt_current_day.getDate() == dt_datetime.getDate() &&
						dt_current_day.getMonth() == dt_datetime.getMonth())
						str_buffer += "<a href=\"javascript:window.opener." + str_target + ".value='" + dtdtstr(dt_current_day) + "'; window.opener.multiple_dates('" + str_target + "', '" + dtdtstr(dt_current_day) + "', '" + week_daynames[dt_current_day.getDay()] + ", " + dt_current_day.getDate() + " de " + arr_months[dt_current_day.getMonth()] + " de " + dt_current_day.getFullYear() + "'); window.opener.update_date_field('" + str_target + "'); window.close();\"><span style=\"font-family: Verdana, Arial; color: #ffffff; font-size: 11px; text-decoration: none;\" title=\"" + week_daynames[dt_current_day.getDay()] + ", " + dt_current_day.getDate() + " de " + arr_months[dt_current_day.getMonth()] + " de " + dt_current_day.getFullYear() + "\">";
					else
						str_buffer += "<a href=\"javascript:window.opener." + str_target + ".value='" + dtdtstr(dt_current_day) + "'; window.opener.multiple_dates('" + str_target + "', '" + dtdtstr(dt_current_day) + "', '" + week_daynames[dt_current_day.getDay()] + ", " + dt_current_day.getDate() + " de " + arr_months[dt_current_day.getMonth()] + " de " + dt_current_day.getFullYear() + "'); window.opener.update_date_field('" + str_target + "'); window.close();\"><span style=\"font-family: Verdana, Arial; color: #606060; font-size: 11px; text-decoration: none;\" title=\"" + week_daynames[dt_current_day.getDay()] + ", " + dt_current_day.getDate() + " de " + arr_months[dt_current_day.getMonth()] + " de " + dt_current_day.getFullYear() + "\">";
				else
					// Días de otros meses
					str_buffer += "<a href=\"javascript:window.opener." + str_target + ".value='" + dtdtstr(dt_current_day) + "'; window.opener.multiple_dates('" + str_target + "', '" + dtdtstr(dt_current_day) + "', '" + week_daynames[dt_current_day.getDay()] + ", " + dt_current_day.getDate() + " de " + arr_months[dt_current_day.getMonth()] + " de " + dt_current_day.getFullYear() + "'); window.opener.update_date_field('" + str_target + "'); window.close();\"><span style=\"font-family: Verdana, Arial; color: #aaaaaa; font-size: 11px; text-decoration: none;\" title=\"" + week_daynames[dt_current_day.getDay()] + ", " + dt_current_day.getDate() + " de " + arr_months[dt_current_day.getMonth()] + " de " + dt_current_day.getFullYear() + "\">";

				str_buffer += dt_current_day.getDate() + "</span></a></td>\n";
				dt_current_day.setDate(dt_current_day.getDate() + 1);
		}
		// Impresión de la fila del pie de tabla
		str_buffer += "	</tr>\n";
	}
	// Impresión de la fila del pie de tabla del calendario
	str_buffer +=
		"	</table>\n\n" +
		"	</td>\n" +
		"</tr>\n</table>\n\n" +
		"</body>\n" +
		"</html>\n";

	var vWinCal = window.open("", "Calendario", "width=216,height=170,status=no,resizable=no,top=200,left=200");
	vWinCal.opener = self;
	vWinCal.focus();
	var calc_doc = vWinCal.document;
	calc_doc.write (str_buffer);
	calc_doc.close();
}

// Función que valida una fecha con formato dd/mm/yyyy.
function strdt (str_datetime) {
	var re_date = /^(\d+)\/(\d+)\/(\d+)$/;
	if (!re_date.exec(str_datetime)) {
		alert("El formato de fecha no es válido: " + str_datetime);
		return (new Date());
	}
	return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1));
}

// Función que formatea una fecha con formato dd/mm/yyyy.
function dtdtstr (dt_datetime) {
	dt_datetime.getDate();
	var dt_day   = String(dt_datetime.getDate());
	var dt_month = String(dt_datetime.getMonth()+1);
	var dt_year  = String(dt_datetime.getFullYear());
	if (dt_day.length < 2) dt_day = "0" + dt_day;
	if (dt_month.length < 2) dt_month =+ "0" + dt_month;
	return (dt_day + "/" + dt_month + "/" + dt_year);
}

function multiple_dates (str_multiple_dates, str_selected_date, str_selected_long_date) {
	var bln_add = true;
	var obj_multiple_dates = eval(str_multiple_dates);
	if(obj_multiple_dates.type == 'select-one') {
		for (i = 0; i < obj_multiple_dates.options.length; i ++) {
			if (obj_multiple_dates.options[i].value == str_selected_date) {
				alert('La fecha seleccionada ya está en la lista.');
				bln_add = false;
				break;
			}
		}
		if (bln_add) {
			obj_multiple_dates.options[obj_multiple_dates.options.length] = new Option(str_selected_long_date, str_selected_date);
		}
	}
}

function remove_date (str_multiple_dates) {
	var obj_multiple_dates = eval(str_multiple_dates);
	if (obj_multiple_dates.selectedIndex != -1)
		obj_multiple_dates.options[obj_multiple_dates.selectedIndex] = null;
	else
		alert('Seleccione antes una fecha.');
}

function update_date_field (str_multiple_dates) {
	var str_dates;
	var obj_multiple_dates = eval(str_multiple_dates);
	str_dates = "";
	if(obj_multiple_dates.type == 'select-one') {
		if (obj_multiple_dates.options.length > 0) {
			for (var i = 0; i < obj_multiple_dates.options.length; i ++) {
				if (i == 0) {
					str_dates = obj_multiple_dates.options[i].value;
				}
				else {
					str_dates += "," + obj_multiple_dates.options[i].value;
				}
			}
		}
		else
			str_dates = "";
		eval(str_multiple_dates.replace('MD', '') + ".value = '" + str_dates + "'");
	}
}

//-->
