if(typeof wizardControl == "undefined") {
	var wizardControl = function(){
		this.ID;
		this.dialog;
		this.step;
		this.steps;
		this.finish;
		this.redirectURL;
		
		var dialogObj;
		
		this.render = function(){
			var id = this.ID;
			var redirectURL = this.redirectURL;
			this.getSteps();
			dialogObj = new YAHOO.widget.Dialog("wizardDialog_" + this.ID, {
				fixedcenter: true,
				hideaftersubmit: false,
				close: false
			});
			dialogObj.callback.success = function(o){
				var finish = wizardControls[id].finish;
				if (finish == true) {
					if (redirectURL == "") {
						window.location.href = window.location.href;
					} else {
						window.location.href = redirectURL;
					}
				}
			};
			dialogObj.callback.failure = function(o){
				alert("Submit ERROR: " + o.statusText);
			};
			
		}
		
		this.refresh = function(){
			this.setupButtons();	
			var callback = {
				success: function(o){
					var body = o.responseText;
					
					dialogObj.setBody(body);
					dialogObj.render(document.body);
					
				/* This runs any SCRIPT tags found in the dialog
				 * I don't really know how to make this global.
				 * todo: move to a global location.
				 */
					var node = document.createElement("div");
					node.innerHTML = body;
					var scripts = node.getElementsByTagName("script");
					for (var x = 0; x < scripts.length; x++) {
						eval(scripts[x].innerHTML);
					}
					
				},
				
				failure: function(o){
					alert("ERROR: " + o.statusText);
				},
				
				cache: false
			}
			
			var transaction = YAHOO.util.Connect.asyncRequest("GET", "index.php?dialog=" + this.dialog + "&step=" + this.step, callback, null);
		}
		
		this.show = function(){
			this.step = 1;
			this.finish = false;
			this.refresh();
            this.refresh(); // Fix for a stupid issue...didn't load correctly the 1st time
			dialogObj.show();
			dialogObj.bringToTop();
		}
		
		this.hide = function(){
			dialogObj.hide();
		}
		
		this.getSteps = function() {
			var id = this.ID;
			var callback = {
				success: function(o){
					wizardControls[id].steps = o.responseText;	
				},
				
				failure: function(o){
					alert("getSteps ERROR: " + o.statusText);
				},
				cache: false
			}
			var transaction = YAHOO.util.Connect.asyncRequest("GET", "index.php?dialog=" + this.dialog + "&action=getSteps", callback, null);		
		}
		
		this.setupButtons = function() {
			var id = this.ID;
			if(this.step == 1 && this.steps == 1) {
				dialogObj.cfg.queueProperty("buttons", [{
					text: "Finish",
					handler: function(){
						wizardControls[id].finish = true;
						this.submit();
						this.hide();
					},
					isDefault: true
				}, {
					text: "Cancel",
					handler: function(){
                        var redirectURL = wizardControls[id].redirectURL;
                        if (redirectURL == "") {
                            window.location.href = window.location.href;
                        } else {
                            window.location.href = redirectURL;
                        }
						this.hide();
					}
				}]);
			}
			else if(this.step == 1 && this.steps > 1) {
				dialogObj.cfg.queueProperty("buttons", [{
					text: "Next",
					handler: function(){
						this.submit();
						if (wizardControls[id].step < wizardControls[id].steps) {
							wizardControls[id].step = wizardControls[id].step + 1;
							wizardControls[id].refresh();
						}
					},
					isDefault: true
				}, {
					text: "Cancel",
					handler: function(){
                        var redirectURL = wizardControls[id].redirectURL;
                        if (redirectURL == "") {
                            window.location.href = window.location.href;
                        } else {
                            window.location.href = redirectURL;
                        }
						this.hide();
					}
				}]);				
			}
			else if(this.step > 1 && this.step < this.steps) {
				dialogObj.cfg.queueProperty("buttons", [{
					text: "Back",
					handler: function(){
						this.submit();
						if (wizardControls[id].step > 1) {
							wizardControls[id].step = wizardControls[id].step - 1;
							wizardControls[id].refresh();
						}
					}
				}, {
					text: "Next",
					handler: function(){
						this.submit();
						if (wizardControls[id].step < wizardControls[id].steps) {
							wizardControls[id].step = wizardControls[id].step + 1;
							wizardControls[id].refresh();
						}
					},
					isDefault: true
				}, {
					text: "Cancel",
					handler: function(){
                        var redirectURL = wizardControls[id].redirectURL;
                        if (redirectURL == "") {
                            window.location.href = window.location.href;
                        } else {
                            window.location.href = redirectURL;
                        }
						this.hide();
					}
				}]);
				
			} else { // Final Step
				dialogObj.cfg.queueProperty("buttons", [{
					text: "Back",
					handler: function(){
						this.submit();
						if (wizardControls[id].step > 1) {
							wizardControls[id].step = wizardControls[id].step - 1;
							wizardControls[id].refresh();
						}
					}
				}, {
					text: "Finish",
					handler: function(){
						wizardControls[id].finish = true;
						this.submit();
						this.hide();
					},
					isDefault: true
				}, {
					text: "Cancel",
					handler: function(){
                        var redirectURL = wizardControls[id].redirectURL;
                        if (redirectURL == "") {
                            window.location.href = window.location.href;
                        } else {
                            window.location.href = redirectURL;
                        }
						this.hide();
					}
				}]);
				
			}
		}
	}	
}
if(typeof wizardControls == "undefined") {
	var wizardControls = [];
}