/******************************************************************************
 * Vinculo del logo hacia el home
 */
window.onload = function () {
    var ctrlForm = new ControlFormBusqueda(
        document.getElementById('operacionVenta'),
        document.getElementById('operacionAlquiler'),
        document.getElementById('showSelectPeriodo'),
        document.getElementById('selDesdeVenta'),
        document.getElementById('selHastaVenta'),
        document.getElementById('selDesdeAlquiler'),
        document.getElementById('selHastaAlquiler')
    );
};

function ControlFormBusqueda () { return this.construct.apply(this, arguments); };
ControlFormBusqueda.prototype = {
	estadoActual : null,
    operacionVenta : null,
    operacionAlquiler : null,
    showSelectPeriodo : null,
    selDesdeVenta : null,
    selHastaVenta : null,
    selDesdeAlquiler : null,
    selHastaAlquiler : null,

    construct : function (operacionVenta, operacionAlquiler, showSelectPeriodo,
                selDesdeVenta, selHastaVenta, selDesdeAlquiler, selHastaAlquiler) {
        this.operacionVenta = operacionVenta;
        this.operacionAlquiler = operacionAlquiler;
        this.showSelectPeriodo = showSelectPeriodo;
        this.selDesdeVenta = selDesdeVenta;
        this.selHastaVenta = selHastaVenta;
        this.selDesdeAlquiler = selDesdeAlquiler;
        this.selHastaAlquiler = selHastaAlquiler;

        this.prepararOptions();
    },
    cambioEstado : function () {
        if (this.operacionVenta.checked && this.estadoActual != 'venta') {
            this.estadoActual = 'venta';
            return 1;
        } else {
            if (this.operacionAlquiler.checked && this.estadoActual != 'alquiler') {
                this.estadoActual = 'alquiler';
                return 2;
            }
        }
        return 0;
    },
    prepararOptions : function () {
        if (this.operacionVenta && this.operacionAlquiler && this.showSelectPeriodo) {
        	// se inicia el estado del fomulario
            switch ( this.cambioEstado() ) {
                case 1: this.formByVenta(); break;
                case 2: this.formByAlquiler(); break;
            }

            // los eventos de los options
            var tthis = this;
            this.operacionVenta.onclick = function () {
                if ( tthis.cambioEstado() === 1 ) {
                    tthis.formByVenta();
                }
            }
            this.operacionAlquiler.onclick = function () {
                if ( tthis.cambioEstado() === 2) {
                    tthis.formByAlquiler();
                }
            }
        }
    },
    formByAlquiler : function () {
        this.showSelectPeriodo.className = 'visible';

        this.selDesdeVenta.disabled = true;    this.selDesdeVenta.className = 'invisibles';
        this.selHastaVenta.disabled = true;    this.selHastaVenta.className = 'invisibles';
        this.selDesdeAlquiler.disabled = false;    this.selDesdeAlquiler.className = 'visibles';
        this.selHastaAlquiler.disabled = false;    this.selHastaAlquiler.className = 'visibles';
    },
    formByVenta : function () {
        this.showSelectPeriodo.className = 'invisible';

        this.selDesdeVenta.disabled = false;    this.selDesdeVenta.className = 'visibles';
        this.selHastaVenta.disabled = false;    this.selHastaVenta.className = 'visibles';
        this.selDesdeAlquiler.disabled = true;    this.selDesdeAlquiler.className = 'invisibles';
        this.selHastaAlquiler.disabled = true;    this.selHastaAlquiler.className = 'invisibles';
    }
};
