
function Calendar(objName, pIdioma)
{    

	// Propiedades
	if ((pIdioma) && (pIdioma != ""))
	    this.idioma = pIdioma
	else
	    this.idioma = "es"

//alert(this.idioma); 
	
	this.name = objName;			        // Nombre objecto
	this.aplicacion = "logitravel"	        // Directorio base	
	this.txt = "Fechas en oferta"	        // Texto de los días a resaltar
	this.txtMinimos="Precio mas bajo" 
	this.txtPromocion= "Fecha con descuento"
	this.txtClass = "verdana6rojoBold"      // Fuente y color del texto de los días a resaltar
	this.squareColor = "Bbeig"		        // Color del fondo de los días a resaltar
 	this.titleFont = "verdana9rojoBold" 	// Fuente y color del nombre de mes y 
	this.daysFont = "verdana7blanco"		// Fuente y color del nombre de los días de la semana
	this.daysBackgroundColor = "fondo"		// Color de fondo de los días de la semana
	this.backgroundColor = "Bazulflojo"		// Color de fondo de los días
	this.weekendBackgroundColor	="Bazul"	// Color de fondo de los días de un fin de semana
	this.daysNumberFont = "verdana7"		// Fuente de los días 
	this.daysPreviosNumberFont = "verdana7gris"		//Fuente de los días previos a hoy - no seleccionables
	this.daysNumberFontMinimos= this.daysNumberFont; 
	 this.daysNumberFontPromocion= this.daysNumberFont; 
	this.tableColor = "#FAAF05"				// Color de la tabla donde está el mes y 
	this.outerTableColor = "#FAAF05"		// Color de la tabla que envuelve al calendario
	this.innerTableColor = "white"			// Color de la tabla interna del calendario
	this.todayFont = "verdana7rojoBold"	// Fuente y color de hoy
	this.todayBackgroundColor = ""	        // Fuente y color de hoy
	this.offSetX = 0						// Desplazamiento relativo a la posición x
	this.offSetY = 0						// Desplazamiento relativo a la posición y
	this.flechaIzq = "/"+ this.aplicacion+"/images/flecharojaizq.gif"	// Imagen correspondiente a la flecha izquierda
	this.flechaDer = "/"+ this.aplicacion+"/images/flecharojader.gif"	// Imagen correspondiente a la flecha derecha
    this.txtCerrar = "Cerrar";
    this.imagenCerrar = "";	                // Imagen de cerrar calendario
    this.claseCerrar = "verdana6bold";      //Fuente del texto 'cerrar'
    this.funcCerrar = "";
    this.funcCambioMes = null;				//Funcion que se ejecuta al cambiar el mes
  this.daysNumberFontDestacados = "verdana7rojoBold"
	this.fontface = "verdana";		        // tipo de fuente en modo 
	this.fontsize = 8;				        //  de fuente en modo 
	this.gNow = new Date();			        // fecha actual
	this.gMin = new Date();			        // fecha máxima a ensear en el calendario
	this.gMax = new Date();			        // fecha mínima a ensear en el calendario
	this.ggWinContent="";
	this.ggPosX = -1;				        // posición x
	this.ggPosY = -1;				        // posición y
	this.gYear = "";
	this.gMonth ="";
	this.gMonthName	="";
	this.gYearly = false;	
	this.gFormat = "";
	this.gReturnItem = "";
	this.returnFunction = null
	this.idForm = 0;
	this.tdId = null;                       // id del td, si se va a incrustar dentro de una tabla
	this.mesMayuscula = false;              //Que el mes salga en mayuscula	
	this.mantenerAbierto = false;			//Hace que se mantenga abierto al ejecutar la funcion
	this.pintarPie = true;
//	this.pintarLeyenda= false; 
//	this.txtVerLenyenda= "Ver Leyenda"; 
//	this.divLeyenda = "" 
	//nombres de los meses
	this.monthNames = new Array(5,12);
    this.monthNames[0] = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];         //Español
    this.monthNames[1] = ["Janeiro", "Fevereiro", "Marco", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"];         //Portugués
    this.monthNames[2] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];            //Inglés
    this.monthNames[3] = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "Décembre"];             //Francés
    this.monthNames[4] = ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"];    //Italiano
    this.monthNames[5] = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"];				//Alemania
    this.objContenedor=""; 
    
    //nombres de los dias
    this.dayNames = new Array(5,7);
    this.dayNames[0] = ["Lu", "Ma", "Mi", "Ju", "Vi", "Sa", "Do"];  //Español
    this.dayNames[1] = [" 2", " 3", " 4", " 5", " 6", "Sa", "Do"];       //Portugues
    this.dayNames[2] = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"];  //Inglés
    this.dayNames[3] = ["Lu", "Ma", "Me", "Je", "Ve", "Sa", "Di"];  //Francés
    this.dayNames[4] = ["Lu", "Ma", "Me", "Gi", "Ve", "Sa", "Do"];  //Italiano
    this.dayNames[5] = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"];  //Italiano
     
	// Días de los meses en aos no bisiestos
	this.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	// Días de los meses en aos bisiestos
	this.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	
	// fechas a destacar
	this.listaFechas;				
	this.listaFechasMinimos ;  
    this.listaFechasPromocion ;  

	// Metodos
	this.calendarMethod = fCalendarMethod;
	this.calendarGetMonth = fCalendarGetMonth;
	
	this.calendar_get_daysofmonth = fCalendar_get_daysofmonth;
	this.calendar_calc_month_year = fCalendar_calc_month_year;
	this.getMonthlyCalendarCode = fGetMonthlyCalendarCode;
	this.getTituloFecha = fGetTituloFecha; 
	this.show = fShow;
