/*

	Copyright 2007 - Artlogic Media Limited - http://www.artlogic.net/
	All rights reserved.

*/



/*	PAGES */

var home_loaded = false;


var pages = {
	
	/*
	
		load a 'page' of html

	
		to call a page, use one of the following syntaxes:
		
		pages.pagename()				-	loads the page with the specified page name, e.g. pages.recent_news()
		pages.load_content_el(el, url)	-	loads content into a specific element of the main 'container' element, revealing the container element
		pages.load_page_el(el, url)		-	loads content into a specific element of the current 'page' element, revealing the page element
		
		
		'pages.home()' is loaded at startup (see main.js)
		
		to create a custom function, duplicate and modify the 'load()' function, below
		
		
	*/
		
	
	//	pages
	
		home: function() {
			var el = 'profileBoxDisabledContent';
			var url = '/page/home';
			this.load_content_el(el, url);
            home_loaded = true;
		},
		
		recent_news: function() {
			var el = 'pages';
			var url = '/news/';
			this.load_page(el, url);
		},
		
		news_item: function(id) {
			var url = '/news/read_more/?id=' + id;
			this.load_page('newsStoryContents', url);
		},
		
		courses: function() {
			var el = 'pages';
			var url = '/courses/';
			this.load_page(el, url);
		},
		
		courses_read_more: function(id, institution_id) {
            removeClass($('coursePane'), 'disabled')
			var url = '/courses/read_more/?id=' + id;
			this.load_page('courseDetails', url);
			$('institution_logo_image').src = '/images/institutionlogos/institution' + institution_id + '.gif';
		},
		
		contact_us: function() {
			var el = 'pages';
			var url = '/page/contact_us';
			this.load_page(el, url);
		},

        update_contact_details_pane: function(url) {
            removeClass($('contactPane'), 'disabled')
            ie_safe_ajax_updater(url, 'contactDetails');
        },
		
		job_noticeboard: function() {
			var el = 'pages';
			var url = '/jobs';
			this.load_page(el, url);
		},

        submit_job: function() {
            if (current_user.logged_in) {
                var el = 'pages';
                var url = '/jobs/new';
                this.load_page(el, url);
            } else {
                alert("You must be logged in to use this feature.");
            }
        },
		
		links: function() {
			var el = 'pages';
			var url = '/page/links';
			this.load_page(el, url);
		},
		
		search_and_close_editor: function() {
            if (edit.editor_active) {
                edit.close_editor();
            }
            if(!home_loaded) {
                pages.home();
            }
			hide('pages');
			show('container');
		},

		search: function() {
			hide('pages');
			show('container');
		},
		
		register: function() {
			var el = 'pages';
			var url = '/registration/form';
			this.load_page(el, url);
		},
		
		terms: function() {
			var el = 'pages';
			var url = '/page/terms';
			this.load_page(el, url);
		},	

        reset_password: function() {
			var el = 'pages';
			var url = '/reset_password/email_form';
			this.load_page(el, url);
        },

        remove_account: function() {
			var el = 'pages';
			var url = '/remove_account';
			this.load_page(el, url);
        },

        do_remove_account: function() {
			var el = 'pages';
			var url = '/remove_account/do_it';
			this.load_page(el, url);
            sessions.logout(true);
        },

		faq: function() {
			var el = 'pages';
			var url = '/page/faq';
			this.load_page(el, url);
		},	
		
		
		
	//	functions used by the above

		load_page: function(el, url) {
			// loads a page into the 'page' element, hiding the main content
			hide('container');
			show('pages');
			this.load(el, url);
		},
	
		load_content_el: function(el, url) {
			// loads html into an element of the main content
			show('container');
			hide('pages');
			this.load(el, url);
		},
	
		load_page_el: function(el, url) {
			// loads html into an element within the current 'page' element
			hide('container');
			show('pages');
			this.load(el, url);
		},
		
		load: function(el, url) {
			new Ajax.Request(url, {
				method: 'get',
				asynchronous: true, 
				evalScripts: true,
				onComplete: function(req) {
                    var response = req.responseText;
                    var div = document.createElement('div');
                    div.innerHTML = response;
                    $(el).innerHTML = '';
                    $(el).appendChild(div);
				}
			});
		}
	

}

var email_delay_warning = "Please note that emails can take 30 minutes or so " + 
    "to arrive as many large email providers (like Yahoo, GMail and Hotmail) " +
    "deliberately delay incoming messages to help cut down spam.";

var registration = {
    submit_visitor_signup: function() {
        var success_message = "Your registration request has " +
        "been successful. You will soon receive an email with a link to " + 
        "confirm your registration.";
        this.post_form('visitor_signup_form', success_message);
        debug('called submit_visitor_signup');
    },

    submit_alumni_signup: function() {
        var success_message = "You request has been successful. Your will " +
        "soon receive an email with a confirmation link.";
        this.post_form('alumni_signup_form', success_message);
        debug('called submit_alumni_signup');
    },

    post_form : function(form_id, success_message) {
        var postBody = Form.serialize(form_id);
        set_cursor_style('progress');
        new Ajax.Request($(form_id).action, {
            method: 'post',
			postBody: postBody,
            asynchronous: true, 
            evalScripts: false,
            onComplete: function(req) {
                set_cursor_style('auto');
				var response = decodeJSON(req.responseText);
                if (response.error === null) {
                    alert(success_message + "\n\n" + 
                          email_delay_warning);
                    window.location = '/';
                } else {
                    alert(response.error);
                }
                debug(response);
            }
        });
    }
};

var reset_password = {
    request_reset_password: function() {
        this.post_form('reset_password_form', "We have sent you an email with a" +
        " link to reset your password. " + email_delay_warning);
        debug('called request_reset_password');
    },

    submit_reset_password: function() {
        this.post_form('reset_password_form', "Your password has been successfully modified.\nYou may now use your new password to log in.");
        debug('called submit_reset_password');
    },    
    
    post_form : function(form_id, success_message) {
        var postBody = Form.serialize(form_id);
        set_cursor_style('progress');
        new Ajax.Request($(form_id).action, {
            method: 'post',
			postBody: postBody,
            asynchronous: true, 
            evalScripts: false,
            onComplete: function(req) {
				var response = decodeJSON(req.responseText);
                set_cursor_style('auto');
                if (response.error === null) {
                    alert(success_message);
                    window.location = '/';
                } else {
                    alert(response.error);
                }
                debug(response);
            }
        });
    }
};

/*EOF*/
