/*
	componente para el manejo de fechas
	constructor
		Fecha :: Fecha() 
*/
	function Fecha()
	{
//															Atributos
		this.ainDays			= null;
		this.astMonths			= null;
		this.astFullMonths		= null
		this.inMonth			= 0;
		this.inDay				= 0;
		this.inYear				= 0;
		this.stLanguage			= "spanish";
//															Metodos
		this.setLanguage		= Fecha_setLanguage;
		this.getMonthName		= Fecha_getMonthName;
		this.getShortMonthName	= Fecha_getShortMonthName;
		this.getDayOfWeek		= Fecha_getDayOfWeek;
		this.getDayOfWeekName	= Fecha_getDayOfWeekName;
		this.getMonth			= Fecha_getMonth;
		this.getDay				= Fecha_getDay;
		this.getYear			= Fecha_getYear;
		this.parseDate			= Fecha_parseDate;
		this.getFullYear		= Fecha_getFullYear;
		this.toString			= Fecha_toString;
		this.clone				= Fecha_clone;
		this.incMonth			= Fecha_incMonth;
		this.incDay				= Fecha_incDay;
		this.incYear			= Fecha_incYear;
		this.setDate			= Fecha_setDate;
		this.getDate			= Fecha_getDate;
		this.compareTo			= Fecha_compareTo;
		this.boValidateNumbers	= Fecha_boValidateNumbers;
		this.boValidateEmpty	= Fecha_boValidateEmpty;
//															Constructor
		this.astMonths			= ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"];
		this.astFullMonths		= ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"];
		this.astEngMonths		= ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
		this.ainDays   			= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
		this.ainDaysOfWeek		= ["domingo", "lunes", "martes", "miercoles", "jueves", "viernes", "sabado"];
		this.ainEngDaysOfWeek	= ["sunday", "monday", "tuesday", "wenesday", "thursday", "fiday", "saturday"];
		var obHoy				= new Date();
		this.setDate( obHoy.getDate(), obHoy.getMonth() + 1, obHoy.getFullYear() )
	}
	
	function Fecha_boValidateEmpty( stCadena )
	{
		if(! /\S/.test( stCadena ))
		{
			return false;
		}
		return true;
	}	
	
	function Fecha_setLanguage( stLanguage )
	{
		if ( stLanguage == "spanish" || stLanguage == "english" )
		{
			this.stLanguage = stLanguage;
			return true
		}
		return false
	}
	
	function Fecha_boValidateNumbers( stString )
	{
		return !/\D/.test(stString);
	}
	
	function Fecha_getDayOfWeek()
	{
		var obTempDate	= new Date( this.getFullYear(), this.getMonth() - 1, this.getDay() )
		return obTempDate.getDay();
	}
	
	function Fecha_getDayOfWeekName()
	{
		if ( this.stLanguage == "spanish" )
		{
			return this.ainDaysOfWeek[ this.getDayOfWeek() ];
		}
		else
		{
			return this.ainEngDaysOfWeek[ this.getDayOfWeek() ];
		}
	}
