var AJAX_STAT_SUCCESS = "success";
var PACKAGE_STATUS_ERROR = "error";

function PowerbeltWizard(fieldName, stepCount) {
	this.fieldName = fieldName;
	this.stepCount = stepCount;
	this.baseUrl = 'index.php?page=WizardServer&type=';
	this.parameter = null;
	this.stepsData = {};
	this.currentStep = 0;
}

PowerbeltWizard.prototype.initializeWizard = function (wizardType, parameter) {
	this.url = this.baseUrl + wizardType;
	this.parameter = parameter;
	this.initializeSpecificWizard();
	this.loadCurrentStepData(-1);
}

PowerbeltWizard.prototype.loadCurrentStepData = function (idx) {
	var pData = jQuery.extend({}, this.stepsData);
	pData.parameter = this.parameter;
	pData.currentStep = idx;
	this.loadWizardData(pData, this, 'wizardDataLoaded');	
}

PowerbeltWizard.prototype.loadWizardData = function(pData, callbackObj, callbackFunctionName, overrideUrl) {
	var urlToLoad = this.url;
	if (overrideUrl != undefined) {
		urlToLoad = overrideUrl;
	}
	
	var self = this;
	jQuery.ajax({
		type: "GET",
		dataType: "json",
		url: urlToLoad,
		data: pData,
		complete: function(jsonData, stat) {
			if(stat == AJAX_STAT_SUCCESS) {
				var error = self.notifyCallbackAfterLoad(callbackObj, callbackFunctionName, jsonData);
				if ( error != null) {
					alert("Error:" + error);
				}
			} else {
				alert("Error:" + stat);
			}
		}
	});	
}

PowerbeltWizard.prototype.notifyCallbackAfterLoad = function(callbackObj, callbackFunctionName, jsonData) {
	var respObj = eval("(" + jsonData.responseText + ")");
	if (respObj.status == PACKAGE_STATUS_ERROR) {
		return respObj.errorMessage;
	} else {
		callbackObj[callbackFunctionName](respObj);
		return null;
	}
}

PowerbeltWizard.prototype.wizardDataLoaded = function(data) {
	this.currentStep = data.currentStep; 
	for(var i=0; i<data.stepsData.length; i++) {
		this.updateOneInput(i, data.stepsData[i]);
	}
	this.showStepMarkers(data.currentStep, data.stepsData.length);
	// ie6 anti-autoscroll fix
	if (data.currentStep != 0) {
		jQuery('#wizardStep' + data.currentStep).focus();
	}
	// implement in subclasses
	this.performPostLoadingOperations(data);
}

PowerbeltWizard.prototype.showStepMarkers = function(cs, l) {
	for(var i=0; i<=cs; i++) {
		jQuery('#wizardStepMarker' + i).show();
	}
	for(var i=cs + 1; i<=l; i++) {
		jQuery('#wizardStepMarker' + i).hide();
	}
}

PowerbeltWizard.prototype.updateOneInput = function(i, sd) {
	var id = this.fieldName + i;
	var jqid = '#' + id;
	if (sd.enabled) {
		jQuery(jqid).removeAttr("disabled");
	} else {
		jQuery(jqid).attr("disabled", "disabled");
	}
	
	if (sd.type == 'Dropdown') {
		jQuery(jqid).children().remove();
		for(var vc=0; vc <  sd.av.length; vc++) {
			var el = jQuery(document.createElement("option")).attr("value",sd.av[vc][0]).text(sd.av[vc][1]);
			jQuery(jqid).append(el);
		}
	}
	if (sd.data != null) {
		jQuery(jqid).val(sd.data);
	}
	
	if (!sd.valid) {
		alert(sd.validationMessage);
	}
}

PowerbeltWizard.prototype.dropdownChanged = function(idx) {
	this.stepsData = {};
	for(var i=0; i<= idx; i++) {
		var id = this.fieldName + i;
		var jqid = '#' + id;
		this.stepsData[this.fieldName + i] = jQuery(jqid).val();
	}
	this.loadCurrentStepData(idx);
}


PowerbeltWizard.prototype.textfieldChanged = function(idx) {
	this.stepsData = {};
	for(var i=0; i<= idx; i++) {
		var id = this.fieldName + i;
		var jqid = '#' + id;
		this.stepsData[this.fieldName + i] = jQuery(jqid).val();
	}
	this.loadCurrentStepData(idx);
}

PowerbeltWizard.prototype.textareaChanged = function(idx) {
	this.stepsData = {};
	for(var i=0; i<= idx; i++) {
		var id = this.fieldName + i;
		var jqid = '#' + id;
		this.stepsData[this.fieldName + i] = jQuery(jqid).val();
	}
	this.loadCurrentStepData(idx);
}

PowerbeltWizard.prototype.fillStepsData = function(currentStep, stepKeys) {
	//alert(stepKeys.toSource());
}

PowerbeltWizard.prototype.initializeSpecificWizard = function() {}