//	this.showY = fShowY;
	this.getDia = fGetDia;
	this.cal_header = fCal_header;
	this.cal_data = fCal_data;
	this.format_day = fFormat_day;
	this.format_css = fFormat_css;
	this.write_weekend_string = fWrite_weekend_string;
	this.write_today_string = fWrite_today_string;
	this.format_data = fFormat_data;
	this.Build = fBuild;
	this.show_yearly_calendar = fShow_yearly_calendar;
	this.show_calendar = fShow_calendar;	
	this.getIndexIdioma=_getIndexIdioma; 
	this.esFechaOk = fEsFechaOk; 
	//this.funcAbrirLeyenda = fFuncAbrirLeyenda; 	
}

// Decuelve el indice del idioma
function _getIndexIdioma(idioma){
    switch(idioma){
	case "br": case "pt": return 1;
	case "en": return 2;
	case "fr": return 3;
	case "it": return 4;
	case "de": return 5;
	default: return 0;
	}
}

// Devuelve un mes en concreto
function fCalendarGetMonth(num) {
    var nomMes = this.monthNames[this.getIndexIdioma(this.idioma)][num];
	
	if (this.mesMayuscula)
		return nomMes.toUpperCase();
	else
		return nomMes;
}

// crea calendario con los datos pasados
function fCalendarMethod(p_item, p_month, p_year, p_format) {

	if ((p_month == null) && (p_year == null))	return;

	if (p_month == null) {
		this.gMonthName = null;
		this.gMonth = null;
		this.gYearly = true;
	} else {
		this.gMonthName = this.calendarGetMonth(p_month);
		this.gMonth = new Number(p_month);
		this.gYearly = false;
	}

	this.gYear = p_year;
	this.gFormat = p_format;
	this.gReturnItem = p_item;

}

// Devuelve días de un mes segun el ano.
function fCalendar_get_daysofmonth(monthNo, p_year) {
	/* 
	Check for leap year ..
	1.Years evenly divisible by four are normally leap years, except for... 
	2.Years also evenly divisible by 100 are not leap years, except for... 
	3.Years also evenly divisible by 400 are leap years. 
	*/	
	if ((p_year % 4) == 0) {
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return this.DOMonth[monthNo];
	
		return this.lDOMonth[monthNo];
	} else
		return this.DOMonth[monthNo];
		
}

// Devueleve un array con el siguiente/anterior mes y ano 
function fCalendar_calc_month_year(p_Month, p_Year, incr) {
	/* 
	Will return an 1-D array with 1st element being the calculated month 
	and second being the calculated year 
	after applying the month increment/decrement as specified by 'incr' parameter.
	'incr' will normally have 1/-1 to navigate thru the months.
	*/
	var ret_arr = new Array();

	if (incr == -1) {
		// B A C K W A R D
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year,10) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month,10) - 1;
			ret_arr[1] = parseInt(p_Year,10);
		}
	} else if (incr == 1) {
		// F O R W A R D
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year,10) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month,10) + 1;
			ret_arr[1] = parseInt(p_Year,10);
		}
	}
	
	return ret_arr;
	
}
// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
//new Calendar();

// Devuelve el código HTML del calendario a ensenar
function fGetMonthlyCalendarCode() {
	var vCode = "";
	var vHeader_Code = "";
	var vData_Code = "";
	
	vHeader_Code = this.cal_header();
	vData_Code = this.cal_data();
	
	// Begin Table Drawing code here..
	vCode += "<div class='calDivData' ";
	vCode += ">";
	vCode += vHeader_Code + vData_Code;
	if (this.pintarPie){
		vCode += "  <div class='calDivPie'>";
		var funcClose = "";
		if(this.funcCerrar!="" && this.tdId!=undefined && this.tdId!="")
			funcClose = this.funcCerrar;
		else funcClose = "{ var a = cClick();" + this.funcCerrar + "}";
		vCode += "      <div class='calDivPieLeft'>"+ this.txt +"</div><div class='calDivPieRight'><a href='javascript:void(0);' OnClick='javascript:"+funcClose+";'>";
		
		if (this.imagenCerrar != "")
			vCode += "&nbsp;<img src='"+ this.imagenCerrar + "' border=0>";
		else
			vCode += this.txtCerrar;
		
		vCode += "  </a></div>";
	}

	vCode += "</div>";
	
	return vCode;	
	
}