/*
 	funcion que compara dos objetos del tipo Fecha
 		int Fecha :: compareTo( Fecha obFechaComp ) 
 	Parametros:
 		obFechaComp	fecha para comparar
 	Retorna
 		1 	si el parametro es menor
 		-1	si el parametro es mayor
 		0	en caso de ser iguales
 */
	function Fecha_compareTo( obFechaComp )
	{
		var stSelfYear	= this.getFullYear();
		var stSelfMonth	= this.inMonth > 9 ? this.inMonth : "0" + this.inMonth;
		var stSelfDay	= this.inDay > 9 ? this.inDay : "0" + this.inDay;
		var stDateYear	= obFechaComp.getFullYear();
		var stDateMonth	= obFechaComp.inMonth > 9 ? obFechaComp.inMonth : "0" + obFechaComp.inMonth;
		var stDateDay	= obFechaComp.inDay > 9 ? obFechaComp.inDay : "0" + obFechaComp.inDay;
		var inSelf		= eval ( "" + stSelfYear + stSelfMonth + stSelfDay );
		var inDate		= eval ( "" + stDateYear + stDateMonth + stDateDay );
		
		if ( inSelf > inDate )
		{
			return 1;
		}
		else if ( inSelf < inDate )
		{
			return -1;
		}
		else
		{
			return 0;
		}
	}
	
	/*
	 	Funcion que parsea la cadena fecha que tiene el formato de fecha
	 */
	function Fecha_parseDate( stDate, stFormato )
	{
		stFormato		= stFormato ? stFormato.toLowerCase() : "dma";
		var inMonth		= "";
		var inDay		= "";
		var inYear		= "";
		var astFecha	= null;
		var boBisiesto	= false;
		var inIdnYear	= stFormato.charAt(0) == "a" ? 0 : stFormato.charAt(1) == "a" ? 1 : 2;
		var inIdnMonth	= stFormato.charAt(0) == "m" ? 0 : stFormato.charAt(1) == "m" ? 1 : 2;
		var inIdnDay	= stFormato.charAt(0) == "d" ? 0 : stFormato.charAt(1) == "d" ? 1 : 2;
		
		stDate			= stDate.toLowerCase();
		astFecha		= stDate.split ( "-" );
		if ( astFecha.length != 3 )
		{
			astFecha	= stDate.split ( "/" );
			if ( astFecha.length != 3 )
			{
				astFecha	= stDate.split ( " de " );
				if ( astFecha.length != 3 )
				{
					return false;
				}
			}
		}
		
		if ( !this.boValidateNumbers( astFecha[inIdnDay] ) || !this.boValidateNumbers( astFecha[inIdnYear] ) )
		{
			return false;
		}
		if ( !this.boValidateEmpty( astFecha[inIdnDay] ) || !this.boValidateEmpty( astFecha[inIdnYear] ) )
		{
			return false;
		}
		if ( astFecha[inIdnYear].length != 2 && astFecha[inIdnYear].length != 4 )
		{
			return false;
		}
		if ( eval( astFecha[inIdnDay] ) == 0 )
		{
			return false;
		}
		if ( !this.boValidateNumbers( astFecha[inIdnMonth] ) )
		{ 
			var i = 0
			for ( i = 0; i < this.astMonths.length ; i++)
			{
				if ( this.astMonths[i] == astFecha[inIdnMonth] )
				{
					inMonth = i + 1;
					break;
				}
			}
			if ( i == this.astMonths.length )
			{
				for (var i = 0; i < this.astFullMonths.length ; i++)
				{
					if ( this.astFullMonths[i] == astFecha[inIdnMonth] )
					{
						inMonth = i + 1;
						break;
					}
				}
				if ( i == this.astMonths.length )
				{
					for (var i = 0; i < this.astFullMonths.length ; i++)
					{
						if ( this.astEngMonths[i] == astFecha[inIdnMonth] )
						{
							inMonth = i + 1;
							break;
						}
					}
					if ( i == this.astFullMonths.length )
					{
						return false;
					}
				}
			}
			
		}
		else
		{
			inMonth = eval( astFecha[inIdnMonth] );
		}
		inDay		= eval( astFecha[inIdnDay] );
		inYear		= eval( astFecha[inIdnYear] );
		if ( inYear < 100 )
		{
			inYear		= inYear > 40 ? inYear + 1900 : inYear + 2000;
		}
		boBisiesto 	= ((inYear % 4)==0 && ((inYear % 100)!=0 || (inYear % 400)==0)) ? true : false;
		if (boBisiesto)
		{
			this.ainDays[1] = 29;
		}
		if ( inMonth <= 12 && inDay <= this.ainDays[inMonth-1] )
		{
			this.inMonth	= inMonth;
			this.inYear		= inYear;
			this.inDay		= inDay;
			return true
		}
		return false;
	}
	
	function Fecha_getMonthName()
	{
		if ( this.stLanguage == "spanish" )
		{
			return this.astFullMonths[this.inMonth - 1];
		}
		else
		{
			return this.astEngMonths[ this.inMonth - 1 ];
		}
	}
	
	function Fecha_getShortMonthName()
	{
		return this.astMonths[this.inMonth - 1];
	}
	
	function Fecha_getMonth()
	{
		return this.inMonth;
	}
	
	function Fecha_getDay()
	{
		return this.inDay;
	}
	
	function Fecha_getFullYear()
	{
		return this.inYear;
	}
	
	function Fecha_getYear()
	{
		var stShortYear = "" + this.inYear;
		while ( stShortYear.length < 2 )
		{
			stShortYear = "0" + stShortYear;
		}
		return stShortYear.substring( stShortYear.length - 2, stShortYear.length )
	}
	
	function Fecha_toString()
	{
		return this.getDate();
	}
	
	function Fecha_clone()
	{
		var obCopy	= new Fecha();
		obCopy.parseDate(this.toString());
		return obCopy;
	}
	
	function Fecha_incYear( inYears )
	{
		inYears	= inYears || 1;
		this.inYear	+= inYears;
	}
	
	function Fecha_incMonth( inMonths )
	{
		inMonths 		= inMonths || 1;
		this.inMonth 	+= inMonths;
		if (this.inMonth <= 0)
		{
			while(this.inMonth <= 0)
			{
				this.inMonth += 12;
				this.incYear(-1);
			}
		}
		else if(this.inMonth > 12)
		{
			while(this.inMonth >12)
			{
				this.inMonth -= 12;
				this.incYear(1);
			}
		}
		if (this.inDay > this.ainDays[this.inMonth - 1])
		{
			this.inDay = this.ainDays[this.inMonth - 1]
			if (this.inMonth == 2)
				this.inDay = 28
		}
	}
	
	function Fecha_incDay(inDays)
	{
		inDays			= inDays || 1;
		var inDiaDelMes	= this.inDay;
		inDiaDelMes		+= inDays;
		if ( inDiaDelMes <= 0)
		{
			while( inDiaDelMes <= 0 )
			{
				this.incMonth(-1);
				inDiaDelMes += this.ainDays[this.getMonth() - 1];
			}
		}
		else if( inDiaDelMes > this.ainDays[this.getMonth() - 1])
		{
			while( inDiaDelMes > this.ainDays[this.getMonth() - 1])
			{
				inDiaDelMes -= this.ainDays[this.getMonth() - 1];
				this.incMonth(1);
			}
		}
		this.inDay = inDiaDelMes
	}
	
	function Fecha_setDate( inDay, stMonth, inYear )
	{
		return this.parseDate(inDay + "-" + stMonth + "-" + inYear)
	}
	
	function Fecha_getDate( stFormato )
	{
		var stDay	= this.getDay() > 9 ? this.getDay() : "0" + this.getDay();
		var stMonth	= this.getMonth() > 9 ? this.getMonth() : "0" + this.getMonth();
		switch ( stFormato )
		{
			case "dd-mm-aa":  				return stDay 					+ "-" 		+ stMonth 					+ "-" 		+ this.getYear(); 	break;
			case "dd/mm/aa":  				return stDay 					+ "/" 		+ stMonth 					+ "/" 		+ this.getYear(); 	break;
			case "dd de mm de aa":			return stDay 					+ " de " 	+ stMonth 					+ " de "	+ this.getYear(); 	break;
			
			case "dd-mes-aa": 				return stDay 					+ "-" 		+ this.getShortMonthName() 	+ "-" 		+ this.getYear(); 	break;
			case "dd/mes/aa": 				return stDay 					+ "/" 		+ this.getShortMonthName() 	+ "/" 		+ this.getYear(); 	break;
			case "dd de mes de aa":			return stDay 					+ " de " 	+ this.getShortMonthName() 	+ " de " 	+ this.getYear(); 	break;
			
			case "dd-mesCom-aa":			return stDay 					+ "-" 		+ this.getMonthName() 		+ "-" 		+ this.getYear(); 	break;
			case "dd/mesCom/aa":			return stDay 					+ "/" 		+ this.getMonthName() 		+ "/" 		+ this.getYear(); 	break;
			case "dd de mesCom de aa":		return stDay 					+ " de " 	+ this.getMonthName() 		+ " de " 	+ this.getYear(); 	break;
			
			case "dd-mm-aaaa":  			return stDay 					+ "-" 		+ stMonth 					+ "-" 		+ this.getFullYear(); break;
			case "dd/mm/aaaa":  			return stDay 					+ "/" 		+ stMonth 					+ "/" 		+ this.getFullYear(); break;
			case "dd de mm de aaaa":  		return stDay 					+ " de " 	+ stMonth 					+ " de " 	+ this.getFullYear(); break;
			
			case "dd-mes-aaaa": 			return stDay 					+ "-" 		+ this.getShortMonthName() 	+ "-" 		+ this.getFullYear(); break;
			case "dd/mes/aaaa": 			return stDay 					+ "/" 		+ this.getShortMonthName() 	+ "/" 		+ this.getFullYear(); break;
			case "dd de mes de aaaa": 		return stDay 					+ " de " 	+ this.getShortMonthName() 	+ " de " 	+ this.getFullYear(); break;
			
			case "dd-mesCom-aaaa":			return stDay 					+ "-" 		+ this.getMonthName() 		+ "-" 		+ this.getFullYear(); break;
			case "dd/mesCom/aaaa":			return stDay 					+ "/" 		+ this.getMonthName() 		+ "/" 		+ this.getFullYear(); break;
			case "dd de mesCom de aaaa":	return stDay 					+ " de " 	+ this.getMonthName() 		+ " de " 	+ this.getFullYear(); break;
			
			case "mm-dd-aa":  		return stMonth 					+ "-" + stDay 						+ "-" + this.getYear(); 	break;
			case "mm/dd/aa":  		return stMonth 					+ "/" + stDay 						+ "/" + this.getYear(); 	break;
			
			case "mes-dd-aa": 		return this.getShortMonthName() + "-" + stDay 						+ "-" + this.getYear(); 	break;
			case "mes/dd/aa": 		return this.getShortMonthName() + "/" + stDay 						+ "/" + this.getYear(); 	break;
			
			case "mesCom-dd-aa":	return this.getMonthName()	 	+ "-" + stDay 						+ "-" + this.getYear(); 	break;
			case "mesCom/dd/aa":	return this.getMonthName() 		+ "/" + stDay 						+ "/" + this.getYear(); 	break;
			
			case "mm-dd-aaaa":  	return stMonth 					+ "-" + stDay 						+ "-" + this.getFullYear(); break;
			case "mm/dd/aaaa":  	return stMonth 					+ "/" + stDay 						+ "/" + this.getFullYear(); break;
			
			case "mes-dd-aaaa": 	return this.getShortMonthName() + "-" + stDay 						+ "-" + this.getFullYear(); break;
			case "mes/dd/aaaa": 	return this.getShortMonthName() + "/" + stDay 						+ "/" + this.getFullYear(); break;
			
			case "mesCom-dd-aaaa":	return this.getMonthName() 		+ "-" + stDay 						+ "-" + this.getFullYear(); break;
			case "mesCom/dd/aaaa":	return this.getMonthName() 		+ "/" + stDay 						+ "/" + this.getFullYear(); break;
			default :  				return stDay + "/" + this.getMonthName() + "/" + this.getFullYear();
		}
		return stDay + "-" + this.getMonthName() + "-" + this.getFullYear();
	}
