/*
 * @author Marcus Cavalcanti (www.marcuscavalcanti.net/blog)
 * @date 2009.04.28
 * @description Wizard forms without JS libraries.
 */

// global vars
var WIZARD_PREFIX 	 = 'wizard-';
var PREVIOUS_CONTROL_ID  = 'previous-wizard';
var NEXT_CONTROL_ID 	 = 'next-wizard';
var LOADING_ID 		 = 'loading';
var MENU_PREFIX 	 = 'menu-';
var MENU_ENABLE_BGCOLOR  = '#000000';
var MENU_DISABLE_BGCOLOR = '#FFFFFF';

// show specific wizard
function showWizard (pageNum) {
	toggleLoading();
	
	hideAllWizards();
	
	document.getElementById(WIZARD_PREFIX+pageNum).style.display = '';				
	
	changeCurrentMenuBehaviour();
	
	manageControls();				
	
	toggleLoading();
}

// change background color of current menu
function changeCurrentMenuBehaviour () {
	var flag	= true;
	var menu	= 1;
	
	while ( flag ) {
		var link = document.getElementById(MENU_PREFIX+menu);
		
		if ( link == null )
			flag = false;
		else
			link.style.background = MENU_DISABLE_BGCOLOR;
		
		menu++;
	}

	document.getElementById(MENU_PREFIX+getCurrentWizard()).style.background = MENU_ENABLE_BGCOLOR;				
}

// hide all wizards
function hideAllWizards () {
	var flag = true;
	var page = 1;		
			
	while ( flag ) {
		var wizard = document.getElementById(WIZARD_PREFIX+page);
		
		if ( wizard == null )
			flag = false;
		else
			wizard.style.display = 'none';
		
		page++;
	}
}

// display/hide loading message 
function toggleLoading() {
	var loadingStatus = document.getElementById('loading').style.display;
	
	if ( loadingStatus == 'none' ) 
		document.getElementById(LOADING_ID).style.display = '';
	else 
		document.getElementById(LOADING_ID).style.display = 'none';
}	

// get current selected wizard
function getCurrentWizard () {
	var flag	= true;
	var page	= 1;
	
	while ( flag ) {
		var wizard = document.getElementById(WIZARD_PREFIX+page);
		
		if ( wizard == null )
			flag = false;
		else
			if ( wizard.style.display != 'none' )
				var currentWizard = page;
		
		page++;
	}
	
	return currentWizard;
}

// go to next wizard
function nextWizard () {
	if ( existsNextWizard() ) 
		showWizard(getCurrentWizard()+1);
		
	manageControls();
}

// back to previous wizard
function previousWizard () {	
	if ( existsPreviousWizard() )
		showWizard(getCurrentWizard()-1);
		
	manageControls();
}

// manage "next" and "previous" buttons
function manageControls () {
	if ( !existsNextWizard() )
		document.getElementById(NEXT_CONTROL_ID).style.display = 'none';
	else
		document.getElementById(NEXT_CONTROL_ID).style.display = '';
		
	if ( !existsPreviousWizard() )
		document.getElementById(PREVIOUS_CONTROL_ID).style.display = 'none';
	else
		document.getElementById(PREVIOUS_CONTROL_ID).style.display = '';
}

// verify if exists a next wizard
function existsNextWizard () {
	var nextWizardNum = getCurrentWizard() + 1;
	var nextWizard    = document.getElementById(WIZARD_PREFIX+nextWizardNum);
	
	if ( nextWizard != null ) 
		return true;
	else
		return false;
}

// verify if exists a previous wizard
function existsPreviousWizard () {
	var previousWizardNum = getCurrentWizard() - 1;
	
	if ( previousWizardNum > 0 )
		return true;
	else 
		return false;
}
