Ext.onReady(function(){
    var win;
    var button = Ext.get('show-email-form');

    button.on('click', function(){
        // create the window on the first click and reuse on subsequent clicks
        if(!win){
            win = new Ext.Window({
                applyTo     : 'mail-win',
                layout      : 'fit',
                width       : 400,
                height      : 380,
                closeAction :'hide',
                plain       : true,
                modal       : true,
                resizable   : false,
				iconCls     : 'bulb-icon',
                items       : new Ext.Panel({
                    el        : 'mail-panel',
					deferredRender : false,
                    border         : false,
                    items:[
                    		{contentEl:'mail-form', layout: 'fit', border: false}
                    ]
                }),

                buttons: [{
                    text     : 'Invia',
                    handler  : function(){
						if (!isEmpty(document.frmEmail.name) && !isEmpty(document.frmEmail.email) && !isEmpty(document.frmEmail.subject)) {
							document.frmEmail.submit();
							win.hide();
							Ext.MessageBox.alert('Drybit.com - Messaggio Inviato', '<b>Sarete contattati al pi&uacute; presto da un nostro consulente.</b>');
							document.frmEmail.reset();
						} else {
							Ext.MessageBox.alert('Drybit.com - Attenzione!', '<b>Tutti i campi sono obbligatori.</b>');
						}
						
                    }
                },{
                    text     : 'Chiudi',
                    handler  : function(){
                        win.hide();
                    }
                }]
            });
        }
        win.show(button);
    });
});

function isEmpty(thefield) {
	var re = /^\s{1,}$/g; //match white space i.e. space, tab, form-feed, etc.
	if ((thefield.value.length == 0) || (thefield.value == null) || ((thefield.value.search(re)) > -1)) {
		return true;
	}
	return false;
}
