//member/detail_new.php
// BYC Main Menu
//
	
	Ext.QuickTips.init();
	
	imagePath = '/images/';
	scriptPath = '/components/';

	// Get Location of images
	loggedInUser = getCookie('lid');
	userName = getCookie('firstName');
	
	menuDisable = false;
	lip = '';
	
	// Set Menu based on logon state 
	function menuState(state){
		// We are logged in
		if(state==true){
			// Setup the menu item state
    		Ext.getCmp('mnuCommittees').enable();
    		Ext.getCmp('mnuMembers').enable();
    		Ext.getCmp('mnuTreasurer').enable();
    		Ext.getCmp('mnuFleetCaptain').enable();
    		Ext.getCmp('mnuAnnual').enable();
    		Ext.getCmp('mnuFloatPlan').enable();
    		Ext.getCmp('mnuBylaws').enable();
    		Ext.getCmp('mnuTowline').enable();
    		Ext.getCmp('mnuPicnic').enable();
    		Ext.getCmp('mnuCruises').enable();
    		Ext.getCmp('mnuEmergency').enable();
    		Ext.getCmp('mnuBYCColumnists').enable();
    		
    		// Setup the button State
    	    Ext.getCmp('logbutton').setText('Log-Off');
    	    Ext.getCmp('userName').setText('Welcome ' + getCookie('firstName'));
		}
		// We are not logged in
		else{
			// Set the menu item state
    		Ext.getCmp('mnuCommittees').disable();
    		Ext.getCmp('mnuMembers').disable();
    		Ext.getCmp('mnuTreasurer').disable();
    		Ext.getCmp('mnuFleetCaptain').disable();
    		Ext.getCmp('mnuAnnual').disable();
	   		Ext.getCmp('mnuFloatPlan').disable();
    		Ext.getCmp('mnuBylaws').disable();
    		Ext.getCmp('mnuTowline').disable();
    		Ext.getCmp('mnuPicnic').disable();
    		Ext.getCmp('mnuCruises').disable();
    		Ext.getCmp('mnuEmergency').disable();
    		Ext.getCmp('mnuBYCColumnists').disable();
    		
    		// Set the button State
			Ext.getCmp('logbutton').setText('Log-in');
    	    Ext.getCmp('userName').setText('');
    	    
    	    Ext.getCmp('mnuAdmin').setVisible(false);
		}
	};
	
//////////////////////////////////////////////////////
	
