/* form Object*/
function form() {
	/* properties */
	this.steps = new Array();
	/* methods */
	this.addFormStep = function(step) {
		this.steps[this.steps.length] = step;
	};
	this.addFormStepIndex = function(step, index) {
		for(var i = this.steps.length; i > index; i--) {
			this.steps[i] = this.steps[i-1];
		}
		this.steps[index] = step;
	};
	this.removeFormStepIndex = function(index) {
		var length = this.steps.length;
		for(var i = index; i < length; i++) {
			this.steps[i] = this.steps[i+1];
		}
		this.steps[length - 1] = null;
	};
	this.getIndexById = function(id) {
		for(var i = 0; i < this.steps.length; i++) {
			if(this.steps[i].id == id) {
				return i;
			}
		}
		return -1;
	}
	this.getStepByIndex = function(index) {
		return this.steps[index];
	}
	this.goToStepByIndex = function(index) {
		for(var i = 0; i < this.steps.length; i++) {
			if(i == index) {
				this.steps[i].show();
			} else {
				this.steps[i].hide();
			}
		}
	}
	this.initialise = function(currentStepIndex) {
		for(var i = 0; i < this.steps.length; i++) {
			var step = this.steps[i];
			var thisForm = this;
			if(step.next) {
				step.next.click(function(e){
					e.preventDefault();
					var formStepId = $(this).parent().parent().parent().parent().attr('id');
					var thisIndex = thisForm.getIndexById(formStepId);
					var nextIndex = thisForm.getIndexById(formStepId) + 1;
					var thisStep = thisForm.getStepByIndex(thisIndex);
					var nextStep = thisForm.getStepByIndex(nextIndex);
					if(thisStep.validate()){
						thisStep.hide();
						nextStep.show();
						if(nextStep.summary != null) {
							nextStep.summarise();
						}
					}
				});
			}
			if(step.previous) {
				step.previous.click(function(e){
					e.preventDefault();
					var formStepId = $(this).parent().parent().parent().parent().attr('id');
					var thisIndex = thisForm.getIndexById(formStepId);
					var prevIndex = thisForm.getIndexById(formStepId) - 1;
					var thisStep = thisForm.getStepByIndex(thisIndex);
					var prevStep = thisForm.getStepByIndex(prevIndex);
					thisStep.hide();
					prevStep.show();
				});
			}
			if(step.finished) {
				step.finished.click(function(e){
					e.preventDefault();
					thisForm.submit();
				});
			}
			if(i != currentStepIndex) {
				step.hide();
			}
		}
	}
}

/* formStep Object */
function formStep(pageBandId) {
	/* properties */
	this.id = pageBandId;
	this.pageBand = $('div#' + pageBandId);
	this.next = null;
	this.validation = $('div#' + pageBandId + ' > div > ul > li.validation');
	this.validate = function() { return true; };
	if($('div#' + pageBandId + ' > div > ul > li > button.next').length) {
		this.next = $('div#' + pageBandId + ' > div > ul > li > button.next');
	}
	this.previous = null;
	if($('div#' + pageBandId + ' > div > ul > li > button.previous').length) {
		this.previous = $('div#' + pageBandId + ' > div > ul > li > button.previous');
	}
	this.finished = null;
	if($('div#' + pageBandId + ' > div > ul > li > button.finished').length) {
		this.finished = $('div#' + pageBandId + ' > div > ul > li > button.finished');
	}
	this.summarise = function() { };
	this.summary = null
	if($('div#' + pageBandId + ' > div > dl#summaryList').length) {
		this.summary = $('div#' + pageBandId + ' > div > dl#summaryList');
	}
	this.submit = function() { };
	/* methods */
	this.hide = function() {
		this.pageBand.hide();
	};
	this.show = function() {
		this.pageBand.show();
	};
}
