Ext.BLANK_IMAGE_URL = 'ext/resources/images/default/s.gif';

Ext.onReady(function() {
	// Country codes Mapping definition
	var countryMap = new Array();
	countryMap['gr'] = 'Greece';
	countryMap['**'] = 'Greece';
	countryMap['de'] = 'Germany';
	countryMap['uk'] = 'United Kingdom';
	countryMap['us'] = 'United States';
	countryMap['fr'] = 'France';
	countryMap['it'] = 'Italy';
	countryMap['cy'] = 'Cyprus';

	// LOGIN FORM DEFINITION
	var loginFormDEF = function(config) {
		Ext.apply(this, config, {
			title: capLoginTitle,
			border: true,
			width: 400,
			autoHeight: true,
			xtype: 'form',
			layout: 'form',
			frame: true,
			renderTo: 'submitFormDiv',
			bodyStyle:'padding:5px',
			defaults: {
				xtype: 'textfield',
				anchor: '100%'
			},
			items: [
				this.email = new Ext.form.TextField({
					name: 'email',
					fieldLabel: 'E-Mail',
					allowBlank: false,
					vtype: 'email'
				}),{
					inputType: 'password',
					name: 'password',
					fieldLabel: capPassword,
					allowBlank: false				
				}
			],
			buttons: [
				this.login = new Ext.Button({
					text: capLogin,
					icon: 'ext/icons/tick.png',
					scope: this,
					handler: function() {
						this.login.disable();
						this.getForm().submit({
							url: 'ajax.pl',
							waitMsg: capPleaseWait,
							scope: this,
							params: {
								task: 'LOGIN',
								lang: lang
							},
							success: function(form, o) {
								helpPanel.hide();								
								this.getEl().fadeOut({
									scope: this,
									callback: function() {
										this.hide();
										this.login.enable();
										var submitForm = new submitFormDEF();
										submitForm.initialize(o.result.webmasterid);
										if (lang == 'english') {
											helpPanel.load({
												url: 'submitionhelp.html',
												text: 'Loading...'
											});
										} else {
											helpPanel.load({
												url: 'submitionhelp_gr.html',
												text: 'Φόρτωση...'
											});
										}
										helpPanel.setTitle(capSubmitHelpTitle);
										helpPanel.show();

									}
								});
							},
							failure: function(form, o) {
								if (o.failureType == 'client') {
									if (lang == 'english') {
										Ext.MessageBox.alert('Error', 'There are errors in your form. Please correct before proceeding.');
									} else {
										Ext.MessageBox.alert('Λάθος', 'Υπάρχουν λάθη στη φόρμα εισόδου. Παρακαλώ διορθώστε πριν προχωρήσετε.');
									}
								} else {
									if (lang == 'english') {
										Ext.MessageBox.alert('Error', 'Wrong e-mail and password combination. Please try again.');
									} else {
										Ext.MessageBox.alert('Λάθος', 'Λάθος e-mail ή/και κωδικός εισόδου. Παρακαλώ, προσπαθήστε ξανά.');
									}									
								}
								this.login.enable();
							}
						});
					}
				}),{
					text: capRetrievePassword,
					icon: 'ext/icons/lock_open.png',
					scopt: this,
					handler: function() {
						Ext.Msg.prompt(capRetrievePassword, capRetrievePasswordText, function(btn, text) {
							if (btn == 'ok') {
								Ext.Ajax.request({
									url: 'ajax.pl',
									params: {
										task: 'RETRIEVEPASSWORD',
										email: text,
										lang: lang
									},
									success: function(result) {
										response = Ext.util.JSON.decode(result.responseText);
										if (response.success) {
											if (lang == 'english') {
												Ext.Msg.alert("Success", "Password sent to your e-mail address.");
											} else {
												Ext.Msg.alert("Ολοκληρώθηκε", "Ο κωδικός σας έχει σταλεί στο e-mail σας.");
											}
										} else {
											if (lang == 'english') {
												Ext.Msg.alert("Error", "Cannot retrieve password. "+response.reason);
											} else {
												Ext.Msg.alert("Λάθος", "Αδύνατη η ανάκτηση κωδικού. "+response.reason);
											}										
										}
									},
									failure: function(result) {
										if (lang == 'english') {
											Ext.Msg.alert("System Error", "Cannot retrieve password.");
										} else {
											Ext.Msg.alert("Σφάλμα Συστήματος", "Αδύνατη η ανάκτηση κωδικού.");
										}
									}
								});
							}
						});
					}
				},
				this.createAccount = new Ext.Button({
					text: capCreateAccount,
					icon: 'ext/icons/user_add.png',
					scope: this,
					handler: function() {
						this.createAccount.disable();
						helpPanel.hide();
						this.getEl().switchOff({
							scope: this,
							callback: function() {
								this.hide();
								this.createAccount.enable();
								webmastersForm = new webmastersFormDEF();
								webmastersForm.getEl().fadeIn();
								webmastersForm.show();
								initCountry = countryMap[remoteCountry];
								if (typeof(initCountry) == 'undefined') {
									initCountry = '';
								}
								webmastersForm.country.setValue(initCountry); // set country value
								webmastersForm.region.store.baseParams.country = '';
								webmastersForm.region.store.baseParams.task = 'LISTINGREGION';
								webmastersForm.region.store.baseParams.lang = lang;
								if (initCountry == 'Greece' || initCountry == 'United States' || initCountry == 'United Kingdom' || initCountry == 'Cyprus') {
									webmastersForm.region.enable();
									webmastersForm.region.allowBlank = false;
									webmastersForm.region.store.baseParams.country = initCountry;
									webmastersForm.region.store.reload();
								}
								webmastersForm.country.pageSize = '';
								webmastersForm.country.store.baseParams.task = 'LISTINGCOUNTRY';
								webmastersForm.country.store.baseParams.lang = lang;
								if (lang == 'english') {
									helpPanel.load({
										url: 'webmasterterms.html',
										text: 'Loading...'
									});
								} else {
									helpPanel.load({
										url: 'webmasterterms_gr.html',
										text: 'Φόρτωση...'
									});
								}
								helpPanel.setTitle(capTermsTitle);
								helpPanel.show();
							}
						});
					}
				})
			],
			listeners: {
				show: function(form) {
					form.getForm().findField('email').focus(true, true);
				},
				render: function(form) {
					form.getForm().findField('email').focus(true, true);
				}
			}
		});
		loginFormDEF.superclass.constructor.apply(this, arguments);
	};
	Ext.extend(loginFormDEF, Ext.FormPanel, {});

	// LOGIN FORM HELP
	var helpPanelDEF = function(config) {
		Ext.apply(this, config, {
			title: capHelpPanelTitle,
			border: true,
			width: 640,
			autoHeight: true,
			xtype: 'panel',
			layout: 'fit',
			frame: false,
			collapsible: true,
			bodyStyle:'padding:15px; background-color:white',
			renderTo: 'helpDiv',
			autoLoad: {
				url: 'loginhelp.html',
				charset: 'iso-8859-7'
			}
		});
		helpPanelDEF.superclass.constructor.apply(this, arguments);
	};
	Ext.extend(helpPanelDEF, Ext.Panel, {});

	// WEBMASTERS FORM DEFINITION
	var comLangList = new Array();
	if (lang == 'english') {
		comLangList = [['english', 'English'],['greek', 'Greek']];
	} else {
		comLangList = [['greek', 'Ελληνικά'],['english', 'Αγγλικά']];
	}

	var webmastersFormDEF = function(config) {
		Ext.apply(this, config, {
			title: capWebmasterRegistrationForm,
			border: true,
			width: 640,
			autoHeight: true,
			xtype: 'form',
			layout: 'form',
			renderTo: 'submitFormDiv',
			frame: true,
			bodyStyle:'padding:5px',
			hidden: true,
			style: {
				visibility: 'hidden'
			},
			defaults: {
				xtype: 'textfield'
			},
			items: [
				{
					name: 'email',
					fieldLabel: capEmail,
					labelStyle: 'font-weight:bold;',
					allowBlank: false,
					vtype: 'email',
					anchor:'100%',
					maxLength: 100,
					listeners: {
						scope: this, // scope to the FormPanel
						render: function() {
							this.tip = tipDEF(this.getForm().findField('email'), capEmailTooltip)
						}
					}
				},{
					inputType: 'password',
					name: 'password',
					fieldLabel: capPassword,
					labelStyle: 'font-weight:bold;',
					allowBlank: false,
					anchor:'100%',
					minLength: 5,
					maxLength: 20
				},{
					inputType: 'password',
					name: 'vpassword',
					fieldLabel: capVerifyPassword,
					allowBlank: false,
					anchor:'100%',
					minLength: 5,
					maxLength: 20
				},{
					xtype: 'fieldset',
					title: capContactDetails,
					defaults: {
						xtype: 'textfield'
					},
					items: [
						{
							name: 'name',
							fieldLabel: capFullName,
							allowBlank: false,
							anchor:'100%',
							maskRe: /[A-Za-zΑ-Ωα-ωάέύίόήώΰύΐί ]+/,
							minLength: 5
						},{
							name: 'address',
							fieldLabel: capAddress,
							allowBlank: true,
							anchor:'100%',
							minLength: 5
						},{
							name: 'postcode',
							fieldLabel: capPostCode,
							allowBlank: false,
							anchor:'100%'
						},{
							name: 'city',
							fieldLabel: capCity,
							allowBlank: true,
							anchor:'100%',
							vtype: 'alpha'
						},{
							name: 'tel',
							fieldLabel: capTelephone,
							allowBlank: true,
							anchor:'100%'
						}
					]
				},{
					xtype: 'fieldset',
					title: capRegionLanguage,
					defaults: {
						xtype: 'textfield'
					},
					items: [
						this.country = countryFieldDEF(this),
						this.region = new geographyFieldDEF({
							anchor: '100%', 
							disabled: true, 
							fieldLabel: capRegion,
							emptyText: capChooseRegion,
							lang: lang,
							name: 'region',
							hiddenName: 'region'
						}),{
							xtype:'combo',
							name: 'comlang',
							hiddenName: 'comlang',
							fieldLabel: capPreferedLanguage,
							allowBlank: false,
							valueField: 'id',
							displayField: 'title',
							mode: 'local',
							triggerAction: 'all',
							hideOnSelect:false,
							forceSelection: true,
							store: new Ext.data.ArrayStore({
								fields:[ 'id', 'title' ],
								data: comLangList
							})
						}
					]
				},{
					xtype: 'checkbox',
					hideLabel: true,
					boxLabel: capAcceptTerms,
					scope: this,
					handler: function(dummyThis, btnStatus) {
						if (btnStatus) {
							this.submit.enable();
							helpPanel.expand(true);
						} else {
							this.submit.disable();
						}
					}
				
				}
			],
			buttons: [
				this.submit = new Ext.Button({
					text: capSubmit,
					icon: 'ext/icons/accept.png',
					scope: this,
					disabled: true,
					handler: function() {
						// Validate form (check if passwords match)
						if (this.getForm().findField('password').getValue() != '') {
							if (this.getForm().findField('password').getValue() != this.getForm().findField('vpassword').getValue()) {
								if (lang == 'english') {
									Ext.Msg.alert('Error', 'Passwords do not match');
								} else {
									Ext.Msg.alert('Λάθος', 'Ο κωδικός και κωδικός επιβεβαίωσης πρέπει να είναι ίδιοι.');
								}
								return;
							}
						}
						// Ajax call
						this.getForm().submit({
							url: 'ajax.pl',
							waitMsg: capPleaseWait,
							scope: this,
							params: {
								task: 'UPDATEWEBMASTER',
								lang: lang
							},
							success: function(form, o) {
								this.getEl().fadeOut({
									scope: this,
									callback: function() {
										this.hide();
										this.destroy();
										var webmastersPanel = new webmastersPanelDEF({
											webmasterid: o.result.response // custom property
										});
										webmastersPanel.getEl().fadeIn();
										webmastersPanel.show();
									}
								});
							},
							failure: function(form, o) {
								if (o.failureType == 'client') {
									if (lang == 'english') {
										Ext.MessageBox.alert('Error', 'There are errors in your form. Please correct before proceeding.');
									} else {
										Ext.MessageBox.alert('Λάθος', 'Υπάρχουν λάθη στη φόρμα εγγραφής. Παρακαλώ διορθώστε πριν προχωρήσετε.');
									}
								} else {
									reason = '';
									if (typeof(o.result) != 'undefined') {
										if (typeof(o.result.reason) != 'undefined') {
											reason = o.result.reason;
										}
									}
									if (lang == 'english') {
										Ext.MessageBox.alert('Error', 'Could not create webmaster account. '+reason);
									} else {
										Ext.MessageBox.alert('Λάθος', 'Αδύνατη η εγγραφή. '+reason);
									}
								}
							}
						});
					}
				}),{
					text: capCancel,
					icon: 'ext/icons/cancel.png',
					scope: this,
					handler: function() {
						this.getEl().fadeOut({
							scope: this,
							callback: function() {
								this.hide();
								this.destroy();
								loginForm.show();
								loginForm.getEl().fadeIn();
								helpPanel.setTitle(capHelpPanelTitle);
								var helpfile = 'loginhelp.html';
								if (lang == 'greek') {
									helpfile = 'loginhelp_gr.html';
								}
								helpPanel.load({
									url: helpfile,
									text: 'Loading...'
								})
								helpPanel.show();
							}
						})
					}
				}
			],
			listeners: {
				render: function(form) {
					form.getForm().findField('email').focus(true, true);
				}
			}
		});
		webmastersFormDEF.superclass.constructor.apply(this, arguments);
	};
	Ext.extend(webmastersFormDEF, Ext.FormPanel, {});

	
	// WEBMASTER CONFIRM PANEL
	var webmastersPanelDEF = function(config) {
		Ext.apply(this, config, {
			title: capWebmasterConfirmTitle,
			border: true,
			width: 640,
			autoHeight: true,
			xtype: 'panel',
			layout: 'form',
			frame: true,
			bodyStyle:'padding:5px',
			renderTo: 'submitFormDiv',
			hidden: true,
			style: {
				visibility: 'hidden'
			},
			html: capWebmasterConfirm,
			buttons: [{
				scope: this,
				text: capWebmasterContinue,
				handler: function() {
					this.getEl().fadeOut({
						scope: this,
						callback: function() {
							this.hide();
							var submitForm = new submitFormDEF();
							submitForm.initialize(this.webmasterid);
							this.destroy();
						}
					})
				}
			}]
		});
		webmastersPanelDEF.superclass.constructor.apply(this, arguments);
	};
	Ext.extend(webmastersPanelDEF, Ext.Panel, {});
	
	
	// SUBMISION FORM DEFINITION
	if (lang == 'english') {
		var langList = [
			[1, 'Greek'],
			[2, 'English'],
			[3, 'German'],
			[4, 'French'],
			[5, 'Italian'],
			[6, 'Spanish'],
			[7, 'Portuguese'],
			[9, 'Russian'],
			[10, 'Dutch'],

		];
	} else {
		var langList = [
			[1, 'Ελληνικά'],
			[2, 'Αγγλικά'],
			[3, 'Γερμανικά'],
			[4, 'Γαλλικά'],
			[5, 'Ιταλικά'],
			[6, 'Ισπανικά'],
			[7, 'Πορτογαλικά'],
			[9, 'Ρώσσικα'],
			[10, 'Ολλανδικά']
		];
	}
	var submitFormDEF = function(config) {
		Ext.apply(this, config, {
			title: capSubmissionForm,
			border: true,
			width: 640,
			autoHeight: true,
			xtype: 'form',
			layout: 'form',
			frame: true,
			bodyStyle:'padding:5px',
			renderTo: 'submitFormDiv',
			hidden: true,
			style: {
				visibility: 'hidden'
			},
			items: [
				this.ID = new Ext.form.Hidden({
					name: 'id'
				}),
				this.webmasterid = new Ext.form.Hidden({
					name: 'webmasterid'
				}),{
					xtype: 'hidden',
					name: 'moparent'
				},{
					xtype: 'hidden',
					name: 'cruser'
				},{
					xtype: 'hidden',
					name: 'crdate'
				},
				this.url = new Ext.form.TextField({
					name: 'url',
					fieldLabel: 'Url',
					allowBlank: false,
					vtype: 'url',
					triggerClass: 'x-form-search-trigger',
					anchor: '100%',
					listeners: {
						scope: this, // scope to the form
						change: function(f, newValue, oldValue) {
							if (f.validate()) {
								this.load({
									scope: this, // set scope to the form window
									url: 'ajax.pl',
									params: {
										task: 'READHOST',
										url: this.url.getValue(),
										webmasterid: this.webmasterid.getValue()
									},
									success: function(form, o) {
										this.parent.updateRawValue();
										this.country.updateRawValue('english');
										this.region.updateRawValue(lang);
									},
									failure: function(form, o) {
										if (o.result.reason == 'EXISTS') {
											if (lang == 'english') {
												Ext.Msg.alert("Error", "This website already exists into our directory and belongs to another webmaster. You can only change details of your own submissions.");
											} else {
												Ext.Msg.alert("Λάθος", "Η ιστοσελίδα που συμπληρώσατε υπάρχει ήδη στον κατάλογό μας και ανήκει σε διαφορετικό λογαριασμό. Μπορείτε να τροποποιήσετε μόνο τις δικές σας ιστοσελίδες.");
											}
											this.url.setValue('');
											this.url.focus(true, true);
										}
									}
								});
							}
						}
					}
				}),
				this.parent = new categoryFieldDEF({
					anchor: '100%',
					fieldLabel: capCategory,
					allowBlank: false
				}),
				this.country = countryFieldDEF(this),
				this.region = new geographyFieldDEF({
					anchor: '100%', 
					fieldLabel: capRegion, 
					emptyText: capChooseRegion,
					name: 'region',
					hiddenName: 'region'
				}),{
					xtype: 'lovcombo',
					name: 'flang', // because it posts two lang fields (lang is global... don't know why it posts this)
					hiddenName: 'flang',
					emptyText: capChooseLanguages,
					fieldLabel: capLanguages,
					mode: 'local',
					triggerAction: 'all',
					hideOnSelect:false,
					valueField: 'id',
					displayField: 'title',
					forceSelection: true,
					allowBlank: false,
					anchor: '100%',
					store: new Ext.data.ArrayStore({
						fields:[ 'id', 'title' ],
						data: langList
					}),
					beforeBlur: Ext.emptyFn // Need this so it does not clear the multiple values selected
				},{
					xtype: 'fieldset',
					title: capEnglish,
					layout: 'form',
					defaults: {
						anchor: '100%',
						xtype: 'textfield',
						allowBlank: false
					},
					items: [{
						name: 'entitle',
						fieldLabel: capTitle,
						maxLength: 100,
						listeners: {
							render: function() {
								this.tip = tipDEF(this, capTitleTooltip);
							}
						}
					},{
						name: 'endescr',
						xtype: 'textarea',
						fieldLabel: capDescription,
						maxLength: 250,
						listeners: {
							render: function() {
								this.tip = tipDEF(this, capDescrTooltip);
							}
						}
					}]
				},{
					xtype: 'fieldset',
					title: capGreek,
					layout: 'form',
					defaults: {
						anchor: '100%',
						xtype: 'textfield',
						allowBlank: false
					},
					items: [{
						name: 'grtitle',
						fieldLabel: capTitle,
						maxLength: 100,
						listeners: {
							render: function() {
								this.tip = tipDEF(this, capTitleTooltip);
							}
						}
					},{
						name: 'grdescr',
						xtype: 'textarea',
						fieldLabel: capDescription,
						maxLength: 250,
						listeners: {
							render: function() {
								this.tip = tipDEF(this, capDescrTooltip);
							}
						}
					}]
				},{
					xtype: 'fieldset',
					title: capComments,
					layout: 'form',
					items: [{
						name: 'message',
						xtype: 'textarea',
						hideLabel: true,
						anchor: '100%'
					}]
				}
			],
			buttons: [
				{
					text: capSubmit,
					icon: 'ext/icons/accept.png',
					scope: this,
					handler: function(button) {
						button.disable();
						this.getForm().submit({
							url: 'ajax.pl',
							waitMsg: capPleaseWait,
							scope: this,
							params: {
								task: 'UPDATEHOST',
								lang: lang
							},
							success: function(form, o) {
								// returns the created id
								// returns positive mod if it's a modification
								var response = Ext.util.JSON.decode(o.result.response);
								window.location.href = 'donesubmit.pl?id='+response.id+'&mod='+response.mod+'&lang='+lang;
							},
							failure: function(form, o) {
								button.enable();
								if (o.failureType == 'client') {
									if (lang == 'english') {
										Ext.MessageBox.alert('Error', 'There are errors in your form. Please correct before proceeding.');
									} else {
										Ext.MessageBox.alert('Λάθος', 'Υπάρχουν λάθη στη φόρμα καταχώρησης. Παρακαλώ διορθώστε πριν προχωρήσετε.');
									}
								} else {
									if (lang == 'english') {
										Ext.MessageBox.alert('System Error', 'Could not perform submission. Please try again or contact our support if the problem persists.');
									} else {
										Ext.MessageBox.alert('Σφάλμα Συστήματος', 'Αδύνατη καταχώρηση. Παρακαλώ προσπαθήστε ξανά ή επικοινωνήστε μαζί μας εάν το πρόβλημα επαναληφθεί.');
									}
								}
							}
						});
					}
				},{
					text: capCancel,
					icon: 'ext/icons/cancel.png',
					scope: this,
					handler: function() {
						this.getEl().fadeOut({
							scope: this,
							callback: function() {
								this.hide();
								this.destroy();
								loginForm.show();
								loginForm.getEl().fadeIn();
								helpPanel.setTitle(capHelpPanelTitle);
								helpPanel.load({
									url: 'loginhelp.html',
									text: 'Loading...'
								})
								helpPanel.show();
							}
						});
					}
				}
			],
			listeners: {
				render: function(form) {
					form.getForm().findField('url').focus(true, true);
				}
			}
		});
		submitFormDEF.superclass.constructor.apply(this, arguments);
		
		// functions overriden should exist into the fields definitions
		this.url.onTriggerClick = function() {
			window.open(this.getValue());
		}
	}
	Ext.extend(submitFormDEF, Ext.FormPanel, {
		initialize: function(webmasterid) {
			this.country.pageSize = '';
			this.country.store.baseParams.task = 'LISTINGCOUNTRY'
			this.country.store.baseParams.lang = 'english'
			this.region.store.baseParams.task = 'LISTINGREGION';
			this.region.store.baseParams.lang = lang;
			this.getEl().fadeIn();
			this.show();
			if (initParent > 0) {
				this.parent.setValue(initParent) ;
				this.parent.updateRawValue();
			}
			this.webmasterid.setValue(webmasterid);
			initCountry = countryMap[remoteCountry];
			if (typeof(initCountry) == 'undefined') {
				initCountry = '';
			}
			this.country.setValue(initCountry); // set country value
			if (initCountry == 'Greece' || initCountry == 'Ελλάδα' || initCountry == 'United States' || initCountry == 'United Kingdon' || initCountry == 'Cyprus') {
				this.region.enable();
				this.region.store.baseParams.country = initCountry;
				this.region.allowBlank = false;
			}
		}
	});

	// CATEGORY FIELD DEFINITION
	var categoryFieldDEF = function(config) {
		Ext.apply(this, config, {
			name: 'dparent',
			hiddenName: 'parent',
			hiddenValue: 'id',
			fieldLabel: 'Category',
			displayField: 'path',
			valueField: 'id',
			forceSelection: true,
			store: new Ext.data.JsonStore({
				id: 'id',
				root: 'rows',
				totalProperty: 'total',
				fields: [
					'id', 'path'
				],
				url: 'ajax.pl',				
				baseParams: {
					task: "LISTINGCAT",
					lang: lang
				},
				autoLoad: true
			}),
			triggerAction: 'all',
			pageSize: 12,
			minChars: 3,
			loadingText: capSearching,
			emptyText: capChooseCategory,
			afterPageText: 'of {0}',
			beforePageText: 'Page',
			selectOnFocus:true,
			typeAhead: false,
			updateRawValue: function() {
				Ext.Ajax.request({
					scope: this,
					url: 'ajax.pl',
					params: {
						task: 'READCAT',
						id: this.value,
						lang: lang
					},
					success: function(result) {
						var response = Ext.util.JSON.decode(result.responseText);
						if (this.getValue() == 0) {
							this.setValue('');
						} else {
							this.store.add(
								new Ext.data.Record(
									{id:this.value, path:response.data.path}
								)
							);
							this.setValue(this.getValue());
						}
					}
				});
			},
			listeners: {
				render: function() {
					this.tip = tipDEF(this, capCategoryTooltip);
				}
			}
		});
		categoryFieldDEF.superclass.constructor.apply(this, arguments);
	};
	Ext.extend(categoryFieldDEF, Ext.form.ComboBox, {});
	
	// GEOGRAPHY FIELD DEFINITION
	var geographyFieldDEF = function(config) { 
		Ext.apply(this, config, {
			name: 'region',
			hiddenName: 'region',
			fieldLabel: 'Geography',
			displayField: 'path',
			valueField: 'id',
			forceSelection: true,
			store: new Ext.data.JsonStore({
				id: 'id',
				root: 'rows',
				totalProperty: 'total',
				fields: [
					'id', 'path'
				],
				url: 'ajax.pl',				
				baseParams: {
					task: "LISTINGGEO",
					lang: this.lang
				},
				autoLoad: true
			}),
			triggerAction: 'all',
			pageSize: 12,
			minChars: 3,
			loadingText: capSearching,
			emptyText:capChooseRegion,
			afterPageText: capOf+' {0}',
			beforePageText: capPage,
			selectOnFocus:true,
			typeAhead: false,
			updateRawValue: function(ulang) {
				if (typeof(ulang) == 'undefined') {
					ulang = lang;
				}
				Ext.Ajax.request({
					url: 'ajax.pl',
					params: {
						task: 'READGEO',
						id: this.value,
						lang: ulang
					},
					scope: this,
					success: function(result) {
						var response = Ext.util.JSON.decode(result.responseText);
						if (this.getValue() == 0) {
							this.setValue('');
						} else {
							this.store.add(
								new Ext.data.Record(
									{id:this.value, path:response.data.path}
								)
							);
							this.setValue(this.getValue());
						}
					}
				});
			},
			listeners: {
				render: function() {
					this.tip =	tipDEF(this, capRegionTooltip);
				}
			}
		});
		geographyFieldDEF.superclass.constructor.apply(this, arguments);
	};
	Ext.extend(geographyFieldDEF, Ext.form.ComboBox, {});

	var countryFieldDEF = function(form) {
		return new geographyFieldDEF({
			anchor: '100%', 
			fieldLabel: capCountry, 
			emptyText: capChooseCountry,
			name: 'country',
			hiddenName: 'country',
			listeners: {
				scope: this,
				change: function(o, newValue, oldValue) {
					if (newValue == greeceid || newValue == ukid || newValue == usid || newValue==cyprusid)  {
						form.region.enable();
						form.region.allowBlank = false;
						form.region.setValue(0);
						form.region.setRawValue('');
						form.region.store.baseParams.country = newValue;
						form.region.store.load({
							params: {
								country: newValue
							}
						});
					} else {
						form.region.setValue(0);
						form.region.setRawValue('');
						form.region.disable();
						form.region.allowBlank = true;
					}
				}
			}
		})
	};
	
	// Tooltip default definition
	function tipDEF(o, html) {
		return new Ext.ToolTip({
			target: o.getEl(),
			anchor: 'top',
			anchorOffset: 85,
			html: html
		});
	}

	// clear "loading" message
	Ext.get('submitFormDiv').dom.innerHTML = '';
	
	Ext.QuickTips.init();
	var loginForm = new loginFormDEF();
	var helpfile = 'loginhelp.html';
	if (lang == 'greek') {
		helpfile = 'loginhelp_gr.html';
	}
	var helpPanel = new helpPanelDEF({autoLoad: helpfile});

}); // eo function onReady


