Ext.onReady(function(){ 
	Ext.QuickTips.init();
	Ext.apply(Ext.QuickTips.getQuickTip(), { showDelay: 0, trackMouse: true });
});


function downloadForm(target,file){
	if (!Ext.getCmp('downloadWin')){
		new Ext.FormPanel({ id:'downloadForm', url:'requests/downloadFactsheet.php', waitMsgTarget: true, autoWidth:true, autoHeight:true,
			baseParams:{ fileName: file, fsType:'Architectural' }, 
			frame:false, bodyBorder:false, border:false, bodyStyle:'padding:5px 0 10px 15px; background:transparent', 
			labelWidth: 80, defaultType: 'textfield', 
			reader : new Ext.data.XmlReader({ record : 'contact', success: '@success' }),
			errorReader: new Ext.form.XmlErrorReader(), 
			defaults:{ width: 180, msgTarget:'side', labelStyle:'color:#333;' }, 
			items: [ 
				{ fieldLabel: 'First Name', name: 'name', id:'name', allowBlank:false }, 
				{ fieldLabel: 'Last Name', name: 'surname', id:'surname', allowBlank:false },
				{ fieldLabel: 'Company', name: 'sname', id:'sname', allowBlank:false },
				{ fieldLabel: 'Email', name: 'email', vtype:'email', id:'email', allowBlank:false }, 
				{ xtype:'label', html:'* <b>Please note</b>: The download link will be sent to this email.', style:'color: #666;padding:5px 0 10px;display:inline-block;', width:300 },
				{ xtype:'checkbox', hideLabel:true, boxLabel: 'Tick here if you do not want to receive future Ecocem emails', name: 'nospam', id:'nospam', ctCls:'form-checkbox', width:'100%' },
				{ xtype:'label', id:'downloadInfoLabel', cls:'formInfoLabel', html:'The link to download the Factsheet has been sent to the email address provided.', style:'display:none;' }
			]
		});
		new Ext.Window({ id:'downloadWin', width: 345, layout: 'fit', modal:true, resizable:false, plain:true, height:235,
			buttonAlign:'center',	title: 'Download Request', items: Ext.getCmp('downloadForm'), animateTarget:target,
			buttons: [ { text: 'Submit', id:'btndownload', handler: downloadSubmit }, { text: 'Cancel', id:'btnCanceldownload', handler:function(){ Ext.getCmp('downloadWin').hide(); }} ]
		});
	}
	Ext.getCmp('downloadWin').show();
}

function downloadSubmit(){
	Ext.getCmp('downloadForm').getForm().submit({
		waitMsg:'Preparing your factsheet...', 
		success: function(){ 
			Ext.getCmp('downloadForm').getForm().reset();
			Ext.get('btndownload').fadeOut({ easing:'easeOut' });
			Ext.get('btnCanceldownload').shift({ x: Ext.get('btnCanceldownload').getX()-50, easing:'easeOut', duration: .5 });
			Ext.getCmp('btnCanceldownload').setText('Close');
			var formItems=Ext.getCmp('downloadForm').items.items;
			for (var i=0; i<formItems.length-1; i++){
				try{formItems[i].getEl().parent('.x-form-item').slideOut('t',{ easing:'easeOut', useDisplay:true });}
				catch(e){formItems[i].getEl().slideOut('t',{ easing:'easeOut', useDisplay:true });}
			} 
			Ext.get('downloadInfoLabel').fadeIn({ easing:'easeIn' });
		}
	});
}


// A reusable error reader class for XML forms
Ext.form.XmlErrorReader = function(){ Ext.form.XmlErrorReader.superclass.constructor.call(this, { record : 'field', success: '@success' }, ['id', 'msg' ] ); };
Ext.extend(Ext.form.XmlErrorReader, Ext.data.XmlReader);

