/////////////////////////////////////////////////////////////////
// FECHA.JS
// Funciones para el manejo de las fechas tipo Date de JavaScript
// (Created 13/07/2000) (c)Antonienko
// (Updated 04/10/2000) (c)Finwe Bug Fix  - PT_compara_fecha_actual
// (Updated 24/04/2001) (c)Finwe - PT_compara_fecha_actual_new
// (Updated 30/04/2001) (c)Finwe - HoraCorrecta
/////////////////////////////////////////////////////////////////


// FUNCION ArgsFecha2JSDate
// Devuelve el objeto Date de JS correspondiente a los parametros introducidos
// Parametros --> Deben ser todos de tipo number
function ArgsFecha2JSDate(dia,mes,anyo,hora,minuto,segundo)
{
	if (FechaCorrecta(dia,mes,anyo,hora,minuto,segundo))
	{
		fecha_temp = new Date()
		fecha_temp.setDate(dia)
		fecha_temp.setMonth(mes-1)
		fecha_temp.setYear(anyo)
		fecha_temp.setHours(hora)
		fecha_temp.setMinutes(minuto)
		fecha_temp.setSeconds(segundo)
		return fecha_temp
	}
	else return false
}


// FUNCION FechaCorrecta
// Comprueba que los parametros introducidos corresponda a un fecha correcta
// Parametros --> Deben ser todos de tipo number
function FechaCorrecta(dia,mes,anyo,hora,minuto,segundo) 			//v1.0
{
	if(isNaN(dia) || isNaN(mes) || isNaN(anyo) || isNaN(hora) || isNaN(minuto) || isNaN(segundo)) return false
	if(hora < 0 || hora > 23) return false
	if(minuto < 0 || minuto > 59) return false
	if(segundo < 0 || segundo > 59) return false
	if(dia < 1 || dia > 31) return false
	if(mes < 1 || mes > 12) return false
	if( ((mes==2)||(mes==4)||(mes==6)||(mes==9)||(mes==11)) && (dia==31) ) return false
	if((mes==2)){
		if(dia > 29) return false
		if(anyo%4 == 0) 
		{
			if( (anyo%100 == 0) && (anyo%400!=0) && (dia == 29)) return false
		}
		else if(dia == 29) return false
	}
	return true
}
	
// FUNCION HoraCorrecta
// Comprueba que los parametros introducidos corresponda a una hora correcta
// Parametros --> Deben ser todos de tipo number
function HoraCorrecta(hora,minuto,segundo) 			//v1.0
{
	if(isNaN(hora) || isNaN(minuto) || isNaN(segundo)) return false
	if(hora < 0 || hora > 23) return false
	if(minuto < 0 || minuto > 59) return false
	if(segundo < 0 || segundo > 59) return false
	return true
}

/*ATENCION LOS OBJETOS QUE HYA DEBAJO DE ESTE COMENTARIO LOS HE DEJADO POR COMPATIBILIDAD
DEL FICHERO CON LOS ANTIGUOS PROYECTOS, PERO DEBERIAN DEJAR DE USARSE*/
function comprueba_fecha(fecha)
{
	if (fecha != "" ) {
		dia = fecha.substring(0,2)
		mes = fecha.substring(3,5)
		anyo = fecha.substr(6)
		ndia = new Number(dia)
		nmes = new Number(mes)
		nanyo = new Number(anyo)
		if (isNaN(ndia) || isNaN(nmes) || isNaN(nanyo)) {
			return false
		}
		else {
			if (fecha.charAt(2) != '/' || fecha.charAt(5) != '/') {
			return false
			}
			else {
					if (dia < 1 || dia > 31 ) {return false}
					else {
						if (mes < 1 || mes > 12) {return false}
						else {
							if ((( mes==2 ) || ( mes==4) || ( mes==6 ) || ( mes==9) || ( mes==11)) && ( dia==31)) {return false}
							else {
								if (mes == 2) {
									if (dia>29) {return false}
									else {
										if ( anyo%4==0 ) {
											if (( anyo%100==0 ) && ( anyo%400!= 0) && ( dia== 29 )) {return false}
											else return true
										}
										else {
											if (dia == 29) {
												return false
											}
											else {
												return true
											}
										}
									}
								}
								else {
									return true
								}
							}
						}
					}
				
			}
		}
	}
	else return true
}

// Objeto que guarda la fecha que hay en la base de datos y la actual, en formato fecha de JavaScript,
// y ademas indica si la fecha es mayor, menor o igual a la fecha actual. Todas las fechas estan con
// hora minuto y segundo igual a 0
//  v1.1 (29/02/2000) (c)Antonienko
//  v1.2 (30/05/2000) (c)Finwe    Bub Fix: Error al comparar las fecha, compara con milisegundos y no hay metodo para 
//										   ponerlos a 0. Arreglado quitando los 3 ultimos valores que devuelve el 
//                                         get time y concatenarle 000


