﻿
/*
Este codigo ha sido desarrollado y es mantenido por www.constructorasoftware.com
Para información sobre sus derechos de uso y demas por favor contactenos a info@constructorasoftware.com
*/
CCalendario.prototype.ClientID = new String();
CCalendario.prototype.calIzquierda = new String();
CCalendario.prototype.calDerecha = new String();
CCalendario.prototype.FechaMinima = new Date();
CCalendario.prototype.FechaMaxima = new Date();
CCalendario.prototype.FechaActual = new Date();
CCalendario.prototype.FechaMostrar = new Date();
CCalendario.prototype.FechaSeleccionar = new Date();
CCalendario.prototype.mostrarDiaAtras = true;
CCalendario.prototype.mostrarDiaAdelante = true;
CCalendario.prototype.mostrarMesAtras = true;
CCalendario.prototype.mostrarMesAdelante = true;
CCalendario.prototype.mostrarFechaMostrar = true;
CCalendario.prototype.Cultura = "es-CO";
CCalendario.prototype.nombreTarget = new Date();
CCalendario.prototype.accionClickCliente = function () {};

//NDSC
var arrayNDSCes = ["dom", "lun", "mar", "mie", "jue", "vie", "sab"];
var arrayNDSCen= ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
var arrayNDSCfr= ["dim", "lun", "mar", "mer", "jeu", "ven", "sam"];
//NDSL
var arrayNDSLes = ["domingo", "lunes", "martes", "miercoles", "jueves", "viernes", "sabado"];
var arrayNDSLen = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
var arrayNDSLfr = ["dimamche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"];
// NMC
var arrayNMCen = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug",  "sep",  "oct",  "nov",  "dec"];
var arrayNMCfr = ["jan", "fév", "mar", "avr", "mai", "jun", "jul", "aoû",  "sep",  "oct",  "nov",  "déc"];
var arrayNMCes = ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago",  "sep",  "oct",  "nov",  "dic"];
// NML
var arrayNMLes = ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto",  "septiembre",  "octubre",  "noviembre",  "diciembre"];
var arrayNMLen = ["january", "february", "march", "april", "may", "june", "july", "august",  "september",  "october",  "november",  "december"];
var arrayNMLfr = ["janvier", "février", "mars", "avril", "mai", "juin", "juilllet", "août",  "septembre",  "octobre",  "novembre",  "décembre"];
// DPM
var intDPMn = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var intDPMb = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];


