﻿// JScript File

function seleccionFecha(name) {
    if (name) {
        this.name = name;
    }
    else {
        this.name = "seleccionFecha";
    }
    this.fIni = null;
    this.fFin = null;
    this.noches = null;
    this.formatoFecha = "dd/MM/yyyy";
    this.CambiandoFecha = 0;
}

seleccionFecha.prototype.CalcularDiasFecha = function(origen, inicial) {
    if ((this.CambiandoFecha == 0) && this.fFin) {
        this.CambiandoFecha = 1;
        var fInicial = StringToDate(this.fIni.value, this.formatoFecha);
        var fFinal = StringToDate(this.fFin.value, this.formatoFecha);
        var arrayFSal = this.fFin.value.split("/");
        var fSal = new Date(arrayFSal[2], arrayFSal[1] - 1, arrayFSal[0]);
        if (origen == "fecha") {
            if (inicial) {
                var dias = this.noches.options[this.noches.selectedIndex].value;
                fFinal = fInicial;
                for (i = 0; i < dias; i++) {
                    fFinal = addDay(fFinal);
                }
                this.fFin.value = DateToString(fFinal, this.formatoFecha);
            }
            else {
                var dias = 0;
                while (dias < 30 && fInicial < fFinal) {
                    dias++;
                    fFinal = removeDay(fFinal);
                }
                if (this.noches) {
                    this.noches.value = dias;
                }
                if (dias == 30) {
                    this.fFin.value = DateToString(fFinal, this.formatoFecha);
                }
            }
        }
        else if (origen == "dias") {
            var dias = this.noches.options[this.noches.selectedIndex].value;
            fFinal = fInicial;
            for (i = 0; i < dias; i++) {
                fFinal = addDay(fFinal);
            }
            this.fFin.value = DateToString(fFinal, this.formatoFecha);
        }
        this.CambiandoFecha = 0;
    }
}



function addDay(fecha) {
    var dia = fecha.getDate();
    var mes = fecha.getMonth() + 1;
    var ano = fecha.getFullYear();
    dia = dia + 1;
    if (dia > diasMes(mes, ano)) {
        dia = 1;
        mes++;
        if (mes > 12) {
            mes = 1;
            ano++;
        }
    }
    return new Date(ano, mes - 1, dia)
}


function removeDay(fecha) {
    var dia = fecha.getDate();
    var mes = fecha.getMonth() + 1;
    var ano = fecha.getFullYear();
    dia--;
    if (dia < 1) {
        mes--;
        if (mes < 1) {
            mes = 12;
            ano--;
        }
        dia = diasMes(mes, ano);
    }
    return new Date(ano, mes - 1, dia)
}





function diasMes(mes, ano) {
    switch (mes) {
        case 1:
            return 31;
            break;
        case 3:
            return 31;
            break;
        case 5:
            return 31;
            break;
        case 7:
            return 31;
            break;
        case 8:
            return 31;
            break;
        case 10:
            return 31;
            break;
        case 12:
            return 31;
            break;
        case 4:
            return 30
            break;
        case 6:
            return 30
            break;
        case 9:
            return 30
            break;
        case 11:
            return 30
            break;
        case 2:
            if (Math.floor(ano / 4) == (ano / 4)) {
                return 29
            } else {
                return 28
            }
            break;
    }
}

seleccionFecha.prototype.CalcularDiasFechaSinNoches = function() {
    var arrayFEnt = this.fIni.value.split("/");
    var fEnt = new Date(arrayFEnt[2], arrayFEnt[1] - 1, arrayFEnt[0]);
    var arrayFSal = this.fFin.value.split("/");
    var fSal = new Date(arrayFSal[2], arrayFSal[1] - 1, arrayFSal[0]);
    if (fEnt > fSal) {
        fSal = addDay(fEnt);
        this.fFin.value = DateToString(fSal, "dd/MM/yyyy");
    }
}