/*function fFuncAbrirLeyenda()
{   
    var obj; 
    obj = eval("document.getElementById('"+ this.divLeyenda +"')"); 
    
    if ((obj.style.display =="") || (obj.style.display =="none"))
     {obj.style.display="block";}
     else 
     {obj.style.display="none";}
     
}
*/
function fShow() {
	var vCode = "";
	var today = new Date();
	// build content into global var ggWinContent
	//ggWinContent += ("<FONT FACE='" + fontface + "' ><B>");
	//ggWinContent += (this.gMonthName + " " + this.gYear);
	//ggWinContent += "</B><BR>";
	// Show navigation buttons
	var prevMMYYYY = this.calendar_calc_month_year(this.gMonth, this.gYear, -1);
	var prevMM = prevMMYYYY[0];
	var prevYYYY = prevMMYYYY[1];

	var nextMMYYYY = this.calendar_calc_month_year(this.gMonth, this.gYear, 1);
	var nextMM = nextMMYYYY[0];
	var nextYYYY = nextMMYYYY[1];
		
	var width;
	//Si el mes va en mayuscula, todo junto
//	if (!this.mesMayuscula)
//		width = "WIDTH='100%'"
		
	this.ggWinContent += "<div class='calDivCab'><div class='calDivCabIzq'>"
	if (!(this.gMonth == today.getMonth() && today.getFullYear() == this.gYear)){
		this.ggWinContent += ("<A HREF=\"javascript:void(0);\" " +
			"onMouseOver=\" return true;\" " +
			"onMouseOut=\"window.status=''; return true;\" " +
			"onClick=\"");
		if (this.funcCambioMes != null){
			this.ggWinContent += this.funcCambioMes + "(" + (prevMM + 1)  + ",'" + this.idForm + "');";
		}	
		this.ggWinContent += (this.name + ".Build(" + 
			"'" + this.gReturnItem + "', '" + prevMM + "', '" + prevYYYY + "', '" + this.gFormat + "'" +
			");");
		this.ggWinContent += ("\"><img border=0 src=' " + this.flechaIzq + "'></A>");
	}
	this.ggWinContent += ("</div>");
	this.ggWinContent += ("<div class='calDivCabCen'>");
	this.ggWinContent += (this.gMonthName + " " + this.gYear);
	this.ggWinContent += ("</div><div class='calDivCabDer'>");
	
	/* style=\"cursor:hand\" onclick=\""+ this.name + ".Build(" + 
		"'" + this.gReturnItem + "', '" + nextMM + "', '" + nextYYYY + "', '" + this.gFormat + "'" +
		");" + "\">");*/
	if (!(today.getFullYear() + 1 == this.gYear && this.gMonth == (today.getMonth() - 1))){
	    this.ggWinContent += ("<A HREF=\"javascript:void(0);\" " +
		"onMouseOver=\"return true;\" " +
		"onMouseOut=\"window.status=''; return true;\" " +
		"onClick=\"");
		if (this.funcCambioMes != null){
			this.ggWinContent += this.funcCambioMes + "(" + (nextMM + 1)  + ",'" + this.idForm + "');";
		}
		this.ggWinContent += (this.name + ".Build(" + 
		"'" + this.gReturnItem + "', '" + nextMM + "', '" + nextYYYY + "', '" + this.gFormat + "'" +
		");");
	this.ggWinContent += ("\"><img border=0 src='" + this.flechaDer + "'></A>");
	}
	this.ggWinContent += (" </div></div>");
	// Get the complete calendar code for the month, and add it to the
	//	content var
	vCode = this.getMonthlyCalendarCode();
	this.ggWinContent += vCode;	
	 
	
}

/*
function fShowY()
{
	var vCode = "";
	var i;

	this.ggWinContent += "<FONT FACE='" + this.fontface + "' ><B>"
	this.ggWinContent += ("Ano : " + this.gYear);
	this.ggWinContent += "</B><BR>";

	// Show navigation buttons
	var prevYYYY = parseInt(this.gYear,10) - 1;
	var nextYYYY = parseInt(this.gYear,10) + 1;
	
	this.ggWinContent += ("<TABLE WIDTH='100%' BORDER=1 CELLSPACING=0 CELLPADDING=0 BGCOLOR='#e0e0e0' style='font-size:" + this.fontsize + "pt;'><TR><TD ALIGN=center>");
	this.ggWinContent += ("[<A HREF=\"javascript:void(0);\" " +
		"onMouseOver=\"window.status='Retroceder un ano'; return true;\" " +
		"onMouseOut=\"window.status=''; return true;\" " +
		"onClick=\"" + this.name +".Build(" + 
		"'" + this.gReturnItem + "', null, '" + prevYYYY + "', '" + this.gFormat + "'" +
		");" +
		"\"><<\Ano<\/A>]</TD><TD ALIGN=center>");
	this.ggWinContent += "       </TD><TD ALIGN=center>";
	this.ggWinContent += ("[<A HREF=\"javascript:void(0);\" " +
		"onMouseOver=\"window.status='Avanzar un ano';return true;\" " +
		"onMouseOut=\"window.status=''; return true;\" " +
		"onClick=\"" + this.name +".Build(" + 
		"'" + this.gReturnItem + "', null, '" + nextYYYY + "', '" + this.gFormat + "'" +
		");" +
		"\">Ano>><\/A>]</TD></TR></TABLE><BR>");

	// Get the complete calendar code for each month.
	// start a table and first row in the table
	this.ggWinContent += ("<TABLE WIDTH='100%' BORDER=0 CELLSPACING=0 CELLPADDING=5 style='font-size:" + this.fontsize + "pt;'><TR>");
	var j;
	for (i=0; i<12; i++) {
		// start the table cell
		this.ggWinContent += "<TD ALIGN='center' VALIGN='top'>";
		this.gMonth = i;
		this.gMonthName = this.calendarGetMonth(this.gMonth);
		vCode = this.getMonthlyCalendarCode();
		this.ggWinContent += (this.gMonthName + "/" + this.gYear + "<BR>");
		this.ggWinContent += vCode;
		this.ggWinContent += "</TD>";
		if (i == 3 || i == 7) {
			this.ggWinContent += "</TR><TR>";
			}

	}

	this.ggWinContent += "</TR></TABLE></font><BR>";
	//alert("hola");
}
*/
function fGetDia(oFecha){
	var aux=oFecha.getDay()
	switch(aux){
		case 0:
			return 7
		default:
			return aux
	}
}

