﻿var gimiProfilePanel;
var gimiLoginPanel;
var gimiRegisterPanel;
var gimiWhatsOnPanel;
var gimiWhatsOnTabPanel;
var navigationPanel;
var accessDeniedWindow;
var aboutPostcodeWindow;
var aboutBirthdateWindow;
var aboutBookmarkWindow;
var gimiAdvicePanel;

/**
 * Standard page initialisation using the Ext library
 */
Ext.onReady(function(){
    // Initialise the main "My Gimi" panel
    if (Ext.get('my-gimi')) {
        myGimiPanel = new Ext.Panel({
            collapsible: true,
            applyTo: 'my-gimi',
            height: 445
        });
    }
    
    // If not logged in, initialise the login panel
    if (Ext.get('gimi-login')) {
        gimiLoginPanel = new Ext.Panel({
            collapsible: true,
            applyTo: 'gimi-login'
        });
    }
    
    // If not logged in, initialise the registration panel
    if (Ext.get('gimi-register')) {
        gimiRegisterPanel = new Ext.Panel({
            collapsible: true,
            applyTo: 'gimi-register',
            autoShow: true
        });
        gimiRegisterPanel.hide();
    }
    
    // If logged in but editing profile the register panel makes a comeback
    // under an alternate ID!
    if (Ext.get('gimi-editprofile')) {
        gimiRegisterPanel = new Ext.Panel({
            collapsible: true,
            applyTo: 'gimi-editprofile'
        });
    }
    
    // If not logged in, initialise the what's on panel
    if (Ext.get('gimi-whats-on')) {
        gimiWhatsOnPanel = new Ext.Panel({
            collapsible: true,
            draggable: false,
            applyTo: 'gimi-whats-on'
        });
    }
    
    // Apply tabs to the what's on content
    if (Ext.get('gimi-whats-on-tabs')) {
        gimiWhatsOnTabPanel = new Ext.TabPanel({
            applyTo: 'gimi-whats-on-tabs',
            activeTab: 0,
            border: false,
            plain: true,
            deferredRender: false,
            autoTabs: true
        });
    }
    
    // If it exists (doesn't on all templates), initialise the
    // navigation
    if (Ext.get('navigation')) {
        navigationPanel = new Ext.Panel({
            collapsible: true,
            applyTo: 'navigation'
        });
    }
    
    if (Ext.get('advice-and-info')) {
        new Ext.Panel({
            layout: 'fit',
            collapsible: true,
            draggable: false,
            applyTo: 'advice-and-info'
        });
        
        if (Ext.get('advice-and-info-tabs')) {
            new Ext.TabPanel({
                applyTo: 'advice-and-info-tabs',
                activeTab: 0,
                border: false,
                plain: true,
                deferredRender: false,
                autoTabs: true,
                autoHeight: true
            });
        }
    }
});

/**
 * Called if a link is accessed that requires the user to be logged in.
 */
function accessDenied(control) {
    // Construct an Ext window and load in the image that identifies
    // that we need to log in.
    if (!accessDeniedWindow) {
        accessDeniedWindow = new Ext.Window({
            layout:'fit',
            width: 312,
            height: 374,
            closeAction: 'hide',
            plain: true,
            title: 'Not logged in?',
            
            buttons: [{
                text:'Sign in',
                handler: function(){
                    accessDeniedWindow.hide();
                    signIn();
                }
            },{
                text: 'Join Free',
                handler: function(){
                    accessDeniedWindow.hide();
                    joinGimi();
                }
            }]
        });
        
        accessDeniedWindow.html = '<div style="width: 302px; height: 364px; background: url(gimi/images/common/accessdenied.gif);">' +
            '<div style="display: none;">trying to find something better to do?  gimi.co.uk the social network that connects you with whats going on.  Sign in or join free.</div>' +
            '</div>';
    }
    
    accessDeniedWindow.show(control);
    return false;
}

/**
 * Should be called by clicking "Sign In" somewhere.
 */
function signIn() {
    if (gimiRegisterPanel) {
        gimiRegisterPanel.hide();
    }
    
    if (gimiLoginPanel) {
        gimiLoginPanel.show();
        
        // Focus the login panel
        gimiLoginPanel.focus();
        
        // Make sure the username field is focussed
        if (document.forms.lhsloginform) {
            document.forms.lhsloginform.username.focus();
            document.forms.lhsloginform.username.select();
        }
        
        return false;
    }
    
    // If got here then something went wrong - so allow the link to work
    return true;
}

/**
 * Should be called by clicking on a link to "Join" Gimi.  This prompts
 * the standard login panel to be swapped for a join Gimi panel.  The
 * user then fills in their details and attempts to register.
 */
function joinGimi() {
    // Hide the login panel and display the register panel
    if (gimiLoginPanel) {
        gimiLoginPanel.hide();
    }
    
    if (gimiRegisterPanel) {
        gimiRegisterPanel.show();
        
        // Focus the register panel
        gimiRegisterPanel.focus();
        
        if (document.forms.lhsjoinform) {
            document.forms.lhsjoinform.fullname.focus();
            document.forms.lhsjoinform.fullname.select();
        }
        
        return false;
    }

    // If got here then something went wrong - so allow the link to work
    return true;
}