//////////////////// Main Menu Bar /////////////////////

	// Menu for what's going on in the club
	var menuAbout = new Ext.menu.Menu({
		id: 'menuAbout',
		items: [{
			text: 'BYC By Year',
			handler: function() {
				window.location='/history/byyear.php';
			}
		},{
			text: 'History',
			handler: function() {
				window.location='/bycHistory.html';
			},
			menu: {
				items: [
				    {
					text: 'BYC',
					handler: function() {
						window.location='/bycHistory.html';
					}
				},{
					text: 'Skipperettes',
					handler: function() {
						window.location='/skipperettes/history/skiphistory.html';
					}
				}]
			}
		},{
			text: 'Youth Program',
			handler: function() {
				window.location='/committees/sail/youth/';
			}
		},'-',{
			text: 'Reciprocal Clubs',
			handler: function() {
				window.location='/recip/RecipClubs.html';
			}
		},'-',{
			text: 'New Membership',
			handler: function() {
				window.location='/newmembers.html';
			}
		}]
	});
	
	// Menu for the Outside Activities
	var menuFacilities = new Ext.menu.Menu({
		id: 'menuFacilities',
		items: [{
			text: 'Clubhouse',
			handler: function() {
				window.location='/bycClubhouse.html';
			}
		},{
			text: 'Oro Bay',
			handler: function() {
				window.location='/OroBay.html';
			}
		},{
			text: 'Railway',
			handler: function() {
				window.location='/shipshape/railway.php';
			}
		},{
			text: 'Waste Water',
			handler: function() {
				window.location='/committees/wastewater.php';
			}
		},'-',{
			text: 'Emergency Response',
			id: 'mnuEmergency',
			disabled: menuDisable,
			handler: function() {
				window.location='/safety/intro.php';
			}
		}]
	});
	
	// Menu for Events
	var menuEvents = new Ext.menu.Menu({
		id: 'menuEvents',
		items: [{
			text: 'Upcoming Events',
			handler: function() {
				window.location='/events/currentevents.php';
			}
		},{
			text: 'Past Events',
			handler: function() {
				window.location='/events/pastevents.php';
			}
		},'-',{
			text: 'Awards Night',
			handler: function() {
				window.location='/events/awards.html';
			}
		},{
			text: 'BYC Annual Lighted Boat Parade',
			handler: function() {
				window.location='/events/xmasships.html';
			}
		},{
			text: 'Calendar',	
			handler: function() {
				window.location='/calendar/';
			}
		},{
			text: 'Cruises',	
			id: 'mnuCruises',
			disabled: menuDisable,
			handler: function() {
				window.location='/committees/fleetcapt.php';		// Login Required
			}
		},{
			text: 'Heavy Weather',
			handler: function() {
				window.location='/heavy_weather/index.html';
			}
		},{
			text: 'Oktoberfest',	
			handler: function() {
				window.location='/events/oktfest.php';
			}
		},{
			text: 'Opening Days',
			handler: function() {
				window.location='/OpeningDay/od09.html';
			}
		},{
			text: 'Youth Sailing',
			id: 'mnuYouthSailing',
			handler: function() {
				window.location='/committees/sail/youth/';
			}
		},{
			text: 'Picnics in the Park ',					// Login Required *
			id: 'mnuPicnic',
			disabled: menuDisable,
			handler: function() {
				window.location='/events/picnics.php';
			},
			menu: {
				items: [
				    '<b class="menu-title"><CENTER>Photos of Picnics</CENTER></b>',
				    {
					text: 'Jun 16, 2010',					// Login Required *
					handler: function() {
						window.location='/events/picnics/10/jun/index.php';
					}
				}]
			}
		}]
	});

	// Menu for Management
	var menuManagement = new Ext.menu.Menu({
		id: 'menuManagement',
		items: [{
			text: 'Bridge Officers',
			handler: function() {
				window.location='/bridge/bridge.html';
			}
		},{
			text: 'Secretary',
			handler: function() {
				window.location='/club/secretary.php/';
			}
		},{
			text: 'Treasurer',
			id: 'mnuTreasurer',
			disabled: menuDisable,
			handler: function() {
				window.location='/club/ecomm/treasurer.php';
			}
		},{
			text: 'Fleet Captain',		// Login Required *
			id: 'mnuFleetCaptain',
			disabled: menuDisable,
			handler: function() {
				window.location='/committees/fleetcapt.php';
			}
		},{
			text: 'Fleet Surgeon',
			handler: function() {
				window.location='/club/surgeon.php';
			}
		},{
			text: 'Fleet Chaplain',
			handler: function() {
				window.location='/member/chaplain.php';
			}
		}]
	});

	// Menu for Committees						// All Require Login
	var menuCommittees = new Ext.menu.Menu({	// Login Required *
		id: 'menuCommittees',
		items: [{
			text: 'BYC Employees',				// Login Required *
			handler: function() {
				window.location='/club/ecomm/employees.php';
			}
		},{
			text: 'Corrosion Control',			// Login Required *
			handler: function() {
				window.location='/committees/corrosion';
			}
		},{
			text: 'E-Committee',				// Login Required *
			handler: function() {
				window.location='/committees/ecomm/ecomm.html';
			}
		},{
			text: 'Environmental',				// Login Required *
			handler: function() {
				window.location='/committees/environment.php';
			}
		},{
			text: 'Fleet Captain',				// Login Required *
			handler: function() {
				window.location='/club/fc.html';
			}
		},{
			text: 'Float',						// Login Required *
			handler: function() {
				window.location='/club/FloatComm10.php';
			}
		},{
			text: 'Railway',					// Login Required *
			handler: function() {
				window.location='/committees/rail/';
			}
		},{
			text: 'Regatta for Power',
			handler: function() {
				window.location='/bycPowerBoat.html/';
			}
		},{
			text: 'Regatta for Sail',
			handler: function() {
				window.location='/committees/sail/';
			},
			menu: {
				items: [
				    {
					text: 'WSSA Races',
					handler: function() {
						window.location='/committees/sail/wssa/';
					}
				},{
					text: 'Youth Program',
					handler: function() {
						window.location='/committees/sail/youth/';
					}
				}]
			}
		},{
			text: 'Waste Water',				// Login Required *
			handler: function() {
				window.location='/committees/wastewater';
			}
		},{
			text: 'Web Team',
			handler: function() {
				window.location='/web/info.html';
			}
		},{
			text: 'Work Parties',				// Login Required *
			handler: function() {
				window.location='/shipshape/workparties.html';
			}
		}]
	});

	// Menu for Members Only
	var menuMembers = new Ext.menu.Menu({		// All Require Login
		id: 'menuMembers',
		items: [{
			text: 'Bylaws',						// Login Required *
			handler: function() {
				window.location='/club/bylaws/byc.php';
			}
		},{
			text: 'Crossing the Bar',	
			handler: function() {
				window.location='/crossingbar.html';
			}
		},{
			text: 'Float Map',					// Login Required *
			handler: function() {
				window.location='/club/floats.php';
			}
		},{
			text: 'Forms',						// Login Required *
			handler: function() {
				window.location='/club/forms.php';
			}
		},{
			text: 'Fuel',						// Login Required *
			handler: function() {
				window.location='/committees/fuel.php';
			}
		},{
			text: 'Member Lists',					// Login Required *  
			handler: function() {
				window.location='/member/detail_new.php';
			},
			menu: {
				items: [
				    {
						text: 'List of Members',				// Login Required *
						handler: function() {
							window.location='/member/detail_new.php';
				    	}
					},{
						text: 'List of Chairs',				// Login Required *
						handler: function() {
							window.location='/member/listofchairs.html';
				    	}
					},{
						text: 'Member Details (Excel)',		// Login Required *
						handler: function() {
							window.location='/club/ecomm/memapr10.xls';
						}
					}]
			}
		},{
			text: 'Parking',						// Login Required *
			handler: function() {
				window.location='/shipshape/parking.html';
			}
		},{
			text: 'This Years Photos',						// Login Required *
			handler: function() {
				window.location='/club/photos/';
			}
		},{
			text: 'Points',						// Login Required *
			handler: function() {
				window.location='/club/points/monthly.php';
			}
		},{
			text: 'Skipperettes',				// Login Required *
			handler: function() {
				window.location='/skipperettes';
			},
		menu: {
				items: [
				    {
						text: 'Main Page',				// Login Required *
						handler: function() {
							window.location='/skipperettes';
				    	}
					},{
						text: 'Skupperettes',				// Login Required *
						handler: function() {
							window.location='/skipperettes/history/skupps.html';
				    	}
					},'-',{
						text: 'History - Overview',		// Login Required *
						handler: function() {
							window.location='/skipperettes/history/skiphistory.html';
						}
					},{
						text: 'History - Detailed',		// Login Required *
						handler: function() {
							window.location='/skipperettes/history/skiphist.html';
						}
					},{
						text: 'History - Part I',		// Login Required *
						handler: function() {
							window.location='/skipperettes/history/skiphistI.html';
						}
					},{
						text: 'History - Part II',		// Login Required *
						handler: function() {
							window.location='/skipperettes/history/skiphistII.html';
						}
					},{
						text: 'History - Part III',		// Login Required *
						handler: function() {
							window.location='/skipperettes/history/skiphistIII.html';
						}
					},{
						text: 'History - Part IV',		// Login Required *
						handler: function() {
							window.location='/skipperettes/history/skiphistIV.html';
						}
					},{
						text: 'History - Part V',		// Login Required *
						handler: function() {
							window.location='/skipperettes/history/skiphistV.html';
						}
					},'-',{
						text: 'Past Presidents',		// Login Required *
						handler: function() {
							window.location='/skipperettes/history/skippres.html';
						}
					}]
			}
		},{
			text: 'Yachts',						// Login Required *
			handler: function() {
				window.location='/club/yachts.php';
			}
		},'-',{
			text: 'Edit Account',					// Login Required *
			handler: function() {
				window.location='/account/index.php';
			}
		},{
			text: 'Admin',						// Login Required *
			id: 'mnuAdmin',
			hidden: true,
			handler: function() {
				window.location='/admin/admin.php';
			},
			menu: {
				items: [{
					text: 'Users',						// Login Required *
						handler: function() {
						window.location='/admin/users.php';
					}
				},{
					text: 'Roles',						// Login Required *
						handler: function() {
						window.location='/admin/roles.php';
					}
				}]
			}
		}]
	});

	// Menu for Publications
	var menuPublications = new Ext.menu.Menu({
		id: 'mnuPublications',
		items: [{
			text: 'Annual',						// Login Required *
			id: 'mnuAnnual',
			disabled: menuDisable,
			handler: function() {
				window.location='/annual/index.php';
			}
		},{
			text: 'Bylaws',						// Login Required *
			id: 'mnuBylaws',
			disabled: menuDisable,
			handler: function() {
				window.location='/club/bylaws/byc.php';
			}
		},{
			text: 'Towline',					// Login Required *
			id: 'mnuTowline',
			disabled: menuDisable,
			handler: function() {
				window.location='/towline/';
			}
		},{
			text: 'Float Plan',					// Login Required *
			id: 'mnuFloatPlan',
			disabled: menuDisable,
			handler: function() {
				window.location='/club/floats.php';
			}
		},'-',{
			text: 'BYC Columnists',				// Login Required *
			id: 'mnuBYCColumnists',
			disabled: menuDisable,
			handler: function() {
				window.location='/club/columnists/index.php';
			}
		},{
			text: 'BYC In The News',			// Login Required *
			id: 'mnuBYCNews',
			disabled: menuDisable,
			handler: function() {
				window.location='/press/inthenews.html';
			}
		},{
			text: 'Press Releases',
			handler: function() {
				window.location='/press/';
			}
		}]
	});

	// Menu for External Links
	var menuLinks = new Ext.menu.Menu({
		id: 'menuLinks',
		items: [{
			text: 'Grand Fourteen',
			handler: function() {
				window.location='/events/clubs.html';
			}
		},{
			text: 'Internet Resources',
			handler: function() {
				window.location='/bycLinks.html';
			}
		},{
			text: 'Tide Charts',
			handler: function() {
				window.location='/tides.html';
			}
		},{
			text: 'Weather Links',
			handler: function() {
				window.location='/weather.html';
			}
		},'-',{
			text: 'BYC Archives',
			handler: function() {
				window.location='/indexpage.html';
			}
		},{
			text: 'BYC Site Map',
			handler: function() {
				window.location='/sitemap.html';
			}
		},{
			text: 'Google BYC',
			handler: function() {
				window.location='/googlebyc.html';
			}
		},'-',{
			text: 'Sponsors',
			handler: function() {
				window.location='/ads/index.html';
			}
		}]
	});
	
