/*******************************************************************************
 * registra el control de buscador por numero de referencia en el home
 */
Registro.set('onload', function() {
    var formFindById = new FormFindById('formFindById', 'idpropiedad', 'msg');
    formFindById.setMensajeError('<div class="error">Solo puede ingresar números validos</div>')
                .setTime(2)
                .prepare();
});

/*******************************************************************************
 * clase para el control del buscador por referencia 
 */
var FormFindById = function(){ return this.construct.apply(this, arguments); };
FormFindById.prototype = {
    form : null,
    input : null,
    zonaMsg : null,
    msgLeft : 50,

    msgError : '',

    time : 4000,
    timeOut : null,

    construct : function(form, input, zonaMsg) {
        this.form = form;
        this.input = input;
        this.zonaMsg = zonaMsg;

        return this;
    },
    validar : function() {
        return ( (new Number(this.input.value)).valueOf() > 0 ) ? true : false;
    },
    mostrarError : function() {
        this.zonaMsg.innerHTML = this.msgError;
        this.zonaMsg.style.left = this.msgLeft +'px';

        if (this.timeOut === null) {
            var clouser = this;
            this.timeOut = window.setTimeout((
                function () {
                    clouser.zonaMsg.innerHTML = '';
                    window.clearTimeout( clouser.timeOut );
                    clouser.timeOut = null;
                }
            ), this.time);
        }
    },
    setMensajeError : function (msg) {
        this.msgError = msg;
        return this;
    },
    setTime : function (time) {
        this.time = (time*1000);
        return this;
    },
    prepare : function() {
        this.form = document.getElementById(this.form);
        this.input = document.getElementById(this.input);
        this.zonaMsg = document.getElementById(this.zonaMsg);

        if (this.form && this.input && this.zonaMsg) {
            var widthWindow = window.screen.width;
            this.msgLeft = (widthWindow > 940)
                ? ((widthWindow - 940) / 2)
                : 50;

            var clouser = this;

            this.form.onsubmit = function () {
                if ( clouser.validar() ) {
                    return true;
                }
                clouser.mostrarError();
                return false;
            };
            this.input.onfocus = function () {
                if (this.value === '' || this.value === 'referencia') {
                    this.value = '';
                }
            };
            this.input.onblur = function () {
                if (this.value === '') {
                    this.value = 'referencia';
                }
            };
            //this.input.focus();
            return true;
        }
        return false;
    }
}
