﻿/* DATECONFIG class */
function DF_DateConfig(sLanguage)
{
	//sLanguage: english or español
	if (sLanguage == "english")
	{
		this.DF_MonthLongNames = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");
		this.DF_MonthShortNames = new Array ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		this.DF_DayLongNames = new Array ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
		this.DF_DayShortNames = new Array ("Su","Mo","Tu","We","Th","Fr","Sa");
	}
	else
	{
		this.DF_MonthLongNames = new Array ("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Setiembre","Octubre","Noviembre","Diciembre");
		this.DF_MonthShortNames = new Array ("Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Set","Oct","Nov","Dic");
		this.DF_DayLongNames = new Array ("Domingo","Lunes","Martes","Miércoles","Jueves","Viernes","Sábado");
		this.DF_DayShortNames = new Array ("Do","Lu","Ma","Mi","Ju","Vi","Sa");
	}
	this.DF_DaysInMonth = new Array (31,28,31,30,31,30,31,31,30,31,30,31);
	this.DF_DateFormatStrings = new Array ("d","dd","ddd","dddd","m","mm","mmm","mmmm","yy","yyyy");
	this.DF_DateFormatRegExp = new Array ("([1-9]|[12][0-9]|30|31)","(0[1-9]|[12][0-9]|30|31)","(" + this.DF_DayShortNames.join("|") + ")","(" + this.DF_DayLongNames.join("|") + ")","([1-9]|10|11|12)","(0[1-9]|10|11|12)","(" + this.DF_MonthShortNames.join("|") + ")","(" + this.DF_MonthLongNames.join("|") + ")","([0-9]{2})","([0-9]{4})");
}







/* DATE class */
function DF_Date(sFormat, iFromYear, iToYear, oConfig)
{
	this.format = sFormat;
	this.regexpformat = "";
	this.minyear = (iFromYear ? iFromYear : 0);
	this.maxyear = (iToYear ? ((iToYear >= this.minyear) ? iToYear : 3000) : 3000);
	this.config = ((oConfig != null) ? oConfig : oDF_DateConfig);
	this.date = new Date();
	this.getregularexpression = DF_DateGetRegularExpression;
	this.builddate = DF_DateBuildDate;
	this.parsedate = DF_DateParseDate;
	this.getdaysinmonth = DF_DateGetDaysInMonth;
	this.validdate = DF_DateValidDate;
	
	this.getregularexpression();
}

function DF_DateGetRegularExpression()
{
	var i = 0;
	var sRegExp = this.format;
	var oRegExp;
	var sSearch;
	for (i=0; i<this.config.DF_DateFormatStrings.length; i++)
	{
		sSearch = "\\{" + this.config.DF_DateFormatStrings[i] + "\\}";
		oRegExp = new RegExp(sSearch, "g");
		sRegExp = sRegExp.replace(oRegExp,this.config.DF_DateFormatRegExp[i]);
	}
	this.regexpformat = sRegExp;
}

function DF_DateBuildDate(iDay, iMonth, iYear)
{
	//year parameter is in 4 digit format
	if (!this.validdate(iDay, iMonth, iYear)) return "date incorrect";
	
	var sDay = "" + iDay;
	var sLongDay = ((iDay < 10) ? "0" : "") + iDay;
	var dDate = new Date(iYear, iMonth - 1, iDay);
	var iDayNameIndex = dDate.getDay();
	var sDayName = this.config.DF_DayShortNames[iDayNameIndex];
	var sLongDayName = this.config.DF_DayLongNames[iDayNameIndex];
	var sMonth = "" + iMonth;
	var sLongMonth = ((iMonth < 10) ? "0" : "") + iMonth;
	var sMonthName = this.config.DF_MonthShortNames[iMonth-1];
	var sLongMonthName = this.config.DF_MonthLongNames[iMonth-1];
	var sLongYear = "" + iYear;
	var sYear = sLongYear.substr(2);
	var arrValues = new Array (sDay,sLongDay,sDayName,sLongDayName,sMonth,sLongMonth,sMonthName,sLongMonthName,sYear,sLongYear);
	var i = 0;
	var sTemp = this.format;
	var oRegExp;
	var sSearch;
	for (i=0; i<this.config.DF_DateFormatStrings.length; i++)
	{
		sSearch = "\\{" + this.config.DF_DateFormatStrings[i] + "\\}";
		oRegExp = new RegExp(sSearch, "g");
		sTemp = sTemp.replace(oRegExp,arrValues[i]);
	}
	return sTemp;
}

