/**
 * 
 */
var Form = function(id) {
	var form = document.getElementById(id);
	this.form = form;
	this.form.className = this.form.className + " formWiz";
	this.findSubmitBtt = function(name) {
		for (i=0; i<form.elements.length; i++){
			if (form.elements[i].type=="submit") {
				form.elements[i].type = "hidden";
				return form.elements[i];
			}
		}
		return null;
	};
	
	this.submitButt = null;
	this.submitButt = this.findSubmitBtt("submit");
	
	this.sections = this.form.getElementsByTagName("fieldset");
	this.length = this.sections.length;
	for(var i = 1; i < this.length; ++i) {
		this.hide(this.sections[i]);
	}
	this.index = 0;
	this.createButtonNav();
	this.visibleSection = this.sections[0];
};

Form.prototype.show = function(section) {
	var tmp = document.createElement(section.tagName);
	section.style.display = tmp.style.display;
};
Form.prototype.hide = function(section) {
	section.style.display = "none";
};
Form.prototype.nextSection = function() {
	++this.index;
	if(this.index == this.length) {
		this.index = this.length - 1;
		alert("dalej nic nie ma");
	} else {
		this.showPrevButt(true);
		this.hide(this.visibleSection);
		this.show(this.sections[this.index]);
		this.visibleSection = this.sections[this.index];
		if(this.index == this.length - 1) {
			this.showSubmitButt(true);
		} 
	}
	return false;
};

Form.prototype.prevSection = function() {
	if(this.index == this.length - 1){	
		this.showSubmitButt(false);
	}
	--this.index;
	if(this.index < 0) {
		this.index = 0;
		alert("koniec");
	} else {
		
		this.hide(this.visibleSection);
		this.show(this.sections[this.index]);
		this.visibleSection = this.sections[this.index];
		
		if(this.index <= 0) {
			this.showPrevButt(false);
		}
	}
};

Form.prototype.createButtonNav = function() {
	//tworzenie kontenera
	this.buttonNav = document.createElement("div");
	this.buttonNav.className = "buttonNav";
	//tworzenie przycisku next
	var oForm = this;
	this.bNext = document.createElement("input");
	//this.bNext.type = "button";
	this.bNext.name = "next";
	this.bNext.title = "Dalej";
	this.bNext.type = "image";
	this.bNext.src = "/public/images/special/next.png";
	this.bNext.className = "rightButt";
	this.bNext.onclick = function(oEvent) {
		oEvent = oEvent || window.event;
		oForm.nextSection();
		if(oEvent.stopPropagation) {
			oEvent.stopPropagation();
		} else {
			oEvent.cancelBubble = true;
		} 
		return false;
	}
	//tworzenie przycisku prev
	this.bPrev = document.createElement("input");
	this.bPrev.type = "image";
	this.bPrev.name = "prev";
	this.bPrev.title = "Wróć";
	this.bPrev.src = "/public/images/special/back.png";
	this.bPrev.className = "leftButt";
	this.bPrev.style.display = "none";
	this.bPrev.onclick = function(oEvent) {
		oEvent = oEvent || window.event;
		oForm.prevSection();
		if(oEvent.stopPropagation) {
			oEvent.stopPropagation();
		} else {
			oEvent.cancelBubble = true;
		} 
		return false;
	}
	this.buttonNav.appendChild(this.bPrev);
	this.buttonNav.appendChild(this.bNext);
	this.form.appendChild(this.buttonNav);
}

Form.prototype.showSubmitButt = function(b) {
	if(b) {
		if(!this.submitButt) {
			this.submitButt = document.createElement("input");
			this.submitButt.type = "image";
			this.submitButt.title = "Wyślij";
			this.submitButt.name = "submitform";
		} else {
			var tmp = document.createElement("input");
			tmp.className = "rightButt";
			tmp.type = "image";
			tmp.name = "submitform";
			tmp.title = this.submitButt.value;
			tmp.src = "/public/images/special/accept.png";
			this.submitButt = tmp;
		}
		
		this.buttonNav.replaceChild(this.submitButt, this.bNext);
	} else {
		this.buttonNav.replaceChild(this.bNext, this.submitButt);
	}
	
}
Form.prototype.showPrevButt = function(b) {
	if(b) {
		this.show(this.bPrev);
		
	} else {
		this.hide(this.bPrev);
	}
	
}