function PT_compara_fecha_actual(fecha_bd)
{
	this.fecha_act = new Date()
	this.fecha_act.setHours(0)
	this.fecha_act.setMinutes(0)
	this.fecha_act.setSeconds(0)
	fecha_act_mili = new String(this.fecha_act.getTime())
	fecha_act_mili = fecha_act_mili.substring(0,fecha_act_mili.length-3)
	fecha_act_mili = fecha_act_mili+"000"
	this.fecha_act.setTime(fecha_act_mili)
	 
	
		
	fecha_str = new String(fecha_bd)
	this.fecha_bd = new Date ()
	this.fecha_bd.setTime(Date.parse(fecha_str))
	
	
	if (fecha_str.match(null) ) {
		this.es_menor = false
		this.es_mayor = false
		this.es_igual = false
	}
	else {
		if (this.fecha_bd.getTime() == this.fecha_act.getTime()) {
			this.es_menor = false
			this.es_mayor = false
			this.es_igual = true
		}
		else if (this.fecha_bd.getTime() > this.fecha_act.getTime()) {
			this.es_menor = false
			this.es_mayor = true
			this.es_igual = false
		}
		else if (this.fecha_bd.getTime() < this.fecha_act.getTime()) {
			this.es_menor = true
			this.es_mayor = false
			this.es_igual = false
		}
	}
}

// Objeto que guarda la fecha que hay en la base de datos y la actual, en formato fecha de JavaScript,
// y ademas indica si la fecha es mayor, menor o igual a la fecha actual. Todas las fechas estan con
// hora minuto y segundo igual a 0
// Funcion que usa la nueva funcion setMilliseconds de la version 1.4 de JavaScript

function PT_compara_fecha_actual_new(fecha_bd)
{
	this.fecha_act = new Date()
	this.fecha_act.setHours(0)
	this.fecha_act.setMinutes(0)
	this.fecha_act.setSeconds(0)
	this.fecha_act.setMilliseconds(000); 
	this.fecha_act.setTime(this.fecha_act.getTime())
	 

	fecha_str = new String(fecha_bd)
	this.fecha_bd = new Date ()
	this.fecha_bd.setTime(Date.parse(fecha_str))
	
	
	if (fecha_str.match(null) ) {
		this.es_menor = false
		this.es_mayor = false
		this.es_igual = false
	}
	else {
		if (this.fecha_bd.getTime() == this.fecha_act.getTime()) {
			this.es_menor = false
			this.es_mayor = false
			this.es_igual = true
		}
		else if (this.fecha_bd.getTime() > this.fecha_act.getTime()) {
			this.es_menor = false
			this.es_mayor = true
			this.es_igual = false
		}
		else if (this.fecha_bd.getTime() < this.fecha_act.getTime()) {
			this.es_menor = true
			this.es_mayor = false
			this.es_igual = false
		}
	}
}

// Objeto que compara dos fechas, en formato fecha de JavaScript,
// y ademas indica si la fecha 1 es mayor, menor o igual a la fecha 2. Todas las fechas estan con
// hora minuto y segundo igual a 0
//  v1.5 (8/05/2000) (c)Antonienko
function PT_compara_fechas(fecha1,fecha2)
{
	fecha_str1 = new String(fecha1)
	this.fecha1 = new Date ()
	this.fecha1.setTime(Date.parse(fecha_str1))
		
	fecha_str2 = new String(fecha2)
	this.fecha2 = new Date ()
	this.fecha2.setTime(Date.parse(fecha_str2))
	
	if (fecha_str1.match(null) ) {
		this.es_menor = false
		this.es_mayor = false
		this.es_igual = false
	}
	else {
		if (fecha_str2.match(null) ) {
			this.es_menor = false
			this.es_mayor = false
			this.es_igual = false
		}

		else {
	
			if (this.fecha1.getTime() < this.fecha2.getTime()) {
				this.es_menor = true
				this.es_mayor = false
				this.es_igual = false
			}
			else if (this.fecha1.getTime() > this.fecha2.getTime()) {
				this.es_menor = false
				this.es_mayor = true
				this.es_igual = false
			}
			else if (this.fecha1.getTime() == this.fecha2.getTime()) {
				this.es_menor = false
				this.es_mayor = false
				this.es_igual = true
			}
		}
	}
}
// Dado un mes y un anyo devuelve el numero de dias que tiene ese mes
//  v1.0 (4/10/2000) (c)Finwe
function GetMonthDays(mes,anyo)
{
	if((mes==2)){
		if(anyo%4 == 0) 
		{
			if( (anyo%100 == 0) && (anyo%400!=0) ) return 28
			else return 29
		}
		else  return 28
	}
	else if( ((mes==2)||(mes==4)||(mes==6)||(mes==9)||(mes==11)) ) return 30
	else{
		return 31
	}
}

