﻿Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'qtip';

    var txtNomeBebe = new Ext.form.TextField({
        id: 'txtNomeBebe',
        fieldLabel: 'Nome do Bebê',
        emptyText: '',
        labelSeparator: '',
        tabIndex: 41,
        width: 25,
        anchor: '95%'
    });

    var txtDataNascimento = new Ext.form.DateField({
        id: 'dtfDataNascimento',
        fieldLabel: 'Data de Nascimento',
        emptyText: '',
        tabIndex: 42,
        width: 60,
        anchor: '95%',
        getValue: function(ToString) {
            if (!ToString) {
                return this.parseDate(Ext.form.DateField.superclass.getValue.call(this)) || "";
            } else {
                return this.value;
            }
        }
    });

    var txtMae = new Ext.form.TextField({
        id: 'txtMae',
        fieldLabel: 'Nome da Mãe',
        emptyText: '',
        tabIndex: 43,
        width: 20,
        anchor: '95%'
    });

    var txtPai = new Ext.form.TextField({
        id: 'txtPai',
        fieldLabel: 'Nome do Pai',
        emptyText: '',
        tabIndex: 44,
        width: 20,
        anchor: '97%'
    });

    var FormAlbumBebe = new Ext.form.FormPanel({
        id: 'FormAlbumBebe',
        labelAlign: 'top',
        frame: false,
        border: false,
        bodyStyle: 'background-color:transparent;',
        url: '',
        width: 470,
        renderTo: 'PesqForm',
        buttonAlign: 'center',
        bodyStyle: 'background-color:transparent;',
        items: [
            {
                layout: 'column',
                border: false,
                bodyStyle: 'background-color:transparent;',
                items: [
                    { columnWidth: .7, border: false, layout: 'form', items: [txtNomeBebe], bodyStyle: 'background-color:transparent;' },
                    { columnWidth: .3, border: false, layout: 'form', items: [txtDataNascimento], bodyStyle: 'background-color:transparent;' }
                ]
            }, {
                layout: 'column',
                border: false,
                bodyStyle: 'background-color:transparent;',
                items: [
                    { columnWidth: .5, border: false, layout: 'form', items: [txtMae], bodyStyle: 'background-color:transparent;' },
                    { columnWidth: .5, border: false, layout: 'form', items: [txtPai], bodyStyle: 'background-color:transparent;' }
                ]
            }
	    ],
        buttons: [
	        new Ext.Button({
	            id: 'limparPesqBebe',
	            text: 'Limpar',
	            tooltip: 'Limpar',
	            tabIndex: 46,
	            handler: function() {
	                FormAlbumBebe.getForm().reset();
	            }
	        }),
	        new Ext.Button({
	            id: 'btnEnviarBebe',
	            text: 'Pesquisar Bebê',
	            tooltip: 'Pesquisar Bebê',
	            tabIndex: 45,
	            minWidth: 50,
	            handler: function() {
	                if (FormAlbumBebe.form.isValid()) {
	                    var qryStr = '';

	                    var txtNomeBebe = Ext.getCmp('txtNomeBebe');
	                    if (txtNomeBebe.getValue() != '') {
	                        qryStr += 'txtNomeBebe=' + txtNomeBebe.getValue() + '&';
	                    }

	                    var dtfDataNascimento = Ext.getCmp('dtfDataNascimento');
	                    if (dtfDataNascimento.getValue(true) != undefined && dtfDataNascimento.getValue(true) != '') {
	                        qryStr += 'dtfDataNascimento=' + dtfDataNascimento.getValue(true) + '&';
	                        qryStr = qryStr.replace('/', '-');
	                        qryStr = qryStr.replace('/', '-');
	                    }

	                    var txtMae = Ext.getCmp('txtMae');
	                    if (txtMae.getValue() != '') {
	                        qryStr += 'txtMae=' + txtMae.getValue() + '&';
	                    }

	                    var txtPai = Ext.getCmp('txtPai');
	                    if (txtPai.getValue() != '') {
	                        qryStr += 'txtPai=' + txtPai.getValue() + '&';
	                    }

	                    if (qryStr != '') {
	                        qryStr = qryStr.substring(0, qryStr.length - 1);
	                        window.location.href = 'Album_bebe.aspx?' + qryStr.toString();
	                    } else {
	                        Ext.Msg.alert('Bensaúde - Informação', 'Por favor, filtre sua pesquisa com pelo menos um campo.');
	                    }
	                }
	            }
	        })

	    ]
    });

    var arr = new Array();
    var qryStr = window.location.search.substring(1);

    if (qryStr != '') {
        var vars = qryStr.split('&');

        if (vars.length > 0) {
            for (var i = 0; i < vars.length; i++) {
                arr[i] = vars[i].split('=');
                arr[i] = arr[i].toString().replace(',', '*');
            }

            for (var i = 0; i < arr.length; i++) {
                var x = arr[i].toString().split('*');
                if (x[0].toString() == 'txtNomeBebe') {
                    var txtNomeBebe = Ext.getCmp('txtNomeBebe');
                    txtNomeBebe.setValue(x[1].toString());
                }
                else if (x[0].toString() == 'dtfDataNascimento') {
                    var dtfDataNascimento = Ext.getCmp('dtfDataNascimento');
                    dtfDataNascimento.setValue(x[1].toString().replace('-', '/').replace('-', '/'));
                }
                else if (x[0].toString() == 'txtMae') {
                    var txtMae = Ext.getCmp('txtMae');
                    txtMae.setValue(x[1].toString());
                }
                else if (x[0].toString() == 'txtPai') {
                    var txtPai = Ext.getCmp('txtPai');
                    txtPai.setValue(x[1].toString());
                }
            }
        }
    }
});