/**
 * Displays and focusses the feedback form.
 */
function feedback() {
    if (gimiWhatsOnTabPanel) {
        // Display the feedback tab
        gimiWhatsOnTabPanel.activate(2);
        gimiWhatsOnTabPanel.focus();
    
        return false;
    }
    
    // If got here then didn't manage to do anything - so honour normal link
    // to form.
    return true;
}

/**
 * Called via the join form - pops up a window displaying info about
 * why the postcode needs to be filled in.
 */
function aboutPostcodePopup(control) {
    if(!aboutPostcodeWindow){
        aboutPostcodeWindow = new Ext.Window({
            el: 'about-postcode-win',
            layout: 'fit',
            width: 300,
            modal: true,
            closeAction: 'hide',
            plain: true
        });
    }
    aboutPostcodeWindow.show(control);

    return false;
}

/**
 * Called via the join form - pops up a window displaying info about
 * why the birthdate needs to be filled in.
 */
function aboutBirthdatePopup(control) {
    if(!aboutBirthdateWindow){
        aboutBirthdateWindow = new Ext.Window({
            el: 'about-birthdate-win',
            layout: 'fit',
            width: 300,
            modal: true,
            closeAction: 'hide',
            plain: true
        });
    }
    aboutBirthdateWindow.show(control);

    return false;
}

/**
 * On the bottom of every page is a bookmark this page link.  This
 * pops up a modal window with help about what the link is about.
 */
function aboutBookmarkPopup(control) {
    if(!aboutBookmarkWindow){
        aboutBookmarkWindow = new Ext.Window({
            el: 'about-bookmark-win',
            layout: 'fit',
            width: 300,
            modal: true,
            closeAction: 'hide',
            plain: true
        });
    }
    aboutBookmarkWindow.show(control);

    return false;
}

/**
* Used for the contribute event template form validation
*/
function validateFieldRegex(field, initValue, errMsg, Unit) {
	if (field == null) {
		return false;
	}
	var regex = null;
	switch (Unit) {
		case "Day":
			regex = (/([0-2][0-9])|(3[0-1])/);
			break;

		case "Month":
			regex = (/(0[0-9])|(1[0-2])/);
			break;

		case "Year":
			regex = (/[0-9][0-9][0-9][0-9]/);
			break;

		case "Hour":
			regex = (/([0-1][0-9])|(2[0-4])/);
			break;

		case "Minute":
			regex = (/[0-5][0-9]/);
			break;
	}
	if (field && ((field.value == "") || (field.value == initValue) || (field.value.match(regex) == null))) {
		alert(errMsg);
		field.focus();
		field.select();
		return false;
	} else {
		return true;
	}
}

/**
 * Checks the gimi registration form has been completed ok
 */
function validateField(field, initValue, errMsg) {
    if (field && ((field.value == "") || (field.value == initValue))) {
        alert(errMsg);
        field.focus();
        field.select();
        return false;
    } else {
        return true;
    }
}

/**
 * Checks the gimi registration form has been completed ok
 */
function validateRegistration(joinForm) {
    var initialChecks = (
        validateField(joinForm.fullname, "", "Please enter your name") &&
        validateField(joinForm.NUserName, "", "Please enter your email address") &&
        validateField(joinForm.NPassword, "", "Please enter your password") &&
        validateField(joinForm.NCPassword, "", "Please confirm your password") &&
        validateField(joinForm.Hint, "", "Please enter a hint") &&
        validateField(joinForm.postcode, "", "Please enter your postcode") &&
        validateField(joinForm.birthdayday, "dd", "Please enter your birth day") &&
        validateField(joinForm.birthdaymonth, "mm", "Please enter your birth month") &&
        validateField(joinForm.birthdayyear, "yyyy", "Please enter your birth year")
    );
    
    if (initialChecks) {
        // Check the passwords match
        if (joinForm.NPassword && (joinForm.NPassword.value != joinForm.NCPassword.value)) {
            // Doh!
            alert("Your passwords do not match");
            joinForm.NCPassword.focus();
            joinForm.NCPassword.select();
            
            return false;
        } else if (joinForm.npword && (joinForm.npword.value != joinForm.cnpword.value)) {
			// Doh 2!
            alert("Your passwords do not match");
            joinForm.cnpword.focus();
            joinForm.cnpword.select();
            
            return false;
        } else {
            // OK!
            return true;
        }
    } else {
        // Failed!
        return false;
    }
}
function processSelection(what) {
				var s = what.options[what.selectedIndex].value;
				var unit = null;
				if (what.selectedIndex == 0) {
					unit = "No recurrence";
				}
				if (what.selectedIndex == 1) {
					unit = "Months";
				}
				if (what.selectedIndex == 2) {
					unit = "Weeks";
				}
				if (what.selectedIndex == 3) {
					unit = "Days";
				}
				if(!s) return;
				document.getElementById('out').firstChild.nodeValue = "  " + unit;				
				if (what.selectedIndex != 0) {					
					document.getElementById('recurforwrapper').style.display = "block";
					document.getElementById('untilwrapper').style.display = "block";

				}				
				if (what.selectedIndex == 0) {
					document.getElementById('recurforwrapper').style.display = "none";
					document.getElementById('untilwrapper').style.display = "none";
				}				
			}
			

