﻿// JScript File
//function actualizarSelectNoches() {
//	$(".selectorNoches").each(function(){
//        var id = $(this).attr('id');
//	    var idFrags = id.split('_');
//	    //eliminamos el primer y ultimo elemento del array, para obtener la base de las IDs
//        idFrags.pop();
//	    idFrags.shift();
//	    var baseID = '';
//	    for (a in idFrags){
//		    if(idFrags[a] != '') baseID += '_' + idFrags[a];
//	    }
//	    alert(targetID);
//	    var selectDuracion = $('#' + baseID + '_noches');
//	    var fechaIni = $('#' + baseID + '_fIni').datepicker('getDate');
//	    var fechaFin = $('#' + baseID + '_fFin').datepicker('getDate');
//	    var diff = Math.ceil(fechaFin - fechaIni) / (1000*60*60*24);
//	    if(diff < 30){
//		    selectDuracion.val(diff);
//	    }else{
//		    selectDuracion.val('00');
//        }
//	});
//}

function comprobarDiaIni(fecha) {
    var d = fecha.getDate().toString();
    var m = (fecha.getMonth() + 1).toString();
    var y = fecha.getFullYear();
    if (d.length < 2) d = "0" + d;
    if (m.length < 2) m = "0" + m;
    var fechaStr = y + '/' + m + '/' + d;
    var idBase = $(this).getIdBase();
    var fechasValidasStr = $('#' + idBase + '_diasActivosIni').val();
    var fechasValidasArr = fechasValidasStr.split(",");
    
    if(jQuery.inArray(fechaStr, fechasValidasArr) == -1){
        return[false, ''];
    }else{
        return[true, ''];
    }
}

function comprobarDiaFin(fecha) {
    var d = fecha.getDate().toString();
    var m = (fecha.getMonth() + 1).toString();
    var y = fecha.getFullYear();
    if (d.length < 2) d = "0" + d;
    if (m.length < 2) m = "0" + m;
    var fechaStr = y + '/' + m + '/' + d;
    var idBase = $(this).getIdBase();
    var fechasValidasStr = $('#' + idBase + '_diasActivosFin').val();
    var fechasValidasArr = fechasValidasStr.split(",");

    if (jQuery.inArray(fechaStr, fechasValidasArr) == -1) {
        return [false, ''];
    } else {
        return [true, ''];
    }
}

$.fn.extend({
 getIdBase: function() {
    var idFrags = $(this).attr('id').split('_');
    //quitamos el primer y el ultimo segmento (vacio y especifico, respectivamente)
    idFrags.pop();
    idFrags.shift();
    var baseID = '';
    for (a in idFrags) {
        if (idFrags[a] != '') baseID += '_' + idFrags[a];
    }
    return baseID;
}
});


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);
        if (origen == "fechaNoches") {
            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);
        }
        else if (origen == "fecha") {
            if (fFinal <= fInicial) {
                fFinal = addDay(fInicial);
                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;
   }
}


