var formEnquiry, formContact, formDialog;

var Directory = {
	showFormMail: function( params ){
		formContact = new Ext.form.FormPanel({
				id			: 'form-contact',
				baseCls		: 'x-plain',
				labelWidth	: 100,
				defaultType	: 'textfield', 
				waitMsgTarget 	: 'form-contact',
				monitorValid	: true,
				items		: [
						{ fieldLabel: 'Name', name: 'name', anchor:'100%' },
						{ fieldLabel: 'Email Address', name: 'email', anchor: '100%' },
						{ fieldLabel: 'Subject', name: 'subject', anchor: '100%' },
						{ xtype:'htmleditor', name:'message', hideLabel: true, enableSourceEdit: false, anchor:'100%' }
					]
			});
		
		this.showDialog( formContact );
	},
	showFormEnquiry: function( params ){
		formEnquiry = new Ext.form.FormPanel({
				id			: 'form-enquiry',
				baseCls		: 'x-plain',
				labelWidth	: 100,
				defaultType	: 'textfield',	
				waitMsgTarget 	: 'form-enquiry',
				monitorValid	: true,
				items		: [
						{ fieldLabel: 'Name', name: 'name', anchor:'100%' },
						{ fieldLabel: 'Email Address', name: 'email', anchor: '100%' },
						{ fieldLabel: 'Subject', name: 'subject', anchor: '100%' },
						{ xtype:'htmleditor', name:'message', hideLabel: true, enableSourceEdit: false, anchor:'100%' }
					]
			});
		
		this.showDialog( formEnquiry );
	},	
	showDialog: function( objForm ){
		formDialog = new Ext.Window({
				layout	: 'fit',
				plain	: true,
				modal	: true,
				iconCls	: '',
				title	: 'Send Enquiry',
				width	: 580,
				height	: 360,
				resizable	: false,
				bodyStyle	: 'padding:5px;',
				buttonAlign	: 'center',
				items	: objForm,				
				buttons	: [
						{text: 'Send', handler: function(){
								objForm.getForm().submit({
									url		: '/ajax.php',
									method	: 'POST',
									params	: {action: 'directory', task: 'login'},
									waitMsg	: 'Sending...',
									scope	: this,
									failure	: function( form, action ){
										Ext.MessageBox.show({
												title	: 'User Login:',
												msg		: action.result.msg,
												icon	: Ext.MessageBox.WARNING,
												buttons	: Ext.MessageBox.OK,
												fn		: function(){}
											});
										},
									success	: function( form, action ){
											alert( 'hasil' );
											// alert( Ext.encode(action.result) );
											// window.location.href = 'index.php';
										}
								});
							}},
						{text: 'Cancel', handler: function(){
								formDialog.close()
							}}
					]
			});
		
		formDialog.show();
		formDialog.center();
	},
	sendMessage: function(){
		var loader	= Ext.get( 'wait-mask' );		
		var conn	= new Ext.data.Connection({
				url: '/ajax.php', 
				extraParams: {action: 'directory', output: 'json', task: 'send-message'},
				method: 'POST'
			});
		
		conn.on('beforerequest', function(){
				loader.mask(
						'<span style="color:#FF9900"><b>Sending message, please wait...</b></span>', 
						'x-mask-loading'
					);
			}, this);
		
		conn.on('requestcomplete', function(){loader.unmask()}, this);
		
		conn.request({
				params	: Ext.Ajax.serializeForm('siteForm'),
				failure	: function(){
					loader.unmask();
					
					Ext.Msg.show({
							title	: 'Sending Message:',
							msg		: 'There was a problem with the request.',
							icon	: Ext.MessageBox.ERROR,
							buttons	: Ext.MessageBox.OK
						});	
					},
				success	: function( response ){
						loader.unmask();
						
						var result = Ext.decode(response.responseText);
						if( result.success==false ){
							Ext.MessageBox.show({
									title	: 'Sending Message:',
									msg		: result.message,
									icon	: Ext.MessageBox.WARNING,
									buttons	: Ext.MessageBox.OK
								});	
							return false;
						}
						
						Ext.MessageBox.show({
								title	: 'Sending Message:',
								msg		: result.message,
								icon	: Ext.MessageBox.INFO,
								buttons	: Ext.MessageBox.OK,
								fn		: function(){
										history.go(-1);
									}
							});
						return true;
					}
			});
		
		return true;
	},
	sendEnquiry: function(){
		var loader	= Ext.get( 'wait-mask' );		
		var conn	= new Ext.data.Connection();
		
		conn.on('beforerequest', function(){
				loader.mask(
						'<span style="color:#FF9900"><b>Sending message, please wait...</b></span>', 
						'x-mask-loading'
					);
			}, this);
		
		conn.on('requestcomplete', function(){}, this);
		
		conn.request({
				url		: '/ajax.php',
				params	: {action: 'directory', task: 'send-enquiry', output: 'json'},
				method	: 'post',
				form	: 'siteForm',
				isUpload: false,
				scope	: this,
				failure	: function(){
					loader.unmask();
					
					Ext.Msg.show({
							title	: 'Sending Enquiry:',
							msg		: 'There was a problem with the request.',
							icon	: Ext.MessageBox.ERROR,
							buttons	: Ext.MessageBox.OK
						});	
					},
				success	: function( response ){
						loader.unmask();
						
						alert( response.responseText );
						
						var result = Ext.decode(response.responseText);
						if( result.success==false ){
							Ext.MessageBox.show({
									title	: 'Sending Enquiry:',
									msg		: result.message,
									icon	: Ext.MessageBox.WARNING,
									buttons	: Ext.MessageBox.OK
								});	
							return false;
						}
						
						Ext.MessageBox.show({
								title	: 'Sending Enquiry:',
								msg		: result.message,
								icon	: Ext.MessageBox.INFO,
								buttons	: Ext.MessageBox.OK,
								fn		: function(){
										history.go(-1);
									}
							});
						return true;
					}
			});
		
		return true;
	}
}

Ext.onReady(function(){
		Ext.QuickTips.init();
		Ext.form.Field.prototype.msgTarget = 'side';
	});