function fCal_header(){
    var dayNames = this.dayNames[this.getIndexIdioma(this.idioma)];
	var vCode = "";
	
	
	vCode += "<div class='calDivCabData'>";
	for (var i=0;i<7;i++)
	    vCode += "<div class='calDivCabDataDay'>"+ dayNames[i] +"</div>";
	 vCode += "</div>";
	
	return vCode;
}

function fCal_data() {    
	if (this.mantenerAbierto){

		this.gNow.setMonth(this.gMonth);
		this.gNow.setYear(this.gYear);		
	}

	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);

	var vFirstDay=this.getDia(vDate)//vDate.getDay();
	//alert(vDate);
	var vDay=1;
	var vLastDay=this.calendar_get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay=0;
	var vCode = "<div class='calDivBodyData'>";

	/*
	Get day for the 1st of the requested month/year..
	Place as many blank cells before the 1st day of the month as necessary. 
	*/
	//alert(vFirstDay)
	
	vCode = vCode + "<table cellspacing='1' cellpadding='1'><TR>";
	
	for (i=1; i<vFirstDay; i++) {	    
		vCode = vCode + "<TD class='" + this.write_weekend_string(i) + "'></TD>";
	}

	// Write rest of the 1st week
	
	for (j=vFirstDay; j<=7; j++) {
		if(this.format_day(vDay) =="-1"){
			vCode = vCode + "<TD class='" + this.write_weekend_string(j) + 
			"'><span class='calDivBodyDataPreviousDay'>" + vDay + "</span></TD>";
		}else{
			if(this.format_day(vDay) !=""){
			
	vDate.setDate(vDay);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);
				vCode = vCode + "<TD class='" + this.write_weekend_string(j) + " " + this.write_today_string(vDay) + "'>" + 					
					"<A HREF='javascript:void(0);' " +
						"onMouseOver=\"return true;\" " +
						"onMouseOut=\"window.status=' '; return true;\" " 
						if (this.returnFunction!=null)
						{
							vCode += "onClick=\"" + this.returnFunction + "('" + 
							this.format_data(vDay) + "','"+this.idForm +
							"');";
							if (!this.mantenerAbierto){ 
								if (this.tdId != null && this.tdId != ""){
									vCode += "\"";
								}else{
									vCode += "this.ggPosX=-1;this.ggPosY=-1;nd();nd();\"";
								}
							}else{
								vCode += "this.firstChild.className='calDivBodyDataToday';\"";
							}
						}
						//vCode += ">" + 
						vCode += " title=\""+ this.getTituloFecha(vDate) +"\" >" +
						this.format_day(vDay) + 
						"</A>" + 
						"</TD>";
			}else{
				vCode = vCode + "<TD class='" + this.write_weekend_string(j) + " " + this.write_today_string(vDay) + "'>" + 
					"<A HREF='javascript:void(0);' " +
						"onMouseOver=\"return true;\" " +
						"onMouseOut=\"window.status=' '; return true;\" " 
						if (this.returnFunction!=null)
						{
							vCode += "onClick=\"" + this.returnFunction + "('" + 
							this.format_data(vDay) + "','"+this.idForm +
							"')";
							if (!this.mantenerAbierto){ 
								if (this.tdId != null && this.tdId != ""){
									vCode += "\"";
								}else{
									vCode += "this.ggPosX=-1;this.ggPosY=-1;nd();nd();\"";
								}
							}else{
								vCode += "this.firstChild.className='calDivBodyDataToday';\"";
							}
						}	
						vCode += " title=\""+ this.getTituloFecha(vDate) +"\" >" +
						vDay + 
						"</A>" + 
						"</TD>";
			}
		}
		vDay=vDay + 1;
	}
	vCode = vCode + "</TR>";	
	// Write the rest of the weeks
	for (k=2; k<7; k++) {
		vCode = vCode + "<TR>";

		for (j=1; j<=7; j++) {
			if(this.format_day(vDay) =="-1"){
				vCode = vCode + "<TD class='" + this.write_weekend_string(j) + 
				"'><span class='" + this.daysPreviosNumberFont + "'>" + vDay + "</span></TD>";
			}else{
				if(this.format_day(vDay) !=""){
	vDate.setDate(vDay);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);					
					vCode = vCode + "<TD class='" + this.write_weekend_string(j) + " " + this.write_today_string(vDay) + "'>" + 
						"<A HREF='javascript:void(0);' " +
							"onMouseOver=\"return true;\" " +
							"onMouseOut=\"window.status=' '; return true;\" " 							
							if (this.returnFunction!=null)
							{
								vCode += "onClick=\"" + this.returnFunction + "('" + 
								this.format_data(vDay) + "','"+this.idForm +
								"');" 
								if (!this.mantenerAbierto){ 
									if (this.tdId != null && this.tdId != ""){
										vCode += "\"";
									}else{
										vCode += "this.ggPosX=-1;this.ggPosY=-1;nd();nd();\"";
									}
								}else{
								vCode += "this.firstChild.className='calDivBodyDataToday';\"";
								}
							}
							vCode += " title=\""+ this.getTituloFecha(vDate) +"\" >" + 
							this.format_day(vDay) + 
						"</A>" + 
						"</TD>";
				}else{
					vCode = vCode + "<TD class='" + this.write_weekend_string(j) + " " + this.write_today_string(vDay) + "'>" + 
						"<A HREF='javascript:void(0);' " +
							"onMouseOver=\"return true;\" " +
							"onMouseOut=\"window.status=' '; return true;\" " 
							if (this.returnFunction!=null)
							{
								vCode += "onClick=\"" + this.returnFunction + "('" + 
								this.format_data(vDay) + "','"+this.idForm +
								"');";
								if (!this.mantenerAbierto){ 
									if (this.tdId != null && this.tdId != ""){
										vCode += "\"";
									}else{
										vCode += "this.ggPosX=-1;this.ggPosY=-1;nd();nd();\"";
									}
								}else{
								vCode += "this.firstChild.className='calDivBodyDataToday';\"";
								}
							}
							vCode += ">" + 
							vDay + 
							"</A>" + 
							"</TD>";
				}
			}
			vDay=vDay + 1;

			if (vDay > vLastDay) {
				vOnLastDay = 1;
				break;
			}
		}

		if (j == 7)
			vCode = vCode + "</TR>";
		if (vOnLastDay == 1)
			break;
	}
	
	// Fill up the rest of last week with proper blanks, so that we get proper square blocks
	for (m=1; m<=(7-j); m++) {
		if (this.gYearly)
			vCode = vCode + "<TD class='" + this.write_weekend_string(j+m) + 
			"'></TD>";
		else
			vCode = vCode + "<TD class='" + this.write_weekend_string(j+m) + 
			"'><span>&nbsp;</span></TD>";
	}
	//alert(vCode)
	vCode += "</table></div>";
	return vCode;
}
function fGetTituloFecha(vdate)
{
    var str =""; 
    if (this.esFechaOk(this.listaFechas,vdate))
        str = this.txt +";"; 
    if (this.esFechaOk(this.listaFechasMinimos,vdate))
        str += this.txtMinimos +";"; 
    if (this.esFechaOk(this.listaFechasPromocion,vdate))
        str += this.txtPromocion +";"; 
    return str; 
}