// CALENDARIO
function CCalendario()
{};
//  NOMBRE CORTO DEL DIA DE LA SEMANA POR NUMERO 
CCalendario.prototype.NDSC= function (intNumDia)
{
    if(this.Cultura == "en-US") return arrayNDSCen[intNumDia];
    if(this.Cultura == "fr-FR") return arrayNDSCfr[intNumDia];
    // POR DEFECTO
    return arrayNDSCes[intNumDia];
}
//  NOMBRE LARGO DEL DIA DE LA SEMANA POR NUMERO 
CCalendario.prototype.NDSL= function (intNumDia)
{
    if(this.Cultura == "en-US") return arrayNDSLen[intNumDia];
    if(this.Cultura == "fr-FR") return arrayNDSLfr[intNumDia];
    // POR DEFECTO
    return arrayNDSLes[intNumDia];
}
//  NOMBRE CORTO DE UN MES POR NUMERO
CCalendario.prototype.NMC = function (intNumeroMes)
{
    if(this.Cultura == "en-US") return arrayNMCen[intNumeroMes];
    if(this.Cultura == "fr-FR") return arrayNMCfr[intNumeroMes];
    // POR DEFECTO
    return arrayNMCes[intNumeroMes];
}
//  NOMBRE LARGO DE UN MES POR NUMERO
CCalendario.prototype.NML=function (intNumeroMes)
{
    if(this.Cultura == "en-US") return arrayNMLen[intNumeroMes];
    if(this.Cultura == "fr-FR") return arrayNMLfr[intNumeroMes];
    // POR DEFECTO
    return arrayNMLes[intNumeroMes];
}
//  METODOS
CCalendario.prototype.debug = function(strDebug)
{
    var strContenido= new String();
    
    strContenido = document.getElementById(this.NombreControlDebug).innerHTML;
    strContenido += "<br />";
    strContenido += strDebug;
    
    document.getElementById(this.NombreControlDebug).innerHTML =  strContenido;
}
// AGREGA UNA CELDA PAR VALOR
CCalendario.prototype.HTMLfilaClaveValor = function (strClave, strValor)
{
    return "<tr><td>" + strClave + "</td><td>" + strValor + "</td></tr>";
}
// AGREGA UNA CELDA CON ESTILO, COLSPAN Y SCRIPT
CCalendario.prototype.HTMLCelda = function (strValor, intColSpan, strClass, strScript, strOnMouseOver, strOnMouseOut)
{
    var strAtributos = "";
    if(intColSpan != undefined) strAtributos+= " colspan = '" + intColSpan + "' ";
    if(strClass != undefined) strAtributos+= " class = '" + strClass + "' ";
    if(strScript!= undefined) strAtributos+= " " + strScript + " ";
    if(strOnMouseOver!= undefined) strAtributos+= " onmouseover = 'javascript:" + strOnMouseOver + "(this);' ";
    if(strOnMouseOut!= undefined) strAtributos+= " onmouseout = 'javascript:" + strOnMouseOut + "(this);' ";
    
    return  "<td " + strAtributos + ">" + strValor + "</td>";
}
//  MUEVE UN DIA HACIA ADELANTE LA FECHA POR MOSTRAR
CCalendario.prototype.setFecha = function (strFecha, blnLocal)
{
    var strFechaT = new String(strFecha);
    var strFechaA = strFechaT.split("|");
        
    this.FechaMostrar.setDate(strFechaA[0]);
    this.FechaMostrar.setMonth(strFechaA[1]);
    this.FechaMostrar.setFullYear(strFechaA[2]);
    
    var intMes = parseInt(strFechaA[1]);
    
//    if(this.ClientID == "oCctl00_ContentPlaceHolder1_C1_cd2_C1")
//    {
//        alert("Izquierda :"+this.calIzquierda);
//        alert("Derecha :"+this.calDerecha);
//    }
    
    // VERIFICA SI LA LLAMADA ES LOCAL
    if (blnLocal == null)
    {
        // FECHA SELECCIONAR LOCAL
        this.FechaSeleccionar = this.FechaMostrar;

        try
        {
            var objTarget = document.getElementById(this.nombreTarget);
            
            objTarget.value = this.NDSL(this.FechaSeleccionar.getDay()) + " " +
                            + this.FechaSeleccionar.getFullYear() + "/" 
                            + (parseInt(this.FechaSeleccionar.getMonth()) + 1) + "/" 
                            + this.FechaSeleccionar.getDate();
                            
            if(this.accionClickCliente != null)
                this.accionClickCliente();
            
        }
        catch(ex)
        {
            alert(ex);
        }
//        // POSTBACK A LADO SERVIDOR
//        doPostBackAsync(this.ClientID + '_btnFecha', this.FechaSeleccionar.getFullYear() + "/" 
//                                                   + this.FechaSeleccionar.getMonth() + "/" 
//                                                   + this.FechaSeleccionar.getDate() + "/" );


       

        // TIENE DEFINIDO CALENDARIO A LA IZQUIERDA
        if (this.calIzquierda != "")
        {
        
            strFecha = this.calIzquierda + ".FechaSeleccionar = this.FechaSeleccionar;";
            
            intMes -= 1;
            intAno = parseInt(strFechaA[2]);
            
            if(intMes < 0)
            {
                intMes = 11;
                intAno -= 1;
            } 
            strFecha += this.calIzquierda + ".setFecha(\'" + strFechaA[0] + "|" + intMes + "|" + intAno + "\', false)";
            eval(strFecha);
        }
        
        // TIENE DEFINIDO CALENDARIO A LA DERECHA
        if (this.calDerecha != "")
        {
            strFecha = this.calDerecha + ".FechaSeleccionar = this.FechaSeleccionar;";
            
            intMes += 1;
            intAno = parseInt(strFechaA[2]);
            
            if(intMes > 11)
            {
                intMes = 0;
                intAno += 1;
            } 
            
            strFecha += this.calDerecha + ".setFecha(\'" + strFechaA[0] + "|" + intMes + "|" + intAno + "\', false)";
            eval(strFecha);
        }
    }
    this.dibuja();
}
//  MUEVE UN AÑO HACIA ADELANTE LA FECHA POR MOSTRAR
CCalendario.prototype.setMes = function (intNumMes, blnLocal)
{
    this.FechaMostrar.setMonth(intNumMes);
    
    // VERIFICA SI LA LLAMADA ES LOCAL
    if (blnLocal == null)
    {
        // TIENE DEFINIDO CALENDARIO A LA IZQUIERDA
        if (this.calIzquierda != "")
        {
            intNumMes -= 1;
            strFecha = this.calIzquierda + ".setMes(\'" + intNumMes + "\', false)";
            eval(strFecha);
        }
        
        // TIENE DEFINIDO CALENDARIO A LA DERECHA
        if (this.calDerecha != "")
        {
            intNumMes += 1;
            strFecha = this.calDerecha + ".setMes(\'" + intNumMes + "\', false)";
            eval(strFecha);
        }
    }
    
    this.dibuja();
}
//  MUEVE UN AÑO HACIA ADELANTE LA FECHA POR MOSTRAR
CCalendario.prototype.AñoAdelante = function (intNumAño, blnLocal)
{
    this.FechaMostrar.setFullYear(this.FechaMostrar.getFullYear() + intNumAño);
    
    if (blnLocal == null)
    {
        // TIENE DEFINIDO CALENDARIO A LA IZQUIERDA
        if (this.calIzquierda != "")
        {
            intNumAño -= 1;
            strFecha = this.calIzquierda + ".AñoAdelante(\'" + intNumAño + "\', false)";
            eval(strFecha);
        }
        
        // TIENE DEFINIDO CALENDARIO A LA DERECHA
        if (this.calDerecha != "")
        {
            intNumAño += 1;
            strFecha = this.calDerecha + ".AñoAdelante(\'" + intNumAño + "\', false)";
            eval(strFecha);
        }
    }
    
    this.dibuja();
}
//  MUEVE UN AÑO HACIA ATRAS
CCalendario.prototype.AñoAtras = function (intNumAño, blnLocal)
{
    this.FechaMostrar.setFullYear(this.FechaMostrar.getFullYear() - intNumAño);
    
     // VERIFICA SI LA LLAMADA ES LOCAL
    if (blnLocal == null)
    {
        // TIENE DEFINIDO CALENDARIO A LA IZQUIERDA
        if (this.calIzquierda != "")
        {
            intNumAño -= 1;
            strFecha = this.calIzquierda + ".AñoAtras(\'" + intNumAño + "\', false)";
            eval(strFecha);
        }
        
        // TIENE DEFINIDO CALENDARIO A LA DERECHA
        if (this.calDerecha != "")
        {
            intNumAño += 1;
            strFecha = this.calDerecha + ".AñoAtras(\'" + intNumAño + "\', false)";
            eval(strFecha);
        }
    }
    
    this.dibuja();
}
//  MUEVE UN MES HACIA ADELANTE LA FECHA POR MOSTRAR
CCalendario.prototype.MesAdelante = function (intNumMes, blnLocal)
{
    intMes = parseInt(intNumMes);
    intNuevoMes = this.FechaMostrar.getMonth() + intMes;
    
    this.FechaMostrar.setMonth(intNuevoMes);
      
     // VERIFICA SI LA LLAMADA ES LOCAL
    if (blnLocal == null)
    {
        // TIENE DEFINIDO CALENDARIO A LA IZQUIERDA
        if (this.calIzquierda != "")
        {
            strFecha = this.calIzquierda + ".MesAdelante(\'" + intMes + "\', false)";
            eval(strFecha);
        }
        
        // TIENE DEFINIDO CALENDARIO A LA DERECHA
        if (this.calDerecha != "")
        {
            strFecha = this.calDerecha + ".MesAdelante(\'" + intMes + "\', false)";
            eval(strFecha);
        }
    }
    
    this.dibuja();
}
//  MUEVE UN MES HACIA ATRAS
CCalendario.prototype.MesAtras = function (intNumMes, blnLocal)
{
    intMes = parseInt(intNumMes);
    intNuevoMes = this.FechaMostrar.getMonth() - intMes;
    this.FechaMostrar.setMonth(intNuevoMes);

    // VERIFICA SI LA LLAMADA ES LOCAL
    if (blnLocal == null)
    {
        // TIENE DEFINIDO CALENDARIO A LA IZQUIERDA
        if (this.calIzquierda != "")
        {
            strFecha = this.calIzquierda + ".MesAtras(\'" + intMes + "\', false)";
            eval(strFecha);
        }
        
        // TIENE DEFINIDO CALENDARIO A LA DERECHA
        if (this.calDerecha != "")
        {
            strFecha = this.calDerecha + ".MesAtras(\'" + intMes + "\', false)";
            eval(strFecha);
        }
    }
    this.dibuja();
}
//  MUEVE UN DIA HACIA ADELANTE LA FECHA POR MOSTRAR
CCalendario.prototype.DiaAdelante = function (intNumDias, blnLocal)
{
    intDia = parseInt(intNumDias) + this.FechaMostrar.getDate(); 
    this.FechaMostrar.setDate(intDia);
    
    // VERIFICA SI LA LLAMADA ES LOCAL
    if (blnLocal == null)
    {
        // TIENE DEFINIDO CALENDARIO A LA IZQUIERDA
        if (this.calIzquierda != "")
        {
            strFecha = this.calIzquierda + ".DiaAdelante(\'" + intNumDias + "\', false)";
            eval(strFecha);
        }
        
        // TIENE DEFINIDO CALENDARIO A LA DERECHA
        if (this.calDerecha != "")
        {
            strFecha = this.calDerecha + ".DiaAdelante(\'" + intNumDias + "\', false)";
            eval(strFecha);
        }
    }
    
    this.dibuja();
}
//  MUEVE UN DIA HACIA ATRAS
CCalendario.prototype.DiaAtras = function (intNumDias, blnLocal)
{
    intDia = this.FechaMostrar.getDate() - parseInt(intNumDias) ; 
    this.FechaMostrar.setDate(intDia);
    
    // VERIFICA SI LA LLAMADA ES LOCAL
    if (blnLocal == null)
    {
        // TIENE DEFINIDO CALENDARIO A LA IZQUIERDA
        if (this.calIzquierda != "")
        {
            strFecha = this.calIzquierda + ".DiaAtras(\'" + intNumDias + "\', false)";
            eval(strFecha);
        }
        
        // TIENE DEFINIDO CALENDARIO A LA DERECHA
        if (this.calDerecha != "")
        {
            strFecha = this.calDerecha + ".DiaAtras(\'" + intNumDias + "\', false)";
            eval(strFecha);
        }
    }

    this.dibuja();
}
//  DIA DE LA SEMANA DEL PRIMER DIA DEL MES DE UN MES/AÑO
CCalendario.prototype.NumeroPrimerDiaMes = function (intNumMes, intAño)
{
    oFecha = new Date(intAño, intNumMes, 1);
    return oFecha.getDay();
}
//  NUMERO DE DIAS POR MES POR NUMERO
CCalendario.prototype.diasPorMes= function (intNumMes, blnEsBisiesto)
{
    if(blnEsBisiesto)
        return intDPMb[intNumMes];
    else
        return intDPMn[intNumMes];
}
//  NOMBRE CORTO DEL DIA DE LA SEMANA POR NUMERO 
CCalendario.prototype.esBisiesto= function (intAño)
{
    return ((intAño % 4 == 0 && intAño % 100 != 0) || intAño % 400 == 0) ? true : false;
}
// DIBUJA EL GADGET
CCalendario.prototype.dibuja = function()
{
    this.dibujaCalendario();
}
// DIBUJA EL CALENDARIO
CCalendario.prototype.dibujaCalendario = function()
{
try
{
    var intMesAnterior = 0;
    var intAñoAnterior = 0;
    var intMesSiguiente = 0;
    var intAñoSiguiente = 0;
    var intDia = 0;
    var strContenido = "";
    var intNumColumna = 0;
    var blnFinalizado = false;
    var intUltimoDiaMesAnterior =0;
    var intUltimoDiaMes =0;
    
    // NUMERO DE DIA DE LA SEMANA EN QUE CAE EL PRIMERO DEL MES 0D/6S
    var intNumPrimerDiaMes = this.NumeroPrimerDiaMes(this.FechaMostrar.getMonth(), this.FechaMostrar.getFullYear())
    
    if(this.FechaMostrar.getMonth() == 0)
    {
        intMesAnterior = 11;
        intAñoAnterior = this.FechaMostrar.getFullYear() - 1;
    }
    else
    {
        intMesAnterior = this.FechaMostrar.getMonth()-1;
        intAñoAnterior = this.FechaMostrar.getFullYear();
    }
	
    if(this.FechaMostrar.getMonth() == 11)
    {
        intMesSiguiente = 0;
        intAñoSiguiente = this.FechaMostrar.getFullYear() + 1;
    }
    else
    {
        intMesSiguiente = this.FechaMostrar.getMonth() + 1;
        intAñoSiguiente = this.FechaMostrar.getFullYear();
    }
	
    intUltimoDiaMesAnterior = this.diasPorMes(intMesAnterior, this.esBisiesto(intAñoAnterior));
    intUltimoDiaMes = this.diasPorMes(this.FechaMostrar.getMonth(), this.esBisiesto(this.FechaMostrar.getFullYear()));
    
    strContenido += "<table cellpadding='0'cellspacing ='0' class='tablaCalendario'><tr>";
    
    if ((!this.mostrarMesAtras) 
        | ((this.FechaMostrar.getMonth() <= this.FechaMinima.getMonth())) & (this.FechaMostrar.getFullYear() <= this.FechaMinima.getFullYear()))
        strContenido += this.HTMLCelda("", 1, "Cal_1", "");
    else
        strContenido += this.HTMLCelda("<<", 1, "Cal_1", "onclick=\'javascript:" + this.ClientID + ".MesAtras(1);\'" );
        
    
    if(this.mostrarDiaAtras)
        strContenido += this.HTMLCelda("<", 1, "Cal_1", "onclick=\'javascript:" + this.ClientID + ".DiaAtras(1);\'" );
    else
        strContenido += this.HTMLCelda("", 1, "Cal_1", "");
    
    strContenido += this.HTMLCelda(this.NML(this.FechaMostrar.getMonth()) + "/" + this.FechaMostrar.getFullYear().toString() , 3, "Cal_1", "");
       
    if(this.mostrarDiaAdelante)    
        strContenido += this.HTMLCelda(">", 1, "Cal_1", "onclick=\'javascript:" + this.ClientID + ".DiaAdelante(1);\'" );
    else
        strContenido += this.HTMLCelda("", 1, "Cal_1", "");
        
    if((!this.mostrarMesAdelante)
       | ((this.FechaMostrar.getMonth() >= this.FechaMaxima.getMonth())) & (this.FechaMostrar.getFullYear() >= this.FechaMaxima.getFullYear()))
        strContenido += this.HTMLCelda("", 1, "Cal_1", "");
    else
        strContenido += this.HTMLCelda(">>", 1, "Cal_1", "onclick=\'javascript:" + this.ClientID + ".MesAdelante(1);\'" );
        
        
    strContenido += "</tr><tr>";
    
    for(intDiaSemana = 0; intDiaSemana <= 6; intDiaSemana++)
    {
        strContenido += this.HTMLCelda(this.NDSC(intDiaSemana),
                                       1, "Cal_2", "");
    }
    strContenido += "</tr><tr>";
    
    strCalendario = new String("<table cellpadding='0' cellspacing ='0' class='tablaDias' ><tr >");
        
        
    while(!blnFinalizado)
    {
        // POSICION DE ARRANQUE
        intDia = intUltimoDiaMesAnterior -  intNumPrimerDiaMes + 1 ;
        
        
        // MES ANTERIOR ULTIMOS DIAS
        if(intNumPrimerDiaMes != 7)
            while(intDia <= intUltimoDiaMesAnterior)
            {
                // FECHA DIBUJANDO
                dteFecha = new Date(intAñoAnterior, intMesAnterior, intDia);
                strCalendario += this.HTMLCelda("P", 1, "Cal_3", "");
                
                intDia++;
                intNumColumna++;
            }    
        
        // MES ACTUAL
        intDia = 1;
        while(intDia <= intUltimoDiaMes)
        {
            // FECHA DE DIBUJO
            //strFecha = new String((this.FechaMostrar.getMonth() + 1) + "/" + intDia + "/" + this.FechaMostrar.getFullYear() + " 00:00:00");
            dteFecha = new Date();
            dteFecha.setFullYear(this.FechaMostrar.getFullYear(), this.FechaMostrar.getMonth(), intDia);
            dteFecha.setHours(0,0,0,0);
                       
                        
            if ((dteFecha >= this.FechaMinima) & (dteFecha <= this.FechaMaxima))
            {
                if ((dteFecha.getMonth() == this.FechaSeleccionar.getMonth()) & (dteFecha.getDate() == this.FechaSeleccionar.getDate()))
                    strCalendario += this.HTMLCelda(intDia, 1, "Cal_5", 
                    "onclick=\'javascript:" + this.ClientID + ".setFecha(\""+ intDia + "|" + this.FechaMostrar.getMonth() + "|"+ this.FechaMostrar.getFullYear() + "\");\'",
                    "calendario_onMouseOver", "calendario_onMouseOut");
                else
                    strCalendario += this.HTMLCelda(intDia, 1, "Cal_6", 
                    "onclick=\'javascript:" + this.ClientID + ".setFecha(\""+ intDia + "|" + this.FechaMostrar.getMonth() + "|"+ this.FechaMostrar.getFullYear() + "\");\'",
                    "calendario_onMouseOverSel", "calendario_onMouseOutSel");
            }
            else   
            {          
                //alert("Fecha minima :" + this.FechaMinima + " - fecha :" + dteFecha + " Fecha maxima :" + this.FechaMaxima );
                strCalendario += this.HTMLCelda("C", 1, "Cal_4", "");
             }   
            intDia++
            intNumColumna++;
            if(intNumColumna == 7)
            {
                strCalendario += "</tr><tr>";
                intNumColumna = 0;
            }
        }
        
        // MES SIGUIENTE, PRIMEROS DIAS
        intDia = 1;
        while(intNumColumna < 7)
        {
            strCalendario += this.HTMLCelda("N", 1, "Cal_4", "");
            intNumColumna++;
            intDia++
        }
        
        blnFinalizado = true;
    }
    strContenido += "<tr><td colspan='7' style='padding:3px;'>" + strCalendario + "</td></tr>";
    strContenido += "</tr>";
    
    if(this.mostrarFechaMostrar)
    {
        strContenido += "<tr>";
        strContenido += this.HTMLCelda(this.FechaMostrar.getDate()       + "/" + 
                                       this.NMC(this.FechaMostrar.getMonth())    + "/" + 
                                       this.FechaMostrar.getFullYear(), 7, "", "");
        strContenido += "</tr>";
    }
    strContenido += "</table>";
	
    document.getElementById(this.NombreControl).innerHTML =  strContenido;
 }
 catch(ex)
 {}
    
}