function loginWindow(win){
    
	// Validate User
	inputUserName = Ext.getCmp('username').getValue();
	inputUserName = inputUserName.replace(' ', '');
	inputUserName = inputUserName.replace('/', '');
	inputUserName = inputUserName.replace('\\', '');
	inputUserName = inputUserName.replace(':', '');

	inputPassword = Ext.getCmp('password').getValue();
	inputPassword = inputPassword.replace(' ', '');
	inputPassword = inputPassword.replace('/', '');
	inputPassword = inputPassword.replace('\\', '');
	inputPassword = inputPassword.replace(':', '');
	lip = inputPassword
	
	// Check Login information
	Ext.Ajax.request({
		waitMsg: 'Logging in...',
		showLoadIndicator: true,
		url: scriptPath + "authacc.php?action=login&username=" + inputUserName + "&password=" + inputPassword,
		success: loginsuccess,
		failure: logfailed
	})
	win.hide();
};

function loginsuccess(response){
	
	// Get returned message
	temp = response.responseText;
	
	// Get status of login attempt
	parStart = temp.search(':') + 1;
	parEnd = temp.search('`');
	logStatus = temp.substring(parStart,parEnd);
	temp = temp.substring(parEnd+1);
	
	// See if we have a valid login
	if(logStatus == 'true') {
	
		//Setup for Successful Login
	    Ext.getCmp('logbutton').setText('Log-Off');
		
		// get users loginID
		parEnd = temp.search('`');
		logName = temp.substring(0,parEnd);
		temp = temp.substring(parEnd+1);
		setCookie('lid', logName, 30);
		
		// get users First Name
		parEnd = temp.search('`');
		firstName = temp.substring(0,parEnd);
		temp = temp.substring(parEnd+1);
		setCookie('firstName', firstName, 30);
		Ext.getCmp('userName').setText('Welcome ' + firstName);
		
		// get users Last Name
		parEnd = temp.search('`');
		lastName = temp.substring(0,parEnd);
		temp = temp.substring(parEnd+1);
		setCookie('lastName', lastName, 30);
		
		// get users Role
		parEnd = temp.search('`');
		role = temp.substring(0,parEnd);
		temp = temp.substring(parEnd+1);
		setCookie('rid', role, 30);
		
		// Set Menu State
		if(logState=='yes'){
			loggedIn = true;
			menuState(true);
			if(role & 1){
				Ext.getCmp('mnuAdmin').setVisible(true);
			}
		}
		
		//Determine the state settings
		st = getCookie('st');
		if(Ext.getCmp('rdUsername').getValue()) st1 = '1';
		if(Ext.getCmp('rdPassword').getValue()) st1 = '2';
		if(Ext.getCmp('rdNone').getValue()) st1 = '';
		if(st != st1){
			setCookie('st', st1, 30);
			if(st1 == '1'){
				setCookie('lid', logName, 30);
				setCookie('ep', '', 30);
			}
			else if(st1 == '2'){
				setCookie('lid', logName, 30);
				un = logName.toLowerCase();
				ep = doSecure(un, lip);
				setCookie('ep', ep, 30);
			}
			else{
				setCookie('lid', '', 30);
				setCookie('ep', '', 30);
			}
		}
		lip = '';
	    
	    //Setup Menus for Login
	    menuState(true);
	}
	else{
		alert(temp);
		menuState(false);
		lip = '';
	}
		
};