function fFormat_day(vday)
{
	var vNowDay = this.gNow.getDate();
	var vNowMonth = this.gNow.getMonth();
	var vNowYear = this.gNow.getFullYear();
	
	var aux = this.gMonth+1

	var oFec = parseDate(vday+"/"+aux+"/"+this.gYear)
	oFec.setHours(0)
	
	if(oFec<gMin)
		return "-1";
	
	
	//alert(vday);
	if(this.listaFechas==null)
	{
		if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
		{
				return ("<SPAN class='calDivBodyDataToday'>" + vday + "</SPAN>");
		}		
		return "<SPAN class='calDivBodyDataNoToday'>" + vday + "</SPAN>";
	}
	else{
	  
		var ok=false
		var dia,mes,any;
		for (var i=0; i<this.listaFechas.length; i++)
		{
			n = this.listaFechas[i];
			dia = n.substring(0,2);
			mes = parseInt(n.substring(3,5),10);
			any = parseInt(n.substring(6,10),10); 
		    odate = new Date(any, mes-1, dia)
			
			if((oFec.getDate()==odate.getDate()) && (oFec.getMonth()==odate.getMonth()) && (oFec.getFullYear()==odate.getFullYear())){
				ok=true
			}
		}		
	
		if(!ok)
		{
			return "-1";
		}
		else if (this.esFechaOk(this.listaFechasPromocion,oFec))
        {
			return ("<SPAN class='calDivBodyDataNoTodayPromocion'>" + vday + "</SPAN>") 
        }
        else if (this.esFechaOk(this.listaFechasMinimos,oFec))
        {
			return ("<SPAN class='calDivBodyDataNoTodayMinimo'>" + vday + "</SPAN>") 
        }
        else if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
		{
			return ("<SPAN class='calDivBodyDataNoToday'>" + vday + "</SPAN>");
		}	
		
		return ("<SPAN class='calDivBodyDataNoTodayDestacado'>" + vday + "</SPAN>")
	}
}

function fEsFechaOk(arrayFechas,fecha)
{
    
    if (arrayFechas!=null && arrayFechas.length>0)
	{
	    for (i =0; i<arrayFechas.length;i++)
	    {
	        n = arrayFechas[i];
	        dia = n.substring(0,2);
	        mes = parseInt(n.substring(3,5),10);
	        any = parseInt(n.substring(6,10),10); 

            odate = new Date(any, mes-1, dia)
	        if((fecha.getDate()==odate.getDate()) && (fecha.getMonth()==odate.getMonth()) && (fecha.getFullYear()==odate.getFullYear()))
	        {return true ; }
	    }
	}
	return false; 
    
}

