function isInt(x) { var y=parseInt(x); if (isNaN(y)) return false; return x==y && x.toString()==y.toString(); }

var Wizard = Class.create({
	initialize: function(rootFieldset) {
		this.root       = $(rootFieldset);
		this.current    = 0;

		this.form = $(rootFieldset).parentNode;
		while(this.form.nodeName != 'FORM') {
			this.form = $(this.form).parentNode;
		}
		Event.observe(this.form, 'submit', this.submit.bindAsEventListener(this));

		this.pages = new Array();
		this.menuItems = new Array();
		var pageNum = 0;

		rootChildNodes = $A(this.root.childNodes);
		for (var i = 0, len_i = rootChildNodes.length; i < len_i; ++i) {
			if(rootChildNodes[i].nodeName == 'FIELDSET') {
				this.pages.push($(rootChildNodes[i]));

				rootChildNodes[i].style.display = 'none';

				innerChildNodes = $A(rootChildNodes[i].childNodes);

				pageNum++;
			}
		}

		this.backBtn = new Element('button', {'class': 'button', 'type': 'button'}).update('Back').insert({'top': new Element('span', {'class': 'ss_sprite ss_arrow_left'})});
		this.backBtn.style.cssFloat = this.backBtn.style.styleFloat = 'left';
		this.backBtn.observe('click', this.back.bindAsEventListener(this));
		this.nextBtn = new Element('button', {'class': 'button', 'type': 'button'}).update('Next').insert({'bottom': new Element('span', {'class': 'ss_sprite ss_arrow_right'})});
		this.nextBtn.style.cssFloat = this.nextBtn.style.styleFloat = 'right';
		this.nextBtn.observe('click', this.next.bindAsEventListener(this));

		this.btnDiv  = this.root.next('fieldset');

		this.submitBtn = this.btnDiv.childElements()[0];
		this.submitBtn.style.cssFloat = this.submitBtn.style.styleFloat = 'right';
		this.submitBtn.hide();

		this.btnDiv.insert({'top':this.nextBtn});
		this.btnDiv.insert({'top':this.backBtn});

		this.backBtn.hide();
		this.pages[0].style.display = 'block';
	},
	back: function(event) {
		if (this.current-1 < 0) {
			alert("Already at first Option");
		} else {
			this.pages[this.current].style.display   = 'none';
			this.pages[this.current-1].style.display = 'block';
			this.current = parseInt(this.current) - 1;
		}
		this.updateButtons();
		this.focusFirstField();
	},
	next: function(event) {
		if(this.current+1 >= this.pages.length) {
			alert("Already at last Option");
		} else {
			this.pages[this.current].style.display   = 'none';
	  	this.pages[this.current+1].style.display = 'block';
			this.current = parseInt(this.current) + 1;
		}
		this.updateButtons();
		this.focusFirstField();
	},
	jump: function(event) {
		nextPage   = event.element().value
		if(!isInt(nextPage)) nextPage = event.element().pageNumber;

		if(nextPage >= 0 && nextPage < this.pages.length) {
			this.pages[this.current].style.display   = 'none';
			this.pages[nextPage].style.display = 'block';
			this.current = parseInt(nextPage);
		} else {
			alert("Not a valid page");
		}
		this.updateButtons();
		this.focusFirstField();
	},
	focusFirstField: function() {
		var currentPage = this.pages[this.current];
		var fieldFocused = false;

		page = $A(currentPage.childNodes);
		for (var i = 0, len_i = page.length; i < len_i; ++i) {
			if(page[i].nodeName == 'DIV') {
				div = $A(page[i].childNodes);
				for (var j = 0, len_j = div.length; j < len_j; ++j) {
					if(div[j].nodeName == 'INPUT' || div[j].nodeName == 'SELECT') {
						div[j].focus();
						fieldFocused = true;
						break;
					}
				}
			} else if (page[i].nodeName == 'INPUT' || page[i].nodeName == 'SELECT') {
				page[i].focus();
				fieldFocused = true;
				break;
			}
			if(fieldFocused) {
				break;
			}
		}
	},
	updateButtons: function() {
		if(this.current <= 0) {
			this.backBtn.hide(); /* disabled = true; */
		} else {
			this.backBtn.show(); /* disabled = false; */
		}
		if(this.current >= (this.pages.length - 1)) {
			this.nextBtn.hide(); /* disabled = true; */
      this.submitBtn.show();
		} else {
			this.nextBtn.show(); /* disabled = false; */
      this.submitBtn.hide();
		}
	},
	submit: function(event) {
		if(this.current >= (this.pages.length - 1)) {
			/*validates = true;
			currentPage = this.current;
			for (var i = 0, len_i = this.pages.length; i < len_i; ++i) {
				this.current = parseInt(i);
				validates = validates && this.validatePage(false);
			}
			if(validates) { */
				//pass
			/*} else {
				Event.stop(event);
				alert('The data you have submitted is incorrect. Please Correct all of the items highlighted in red and then try again.');
			}*/
		} else {
			Event.stop(event);
			this.next();
		}
	}
});