function logoffsuccess(response){
	
	// Get returned message
	temp = response.responseText;
	
	// Get status of returned value
	returnStatus = temp.substring(temp.search(':')+1,temp.search(','));
	Ext.getCmp('mnuAdmin').setVisible(false);
	if(returnStatus == 'true') {
		Ext.getCmp('userName').setText('');
	}
	lip = '';
	setCookie('rid', 0, 30);
};

function logfailed(response){
	
	// Get returned message
	temp = response.responseText;
	
	alert('Login Failed, please try again.');
	menuState(false);
	lip = '';
};

	// Create Main Menu Object
	var mainMenu = new Ext.Toolbar({
		baseCls: 'x-menu',
		id: 'mainMenu',
		items: [{
			text:'About BYC',
			iconCls: 'bmenu',
			arrowAlign: 'bottom',
			menu: menuAbout
			},'-',{
				text:'Facilities',
				iconCls: 'bmenu',
				arrowAlign: 'bottom',
				menu: menuFacilities  	// assign menu by instance
			},'-',{
				text:'Events',
				iconCls: 'bmenu',
				arrowAlign: 'bottom',
				menu: menuEvents  		// assign menu by instance
			},'-',{
				text:'Management',
				iconCls: 'bmenu',
				arrowAlign: 'bottom',
				menu: menuManagement  	// assign menu by instance
			},'-',{
				text:'Committees',
				id: 'mnuCommittees',
				iconCls: 'bmenu',
				arrowAlign: 'bottom',
				disabled: menuDisable,
				menu: menuCommittees  	// assign menu by instance
			},'-',{
				text:'Members',
				id: 'mnuMembers',
				iconCls: 'bmenu',
				arrowAlign: 'bottom',
				disabled: menuDisable,
				menu: menuMembers  		// assign menu by instance
			},'-',{
				text:'Publications',
				iconCls: 'bmenu',
				arrowAlign: 'bottom',
				menu: menuPublications  // assign menu by instance
			},'-',{
				text:'Links',
				iconCls: 'bmenu',
				arrowAlign: 'bottom',
				menu: menuLinks  		// assign menu by instance
			},'->',{
				xtype: 'label',
				id: 'userName',
				text:''
			},'-','-',{
				xtype: 'button',
				id: 'logbutton',
				iconCls: 'bmenu',
				text:'Log-in',
				arrowAlign: 'bottom',
				height: 35,
				width: 80,
				handler: function(){
					if (Ext.getCmp('logbutton').getText() == 'Log-in'){
						var win = new Ext.Window({
						    layout: 'absolute',
						    title: 'Please Login',
						    width: 270,
						    height: 325,
						    html: '<CENTER>Forgotten or misplaced<BR>your username or password<BR><A href="/password.html">click here</CENTER></A>',
						    closeAction: 'hide',
						    deferredRender: false,
							shadow: true,
							shadowOffset: 10,
						    border: true,
							items: [{
					            xtype: 'label',
					            text: 'Username:',
					            x: 50,
					            y: 68,
					            width: 150
							},{
					            xtype: 'textfield',
					            id: 'username',
					            emptyText: 'Enter Username',
					            name: 'user-name',
					            x: 50,
					            y: 85,
					            width: 150
							},{
					            xtype: 'label',
					            text: 'Password:',
					            x: 50,
					            y: 118,
						        width: 150
							},{
					            xtype: 'textfield',
					            id: 'password',
					            inputType: 'password',
					            name: 'password',
					            x: 50,
					            y: 135,
						        width: 150
							},{
					            xtype: 'radio',
					            id: 'rdUsername',
					            name: 'pwSave',
					            x: 50,
					            y: 170
							},{
					            xtype: 'label',
					            text: 'Only Save Username',
					            x: 67,
					            y: 173,
						        width: 150
							},{
					            xtype: 'radio',
					            id: 'rdPassword',
					            name: 'pwSave',
					            x: 50,
					            y: 190
							},{
					            xtype: 'label',
					            text: 'Save Both',
					            x: 67,
					            y: 193,
						        width: 150
							},{
					            xtype: 'radio',
					            id: 'rdNone',
					            name: 'pwSave',
					            checked: true,
					            x: 50,
					            y: 210
							},{
					            xtype: 'label',
					            text: 'Do Not Save',
					            x: 67,
					            y: 213,
						        width: 150
							}],
						    buttons: [{
						        text: 'Login',
						        id: 'btnLogin',
						        handler: function(){
						    		loginWindow(win);
					            	win.hide();
					        	}
						    },{
						        text: 'Cancel',
						        id: 'btnLogCancel',
						        handler: function(){
						            win.hide();
						        }
						    }]
						});
						win.show(this);
						
						// Setup Login based on Previous Settings
						st = getCookie('st');
						un = getCookie('lid');
						if( st == '1' ){		// Saved Username only
							Ext.getCmp('rdUsername').setValue(true);
							Ext.getCmp('username').setValue(un);
						}
						else if( st == '2'){	// Saved Username & Password
							Ext.getCmp('rdPassword').setValue(true);
							Ext.getCmp('username').setValue(un);
							un = un.toLowerCase();
							ep = getCookie('ep');
							pw = doUnsecure(un, ep);
							pw = pw.replace(' ', '');
							Ext.getCmp('password').setValue(pw);
						}
					}
					else{
						Ext.getCmp('logbutton').setText('Log-in');
						menuState(false);
						
						// Log-Off
						Ext.Ajax.request({
							waitMsg: 'Logging off...',
							showLoadIndicator: true,
							url: scriptPath + "authacc.php?action=logoff",
							success: logoffsuccess,
							failure: logfailed
						})
					}
				}
			}]
	});
	
	// Get Session Information from Server
	Ext.Ajax.request({
		url: scriptPath + "authacc.php?action=session&name=auth",
		success: function(response){
		
			// Get Authentication Response	
			temp = response.responseText;
		
			// Get Login State
			parEnd = temp.search('`');
			logState = temp.substring(0,parEnd);
			temp = temp.substring(parEnd+1);
			
			// Get Role
			parEnd = temp.search('`');
			role = temp.substring(0,parEnd);
			
//			alert('Role: ' + role);
			
			// Set Menu State
			if(logState=='yes'){
				loggedIn = true;
				menuState(true);
				Ext.getCmp('userName').setText('Welcome ' + getCookie('firstName'));
				if(role & 1){		// Admin Role
					Ext.getCmp('mnuAdmin').setVisible(true);
				}
			}
			else{
				menuState(false);
			}
    	},
    	failure:  function(response){ 
			menuState(false);
    	}
	})