function fFormat_css(vday,vdate) 
{
	var vNowDay = this.gNow.getDate();
	var vNowMonth = this.gNow.getMonth();
	var vNowYear = this.gNow.getFullYear();
	var aux = this.gMonth+1

	var oFec = parseDate(vday+"/"+aux+"/"+this.gYear)
	oFec.setHours(0)
	
	if(oFec<gMin)
		return "-1";

	if(this.listaFechas==null){
		return this.write_weekend_string(this.getDia(vdate))
	}
	//	alert(this.listaFechas.length); 
		
	var ok=false
	var dia,mes,any;
	for (var i=0; i<this.listaFechas.length; i++)
	{
		n = this.listaFechas[i];
		//alert(n.text)
		dia = n.substring(0,2);
		mes = parseInt(n.substring(3,5),10);
		any = parseInt(n.substring(6,10),10); 
		
		//alert(any); 
		
//		if ((mes<tmonth) && (tmonth-1!=mes))
//	      any = tyear + 1;
//		else
//	      any = tyear;
		
	    odate = new Date(any, mes-1, dia)
		//alert(odate)
		//alert(((odate-hoy)/1000/60/60/24))
		if((oFec.getDate()==odate.getDate()) && (oFec.getMonth()==odate.getMonth()) && (oFec.getFullYear()==odate.getFullYear())){			
			ok=true
		}
	}		

	//if(!ok)
		return this.write_weekend_string(this.getDia(vdate))					
	/*else
		return ("class='" + this.squareColor + "1'");*/
}

function fWrite_today_string(vday) 
{
	if (this.todayBackgroundColor != "") {
		var vNowDay = this.gNow.getDate();
		var vNowMonth = this.gNow.getMonth();
		var vNowYear = this.gNow.getFullYear();
		var aux = this.gMonth+1
	
		if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
			{
					return (" class='calDivBodyDataTDToday'");
			}	
		else
			{
					return "";
			}	
	}	
	return ""
}

function fWrite_weekend_string(vday) {
	var x;
	
	// Return special formatting for the weekend day.
	for (x=0; x<2; x++) 
	{
		 if ((vday == 6) || (vday == 7))
		{
			return ("calDivBodyDataWeekend");
		}
		else
		{
			return ("calDivBodyDataDay");
		}
	}
}

function fFormat_data(p_day)
{
	var vData;
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vMon = this.calendarGetMonth(this.gMonth).substr(0,3).toUpperCase();
	var vFMon = this.calendarGetMonth(this.gMonth).toUpperCase();
	var vY4 = new String(this.gYear);
	var vY2 = new String(this.gYear.substr(2,2));
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

	switch (this.gFormat) {
		case "MM\/DD\/YYYY" :
			vData = vMonth + "\/" + vDD + "\/" + vY4;
			break;
		case "MM\/DD\/YY" :
			vData = vMonth + "\/" + vDD + "\/" + vY2;
			break;
		case "MM-DD-YYYY" :
			vData = vMonth + "-" + vDD + "-" + vY4;
			break;
		case "YYYY-MM-DD" :
			vData = vY4 + "-" + vMonth + "-" + vDD;
			break;
		case "MM-DD-YY" :
			vData = vMonth + "-" + vDD + "-" + vY2;
			break;
		case "DD\/MON\/YYYY" :
			vData = vDD + "\/" + vMon + "\/" + vY4;
			break;
		case "DD\/MON\/YY" :
			vData = vDD + "\/" + vMon + "\/" + vY2;
			break;
		case "DD-MON-YYYY" :
			vData = vDD + "-" + vMon + "-" + vY4;
			break;
		case "DD-MON-YY" :
			vData = vDD + "-" + vMon + "-" + vY2;
			break;
		case "DD\/MONTH\/YYYY" :
			vData = vDD + "\/" + vFMon + "\/" + vY4;
			break;
		case "DD\/MONTH\/YY" :
			vData = vDD + "\/" + vFMon + "\/" + vY2;
			break;
		case "DD-MONTH-YYYY" :
			vData = vDD + "-" + vFMon + "-" + vY4;
			break;
		case "DD-MONTH-YY" :
			vData = vDD + "-" + vFMon + "-" + vY2;
			break;
		case "DD\/MM\/YYYY" :
			vData = vDD + "\/" + vMonth + "\/" + vY4;
			break;
		case "DD\/MM\/YY" :
			vData = vDD + "\/" + vMonth + "\/" + vY2;
			break;
		case "DD-MM-YYYY" :
			vData = vDD + "-" + vMonth + "-" + vY4;
			break;
		case "DD-MM-YY" :
			vData = vDD + "-" + vMonth + "-" + vY2;
			break;
		default :
			vData = vMonth + "\/" + vDD + "\/" + vY4;
	}

	return vData;
}

