/***********************************************************************
 *  Filename: mod25_wizard.js                                          *
 *  Date Created: 25JUL06                                              *
 *  Date Last Modified: 11AUG06                                        *
 *  Package: mod25                                                     *
 *  Type: Class definition                                             *
 *  Copyright © 2006 Mod25 Solutions, LLC. All Rights Reserved         *
 *  Notes: This class contains functions for creating a wizard.        *
 **********************************************************************/


/***********************************************************************
 * Requirements:                                                       *
 *      mod25_slide                                                    *
 *      mod25_events                                                   *
 *      mod25_net                                                      *
 **********************************************************************/
 
 
// Wizard object constructor
function mod25_wizard(sName) {
	// Local variables
		// The name of the wizard
		this.sName = sName;
		// The slide that is currently shown
		this.nSlideIndex = 0
		// The slides that make up the wizard
		this.slides = new Array();
		// Boolean that is true if the wizard has been started
		this.bStarted = false;

	return this;
}

// The number of slides contained in the wizard
mod25_wizard.prototype.nNumSlides = function() {
	// Return the number of slides added to the wizard
	return this.slides.length;
}

// Read the wizard properties
mod25_wizard.prototype.getProperties = function(sName) {
	// Check if there is a param tag
	var wizTag = document.getElementsByTagName('param');
	
	// check if any tags were returned
	if (!wizTag || wizTag.length <= 0)
		return false;
	
	// Loop through the returned tags looking for the wizard tag with the name of this wizard
	var i;
	for (i = 0; i < wizTag.length; i++) {
	
		// Check the name of the tag returned
		if (wizTag[i].getAttribute('NAME') == sName) {
			// Set the properties from the tag
			this.params = wizTag[i];
			this.sUrl = wizTag[i].getAttribute('URL');
		}
	}

	return true;
}

// Generate the form to be submitted
mod25_wizard.prototype.submitForm = function(async) {

	// Setup the url
	var formUrl = this.sUrl;
	var data = "";
	
	// Loop through the slides
	var i;
	for (i = 0; i < this.nNumSlides(); i++) {
		// Loop through the input components
		var a;
		for (a = 0; a < this.slides[i].input.length; a++) {
			// Add the input component name and value to the url
			data += escape(this.slides[i].input[a].name) + '=' + escape(this.slides[i].input[a].value);
			data += '&';
		}
	}
	
	if (async == true) {
		// Send the request
		mod25_net.sendRequest("GET", formUrl, data);
	} else {
		window.location = formUrl + data;
	}
	
	return true;
}

// Add a slide to the wizard
mod25_wizard.prototype.addSlide = function(sldNew) {	
	// Add the slide to the array
	this.slides[this.nNumSlides()] = sldNew;
	
	// Set this wizard as the parent of the slide
	sldNew.parent = this;

	return true;
}

// Start the wizard
mod25_wizard.prototype.startWizard = function() {

	// Check if any slides have added
	if (this.nNumSlides() <= 0) {
		return false;
	}
	
	// Get the properties for this wizard
	this.getProperties(this.sName);
	
	// Show the first slide
	this.slides[this.nSlideIndex].show();
	
	// Start the wizard
	this.bStarted = true;
		
	return true;
}

// Stop the wizard
mod25_wizard.prototype.stopWizard = function() {

	// Check if the wizard has been started
	if (this.bStarted == false) {
		return false;
	}
	
	// Hide the current slide
	this.slides[this.nSlideIndex].hide();
	
	// Set the slide index to 0
	this.nSlideIndex = 0;
	
	// Set the started boolean to false
	this.bStarted = false;
	
	return true;
}

// Hide the current slide and goto the specified slide
mod25_wizard.prototype.goSlide = function(iSlide) {
	// Check if the wizard has been started
	if (this.bStarted == false)
		return false;
		
	// Check that the specified slide is a valid slide
	if (iSlide < 0 || iSlide > (this.nNumSlides() - 1))
		return false;

	// Hide the current slide
	this.slides[this.nSlideIndex].hide();
	
	// Set the slide index to the specified slide
	this.nSlideIndex = iSlide;
	
	// Show the new slide
	this.slides[this.nSlideIndex].show();
	
	return true;
}

// Hide the current slide and show the next
mod25_wizard.prototype.nextSlide = function() {
	// Check if the wizard has been started
	if (this.bStarted == false)
		return false;

	// Check if we are at the end
	if (this.nSlideIndex >= (this.nNumSlides() - 1)) {
		return false;
	}
		
	// Hide the current slide
	this.slides[this.nSlideIndex].hide();
	
	// Increase the slide index
	this.nSlideIndex++;
	
	// Show the new slide
	this.slides[this.nSlideIndex].show();
	
	return true;
}

// Hide the current slide and show the previous
mod25_wizard.prototype.prevSlide = function() {
	// Check if the wizard has been started
	if (this.bStarted == false)
		return false;

	// Check if we are at the beginning
	if (this.nSlideIndex == 0)
		return false;
	
	// Hide the current slide
	this.slides[this.nSlideIndex].hide();
	
	// Decrease the slide index
	this.nSlideIndex--;
	
	// Show the new slide
	this.slides[this.nSlideIndex].show();
	
	return true;
}
