
Ext.onReady(function(){
  // data stores for the comboboxes
  var aOptions = [[1, GetTextInLanguage('CONTACT_FORM_REGARDING_OPTION_1', selectedLanguage)], [2, GetTextInLanguage('CONTACT_FORM_REGARDING_OPTION_2', selectedLanguage)], [3, GetTextInLanguage('CONTACT_FORM_REGARDING_OPTION_3', selectedLanguage)], [4, GetTextInLanguage('CONTACT_FORM_REGARDING_OPTION_4', selectedLanguage)], [5, GetTextInLanguage('CONTACT_FORM_REGARDING_OPTION_5', selectedLanguage)]];
  var oRegardingOptionsDS = new Ext.data.Store({
              proxy:  new Ext.data.MemoryProxy(aOptions),
              reader: new Ext.data.ArrayReader(
                  {id: 0},
                  [
                      {name: 'Option_Id'}, {name: 'Option'}
                  ]
              )
          }); 
  oRegardingOptionsDS.load();



	var Form = Ext.form;
	var contactForm = new Ext.FormPanel({
				id: 'contactFormID',
				renderTo: 'contactFormContainer',
        frame: true,
        title: GetTextInLanguage('CONTACT_FORM_TITLE', selectedLanguage),
        bodyStyle:'padding:5px 5px 0',
        defaults: {width: 230},
        labelWidth: 130,
        defaultType: 'textfield',

        items: [{
        				id: 'emailInput',
                fieldLabel: GetTextInLanguage('CONTACT_FORM_EMAIL', selectedLanguage),
                name: 'email',
                value: '', 
                xtype: 'textfield',
                msgTarget: 'under',
                vtype:'email'
            },{
                fieldLabel: GetTextInLanguage('CONTACT_FORM_REGARDING', selectedLanguage),
                hiddenName: 'Option',
                id: 'regardingOption',
                labelSeparator: '',
                xtype: 'combo',
                store: oRegardingOptionsDS,
                emptyText: GetTextInLanguage('CONTACT_FORM_CHOOSE_OPTION', selectedLanguage),
                editable: false, 
                mode: 'local',
                triggerAction: 'all',
                valueField:'Option',
                msgTarget: 'under',
                displayField: 'Option' 
            },{
            		id: 'url',
                fieldLabel: GetTextInLanguage('CONTACT_FORM_URL_OPTIONAL', selectedLanguage),
                name: 'url',
                value: '', 
                xtype: 'textfield',
                vtype: 'url'
            },{
            		id: 'subject',
                fieldLabel: GetTextInLanguage('CONTACT_FORM_SUBJECT', selectedLanguage),
                value: '', 
                name: 'subject',
                xtype: 'textfield'
            },
            {
                id: 'message',
                name: 'message',
                fieldLabel: GetTextInLanguage('CONTACT_FORM_MESSAGE', selectedLanguage),
                xtype:'textarea'
            },{
                xtype:'box'
                ,fieldLabel: GetTextInLanguage('CONTACT_FORM_VERIFICATION_CODE', selectedLanguage)
                ,anchor:''
                ,width: '100%'
                ,autoEl:{
                     tag:'div', 
                     style:'margin:8px 0 8px 0px', 
                     children:[{
                        tag:'div'
                        ,style:'float: left; font-size: 12px'
                        ,html: GetTextInLanguage('CONTACT_FORM_VERIFICATION_CODE', selectedLanguage)
                    },{
                        tag:'div'
                        ,style:'margin:0 0 4px 133px'
                        ,html: sCrypt.replace(/&quot;/g,'"')
                    }]
                 }
            
            },{
            		id: 'cryptInput',
                fieldLabel: GetTextInLanguage('CONTACT_FORM_ENTER_CODE', selectedLanguage),
                value: '', 
                name: 'cryptInput',
                xtype: 'textfield'
            }
        ],
        buttons: [{text: GetTextInLanguage('CONTACT_FORM_SEND', selectedLanguage), type: 'submit', listeners: { click: { fn : function() {
                                        if (!contactForm.items.get('emailInput').isValid())
																				{
																					Ext.MessageBox.alert(GetTextInLanguage('CREATE_ACCOUNT_FORM_ALERT_MESSAGE_TITLE', selectedLanguage), GetTextInLanguage('CREATE_ACCOUNT_FORM_ALERT_MESSAGE_EMAIL_NOT_VALID', selectedLanguage));
																					return;
																				}
																				if (contactForm.items.get('emailInput').getValue() == '')
																				{
																					Ext.MessageBox.alert(GetTextInLanguage('CREATE_ACCOUNT_FORM_ALERT_MESSAGE_TITLE', selectedLanguage), GetTextInLanguage('CONTACT_FORM_ALERT_MESSAGE_EMAIL_EMPTY', selectedLanguage));
																					return;
																				}
																				if (contactForm.items.get('regardingOption').getValue() == '')
																				{
																					Ext.MessageBox.alert(GetTextInLanguage('CONTACT_FORM_REGARDING', selectedLanguage), GetTextInLanguage('CONTACT_FORM_ALERT_MESSAGE_REGARDING_OPTION_EMPTY', selectedLanguage));
																					return;
																				}
																				if (!contactForm.items.get('url').isValid())
																				{
																					Ext.MessageBox.alert(GetTextInLanguage('CONTACT_FORM_URL', selectedLanguage), GetTextInLanguage('CONTACT_FORM_ALERT_URL_NOT_VALID', selectedLanguage));
																					return;
																				}
																				if (contactForm.items.get('subject').getValue() == '')
																				{
																					Ext.MessageBox.alert(GetTextInLanguage('CONTACT_FORM_SUBJECT', selectedLanguage), GetTextInLanguage('CONTACT_FORM_ALERT_SUBJECT_EMPTY', selectedLanguage));
																					return;
																				}		
																				if (contactForm.items.get('message').getValue() == '')
																				{
																					Ext.MessageBox.alert(GetTextInLanguage('CONTACT_FORM_MESSAGE', selectedLanguage), GetTextInLanguage('CONTACT_FORM_ALERT_MESSAGE_EMPTY', selectedLanguage));
																					return;
																				}		
																				var myMask = new Ext.LoadMask(contactForm.getEl(), {msg:GetTextInLanguage('CREATE_ACCOUNT_FORM_MESSAGE_PLEASE_WAIT', selectedLanguage)}); 
																				myMask.show();		
																				var req = Ext.Ajax.request({
																					url : 'data/login.php' , 
																					params : { action : 'contact_form', email: contactForm.items.get('emailInput').getValue(), regarding: contactForm.items.get('regardingOption').getValue(), url: contactForm.items.get('url').getValue(), subject: contactForm.items.get('subject').getValue(), message: contactForm.items.get('message').getValue(), crypt: contactForm.items.get('cryptInput').getValue() },
																					method: 'POST',
																					success: function ( result, request ) {
																							myMask.hide();								
																							try
																							{
																								var jsonData = Ext.util.JSON.decode(result.responseText);
																								if (jsonData['status'] == 'ok')
																									Ext.Msg.show({
																									   title:GetTextInLanguage('CREATE_ACCOUNT_FORM_MESSAGE_SENT_TITLE', selectedLanguage),
																									   msg: GetTextInLanguage('CONTACT_FORM_MESSAGE_SENT', selectedLanguage),
																									   buttons: Ext.Msg.OK,
																									   fn: function() { document.location = ''; },
																									   icon: Ext.MessageBox.EXCLAMATION
																									});																																						
																								else
																									Ext.MessageBox.alert(GetTextInLanguage('FAILED_MESSAGE', selectedLanguage), jsonData['message']); 
																							}
																							catch (err)
																							{
																							  Ext.MessageBox.alert(GetTextInLanguage('FAILED_MESSAGE', selectedLanguage), GetTextInLanguage('COMMUNICATION_ERROR', selectedLanguage) + result.responseText); 																							
																							}																							
																					},
																					failure: function ( result, request) { 
																						myMask.hide();
                              							Ext.MessageBox.alert(GetTextInLanguage('FAILED_MESSAGE', selectedLanguage), GetTextInLanguage('INTERNAL_ERROR', selectedLanguage)); 
																					} 
																				});																				
																				}}}
				}]
    	}); 
   
		contactForm.render();
 
});