// Construye el calendario
function fBuild(p_item, p_month, p_year, p_format) {

	this.calendarMethod(p_item, p_month, p_year, p_format);
 

	// initialize the content string
	this.ggWinContent = "";

	// Choose appropriate show function
	if (this.gYearly) {
		// and, since the yearly calendar is so large, override the positioning and fontsize
		// warning: in IE6, it appears that "select" fields on the form will still show
		//	through the "over" div; Note: you can set these variables as part of the onClick
		//	javascript code before you call the show_yearly_calendar function
		if (this.ggPosX == -1) this.ggPosX = 10;
		if (this.ggPosY == -1) this.ggPosY = 10;
		if (this.fontsize == 8) this.fontsize = 6;
		// generate the calendar
		this.showY();
	}else{
		this.show();
	}
		
		
	// if this is the first calendar popup, use autopositioning with an offset
	if (this.ggPosX == -1 && this.ggPosY == -1) {
		//Si el id del td viene informado, no lo mostramos en popup, lo incrustamos en la tabla
		if (this.tdId != null && this.tdId != ""){
			document.getElementById(this.tdId).innerHTML = this.ggWinContent;
		}else{
		    overlib(this.ggWinContent, AUTOSTATUSCAP, STICKY, CLOSECLICK, CSSSTYLE,
				TEXTSIZEUNIT, "pt", TEXTSIZE, 8, CAPTIONSIZEUNIT, "pt", CAPTIONSIZE, 8, CLOSESIZEUNIT, "pt", CLOSESIZE, 8,
				CAPTION, "Selecciona una fecha", this.offSetX, -120 , this.offSetY, 120, BGCOLOR, this.outerTableColor);
			// save where the 'over' div ended up; we want to stay in the same place if the user
			//	clicks on one of the year or month navigation links
			if ((ns4) || (ie4) ) {
				this.ggPosX = parseInt(over.left,10);
				this.ggPosY = parseInt(over.top,10);
			}else if (ns6) {
				this.ggPosX = parseInt(over.style.left,10);
				this.ggPosY = parseInt(over.style.top,10);
			}
		}
		
	}else {
		//Si el id del td viene informado, no lo mostramos en popup, lo incrustamos en la tabla
		if (this.tdId != null && this.tdId != ""){
			document.getElementById(this.tdId).innerHTML = this.ggWinContent;			
		}else{
			// we have a saved X & Y position, so use those with the FIXX and FIXY options
	 		overlib(this.ggWinContent, AUTOSTATUSCAP, STICKY, CLOSECLICK, CSSSTYLE,
				TEXTSIZEUNIT, "pt", TEXTSIZE, 8, CAPTIONSIZEUNIT, "pt", CAPTIONSIZE, 8, CLOSESIZEUNIT, "pt", CLOSESIZE, 8,
				CAPTION, "Selecciona una fecha", FIXX, this.ggPosX, FIXY, this.ggPosY);
		}
	}
	
	//window.scroll(ggPosX, ggPosY);
}

// ------------------------------------------------------------------
// Utility functions for parsing in getDateFromFormat()
// ------------------------------------------------------------------
function _isInteger(val) {
	var digits="1234567890";
	for (var i=0; i < val.length; i++) {
		if (digits.indexOf(val.charAt(i))==-1) { return false; }
		}
	return true;
}

function _getInt(str,i,minlength,maxlength) {
	for (var x=maxlength; x>=minlength; x--) {
		var token=str.substring(i,i+x);
		if (token.length < minlength) { return null; }
		if (_isInteger(token)) { return token; }
		}
	return null;
}
	
