
function FieldError(origin, mesg, val) {
	this.nameOrId = origin;
	this.errorMesg = mesg;
	this.errorValue = val;
}

var errorHandler = new Object();

errorHandler.errorList = new Array();

errorHandler.ERROR_TYPE_VALIDATION = "validationError";
errorHandler.ERROR_TYPE_EXCEPTION = "exceptionError";

// default to validation error
errorHandler.errorType = this.ERROR_TYPE_VALIDATION;

errorHandler.setErrorType = function(errorType) {
	errorHandler.errorType = errorType;
}

errorHandler.dateFormatExamplesShown = false;

errorHandler.addError = function(element, errorMesg, dateFormatExamples) {
	if (dateFormatExamples) {
		if (errorHandler.dateFormatExamplesShown == false) {
			errorMesg += ' ' + dateFormatExamples;
			errorHandler.dateFormatExamplesShown = true;
		}
	}
	
	if (element) {
		errorHandler.errorType = this.ERROR_TYPE_VALIDATION;
		var fieldErr;
		if (typeof element == "object") {
			fieldErr = new FieldError(element.id, errorMesg, element.value);
		} else if (typeof element == "string") {
			fieldErr = new FieldError(element, errorMesg, null);
		}
		this.errorList.push(fieldErr);
	} else {
		this.errorList.push(errorMesg);
	}
}

errorHandler.showErrors = function(contextPath) {
    var paramNames  = new Array();
    var paramValues = new Array();

    paramNames[0]  = 'errorType';
    paramValues[0] = this.errorType;
	// highlight errored fields
	if (this.errorType == this.ERROR_TYPE_VALIDATION) {
		if (this.errorList.length == 0) {
		 	return;
		}
		errorHighLightHandler.showAllHighLight(this.errorList);
	}
	for (var i = 0; i < this.errorList.length; i++) {	
		var err = this.errorList[i];
		paramNames.push('errorMessage');
		if (typeof err == 'object') {
			paramValues.push(err.errorMesg);
		} else {
			paramValues.push(err);
		}
	}
	if(this.onShowError) {
		this.onShowError();
	}
	if(contextPath) {
		dialogUtil.showDialog('errorDialog', contextPath + '/displayError.htm', 400, 250, paramNames, paramValues);
	} else {
		dialogUtil.showDialog('errorDialog', 'displayError.htm', 400, 250, paramNames, paramValues);	
	}
	
}

errorHandler.showError = function(eName) {
	if (this.errorList.length == 0 || !eName) {
		return;
	}
	// highlight errored fields
	errorHighLightHandler.showElementHighLight(eName);

	var paramNames  = new Array();
	var paramValues = new Array();

	for (var i = 0; i < this.errorList.length; i++) {
		var fieldErr = this.errorList[i];
		if(eName == fieldErr.nameOrId) {
		    paramNames.push('errorMessage');
		    paramValues.push(fieldErr.errorMesg);
		}
	}
	if(this.onShowError) {
		this.onShowError();
	}	
	dialogUtil.showDialog('errorDialog', 'displayError.htm', 400, 250, paramNames, paramValues);
}

errorHandler.getAllErrors = function() {
	return this.errorList;	
}

errorHandler.deleteAllErrors = function() {
	this.errorList = new Array();	
}

errorHandler.deleteError = function(name) {
	var i = 0;
	var found = false;
	for (i = 0; i < this.errorList.length; i++) {
		if (this.errorList[i].nameOrId == name) {
			found = true;
			break;	
		}
	}
	if(found) {
		for(; i < this.errorList.length - 1; i++) {
			this.errorList[i] = this.errorList[i + 1]; 
		}
		this.errorList.pop();
	}
}

errorHandler.printAllError = function() {
	var i = 0;
	for(i = 0; i < this.errorList.length; i++) {
		Console.println(this.errorList[i].nameOrId+"|"+this.errorList[i].errorMesg);
	}
}

errorHandler.hasError = function() {
	return (this.errorList.length > 0);	
}

errorHandler.hasValidationError = function() {
	return this.hasError() && (this.errorType == this.ERROR_TYPE_VALIDATION);
}

errorHandler.clearAllErrorsAndHighlights = function() {
	if (this.errorType == this.ERROR_TYPE_VALIDATION) {
		errorHighLightHandler.clearAllErrorHighLight();
	}
	this.deleteAllErrors();
}

errorHandler.onShowError = null; // callback on show error