LoginWindow = function() {
	this.fp = new Ext.form.FormPanel({
		autoScroll: true,
		defaultType: 'textfield',
		labelWidth: 80,
		labelAlign: 'right',
		items:
		[{
			id: 'username',
			fieldLabel:'Nickname',
			name:'data[SiteUser][username]',
			allowBlank:false
		},{
			fieldLabel:'Password',
			name:'data[SiteUser][password]',
			inputType:'password',
			allowBlank:false
		}],
		bbar: [
			{
				text: 'Login',
				handler: this.doLogin,
				scope: this
			},
			{
				text: 'Cancel',
				handler: function() {
					this.close();
				},
				scope: this
			}
		]
	});
	LoginWindow.superclass.constructor.call(this,{
		id: 'login-window',
		width: 280,
		height: 140,
		modal: true,
		layout: 'fit',
		title: 'Login',
		items: [ this.fp ]
	});
}
Ext.extend(LoginWindow, Ext.Window, {
	doLogin: function() {
		if ( this.fp.getForm().isValid() ) {
			this.fp.getForm().submit({
				waitMsg: 'Logging in...',
				waitTitle: 'Login',
				url: HOME_DIR+'site_users/login',
				scope: this,
				success: function(frm,action) {
					var data = action.response.responseText.evalJSON(true);
					if ( data.success ) {
						this.close();
						/*
						new Ajax.Updater('top_menu',HOME_DIR+'site_users/top_menu');
						new Ajax.Request(HOME_DIR+'site_users/join_box', {
							method: 'get',
							onSuccess: function(transport) {
								$('join').hide();
								$('top_login').hide();
								$('header').insert(transport.responseText);
							}
						});
						*/
						document.location.href =HOME_DIR;
					}
					else {
						new Ext.Msg.alert('Login Error', data.message);
					}
				},
				failure: function(f,a) {
					Ext.Msg.show({
						title: 'Error!',
						msg: a.result.message,
						buttons: Ext.MessageBox.OK,
						icon: Ext.Msg.ERROR
					});
				}
			});
		}
	}
});
ForgotPasswordWindow = function() {
	this.fp = new Ext.form.FormPanel({
		autoScroll: true,
		defaultType: 'textfield',
		labelWidth: 80,
		labelAlign: 'right',
		items: [
			{
				name: 'email',
				fieldLabel: 'Email',
				minLength: 3,
				allowBlank:false,
				vtype: 'email',
				width: 200
			}
		],
		bbar: [
			{
				text: 'Send my password',
				handler: this.sendPassword,
				scope: this
			},
			{
				text: 'Cancel',
				handler: function() {
					this.close();
				},
				scope: this
			}
		]
	});
	ForgotPasswordWindow.superclass.constructor.call(this,{
		id: 'forgot-password',
		width: 350,
		height: 100,
		modal: true,
		layout: 'fit',
		title: 'Forgot Password',
		items: [ this.fp ]
	});
};
Ext.extend(ForgotPasswordWindow,Ext.Window,{
	sendPassword: function() {
		this.fp.getForm().submit({
			waitMsg: 'Sending Reminder...',
	                waitTitle: 'Forgot Password',		
			url: HOME_DIR+'site_users/forgotpassword',
			success: function(f,a) {
				if ( a.result.success ) {
					Ext.Msg.alert('Password Sent','Your password has been sent successfully');
					this.close();
				}
				else {
					Ext.Msg.show({
						title: 'Error!',
						msg: a.result.error.reasons,
						buttons: Ext.MessageBox.OK,
						icon: Ext.Msg.ERROR
					});
				}
			},
			failure: function(f,a) {
				Ext.Msg.show({
					title: 'Error!',
					msg: a.result.error.reasons,
					buttons: Ext.MessageBox.OK,
					icon: Ext.Msg.ERROR
				});
			},
			scope: this
		});
	}
});
function forgotPassword() {
	var fpWin = new ForgotPasswordWindow();
	fpWin.show();
}
function openLoginWindow() {
	var lWin = new LoginWindow();
	lWin.show();
}
function login() {
	new Ext.form.BasicForm('login_form').submit({
		waitMsg: 'Logging in...',
		waitTitle: 'Login',
		success: function(frm,action) {
			var data = action.response.responseText.evalJSON(true);
			if ( data.success ) {
				/*
			  	new Ajax.Updater('login_box',HOME_DIR+'site_users/login_box');
			  	new Ajax.Updater('top_menu',HOME_DIR+'site_users/top_menu');
			  	new Ajax.Updater('homepage_banners',HOME_DIR+'site_pages/homepage_banners');
				new Ajax.Request(HOME_DIR+'site_users/join_box', {
					method: 'get',
					onSuccess: function(transport) {
						$('join').hide();
						$('header').insert(transport.responseText);
					}
				});
				*/
				document.location.href = HOME_DIR;
				
			}
			else {
				new Ext.Msg.alert('Login Error', data.message);
			}
		},
		failure: function(f,a) {
			Ext.Msg.show({
				title: 'Error!',
				msg: a.result.message,
				buttons: Ext.MessageBox.OK,
				icon: Ext.Msg.ERROR
			});
		}
	});
	/*
	$('login_form').request({
	  onSuccess: function(transport){ 
	  	var data = transport.responseText.evalJSON(true);
		if ( data.success ) {
		  	new Ajax.Updater('login_box',HOME_DIR+'site_users/login_box');
			//new Ajax.Updater('join',HOME_DIR+'site_users/join_box');
			new Ajax.Request(HOME_DIR+'site_users/join_box', {
				method: 'get',
				onSuccess: function(transport) {
					$('join').hide();
					$('header').insert(transport.responseText);
				}
			});
		}
		else {
			alert(data.message);
			$('login_form').appear();
			$('login_form').highlight();
		}
	  }
	})	
	*/
}


  function submitOnEnter(e) {
            var ENTER_KEY = 13;
            var code = "";
      
            if (window.event) // IE
            {
                code = window.event.keyCode;
            }
            else if (e.which) // Netscape/Firefox/Opera
            {
                code = e.which;
            }
          
            if (code == ENTER_KEY) {
		login();
            }
      }           
      
      

Ext.onReady(function() {
	if ( $('password') ) {
		$('password').onkeypress = submitOnEnter;
	}
});