// ------------------------------------------------------------------
// getDateFromFormat( date_string , format_string )
//
// This function takes a date string and a format string. It matches
// If the date string matches the format string, it returns the 
// getTime() of the date. If it does not match, it returns 0.
// ------------------------------------------------------------------
function getDateFromFormat(val,format) {
//alert(); 

	val=val+"";
	format=format+"";
	var i_val=0;
	var i_format=0;
	var c="";
	var token="";
	var token2="";
	var x,y;
	var now=new Date();
	var year=now.getFullYear();
	var month=now.getMonth()+1;
	var date=1;
	var hh=now.getHours();
	var mm=now.getMinutes();
	var ss=now.getSeconds();
	var ampm="";

	while (i_format < format.length) {
		// Get next token from format string
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		// Extract contents of value based on format token
		if (token=="yyyy" || token=="yy" || token=="y") {
			if (token=="yyyy") { x=4;y=4; }
			if (token=="yy")   { x=2;y=2; }
			if (token=="y")    { x=2;y=4; }
			year=_getInt(val,i_val,x,y);
			if (year==null) { return 0; }
			i_val += year.length;
			if (year.length==2) {
				if (year > 70) { year=1900+(year-0); }
				else { year=2000+(year-0); }
				}
			}
		else if (token=="MMM"||token=="NNN"){
			month=0;
			for (var i=0; i<MONTH_NAMES.length; i++) {
				var month_name=MONTH_NAMES[i];
				if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) {
					if (token=="MMM"||(token=="NNN"&&i>11)) {
						month=i+1;
						if (month>12) { month -= 12; }
						i_val += month_name.length;
						break;
						}
					}
				}
			if ((month < 1)||(month>12)){return 0;}
			}
		else if (token=="EE"||token=="E"){
			for (var i=0; i<DAY_NAMES.length; i++) {
				var day_name=DAY_NAMES[i];
				if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) {
					i_val += day_name.length;
					break;
					}
				}
			}
		else if (token=="MM"||token=="M") {
			month=_getInt(val,i_val,token.length,2);
			if(month==null||(month<1)||(month>12)){return 0;}
			i_val+=month.length;}
		else if (token=="dd"||token=="d") {
			date=_getInt(val,i_val,token.length,2);
			if(date==null||(date<1)||(date>31)){return 0;}
			i_val+=date.length;}
		else if (token=="hh"||token=="h") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<1)||(hh>12)){return 0;}
			i_val+=hh.length;}
		else if (token=="HH"||token=="H") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<0)||(hh>23)){return 0;}
			i_val+=hh.length;}
		else if (token=="KK"||token=="K") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<0)||(hh>11)){return 0;}
			i_val+=hh.length;}
		else if (token=="kk"||token=="k") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<1)||(hh>24)){return 0;}
			i_val+=hh.length;hh--;}
		else if (token=="mm"||token=="m") {
			mm=_getInt(val,i_val,token.length,2);
			if(mm==null||(mm<0)||(mm>59)){return 0;}
			i_val+=mm.length;}
		else if (token=="ss"||token=="s") {
			ss=_getInt(val,i_val,token.length,2);
			if(ss==null||(ss<0)||(ss>59)){return 0;}
			i_val+=ss.length;}
		else if (token=="a") {
			if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";}
			else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";}
			else {return 0;}
			i_val+=2;}
		else {
			if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
			else {i_val+=token.length;}
			}
		}
	// If there are any trailing characters left in the value, it doesn't match
	if (i_val != val.length) { return 0; }
	// Is date valid for month?
	if (month==2) {
		// Check for leap year
		if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
			if (date > 29){ return 0; }
			}
		else { if (date > 28) { return 0; } }
		}
	if ((month==4)||(month==6)||(month==9)||(month==11)) {
		if (date > 30) { return 0; }
		}
	// Correct hours value
	if (hh<12 && ampm=="PM") { hh=hh-0+12; }
	else if (hh>11 && ampm=="AM") { hh-=12; }
	var newdate=new Date(year,month-1,date,hh,mm,ss);
	return newdate.getTime();
}

var MONTH_NAMES=new Array('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre','Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic');
var DAY_NAMES=new Array('Domingo','Lunes','Martes','Miercoles','Jueves','Viernes','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');

function parseDate(val)
{    
    //var preferEuro=(arguments.length==2)?arguments[1]:false;
    generalFormats=new Array('d-M-y','d MMM y','d MMM,y','d-MMM-y','d MMM','d M y','d/MMM/y','d/MMM','dMy','dMMMy','d.M.Y','d.MMM.y');
    monthFirst=new Array('M/d/y','M-d-y','M.d.y','MMM-d','M/d','M-d','M d','Md','M.d');
    dateFirst =new Array('d/M/y','d-M-y','d.M.y','d-MMM','d/M','d-M','d M','dM','d.M');
    var checkList=new Array('generalFormats','dateFirst','monthFirst');
    var d=null;
    for (var i=0; i<checkList.length; i++) {
	    var l=window[checkList[i]];
	    for (var j=0; j<l.length; j++) {
		    d=getDateFromFormat(val,l[j]);
		    if (d!=0) { return new Date(d); }
		    }
	    }
    return null;
}

function fShow_calendar(p_item,fmin,fechas) {
	
	/* 
		p_month : 0-11 for Jan-Dec; 12 for All Months.
		p_year	: 4-digit year
		p_format: Date format (mm/dd/yyyy, dd/mm/yy, ...)
		p_item	: Return Item.
	*/	
	
    this.gNow = parseDate(eval( p_item + ".value"))
	//alert(this.gNow); 
	
	if(this.gNow==null)
		this.gNow=new Date()
		
	gMin=parseDate(fmin);
	gMin.setHours(0);
	this.listaFechas=fechas;
	//gMax=parseDate(fmax)
	//gMax.setHours(23)
	//alert(gMin)
	//alert(gMax)
	p_format = "DD/MM/YYYY"
	p_item = arguments[0];
	p_month = new String(this.gNow.getMonth());	
	p_year = new String(this.gNow.getFullYear().toString());
	/*if (arguments[1] == null)
		p_month = new String(gNow.getMonth());
	else
		p_month = arguments[1];
	if (arguments[2] == "" || arguments[2] == null)
		p_year = new String(gNow.getFullYear().toString());
	else
		p_year = arguments[2];
	if (arguments[3] == null)
		//p_format = "YYYY-MM-DD";
		p_format = "DD/MM/YYYY"
	else
		p_format = arguments[3];
	*/

	this.Build(p_item, p_month, p_year, p_format);

}
/*
Yearly Calendar Code Starts here
*/
function fShow_yearly_calendar() {
	// Load the defaults..
	//if (p_year == null || p_year == "")
	//	p_year = new String(gNow.getFullYear().toString());
	//if (p_format == null || p_format == "")
	//	p_format = "YYYY-MM-DD";

	p_item = arguments[0];
	if (arguments[1] == "" || arguments[1] == null)
		p_year = new String(this.gNow.getFullYear().toString());
	else
		p_year = arguments[1];
	if (arguments[2] == null)
		p_format = "YYYY-MM-DD";
	else
		p_format = arguments[2];


	this.Build(p_item, null, p_year, p_format);
}