function DF_DateParseDate(sDate)
{
	var dDate = null; //new Date();
	var oRegExp = new RegExp("^" + this.regexpformat + "$","gi");
	var arrResult = oRegExp.exec(sDate);
	var sFormat = "";
	var arrFormatResult;
	var oFormatRegExp;
	var i = 0;
	var j = 0;
	var iDay = -1;
	var iMonth = -1;
	var iYear = -1;
	if (arrResult)
	{
		sFormat = this.format;
		sFormat = sFormat.replace(/\{/g,"({");
		sFormat = sFormat.replace(/\}/g,"})");
		oFormatRegExp = new RegExp("^" + sFormat + "$","gi");
		arrFormatResult = oFormatRegExp.exec(this.format);
		if (arrFormatResult)
		{
			if (arrFormatResult.length == arrResult.length)
			{
				for (i = 1; i < arrFormatResult.length; i++)
				{
					if (iDay == -1)
					{
						if ((arrFormatResult[i] == "{d}") || (arrFormatResult[i] == "{dd}")) iDay = arrResult[i];
					}
					if (iMonth == -1)
					{
						if ((arrFormatResult[i] == "{m}") || (arrFormatResult[i] == "{mm}")) iMonth = arrResult[i];
						if (arrFormatResult[i] == "{mmm}")
						{
							for (j = 0; j < this.config.DF_MonthShortNames.length; j++)
							{
								if (this.config.DF_MonthShortNames[j].toLowerCase() == arrResult[i].toLowerCase())
								{
									iMonth = j + 1;
									break;
								}
							}
						}
						if (arrFormatResult[i] == "{mmmm}")
						{
							for (j = 0; j < this.config.DF_MonthLongNames.length; j++)
							{
								if (this.config.DF_MonthLongNames[j].toLowerCase() == arrResult[i].toLowerCase())
								{
									iMonth = j + 1;
									break;
								}
							}
						}
					}
					if (iYear == -1)
					{
						if ((arrFormatResult[i] == "{yy}") || (arrFormatResult[i] == "{yyyy}")) iYear = arrResult[i];
					}
				}
				if (iDay == -1) iDay = 1;
				if (iMonth == -1) iMonth = 1;
				if (iYear == -1) iYear = new Date().getFullYear();
				if (this.validdate(iDay, iMonth, iYear))
				{
					dDate = new Date(iYear, iMonth - 1, iDay);
				}
			}
		}
	}
	this.date = dDate;
	return dDate;
}

function DF_DateGetDaysInMonth(iMonth, iYear)
{
	var iDaysInMonth = this.config.DF_DaysInMonth[iMonth-1];
	if (iMonth == 2)
	{
		if ((((iYear % 4) == 0) && ((iYear % 100) != 0)) || ((iYear % 400) == 0))
		{
			iDaysInMonth += 1;
		}
	}
	return iDaysInMonth;
}

function DF_DateValidDate(iDay, iMonth, iYear)
{
	if ((iYear < this.minyear) || (iYear > this.maxyear)) return false;
	if ((iMonth < 1) || (iMonth > 12)) return false;
	if (iDay < 1) return false;
	if (iDay > this.getdaysinmonth(iMonth, iYear)) return false;
	
	return true;
}


oDF_DateConfig = new DF_DateConfig("español");