/************************************************************************************
	COMMON.JS
	
	Contains all of the code that is now shared among modules.  In particular, this
	file has a document.onReady handler that will install listeners for form field
	validation and for pagination.
	
	To allow this, individual pages must register a list of fields and pages.  For
	details on how field validation now works, see validators.js.  For details on
	how pagination now works, see below.
*************************************************************************************/

$(document).ready(function() {
	// Install validation listeners
	$('input, select, textarea').blur(function() { 	   
		validate_field(this);
	});
								  
	$('input, select, textarea').focus(function() {
		if (this.className.indexOf('error') != -1) {
			this.value = '';
		}
	});

	/************************************************************************************
		UPDATED 6-23-10 (Jordan)
	
		The way that pagination works is now considerably different.  Each page that wants
		to add pagination into the system must register a page into the global pages
		array.
		
		Each entry in the pages array must be an object of the following format:
		{
			id: '',						// The id of the div of the page
			must_validate: true,		// Whether or not the page must be validated, defaults to true
			next: function() {},		// The function to call when the user wants to proceed
			previous: function() {}		// The function to call when the user wants to go back
		}
		
		Next and previous should handle anything that needs to be done on a page
		transition from that page, but does not need to handle hiding / showing of the
		pages themselves.
		
		The next and previous functions are not passed any parameters.  To go to a new page,
		use go_to_page(id of page), which will handle hiding the current page, showing the new
		page, and updating #title-bar h2 with the correct title.
		
		If next or previous is not defined for a page, then that button will be hidden.
		
		The number shown at the top of each page can be controlled with increment_page_number
		and decrement_page_number.
	*************************************************************************************/
	$(".continue").click(function() {
		if (typeof(pages) == 'undefined' || pages.length == 0) {
			return;
		}
		
		for (var i = 0; i < pages.length; i++) {
			var page = pages[i];
			
			if ($('#' + page.id).is(':visible')) {
				var element = $('#' + page.id + '> div:visible:not(.buttons)');
		
				// Validate all fields on this page
				if (page.must_validate == true || typeof(page.must_validate) == 'undefined') {
					element.find('input, select').each(function() {
						validate_field(this);
					});
					$('#password').focus();
					$('#password').blur();
					$('#confirm-password').focus();
					$('#confirm-password').blur();
					
					if (element.find('.error').length) {
						return false;
					}
				}
				
				if (typeof(page.next) != "undefined") {
					page.next.call(this);
				}
				
				break;
			}
		}
	});
	
	$(".previous").click(function() {
		if (typeof(pages) == 'undefined' || pages.length == 0) {
			return;
		}
		
		for (var i = 0; i < pages.length; i++) {
			var page = pages[i];
			
			if ($('#' + page.id).is(':visible')) {
				if (typeof(page.previous) != "undefined") {
					page.previous.call(this);
				}
				
				break;
			}
		}
	});
});

function go_to_page(id) {
	var current_page;
	var next_page;
	
	for (var i = 0; i < pages.length; i++) {
		var page = pages[i];
		
		if ($('#' + page.id).is(':visible')) {
			current_page = page;
		}
		if (page.id == id) {
			next_page = page;
		}
	}
	
	$('#' + current_page.id).hide();
	$('#' + next_page.id).show();
	
	// Determine previous / next visibility
	if (typeof(next_page.next) == 'undefined') {
		$('.continue').hide();
	} else {
		$('.continue').show();
	}
	if (typeof(next_page.previous) == 'undefined') {
		$('.previous').hide();
	} else {
		$('.previous').show();
	}
	
	// Switch out the title, the navigation function can change this if desired
	if (typeof(next_page.title) != 'undefined') {
		$('#title-bar h2').html(next_page.title);
	}
}

// Sequentially increments the page number
function increment_page_number() {
	switch ($('#number').attr('src')) {
		case 'images/one.png':
			$('#number').attr('src', 'images/two.png');
			break;
		case 'images/two.png':
			$('#number').attr('src', 'images/three.png');
			break;
		case 'images/three.png':
			$('#number').attr('src', 'images/four.png');
			break;
	}
}

// Sequentially decrements the page numbe
function decrement_page_number() {
	switch ($('#number').attr('src')) {
		case 'images/four.png':
			$('#number').attr('src', 'images/three.png');
			break;
		case 'images/three.png':
			$('#number').attr('src', 'images/two.png');
			break;
		case 'images/two.png':
			$('#number').attr('src', 'images/one.png');
			break;
	}
}
