/*
 * PureTrak Login Page Javascript File
 * Copyright(c) 2008-2009, Pure Voice Communictions, Inc.
 * http://www.puretrak.com
 * @author Patrick Hallinan
 *
 */

Ext.onReady(function() {
	// -- Enable quick tips - feedback for any errors
	Ext.QuickTips.init();
	
	var login_form_panel = new Ext.FormPanel({
		id			: 'login_form_panel',
		title		: 'Login - Existing Customers',
		width		: 325,
		height		: 125,
		border		: true,
		frame		: true,
		//style		: {margin: '10px'},
		bodyStyle	: 'padding:5px',
		labelWidth	: 90,
		items		: [{
			xtype		: 'textfield',
			name		: 'login_contact_email',
			fieldLabel	: 'Email Address',
			width		: 190,
			allowBlank	: false,
			vtype		: 'email'
		},{
			xtype		: 'textfield',
			inputType	: 'password',
			name		: 'login_password',
			fieldLabel	: 'Password',
			width		: 125,
			allowBlank	: false
		}],
		buttons	: [{
			text	: 'Login',
			handler	: function() {
				login_form_panel.getForm().submit({
					url		: '/login/process_login',
					method	: 'POST',
					waitMsg	: 'Attempting to login...',
					success	: function(form, action) {
						window.location = action.result.return_url;
					},
					failure	: function(form, action) {
						if(action.failureType == 'server') {
							if(action.result.errors.invalid_login) {
								Ext.Msg.show({
									title	: 'Login Failed',
									msg		: 'ERROR: Invalid login credientials, please try again!',
									buttons	: Ext.Msg.OK,
									icon	: Ext.MessageBox.WARNING,
									minWidth: 350
								});
							}
							login_form_panel.getForm().reset();
						}
					}
				});
			}
		}]
	});
	
	var forgot_pw_panel = new Ext.FormPanel({
		id			: 'forgot_pw_form_panel',
		title		: 'Forgot your password?',
		width		: 325,
		height		: 160,
		border		: true,
		frame		: true,
		bodyStyle	: 'padding:5px',
		labelWidth	: 90,
		items		: [{
			html	: 'Please enter the Email Address for the account you wish to recover a password for.  Your login credientials will be Emailed to you.',
			style	: {marginBottom:'25px'}
		},{
			xtype		: 'textfield',
			name		: 'login_contact_email',
			fieldLabel	: 'Email Address',
			width		: 190,
			allowBlank	: false,
			vtype		: 'email'
		}],
		buttons	: [{
			text	: 'Retrieve Password',
			handler	: function() {
				forgot_pw_panel.getForm().submit({
					url		: '/login/forgot_password_routine',
					method	: 'POST',
					waitMsg	: 'Attempting to retrieve login credientials...',
					success	: function(form, action) {
						forgot_pw_panel.getForm().reset();
						Ext.Msg.show({
							title	: 'Account Information',
							msg		: 'Your account credientials have been Emailed to the address provided.',
							buttons	: Ext.Msg.OK,
							icon	: Ext.MessageBox.WARNING,
							minWidth: 350
						});
					},
					failure	: function(form, action) {
						if(action.failureType == 'server') {
							if(action.result.errors.invalid_account) {
								Ext.Msg.show({
									title	: 'No Record Found',
									msg		: 'ERROR: Unable to locate an account with the supplied Email Address, please try again!',
									buttons	: Ext.Msg.OK,
									icon	: Ext.MessageBox.WARNING,
									minWidth: 350
								});
							}
							forgot_pw_panel.getForm().reset();
						}
					}
				});
			}
		}]
	});
	
	var login_container_panel = new Ext.Panel({
		layout		: 'hbox',
		renderTo	: 'login_form_panel_container',
		autoWidth	: true,
		autoHeight	: true,
		bodyStyle	: 'background:transparent;padding:10px;',
		border		: false,
		items	: [
			login_form_panel,
		{
			xtype	: 'spacer',
			flex:1
		},
			forgot_pw_panel
		]
	});

	var task = {
		run: function() {
			
			var img_array = new Array(
				'products_ad_usps.gif',
				'products_ad_free_vent.gif'
			);
			
			var len = img_array.length;
			var random_number = Math.floor(len*Math.random());
			
			var el = Ext.get('products_ad_container');
				el.sequenceFx().fadeOut({
					easing:'easeOut',
					duration:1.5,
					remove:false,
					callback: function() {
						el.update('<img src="/assets/images/products/ads/'+img_array[random_number]+'" />')
					}
				}).fadeIn({duration: 1.5});
		},
		interval: 10000 // -- 10 seconds
	}
	
	Ext.TaskMgr.start(task);
});
